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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.eactive.eai.data.entity.onl.inflow.InflowControlLog;
import com.eactive.eai.data.entity.onl.inflow.InflowControlLogId;
import com.eactive.eai.data.entity.onl.inflow.QInflowControlGroup;
import com.eactive.eai.data.entity.onl.inflow.QInflowControlLog;
import com.eactive.eai.data.jpa.AbstractDataService;
import com.eactive.eai.rms.onl.manage.inflow.history.InflowControlHistoryManMapper;
import com.eactive.eai.rms.onl.manage.inflow.history.InflowControlHistoryManUI;
import com.eactive.eai.rms.onl.manage.inflow.history.InflowControlHistoryManUISearch;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.Tuple;

@Service
public class InflowControlLogService
		extends AbstractDataService<InflowControlLog, InflowControlLogId, InflowControlLogRepository> {
	
	private final InflowControlService inflowControlService;
	private final InflowControlHistoryManMapper inflowControlHistoryManMapper;

    @Autowired
    public InflowControlLogService(InflowControlService inflowControlService, InflowControlHistoryManMapper inflowControlHistoryManMapper) {
        this.inflowControlService = inflowControlService;
        this.inflowControlHistoryManMapper = inflowControlHistoryManMapper;
    }

	public Page<InflowControlLog> findAll(Pageable pageable, InflowControlHistoryManUISearch uiSearch) {
		QInflowControlLog qInflowConfrolLog = QInflowControlLog.inflowControlLog;
		BooleanBuilder booleanBuilder = new BooleanBuilder();
		booleanBuilder
				.and(qInflowConfrolLog.id.msgdpstyms
						.between(uiSearch.getSearchStartDate() + "000000000",
								uiSearch.getSearchEndDate() + "235959999"));
		return super.repository.findAll(booleanBuilder, pageable);
	}

	public Page<InflowControlHistoryManUI> selectList(Pageable pageable, InflowControlHistoryManUISearch uiSearch) {
		
		Page<Tuple> tuples = inflowControlService.selectLogList(pageable, uiSearch);
		
		return tuples.map(tuple -> {
			InflowControlLog inflowControlLog = tuple.get(QInflowControlLog.inflowControlLog);
			
			String groupName = tuple.get(QInflowControlGroup.inflowControlGroup.groupName);
			
			InflowControlHistoryManUI inflowControlHistoryManUI;
			
			inflowControlHistoryManUI = inflowControlHistoryManMapper.toVo(inflowControlLog);
			
			inflowControlHistoryManUI.setGroupname(groupName);
			
			return inflowControlHistoryManUI;
			
		});
		
		
	}

	public Iterable<InflowControlLog> findAll(InflowControlHistoryManUISearch uiSearch) {
		QInflowControlLog qInflowConfrolLog = QInflowControlLog.inflowControlLog;
		String startTime = StringUtils.defaultIfEmpty(uiSearch.getSearchStartTime(), "000000");
		String endTime = StringUtils.defaultIfEmpty(uiSearch.getSearchEndTime(), "235959");
		BooleanBuilder booleanBuilder = new BooleanBuilder();
		booleanBuilder
				.and(qInflowConfrolLog.id.msgdpstyms
						.between(uiSearch.getSearchStartDate() + startTime + "000",
								uiSearch.getSearchEndDate() + endTime + "999"));
		if (!StringUtils.isEmpty(uiSearch.getSearchInstanceName()))
			booleanBuilder.and(qInflowConfrolLog.eaisevrinstncname.contains(uiSearch.getSearchInstanceName()));
		if (!StringUtils.isEmpty(uiSearch.getSearchApiName()))
			booleanBuilder.and(qInflowConfrolLog.eaisvcname.contains(uiSearch.getSearchApiName()));
		if (!StringUtils.isEmpty(uiSearch.getSearchAdapterGroupName()))
			booleanBuilder.and(qInflowConfrolLog.adptrbzwkgroupname.contains(uiSearch.getSearchAdapterGroupName()));
		return super.repository.findAll(booleanBuilder);
	}
}
