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

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.busi.INumberRulePackAttributeStrategyService;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesCustomerPart;
import cn.estsh.i3plus.pojo.mes.model.GenSerialNoModel;
import cn.estsh.i3plus.pojo.mes.repository.MesCustomerPartRepository;
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;

@Component
public class XiaoPengNumberRule2StrategyService implements INumberRulePackAttributeStrategyService {

    @Autowired
    private MesCustomerPartRepository customerPartRepository;

    @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()) : getMesCustomerPart(genSerialNoModel.getOrganizeCode(), genSerialNoModel.getPartNo());
        if (null != customerPart) {
            genSerialNoModel.setCustPartNo(customerPart.getCustPartNo());
        }else {
            MesPcnException.throwMesBusiException("请检查客户零件信息,零件[%s]客户零件关系未维护", genSerialNoModel.getPartNo());
        }

        //年月日缩写
        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)));
    }

    private MesCustomerPart getMesCustomerPart(String orgaizeCode, String partNo) {
        if (StringUtils.isEmpty(orgaizeCode) || StringUtils.isEmpty(partNo)) return null;
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(orgaizeCode);
        DdlPreparedPack.getStringEqualPack(partNo, MesPcnExtConstWords.ERP_PART_NO, ddlPackBean);
        MesCustomerPart customerPart = customerPartRepository.getByProperty(ddlPackBean);
        return customerPart;
    }

}
