package com.sw.mes.yb.scheduled;

import com.sw.mes.core.utils.NumberUtil;
import com.sw.mes.base.factory.init.dto.SortingConfigOrderInfoDto;
import com.sw.mes.base.factory.init.entity.SortingConfig;
import com.sw.mes.base.factory.init.service.SortingConfigService;
import com.sw.mes.yb.mcs.dto.BlankConfigDto;
import com.sw.mes.yb.mcs.event.BlankConfigEvent;
import com.sw.mes.yb.mcs.event.EventPublishService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.*;

@Slf4j
@Component
public class BlanConfigScheduling {
    @Autowired
    @Qualifier("eventPublishService")
    private EventPublishService eventPublishService;
    @Autowired
    private SortingConfigService sortingConfigService;

    @XxlJob("blanConfigScheduling")
    public void blanConfigScheduling() {
        try {
            List<SortingConfig> sortingConfigs = sortingConfigService.findAll();
            Map<String, SortingConfigOrderInfoDto> sortingConfigOrderInfoMap = sortingConfigService.getSortingConfigOrderInfoMap();
            Map<String, BlankConfigDto> mapBindingInfoDtos = new HashMap<>();
            for (SortingConfig sortingConfig : sortingConfigs) {
                if (!mapBindingInfoDtos.containsKey(sortingConfig.getSortingPort())) {
                    SortingConfigOrderInfoDto sortingConfigOrderInfo = sortingConfigOrderInfoMap.get(sortingConfig.getSortingPort());
                    if (sortingConfigOrderInfo.getStatus()) {
                        if (sortingConfigOrderInfo.getDeleted()) {
                            BlankConfigDto blankConfigDto = new BlankConfigDto();
                            blankConfigDto.setOrderNo(null);
                            blankConfigDto.setSpell(null);
                            blankConfigDto.setBlankNo(sortingConfigOrderInfo.getSortingPort());
                            blankConfigDto.setRanks(null);
                            sortingConfig.setStatus(false);
                            mapBindingInfoDtos.put(sortingConfigOrderInfo.getSortingPort(), blankConfigDto);
                            continue;
                        } else {
                            mapBindingInfoDtos.put(sortingConfigOrderInfo.getSortingPort(), setSortingConfig(sortingConfig, sortingConfigOrderInfo));
                            continue;
                        }
                    }
                    if (sortingConfig.getOrderNos() != null || sortingConfigOrderInfo.getAufnr() != null) {
                        if (sortingConfigOrderInfo.getAufnr() != null) {
                            if (!sortingConfigOrderInfo.getAufnr().equals(sortingConfig.getOrderNos())) {
                                mapBindingInfoDtos.put(sortingConfigOrderInfo.getSortingPort(), setSortingConfig(sortingConfig, sortingConfigOrderInfo));
                            }
                        } else {
                            if (!sortingConfig.getOrderNos().equals(sortingConfigOrderInfo.getAufnr())) {
                                mapBindingInfoDtos.put(sortingConfigOrderInfo.getSortingPort(), setSortingConfig(sortingConfig, sortingConfigOrderInfo));
                            }
                        }
                    }
                }
            }
            if (mapBindingInfoDtos.size() > 0) {
                sortingConfigService.saveAll(sortingConfigs);
                List<BlankConfigDto> blankConfigDtos = new ArrayList<>();
                mapBindingInfoDtos.forEach((key, value) -> blankConfigDtos.add(value));
                eventPublishService.publishEvent(new BlankConfigEvent(this, blankConfigDtos));
            }
        } catch (Exception e) {
            log.error("分选口下发失败：", e);
        }
    }

    private BlankConfigDto setSortingConfig(SortingConfig sortingConfig, SortingConfigOrderInfoDto sortingConfigOrderInfo) {
        sortingConfig.setOrderNos(sortingConfigOrderInfo.getAufnr());
        sortingConfig.setStatus(false);
        BlankConfigDto blankConfigDto = new BlankConfigDto();
        blankConfigDto.setOrderNo(sortingConfigOrderInfo.getAufnr());
        blankConfigDto.setSpell(NumberUtil.object2String(sortingConfigOrderInfo.getStickCount(), null));
        blankConfigDto.setBlankNo(sortingConfigOrderInfo.getSortingPort());
        blankConfigDto.setRanks(sortingConfigOrderInfo.getYbProductGrade());
        return blankConfigDto;
    }
}
