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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesContainerSnBindService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesContainerSnBindModel;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Slf4j
@RestController
@Api(tags = "容器条码绑定")
@RequestMapping(MesCommonConstant.MES_YANFEN + "/container-sn-bind")
public class MesContainerSnBindController {
    @Autowired
    private IMesContainerSnBindService containerSnBindService;

    @PostMapping("/scan-container")
    @ApiOperation(value = "扫描信息")
    public ResultBean doScanContainer(@RequestBody MesContainerSnBindModel model) {
        try {
            ValidatorBean.checkNotNull(model.getContainerSn(), "容器条码不能为空");
            ValidatorBean.checkNotNull(model.getOrganizeCode(), "工厂代码不能为空");
            ValidatorBean.checkNotNull(model.getUserName(), "操作人不能为空");
            model = containerSnBindService.doScanContainer(model);
            return ResultBean.success("扫描成功").setResultObject(model);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/scan-sn")
    @ApiOperation(value = "扫描信息")
    public ResultBean doScanSn(@RequestBody MesContainerSnBindModel model) {
        try {
            ValidatorBean.checkNotNull(model.getContainerSn(), "容器条码不能为空");
            ValidatorBean.checkNotNull(model.getBarCode(), "零件条码不能为空");
            ValidatorBean.checkNotNull(model.getOrganizeCode(), "工厂代码不能为空");
            ValidatorBean.checkNotNull(model.getUserName(), "操作人不能为空");
            if (Objects.equals(model.getBindIsChoosePart(), CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue())) {
                if (StringUtils.isEmpty(model.getPartNo())) {
                    MesPcnException.throwMesBusiException("需要选择零件号");
                }
            }
            model = containerSnBindService.doScanBarcode(model, false);
            return ResultBean.success("扫描成功").setResultObject(model);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/clear")
    @ApiOperation(value = "清空")
    public ResultBean doClear(@RequestBody MesContainerSnBindModel model) {
        try {
            ValidatorBean.checkNotNull(model.getContainerSn(), "容器条码不能为空");
            ValidatorBean.checkNotNull(model.getOrganizeCode(), "工厂代码不能为空");
            ValidatorBean.checkNotNull(model.getUserName(), "操作人不能为空");
            model = containerSnBindService.doClear(model);
            return ResultBean.success("清空成功").setResultObject(model);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/delete-details")
    @ApiOperation(value = "删除")
    public ResultBean doDeleteDetail(@RequestBody MesContainerSnBindModel model) {
        try {
            ValidatorBean.checkNotNull(model.getContainerSn(), "容器条码不能为空");
            ValidatorBean.checkNotNull(model.getOrganizeCode(), "工厂代码不能为空");
            ValidatorBean.checkNotNull(model.getUserName(), "操作人不能为空");
            ValidatorBean.checkNotNull(model.getPackageDetails(), "请选择要删除的条码");
            model = containerSnBindService.doDeleteDetails(model);
            return ResultBean.success("删除成功").setResultObject(model);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/close-package")
    @ApiOperation(value = "强制关箱")
    public ResultBean doClosePackage(@RequestBody MesContainerSnBindModel model) {
        try {
            ValidatorBean.checkNotNull(model.getContainerSn(), "容器条码不能为空");
            ValidatorBean.checkNotNull(model.getOrganizeCode(), "工厂代码不能为空");
            ValidatorBean.checkNotNull(model.getUserName(), "操作人不能为空");
            containerSnBindService.doClosePackage(model);
            return ResultBean.success("关箱成功");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/open-package")
    @ApiOperation(value = "开箱")
    public ResultBean doOpenPackage(@RequestBody MesContainerSnBindModel model) {
        try {
            ValidatorBean.checkNotNull(model.getContainerSn(), "容器条码不能为空");
            ValidatorBean.checkNotNull(model.getOrganizeCode(), "工厂代码不能为空");
            ValidatorBean.checkNotNull(model.getUserName(), "操作人不能为空");
            containerSnBindService.doOpenPackage(model);
            return ResultBean.success("开箱成功");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
