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.MesShiftProdCenterRecord;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesShiftProdCenterRecordRepository;
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 JxCenterWorkingBoardProxySpcrService implements IJxCenterWorkingBoardService {

    private MesShiftProdCenterRecordRepository shiftProdCenterRecordRepository;

    public JxCenterWorkingBoardProxySpcrService() {
        this.shiftProdCenterRecordRepository = (MesShiftProdCenterRecordRepository) SpringContextsUtil.getBean("mesShiftProdCenterRecordRepository");
    }

    @Override
    public Map<String, Object> queryCenterWorkingBoardData(Map<String, Object> 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 shiftCode = (String) dataMap.get(MesExtConstWords.SHIFT_CODE);
        String startTime = (String) dataMap.get(MesExtConstWords.START_TIME);
        String endTime = (String) dataMap.get(MesExtConstWords.END_TIME);

        List<MesShiftProdCenterRecord> shiftProdCenterRecordList = null;
        if (!StringUtils.isEmpty(workCenterCode) && !StringUtils.isEmpty(workTime) && !StringUtils.isEmpty(shiftCode))
            shiftProdCenterRecordList = shiftProdCenterRecordRepository.findByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID, MesExtConstWords.WORK_CENTER_CODE, MesExtConstWords.WORK_TIME, MesExtConstWords.SHIFT_CODE},
                new Object[]{orgainzeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), workCenterCode, workTime, shiftCode});
        else if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
            DdlPackBean packBean = DdlPackBean.getDdlPackBean(orgainzeCode);
            DdlPreparedPack.timeBuilder(startTime, endTime, MesExtConstWords.WORK_TIME, true, true, packBean);
            shiftProdCenterRecordList = shiftProdCenterRecordRepository.findByHqlWhere(packBean);
        }

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

        dataMap.put(MesShiftProdCenterRecord.class.getSimpleName(), shiftProdCenterRecordList);

        return dataMap;
    }

}
