package com.eactive.eai.rms.data.entity.onl.layout;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.data.entity.onl.transformer.layout.LayoutEntity;
import com.eactive.eai.data.entity.onl.transformer.layout.QLayoutEntity;
import com.eactive.eai.data.entity.onl.transformer.layout.QLayoutTypeEntity;
import com.eactive.eai.data.jpa.AbstractDataService;
import com.eactive.eai.rms.data.entity.onl.bzwkdstcd.UserBusinessService;
import com.eactive.eai.rms.onl.manage.rule.layout.ui.LayoutUISearch;
import com.querydsl.jpa.impl.JPAQuery;

@Service
@Transactional
public class LayoutService extends AbstractDataService<LayoutEntity, String, LayoutRepository> {

	@Autowired
	private UserBusinessService userBusinessService;

	public long countByLayoutNameAndCurrentUserBusiness(String layoutName) {

		List<String> currentUserBusiness = userBusinessService.getCurrentUserBusinessCodes();

		QLayoutEntity qlayoutEntity = QLayoutEntity.layoutEntity;
		return getJPAQueryFactory()
				.select(qlayoutEntity.count())
				.from(qlayoutEntity)
				.where(qlayoutEntity.loutname.eq(layoutName))
				.where(qlayoutEntity.eaibzwkdstcd.in(currentUserBusiness))
				.fetchOne();
	}

	public Page<LayoutEntity> findAll(Pageable pageable, LayoutUISearch layoutUISearch) {
		QLayoutEntity qLayoutEntity = QLayoutEntity.layoutEntity;
		QLayoutTypeEntity qLayoutTypeEntity = QLayoutTypeEntity.layoutTypeEntity;
		JPAQuery<?> query = getJPAQueryFactory()
				.from(qLayoutEntity)
				.innerJoin(qLayoutTypeEntity)
				.on(qLayoutEntity.loutptrnname.eq(qLayoutTypeEntity.loutptrnname));

		if (StringUtils.isNotBlank(layoutUISearch.getSearchLoutName())) {
			query.where(qLayoutEntity.loutname.containsIgnoreCase(layoutUISearch.getSearchLoutName()));
		}

		if (StringUtils.isNotBlank(layoutUISearch.getSearchEaiBzwkDstcd())) {
			query.where(qLayoutEntity.eaibzwkdstcd.containsIgnoreCase(layoutUISearch.getSearchEaiBzwkDstcd()));
		}

		if (StringUtils.isNotBlank(layoutUISearch.getSearchLoutDesc())) {
			query.where(qLayoutEntity.loutdesc.containsIgnoreCase(layoutUISearch.getSearchLoutDesc()));
		}

		if (StringUtils.isNotBlank(layoutUISearch.getSearchEaiSevrDstcd())) {
			query.where(qLayoutEntity.eaisevrdstcd.eq(layoutUISearch.getSearchEaiSevrDstcd()));
		}

		if (StringUtils.isNotBlank(layoutUISearch.getSearchLayoutTypeName())) {
			query.where(qLayoutEntity.loutptrnname.eq(layoutUISearch.getSearchLayoutTypeName()));
		}

		if (StringUtils.isNotBlank(layoutUISearch.getSearchUseYn())) {
			query.where(qLayoutEntity.useyn.eq(layoutUISearch.getSearchUseYn()));
		}

		long totalCount = query.select(qLayoutEntity.count()).fetchOne();

		if (pageable.isPaged()) {
			query.limit(pageable.getPageSize()).offset(pageable.getOffset());
		}

		List<LayoutEntity> layoutEntities = query.select(qLayoutEntity).orderBy(qLayoutEntity.loutname.asc()).fetch();

		return new PageImpl<>(layoutEntities, pageable, totalCount);

	}

}
