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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.mes.pcn.api.iservice.base.IPartService;
import cn.estsh.i3plus.mes.pcn.api.iservice.busi.INumberRulePackAttributeStrategyService;
import cn.estsh.i3plus.pojo.mes.bean.MesPart;
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 org.springframework.util.StringUtils;

import java.util.Map;

@Component
public class LiJiaPartSnParamRuleStrategyService implements INumberRulePackAttributeStrategyService {
    @Autowired
    private IPartService partService;

    @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());
        if (null == part) {
            MesPcnException.throwBusiException("请检查零件信息,零件号[%s]信息不存在!", genSerialNoModel.getPartNo());
        }
        if (StringUtils.isEmpty(part.getPartSnParam())) {
            MesPcnException.throwBusiException("请检查零件信息,零件号[%s]未维护零件条码参数!", part.getPartNo());
        }

        genSerialNoModel.partSnParam(part.getPartSnParam());
        return genSerialNoModel;
    }
}
