package cn.estsh.i3plus.ext.mes.pcn.apiservice.controller.busi.bu3;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu3.ISxUnbindProduceSnService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesUnbindProduceSnTravel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.tool.JsonUtilTool;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2023-08-04 10:07
 */
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN + "/unbind-produce-sn")
@Api(tags = "Bu条码解绑")
public class BuUnbindProduceSnController {

    @Autowired
    private ISxUnbindProduceSnService unbindProduceSnService;

    //@PostMapping(value = "/do")
    //@ApiOperation(value = "解绑产品条码")
    //public ResultBean doUnBindProduceSn(MesPersonCheckDetail personCheckDetail) {
    //    try {
    //        unBindProduceSnService.doUnBindProduceSn(personCheckDetail);
    //        return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
    //    } catch (ImppBusiException busExcep) {
    //        return ResultBean.fail(busExcep);
    //    } catch (Exception e) {
    //        return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
    //    }
    //}

    @PostMapping(value = "/mes/do")
    @ApiOperation(value = "MES调PCN接口解绑产品条码")
    public ResultBean doUnBindProduceSn(@RequestBody Map<String, Object> params) {
        try {

            if(!params.containsKey(MesPcnExtConstWords.ORGANIZE_CODE)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("工厂代码不能为空")
                        .build();
            }
            if(!params.containsKey(MesPcnExtConstWords.USER_NAME)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("操作人不能为空")
                        .build();
            }
            if(!params.containsKey(MesPcnExtConstWords.WORK_ORDER_NO)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("工单号不能为空")
                        .build();
            }
            if(!params.containsKey(MesPcnExtConstWords.STATUS)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("工单状态不能为空")
                        .build();
            }

            unbindProduceSnService.doUnbindProduceSn((String) params.get(MesPcnExtConstWords.ORGANIZE_CODE),
                    (String) params.get(MesPcnExtConstWords.USER_NAME),
                    (String) params.get("nowTime"),
                    Integer.parseInt((String) params.get(MesPcnExtConstWords.STATUS)),
                    JsonUtilTool.toList((String) params.get(MesPcnExtConstWords.WORK_ORDER_NO), String.class));
            return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/query-pager")
    @ApiOperation(value = "按条件分页查询解绑条码履历信息")
    public ResultBean queryProduceSnExtByPager(MesUnbindProduceSnTravel unbindProduceSnTravel, Pager pager) {
        try {
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(unbindProduceSnService.queryUnbindProduceSnTravelByPager(unbindProduceSnTravel, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
