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.pojo.mes.bean.MesCustomerPart;
import cn.estsh.i3plus.pojo.mes.model.GenSerialNoModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * @Description : MES-客户条码（成都）
 * @Reference :
 * @Author : jason.niu
 * @CreateDate 2025/03/13 16:47
 * @Modify:
 **/
@Component
public class ChengDuSimpleYearNumberRuleStrategyService implements INumberRulePackAttributeStrategyService {

    @Autowired
    private IMesCustomerPartService mesCustomerPartService;

    @Override
    public GenSerialNoModel execute(GenSerialNoModel genSerialNoModel) {
        Map<String, Object> dataMap = genSerialNoModel.getDataMap();
        MesCustomerPart customerPart = (!CollectionUtils.isEmpty(dataMap) && dataMap.containsKey(MesCustomerPart.class.getSimpleName())) ?
                (MesCustomerPart)dataMap.get(MesCustomerPart.class.getSimpleName()) : mesCustomerPartService.getMesCustomerPart(genSerialNoModel.getOrganizeCode(), genSerialNoModel.getPartNo());
        if (Objects.isNull(customerPart)) {
            MesPcnException.throwMesBusiException("请检查客户零件信息,零件[%s]客户零件关系未维护", genSerialNoModel.getPartNo());
        }
        genSerialNoModel.setCustPartNo(customerPart.getCustPartNo());
        Date date = new Date();
        genSerialNoModel.setYear(getYear(date));
        return genSerialNoModel;
    }

    private String getYear(Date date) {
        return (new SimpleDateFormat("yy")).format(date);
    }
}
