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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.IMesProdMouldStatisticsDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesProdMouldRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Description : 生产开模统计
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/6/26 15:46
 * @Modify:
 **/
@Service
public class MesProdMouldStatisticsDaoImpl implements IMesProdMouldStatisticsDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<MesProdMouldRecord> findUndStatisticsMesProdMouldRecord(String organizeCode, Integer pageSize) {
        StringBuilder sql = new StringBuilder();
        sql.append(" select pmr from MesMouldMappingCfg mmc left join MesProdMouldRecord pmr on mmc.equipmentCode = pmr.equipmentCode");
        sql.append(" where mmc.organizeCode = :organizeCode and  mmc.isDeleted = :isDeleted and mmc.isValid = :isValid ");
        sql.append(" and pmr.organizeCode = :organizeCode and  pmr.isDeleted = :isDeleted and pmr.isValid = :isValid ");
        sql.append(" and (pmr.assetNum is null or pmr.assetNum = '') and pmr.mouldNo is not null and pmr.mouldNo != '' order by pmr.createDatetime asc");
        Query query = entityManager.createQuery(sql.toString(), MesProdMouldRecord.class);
        query.setParameter("organizeCode", organizeCode);
        query.setParameter("isValid", CommonEnumUtil.VALID);
        query.setParameter("isDeleted", CommonEnumUtil.FALSE);
        if (!Objects.isNull(pageSize)) {
            query.setFirstResult(MesPcnExtConstWords.ZERO).setMaxResults(pageSize);
        }
        return query.getResultList();
    }

    @Override
    public int updateMesProductionRecord(List<Long> idList, String organizeCode, boolean updateSyncStatus, String userName) {
        StringBuilder sql = new StringBuilder();
        sql.append(" update MesProductionRecord set moduleStatisticsStatus = :moduleStatisticsStatus,modifyUser = :modifyUser");
        if (updateSyncStatus) {
            sql.append(",systemSyncStatus = :systemSyncStatus,modifyDatetime = :modifyDatetime");
        }
        sql.append(" where organizeCode = :organizeCode and  isDeleted = :isDeleted and isValid = :isValid ");
        sql.append(" and id in (:idList) ");
        Query query = entityManager.createQuery(sql.toString());
        query.setParameter("organizeCode", organizeCode);
        query.setParameter("isValid", CommonEnumUtil.VALID);
        query.setParameter("isDeleted", CommonEnumUtil.FALSE);
        query.setParameter("idList", idList);
        query.setParameter("moduleStatisticsStatus", CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        query.setParameter("modifyUser", userName);
        if (updateSyncStatus) {
            query.setParameter("systemSyncStatus", CommonEnumUtil.FALSE);
            query.setParameter("modifyDatetime", TimeTool.getNowTime(true));
        }
        return query.executeUpdate();
    }
}
