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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.jx.IJxWorkOrderService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkOrderExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WorkOrderPageVo;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WorkOrderQueryDto;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesWorkOrderExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.sqlpack.MesPcnExtHqlPack;
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.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.MesEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-jx
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2023-11-06 15:27
 */
@Service
public class JxWorkOrderService implements IJxWorkOrderService {

    @Autowired
    private MesWorkOrderExtRepository workOrderExtRepository;

    @Override
    public ListPager queryWorkOrderByPager(WorkOrderQueryDto workOrderQueryDto, Pager pager) {
        if (workOrderQueryDto == null) {
            pager = PagerHelper.getPager(pager, workOrderExtRepository.listCount());
            return new ListPager(workOrderExtRepository.listPager(pager), pager);
        } else {
            DdlPackBean hqlPack = MesPcnExtHqlPack.getWindowWorkOrderExt(workOrderQueryDto);
            if (StringUtils.isEmpty(workOrderQueryDto.getOrderByParam())) {
                DdlPreparedPack.getOrderByPack(new Object[]{MesEnumUtil.ASC_OR_DESC.ASC.getValue()}, new String[]{MesPcnExtConstWords.SEQ}, hqlPack);
            } else {
                if (workOrderQueryDto.getOrderByParam().equals(MesPcnExtConstWords.SEQ) ||
                        workOrderQueryDto.getOrderByParam().equals(MesPcnExtConstWords.START_TIME)  ||
                        workOrderQueryDto.getOrderByParam().equals(MesPcnExtConstWords.ORDER_NO) ) {
                    DdlPreparedPack.getOrderByPack(new Object[]{workOrderQueryDto.getAscOrDesc()}, new String[]{workOrderQueryDto.getOrderByParam()}, hqlPack);
                } else {
                    DdlPreparedPack.getOrderByPack(new Object[]{workOrderQueryDto.getAscOrDesc(), MesEnumUtil.ASC_OR_DESC.ASC.getValue()},
                            new String[]{workOrderQueryDto.getOrderByParam(), MesPcnExtConstWords.SEQ}, hqlPack);
                }
            }
            pager = PagerHelper.getPager(pager, workOrderExtRepository.findByHqlWhereCount(hqlPack));

            List<MesWorkOrderExt> workOrderExts = workOrderExtRepository.findByHqlWherePage(hqlPack, pager);

            List<WorkOrderPageVo> workOrderPageVoList = new ArrayList<>();

            for (MesWorkOrderExt workOrderExt : workOrderExts) {
                WorkOrderPageVo workOrderPageVo = new WorkOrderPageVo();

                BeanUtils.copyProperties(workOrderExt, workOrderPageVo);

                workOrderPageVo.setWorkOrderStatusName(MesPcnExtEnumUtil.WORK_ORDER_STATUS.valueOfDescription(workOrderPageVo.getWorkOrderStatus()));

                workOrderPageVo.setWorkOrderTypeName(MesPcnExtEnumUtil.WORK_ORDER_TYPE.valueOfDescription(workOrderPageVo.getWorkOrderType()));

                workOrderPageVoList.add(workOrderPageVo);
            }

            return new ListPager(workOrderPageVoList, pager);
        }
    }
}
