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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.IExteriorScratchProofDetailDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesExteriorScratchProofDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesExteriorScratchProofDetailModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
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: xiangming
 * @CreateDate: 2021-03-12
 * @Description:
 **/
@Service
public class ExteriorScratchProofDetailDao implements IExteriorScratchProofDetailDao {
    @Autowired
    private EntityManager entityManager;

    @Override
    public List<MesExteriorScratchProofDetailModel> queryExteriorScratchProofDetail(String orderNo, String organizeCode, String workCellCode) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select DISTINCT new " + MesExteriorScratchProofDetailModel.class.getName() + "(pe.id, pe.workCenterCode, pe.workCellCode, " +
                " pe.orderTime, pe.orderNo, pe.checkResult, pe.checkUser, pe.checkTime, pe.shiftGroupCheck, pe.shiftGroupCheckUser, " +
                " pe.shiftGroupCheckTime, pe.qualityCheck, pe.qualityCheckUser, pe.qualityCheckTime, " +
                " pe.fileId, f.fileName, f.fileUrl, f.groupName, f.fileOriginName, f.fileSize, f.fileType, " +
                " pe.organizeCode, pe.isValid, pe.isDeleted, pe.createUser, pe.createDatetime, pe.modifyUser, pe.modifyDatetime, pe.remark) ");
        hql.append(" from MesExteriorScratchProofDetail as pe ");
        hql.append(" left join MesFile as f on (pe.fileId = f.id and f.organizeCode = :organizeCode and f.isValid = :isValid and f.isDeleted = :isDeleted) ");
        hql.append(" left join MesOrderJob as oj on (pe.orderNo = oj.orderNo and oj.organizeCode = :organizeCode and oj.isValid = :isValid and oj.isDeleted = :isDeleted) ");
        hql.append(" where pe.organizeCode = :organizeCode ");
        hql.append(" and pe.isValid = :isValid ");
        hql.append(" and pe.isDeleted = :isDeleted ");
        hql.append(" and oj.orderNo = :orderNo ");
        if(!StringUtils.isEmpty(workCellCode)){
            hql.append(" and pe.workCellCode = :workCellCode ");
        }
        hql.append(" order by pe.orderNo,pe.modifyDatetime desc ");

        Query query = entityManager.createQuery(hql.toString(), MesExteriorScratchProofDetailModel.class);

        query.setParameter(MesPcnExtConstWords.ORGANIZE_CODE, organizeCode);
        query.setParameter(MesPcnExtConstWords.IS_VALID, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        query.setParameter(MesPcnExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        query.setParameter(MesPcnExtConstWords.ORDER_NO, orderNo);
        if(!StringUtils.isEmpty(workCellCode)){
            query.setParameter(MesPcnExtConstWords.WORK_CELL_CODE, workCellCode);
        }
        return query.getResultList();
    }

}
