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

import cn.estsh.i3plus.ext.mes.apiservice.dao.IWorkOrderExtDao;
import cn.estsh.i3plus.ext.mes.pojo.model.CompleteRateChartModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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.List;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-bu
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2023-02-09 9:24
 */
@Service
@Slf4j
public class WorkOrderExtDao implements IWorkOrderExtDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<CompleteRateChartModel> queryCompleteRateChart(CompleteRateChartModel model) {
        log.info("正在查询达成率计划数量,参数[{}]", JSONObject.toJSONString(model));
        String selectFiled = " select new " + CompleteRateChartModel.class.getName() + "( woe.startTime as workTime, woe.workCenterCode, woe.orderNo as workOrderNo, woe.workOrderStatus, woe.partNo, woe.partNameRdd, " +
                " woe.qty as planQty, woe.startTime as orderStartTime, pe.categoryCode3, wce.workCenterName, wce.isDont, wce.dontClass) ";
        StringBuffer hql = setCompleteRateChartHql(null, model);
        Query query = entityManager.createQuery(selectFiled+hql.toString(), CompleteRateChartModel.class);
        setCompleteRateChartQuery(query, model);
        return query.getResultList();
    }

    @Override
    public List<CompleteRateChartModel> queryCompleteRateChartReuse(CompleteRateChartModel model) {
        CompleteRateChartModel modelTemp = new CompleteRateChartModel();
        BeanUtils.copyProperties(model, modelTemp);
        //改查询复用计划日期
        modelTemp.setReusePlanDatetimeStart(model.getWorkTimeStart());
        modelTemp.setReusePlanDatetimeEnd(model.getWorkTimeEnd());
        modelTemp.setWorkTimeStart(null);
        modelTemp.setWorkTimeEnd(null);

        log.info("正在查询复用达成率计划数量,参数[{}]", JSONObject.toJSONString(modelTemp));
        String selectFiled = " select new " + CompleteRateChartModel.class.getName() + "( woe.reusePlanDatetime as workTime, woe.workCenterCode, woe.orderNo as workOrderNo, woe.workOrderStatus, woe.partNo, woe.partNameRdd, " +
                " woe.reusePlanCompleteQty as planQty, woe.reusePlanDatetime as orderStartTime, pe.categoryCode3, wce.workCenterName, wce.isDont, wce.dontClass) ";
        StringBuffer hql = setCompleteRateChartHql(null, modelTemp);
        Query query = entityManager.createQuery(selectFiled+hql.toString(), CompleteRateChartModel.class);
        setCompleteRateChartQuery(query, modelTemp);
        return query.getResultList();
    }

    //设置查询达成率报表HQL
    private StringBuffer setCompleteRateChartHql(StringBuffer hql,CompleteRateChartModel model) {
        if (hql == null) {
            hql = new StringBuffer();
        }
        hql.append(" from MesWorkOrderExt as woe ");
        hql.append(" left join MesPartExt as pe on (woe.partNo = pe.partNo and pe.organizeCode = :organizeCode and pe.isValid = :isValid and pe.isDeleted = :isDeleted) ");
        hql.append(" left join MesWorkCenterExt as wce on (woe.workCenterCode = wce.workCenterCode and wce.organizeCode = :organizeCode and wce.isValid = :isValid and wce.isDeleted = :isDeleted) ");
        hql.append(" where woe.organizeCode = :organizeCode ");
        hql.append(" and woe.isValid = :isValid ");
        hql.append(" and woe.isDeleted = :isDeleted ");

        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            hql.append(" and woe.startTime >= :workTimeStart ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            hql.append(" and woe.startTime <= :workTimeEnd ");
        }
        if (!StringUtils.isEmpty(model.getReusePlanDatetimeStart())) {
            hql.append(" and woe.reusePlanDatetime >= :reusePlanDatetimeStart ");
        }
        if (!StringUtils.isEmpty(model.getReusePlanDatetimeEnd())) {
            hql.append(" and woe.reusePlanDatetime <= :reusePlanDatetimeEnd ");
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            hql.append(" and woe.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            hql.append(" and woe.partNo = :partNo ");
        }
        if (!StringUtils.isEmpty(model.getCategoryCode3())) {
            hql.append(" and pe.categoryCode3 = :categoryCode3 ");
        }
        if (!StringUtils.isEmpty(model.getIsDont())) {
            hql.append(" and wce.isDont = :isDont ");
        }
        if (!StringUtils.isEmpty(model.getDontClass())) {
            hql.append(" and wce.dontClass = :dontClass ");
        }

        return hql;
    }

    //设置达成率报表参数
    private void setCompleteRateChartQuery(Query query,CompleteRateChartModel model) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, model.getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());

        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            query.setParameter(MesExtConstWords.WORK_TIME_START, model.getWorkTimeStart().substring(0, 10) + " 00:00:00");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            query.setParameter(MesExtConstWords.WORK_TIME_END, model.getWorkTimeEnd().substring(0, 10) + " 23:59:59");
        }
        if (!StringUtils.isEmpty(model.getReusePlanDatetimeStart())) {
            query.setParameter(MesExtConstWords.REUSE_PLAN_DATETIME_START, model.getReusePlanDatetimeStart().substring(0, 10) + " 00:00:00");
        }
        if (!StringUtils.isEmpty(model.getReusePlanDatetimeEnd())) {
            query.setParameter(MesExtConstWords.REUSE_PLAN_DATETIME_END, model.getReusePlanDatetimeEnd().substring(0, 10) + " 23:59:59");
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            query.setParameter(MesExtConstWords.PART_NO, model.getPartNo());
        }
        if (!StringUtils.isEmpty(model.getCategoryCode3())) {
            query.setParameter(MesExtConstWords.CATEGORY_CODE3, model.getCategoryCode3());
        }
        if (!StringUtils.isEmpty(model.getIsDont())) {
            query.setParameter(MesExtConstWords.IS_DONT, model.getIsDont());
        }
        if (!StringUtils.isEmpty(model.getDontClass())) {
            query.setParameter(MesExtConstWords.DONT_CLASS, model.getDontClass());
        }
    }
}
