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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxPrintRecordService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkCellPrintRecordDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.PrintRecordDetailModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesWorkCellPrintRecordDetailRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.sqlpack.MesPcnExtHqlPack;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

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

    @Autowired
    private MesWorkCellPrintRecordDetailRepository workCellPrintRecordDetailRepository;

    @Override
    public ListPager queryPrinterRecordListPager(MesWorkCellPrintRecordDetail detail, Pager pager) {
        List<PrintRecordDetailModel> resultList = new ArrayList<>();

        DdlPackBean packBean = MesPcnExtHqlPack.getPrinterRecord(detail);
        List<MesWorkCellPrintRecordDetail> detailList = workCellPrintRecordDetailRepository.findByHqlWhere(packBean);
        if (!CollectionUtils.isEmpty(detailList)) {
            Map<String, Long> snMap = detailList.stream().filter(o -> null != o).collect(Collectors.groupingBy(MesWorkCellPrintRecordDetail::getSn, Collectors.counting()));

            detailList = detailList.stream().filter(o -> null != o).distinct().collect(
                    Collectors.collectingAndThen(
                            Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(MesWorkCellPrintRecordDetail::getSn))), ArrayList::new)
            );
            detailList = detailList.stream().filter(o -> null != o).sorted(Comparator.comparing(MesWorkCellPrintRecordDetail::getSn)).collect(Collectors.toList());

            pager = PagerHelper.getPager(pager, detailList.size());

            detailList = detailList.subList((pager.getCurrentPage() - 1) * pager.getPageSize(),
                    (pager.getCurrentPage() * pager.getPageSize()) > detailList.size() ? detailList.size() : pager.getCurrentPage() * pager.getPageSize());

            detailList.stream().filter(o -> null != o).forEach(o -> {
                PrintRecordDetailModel result = new PrintRecordDetailModel();
                result.setSn(o.getSn());
                result.setTagType(o.getTagType());
                result.setTagTypeName(MesPcnExtEnumUtil.PRINT_QUEUE_TYPE.valueOfDescription(o.getTagType()));
                result.setWorkOrderNo(o.getWorkOrderNo());
                result.setWorkCenterCode(o.getWorkCenterCode());
                result.setPartNo(o.getPartNo());
                result.setPartNameRdd(o.getPartNameRdd());
                result.setPrintCount(snMap.get(o.getSn()).intValue());
                resultList.add(result);
            });
        } else {
            pager = PagerHelper.getPager(pager, 0);
        }

        return new ListPager(resultList, pager);
    }

    @Override
    public List<PrintRecordDetailModel> queryPrinterRecordDetailList(String organizeCode, String sn, Integer tagType) {
        List<PrintRecordDetailModel> resultList = null;

        MesWorkCellPrintRecordDetail detail = new MesWorkCellPrintRecordDetail();
        detail.setOrganizeCode(organizeCode);
        detail.setSn(sn);
        detail.setTagType(tagType);

        DdlPackBean packBean = MesPcnExtHqlPack.getPrinterRecord(detail);
        packBean.setOrderByStr(" order by printDatetime desc ");
        List<MesWorkCellPrintRecordDetail> detailList = workCellPrintRecordDetailRepository.findByHqlWhere(packBean);
        if (!CollectionUtils.isEmpty(detailList)) {
            resultList = new ArrayList<>();
            for (MesWorkCellPrintRecordDetail item : detailList) {
                if (null == item) {
                    continue;
                }
                PrintRecordDetailModel result = new PrintRecordDetailModel();
                result.setSn(item.getSn());
                result.setTagType(item.getTagType());
                result.setTagTypeName(MesPcnExtEnumUtil.PRINT_QUEUE_TYPE.valueOfDescription(item.getTagType()));
                result.setSourceModule(item.getSourceModule());
                result.setWorkOrderNo(item.getWorkOrderNo());
                result.setWorkCenterCode(item.getWorkCenterCode());
                result.setPartNo(item.getPartNo());
                result.setPartNameRdd(item.getPartNameRdd());

                result.setPrintDatetime(item.getPrintDatetime());
                result.setPrintUser(item.getCreateUser());
                resultList.add(result);
            }
        }
        return resultList;
    }
}
