package com.zbf.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zbf.common.annotation.Log;
import com.zbf.common.core.controller.BaseController;
import com.zbf.common.core.domain.AjaxResult;
import com.zbf.common.core.page.TableDataInfo;
import com.zbf.common.enums.BusinessType;
import com.zbf.common.utils.poi.ExcelUtil;
import com.zbf.system.domain.TCkOrderdetail;
import com.zbf.system.domain.TCkOrders;
import com.zbf.system.domain.TCkPickingwavegoods;
import com.zbf.system.domain.TOngoodsshelf;
import com.zbf.system.service.ITCkPickingwavegoodsService;
import com.zbf.web.controller.section.EnhanceDataList;

/**
 * 出库任务Controller
 *
 * @author tony
 * @date 2024-06-14
 */
@RestController
@RequestMapping("/system/pickingwavegoods")
public class TCkPickingwavegoodsController extends BaseController {
    @Autowired
    private ITCkPickingwavegoodsService tCkPickingwavegoodsService;

    /**
     * 查询出库任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:pickingwavegoods:list')")
    @EnhanceDataList(entityType = TCkPickingwavegoods.class)
    @GetMapping("/list")
    public TableDataInfo list(TCkPickingwavegoods tCkPickingwavegoods) {
        startPage();
        // tCkPickingwavegoods.setDeptId(getDeptId());
        // SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        // Date now = new Date();
        // String formattedDate = formatter.format(now);
        // tCkPickingwavegoods.setWareDate(formattedDate);
        List<TCkPickingwavegoods> list = tCkPickingwavegoodsService.selectTCkPickingwavegoodsList(tCkPickingwavegoods);
        return getDataTable(list);
    }

    /**
     * 查询出库任务列表(不分页)
     */
    //@PreAuthorize("@ss.hasPermi('system:pickingwavegoods:list')")
    @GetMapping("/selectList")
    public AjaxResult selectList(TCkPickingwavegoods tCkPickingwavegoods) {
        // tCkPickingwavegoods.setDeptId(getDeptId());
        List<TCkPickingwavegoods> list = tCkPickingwavegoodsService.selectTCkPickingwavegoodsList(tCkPickingwavegoods);
        return success(list);
    }


    /**
     * 导出出库任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:pickingwavegoods:export')")
    @Log(title = "出库任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCkPickingwavegoods tCkPickingwavegoods) {
        List<TCkPickingwavegoods> list = tCkPickingwavegoodsService.selectTCkPickingwavegoodsList(tCkPickingwavegoods);
        ExcelUtil<TCkPickingwavegoods> util = new ExcelUtil<TCkPickingwavegoods>(TCkPickingwavegoods.class);
        util.exportExcel(response, list, "出库任务数据");
    }

    /**
     * 获取出库任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:pickingwavegoods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(tCkPickingwavegoodsService.selectTCkPickingwavegoodsById(id));
    }

    /**
     * 新增出库任务
     */
    @PreAuthorize("@ss.hasPermi('system:pickingwavegoods:add')")
    @Log(title = "出库任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCkPickingwavegoods tCkPickingwavegoods) {
        logger.info("新增出库请求：{}", tCkPickingwavegoods);
        tCkPickingwavegoods.setDeptId(getDeptId());
        return toAjax(tCkPickingwavegoodsService.insertTCkPickingwavegoods(tCkPickingwavegoods));
    }

    /**
     * 修改出库任务
     */
    @PreAuthorize("@ss.hasPermi('system:pickingwavegoods:edit')")
    @Log(title = "出库任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCkPickingwavegoods tCkPickingwavegoods) {
        logger.info("修改出库任务开始：{}", tCkPickingwavegoods);
        return toAjax(tCkPickingwavegoodsService.updateTCkPickingwavegoods(tCkPickingwavegoods));
    }

    /**
     * 删除出库任务
     */
    @PreAuthorize("@ss.hasPermi('system:pickingwavegoods:remove')")
    @Log(title = "出库任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        logger.info("删除出库任务列表：{}", ids);
        return toAjax(tCkPickingwavegoodsService.deleteTCkPickingwavegoodsByIds(ids));
    }

    /**
     * 启用
     */
    @PreAuthorize("@ss.hasPermi('system:pickingwavegoods:remove')")
    @Log(title = "出库任务", businessType = BusinessType.UPDATE)
    @DeleteMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable String[] ids) {
        return toAjax(tCkPickingwavegoodsService.disabledTCkPickingwavegoodsByIds(ids));
    }

    /**
     * 禁用
     */
    @PreAuthorize("@ss.hasPermi('system:pickingwavegoods:edit')")
    @Log(title = "出库任务", businessType = BusinessType.UPDATE)
    @DeleteMapping("/enable/{ids}")
    public AjaxResult enable(@PathVariable String[] ids) {
        return toAjax(tCkPickingwavegoodsService.enableTCkPickingwavegoodsByIds(ids));
    }

    /**
     * 修改状态
     */
    @PreAuthorize("@ss.hasPermi('system:pickingwavegoods:edit')")
    @Log(title = "出库任务", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TCkPickingwavegoods tCkPickingwavegoods) {
        return toAjax(tCkPickingwavegoodsService.updateStatus(tCkPickingwavegoods));
    }

    /**
     * 更改物料货架信息
     */
    @PreAuthorize("@ss.hasPermi('system:pickingwavegoods:edit')")
    @Log(title = "更改任务状态", businessType = BusinessType.UPDATE)
    @PostMapping("/handleUpdateStatus/{ids}")
    public AjaxResult handleUpdateStatus(@PathVariable List<String> ids, @RequestBody TCkPickingwavegoods tCkPickingwavegoods) {
        logger.info("更改出库任务状态开始：{}", tCkPickingwavegoods);
        return toAjax(tCkPickingwavegoodsService.updateStatusByPickingids(ids, tCkPickingwavegoods));
    }

    /**
     * 新增出库任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:pickingwavegoods:add')")
    @Log(title = "新增出库任务列表", businessType = BusinessType.INSERT)
    @PostMapping("addTask")
    public AjaxResult addTask(@RequestBody List<TCkOrderdetail> tCkOrderdetailList) {
        logger.info("新增出库单出库请求列表：{}", tCkOrderdetailList);
        return toAjax(tCkPickingwavegoodsService.insertTCkPickingwavegoodsList(tCkOrderdetailList));
    }

    /**
     * 确认出库
     */
    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody List<TCkPickingwavegoods> goodsList) {
        logger.info("确认出库：{}", goodsList);
        return toAjax(tCkPickingwavegoodsService.confirm(goodsList));
    }

    /**
     * 首页出入库未完成数据统计
     * @return
     */
    @GetMapping("/home")
    public AjaxResult home() {

        Map<String, Integer> combinedResults = new HashMap<>();

        TCkPickingwavegoods pickingwavegoods = new TCkPickingwavegoods();
        pickingwavegoods.setDeptId(getDeptId());

        TOngoodsshelf tOngoodsshelf = new TOngoodsshelf();
        tOngoodsshelf.setDeptId(getDeptId());

        TCkOrders tCkOrders = new TCkOrders();

        combinedResults.put("out", tCkPickingwavegoodsService.homePickingwavegoodsCount(pickingwavegoods));
        combinedResults.put("in", tCkPickingwavegoodsService.homeOngoodsshelfCount(tOngoodsshelf));
        combinedResults.put("audit", tCkPickingwavegoodsService.homeTCkOrdersCount(tCkOrders));

        return AjaxResult.success(combinedResults);
    }
}

