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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesCustomerPartService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.api.iservice.base.IPartService;
import cn.estsh.i3plus.mes.pcn.api.iservice.busi.INumberRulePackAttributeStrategyService;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.mes.bean.MesCustomerPart;
import cn.estsh.i3plus.pojo.mes.bean.MesPart;
import cn.estsh.i3plus.pojo.mes.model.GenSerialNoModel;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;
import java.util.StringJoiner;

/**
 * @Description : 门板及cnsl总成条码
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/9/27 13:23
 * @Modify:
 **/
@Component
public class MesDPBarcodeWhNumberRuleStrategyService implements INumberRulePackAttributeStrategyService {

    @Autowired
    private IPartService partService;

    @Autowired
    private IMesCustomerPartService customerPartService;

    @Override
    public GenSerialNoModel execute(GenSerialNoModel genSerialNoModel) {

        if (null == genSerialNoModel) MesPcnException.throwBusiException("入参缺少[GenSerialNoModel]!");

        Map<String, Object> dataMap = genSerialNoModel.getDataMap();

        MesPart part = (!CollectionUtils.isEmpty(dataMap) && dataMap.containsKey(MesPart.class.getSimpleName())) ?
                (MesPart) dataMap.get(MesPart.class.getSimpleName()) : partService.getPartByNo(genSerialNoModel.getOrganizeCode(), genSerialNoModel.getPartNo());

        MesCustomerPart customerPart = (!CollectionUtils.isEmpty(dataMap) && dataMap.containsKey(MesCustomerPart.class.getSimpleName()))
                ? (MesCustomerPart) dataMap.get(MesCustomerPart.class.getSimpleName())
                : customerPartService.getMesCustomerPart(genSerialNoModel.getOrganizeCode(), genSerialNoModel.getPartNo());

        if (null == part) MesPcnException.throwBusiException("请检查零件信息,零件[%s]信息不存在!", genSerialNoModel.getPartNo());
        if (null == customerPart) MesPcnException.throwBusiException("请检查客户零件信息,零件号[%s]对应的客户零件信息不存在!", genSerialNoModel.getPartNo());
        if (StringUtils.isEmpty(customerPart.getCustPartNo())) MesPcnException.throwBusiException("请检查客户零件信息,零件号[%s]对应的客户零件信息未维护客户零件号!", part.getPartNo());
        if (customerPart.getCustPartNo().length() < 4) MesPcnException.throwBusiException("请检查客户零件信息,零件号[%s]对应的客户零件号[%s]长度小于4!", part.getPartNo(), customerPart.getCustPartNo());
        if (StringUtils.isEmpty(genSerialNoModel.getShiftCode())) MesPcnException.throwBusiException("入参缺少班次代码!");

        genSerialNoModel.partSnParam(part.getPartSnParam())
                .dynamicRule(new StringJoiner(MesPcnExtConstWords.COMMA).add(customerPart.getCustPartNo().substring(customerPart.getCustPartNo().length() - 4)).add(genSerialNoModel.getShiftCode()).toString());
        //年月日缩写
        Date date = new Date();
        genSerialNoModel.setYear(getYearShort(date));
        genSerialNoModel.setMonth(getMonthShort(date));
        genSerialNoModel.setDay(getDayShort(date));
        return genSerialNoModel;
    }

    private String getYearShort(Date date) {
        return MesExtEnumUtil.YEAR_SHORT.valueOfDescription(Integer.parseInt(TimeTool.getYear(date)));
    }

    private String getMonthShort(Date date) {
        return MesExtEnumUtil.MONTH_SHORT.valueOfDescription(Integer.parseInt(TimeTool.getMonth(date)));
    }

    private String getDayShort(Date date) {
        return TimeTool.getDay(date);
    }

}
