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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.ISxCommonDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesFourMChange;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPartCheckDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.TotalCheckResult;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
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;

/**
 * @Author: wangjie
 * @CreateDate: 2019/11/21 11:29 上午
 * @Description:
 **/
@Service
public class SxCommonDao implements ISxCommonDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public int queryPagerCount(MesFourMChange fourMChange) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(1) ");
        hql.append(" from MesWorkCell as s ");
        hql.append(" left join MesFourMChange as w on s.workCellCode = w.workCellCode ");
        hql.append(" where s.organizeCode = :organizeCode ");
        hql.append(" and s.isValid = :isValid ");
        hql.append(" and s.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(fourMChange.getWorkCellCode())) {
            hql.append(" and s.workCellCode = :workCellCode ");
        }
        Query query = entityManager.createQuery(hql.toString(), Long.class);
        query.setParameter(MesPcnExtConstWords.ORGANIZE_CODE, AuthUtil.getOrganizeCode());
        query.setParameter(MesPcnExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesPcnExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(fourMChange.getWorkCellCode())) {
            query.setParameter("workCellCode", fourMChange.getWorkCellCode());
        }
        return ((Long) query.getSingleResult()).intValue();
    }


    @Override
    public List<MesFourMChange> queryByPager(MesFourMChange fourMChange, Pager pager) {
        StringBuilder hql = new StringBuilder();
        hql.append(" select  new ")
                .append(MesFourMChange.class.getName())
                .append("( s.workCellCode, ")
                .append("  w.beforeContent, ")
                .append("  w.afterContent, ")
                .append("  w.manageType) ");
        hql.append(" from MesWorkCell as s ");
        hql.append(" left join MesFourMChange as w on s.workCellCode = w.workCellCode ");
        hql.append(" where s.organizeCode = :organizeCode ");
        hql.append(" and s.isValid = :isValid ");
        hql.append(" and s.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(fourMChange.getWorkCellCode())) {
            hql.append(" and s.workCellCode = :workCellCode ");
        }
        hql.append(" order by s.modifyDatetime desc ");
        Query query = entityManager.createQuery(hql.toString(), MesFourMChange.class);
        query.setParameter(MesPcnExtConstWords.ORGANIZE_CODE, AuthUtil.getOrganizeCode());
        query.setParameter(MesPcnExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesPcnExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(fourMChange.getWorkCellCode())) {
            query.setParameter("workCellCode", fourMChange.getWorkCellCode());
        }
        query.setMaxResults(pager.getPageSize());
        if (pager.getCurrentPage() == 0) {
            pager.setCurrentPage(1);
        }
        query.setFirstResult(pager.getCurrentPage() * pager.getPageSize() - pager.getPageSize());
        return query.getResultList();
    }
}
