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

import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxOrganizeExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxWorkCellModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.sqlpack.MesPcnExtHqlPack;
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.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkCell;
import cn.estsh.i3plus.pojo.mes.repository.MesWorkCellRepository;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class SxOrganizeExtService implements ISxOrganizeExtService {

    @Autowired
    private MesWorkCellRepository workCellRepository;

    @Override
    public List<MesWorkCell> findWorkCell(String organizeCode, String workCenterCode) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(workCenterCode, "workCenterCode", packBean);
        DdlPreparedPack.getNumNOEqualPack(MesPcnEnumUtil.MES_WORK_CELL_TYPE.REWORK.getValue(), "workCellType", packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{1}, new String[]{"seq"}, packBean);
        return this.workCellRepository.findByHqlWhere(packBean);
    }

    @Override
    public ListPager<SxWorkCellModel> queryValidWorkCellListPage(SxWorkCellModel workCellModel, Pager pager) {
        List<SxWorkCellModel> resultList = new ArrayList<>();
        DdlPackBean packBean = MesPcnExtHqlPack.getValidWorkCell(workCellModel);
        DdlPreparedPack.getOrderByPack(
                new Object[]{CommonEnumUtil.ASC_OR_DESC.ASC.getValue(), CommonEnumUtil.ASC_OR_DESC.ASC.getValue()},
                new String[]{MesPcnExtConstWords.WORK_CENTER_CODE, MesPcnExtConstWords.WORK_CELL_CODE}, packBean);
        pager = PagerHelper.getPager(pager, workCellRepository.findByHqlWhereCount(packBean));
        List<MesWorkCell> workCellList = workCellRepository.findByHqlWhere(packBean);
        if (!CollectionUtils.isEmpty(workCellList)) {
            for (MesWorkCell workCell : workCellList) {
                if (null == workCell) {
                    continue;
                }
                SxWorkCellModel result = new SxWorkCellModel();
                BeanUtils.copyProperties(workCell, result);
                resultList.add(result);
            }
        }
        return new ListPager<>(resultList, pager);
    }
}
