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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxOutPutStatisticsTravelService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOutPutStatistics;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOutPutStatisticsTravel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOutPutStatisticsTravelRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2022-10-25 9:58
 */
@Service
public class SxOutPutStatisticsTravelService implements ISxOutPutStatisticsTravelService {

    @Autowired
    private MesOutPutStatisticsTravelRepository outPutStatisticsTravelRepository;

    @Override
    public MesOutPutStatisticsTravel insertOutPutStatisticsTravel(MesOutPutStatistics outPutStatistics) {
        MesOutPutStatisticsTravel outPutStatisticsTravel = new MesOutPutStatisticsTravel();
        BeanUtils.copyProperties(outPutStatistics, outPutStatisticsTravel, MesPcnExtConstWords.ID);
        outPutStatisticsTravel.setPid(outPutStatistics.getId());
        MesOutPutStatisticsTravel maxSeqOutPutStatisticsTravel = getMaxSeqOutPutStatisticsTravel(outPutStatistics.getOrganizeCode(), outPutStatisticsTravel.getWorkOrderNo(), outPutStatisticsTravel.getShiftCode(), outPutStatisticsTravel.getWorkTime());
        outPutStatisticsTravel.setSeq(maxSeqOutPutStatisticsTravel == null ? 1 : maxSeqOutPutStatisticsTravel.getSeq() + 1);
        return outPutStatisticsTravelRepository.insert(outPutStatisticsTravel);
    }

    @Override
    public MesOutPutStatisticsTravel getMaxSeqOutPutStatisticsTravel(String organizeCode, String workOrderNo, String shiftCode, String workTime) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(workOrderNo, MesPcnExtConstWords.WORK_ORDER_NO, packBean);
        DdlPreparedPack.getStringEqualPack(shiftCode, MesPcnExtConstWords.SHIFT_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(workTime, MesPcnExtConstWords.WORK_TIME, packBean);
        packBean.setOrderByStr(" order by seq desc ");
        return outPutStatisticsTravelRepository.getByProperty(packBean);
    }

    @Override
    public void deleteOutPutStatisticsTravel(String organizeCode, String modifyDatetime) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getNumEqualPack(MesPcnExtEnumUtil.IF_SYNC_STATUS.ALREADY_SYNC.getValue(), MesPcnExtConstWords.SYSTEM_SYNC_STATUS, packBean);
        DdlPreparedPack.getStringSmallerPack(modifyDatetime, MesPcnExtConstWords.MODIFY_DATE_TIME, packBean);
        List<MesOutPutStatisticsTravel> outPutStatisticsTravels = outPutStatisticsTravelRepository.findByHqlWhere(packBean);
        if(!CollectionUtils.isEmpty(outPutStatisticsTravels)) outPutStatisticsTravelRepository.deleteAll(outPutStatisticsTravels);
    }
}
