package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.base.ep;

import cn.estsh.i3plus.ext.mes.pcn.api.base.ep.IEpBomExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.sqlpack.MesPcnExtHqlPack;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesBom;
import cn.estsh.i3plus.pojo.mes.repository.MesBomRepository;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2021-12-29 13:53
 */
@Service
public class EpBomExtService implements IEpBomExtService {

    @Autowired
    private MesBomRepository bomRepository;

    @Override
    public ListPager<MesBom> getBomListByPartNoAndPager(String organizeCode, String partNo, String itemPartNo, String itemPartName, Pager pager) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(partNo)) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("入参缺少物料号[%s]",partNo)
                    .build();
        }
        DdlPackBean hqlPack = MesPcnExtHqlPack.getBom(organizeCode, partNo, itemPartNo, itemPartName);
        pager = PagerHelper.getPager(pager, bomRepository.findByHqlWhereCount(hqlPack));
        hqlPack.setOrderByStr(" order by modifyDatetime desc ");
        return new ListPager(bomRepository.findByHqlWherePage(hqlPack, pager), pager);
    }
}
