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



import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.user.UserRoomEntity;
import com.qiuguo.iot.data.request.user.UserRoomRequest;
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 UserRoomService extends GenericReactiveCrudService<UserRoomEntity, Long> {

//	@Autowired
//	private ReactiveCrudService<UserRoomEntity, Long> reactiveCrudService;

	public Mono<UserRoomEntity> selectUserRoomByRequest(UserRoomRequest request){
		ReactiveQuery<UserRoomEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(UserRoomRequest::getId, request.getId());
		}
		if(request.getHomeId() != null){
			reactiveQuery = reactiveQuery.and(UserRoomRequest::getHomeId, request.getHomeId());
		}
		if(StringUtils.isNotEmpty(request.getRoomName())){
			reactiveQuery = reactiveQuery.and(UserRoomRequest::getRoomName, request.getRoomName());
		}
		if(request.getSpaceId() != null){
			reactiveQuery = reactiveQuery.and(UserRoomRequest::getSpaceId, request.getSpaceId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(UserRoomRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(UserRoomRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(UserRoomRequest::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<UserRoomEntity>> selectUserRoomsByRequest(UserRoomRequest request){
		ReactiveQuery<UserRoomEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(UserRoomRequest::getId, request.getId());
		}
		if(request.getHomeId() != null){
			reactiveQuery = reactiveQuery.and(UserRoomRequest::getHomeId, request.getHomeId());
		}
		if(StringUtils.isNotEmpty(request.getRoomName())){
			reactiveQuery = reactiveQuery.$like$(UserRoomRequest::getRoomName, request.getRoomName());
		}
		if(request.getSpaceId() != null){
			reactiveQuery = reactiveQuery.and(UserRoomRequest::getSpaceId, request.getSpaceId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(UserRoomRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(UserRoomRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(UserRoomRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(UserRoomRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(UserRoomRequest::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);
//		QueryParamEntity param = new QueryParamEntity();
//		param.setPageSize(request.getPageSize());
//		param.setPageIndex(request.getCurrPage());
////		reactiveQuery.
////		return queryPager(param);
//		return reactiveQuery.paging(request.getCurrPage(),request.getPageSize()).fetch();
	}



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



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



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



	public Mono<Integer> updateCoverUserRoomById(UserRoomEntity entity){
		ReactiveUpdate<UserRoomEntity> update = createUpdate()
                .set(UserRoomEntity::getModifyTime, new Date());
		update = update.set(UserRoomEntity::getHomeId, entity.getHomeId());
		update = update.set(UserRoomEntity::getRoomName, entity.getRoomName());
		update = update.set(UserRoomEntity::getSpaceId, entity.getSpaceId());
		update = update.set(UserRoomEntity::getIsDelete, entity.getIsDelete());
		return update.where(UserRoomEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
