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

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.model.bu3.SxPushDataNotifyParamAssistModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxPushDataNotifyParamModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.pojo.model.platform.PushDataNotifyModel;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
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;

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

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

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

        SxPushDataNotifyParamAssistModel param = (SxPushDataNotifyParamAssistModel) pushParam;

        List<MesAssistOutAndEnter> dataDbList = (List<MesAssistOutAndEnter>) obj[0];

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

        LinkedHashMap<String, String> dataMap = new LinkedHashMap<>();
        dataMap.put("月份", Integer.valueOf(param.getInfluenceDate().substring(5, 7)) + "月");
        dataMap.put("工时", statisticsInfluenceTime(dataDbList) + "分");
        dataMap.put("责任部门", param.getOwnerDepartmentName());
        dataMap.put("合计", statisticsAmount(dataDbList) + "元");
        dataMap.put("单次", CollectionUtils.isEmpty(dataDbList) ? MesExtConstWords.ZERO_STR : String.valueOf(dataDbList.size()));
        data.addListDing(dataMap).title("援进援出信息统计");

        resultList.add(data);

        log.info("生产数据推送通知数据处理 --- SxProdDataPushNotifyStrategyAssistOneService --- 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();
    }
}
