package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.base;

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesShiftService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.util.DateUtil;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesProdShiftRecord;
import cn.estsh.i3plus.pojo.mes.bean.MesShift;
import cn.estsh.i3plus.pojo.mes.repository.MesShiftRepository;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;


/**
 * @Description: 产线与班次的对应关系
 * @Author: gsz
 * @Date: 2024/5/21 14:16
 * @Modify:
 */
@Service
@Slf4j
public class MesShiftServiceImpl implements IMesShiftService {

    @Autowired
    private MesShiftRepository mesShiftRepository;

    @Override
    public ListPager<MesShift> queryMesShiftByPager(MesShift bean, Pager pager) {

        DdlPackBean packBean = DdlPackBean.getDdlPackBean(bean.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(bean.getWorkCenterCode(), "workCenterCode", packBean);
        DdlPreparedPack.getStringEqualPack(bean.getShiftCode(), "shiftCode", packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, packBean);
        pager = PagerHelper.getPager(pager, mesShiftRepository.findByHqlWhereCount(packBean));

        List<MesShift> resultList = mesShiftRepository.findByHqlWherePage(packBean, pager);

        return new ListPager<>(resultList, pager);
    }

    @Override
    public List<MesShift> queryMesShift(String organizeCode, String workCenterCode) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(workCenterCode, "workCenterCode", packBean);
        return mesShiftRepository.findByHqlWhere(packBean);
    }

    @Override
    public Map<String, String> getShiftTimeMap(MesProdShiftRecord record) {
        List<MesShift> shiftList = queryMesShift(record.getOrganizeCode(), record.getWorkCenterCode());
        if (CollectionUtils.isEmpty(shiftList)) {
            return null;
        }
        Optional<MesShift> shiftOptional = shiftList.stream().filter(t -> t.getShiftCode().equals(record.getShiftCode())).findFirst();
        if (!shiftOptional.isPresent()) {
            return null;
        }
        if (StringUtils.isEmpty(shiftOptional.get().getStartTime()) || StringUtils.isEmpty(shiftOptional.get().getEndTime()) || StringUtils.isEmpty(record.getStartTime())) {
            return null;
        }
        String startData = TimeTool.parseStringFormat(record.getStartTime(), DateUtil.BASE_FORMAT, DateUtil.SHORT_FORMAT);
        //白班
        String startTime = startData + " " + shiftOptional.get().getStartTime();
        String endTime = startData + " " + shiftOptional.get().getEndTime();
        //晚班
        if (MesExtEnumUtil.SHIFT_CODE.SHIFT_CODE_20.getValue().equals(record.getShiftCode())) {
            endTime = TimeTool.getToday(DateUtil.addDays(record.getStartTime(), 1)) + " " + shiftOptional.get().getEndTime();
        }
        Map<String, String> shiftTimeMap = new HashMap<>(2);
        shiftTimeMap.put(MesPcnExtConstWords.START_TIME, startTime);
        shiftTimeMap.put(MesPcnExtConstWords.END_TIME, endTime);
        return shiftTimeMap;
    }

}
