package com.sw.mes.mcs.enent;

import com.sw.mes.base.system.entity.DictionaryItem;
import com.sw.mes.base.system.service.DictionaryItemService;
import com.sw.mes.fb.utils.StringUtils;
import com.sw.mes.mcs.dto.SquareRankDto;
import com.sw.mes.mcs.service.MesToMcsService;
import com.sw.mes.wms.dto.FbRatingResultDto;
import com.sw.mes.wms.dto.FbRatingResultOfDto;
import com.sw.mes.wms.service.RatingResultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 方棒评级结果下发监听
 * @Author: GaoXinJian
 * @Date: 2022-01-18 16:46
 */
@Slf4j
@Component
public class SquareRankEventListener implements SmartApplicationListener {
    @Autowired
    private MesToMcsService mesToMcsService;
    @Autowired
    private RatingResultService ratingResultService;

    @Autowired
    private DictionaryItemService dictionaryItemService;
    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> aClass) {
        return aClass== SquareRankEvent.class;
    }

    @Override
    @Async
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
    SquareRankEvent squareRankEvent =(SquareRankEvent) applicationEvent;
        FbRatingResultDto fbRatingResultDto = squareRankEvent.fbRatingResultDto;
        //下发wms
        //ratingResultService.ratingResult(fbRatingResultDto);
        //下发mcs
        SquareRankDto squareRankDto = new SquareRankDto();
        squareRankDto.setSectionNo(fbRatingResultDto.getSn());
        if (fbRatingResultDto.getResultOf() != null) {
            FbRatingResultOfDto resultOf = fbRatingResultDto.getResultOf();
            squareRankDto.setProductRank(resultOf.getProductLevel());
            squareRankDto.setTestRank(resultOf.getFbExperiLevel());
            squareRankDto.setReCheck(StringUtils.isBlank(resultOf.getReCheckType()) || "0".equals(resultOf.getReCheckType()) ? "0":"1");
            squareRankDto.setIsFbSizeCheck(resultOf.getIsFbSizeCheck());
            squareRankDto.setOrderNo(fbRatingResultDto.getOrderNo()==null?"MKFB0000000001":fbRatingResultDto.getOrderNo());
            squareRankDto.setConductiveType(resultOf.getConductiveType());
            squareRankDto.setProductGrade(resultOf.getProductGrade());
            squareRankDto.setProductType(getProductType(resultOf.getProductType()));
            String gradeType = resultOf.getGradeType();
            squareRankDto.setGradeType(gradeType);
            squareRankDto.setLength(resultOf.getLength());
            squareRankDto.setSpecification(resultOf.getSpecification());
            squareRankDto.setSideLengthRange(resultOf.getSideLengthRange());
            squareRankDto.setSectionNo(fbRatingResultDto.getSn());
        }
        mesToMcsService.squareRank(squareRankDto);

        try {
            //方棒评级结果下发MCS
            mesToMcsService.squareRank1(squareRankDto);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 规格转换
     */
    private String getProductType(String productType) {
        List<DictionaryItem> toMCSProductSpec = dictionaryItemService.findByThemeValueAndEnable("ToMCSProductSpec", true);
        Map<String, DictionaryItem> specMap = toMCSProductSpec.stream().collect(Collectors.toMap(DictionaryItem::getName, Function.identity(), (t1, t2) -> t1));
        if (specMap.containsKey(productType)) {
            return specMap.get(productType).getValue();
        } else {
            return "0";
        }
    }
}
