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

import cn.estsh.i3plus.ext.mes.api.base.IReportExtService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesBadCell;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesBadCellDetail;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProdUnbindRecord;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesBadCellDetailRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesBadCellRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProdUnbindRecordRepository;
import cn.estsh.i3plus.ext.mes.pojo.sqlpack.MesExtHqlPack;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
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.mes.repository.MesProdBindRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Service
public class ReportExtService implements IReportExtService {

    @Autowired
    private MesBadCellRepository mesBadCellRDao;

    @Autowired
    private MesBadCellDetailRepository mesBadCellDetailRDao;

    @Autowired
    private MesProdUnbindRecordRepository prodUnbindRecordRepository;

    @Override
    public ListPager<MesBadCell> queryMesBadCell(MesBadCell mesBadCell, Pager pager) {
        DdlPackBean hqlPack = MesExtHqlPack.getMesBadCell(mesBadCell);
        hqlPack.setOrderByStr(mesBadCell.orderBy());
        pager = PagerHelper.getPager(pager, mesBadCellRDao.findByHqlWhereCount(hqlPack));
        return new ListPager(mesBadCellRDao.findByHqlWherePage(hqlPack , pager), pager);
    }

    @Override
    public List<MesBadCellDetail> findMesBadCellDetail(MesBadCellDetail mesBadCellDetail) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(mesBadCellDetail.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(mesBadCellDetail.getWorkOrderNo(), MesExtConstWords.WORK_ORDER_NO, packBean);
        DdlPreparedPack.getStringEqualPack(mesBadCellDetail.getWorkCellCode(), MesExtConstWords.WORK_CELL_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(mesBadCellDetail.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(mesBadCellDetail.getWorkTime(), MesExtConstWords.WORK_TIME, packBean);
        DdlPreparedPack.getStringEqualPack(mesBadCellDetail.getShiftCode(), MesExtConstWords.SHIFT_CODE, packBean);
        return mesBadCellDetailRDao.findByHqlWhere(packBean);
    }

    @Override
    public ListPager<MesProdUnbindRecord> queryProdUnbindRecord(MesProdUnbindRecord prodUnbindRecord, Pager pager) {
        DdlPackBean packBean = MesExtHqlPack.getProdUnbindRecord(prodUnbindRecord);
        pager = PagerHelper.getPager(pager, prodUnbindRecordRepository.findByHqlWhereCount(packBean));
        packBean.setOrderByStr(" order by modifyDatetime desc ");
        return new ListPager(prodUnbindRecordRepository.findByHqlWherePage(packBean , pager), pager);
    }
}
