package com.ruoyi.web.controller.business;

import com.ruoyi.business.domain.*;
import com.ruoyi.business.service.*;
import com.ruoyi.business.utils.OrderCodeFactory;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 品质检验Controller
 *
 * @author wms
 * @date 2022-05-29
 */
@Controller
@RequestMapping("/business/qualityInspect")
public class QualityInspectController extends BaseController {
    private String prefix = "business/qualityInspect";

    @Autowired
    private ITMiStockService tMiStockService;

    @Autowired
    private ITCkPickingwavegoodsService tCkPickingwavegoodsService;

    @Autowired
    private ITCkPickingwavegoodsBackService tCkPickingwavegoodsBackService;

    @Autowired
    private ITCkOrderdetailService tCkOrderdetailService;

    @Autowired
    private ITConveyorStatusService itConveyorStatusService;

    @RequiresPermissions("business:qualityInspect:view")
    @GetMapping()
    public String main(ModelMap mmap)
    {
        // 获取当前用户
        SysUser sysUser = ShiroUtils.getCurrentUser();

        TConveyorStatus tConveyorStatus = new TConveyorStatus();
        // 用户登录IP
        tConveyorStatus.setPcip(ShiroUtils.getIp());
        sysUser.setLoginIp(ShiroUtils.getIp());

        List<TConveyorStatus> tConveyorStatusList = itConveyorStatusService.selectTConveyorStatusList(tConveyorStatus);
        if (tConveyorStatusList != null && tConveyorStatusList.size() > 0) {
            tConveyorStatus = tConveyorStatusList.get(0);
            // 获取用户登录的站台号
            sysUser.setOutStand(tConveyorStatus.getConveryorId());
        }
        mmap.put("sysUser", sysUser);
        mmap.put("tCkPickingwavegoods", new TCkPickingwavegoods());
        return prefix + "/qualityInspect";
    }

    /**
     * 查询退货信息列表
     */
    @RequiresPermissions("business:qualityInspect:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TCkPickingwavegoods tCkPickingwavegoods)
    {
        startPage();
        List<TCkPickingwavegoods> list = tCkPickingwavegoodsService.selectTCkPickingwavegoodsList(tCkPickingwavegoods);
        return getDataTable(list);
    }

    /**
     * 添加品质检验任务
     */
    @RequiresPermissions("business:qualityInspect:add")
    @Log(title = "品质检验", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TCkOrderdetail tCkOrderdetail)
    {
        // start 检查当前站台是否有正在出库的任务
        TCkPickingwavegoods tCkPickingwavegoods = new TCkPickingwavegoods();
        tCkPickingwavegoods.setOutstand(tCkOrderdetail.getOutStand());
        List<TCkPickingwavegoods> outTaskList = tCkPickingwavegoodsService.selectTCkPickingwavegoodsList(tCkPickingwavegoods);
        if (outTaskList.size() > 0) {
            return success("-2");
        }
        // end 检查当前站台是否有正在出库的任务
        TMiStock tMiStock = new TMiStock();
        tMiStock.setGoodsId(tCkOrderdetail.getGoodId());
        tMiStock.setProduclotid(tCkOrderdetail.getPutinId());
        List<TMiStock> list = tMiStockService.selectTMiStockOutList(tMiStock);
        if (list.size() > 0) {
            tCkOrderdetail.setCkId(OrderCodeFactory.getOrderCode("ZJ", ""));
            tCkOrderdetail.setPickNum(BigDecimal.ZERO);
            tCkOrderdetail.setStatus("0");
            tCkOrderdetail.setAddtime(new Date());
            // 获取用户名
            SysUser sysUser = ShiroUtils.getCurrentUser();
            tCkOrderdetail.setUserName(sysUser.getUserName());
            return toAjax(tCkOrderdetailService.insertTCkOrderdetail(tCkOrderdetail));
        }

        return success("-1");
    }

    /**
     * 弹框表格
     */
    @GetMapping("/modalPage")
    public String table()
    {
        return prefix + "/stockSelect";
    }

    /**
     * 查询出库用库存列表
     */
    @RequiresPermissions("business:qualityInspect:list")
    @PostMapping("/query")
    @ResponseBody
    public TableDataInfo query(TMiStock tMiStock)
    {
        startPage();
        List<TMiStock> list = tMiStockService.selectStock(tMiStock);
        return getDataTable(list);
    }

    /**
     * 回库
     */
    @Log(title = "返库", businessType = BusinessType.UPDATE)
    @PostMapping("/backStock")
    @ResponseBody
    public AjaxResult backStock(TCkPickingwavegoods tCkPickingwavegoods)
    {
        tCkPickingwavegoods.setOutstatus("4");
        List<TCkPickingwavegoods> waitForBackTaskList = tCkPickingwavegoodsService.selectTCkPickingwavegoodsList(tCkPickingwavegoods);
        if (waitForBackTaskList.size() < 1) {
            return success("0");
        }
        TCkPickingwavegoods backTask = waitForBackTaskList.get(0);
        backTask.setOutstatus("2");
        return toAjax(tCkPickingwavegoodsService.updateGoodsOutResult(backTask));
    }

    /**
     * 品质检验记录报表画面
     * @return
     */
    @RequiresPermissions("business:qualityInspect:view")
    @GetMapping("/report")
    public String report()
    {
        return prefix + "/qualityInspectReport";
    }

    /**
     * 查询品质检验记录列表
     */
    @RequiresPermissions("business:qualityInspect:list")
    @PostMapping("/report")
    @ResponseBody
    public TableDataInfo reportList(DateCover date)
    {
        startPage();
        List<TCkPickingwavegoodsBack> list = tCkPickingwavegoodsBackService.selectTCkPickingwavegoodsBakByCreateDate(
                date.getStartDate(),
                date.getEndDate(), "5", date.getSearch());
        return getDataTable(list);
    }

    /**
     * 导出品质检验记录报表
     */
    @RequiresPermissions("business:qualityInspect:export")
    @Log(title = "导出品质检验记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DateCover date)
    {
//        tCkPickingwavegoodsBack.setCkType("5");
//        List<TCkPickingwavegoodsBack> list = tCkPickingwavegoodsBackService.selectTCkPickingwavegoodsBackWithoutEmpty(tCkPickingwavegoodsBack);
        List<TCkPickingwavegoodsBack> list = tCkPickingwavegoodsBackService.selectTCkPickingwavegoodsBakByCreateDate(
                date.getStartDate(),
                date.getEndDate(), "5", date.getSearch());
        ExcelUtil<TCkPickingwavegoodsBack> util = new ExcelUtil<TCkPickingwavegoodsBack>(TCkPickingwavegoodsBack.class);
        return util.exportExcel(list, "品质检验记录表");
    }

    /**
     * 状态说明弹框
     */
    @GetMapping("/stsDescribe")
    public String stsDescribe()
    {
        return prefix + "/stsdescribe";
    }
}
