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

import cn.estsh.i3plus.ext.mes.api.base.IMesOrderJobService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesOrderJob;
import cn.estsh.i3plus.ext.mes.pojo.model.SxOrderJobModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesOrderJobRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
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.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.sqlpack.MesHqlPack;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/01/18 16:00
 * @Description:
 **/
@Service
public class MesOrderJobService implements IMesOrderJobService {
    @Autowired
    private MesOrderJobRepository orderJobRepository;

    @Override
    public ListPager<SxOrderJobModel> queryMesOrderJobByPager(MesOrderJob orderJob, Pager pager) {
        List<SxOrderJobModel> resultList = new ArrayList<>();
        DdlPackBean packBean = MesHqlPack.getAllBaseData(AuthUtil.getOrganize().getOrganizeCode());
        DdlPreparedPack.getNumEqualPack(orderJob.getOrderType(), MesExtConstWords.ORDER_TYPE, packBean);
        DdlPreparedPack.getNumEqualPack(orderJob.getOrderStatus(), MesExtConstWords.ORDER_STATUS, packBean);
        DdlPreparedPack.getNumEqualPack(orderJob.getTaskType(), MesExtConstWords.TASK_TYPE, packBean);
        DdlPreparedPack.getNumEqualPack(orderJob.getIsValid(), MesExtConstWords.IS_VALID, packBean);
        DdlPreparedPack.getStringEqualPack(orderJob.getWorkOrderNo(), MesExtConstWords.WORK_ORDER_NO, packBean);
        DdlPreparedPack.getStringEqualPack(orderJob.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(orderJob.getOrderNo(), MesExtConstWords.ORDER_NO, packBean);
        DdlPreparedPack.timeBuilder(orderJob.getGenerateTimeStart(), orderJob.getGenerateTimeEnd(), MesExtConstWords.GENERATE_TIME, packBean, true);
        pager = PagerHelper.getPager(pager, orderJobRepository.findByHqlWhereCount(packBean));
        List<MesOrderJob> orderJobList = orderJobRepository.findByHqlWherePage(packBean, pager);
        if (!CollectionUtils.isEmpty(orderJobList)) {
            for (MesOrderJob item : orderJobList) {
                if (null == item) {
                    continue;
                }
                SxOrderJobModel result = new SxOrderJobModel();
                BeanUtils.copyProperties(item, result);
                result.setTaskTypeName(
                        (StringUtils.isEmpty(item.getTaskType()) || StringUtils.isEmpty(MesExtEnumUtil.MES_OR_TASK_TYPE.valueOfDescription(item.getTaskType())))
                                ? null : MesExtEnumUtil.MES_OR_TASK_TYPE.valueOfDescription(item.getTaskType()));
                result.setOrderTypeName(
                        (StringUtils.isEmpty(item.getOrderType()) || StringUtils.isEmpty(MesExtEnumUtil.MES_OR_ORDER_TYPE.valueOfDescription(item.getOrderType())))
                                ? null : MesExtEnumUtil.MES_OR_ORDER_TYPE.valueOfDescription(item.getOrderType()));
                result.setOrderStatusName(
                        (StringUtils.isEmpty(item.getOrderStatus()) || StringUtils.isEmpty(MesExtEnumUtil.MES_ORJ_ORDER_STATUS.valueOfDescription(item.getOrderStatus())))
                                ? null : MesExtEnumUtil.MES_ORJ_ORDER_STATUS.valueOfDescription(item.getOrderStatus()));
                result.setHandlerMethodName(
                        (StringUtils.isEmpty(item.getHandlerMethod()) || StringUtils.isEmpty(MesExtEnumUtil.HANDLER_METHOD.valueOfDescription(item.getHandlerMethod())))
                                ? null : MesExtEnumUtil.HANDLER_METHOD.valueOfDescription(item.getHandlerMethod()));
                resultList.add(result);
            }
        }
        return new ListPager(resultList, pager);
    }
}
