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


import cn.estsh.i3plus.ext.mes.apiservice.dao.IQManageDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesQManageTaskDetail;
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 QManageDao implements IQManageDao {

    @Autowired
    private EntityManager entityManager;


    @Override
    public List<MesQManageTaskDetail> queryQManageTaskDetailReportByPager(MesQManageTaskDetail mesQmanageTaskDetail, Pager pager) {
        String hql = "select  new " + MesQManageTaskDetail.class.getName() +
                "(qdetail.id,worder.orderNo,qorder.workCenterCode,qdetail.workCellCode,qdetail.workCellName," +
                "qorder.shiftCode,worder.partNo,qdetail.processName,qdetail.manageProjects,qdetail.screwType,qdetail.screwCutterNo," +
                "qdetail.qty,qdetail.tighteningTorque,qdetail.upLimit,qdetail.downLimit,qdetail.manualTightening,qdetail.isIntact,qdetail.spotCheckUser,qdetail.spotCheckTime,qdetail.teamConfirmUser," +
                "qdetail.teamConfirmTime,qdetail.qualityConfirmUser,qdetail.qualityConfirmTime,qdetail.isTeamConfirm,qdetail.isQualityConfirm,qorder.serialNumber, qdetail.checkType)" +
                " from MesQManageTaskDetail qdetail" +
                " LEFT JOIN MesOrderJob qorder on qdetail.orderNo = qorder.orderNo" +
                " and qdetail.organizeCode = qorder.organizeCode and qdetail.isValid = qorder.isValid " +
                " and qdetail.isDeleted = qorder.isDeleted " +
                " LEFT JOIN MesWorkOrder worder on qorder.workOrderNo = worder.orderNo" +
                " and qorder.organizeCode = worder.organizeCode and qorder.isValid = worder.isValid " +
                " and qorder.isDeleted = worder.isDeleted " +
                " where qdetail.organizeCode = :organizeCode " +
                " and qdetail.isValid = :isValid and qdetail.isDeleted = :isDeleted and qorder.orderStatus in ( :orderStatusList )";

        if(!StringUtils.isEmpty(mesQmanageTaskDetail.getWorkCenterCode())){
            hql += " and qorder.workCenterCode = :workCenterCode ";
        }

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

        if(!StringUtils.isEmpty(mesQmanageTaskDetail.getSerialNumber())){
            hql += " and qorder.serialNumber like :serialNumber";
        }

        if(!StringUtils.isEmpty(mesQmanageTaskDetail.getProcessCode())){
            hql += " and qdetail.processCode = :processCode ";
        }

        if(!StringUtils.isEmpty(mesQmanageTaskDetail.getShiftCode())){
            hql += " and qorder.shiftCode = :shiftCode ";
        }

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

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

        if (!StringUtils.isEmpty(mesQmanageTaskDetail.getCheckType())) {
            hql += " and qdetail.checkType = :checkType ";
        }

        if (mesQmanageTaskDetail.getIsTeamConfirm() != null) {
            if (CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == mesQmanageTaskDetail.getIsTeamConfirm()) {
                hql += " and qdetail.isTeamConfirm = :isTeamConfirm ";
            } else {
                hql += " and (qdetail.isTeamConfirm = :isTeamConfirm or qdetail.isTeamConfirm = :isTeamConfirm1) ";
            }
        }

        if (mesQmanageTaskDetail.getIsQualityConfirm() != null) {
            hql += " and qdetail.isQualityConfirm = :isQualityConfirm ";
        }
        hql += " order by qdetail.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", mesQmanageTaskDetail.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(mesQmanageTaskDetail.getWorkCenterCode())){
            queryObject.setParameter("workCenterCode", mesQmanageTaskDetail.getWorkCenterCode());
        }

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

        if(!StringUtils.isEmpty(mesQmanageTaskDetail.getSerialNumber())){
            queryObject.setParameter("serialNumber", "%" + mesQmanageTaskDetail.getSerialNumber() + "%");
        }

        if(!StringUtils.isEmpty(mesQmanageTaskDetail.getProcessCode())){
            queryObject.setParameter("processCode", mesQmanageTaskDetail.getProcessCode());
        }

        if(!StringUtils.isEmpty(mesQmanageTaskDetail.getShiftCode())){
            queryObject.setParameter("shiftCode", mesQmanageTaskDetail.getShiftCode());
        }

        if (!StringUtils.isEmpty(mesQmanageTaskDetail.getCheckType())) {
            queryObject.setParameter("checkType", mesQmanageTaskDetail.getCheckType());
        }

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

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

        if (mesQmanageTaskDetail.getIsTeamConfirm() != null) {
            if (CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == mesQmanageTaskDetail.getIsTeamConfirm()) {
                queryObject.setParameter("isTeamConfirm", mesQmanageTaskDetail.getIsTeamConfirm());
            } else {
                queryObject.setParameter("isTeamConfirm", mesQmanageTaskDetail.getIsTeamConfirm());
                queryObject.setParameter("isTeamConfirm1", 20);
            }
        }

        if (mesQmanageTaskDetail.getIsQualityConfirm() != null) {
            queryObject.setParameter("isQualityConfirm", mesQmanageTaskDetail.getIsQualityConfirm());
        }

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

    @Override
    public int getQManageTaskDetailReportCount(MesQManageTaskDetail mesQmanageTaskDetail) {
        String hql = "select count(distinct qdetail.id) "+
                " from MesQManageTaskDetail qdetail" +
                " LEFT JOIN MesOrderJob qorder on qdetail.orderNo = qorder.orderNo" +
                " and qdetail.organizeCode = qorder.organizeCode and qdetail.isValid = qorder.isValid " +
                " and qdetail.isDeleted = qorder.isDeleted " +
                " LEFT JOIN MesWorkOrder worder on qorder.workOrderNo = worder.orderNo" +
                " and qorder.organizeCode = worder.organizeCode and qorder.isValid = worder.isValid " +
                " and qorder.isDeleted = worder.isDeleted " +
                " where qdetail.organizeCode = :organizeCode " +
                " and qdetail.isValid = :isValid and qdetail.isDeleted = :isDeleted and qorder.orderStatus in ( :orderStatusList )";

        if(!StringUtils.isEmpty(mesQmanageTaskDetail.getWorkCenterCode())){
            hql += " and qorder.workCenterCode = :workCenterCode ";
        }

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

        if(!StringUtils.isEmpty(mesQmanageTaskDetail.getProcessCode())){
            hql += " and qdetail.processCode = :processCode ";
        }

        if(!StringUtils.isEmpty(mesQmanageTaskDetail.getShiftCode())){
            hql += " and qorder.shiftCode = :shiftCode ";
        }

        if (!StringUtils.isEmpty(mesQmanageTaskDetail.getCheckType())) {
            hql += " and qdetail.checkType = :checkType ";
        }

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

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

        if (mesQmanageTaskDetail.getIsTeamConfirm() != null) {
            if (CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == mesQmanageTaskDetail.getIsTeamConfirm()) {
                hql += " and qdetail.isTeamConfirm = :isTeamConfirm ";
            } else {
                hql += " and (qdetail.isTeamConfirm = :isTeamConfirm or qdetail.isTeamConfirm = :isTeamConfirm1) ";
            }
        }

        if (mesQmanageTaskDetail.getIsQualityConfirm() != null) {
            hql += " and qdetail.isQualityConfirm = :isQualityConfirm ";
        }

        Query queryObject = entityManager.createQuery(hql);
        List<Integer> orderStatusList = new ArrayList<>();
        orderStatusList.add(MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECK_COMPLETE.getValue());
        orderStatusList.add(MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CLOSE.getValue());
        queryObject.setParameter("organizeCode", mesQmanageTaskDetail.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(mesQmanageTaskDetail.getWorkCenterCode())){
            queryObject.setParameter("workCenterCode", mesQmanageTaskDetail.getWorkCenterCode());
        }

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

        if(!StringUtils.isEmpty(mesQmanageTaskDetail.getProcessCode())){
            queryObject.setParameter("processCode", mesQmanageTaskDetail.getProcessCode());
        }

        if(!StringUtils.isEmpty(mesQmanageTaskDetail.getShiftCode())){
            queryObject.setParameter("shiftCode", mesQmanageTaskDetail.getShiftCode());
        }

        if (!StringUtils.isEmpty(mesQmanageTaskDetail.getCheckType())) {
            queryObject.setParameter("checkType", mesQmanageTaskDetail.getCheckType());
        }

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

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

        if (mesQmanageTaskDetail.getIsTeamConfirm() != null) {
            if (CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == mesQmanageTaskDetail.getIsTeamConfirm()) {
                queryObject.setParameter("isTeamConfirm", mesQmanageTaskDetail.getIsTeamConfirm());
            } else {
                queryObject.setParameter("isTeamConfirm", mesQmanageTaskDetail.getIsTeamConfirm());
                queryObject.setParameter("isTeamConfirm1", 20);
            }
        }

        if (mesQmanageTaskDetail.getIsQualityConfirm() != null) {
            queryObject.setParameter("isQualityConfirm", mesQmanageTaskDetail.getIsQualityConfirm());
        }

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