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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesWorkCenterService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.base.util.StringUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesProduceSn;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkCenter;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrder;
import cn.estsh.i3plus.pojo.mes.repository.MesWorkCenterRepository;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description :
 * @Reference :
 * @Author :
 * @CreateDate : 2024-05-09 10:54
 * @Modify:
 **/

@Service
@Slf4j
public class MesWorkCenterService implements IMesWorkCenterService {

    @Autowired
    private MesWorkCenterRepository workCenterRepository;


    @Override
    public MesWorkCenter queryMesWorkCenter(MesWorkCenter workCenter) {
        DdlPackBean packBean = getDdlPackBean(workCenter);
        MesWorkCenter result = workCenterRepository.getByProperty(packBean);
        return result;
    }

    @Override
    public List<MesWorkCenter> queryMesWorkCenterList(MesWorkCenter workOrder) {
        DdlPackBean packBean = getDdlPackBean(workOrder);

        return workCenterRepository.findByHqlWhere(packBean);
    }

    @Override
    public ListPager<MesWorkCenter> queryMesWorkCenterByPager(MesWorkCenter workCenter, Pager pager) {

        DdlPackBean packBean = getDdlPackBean(workCenter);

        pager = PagerHelper.getPager(pager, workCenterRepository.findByHqlWhereCount(packBean));

        List<MesWorkCenter> resultList = workCenterRepository.findByHqlWherePage(packBean, pager);

        return new ListPager<>(resultList, pager);
    }

    @Override
    public List<MesWorkCenter> queryMesWorkCenterList(List<String> workCenterCodeList, String organizeCode) {

        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getInPackList(workCenterCodeList, MesPcnExtConstWords.WORK_CENTER_CODE, packBean);
        return workCenterRepository.findByHqlWhere(packBean);

    }

    private DdlPackBean getDdlPackBean(MesWorkCenter workCenter) {

        DdlPackBean packBean = DdlPackBean.getDdlPackBean(workCenter.getOrganizeCode());
        if (!StringUtil.isEmpty(workCenter.getCenterType()) && 0 != workCenter.getCenterType()) {
            DdlPreparedPack.getNumEqualPack(workCenter.getCenterType(), "centerType", packBean);
        }
        DdlPreparedPack.getStringLikerPack(workCenter.getWorkCenterCode(), "workCenterCode", packBean);
        return packBean;
    }
}
