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;

@Component
public class XiaoPengNumberRule2StrategyService implements INumberRulePackAttributeStrategyService {
    @Autowired
    private IMesCustomerPartService customerPartService;

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

        //年月日缩写
        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_SHORT2.valueOfDescription(Integer.parseInt(TimeTool.getYear(date)));
    }

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

    private String getDayShort(Date date) {
        return MesExtEnumUtil.DAY_SHORT2.valueOfDescription(Integer.parseInt(TimeTool.getDay(date)));
    }
}
