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

import cn.estsh.i3plus.ext.mes.api.busi.ep.IEpBaseBusiReportService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPartDivulgeCheckRecord;
import cn.estsh.i3plus.ext.mes.pojo.model.ep.EpPartDivulgeCheckRecordModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesPartDivulgeCheckRecordRepository;
import cn.estsh.i3plus.ext.mes.pojo.sqlpack.MesExtHqlPack;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
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.auth.AuthUtil;
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.Iterator;
import java.util.List;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2021-12-21 10:04
 */
@Service
public class EpBaseBusiReportService implements IEpBaseBusiReportService {

    @Autowired
    private MesPartDivulgeCheckRecordRepository partDivulgeCheckRecordRepository;

    @Override
    public ListPager<EpPartDivulgeCheckRecordModel> queryPartDivulgeCheckRecord(EpPartDivulgeCheckRecordModel partDivulgeCheckRecordModel, Pager pager) {
        if(partDivulgeCheckRecordModel == null){
            pager = PagerHelper.getPager(pager, partDivulgeCheckRecordRepository.listCount());
            return new ListPager(partDivulgeCheckRecordRepository.listPager(pager),pager);
        }else{
            partDivulgeCheckRecordModel.setOrganizeCode(AuthUtil.getOrganizeCode());
            DdlPackBean packBean = MesExtHqlPack.getPartDivulgeCheckRecord(partDivulgeCheckRecordModel);
            int count = partDivulgeCheckRecordRepository.findByHqlWhereCount(packBean);
            pager = PagerHelper.getPager(pager,count);
            DdlPreparedPack.getOrderByPack(new Object[]{"2"}, new String[]{MesExtConstWords.CREATE_DATE_TIME}, packBean);
            List<MesPartDivulgeCheckRecord> mesPartDivulgeCheckRecords = partDivulgeCheckRecordRepository.findByHqlWherePage(packBean, pager);
            if(!CollectionUtils.isEmpty(mesPartDivulgeCheckRecords)){
                List<EpPartDivulgeCheckRecordModel> partDivulgeCheckRecordModelList = new ArrayList<>();
                Iterator<MesPartDivulgeCheckRecord> partDivulgeCheckRecordIterator = mesPartDivulgeCheckRecords.stream().iterator();
                while (partDivulgeCheckRecordIterator.hasNext()){
                    //数据库中记录
                    MesPartDivulgeCheckRecord mesPartDivulgeCheckRecord = partDivulgeCheckRecordIterator.next();
                    //输出记录
                    EpPartDivulgeCheckRecordModel partDivulgeCheckRecordModelTemp = new EpPartDivulgeCheckRecordModel();
                    //copy属性值
                    BeanUtils.copyProperties(mesPartDivulgeCheckRecord, partDivulgeCheckRecordModelTemp);
                    partDivulgeCheckRecordModelTemp.setCheckResultName(MesExtEnumUtil.CHECK_RESULT.valueOfDescription(partDivulgeCheckRecordModelTemp.getCheckResult()));
                    partDivulgeCheckRecordModelList.add(partDivulgeCheckRecordModelTemp);
                }
                return new ListPager(partDivulgeCheckRecordModelList,pager);
            }
            return new ListPager(mesPartDivulgeCheckRecords, pager);
        }
    }
}
