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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IRouteProcessExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.sqlpack.MesPcnExtHqlPack;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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.mes.bean.MesProcess;
import cn.estsh.i3plus.pojo.mes.bean.MesRouteProcess;
import cn.estsh.i3plus.pojo.mes.repository.MesProcessRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesRouteProcessRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

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

    @Autowired
    private MesProcessRepository processRepository;

    @Autowired
    private MesRouteProcessRepository routeProcessRepository;

    @Override
    public ListPager<MesProcess> queryProcessByPager(MesProcess process, Pager pager) {
        if (process == null) {
            pager = PagerHelper.getPager(pager, processRepository.listCount());
            return new ListPager(processRepository.listPager(pager), pager);
        } else {
            DdlPackBean hqlPack = MesPcnExtHqlPack.getMesProcess(process, process.getOrganizeCode());
            pager = PagerHelper.getPager(pager, processRepository.findByHqlWhereCount(hqlPack));
            hqlPack.setOrderByStr(process.orderBy());
            return new ListPager(processRepository.findByHqlWherePage(hqlPack, pager), pager);
        }
    }

    @Override
    public MesProcess getProcessByCode(String organizeCode, String processCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(processCode)) {
            return null;
        }
        return processRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PROCESS_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), processCode});
    }

    @Override
    public MesRouteProcess getRouteProcessByCode(String organizeCode, String routeCode, String processCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(routeCode) || StringUtils.isEmpty(processCode)) {
            return null;
        }
        return routeProcessRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.ROUTE_CODE, MesPcnExtConstWords.PROCESS_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), routeCode, processCode});
    }

    @Override
    public List<MesRouteProcess> getRouteProcessByRoute(String organizeCode, String routeCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(routeCode)) {
            return null;
        }
        return routeProcessRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.ROUTE_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), routeCode});
    }

}
