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


import cn.estsh.i3plus.ext.mes.apiservice.dao.IFirstOrderDetailDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesFirstOrderDetail;
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 FirstOrderDetailDao implements IFirstOrderDetailDao {

    @Autowired
    private EntityManager entityManager;


    @Override
    public List<MesFirstOrderDetail> queryFirstOrderDetailReportByPager(MesFirstOrderDetail mesFirstOrderDetail, Pager pager) {
        String hql = "select  new " + MesFirstOrderDetail.class.getName() +
                "(fdetail.id,forder.workOrderNo,forder.workCenterCode,forder.serialNumber,fdetail.workCellName," +
                "forder.shiftCode,fdetail.processCode,fdetail.processName,fdetail.checkMessage,fdetail.checkStand,fdetail.checkValue," +
                "fdetail.checkResult,fdetail.spotCheckUser,fdetail.spotCheckTime,fdetail.teamConfirmUser," +
                "fdetail.teamConfirmTime,fdetail.qualityConfirmUser,fdetail.qualityConfirmTime,fdetail.orderNo,forder.createDatetime," +
                "fdetail.isQualityConfirm,fdetail.isTeamConfirm,fdetail.upLimit,fdetail.downLimit,fdetail.resultValue,fdetail.importanceSafePartNo," +
                "fdetail.checkFrequency,fdetail.sampleQty,fdetail.confirmMethod)" +
                " from MesFirstOrderDetail fdetail" +
                " LEFT JOIN MesOrderJob forder on fdetail.orderNo = forder.orderNo" +
                " and fdetail.organizeCode = forder.organizeCode and fdetail.isValid = forder.isValid " +
                " and fdetail.isDeleted = forder.isDeleted " +
                " where fdetail.organizeCode = :organizeCode " +
                " and fdetail.isValid = :isValid and fdetail.isDeleted = :isDeleted and forder.orderStatus in ( :orderStatusList )";

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

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

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

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

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

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

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

        if (mesFirstOrderDetail.getIsTeamConfirm() != null) {
            hql += " and fdetail.isTeamConfirm = :isTeamConfirm ";
        }

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

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

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getImportanceSafePartNo())) {
            hql += " and fdetail.importanceSafePartNo = :importanceSafePartNo ";
        }

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getCheckFrequency())) {
            hql += " and fdetail.checkFrequency = :checkFrequency ";
        }

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getSampleQty())) {
            hql += " and fdetail.sampleQty = :sampleQty ";
        }

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getConfirmMethod())) {
            hql += " and fdetail.confirmMethod = :confirmMethod ";
        }
        hql += " order by fdetail.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(MesExtConstWords.ORGANIZE_CODE, mesFirstOrderDetail.getOrganizeCode());
        queryObject.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        queryObject.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        queryObject.setParameter("orderStatusList", orderStatusList);


        if(!StringUtils.isEmpty(mesFirstOrderDetail.getWorkOrderNo())){
            queryObject.setParameter(MesExtConstWords.WORK_ORDER_NO, "%"+ mesFirstOrderDetail.getWorkOrderNo() +"%");
        }

        if(!StringUtils.isEmpty(mesFirstOrderDetail.getProcessCode())){
            queryObject.setParameter(MesExtConstWords.PROCESS_CODE, mesFirstOrderDetail.getProcessCode());
        }

        if(!StringUtils.isEmpty(mesFirstOrderDetail.getWorkCenterCode())){
            queryObject.setParameter(MesExtConstWords.WORK_CENTER_CODE, mesFirstOrderDetail.getWorkCenterCode());
        }

        if(!StringUtils.isEmpty(mesFirstOrderDetail.getSerialNumber())){
            queryObject.setParameter(MesExtConstWords.SERIAL_NUMBER, "%"+ mesFirstOrderDetail.getSerialNumber() +"%");
        }

        if(!StringUtils.isEmpty(mesFirstOrderDetail.getShiftCode())){
            queryObject.setParameter(MesExtConstWords.SHIFT_CODE, mesFirstOrderDetail.getShiftCode());
        }

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getSpotCheckTimeStart())) {
            queryObject.setParameter(MesExtConstWords.SPOT_CHECK_TIME_START, mesFirstOrderDetail.getSpotCheckTimeStart());
        }

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getSpotCheckTimeEnd())) {
            queryObject.setParameter(MesExtConstWords.SPOT_CHECK_TIME_END, mesFirstOrderDetail.getSpotCheckTimeEnd());
        }

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getIsTeamConfirm())) {
            queryObject.setParameter(MesExtConstWords.IS_TEAM_CONFIRM, mesFirstOrderDetail.getIsTeamConfirm());
        }

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getIsQualityConfirm())) {
            queryObject.setParameter(MesExtConstWords.IS_QUALITY_CONFIRM, mesFirstOrderDetail.getIsQualityConfirm());
        }

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getCheckResult())) {
            queryObject.setParameter(MesExtConstWords.CHECK_RESULT, mesFirstOrderDetail.getCheckResult());
        }

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

    @Override
    public int getFirstOrderDetailReportCount(MesFirstOrderDetail mesFirstOrderDetail) {
        String hql = "select count(distinct fdetail.id)" +
                " from MesFirstOrderDetail fdetail" +
                " LEFT JOIN MesOrderJob forder on fdetail.orderNo = forder.orderNo" +
                " and fdetail.organizeCode = forder.organizeCode and fdetail.isValid = forder.isValid " +
                " and fdetail.isDeleted = forder.isDeleted " +
                " where fdetail.organizeCode = :organizeCode " +
                " and fdetail.isValid = :isValid and fdetail.isDeleted = :isDeleted and forder.orderStatus in ( :orderStatusList )";

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

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

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

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

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

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

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

        if (mesFirstOrderDetail.getIsTeamConfirm() != null) {
            hql += " and fdetail.isTeamConfirm = :isTeamConfirm ";
        }

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

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

        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(MesExtConstWords.ORGANIZE_CODE, mesFirstOrderDetail.getOrganizeCode());
        queryObject.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        queryObject.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        queryObject.setParameter("orderStatusList", orderStatusList);


        if(!StringUtils.isEmpty(mesFirstOrderDetail.getWorkOrderNo())){
            queryObject.setParameter(MesExtConstWords.WORK_ORDER_NO, "%"+ mesFirstOrderDetail.getWorkOrderNo() +"%");
        }

        if(!StringUtils.isEmpty(mesFirstOrderDetail.getProcessCode())){
            queryObject.setParameter(MesExtConstWords.PROCESS_CODE, mesFirstOrderDetail.getProcessCode());
        }

        if(!StringUtils.isEmpty(mesFirstOrderDetail.getWorkCenterCode())){
            queryObject.setParameter(MesExtConstWords.WORK_CENTER_CODE, mesFirstOrderDetail.getWorkCenterCode());
        }

        if(!StringUtils.isEmpty(mesFirstOrderDetail.getSerialNumber())){
            queryObject.setParameter(MesExtConstWords.SERIAL_NUMBER, "%"+ mesFirstOrderDetail.getSerialNumber() +"%");
        }

        if(!StringUtils.isEmpty(mesFirstOrderDetail.getShiftCode())){
            queryObject.setParameter(MesExtConstWords.SHIFT_CODE, mesFirstOrderDetail.getShiftCode());
        }

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getSpotCheckTimeStart())) {
            queryObject.setParameter(MesExtConstWords.SPOT_CHECK_TIME_START, mesFirstOrderDetail.getSpotCheckTimeStart());
        }

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getSpotCheckTimeEnd())) {
            queryObject.setParameter(MesExtConstWords.SPOT_CHECK_TIME_END, mesFirstOrderDetail.getSpotCheckTimeEnd());
        }

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getIsTeamConfirm())) {
            queryObject.setParameter(MesExtConstWords.IS_TEAM_CONFIRM, mesFirstOrderDetail.getIsTeamConfirm());
        }

        if (mesFirstOrderDetail.getIsQualityConfirm() != null) {
            queryObject.setParameter(MesExtConstWords.IS_QUALITY_CONFIRM, mesFirstOrderDetail.getIsQualityConfirm());
        }

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getCheckResult())) {
            queryObject.setParameter(MesExtConstWords.CHECK_RESULT, mesFirstOrderDetail.getCheckResult());
        }

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