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


import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesBoxingErrorProofingService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.mes.bean.MesPackage;
import cn.estsh.i3plus.pojo.mes.bean.MesPackageDetail;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "装箱防错")
@RequestMapping(MesCommonConstant.MES_YANFEN + "/boxing-error-proofing")
@Slf4j
public class MesBoxingErrorProofingController {

    @Autowired
    private IMesBoxingErrorProofingService mesBoxingErrorProofingService;


    @GetMapping("/mes-package-details/query")
    @ApiOperation(value = "查询包装明细")
    public ResultBean queryMesPackageDetailByPager(MesPackage mesPackage, Pager pager) {

        try {
            ValidatorBean.checkNotNull(mesPackage.getPackageNo(), "箱条码不能为空");

            mesPackage.setOrganizeCode(!StringUtils.isEmpty(mesPackage.getOrganizeCode())?mesPackage.getOrganizeCode():AuthUtil.getOrganizeCode());
            ListPager<MesPackageDetail> partInspectionListPager = mesBoxingErrorProofingService.queryMesPackageDetailByPager(mesPackage, pager);
            return ResultBean.success("查询成功").setListPager(partInspectionListPager);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/isMemory/get")
    @ApiOperation(value = "查询操作人是否记忆")
    public ResultBean getIsMemory(String organizeCode,String userName) {

        try {
            ValidatorBean.checkNotNull(organizeCode, "工厂代码不能为空");
            ValidatorBean.checkNotNull(userName, "操作人不能为空");

            return ResultBean.success("查询成功").setResultObject(mesBoxingErrorProofingService.getIsMemory(organizeCode, userName));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/do-scan")
    @ApiOperation(value = "扫描")
    public ResultBean doScan(MesPackageDetail mesPackageDetail) {

        try {
            ValidatorBean.checkNotNull(mesPackageDetail.getPackageNo(), "箱条码不能为空");

            mesPackageDetail.setOrganizeCode(!StringUtils.isEmpty(mesPackageDetail.getOrganizeCode())?mesPackageDetail.getOrganizeCode():AuthUtil.getOrganizeCode());
            ConvertBean.serviceModelInitialize(mesPackageDetail,!StringUtils.isEmpty(mesPackageDetail.getModifyUser())?mesPackageDetail.getModifyUser():AuthUtil.getSessionUser().getUserName());
            return mesBoxingErrorProofingService.doScan(mesPackageDetail);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


    @PostMapping("/mes-package-details/saveAll")
    @ApiOperation(value = "保存包装明细")
    public ResultBean saveMesPackageDetails(@RequestBody List<MesPackageDetail> mesPackageDetailList) {
        try {
            //属性校验
            mesPackageDetailList.forEach(mesPackageDetail -> {
                ValidatorBean.checkNotNull(mesPackageDetail.getPackageNo(), "箱条码不能为空");
            });
            MesPackageDetail next = mesPackageDetailList.iterator().next();
            String userName = !StringUtils.isEmpty(next.getModifyUser())?next.getModifyUser():AuthUtil.getSessionUser().getUserName();
            return mesBoxingErrorProofingService.saveMesPackageDetails(mesPackageDetailList, userName);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping("/mes-package/update")
    @ApiOperation(value = "更新包装表")
    public ResultBean updateMesPackage(MesPackage mesPackage) {

        try {
            //属性校验
            ValidatorBean.checkNotNull(mesPackage.getPackageNo(), "箱条码不能为空");
            ValidatorBean.checkNotNull(mesPackage.getIsSealed(), "是否封箱不能为空");

            mesPackage.setOrganizeCode(!StringUtils.isEmpty(mesPackage.getOrganizeCode())?mesPackage.getOrganizeCode():AuthUtil.getOrganizeCode());
            ConvertBean.serviceModelUpdate(mesPackage, !StringUtils.isEmpty(mesPackage.getModifyUser())?mesPackage.getModifyUser():AuthUtil.getSessionUser().getUserName());
            return  mesBoxingErrorProofingService.updateMesPackage(mesPackage);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping("/mes-package-details/update")
    @ApiOperation(value = "更新包装明细")
    public ResultBean updateMesPackageDetails(MesPackageDetail mesPackage) {

        try {
            //属性校验
            ValidatorBean.checkNotNull(mesPackage.getPackageNo(), "箱条码不能为空");
            ValidatorBean.checkNotNull(mesPackage.getSerialNumber(), "替换条码不能为空");
            ValidatorBean.checkNotNull(mesPackage.getOldSerialNumber(), "替换源条码不能为空");

            ConvertBean.serviceModelUpdate(mesPackage, !StringUtils.isEmpty(mesPackage.getModifyUser())?mesPackage.getModifyUser():AuthUtil.getSessionUser().getUserName());
            mesBoxingErrorProofingService.updateMesPackageDetails(mesPackage);
            return ResultBean.success("保存成功");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/unLock")
    @ApiOperation(value = "解锁")
    public ResultBean unLock(String pwd,String organizeCode) {
        try {
            //属性校验
            ValidatorBean.checkNotNull(pwd, "密码不能为空");
            mesBoxingErrorProofingService.unLock(!StringUtils.isEmpty(organizeCode)?organizeCode:AuthUtil.getOrganizeCode(),pwd);
            return ResultBean.success("解锁成功");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


}
