package com.qiuguo.iot.data.service.user;


import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.user.UserSuggestionRecordEntity;
import com.qiuguo.iot.data.request.user.UserSuggestionRecordRequest;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.ezorm.rdb.mapping.ReactiveQuery;
import org.hswebframework.ezorm.rdb.mapping.ReactiveUpdate;
import org.hswebframework.ezorm.rdb.operator.dml.query.SortOrder;
import org.hswebframework.web.api.crud.entity.PagerResult;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.Date;
/**
* <p>
* 意见反馈记录服务类
* </p>
*
* @author wulin
* @since 2023-08-08
*/

@Service
@Slf4j
public class UserSuggestionRecordService extends GenericReactiveCrudService<UserSuggestionRecordEntity, Long> {


	public Mono<UserSuggestionRecordEntity> selectUserSuggestionRecordByRequest(UserSuggestionRecordRequest request){
		ReactiveQuery<UserSuggestionRecordEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(UserSuggestionRecordRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(UserSuggestionRecordRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(UserSuggestionRecordRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(UserSuggestionRecordRequest::getModifyTime, request.getModifyTime());
		}
		if(request.getUserId() != null){
			reactiveQuery = reactiveQuery.and(UserSuggestionRecordRequest::getUserId, request.getUserId());
		}
		if(StringUtils.isNotEmpty(request.getSuggestion())){
			reactiveQuery = reactiveQuery.and(UserSuggestionRecordRequest::getSuggestion, request.getSuggestion());
		}
		if(request.getSystemUserId() != null){
			reactiveQuery = reactiveQuery.and(UserSuggestionRecordRequest::getSystemUserId, request.getSystemUserId());
		}
		if(StringUtils.isNotEmpty(request.getResolution())){
			reactiveQuery = reactiveQuery.and(UserSuggestionRecordRequest::getResolution, request.getResolution());
		}
		if(request.getSuggestionType() != null){
			reactiveQuery = reactiveQuery.and(UserSuggestionRecordRequest::getSuggestionType, request.getSuggestionType());
		}
		SortOrder sortOrder = null;
		if(StringUtils.isNotEmpty(request.getOrder())){
			if(StringUtils.isNotEmpty(request.getSort()) && request.getSort().compareTo("0") == 0){
				sortOrder = SortOrder.desc(request.getOrder());
			}else{
				sortOrder = SortOrder.asc(request.getOrder());
			}
			reactiveQuery = reactiveQuery.orderBy(sortOrder);
		}
		return reactiveQuery.fetchOne();
	}



	public Mono<PagerResult<UserSuggestionRecordEntity>> selectUserSuggestionRecordsByRequest(UserSuggestionRecordRequest request){
		ReactiveQuery<UserSuggestionRecordEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(UserSuggestionRecordRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(UserSuggestionRecordRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(UserSuggestionRecordRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(UserSuggestionRecordRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(UserSuggestionRecordRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(UserSuggestionRecordRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(request.getUserId() != null){
			reactiveQuery = reactiveQuery.and(UserSuggestionRecordRequest::getUserId, request.getUserId());
		}
		if(StringUtils.isNotEmpty(request.getSuggestion())){
			reactiveQuery = reactiveQuery.$like$(UserSuggestionRecordRequest::getSuggestion, request.getSuggestion());
		}
		if(request.getSystemUserId() != null){
			reactiveQuery = reactiveQuery.and(UserSuggestionRecordRequest::getSystemUserId, request.getSystemUserId());
		}
		if(StringUtils.isNotEmpty(request.getResolution())){
			reactiveQuery = reactiveQuery.$like$(UserSuggestionRecordRequest::getResolution, request.getResolution());
		}
		if(request.getSuggestionType() != null){
			reactiveQuery = reactiveQuery.and(UserSuggestionRecordRequest::getSuggestionType, request.getSuggestionType());
		}
		SortOrder sortOrder = null;
		if(StringUtils.isNotEmpty(request.getOrder())){
			if(StringUtils.isNotEmpty(request.getSort()) && request.getSort().compareTo("0") == 0){
				sortOrder = SortOrder.desc(request.getOrder());
			}else{
				sortOrder = SortOrder.asc(request.getOrder());
			}
			reactiveQuery = reactiveQuery.orderBy(sortOrder);
		}
		QueryParamEntity param = QueryParamEntity.of(reactiveQuery.getParam());
		param.setPageIndex(request.getCurrPage());
		param.setPageSize(request.getPageSize());
		param.setPaging(true);
		param.setFirstPageIndex(1);

		return queryPager(param);
	}



	public Mono<UserSuggestionRecordEntity> selectUserSuggestionRecordById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



	public Mono<Integer> insertUserSuggestionRecord(UserSuggestionRecordEntity entity){
		entity.setCreateTime(null);
		entity.setModifyTime(null);
		return insert(entity);
	}



	public Mono<Integer> updateUserSuggestionRecordById(UserSuggestionRecordEntity entity){
		ReactiveUpdate<UserSuggestionRecordEntity> update = createUpdate()
                .set(UserSuggestionRecordEntity::getModifyTime, new Date());
		if(entity.getIsDelete() != null){
			update = update.set(UserSuggestionRecordEntity::getIsDelete, entity.getIsDelete());
		}
		if(entity.getUserId() != null){
			update = update.set(UserSuggestionRecordEntity::getUserId, entity.getUserId());
		}
		if(StringUtils.isNotEmpty(entity.getSuggestion())){
			update = update.set(UserSuggestionRecordEntity::getSuggestion, entity.getSuggestion());
		}
		if(entity.getSystemUserId() != null){
			update = update.set(UserSuggestionRecordEntity::getSystemUserId, entity.getSystemUserId());
		}
		if(StringUtils.isNotEmpty(entity.getResolution())){
			update = update.set(UserSuggestionRecordEntity::getResolution, entity.getResolution());
		}
		if(entity.getSuggestionType() != null){
			update = update.set(UserSuggestionRecordEntity::getSuggestionType, entity.getSuggestionType());
		}
		return update.where(UserSuggestionRecordEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverUserSuggestionRecordById(UserSuggestionRecordEntity entity){
		ReactiveUpdate<UserSuggestionRecordEntity> update = createUpdate()
                .set(UserSuggestionRecordEntity::getModifyTime, new Date());
		update = update.set(UserSuggestionRecordEntity::getIsDelete, entity.getIsDelete());
		update = update.set(UserSuggestionRecordEntity::getUserId, entity.getUserId());
		update = update.set(UserSuggestionRecordEntity::getSuggestion, entity.getSuggestion());
		update = update.set(UserSuggestionRecordEntity::getSystemUserId, entity.getSystemUserId());
		update = update.set(UserSuggestionRecordEntity::getResolution, entity.getResolution());
		update = update.set(UserSuggestionRecordEntity::getSuggestionType, entity.getSuggestionType());
		return update.where(UserSuggestionRecordEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> deleteUserSuggestionRecordById(Long id){
		return createUpdate()
				.set("is_delete", 1)
				.set("modify_time", new Date())
				.where("id", id)
				.execute();
	}



}
