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

import cn.estsh.i3plus.ext.mes.pcn.api.base.ep.IHappenStationService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesHappenStation;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxHappenStationModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesHappenStationRepository;
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.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2021-12-16 10:32
 */
@Service
public class HappenStationService implements IHappenStationService {

    @Autowired
    private MesHappenStationRepository happenStationRepository;


    @Override
    public ListPager<MesHappenStation> queryByPager(MesHappenStation happenStation, Pager pager) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(happenStation.getOrganizeCode());
        DdlPreparedPack.getStringLikerPack(happenStation.getWorkCenterCode(), MesPcnExtConstWords.WORK_CENTER_CODE,packBean);
        DdlPreparedPack.getStringLikerPack(happenStation.getWorkCellCode(),MesPcnExtConstWords.WORK_CELL_CODE,packBean);
        DdlPreparedPack.getStringLikerPack(happenStation.getHappenStation(),MesPcnExtConstWords.HAPPEN_STATION,packBean);
        pager = PagerHelper.getPager(pager,happenStationRepository.findByHqlWhereCount(packBean));
        return new ListPager(happenStationRepository.findByHqlWherePage(packBean, pager), pager);
    }

    @Override
    public ListPager<SxHappenStationModel> queryByPager(SxHappenStationModel happenStation, Pager pager) {
        List<SxHappenStationModel> resultList = new ArrayList<>();
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(happenStation.getOrganizeCode());
        DdlPreparedPack.getStringLikerPack(happenStation.getWorkCenterCode(), MesPcnExtConstWords.WORK_CENTER_CODE,packBean);
        DdlPreparedPack.getStringLikerPack(happenStation.getWorkCellCode(),MesPcnExtConstWords.WORK_CELL_CODE,packBean);
        DdlPreparedPack.getStringLikerPack(happenStation.getHappenStation(),MesPcnExtConstWords.HAPPEN_STATION,packBean);
        pager = PagerHelper.getPager(pager,happenStationRepository.findByHqlWhereCount(packBean));
        List<MesHappenStation> happenStationList = happenStationRepository.findByHqlWherePage(packBean,pager);
        if (!CollectionUtils.isEmpty(happenStationList)) {
            for (MesHappenStation item : happenStationList) {
                if (null == item) {
                    continue;
                }
                SxHappenStationModel result = new SxHappenStationModel();
                BeanUtils.copyProperties(item, result);
                resultList.add(result);
            }
        }
        return new ListPager(resultList,pager);
    }
}
