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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesTemplateService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesPullingOrderInfoService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.base.util.StringUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesLabelTemplate;
import cn.estsh.i3plus.pojo.mes.bean.MesPrintQueue;
import cn.estsh.i3plus.pojo.mes.bean.MesPullingOrderInfo;
import cn.estsh.i3plus.pojo.mes.bean.MesPullingOrderPartInfo;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
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 org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.StringJoiner;

/**
 * 拉动单
 */
@Api("拉动单扫描工位端")
@RestController
@RequestMapping(MesCommonConstant.MES_YANFEN+ "/mesPullingOrderInfo")
public class MesPullingOrderInfoController {

    @Autowired
    private IMesPullingOrderInfoService mesPullingOrderInfoService;

    @Autowired
    private IMesTemplateService mesTemplateService;

    @GetMapping(value = "/query-pager")
    @ApiOperation(value = "查询拉动单")
    public ResultBean queryMesPullingOrderInfoByPager(MesPullingOrderInfo mesPullingOrderInfo, Pager pager) {
        try {
            mesPullingOrderInfo.setOrganizeCode(mesPullingOrderInfo.getOrganizeCode());
            ListPager<MesPullingOrderInfo> partInspectionListPager = mesPullingOrderInfoService.queryMesPullingOrderInfoByPager(mesPullingOrderInfo, pager);
            return ResultBean.success("查询成功").setListPager(partInspectionListPager);

        } catch (ImppBusiException e) {
            return ResultBean.fail(e).build();
        } catch (Exception e) {
            return ResultBean.fail(e);
        }
    }

    @PostMapping(value = "/doScan")
    @ApiOperation(value = "拉动单扫描")
    public ResultBean doMesPullingOrderInfoScan(@RequestBody MesPullingOrderInfo mesPullingOrderInfo) {
        try {
            if (StringUtils.isEmpty(mesPullingOrderInfo.getPullingOrderNo())) {
                throw new ImppBusiException("拉动单号不能为空");
            }
            mesPullingOrderInfoService.doMesPullingOrderInfoScan(mesPullingOrderInfo, !StringUtils.isEmpty(mesPullingOrderInfo.getModifyUser())?mesPullingOrderInfo.getModifyUser(): AuthUtil.getSessionUser().getUserName());
            return ResultBean.success(String.format("拉动单【%s】扫描成功",mesPullingOrderInfo.getPullingOrderNo()))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException e) {
            return ResultBean.fail(e).build();
        } catch (Exception e) {
            return ResultBean.fail(e);
        }
    }
    @PostMapping(value = "/doPrint")
    @ApiOperation(value = "打印未打印拉动单后修改状态")
    public ResultBean doMesPullingOrderInfoPrint(@RequestBody MesPullingOrderInfo mesPullingOrderInfo) {
        try {

            if (StringUtils.isEmpty(mesPullingOrderInfo.getOrganizeCode())) {
                throw new ImppBusiException("工厂不能为空");
            }

            if (StringUtils.isEmpty(mesPullingOrderInfo.getWorkCenterCode())) {
                throw new ImppBusiException("产线不能为空");
            }

            if (StringUtils.isEmpty(mesPullingOrderInfo.getPullCode())) {
                throw new ImppBusiException("拉动组不能为空");
            }

            if (StringUtils.isEmpty(mesPullingOrderInfo.getModifyUser())) {
                throw new ImppBusiException("操作人不能为空");
            }

            String moduleKey = new StringJoiner(MesPcnExtConstWords.COLON)
                    .add(mesPullingOrderInfo.getOrganizeCode())
                    .add("PRINT_PULLING_ORDER_INFO")
                    .add(mesPullingOrderInfo.getWorkCenterCode()).add(mesPullingOrderInfo.getPullCode()).toString();

            synchronized (moduleKey.intern()) {

                List<MesPullingOrderInfo> pullingOrderInfos = mesPullingOrderInfoService.doMesPullingOrderInfoPrint(mesPullingOrderInfo);

                return ResultBean.success(CollectionUtils.isEmpty(pullingOrderInfos) ? "查询暂无可打印数据!!!" : "打印队列查询成功!!!").setResultList(pullingOrderInfos);

            }

        } catch (ImppBusiException e) {
            return ResultBean.fail(e).build();
        } catch (Exception e) {
            return ResultBean.fail(e);
        }
    }
    @PostMapping(value = "/new/doPrint")
    @ApiOperation(value = "打印未打印拉动单后修改状态--带类型")
    public ResultBean doMesPullingOrderInfoNewPrint(@RequestBody MesPullingOrderInfo mesPullingOrderInfo) {
        try {

            if (StringUtils.isEmpty(mesPullingOrderInfo.getOrganizeCode())) {
                throw new ImppBusiException("工厂不能为空");
            }

            if (StringUtils.isEmpty(mesPullingOrderInfo.getWorkCenterCode())) {
                throw new ImppBusiException("产线不能为空");
            }

            if (StringUtils.isEmpty(mesPullingOrderInfo.getPullCode())) {
                throw new ImppBusiException("拉动组不能为空");
            }

            if (StringUtils.isEmpty(mesPullingOrderInfo.getModifyUser())) {
                throw new ImppBusiException("操作人不能为空");
            }

            String moduleKey = new StringJoiner(MesPcnExtConstWords.COLON)
                    .add(mesPullingOrderInfo.getOrganizeCode())
                    .add("PRINT_PULLING_ORDER_INFO")
                    .add(mesPullingOrderInfo.getWorkCenterCode()).add(mesPullingOrderInfo.getPullCode()).toString();

            synchronized (moduleKey.intern()) {

                List<MesPullingOrderInfo> pullingOrderInfos = mesPullingOrderInfoService.doMesPullingOrderInfoPrint(mesPullingOrderInfo);

                return ResultBean.success(CollectionUtils.isEmpty(pullingOrderInfos) ? "查询暂无可打印数据!!!" : "打印队列查询成功!!!").setResultList(pullingOrderInfos);

            }

        } catch (ImppBusiException e) {
            return ResultBean.fail(e).build();
        } catch (Exception e) {
            return ResultBean.fail(e);
        }
    }
    @GetMapping("/template")
    @ApiOperation(value = "查询打印模板和明细")
    public ResultBean queryMesLabelTemplate(MesLabelTemplate labelTemplate ) {
        try {
            ValidatorBean.checkNotNull(labelTemplate.getTemplateCode(), "打印模板不能为空");
            ValidatorBean.checkNotNull(labelTemplate.getOrganizeCode(), "工厂代码不能为空");
            return ResultBean.success("查询成功").setResultObject(mesTemplateService.getMesLabelTemplate(labelTemplate.getTemplateCode(), labelTemplate.getOrganizeCode()));

        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/jis-part-info/query-pager")
    @ApiOperation(value = "查询JIS拉动单明细")
    public ResultBean queryMesPullingOrderPartInfoByPager(MesPullingOrderInfo mesPullingOrderInfo, Pager pager) {
        try {
            ValidatorBean.checkNotNull(mesPullingOrderInfo.getOrganizeCode(), "工厂代码不能为空");
            ValidatorBean.checkNotNull(mesPullingOrderInfo.getWorkCenterCode(), "产线不能为空");
            ValidatorBean.checkNotNull(mesPullingOrderInfo.getPullCode(), "拉动组代码不能为空");

            ListPager<MesPullingOrderPartInfo> partInspectionListPager = mesPullingOrderInfoService.queryMesPullingOrderPartInfoByPager(mesPullingOrderInfo, pager);
            return ResultBean.success("查询成功").setListPager(partInspectionListPager);

        } catch (ImppBusiException e) {
            return ResultBean.fail(e).build();
        } catch (Exception e) {
            return ResultBean.fail(e);
        }
    }

    @PostMapping(value = "/doJisScan")
    @ApiOperation(value = "JIS物料拉动-校验条码")
    public ResultBean doMesPullingOrderInfoJisScan(@RequestBody MesPullingOrderPartInfo info) {
        try {
            if (StringUtil.isEmpty(info.getStatus())||info.getStatus()!= MesExtEnumUtil.PULL_ORDER_PART_STATUS.UN_SENTED.getValue()) {
                throw new ImppBusiException("拉动单明细状态不为未送料状态！");
            }
            MesPullingOrderPartInfo pullingOrderInfo = mesPullingOrderInfoService.doMesPullingOrderInfoJisScan(info, AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("JIS物料拉动校验条码成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(pullingOrderInfo);
        } catch (ImppBusiException e) {
            return ResultBean.fail(e).build();
        } catch (Exception e) {
            return ResultBean.fail(e);
        }
    }
    @PostMapping(value = "/doSend")
    @ApiOperation(value = "JIS物料拉动-送料按钮")
    public ResultBean doMesPullingOrderInfoSend(@RequestBody List<MesPullingOrderPartInfo> infoList) {
        try {
            for (MesPullingOrderPartInfo partInfo : infoList) {
                if (MesExtEnumUtil.PULL_ORDER_PART_STATUS.SENTED.getValue()==partInfo.getStatus()) {
                    throw new ImppBusiException("JIS物料拉动状态不为未送料状态！");
                }
                if (MesExtEnumUtil.PART_PULL_DETAIL_COLOR.RED.getCode()==partInfo.getColor()) {
                    throw new ImppBusiException("JIS物料拉动需要扫描条码校验！");
                }
            }
            List<MesPullingOrderPartInfo> pullingOrderInfos = mesPullingOrderInfoService.doMesPullingOrderInfoSend(infoList, AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("拉动单送料成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(pullingOrderInfos);
        } catch (ImppBusiException e) {
            return ResultBean.fail(e).build();
        } catch (Exception e) {
            return ResultBean.fail(e);
        }
    }
}
