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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IOutPutStatisticsService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOutPutStatistics;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOutPutStatisticsRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class OutPutStatisticsService implements IOutPutStatisticsService {

    @Autowired
    private MesOutPutStatisticsRepository outPutStatisticsRepository;

    @Override
    public MesOutPutStatistics getOutPutStatistics(MesOutPutStatistics outPutStatistics) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(outPutStatistics.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(outPutStatistics.getWorkCenterCode(), MesPcnExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(outPutStatistics.getShiftCode(), MesPcnExtConstWords.SHIFT_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(outPutStatistics.getWorkTime(), MesPcnExtConstWords.WORK_TIME, packBean);
        DdlPreparedPack.getStringEqualPack(outPutStatistics.getWorkOrderNo(), MesPcnExtConstWords.WORK_ORDER_NO, packBean);
        return outPutStatisticsRepository.getByProperty(packBean);
    }

    @Override
    public MesOutPutStatistics getOutPutStatisticsByTimeAndOrderNo(String organizeCode, String workOrderNo, String workTime, String shiftCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo) || StringUtils.isEmpty(workTime) || StringUtils.isEmpty(shiftCode)) {
            return null;
        }
        return outPutStatisticsRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
                        MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.WORK_TIME, MesPcnExtConstWords.SHIFT_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        workOrderNo, workTime, shiftCode});
    }
}
