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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPackageExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProduceSnExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ProduceSnExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxPrintTemplateModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.MesPcnBaseController;
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 cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN)
@Api(tags = "工单条码")
public class ProduceSnExtController extends MesPcnBaseController {

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @GetMapping(value = "/produce-sn-ext/query")
    @ApiOperation(value = "按条件分页查询工单条码信息")
    public ResultBean queryProduceSnExtByPager(MesProduceSnExt produceSnExt, Pager pager) {
        try {
            ValidatorBean.beginValid(produceSnExt)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, produceSnExt.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.WORK_ORDER_NO, produceSnExt.getWorkOrderNo());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(produceSnExtService.queryProduceSnExtByPager(produceSnExt, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/produce-sn-ext/work-order-type/query")
    @ApiOperation(value = "按条件分页查询工单条码信息,区分工单类型")
    public ResultBean queryProduceSnExtByWorkOrderTypeByPager(ProduceSnExtModel produceSnExtModel, Pager pager) {
        try {
            ValidatorBean.beginValid(produceSnExtModel)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, produceSnExtModel.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.WORK_ORDER_TYPE, produceSnExtModel.getWorkOrderType())
                    .notNull(MesPcnExtConstWords.WORK_ORDER_NO, produceSnExtModel.getWorkOrderNo());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(produceSnExtService.queryProduceSnExtByWorkOrderTypeByPager(produceSnExtModel, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/produce-sn-ext/batch-insert")
    @ApiOperation(value = "手动生成条码")
    public ResultBean doWorkOrderCreateSn(@RequestParam(MesPcnExtConstWords.ID) Long id,
                                          @RequestParam(MesPcnExtConstWords.ORGANIZE_CODE) String organizeCode,
                                          @RequestParam(MesPcnExtConstWords.USER_INFO) String userInfo) {
        try {
            ValidatorBean.checkNotNull(id);
            ValidatorBean.checkNotNull(organizeCode);
            ValidatorBean.checkNotNull(userInfo);
            produceSnExtService.doWorkOrderCreateSn(false, id, organizeCode, userInfo);
            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 = "/produce-sn-ext/batch-print")
    @ApiOperation(value = "手动打印条码")
    public ResultBean doWorkOrderPrintSn(@RequestBody SxPrintTemplateModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.USER_INFO, model.getUserInfo());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(produceSnExtService.doWorkOrderPrintSn(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/package/batch-insert")
    @ApiOperation(value = "手动生成包装条码")
    public ResultBean doWorkOrderCreatePackage(@RequestParam(MesPcnExtConstWords.ID) Long id,
                                               @RequestParam(MesPcnExtConstWords.ORGANIZE_CODE) String organizeCode,
                                               @RequestParam(MesPcnExtConstWords.USER_INFO) String userInfo) {
        try {
            ValidatorBean.checkNotNull(id);
            ValidatorBean.checkNotNull(organizeCode);
            ValidatorBean.checkNotNull(userInfo);
            produceSnExtService.doWorkOrderHandCreatePackage(id, organizeCode, userInfo);
            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 = "/package-qty/query")
    @ApiOperation(value = "进行弹框二次提示“包装数量为***，可产生N个包装条码，&&&“")
    public ResultBean getWorkOrderCreatePackageQty(Long id, String organizeCode, String userInfo) {
        try {
            ValidatorBean.checkNotNull(id);
            ValidatorBean.checkNotNull(organizeCode);
            ValidatorBean.checkNotNull(userInfo);
            return ResultBean.success("操作成功").setResultObject(produceSnExtService.getWorkOrderCreatePackageQty(id, organizeCode, userInfo)).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/package/query")
    @ApiOperation(value = "按条件分页查询工单包装信息")
    public ResultBean queryPackageByPager(MesPackageExt pack, Pager pager) {
        try {
            ValidatorBean.beginValid(pack)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, pack.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.WORK_ORDER_NO, pack.getWorkOrderNo());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(produceSnExtService.queryPackageByPager(pack, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/package/batch-print")
    @ApiOperation(value = "手动打印包装条码")
    public ResultBean doWorkOrderPrintPackageNo(@RequestBody SxPrintTemplateModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.USER_INFO, model.getUserInfo());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(produceSnExtService.doWorkOrderPrintPackageNo(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/produce-sn-ext/unbind-produce-sn-check")
    @ApiOperation(value = "查询当前工单下的条码")
    public ResultBean queryProduceSnList(String organizeCode, String workOrderNo, Pager pager){
        try{
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(produceSnExtService.queryProduceSnList(organizeCode, workOrderNo, pager));
        }catch (ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch (Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/produce-sn-ext/unbind")
    @ApiOperation(value = "条码解绑")
    public ResultBean workOrderUnBindSn(String organizeCode, String modifyUser, String workOrderNo, String serialNumber, Boolean keyPiecesIsBindFlag){
        try{
            Boolean flag = produceSnExtService.workOrderUnBindSn(organizeCode, modifyUser, workOrderNo, serialNumber, keyPiecesIsBindFlag);
            if(flag == null){
                return ResultBean.success("解绑成功！").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
            }else if (flag) {
                return ResultBean.success("当前本体条码已绑定关键件，是否确定解绑").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(true);
            }else{
                return ResultBean.success("是否确定解绑当前本体条码").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(false);
            }
        }catch (ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch (Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
