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

import cn.estsh.i3plus.ext.mes.api.busi.jx.IJxCenterWorkingBoardService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesOutPutStatistics;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkCenterExt;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.tool.MathOperation;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrder;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author : wangjie
 * @CreateDate : 2022-02-15
 * @Modify:
 **/
@Slf4j
public class JxCenterWorkingBoardCommonService implements IJxCenterWorkingBoardService {

    private IJxCenterWorkingBoardService proxyService;

    public JxCenterWorkingBoardCommonService() {
        this.proxyService = (IJxCenterWorkingBoardService) SpringContextsUtil.getBean("jxCenterWorkingBoardDispatchService");
    }

    public List<MesWorkOrder> getWorkOrderList2Sort(List<MesWorkOrder> workOrderList, List<MesOutPutStatistics> outPutStatisticsList, MesWorkCenterExt workCenterExtDb) {
        List<String> hasOutPutOrder = CollectionUtils.isEmpty(outPutStatisticsList) ? null : outPutStatisticsList.stream().filter(o -> null != o).map(MesOutPutStatistics::getWorkOrderNo).collect(Collectors.toList());
        return CollectionUtils.isEmpty(workOrderList) ? null :
                workOrderList.stream().filter(o -> (null != o && (
                        o.getStartTime().contains(workCenterExtDb.getWorkTime()) || (MathOperation.compareTo(o.getQty(), o.getCompleteQty()) != 0 || (!CollectionUtils.isEmpty(hasOutPutOrder) && hasOutPutOrder.contains(o.getOrderNo())))
                ))).sorted(Comparator.comparing(MesWorkOrder::getSeq)).collect(Collectors.toList());
    }

    public List<MesWorkOrder> getWorkOrderListByWorkTime(List<MesWorkOrder> workOrderList, String workTime) {
        return CollectionUtils.isEmpty(workOrderList) ? null : workOrderList.stream().filter(o -> (null != o && o.getStartTime().contains(workTime))).collect(Collectors.toList());
    }

    public List<MesOutPutStatistics> getOutPutStatisticsListByWorkTime(List<MesOutPutStatistics> outPutStatisticsList, String workTime) {
        return CollectionUtils.isEmpty(outPutStatisticsList) ? null : outPutStatisticsList.stream().filter(o -> (null != o && o.getWorkTime().equals(workTime))).collect(Collectors.toList());
    }

    public Double getWorkOrderList2AmountQty(List<MesWorkOrder> workOrderList) {
        return CollectionUtils.isEmpty(workOrderList) ? new Double(0) : workOrderList.stream().filter(o -> null != o).mapToDouble(MesWorkOrder::getQty).sum();
    }

    public Double getOutPutStatisticsList2AmountQty(List<MesOutPutStatistics> outPutStatisticsList) {
        return CollectionUtils.isEmpty(outPutStatisticsList) ? new Double(0) : outPutStatisticsList.stream().filter(o -> null != o).mapToDouble(MesOutPutStatistics::getQty).sum();
    }

    public Map<String, List<MesOutPutStatistics>> getOutPutStatisticsMapByOrderNo(List<MesOutPutStatistics> outPutStatisticsList) {
        return CollectionUtils.isEmpty(outPutStatisticsList) ? null : outPutStatisticsList.stream().filter(o -> null != o).collect(Collectors.groupingBy(MesOutPutStatistics::getWorkOrderNo));
    }

    public List<MesOutPutStatistics> sortOutPutStatisticsList(List<MesOutPutStatistics> outPutStatisticsList) {
        return CollectionUtils.isEmpty(outPutStatisticsList) ? null : outPutStatisticsList.stream().filter(o -> null != o).sorted(Comparator.comparing(MesOutPutStatistics::getModifyDatetime).reversed()).collect(Collectors.toList());
    }

    public String getQtyStr(Double qty) { return qty.intValue() + "台"; }

    public String getListSizeStr(List list) { return CollectionUtils.isEmpty(list) ? MesExtConstWords.ZERO_STR : String.valueOf(list.size()); }

    public Double getListSizeD(List list) { return new Double(CollectionUtils.isEmpty(list) ? MesExtConstWords.ZERO : list.size()); }

    public String getBigDecimalPercent(BigDecimal b) {
        return b + MesExtConstWords.PERCENT;
    }

    public BigDecimal divThenMul100Format1RoundHalfUp(Double d1, Double d2) {
        Double d = (MathOperation.compareTo(d1, new Double(0)) == 0 || MathOperation.compareTo(d2, new Double(0)) == 0)
                ? null : MathOperation.mul(MathOperation.div(d1, d2), new Double(100));
        return format1RounHalfUp(d);
    }

    public BigDecimal format1RounHalfUp(Double d) {
        if (StringUtils.isEmpty(d)) d = new Double(0);
        BigDecimal bd = new BigDecimal(d);
        bd = bd.setScale(1, BigDecimal.ROUND_HALF_UP);
        return bd;
    }

    @Override
    public Object execCachedCenterWorkingBoardData(String organizeCode, String flag, String item, Object data) {
        return proxyService.execCachedCenterWorkingBoardData(organizeCode, flag, item, data);
    }
}