package cn.estsh.i3plus.ext.mes.apiservice.daoimpl;


import cn.estsh.i3plus.ext.mes.apiservice.dao.IOnSiteOrderDetailDao;
import cn.estsh.i3plus.ext.mes.apiservice.dao.IQManageDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesOnSiteOrderDetail;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesQManageTaskDetail;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Wynne.Lu
 * @CreateDate: 2019/11/21 11:29 上午
 * @Description:
 **/
@Service
public class OnSiteOrderDetailDao implements IOnSiteOrderDetailDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<MesOnSiteOrderDetail> queryOnSiteOrderDetailReportByPager(MesOnSiteOrderDetail mesOnSiteOrderDetail, Pager pager) {
        String hql = "select  new " + MesOnSiteOrderDetail.class.getName() +
                "(odetail.id,osorder.workCenterCode,osorder.workOrderNo,osorder.partNo," +
                "odetail.projectSortNo,odetail.projectCode,odetail.projectName,odetail.orderNo,osorder.createDatetime,odetail.memo,odetail.checkResult," +
                "odetail.spotCheckUser,odetail.spotCheckTime,odetail.maintainStrategy,odetail.operateUser,odetail.operateTime," +
                "osorder.orderStatus,osorder.modifyUser,osorder.modifyDatetime)" +
                " from MesOnSiteOrderDetail odetail" +
                " LEFT JOIN MesOnSiteOrder osorder on odetail.orderNo = osorder.orderNo" +
                " and odetail.organizeCode = osorder.organizeCode and odetail.isValid = osorder.isValid " +
                " and odetail.isDeleted = osorder.isDeleted " +
                " where odetail.organizeCode = :organizeCode " +
                " and odetail.isValid = :isValid and odetail.isDeleted = :isDeleted and osorder.orderStatus in ( :orderStatusList )";

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getPartNo())){
            hql += " and osorder.partNo = :partNo";
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getWorkOrderNo())){
            hql += " and osorder.workOrderNo like :workOrderNo";
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getWorkCenterCode())){
            hql += " and osorder.workCenterCode like :workCenterCode";
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getOrderNo())){
            hql += " and odetail.orderNo like :orderNo ";
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getCheckResult())){
            hql += " and odetail.checkResult = :checkResult ";
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getMaintainStrategy())){
            hql += " and odetail.maintainStrategy like :maintainStrategy ";
        }

        if (!StringUtils.isEmpty(mesOnSiteOrderDetail.getProjectSortNo())) {
            hql += " and odetail.projectSortNo = :projectSortNo ";
        }

        if (!StringUtils.isEmpty(mesOnSiteOrderDetail.getProjectCode())) {
            hql += " and odetail.projectCode = :projectCode ";
        }

        if (!StringUtils.isEmpty(mesOnSiteOrderDetail.getSpotCheckTimeStart())) {
            hql += " and odetail.spotCheckTime >= :spotCheckTimeStart ";
        }

        if (!StringUtils.isEmpty(mesOnSiteOrderDetail.getSpotCheckTimeEnd())) {
            hql += " and odetail.spotCheckTime <= :spotCheckTimeEnd ";
        }
        hql += " order by odetail.spotCheckTime desc ";

        List<Integer> orderStatusList = new ArrayList<>();
        orderStatusList.add(MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECK_COMPLETE.getValue());
        orderStatusList.add(MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CLOSE.getValue());
        Query queryObject = entityManager.createQuery(hql);
        queryObject.setParameter("organizeCode", mesOnSiteOrderDetail.getOrganizeCode());
        queryObject.setParameter("isDeleted", CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        queryObject.setParameter("isValid", CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        queryObject.setParameter("orderStatusList", orderStatusList);

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getPartNo())){
            queryObject.setParameter(MesExtConstWords.PART_NO, mesOnSiteOrderDetail.getPartNo());
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getWorkOrderNo())){
            queryObject.setParameter("workOrderNo", "%"+ mesOnSiteOrderDetail.getWorkOrderNo() +"%");
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getWorkCenterCode())){
            queryObject.setParameter("workCenterCode", "%"+ mesOnSiteOrderDetail.getWorkCenterCode() +"%");
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getOrderNo())){
            queryObject.setParameter(MesExtConstWords.ORDER_NO, "%"+ mesOnSiteOrderDetail.getOrderNo() +"%");
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getCheckResult())){
            queryObject.setParameter("checkResult", mesOnSiteOrderDetail.getCheckResult());
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getMaintainStrategy())){
            queryObject.setParameter("maintainStrategy", "%"+ mesOnSiteOrderDetail.getMaintainStrategy() +"%");
        }

        if (!StringUtils.isEmpty(mesOnSiteOrderDetail.getProjectSortNo())) {
            queryObject.setParameter("projectSortNo", mesOnSiteOrderDetail.getProjectSortNo());
        }

        if (!StringUtils.isEmpty(mesOnSiteOrderDetail.getProjectCode())) {
            queryObject.setParameter("projectCode", mesOnSiteOrderDetail.getProjectCode());
        }

        if (!StringUtils.isEmpty(mesOnSiteOrderDetail.getSpotCheckTimeStart())) {
            queryObject.setParameter("spotCheckTimeStart", mesOnSiteOrderDetail.getSpotCheckTimeStart());
        }

        if (!StringUtils.isEmpty(mesOnSiteOrderDetail.getSpotCheckTimeEnd())) {
            queryObject.setParameter("spotCheckTimeEnd", mesOnSiteOrderDetail.getSpotCheckTimeEnd());
        }


        queryObject.setMaxResults(pager.getPageSize());
        queryObject.setFirstResult(pager.getCurrentPage() * pager.getPageSize() - pager.getPageSize());
        return queryObject.getResultList();
    }

    @Override
    public int getMesOnSiteOrderDetailReportCount(MesOnSiteOrderDetail mesOnSiteOrderDetail) {
        String hql = "select count(distinct odetail.id)" +
                " from MesOnSiteOrderDetail odetail" +
                " LEFT JOIN MesOnSiteOrder osorder on odetail.orderNo = osorder.orderNo" +
                " and odetail.organizeCode = osorder.organizeCode and odetail.isValid = osorder.isValid " +
                " and odetail.isDeleted = osorder.isDeleted " +
                " where odetail.organizeCode = :organizeCode " +
                " and odetail.isValid = :isValid and odetail.isDeleted = :isDeleted and osorder.orderStatus in ( :orderStatusList )";

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getPartNo())){
            hql += " and osorder.partNo = :partNo";
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getWorkOrderNo())){
            hql += " and osorder.workOrderNo like :workOrderNo";
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getWorkCenterCode())){
            hql += " and osorder.workCenterCode like :workCenterCode";
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getOrderNo())){
            hql += " and odetail.orderNo like :orderNo ";
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getCheckResult())){
            hql += " and odetail.checkResult = :checkResult ";
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getMaintainStrategy())){
            hql += " and odetail.maintainStrategy like :maintainStrategy ";
        }

        if (!StringUtils.isEmpty(mesOnSiteOrderDetail.getProjectSortNo())) {
            hql += " and odetail.projectSortNo = :projectSortNo ";
        }

        if (!StringUtils.isEmpty(mesOnSiteOrderDetail.getProjectCode())) {
            hql += " and odetail.projectCode = :projectCode ";
        }

        if (!StringUtils.isEmpty(mesOnSiteOrderDetail.getSpotCheckTimeStart())) {
            hql += " and odetail.spotCheckTime >= :spotCheckTimeStart ";
        }

        if (!StringUtils.isEmpty(mesOnSiteOrderDetail.getSpotCheckTimeEnd())) {
            hql += " and odetail.spotCheckTime <= :spotCheckTimeEnd ";
        }

        List<Integer> orderStatusList = new ArrayList<>();
        orderStatusList.add(MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECK_COMPLETE.getValue());
        orderStatusList.add(MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CLOSE.getValue());
        Query queryObject = entityManager.createQuery(hql);
        queryObject.setParameter("organizeCode", mesOnSiteOrderDetail.getOrganizeCode());
        queryObject.setParameter("isDeleted", CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        queryObject.setParameter("isValid", CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        queryObject.setParameter("orderStatusList", orderStatusList);

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getPartNo())){
            queryObject.setParameter(MesExtConstWords.PART_NO, mesOnSiteOrderDetail.getPartNo());
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getWorkOrderNo())){
            queryObject.setParameter("workOrderNo", "%"+ mesOnSiteOrderDetail.getWorkOrderNo() +"%");
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getWorkCenterCode())){
            queryObject.setParameter("workCenterCode", "%"+ mesOnSiteOrderDetail.getWorkCenterCode() +"%");
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getOrderNo())){
            queryObject.setParameter(MesExtConstWords.ORDER_NO, "%"+ mesOnSiteOrderDetail.getOrderNo() +"%");
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getCheckResult())){
            queryObject.setParameter("checkResult", mesOnSiteOrderDetail.getCheckResult());
        }

        if(!StringUtils.isEmpty(mesOnSiteOrderDetail.getMaintainStrategy())){
            queryObject.setParameter("maintainStrategy", "%"+ mesOnSiteOrderDetail.getMaintainStrategy() +"%");
        }

        if (!StringUtils.isEmpty(mesOnSiteOrderDetail.getProjectSortNo())) {
            queryObject.setParameter("projectSortNo", mesOnSiteOrderDetail.getProjectSortNo());
        }

        if (!StringUtils.isEmpty(mesOnSiteOrderDetail.getProjectCode())) {
            queryObject.setParameter("projectCode", mesOnSiteOrderDetail.getProjectCode());
        }

        if (!StringUtils.isEmpty(mesOnSiteOrderDetail.getSpotCheckTimeStart())) {
            queryObject.setParameter("spotCheckTimeStart", mesOnSiteOrderDetail.getSpotCheckTimeStart());
        }

        if (!StringUtils.isEmpty(mesOnSiteOrderDetail.getSpotCheckTimeEnd())) {
            queryObject.setParameter("spotCheckTimeEnd", mesOnSiteOrderDetail.getSpotCheckTimeEnd());
        }

        try {
            return ((Long) queryObject.getSingleResult()).intValue();
        }catch (Exception e){
            return 0;
        }
    }
}
