package com.eactive.eai.rms.data.entity.man.cm;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eactive.eai.rms.common.cm.ui.CmLogUISearch;
import com.eactive.eai.rms.data.jpa.AbstractEMSDataSerivce;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

@Service
@Transactional(transactionManager = "transactionManagerForEMS")
public class CmLogEntityService extends AbstractEMSDataSerivce<CmLogEntity, String, CmLogRepository> {

//   public CmLogEntityService(@Qualifier("entityManagerForEMS") EntityManager entityManagerForEMS) {
//      super(entityManagerForEMS);
//   }

   public Page<CmLogEntity> selectList(CmLogUISearch cmLogUISearch, Pageable pageable) {
      QCmLogEntity qCmLogEntity = QCmLogEntity.cmLogEntity;
      QCmLogDetailEntity qCmLogDetail2Entity = QCmLogDetailEntity.cmLogDetailEntity;

      JPAQuery<CmLogEntity> jpaQuery = createBaseQuery(cmLogUISearch);

      long totalCount = jpaQuery.select(QCmLogEntity.cmLogEntity.count()).stream().count();

//      RM37.GUID, RM37.SERVICE_TYPE, RM37.APPL_CD, RM37.DEPLOY_ID, RM37.ROLLBACK_YN, RM37.SUCCESS_YN, RM37.ERROR_MSG, RM37.DEPLOY_TIME, RM37.DEPLOY_END_TIME
      List<CmLogEntity> cmLog = jpaQuery
            .select(qCmLogEntity)
            .limit(pageable.getPageSize())
            .offset(pageable.getOffset())
            .orderBy(qCmLogEntity.deploy_time.desc())
            .fetch();

      return new PageImpl<>(cmLog, pageable, totalCount);
   }

//   public Page<CmLogEntity> selectList(CmLogUISearch cmLogUISearch, Pageable pageable) {
//      QCmLogEntity qCmLogEntity = QCmLogEntity.cmLog2Entity;
//      QCmLogDetail2Entity qCmLogDetail2Entity = QCmLogDetail2Entity.cmLogDetail2Entity;
//
//      JPAQuery<?> query = getJPAQueryFactoryForEMS()
//            .from(qCmLogEntity)
//            .innerJoin(qCmLogDetail2Entity)
//            .on(qCmLogEntity.guid.eq(qCmLogDetail2Entity.guid))
//            .groupBy(qCmLogEntity);
//
//      BooleanBuilder predicate = createSelectListPredicate(cmLogUISearch);
//
//      query.where(predicate);
//
//      long totalCount = query.select(qCmLogEntity.count()).stream().count();
//
//      if(pageable.isPaged()){
//         query.limit(pageable.getPageSize()).offset(pageable.getOffset());
//      }
//
//      List<CmLogEntity> cmLog = query.select(qCmLogEntity).orderBy(qCmLogEntity.guid.asc()).fetch();
//
//      return new PageImpl<>(cmLog, pageable, totalCount);
//   }

   private JPAQuery<CmLogEntity> createBaseQuery(CmLogUISearch cmLogUISearch) {
      QCmLogEntity qCmLogEntity = QCmLogEntity.cmLogEntity;
      QCmLogDetailEntity qCmLogDetailEntity = QCmLogDetailEntity.cmLogDetailEntity;

      BooleanBuilder predicate = createSelectListPredicate(cmLogUISearch);

      return getJPAQueryFactory()
            .select(qCmLogEntity)
            .from(qCmLogEntity)
            .leftJoin(qCmLogDetailEntity)
            .on(qCmLogDetailEntity.id.guid.eq(qCmLogEntity.guid))
            .where(predicate)
            .groupBy(qCmLogEntity.guid, qCmLogEntity.service_type, qCmLogEntity.appl_cd, qCmLogEntity.deploy_id, qCmLogEntity.rollback_yn, qCmLogEntity.success_yn, qCmLogEntity.error_msg, qCmLogEntity.deploy_time, qCmLogEntity.deploy_end_time);
   }

   public BooleanBuilder createSelectListPredicate(CmLogUISearch cmLogUISearch) {
      QCmLogEntity qCmLogEntity = QCmLogEntity.cmLogEntity;
      QCmLogDetailEntity qCmLogDetailEntity = QCmLogDetailEntity.cmLogDetailEntity;
      BooleanBuilder predicate = new BooleanBuilder();

      predicate.and( qCmLogEntity.deploy_time.between( cmLogUISearch.getSearchStartTime(), cmLogUISearch.getSearchEndTime() ) );

      if(StringUtils.isNotBlank(cmLogUISearch.getSearchServiceType())) {
         predicate.and( qCmLogEntity.service_type.eq(cmLogUISearch.getSearchServiceType()) );
      }

      if(StringUtils.isNotBlank(cmLogUISearch.getSearchDeployId())) {
         predicate.and( qCmLogEntity.deploy_id.containsIgnoreCase(cmLogUISearch.getSearchDeployId()) );
      }

      if(StringUtils.isNotBlank(cmLogUISearch.getSearchSuccessYn())) {
         predicate.and( qCmLogEntity.success_yn.containsIgnoreCase(cmLogUISearch.getSearchSuccessYn()) );
      }

      if(StringUtils.isNotBlank(cmLogUISearch.getSearchResourceName())) {
         predicate.and( qCmLogDetailEntity.id.resource_name.containsIgnoreCase( cmLogUISearch.getSearchResourceName() ) );
      }

      return predicate;
   }


   public Page<CmLogEntity> findAll(Pageable pageable, String guid) {
      return repository.findAll( QCmLogEntity.cmLogEntity.guid.containsIgnoreCase(guid) , pageable);
   }

   public Iterable<CmLogEntity> findAll(String guid) {
      return repository.findAll(QCmLogEntity.cmLogEntity.guid.containsIgnoreCase(guid));
   }

//   public long count(String eaiSvcName) {
//      return repository.count(QCmLogEntity.cmLog2Entity.guid.eq(eaiSvcName));
//   }

   public void delete(String id) {
      repository.deleteById(id);
   }



}