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

import cn.estsh.i3plus.ext.mes.api.base.bu.IBusiConfigService;
import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataPushNotifyStrategyFlagService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesBusiConfig;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkCenterNotReachReason;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxPushDataNotifyParamModel;
import cn.estsh.i3plus.pojo.model.platform.PushDataNotifyModel;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private IBusiConfigService busiConfigService;

    @Override
    public List<PushDataNotifyModel> exec(PushDataNotifyModel data, SxPushDataNotifyParamModel pushParam, Object... obj) {

        log.info("生产数据推送通知数据处理 --- SxProdDataPushNotifyStrategyProdRunConditionEmailService --- START --- DATA:{} --- THREAD:{}", JSONObject.toJSONString(data), Thread.currentThread().getName());

        List<MesWorkCenterNotReachReason> workCenterNotReachReasonsList = (List<MesWorkCenterNotReachReason>) obj[0];
        Map<String, String> categoryReachRate = (Map<String, String>) obj[1];
        Map<String, String> workCenterTotalRateMap = (Map<String, String>) obj[2];
        Map<String, String> totalUnWorkTimeMapByWorkCenterCode = (Map<String, String>) obj[3];
        Map<String, String> assistMapByWorkCenterCode = (Map<String, String>) obj[4];

        List<PushDataNotifyModel> resultList = new ArrayList<>();

        List<String> dataList = new ArrayList<>();
        // 产线别生产达成率
        if (!CollectionUtils.isEmpty(workCenterNotReachReasonsList)) {
            dataList.add("<b>产线别生产达成率/完结率</b>");
            for (MesWorkCenterNotReachReason workCenterNotReachReason : workCenterNotReachReasonsList) {
                dataList.add(workCenterNotReachReason.getWorkCenterCode() + "：" +
                        workCenterNotReachReason.getCompleteQty().intValue() + "/"  + workCenterNotReachReason.getPlanQty().intValue() + "&nbsp;&nbsp;" +
                        workCenterNotReachReason.getYieldRateStr() + "&nbsp;&nbsp;" +
                        workCenterNotReachReason.getCompleteRateStr() + "&nbsp;&nbsp;" +
                        workCenterNotReachReason.getPartNo()  + "&nbsp;&nbsp;" +
                        workCenterNotReachReason.getNotReachReason());
            }
        }

        addListMailByStringDataList(categoryReachRate, dataList, "<b>产品别生产达成率/完结率</b>");
        addListMailByStringDataList(workCenterTotalRateMap, dataList, "<b>产线综合能率</b>");
        addListMailByStringDataList(totalUnWorkTimeMapByWorkCenterCode, dataList, "<b>产线非作业时间</b>");
        addListMailByStringDataList(assistMapByWorkCenterCode, dataList, "<b>产线援出</b>");

        MesBusiConfig prodPushUserConfig = busiConfigService.getBusiConfigByCfgCode(pushParam.getOrganizeCode(), "PROD_PUSH_USER_EMAIL");
        if (prodPushUserConfig == null || StringUtils.isEmpty(prodPushUserConfig.getCfgValue())) {
            log.info("生产数据推送通知数据处理 --- SxProdDataPushNotifyStrategyProdRunConditionEmailService --- DISPOSE --- 未维护通知人 --- THREAD:{}", Thread.currentThread().getName());
        } else if (!CollectionUtils.isEmpty(dataList)) {
            data.setUserNames(prodPushUserConfig.getCfgValue());
            data.addListMail(dataList);
            data.title("生产运行情况");
            resultList.add(data);
        } else {
            log.info("生产数据推送通知数据处理 --- SxProdDataPushNotifyStrategyProdRunConditionEmailService --- DISPOSE --- 无数据,不发送消息 --- THREAD:{}", Thread.currentThread().getName());
        }

        log.info("生产数据推送通知数据处理 --- SxProdDataPushNotifyStrategyProdRunConditionEmailService --- END --- DATA:{} --- THREAD:{}", JSONObject.toJSONString(data), Thread.currentThread().getName());

        return resultList;
    }

    private List<String> addListMailByStringDataList(Map<String, String> sourceDataMap, List<String> dataList, String title){
        if (CollectionUtils.isEmpty(sourceDataMap)) return dataList;
        dataList.add(title);
        for (Map.Entry<String, String> dataEntry : sourceDataMap.entrySet()) {
            dataList.add(dataEntry.getKey() + "：" + dataEntry.getValue());
        }
        return dataList;
    }
}
