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.mes.pcn.api.iservice.busi.INumberRulePackAttributeStrategyService;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
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.StringUtils;

import java.util.Date;

/**
 * @Description :
 * @Reference :
 * @Author : Castle
 * @CreateDate : 2024/6/26 9:38
 * @Modify:
 **/
@Component
public class XiaoPengNewNumberRuleStrategyService implements INumberRulePackAttributeStrategyService {
    @Autowired
    private IMesCustomerPartService customerPartService;

    @Override
    public GenSerialNoModel execute(GenSerialNoModel model) {

        if (StringUtils.isEmpty(model.getOrganizeCode()))
            MesPcnException.throwMesBusiException("XiaoPengNumberRuleStrategyService执行异常:缺失组织代码参数");
        if (StringUtils.isEmpty(model.getPartNo()))
            MesPcnException.throwMesBusiException("XiaoPengNumberRuleStrategyService执行异常:缺失零件编码参数");

        final String custPartNo = customerPartService.getCustPartNo(model.getOrganizeCode(), model.getPartNo());
        if (StringUtils.isEmpty(custPartNo)) {
            MesPcnException.throwMesBusiException("请检查客户零件信息,零件[%s]客户零件关系未维护", model.getPartNo());
        }

        model.setCustPartNo(custPartNo);

        Date date = new Date();
        // XIAO_PENG 原客户条码.小鹏汽车 NoSortBarCodeGZ-new
        //{custPartNo}{SPILTRULE}{year}{month}{day}{hour}{minute}{second}{serialNo}
        //客户零件号
        //String custPartNo = model.getCustPartNo();
        //日期${dateShort}
        model.setYear(getYearShort(date));
        model.setMonth(getMonthShort(date));
        model.setDay(getDayShort(date));
        //中杠 -
        //生产日期 + 时间 yyyyMMddHHmmss
        //流水号 5位 每天从1开始
        return model;
    }

    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);
    }
}
