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.repository.MesOutPutStatisticsRepository;
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.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
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;

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

    private IJxCenterWorkingBoardService proxyService;

    private MesOutPutStatisticsRepository outPutStatisticsRepository;

    public JxCenterWorkingBoardProxyOutPutService(IJxCenterWorkingBoardService proxyService) {
        this.proxyService = proxyService;
        this.outPutStatisticsRepository = (MesOutPutStatisticsRepository) SpringContextsUtil.getBean("mesOutPutStatisticsRepository");
    }

    @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 workTime = (String) dataMap.get(MesExtConstWords.WORK_TIME);
        String startTime = (String) dataMap.get(MesExtConstWords.START_TIME);
        String endTime = (String) dataMap.get(MesExtConstWords.END_TIME);

        List<MesOutPutStatistics> outPutStatisticsList = null;
        if (!StringUtils.isEmpty(workCenterCode) && !StringUtils.isEmpty(workTime))
            outPutStatisticsList = outPutStatisticsRepository.findByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID, MesExtConstWords.WORK_CENTER_CODE, MesExtConstWords.WORK_TIME, MesExtConstWords.WORK_ORDER_TYPE},
                new Object[]{orgainzeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), workCenterCode, workTime, MesExtEnumUtil.WORK_ORDER_TYPE.STANDARD_ORDER.getValue()},
                " order by modifyDatetime desc ");
        else if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
            DdlPackBean packBean = DdlPackBean.getDdlPackBean(orgainzeCode);
            DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.WORK_ORDER_TYPE.STANDARD_ORDER.getValue(), MesExtConstWords.WORK_ORDER_TYPE, packBean);
            DdlPreparedPack.timeBuilder(startTime, endTime, MesExtConstWords.WORK_TIME, true, true, packBean);
            outPutStatisticsList = outPutStatisticsRepository.findByHqlWhere(packBean);
        }

        if (CollectionUtils.isEmpty(outPutStatisticsList)) return dataMap;

        dataMap.put(MesOutPutStatistics.class.getSimpleName(), outPutStatisticsList);

        return dataMap;

    }

}
