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.MesAssistOutAndEnter;
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.LinkedHashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private IBusiConfigService busiConfigService;

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

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

        //SxPushDataNotifyParamAssistModel param = (SxPushDataNotifyParamAssistModel) pushParam;

        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<>();

        // 产线别生产达成率
        //if (!CollectionUtils.isEmpty(workCenterNotReachReasonsList)) {
        //    LinkedHashMap<String, String> dataMap = new LinkedHashMap<>();
        //    dataMap.put("产线别生产达成率", "TITLE");
        //    for (MesWorkCenterNotReachReason workCenterNotReachReason : workCenterNotReachReasonsList) {
        //        dataMap.put(workCenterNotReachReason.getWorkCenterCode(), workCenterNotReachReason.getCompleteQty().intValue() + "/" + workCenterNotReachReason.getPlanQty().intValue() + "&nbsp;&nbsp;" + workCenterNotReachReason.getYieldRateStr() + "&nbsp;&nbsp;" + workCenterNotReachReason.getCompleteRateStr());
        //        data.addListDing(dataMap);
        //        dataMap = new LinkedHashMap<>();
        //    }
        //}

        addListDingByStringDataMap(categoryReachRate, data, "产品别生产达成率/完结率");
        addListDingByStringDataMap(workCenterTotalRateMap, data, "产线综合能率");
        addListDingByStringDataMap(totalUnWorkTimeMapByWorkCenterCode, data, "产线非作业时间");
        addListDingByStringDataMap(assistMapByWorkCenterCode, data, "产线援出");
        data.title("生产运行情况");

        MesBusiConfig prodPushUserConfig = busiConfigService.getBusiConfigByCfgCode(pushParam.getOrganizeCode(), "PROD_PUSH_USER_ACCOUNT");
        if (prodPushUserConfig == null || StringUtils.isEmpty(prodPushUserConfig.getCfgValue())) {
            log.info("生产数据推送通知数据处理 --- SxProdDataPushNotifyStrategyProdRunConditionDingService --- DISPOSE --- 未维护通知人 --- THREAD:{}", Thread.currentThread().getName());
        } else if (!CollectionUtils.isEmpty(data.getListDing())) {
            LinkedHashMap<String, String> dataMap = new LinkedHashMap<>();
            dataMap.put("详细内容可在SFP管理端MES模块-报表中心中查看", "TITLE");
            data.addListDing(dataMap);
            data.setUserNames(prodPushUserConfig.getCfgValue());
            resultList.add(data);
        } else {
            log.info("生产数据推送通知数据处理 --- SxProdDataPushNotifyStrategyProdRunConditionDingService --- DISPOSE --- 无数据,不发送消息 --- THREAD:{}", Thread.currentThread().getName());
        }

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

        return resultList;
    }

    private Double statisticsInfluenceTime(List<MesAssistOutAndEnter> dataDbList) {
        return CollectionUtils.isEmpty(dataDbList) ? new Double(0) :
                dataDbList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getInfluenceTime()))).mapToDouble(MesAssistOutAndEnter::getInfluenceTime).sum();
    }

    private Double statisticsAmount(List<MesAssistOutAndEnter> dataDbList) {
        return CollectionUtils.isEmpty(dataDbList) ? new Double(0) :
                dataDbList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getAmount()))).mapToDouble(MesAssistOutAndEnter::getAmount).sum();
    }

    private PushDataNotifyModel addListDingByStringDataMap(Map<String, String> sourceDataMap, PushDataNotifyModel data, String title){
        if (CollectionUtils.isEmpty(sourceDataMap)) return data;
        LinkedHashMap<String, String> dataMap = new LinkedHashMap<>();

        dataMap.put(title, "TITLE");

        for (Map.Entry<String, String> dataEntry : sourceDataMap.entrySet()) {
            dataMap.put(dataEntry.getKey(), dataEntry.getValue());
        }
        data.addListDing(dataMap);

        return data;
    }
}
