package com.sw.mes.yb.mcs.event;

import com.sw.mes.yb.pad.service.ProductionPaintingLineDetailService;
import com.sw.mes.yb.wms.utils.ResultWmsDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.sw.mes.core.dto.RankResultDto;
import com.sw.mes.yb.mcs.service.YbMesToMcsService;
import com.sw.mes.yb.wms.service.RatingResultService;
import com.sw.mes.yb.wms.utils.RequestWmsDto;
import com.sw.mes.yb.wms.utils.RequestWmsUtils;

/**
 * 圆棒评级结果下发监听
 *
 * @Author: GaoXinJian
 * @Date: 2022-02-07 17:46
 */
@Component
public class RankResultEventListener implements SmartApplicationListener {
    @Autowired
    YbMesToMcsService mesToMcsService;

    @Autowired
    private RequestWmsUtils requestUtilsWms;

    @Autowired
    private ProductionPaintingLineDetailService productionPaintingLineDetailService;
    private final String rankResultUrl="/wms/mesToWmsController/rankResult";
    @Lazy
    @Autowired
    private RatingResultService ratingResultService;
//    @Autowired
//    ProductionBindingTrayService productionBindingTrayService;
//    @Autowired
//    InventoryManageInfoService inventoryManageInfoService;
    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> aClass) {
        return aClass == RankResultEvent.class;
    }

    @Override
    @Async
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        RankResultEvent rankResultEvent = (RankResultEvent) applicationEvent;
        //下发mes
        RankResultDto rankResultDto = rankResultEvent.rankResultDto;
//        String sectionType = rankResultDto.getType();
//        BigDecimal length = null;
        if (rankResultDto.getIsCutBack() != null && rankResultDto.getIsCutBack() == 1){
            rankResultDto.setType("反切");
        }else{
            if (rankResultDto.getSticksCount() != null && rankResultDto.getSticksCount().compareTo(1) > 0){
                rankResultDto.setType("拼棒");
//                length = rankResultDto.getLength();
            }
        }
        String crystalNo = rankResultDto.getCrystalNo();
        String sectionNo = rankResultDto.getSectionNo();
//        if(!"外观反切".equals(rankResultDto.getGradeType())) {
//            Boolean aBoolean = productionPaintingLineDetailService.flowDirectionV2(crystalNo, sectionNo, "评级结果下发");
//            if (aBoolean) {
//                rankResultDto.setType("完结尾");
//            }
//        }

        rankResultDto.setCrystalLength(rankResultDto.getLength());
        mesToMcsService.rankResult(rankResultDto);
//        requestUtilsWms.postForWms(new RequestWmsDto(rankResultUrl,rankResultDto, 2));
        //mq
        ratingResultService.ratingResult(rankResultDto);
//        //下发wms
//        try{
//            //查询是否为wms入库晶段
//            InventoryManageInfo oneByRoundSnOrderByIdDesc = inventoryManageInfoService.findOneByRoundSnAndCrystalTypeOrderByIdDesc(rankResultDto.getSectionNo(),0);
//            if (oneByRoundSnOrderByIdDesc != null){
//                log.info("圆棒评级结果下发开始");
//                //查询托盘绑定信息
//                RatingResultDto ratingResultDto = new RatingResultDto();
//                ProductionBindingTray productionBindingTray = productionBindingTrayService.getByCrystalNo(rankResultDto.getSectionNo());
//                if (productionBindingTray != null){
//                    ratingResultDto.setTrayCode(productionBindingTray.getTrayNo());
//                }
//                ratingResultDto.setSectionNo(rankResultDto.getCrystalNo());
//                ratingResultDto.setRoundSn(rankResultDto.getSectionNo());
//                ratingResultDto.setProductLevel(rankResultDto.getProductRank());
//                ratingResultDto.setProductLevelExperiment(rankResultDto.getTestRank());
//                ratingResultDto.setData(sectionType);
//                ratingResultDto.setSectionType(sectionType);
//                ratingResultDto.setProductType(rankResultDto.getSpecs());
//                ratingResultDto.setWareHouseLength(rankResultDto.getWareHouseLength());
//                ratingResultDto.setActualLength(rankResultDto.getActualLength());
//                ratingResultDto.setSpellBarLength(length);
//                ratingResultDto.setActualWeight(rankResultDto.getActualWeight());
//                ratingResultDto.setTheoryWeight(rankResultDto.getTheoryWeight());
//                ratingResultDto.setDiameterMin(rankResultDto.getDiameterMin());
//                ratingResultDto.setDiameterMax(rankResultDto.getDiameterMax());
//                ratingResultDto.setDiameterJudge(rankResultDto.getDiameterJudge());
//                ratingResultDto.setDistortion(rankResultDto.getDistortion());
//                ratingResultDto.setIsBevel(rankResultDto.getIsBevel());
//                ratingResultDto.setConductiveType(rankResultDto.getConductiveType());
//                ratingResultDto.setFineDiameter(rankResultDto.getFineDiameter());
//                ratingResultDto.setFineCutLength(rankResultDto.getFineCutLength());
//                ratingResultDto.setPiecewiseNum(rankResultDto.getPiecewiseNum());
//                ratingResultDto.setOrderNo(rankResultDto.getOrderNo());
//                ratingResultDto.setAgainNum(rankResultDto.getAgainNum());
//                ratingResultDto.setHeadTailRecognition(rankResultDto.getHeadTailRecognition());
//                ratingResultDto.setIsSeeBottom(rankResultDto.getIsSeeBottom());
//                ratingResultDto.setLineMarking(rankResultDto.getLineMarking());
//                ratingResultDto.setIsCutBack(rankResultDto.getIsCutBack());
//                ResultWmsDto resultWmsDto = ratingResultService.ratingResult(ratingResultDto);
//                log.info("圆棒评级结果下发结束"+resultWmsDto);
//            }
//        }catch(Exception e){
//
//        }
    }
}
