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



import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.user.UserHandlingDeviceEntity;
import com.qiuguo.iot.data.request.user.UserHandlingDeviceRequest;
import java.util.List;
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 org.springframework.util.ObjectUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

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

@Service
@Slf4j
public class UserHandlingDeviceService extends GenericReactiveCrudService<UserHandlingDeviceEntity, Long> {


	public Mono<UserHandlingDeviceEntity> selectUserHandlingDeviceByRequest(UserHandlingDeviceRequest request){
		ReactiveQuery<UserHandlingDeviceEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getId, request.getId());
		}
		if(StringUtils.isNotEmpty(request.getCode())){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getCode, request.getCode());
		}
		if(StringUtils.isNotEmpty(request.getValue())){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getValue, request.getValue());
		}
		if(StringUtils.isNotEmpty(request.getType())){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getType, request.getType());
		}
		if(request.getMax() != null){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getMax, request.getMax());
		}
		if(request.getMin() != null){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getMin, request.getMin());
		}
		if(request.getStep() != null){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getStep, request.getStep());
		}
		if(StringUtils.isNotEmpty(request.getMatchingFields())){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getMatchingFields, request.getMatchingFields());
		}
		if(StringUtils.isNotEmpty(request.getCategoryCode())){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getCategoryCode, request.getCategoryCode());
		}
		if(StringUtils.isNotEmpty(request.getCategoryName())){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getCategoryName, request.getCategoryName());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::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<UserHandlingDeviceEntity>> selectUserHandlingDevicesByRequest(UserHandlingDeviceRequest request){
		ReactiveQuery<UserHandlingDeviceEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getId, request.getId());
		}
		if(StringUtils.isNotEmpty(request.getCode())){
			reactiveQuery = reactiveQuery.$like$(UserHandlingDeviceRequest::getCode, request.getCode());
		}
		if(StringUtils.isNotEmpty(request.getValue())){
			reactiveQuery = reactiveQuery.$like$(UserHandlingDeviceRequest::getValue, request.getValue());
		}
		if(StringUtils.isNotEmpty(request.getType())){
			reactiveQuery = reactiveQuery.$like$(UserHandlingDeviceRequest::getType, request.getType());
		}
		if(request.getMax() != null){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getMax, request.getMax());
		}
		if(request.getMin() != null){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getMin, request.getMin());
		}
		if(request.getStep() != null){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getStep, request.getStep());
		}
		if(StringUtils.isNotEmpty(request.getMatchingFields())){
			reactiveQuery = reactiveQuery.$like$(UserHandlingDeviceRequest::getMatchingFields, request.getMatchingFields());
		}
		if(StringUtils.isNotEmpty(request.getCategoryCode())){
			reactiveQuery = reactiveQuery.$like$(UserHandlingDeviceRequest::getCategoryCode, request.getCategoryCode());
		}
		if(StringUtils.isNotEmpty(request.getCategoryName())){
			reactiveQuery = reactiveQuery.$like$(UserHandlingDeviceRequest::getCategoryName, request.getCategoryName());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(UserHandlingDeviceRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(UserHandlingDeviceRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(UserHandlingDeviceRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(UserHandlingDeviceRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(UserHandlingDeviceRequest::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);
	}



	public Mono<UserHandlingDeviceEntity> selectUserHandlingDeviceById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}
	
	public Flux<UserHandlingDeviceEntity> selectByIds(List<Long> ids){
		if (ObjectUtils.isEmpty(ids)) {
			return Flux.empty();
		}
		return createQuery().and("is_delete", 0).in("id", ids).fetch();
	}



	public Mono<Integer> insertUserHandlingDevice(UserHandlingDeviceEntity entity){
		entity.setId(null);
		entity.setCreateTime(null);
		entity.setModifyTime(null);
		return insert(entity);
	}



	public Mono<Integer> updateUserHandlingDeviceById(UserHandlingDeviceEntity entity){
		ReactiveUpdate<UserHandlingDeviceEntity> update = createUpdate()
                .set(UserHandlingDeviceEntity::getModifyTime, new Date());
		if(StringUtils.isNotEmpty(entity.getCode())){
			update = update.set(UserHandlingDeviceEntity::getCode, entity.getCode());
		}
		if(StringUtils.isNotEmpty(entity.getValue())){
			update = update.set(UserHandlingDeviceEntity::getValue, entity.getValue());
		}
		if(StringUtils.isNotEmpty(entity.getType())){
			update = update.set(UserHandlingDeviceEntity::getType, entity.getType());
		}
		if(entity.getMax() != null){
			update = update.set(UserHandlingDeviceEntity::getMax, entity.getMax());
		}
		if(entity.getMin() != null){
			update = update.set(UserHandlingDeviceEntity::getMin, entity.getMin());
		}
		if(entity.getStep() != null){
			update = update.set(UserHandlingDeviceEntity::getStep, entity.getStep());
		}
		if(StringUtils.isNotEmpty(entity.getMatchingFields())){
			update = update.set(UserHandlingDeviceEntity::getMatchingFields, entity.getMatchingFields());
		}
		if(StringUtils.isNotEmpty(entity.getCategoryCode())){
			update = update.set(UserHandlingDeviceEntity::getCategoryCode, entity.getCategoryCode());
		}
		if(StringUtils.isNotEmpty(entity.getCategoryName())){
			update = update.set(UserHandlingDeviceEntity::getCategoryName, entity.getCategoryName());
		}
		if(entity.getIsDelete() != null){
			update = update.set(UserHandlingDeviceEntity::getIsDelete, entity.getIsDelete());
		}
		return update.where(UserHandlingDeviceEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverUserHandlingDeviceById(UserHandlingDeviceEntity entity){
		ReactiveUpdate<UserHandlingDeviceEntity> update = createUpdate()
                .set(UserHandlingDeviceEntity::getModifyTime, new Date());
		update = update.set(UserHandlingDeviceEntity::getCode, entity.getCode());
		update = update.set(UserHandlingDeviceEntity::getValue, entity.getValue());
		update = update.set(UserHandlingDeviceEntity::getType, entity.getType());
		update = update.set(UserHandlingDeviceEntity::getMax, entity.getMax());
		update = update.set(UserHandlingDeviceEntity::getMin, entity.getMin());
		update = update.set(UserHandlingDeviceEntity::getStep, entity.getStep());
		update = update.set(UserHandlingDeviceEntity::getMatchingFields, entity.getMatchingFields());
		update = update.set(UserHandlingDeviceEntity::getCategoryCode, entity.getCategoryCode());
		update = update.set(UserHandlingDeviceEntity::getCategoryName, entity.getCategoryName());
		update = update.set(UserHandlingDeviceEntity::getIsDelete, entity.getIsDelete());
		return update.where(UserHandlingDeviceEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
