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



import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.user.UserHomeEntity;
import com.qiuguo.iot.data.request.user.UserHomeRequest;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
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;

/**
* <p>
* 服务类
* </p>
*
* @author wulin
* @since 2023-09-18
*/

@Service
@Slf4j
public class UserHomeService extends GenericReactiveCrudService<UserHomeEntity, Long> {


	public Mono<UserHomeEntity> selectUserHomeByRequest(UserHomeRequest request){
		ReactiveQuery<UserHomeEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(UserHomeRequest::getId, request.getId());
		}
		if(request.getUserId() != null){
			reactiveQuery = reactiveQuery.and(UserHomeRequest::getUserId, request.getUserId());
		}
		if(StringUtils.isNotEmpty(request.getHomeName())){
			reactiveQuery = reactiveQuery.and(UserHomeRequest::getHomeName, request.getHomeName());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(UserHomeRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(UserHomeRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(UserHomeRequest::getModifyTime, request.getModifyTime());
		}
		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<UserHomeEntity>> selectUserHomesByRequest(UserHomeRequest request){
		ReactiveQuery<UserHomeEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(UserHomeRequest::getId, request.getId());
		}
		if(request.getUserId() != null){
			reactiveQuery = reactiveQuery.and(UserHomeRequest::getUserId, request.getUserId());
		}
		if(StringUtils.isNotEmpty(request.getHomeName())){
			reactiveQuery = reactiveQuery.$like$(UserHomeRequest::getHomeName, request.getHomeName());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(UserHomeRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(UserHomeRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(UserHomeRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(UserHomeRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(UserHomeRequest::getModifyTime, request.getModifyTimeEnd());
		}
		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);
		//return reactiveQuery.paging(request.getCurrPage(), request.getPageSize()).fetch();
	}



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



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



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



	public Mono<Integer> updateCoverUserHomeById(UserHomeEntity entity){
		ReactiveUpdate<UserHomeEntity> update = createUpdate()
                .set(UserHomeEntity::getModifyTime, new Date());
		update = update.set(UserHomeEntity::getUserId, entity.getUserId());
		update = update.set(UserHomeEntity::getHomeName, entity.getHomeName());
		update = update.set(UserHomeEntity::getIsDelete, entity.getIsDelete());
		return update.where(UserHomeEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
