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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IPartExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IPackageExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesSubassemblyRemadeRecord;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxSubassemblyRemadeDetailModel;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesPackage;
import cn.estsh.i3plus.pojo.mes.bean.MesPackageDetail;
import cn.estsh.i3plus.pojo.mes.bean.MesPart;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class SxSubassemblyRemadeDispatchService {

    @Autowired
    private IPackageExtService packageExtService;

    @Autowired
    private IPartExtService partExtService;

    public void checkPackage(String organizeCode, String sn, MesPackage packageDb, String msg) {
        if (null == packageDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("扫描的%s条码[%s]不存在数据", msg, sn)
                    .build();
        }
        if (MesPcnEnumUtil.PACKAGE_IS_SEALED.UNSEALED.getValue() == packageDb.getIsSealed()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("扫描的%s条码[%s]未封箱", msg, sn)
                    .build();
        }

        MesPackageDetail packageDetailDb = packageExtService.getPackageDetailByPackNo(organizeCode, packageDb.getPackageNo());
        if (null != packageDetailDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("扫描的%s条码[%s]有关联产品条码[%s],不支持操作组件返工",
                            msg, sn, packageDetailDb.getSerialNumber())
                    .build();
        }
    }

    public void checkSubassemblyRemadeRecord(String sn, MesSubassemblyRemadeRecord subassemblyRemadeRecordDb, String msg) {
        if (null == subassemblyRemadeRecordDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("扫描的%s条码[%s]不存在数据", msg, sn)
                    .build();
        }
    }

    public void checkPart(String organizeCode, String sn, MesPackage packageDb, String workOrderNo, String partNo, String msg) {
        MesPart partDb = partExtService.getPart(organizeCode, packageDb.getPartNo());
        if (null == partDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("扫描的%s条码[%s]关联的物料[%s]不存在数据", msg, sn, packageDb.getPartNo())
                    .build();
        }

        if (StringUtils.isEmpty(partDb.getCategoryCode2())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("扫描的%s条码[%s]关联的物料[%s]未配置[物料类型2]", msg, sn, packageDb.getPartNo())
                    .build();
        }

        if (!packageDb.getPartNo().equals(partNo)) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("扫描的%s条码[%s]关联的物料[%s]与当前返工工单[%s]关联的物料[%s]不一致",
                            msg, sn, packageDb.getPartNo(), workOrderNo, partNo)
                    .build();
        }
    }

    public SxSubassemblyRemadeDetailModel packDetail(String sn, MesSubassemblyRemadeRecord subassemblyRemadeRecordDb) {
        SxSubassemblyRemadeDetailModel detail = new SxSubassemblyRemadeDetailModel();
        //detail.setPackageNo(packageDb.getPackageNo());
        //detail.setCtNo(packageDb.getCtNo());
        //detail.setLotNo(packageDb.getLotNo());
        //detail.setPartNo(packageDb.getPartNo());
        //detail.setPartNameRdd(packageDb.getPartNameRdd());
        //detail.setInitWorkCenterCode(packageDb.getWorkCenterCode());
        //detail.setInitWorkOrderNo(packageDb.getWorkOrderNo());
        //detail.setInitQty(packageDb.getQty());
        //detail.setWorkCenterCode(workCenterCode);
        //detail.setWorkOrderNo(workOrderNo);
        //detail.setQty(packageDb.getQty());
        BeanUtils.copyProperties(subassemblyRemadeRecordDb, detail);
        detail.setQty(detail.getInitQty());
        detail.setSn(sn);
        detail.setScanDatetime(TimeTool.getNowTime(true));
        return detail;
    }

}
