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 CmLogDetailEntityService extends AbstractEMSDataSerivce<CmLogDetailEntity, CmLogDetailEntityId, CmLogDetailRepository> {

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

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

      JPAQuery<CmLogDetailEntity> jpaQuery = createBaseQuery(cmLogUISearch);

      long totalCount = jpaQuery.select(qCmLogDetailEntity.cmLogDetailEntity.count()).stream().count();

      List<CmLogDetailEntity> cmLogDetail = jpaQuery
            .select(qCmLogDetailEntity)
            .limit(pageable.getPageSize())
            .offset(pageable.getOffset())
            .orderBy(qCmLogDetailEntity.id.guid.asc(), qCmLogDetailEntity.id.resource_name.asc(), qCmLogDetailEntity.id.resource_path.asc())
            .fetch();

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

   private JPAQuery<CmLogDetailEntity> createBaseQuery(CmLogUISearch cmLogUISearch) {
      QCmLogDetailEntity qCmLogDetailEntity = QCmLogDetailEntity.cmLogDetailEntity;

      BooleanBuilder predicate = createSelectListPredicate(cmLogUISearch);

      return getJPAQueryFactory()
            .select(qCmLogDetailEntity)
            .from(qCmLogDetailEntity)
            .where(predicate);
   }

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

      predicate.and( qCmLogDetailEntity.id.guid.eq( cmLogUISearch.getGuid() ) );

      if(StringUtils.isNotBlank(cmLogUISearch.getSearchResourceType())) {
         predicate.and( qCmLogDetailEntity.id.resource_path.containsIgnoreCase( cmLogUISearch.getSearchResourceType() ) );
      }

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

      return predicate;
   }

//   public int setNullCmLogDetail(String deployTime){
//      QCmLogEntity qCmLogEntity = QCmLogEntity.cmLogEntity;
//      QCmLogDetailEntity qCmLogDetailEntity = QCmLogDetailEntity.cmLogDetailEntity;
//
//      int resultCnt = 0;
//
//      return resultCnt;
//   }


}