package com.scm.contract.service.print;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.scm.contract.dao.print.PrintManagementDao;
import com.scm.contract.model.apply.DictionaryTagType;
import com.scm.contract.model.apply.LabelApplication;
import com.scm.contract.model.print.DictionaryPrintStatus;
import com.scm.contract.model.print.PrintManagement;
import com.scm.contract.model.print.SerialNumberRange;
import com.scm.contract.service.apply.DictionaryTagTypeService;
import com.scm.contract.service.apply.LabelApplicationService;
import com.set.direwolf.core.mybatis.CrudService;
import com.set.direwolf.core.mybatis.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author zuo
 * @date 2024/7/31 12:11
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class PrintManagementService extends CrudService<PrintManagementDao, PrintManagement, String> {

    @Autowired
    private LabelApplicationService labelApplicationService;

    @Autowired
    private DictionaryTagTypeService tagTypeService;

    @Autowired
    private PrintCodeService printCodeService;

    @Autowired
    private DictionaryPrintStatusService printStatusService;

    @Autowired
    private ReprintCodeDetailService reprintCodeDetailService;

    public void setName(List<PrintManagement> printManagements) {
        if (CollectionUtils.isNotEmpty(printManagements)) {

            Set<String> tagTypeIds = new HashSet<>();
            Set<String> detailIds = new HashSet<>();
            Set<String> reprintCodeIds = new HashSet<>();

            Set<String> printStatusIds = new HashSet<>();
            for (PrintManagement printManagement : printManagements) {
                Boolean isReprint = printManagement.getIsReprint();
                if (!Objects.isNull(isReprint)){
                    String printManagementId = printManagement.getId();
                    if (isReprint) {
                        reprintCodeIds.add(printManagementId);
                    }else {
                        detailIds.add(printManagementId);
                    }
                }
                tagTypeIds.add(printManagement.getTagTypeId());
                Integer printStatus = printManagement.getPrintStatus();
                if (!Objects.isNull(printStatus)) {
                    printStatusIds.add(printStatus.toString());
                }
            }
            Map<String, DictionaryTagType> tagTypeMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(tagTypeIds)){
                List<DictionaryTagType> tagTypes = tagTypeService.listByIds(new ArrayList<>(tagTypeIds));
                if (CollectionUtils.isNotEmpty(tagTypes)) {
                    for (DictionaryTagType tagType : tagTypes) {
                        tagTypeMap.put(tagType.getId(), tagType);
                    }
                }
            }


            Map<String, SerialNumberRange> serialNumberRangeMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(detailIds)){
                List<SerialNumberRange> serialNumberRanges = printCodeService.getSerialNumberRange(new ArrayList<>(detailIds));
                for (SerialNumberRange serialNumberRange : serialNumberRanges) {
                    serialNumberRangeMap.put(serialNumberRange.getId(), serialNumberRange);
                }
            }
            if (CollectionUtils.isNotEmpty(reprintCodeIds)){
                List<SerialNumberRange> serialNumberRanges = reprintCodeDetailService.getSerialNumberRange(new ArrayList<>(reprintCodeIds));
                for (SerialNumberRange serialNumberRange : serialNumberRanges) {
                    serialNumberRangeMap.put(serialNumberRange.getId(), serialNumberRange);
                }
            }

            Map<Integer, DictionaryPrintStatus> printStatusesMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(printStatusIds)) {
                List<DictionaryPrintStatus> printStatuses = printStatusService.listByIds(new ArrayList<>(printStatusIds));
                for (DictionaryPrintStatus printStatus : printStatuses) {
                    printStatusesMap.put(printStatus.getId(), printStatus);
                }
            }

            for (PrintManagement printManagement : printManagements) {

                String tagTypeId = printManagement.getTagTypeId();
                DictionaryTagType tagType = tagTypeMap.getOrDefault(tagTypeId, new DictionaryTagType());
                printManagement.setTagTypeName(tagType.getName());

                String printManagementId = printManagement.getId();
                SerialNumberRange serialNumberRange = serialNumberRangeMap.get(printManagementId);
                if (!Objects.isNull(serialNumberRange)) {
                    List<String> list = new ArrayList<>();
                    list.add(serialNumberRange.getMin());
                    list.add(serialNumberRange.getMax());
                    printManagement.setSerialNumberRange(list);
                }

                Integer printStatus = printManagement.getPrintStatus();
                if (!Objects.isNull(printStatus)) {
                    DictionaryPrintStatus printStatus1 = printStatusesMap.getOrDefault(printStatus, new DictionaryPrintStatus());
                    printManagement.setPrintStatusName(printStatus1.getName());
                }
            }
        }
    }

    public Page<PrintManagement> findPage(Page<PrintManagement> page, PrintManagement entity) {
        entity.setPage(page);
        page.setList(this.dao.findList(entity));
        List<PrintManagement> list = page.getList();

        if (CollectionUtils.isNotEmpty(list)){
            this.setName(list);
            Set<String> labelApplicationIds = new HashSet<>();
            for (PrintManagement printManagement : list) {
                labelApplicationIds.add(printManagement.getLabelApplicationId());
            }
            //设置供应商名称
            List<LabelApplication> labelApplications = labelApplicationService.getListByIds(new ArrayList<>(labelApplicationIds));
            labelApplicationService.setName(labelApplications, false);
            Map<String, String> map = new HashMap<>();
            for (LabelApplication labelApplication : labelApplications) {
                map.put(labelApplication.getId(), labelApplication.getProviderName());
            }
            for (PrintManagement printManagement : list) {
                String labelApplicationId = printManagement.getLabelApplicationId();
                printManagement.setProviderName(map.get(labelApplicationId));
            }
        }
        return page;
    }
}
