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.MesRepairTask;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesRepairTaskRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
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 JxCenterWorkingBoardProxyRepairService implements IJxCenterWorkingBoardService {

    private IJxCenterWorkingBoardService proxyService;

    private MesRepairTaskRepository repairTaskRepository;

    public JxCenterWorkingBoardProxyRepairService(IJxCenterWorkingBoardService proxyService) {
        this.proxyService = proxyService;
        this.repairTaskRepository = (MesRepairTaskRepository) SpringContextsUtil.getBean("mesRepairTaskRepository");
    }

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

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

        String organizeCode = (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<MesRepairTask> repairTaskDbList = null;
        if (!StringUtils.isEmpty(workCenterCode) && !StringUtils.isEmpty(workTime))
            repairTaskDbList = repairTaskRepository.findByProperty(
                    new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID, MesExtConstWords.WORK_CENTER_CODE, MesExtConstWords.WORK_TIME},
                    new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), workCenterCode, workTime});
        else if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
            DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
            DdlPreparedPack.timeBuilder(startTime, endTime, MesExtConstWords.WORK_TIME, true, true, packBean);
            repairTaskDbList = repairTaskRepository.findByHqlWhere(packBean);
        }

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

        dataMap.put(MesRepairTask.class.getSimpleName(), repairTaskDbList);

        return dataMap;
    }

}
