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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.IMesNcProcessingDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesNcProcessingPartAndLotModel;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class MesNcProcessingDao implements IMesNcProcessingDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public int queryDefectRecordByPagerCount(String partNo, String lotNo, String org) {

        StringBuffer hql = new StringBuffer();
        hql.append("select  count(1)  from  mes_defect_record as mdr ");
        hql.append(" where mdr.organizeCode = :organizeCode ");
        hql.append(" and mdr.isValid = :isValid ");
        hql.append(" and mdr.isDeleted = :isDeleted ");
        hql.append(" and mdr.part_no = :partNo ");
        hql.append(" and mdr.lotNo = :lotNo ");
        hql.append(" and mdr.nc_type = 0 ");
        hql.append(" group by mdr.part_no , mdr.lotNo ");

        Query query = entityManager.createNativeQuery(hql.toString());

        query.setParameter("organizeCode", org);
        query.setParameter("isValid", 1);
        query.setParameter("isDeleted", 2);

        List list = query.getResultList();
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        return list.size();
    }

    @Override
    public List<MesNcProcessingPartAndLotModel> queryDefectRecordByPager(String partNo, String lotNo, String org, Pager pager) {

        StringBuffer hql = new StringBuffer();
        hql.append("select  mdr.part_no ,mdr.part_name , mdr.lotNo from  mes_defect_record as mdr ");
        hql.append(" where mdr.organizeCode = :organizeCode ");
        hql.append(" and mdr.isValid = :isValid ");
        hql.append(" and mdr.isDeleted = :isDeleted ");
        hql.append(" and mdr.part_no = :partNo ");
        hql.append(" and mdr.lotNo = :lotNo ");
        hql.append(" and mdr.nc_type = 0 ");
        hql.append(" group by mdr.part_no ,mdr.part_name , mdr.lotNo ");

        Query query = entityManager.createNativeQuery(hql.toString());

        query.setParameter("organizeCode", org);
        query.setParameter("isValid", 1);
        query.setParameter("isDeleted", 2);

        if (pager.getCurrentPage() == 0) {
            pager.setCurrentPage(1);
        }
        query.setMaxResults(pager.getPageSize());  //设置获取的数量
        query.setFirstResult(pager.getCurrentPage() * pager.getPageSize() - pager.getPageSize());

        List list = query.getResultList();

        List<MesNcProcessingPartAndLotModel> modelList = new ArrayList<>();

        for (Object result : list) {
            Object[] cells = (Object[]) result;
            MesNcProcessingPartAndLotModel model = new MesNcProcessingPartAndLotModel();
            String defectPartNo = String.valueOf(cells[0]);
            String defectPartName = String.valueOf(cells[1]);
            String defectLot = String.valueOf(cells[2]);
            model.setPartNo(defectPartNo);
            model.setPartName(defectPartName);
            model.setLotNo(defectLot);
            modelList.add(model);
        }
        return modelList;

    }

    @Override
    public List<String> queryPartInspectionCreateUser(String organizeCode) {
        String sql = "SELECT DISTINCT(create_user) " +
                "FROM mes_part_inspection " +
                "WHERE is_valid = :isValid AND is_deleted = :isDeleted AND organize_code = :organizeCode ";
        Query query = entityManager.createNativeQuery(sql);
        query.setParameter("isValid", CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        query.setParameter("isDeleted", CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        query.setParameter("organizeCode", organizeCode);
        List resultList = query.getResultList();
        log.info("queryPartInspectionCreateUser-查询结果-" + resultList);
        if (CollectionUtils.isEmpty(resultList)) {
            return null;
        }else {
            return (List<String>) resultList;
        }
    }

}
