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.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrder;
import cn.estsh.i3plus.pojo.mes.repository.MesWorkOrderRepository;
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.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

    private IJxCenterWorkingBoardService proxyService;

    private MesWorkOrderRepository workOrderRepository;

    public JxCenterWorkingBoardProxyOrderService(IJxCenterWorkingBoardService proxyService) {
        this.proxyService = proxyService;
        this.workOrderRepository = (MesWorkOrderRepository) SpringContextsUtil.getBean("mesWorkOrderRepository");
    }

    @Override
    public Map<String, Object> queryCenterWorkingBoardData(Map<String, Object> dataMap) {

        if (null != proxyService) dataMap = proxyService.queryCenterWorkingBoardData(dataMap);

        String orgainzeCode = (String) dataMap.get(MesExtConstWords.ORGANIZE_CODE);
        String workCenterCode = (String) dataMap.get(MesExtConstWords.WORK_CENTER_CODE);
        String startTime = (String) dataMap.get(MesExtConstWords.START_TIME);
        String endTime = (String) dataMap.get(MesExtConstWords.END_TIME);

        if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) return dataMap;

        DdlPackBean packBean = DdlPackBean.getDdlPackBean(orgainzeCode);
        DdlPreparedPack.getStringEqualPack(workCenterCode, MesExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.WORK_ORDER_TYPE.STANDARD_ORDER.getValue(), MesExtConstWords.WORK_ORDER_TYPE, packBean);
        DdlPreparedPack.timeBuilder(startTime + MesExtConstWords.APPEND_ZERO_HMS, endTime + MesExtConstWords.APPEND_24_HMS, MesExtConstWords.START_TIME, true, true, packBean);
        List<MesWorkOrder> workOrderList = workOrderRepository.findByHqlWhere(packBean);
        workOrderList = CollectionUtils.isEmpty(workOrderList) ? null : workOrderList.stream().filter(o -> (null != o && MesExtEnumUtil.WORK_ORDER_STATUS.CREATE.getValue() != o.getWorkOrderStatus() && MesExtEnumUtil.WORK_ORDER_STATUS.REMOVE.getValue() != o.getWorkOrderStatus())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(workOrderList)) return dataMap;

        dataMap.put(MesWorkOrder.class.getSimpleName(), workOrderList);

        return dataMap;

    }
}
