package com.eactive.eai.rms.data.entity.onl.apim.portalinquiry;

import com.eactive.apim.portal.qna.entity.Inquiry;
import com.eactive.apim.portal.qna.entity.QInquiry;
import com.eactive.eai.rms.data.jpa.AbstractEMSDataSerivce;
import com.querydsl.core.BooleanBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(transactionManager = "transactionManagerForEMS")
public class PortalInquiryService extends AbstractEMSDataSerivce<Inquiry, String, PortalInquiryRepository> {

	public Page<Inquiry> findAll(Pageable pageable, PortalInquirySearch portalInquirySearch) {
		QInquiry qInquiry = QInquiry.inquiry;

		BooleanBuilder predicate = new BooleanBuilder();

		if (StringUtils.isNotBlank(portalInquirySearch.getSearchInquiryStatus())) {
			predicate.and(qInquiry.inquiryStatus.eq(portalInquirySearch.getSearchInquiryStatus()));
		}

		if (StringUtils.isNotBlank(portalInquirySearch.getSearchSubjectDetail())) {
			predicate.and(qInquiry.inquirySubject.containsIgnoreCase(portalInquirySearch.getSearchSubjectDetail())
					.or(qInquiry.inquiryDetail.containsIgnoreCase(portalInquirySearch.getSearchSubjectDetail())));
		}

		if (StringUtils.isNotBlank(portalInquirySearch.getSearchInquiryUserName())) {
			predicate.and(qInquiry.inquirer.userName.containsIgnoreCase(portalInquirySearch.getSearchInquiryUserName()));
		}

		if (StringUtils.isNotBlank(portalInquirySearch.getSearchOrgId())) {
			predicate.and(qInquiry.inquirer.portalOrg.id.eq(portalInquirySearch.getSearchOrgId()));
		}

		return repository.findAll(predicate, pageable);
	}
}