package com.shkj.wms.controller.business;

import com.shkj.common.annotation.RepeatSubmit;
import com.shkj.common.constant.RedisConstant;
import com.shkj.common.core.controller.BaseController;
import com.shkj.common.core.domain.Result;
import com.shkj.common.core.page.TableDataInfo;
import com.shkj.wms.bo.BaseBarcodesOutQueryBo;
import com.shkj.wms.domain.BaseBarcodesIn;
import com.shkj.wms.domain.BasePoint;
import com.shkj.wms.service.IBaseBarcodesInService;
import com.shkj.wms.service.IBaseBarcodesOutService;
import com.shkj.wms.service.IBaseGoodsDataService;
import com.shkj.wms.vo.BaseBarcodesOutTaskVo;
import com.shkj.wms.vo.BaseBarcodesOutVo;
import com.shkj.wms.vo.BaseContainerVo;
import com.shkj.wms.vo.BaseInOutQtyVo;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("条码信息")
@RequiredArgsConstructor(onConstructor_ =@Autowired)
@RestController
@RequestMapping("/wms/barcodes")
public class BaseBarcodesController extends BaseController {

    @Autowired
    IBaseBarcodesInService iBaseBarcodesInService;
    @Autowired
    IBaseGoodsDataService iBaseGoodsDataService;
    @Resource
    RedisTemplate redisTemplate;
    @Autowired
    IBaseBarcodesOutService iBaseBarcodesOutService;

    /**
     * 获取入库条码信息
     */
    @GetMapping("/checkQrCode")
    @RepeatSubmit
    public Result<BaseBarcodesIn> checkQrCode(@RequestParam("qrCode") String qrCode){
        return iBaseBarcodesInService.getBaseBarcodesInByQrCode(qrCode);
    }


    /**
     * 获取保险杠所有颜色
     */
    @GetMapping("/getBumperColor")
    @RepeatSubmit
    public Result<String> getBumperColor(){
        return Result.ok().data(iBaseGoodsDataService.getBumperColor());
    }


    /**
     * 获取保险杠所有车型
     */
    @GetMapping("/getBumperVehicheModel")
    @RepeatSubmit
    public Result<String> getBumperVehicheModel(){
        return Result.ok().data(iBaseGoodsDataService.getBumperVehicheModel());
    }

    /**
     * 获取保险杠所有派生
     */
    @GetMapping("/getBumperDerive")
    @RepeatSubmit
    public Result<String> getBumperDerive(){
        return Result.ok().data(iBaseGoodsDataService.getBumperDerive());
    }

    /**
     * 根据车型代码/派生获取待出库数据
     */
    @GetMapping("/getBarcodesOutData")
    @RepeatSubmit
    public Result<Boolean> getBarcodesOutData(BaseBarcodesOutQueryBo bo){
        List<BaseBarcodesOutVo> outVoList = iBaseBarcodesOutService.getBarcodesOutData(bo);
        return Result.ok().data(outVoList);
    }

    /**
     * 查询获取位置代码
     */
    @GetMapping("/getBumperType")
    @RepeatSubmit
    public Result<String> getBumperType(){
        return Result.ok().data(iBaseGoodsDataService.getBumperLocationCode());
    }


    /**
     * 获取当前日期出入库的数量
     */
    @GetMapping("/getCurrentDateInAndOutQty")
    @RepeatSubmit
    public Result<BaseInOutQtyVo> getCurrentDateInAndOutQty(){
        return Result.ok().data(iBaseBarcodesInService.getCurrentDateInAndOutQty());
    }

    /**
     * 获取出库队列的汇总数据，总任务数，已下发任务数，剩余任务数
     * @param bo
     * @return
     */
    @GetMapping("/getBarcodesOutDataTaskInfo")
    @RepeatSubmit
    public Result<Boolean> getBarcodesOutDataTaskInfo(BaseBarcodesOutQueryBo bo){
        List<BaseBarcodesOutTaskVo> outVoList = iBaseBarcodesOutService.getBarcodesOutDataTaskInfo(bo);
        return Result.ok().data(outVoList);
    }


    /**
     * 出库队列任务的详细信息
     * @param bo
     * @return
     */
    @GetMapping("/getBarcodesOutDataTaskInfoDetail")
    @RepeatSubmit
    public TableDataInfo<BaseBarcodesOutVo> getBarcodesOutDataTaskInfoDetail(BaseBarcodesOutQueryBo bo){
        startPage();
        List<BaseBarcodesOutVo> outVoList = iBaseBarcodesOutService.getBarcodesOutDataTaskInfoDetail(bo);
        return getDataTable(outVoList);
    }

    /**
     * 手动下发任务
     * @param bo
     * @return
     */
    @PostMapping("/handMoveOutboundInfoTask")
    @RepeatSubmit
    public Result<Boolean> handMoveOutboundInfoTask(BaseBarcodesOutQueryBo bo){
        return  iBaseBarcodesOutService.handMoveOutboundInfoTask(bo);
    }

    /**
     *  手动下发出库任务
     * @param vin
     * @param status
     * @return
     */
    @PostMapping("/handOutboundInfoTask")
    @RepeatSubmit
    public Result<Boolean> handOutboundInfoTask(@RequestParam("vin") String vin,@RequestParam("status") String status){
        return  iBaseBarcodesOutService.handOutboundInfoTask(vin,status);
    }

    /**
     * 切换出库方式
     * @author Comair
     * @date 2023/8/12 15:21
     * @param status
     * @return com.shkj.common.core.domain.Result<java.lang.Boolean>
     */
    @PostMapping("/handOutboundStatus")
    @RepeatSubmit
    public Result<Boolean> handOutboundStatus(@RequestParam("status") String status){
        return  iBaseBarcodesOutService.handOutboundStatus(status);
    }

    @PostMapping("/handOutEmptyStatus")
    @RepeatSubmit
    public Result<Boolean> handOutEmptyStatus(@RequestParam("status") String status){
        return  iBaseBarcodesOutService.handOutEmptyStatus(status);
    }

    /**
     * 获取redis 中关于手动出库的信息
     * @author Comair
     * @date 2023/8/12 15:45
     * @return com.shkj.common.core.domain.Result<java.lang.Boolean>
     */
    @PostMapping("/getOutBoundRedisInfo")
    @RepeatSubmit
    public Result<Map<String,String>> getOutBoundRedisInfo(){
        if(ObjectUtils.isEmpty(redisTemplate.opsForValue().get(RedisConstant.out_plan_type))){
            redisTemplate.opsForValue().set(RedisConstant.out_plan_type,"立库出库");
        }
        String outPlanType = redisTemplate.opsForValue().get(RedisConstant.out_plan_type).toString();
        String startTaskVin = ObjectUtils.isNotEmpty(redisTemplate.opsForValue().get(RedisConstant.startTaskVin)) ? redisTemplate.opsForValue().get(RedisConstant.startTaskVin).toString() : "";
        String endTaskVin = ObjectUtils.isNotEmpty(redisTemplate.opsForValue().get(RedisConstant.endTaskVin)) ? redisTemplate.opsForValue().get(RedisConstant.endTaskVin).toString() : "";
        Map<String,String> map = new HashMap<>();
        map.put("outPlanType",outPlanType);
        map.put("startTaskVin",startTaskVin);
        map.put("endTaskVin",endTaskVin);
        return  Result.ok().data(map);
    }

    @PostMapping("/getOutEmptyRedisInfo")
    @RepeatSubmit
    public Result<Map<String,String>> getOutEmptyRedisInfo(){
        String outPlanType = "初始化";
        Map<String,String> map = new HashMap<>();
        if(ObjectUtils.isNotEmpty(redisTemplate.opsForValue().get(RedisConstant.out_empty_type))){
            outPlanType = redisTemplate.opsForValue().get(RedisConstant.out_empty_type).toString();
        }
        map.put("outPlanType",outPlanType);
        return  Result.ok().data(map);
    }

    @GetMapping("/getOutEmptyInfo")
    public TableDataInfo<BasePoint> getOutEmptyInfo()
    {
        startPage();
        List<Map<String,String>> list = iBaseBarcodesOutService.getOutEmptyInfo();
        return getDataTable(list);

    }

    /**
     * 手动下发任务-暂停任务
     * @param bo
     * @return
     */
    @PostMapping("/handMoveOutboundInfoTaskForPause")
    @RepeatSubmit
    public Result<Boolean> handMoveOutboundInfoTaskForPause(BaseBarcodesOutQueryBo bo){
        return  iBaseBarcodesOutService.handMoveOutboundInfoTaskForPause(bo);
    }

    /**
     * 手动下发任务-暂停任务
     * @param bo
     * @return
     */
    @PostMapping("/handMoveOutboundInfoTaskForCancel")
    @RepeatSubmit
    public Result<Boolean> handMoveOutboundInfoTaskForCancel(BaseBarcodesOutQueryBo bo){
        return  iBaseBarcodesOutService.handMoveOutboundInfoTaskForCancel(bo);
    }



}
