package com.shkj.wms.controller.business;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shkj.common.annotation.Log;
import com.shkj.common.core.controller.BaseController;
import com.shkj.common.core.domain.Result;
import com.shkj.common.core.page.TableDataInfo;
import com.shkj.common.enums.BusinessType;
import com.shkj.wms.bo.*;
import com.shkj.wms.config.ValidList;
import com.shkj.wms.domain.BusinStockInv;
import com.shkj.wms.enums.InvStatusEnum;
import com.shkj.wms.enums.YesAndNoEnum;
import com.shkj.wms.service.IBusinStockInvService;
import com.shkj.wms.vo.BusinStockInvExeVo;
import com.shkj.wms.vo.BusinStockInvVo;
import com.shkj.wms.vo.StockLocationGoodsAreaStorageVo;
import com.shkj.wms.vo.StockLocationInvVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 库内盘点管理
 *
 * @author lch
 * @menu 库内盘点管理
 * @date 2022-06-10
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wms/stock/inv")
public class BusinStockInvController extends BaseController {

    private final IBusinStockInvService iBusinStockInvService;

    /**
     * 根据条件查询商品货位准备生成盘点计划
     */
    @GetMapping("/getBusinStockInvList")
    public TableDataInfo<BusinStockInvExeVo> getBusinStockInvList(BusinStockInvQueryBo bo) {
        startPage();
        List<BusinStockInvExeVo> list = iBusinStockInvService.getBusinStockInvList(bo);
        return getDataTable(list);
    }


    /**
     * 根据条件查询查询待执行的盘点计划
     */
    @GetMapping("/queryInvMold")
    public TableDataInfo<StockLocationGoodsAreaStorageVo> queryInvMold(BusinStockInvGoodsQueryBo bo) {
        startPage();
        List<StockLocationGoodsAreaStorageVo> list = iBusinStockInvService.queryInvMold(bo);
        return getDataTable(list);
    }

    @GetMapping("/queryInvLocation")
    public TableDataInfo<StockLocationInvVo> queryInvLocation(BusinStockInvGoodsQueryBo bo) {
        startPage();
        List<StockLocationInvVo> list = iBusinStockInvService.queryInvLocation(bo);
        return getDataTable(list);
    }



    /**
     * 新增盘点计划
     */
    @Log(title = "新增盘点计划", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result<Boolean> add(@Valid @RequestBody BusinStockInvAddBo bo) {
        return iBusinStockInvService.insertByAddNewBo(bo);
    }

    /**
     * 新增盘点计划（弃用）
     */
    @Log(title = "新增盘点计划", businessType = BusinessType.INSERT)
    @PostMapping("/addStockInv")
    public Result<Boolean> addStockInv(@Valid @RequestBody BusinStockInvAddBo bo) {
        return iBusinStockInvService.insertByAddBo(bo);
    }


    /**
     * pda中查询待盘点的信息（托盘过滤）
     */
    @GetMapping("/pdaInvByContainerCode")
    public TableDataInfo<BusinStockInvVo> pdaInvByContainerCode(PdaInvByContainerCodeBo bo) {
        startPage();
        List<BusinStockInvVo> list = iBusinStockInvService.pdaInvByContainerCode(bo);
        return getDataTable(list);
    }


    /**
     * pda盘点完成（数量进行确认）弃用
     */
    @Log(title = "盘点录入保存", businessType = BusinessType.UPDATE)
    @PostMapping("/entrySave2")
    public Result<Boolean> entrySave2(@Valid @RequestBody ValidList<BusinStockInvEditBo> bo) {
        return iBusinStockInvService.entrySave(bo);
    }


    /**
     * pda盘点完成（数量进行确认）
     */
    @Log(title = "盘点录入保存", businessType = BusinessType.UPDATE)
    @PostMapping("/entrySave")
    public Result<Boolean> entrySave(@Valid @RequestBody ValidList<BusinStockInvEditBo> bo) {
        return iBusinStockInvService.entrySave(bo);
    }

    /**
     * 查询未确认的盘点单（盘点确认页面下拉框）
     */
    @GetMapping("/differenceListOpt")
    public Result<List<BusinStockInv>> differenceListOpt() {
        LambdaQueryWrapper<BusinStockInv> wrapper = new LambdaQueryWrapper<BusinStockInv>().eq(BusinStockInv::getInvStatus, InvStatusEnum.difference.getValue());
        List<BusinStockInv> list = iBusinStockInvService.list(wrapper);
        List<BusinStockInv> invList = new ArrayList<>();
        Map<String, List<BusinStockInv>> collect = list.stream().collect(Collectors.groupingBy(x -> x.getInvOrderNo()));
        for (Map.Entry<String, List<BusinStockInv>> entry : collect.entrySet()) {
            BusinStockInv inv = entry.getValue().get(0);
            invList.add(inv);
        }
        return Result.ok().data(invList);
    }
    /**
     * 查询已完成的盘点单（页面下拉框）
     */
    @GetMapping("/finishedOpt")
    public Result<List<BusinStockInv>> finishedOpt() {
        LambdaQueryWrapper<BusinStockInv> wrapper = new LambdaQueryWrapper<BusinStockInv>()
                .eq(BusinStockInv::getInvStatus, InvStatusEnum.end.getValue());
        List<BusinStockInv> list = iBusinStockInvService.list(wrapper);
        List<BusinStockInv> invList = new ArrayList<>();
        Map<String, List<BusinStockInv>> collect = list.stream().collect(Collectors.groupingBy(x -> x.getInvOrderNo()));
        for (Map.Entry<String, List<BusinStockInv>> entry : collect.entrySet()) {
            BusinStockInv inv = entry.getValue().get(0);
            invList.add(inv);
        }
        return Result.ok().data(invList);
    }

    /**
     * 盘点差异列表(盘点确认页面)
     */
    @GetMapping("/differenceList")
    public TableDataInfo<BusinStockInvVo> differenceList(BusinStockInvQueryBo bo) {
        startPage();
        bo.setInvWay(null);
        bo.setHaveDifference(null);
        bo.setInvStatus(InvStatusEnum.difference.getValue());
        List<BusinStockInvVo> list = iBusinStockInvService.queryList(bo);
        return getDataTable(list);
    }


    /**
     * 取消盘点计划
     */
    @Log(title = "取消盘点计划", businessType = BusinessType.UPDATE)
    @PostMapping("/cancel")
    public Result<Boolean> cancel(@Valid @RequestBody List<Long> ids) {
        return iBusinStockInvService.cancel(ids);
    }


    /**
     * 差异盘点列表中盘点确认（生成盘点计划按钮）
     */
    @Log(title = "差异盘点列表中盘点确认", businessType = BusinessType.UPDATE)
    @PostMapping("/confirm")
    public Result<Boolean> confirm(@Valid @RequestBody List<Long> ids) {
        return iBusinStockInvService.confirm(ids);
    }


    /**
     * 盘点计划中的，执行盘点计划
     */
    @Log(title = "盘点计划中的，执行盘点计划", businessType = BusinessType.UPDATE)
    @PostMapping("/exec")
    public Result<Boolean> exec(@RequestParam String invOrderNo,Integer layer) {
        return iBusinStockInvService.exec(invOrderNo,layer);
    }
    @Log(title = "盘点计划中的，取消盘点计划", businessType = BusinessType.UPDATE)
    @PostMapping("/execCancel")
    public Result<Boolean> execCancel(@RequestParam String invOrderNo) {
        return iBusinStockInvService.execCancel(invOrderNo);
    }



    /**
     * 报表中的盘点记录（只显示已完成且有差异数量的数据）
     */
    @GetMapping("/queryFinishInvRecords")
    public TableDataInfo<BusinStockInvVo> queryFinishInvRecords(BusinStockInvQueryBo bo) {
        startPage();
        bo.setInvWay(null);
        bo.setInvStatus(InvStatusEnum.end.getValue());
        bo.setHaveDifference(YesAndNoEnum.YES.getValue());
        List<BusinStockInvVo> list = iBusinStockInvService.queryList(bo);
        return getDataTable(list);
    }
}
