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



import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.device.DeviceAlarmClockRecordEntity;
import com.qiuguo.iot.data.request.device.DeviceAlarmClockRecordRequest;
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 DeviceAlarmClockRecordService extends GenericReactiveCrudService<DeviceAlarmClockRecordEntity, Long> {


	public Mono<DeviceAlarmClockRecordEntity> selectDeviceAlarmClockRecordByRequest(DeviceAlarmClockRecordRequest request){
		ReactiveQuery<DeviceAlarmClockRecordEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getModifyTime, request.getModifyTime());
		}
		if(request.getDeviceId() != null){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getDeviceId, request.getDeviceId());
		}
		if(request.getUserId() != null){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getUserId, request.getUserId());
		}
		if(request.getTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getTime, request.getTime());
		}
		if(request.getRepeat() != null){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getRepeat, request.getRepeat());
		}
		if(StringUtils.isNotEmpty(request.getRepeatDay())){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getRepeatDay, request.getRepeatDay());
		}
		if(StringUtils.isNotEmpty(request.getTitle())){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getTitle, request.getTitle());
		}
		if(StringUtils.isNotEmpty(request.getReadText())){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getReadText, request.getReadText());
		}
		if(request.getSoundType() != null){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getSoundType, request.getSoundType());
		}
		if(StringUtils.isNotEmpty(request.getSound())){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getSound, request.getSound());
		}
		if(StringUtils.isNotEmpty(request.getSoundName())){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getSoundName, request.getSoundName());
		}
		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<DeviceAlarmClockRecordEntity>> selectDeviceAlarmClockRecordsByRequest(DeviceAlarmClockRecordRequest request){
		ReactiveQuery<DeviceAlarmClockRecordEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceAlarmClockRecordRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceAlarmClockRecordRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceAlarmClockRecordRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceAlarmClockRecordRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(request.getDeviceId() != null){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getDeviceId, request.getDeviceId());
		}
		if(request.getUserId() != null){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getUserId, request.getUserId());
		}
		if(request.getTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceAlarmClockRecordRequest::getTime, request.getTimeStart());
		}
		if(request.getTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceAlarmClockRecordRequest::getTime, request.getTimeEnd());
		}
		if(request.getRepeat() != null){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getRepeat, request.getRepeat());
		}
		if(StringUtils.isNotEmpty(request.getRepeatDay())){
			reactiveQuery = reactiveQuery.$like$(DeviceAlarmClockRecordRequest::getRepeatDay, request.getRepeatDay());
		}
		if(StringUtils.isNotEmpty(request.getTitle())){
			reactiveQuery = reactiveQuery.$like$(DeviceAlarmClockRecordRequest::getTitle, request.getTitle());
		}
		if(StringUtils.isNotEmpty(request.getReadText())){
			reactiveQuery = reactiveQuery.$like$(DeviceAlarmClockRecordRequest::getReadText, request.getReadText());
		}
		if(request.getSoundType() != null){
			reactiveQuery = reactiveQuery.and(DeviceAlarmClockRecordRequest::getSoundType, request.getSoundType());
		}
		if(StringUtils.isNotEmpty(request.getSound())){
			reactiveQuery = reactiveQuery.$like$(DeviceAlarmClockRecordRequest::getSound, request.getSound());
		}
		if(StringUtils.isNotEmpty(request.getSoundName())){
			reactiveQuery = reactiveQuery.$like$(DeviceAlarmClockRecordRequest::getSoundName, request.getSoundName());
		}
		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<DeviceAlarmClockRecordEntity> selectDeviceAlarmClockRecordById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



	public Mono<Integer> updateDeviceAlarmClockRecordById(DeviceAlarmClockRecordEntity entity){
		ReactiveUpdate<DeviceAlarmClockRecordEntity> update = createUpdate()
                .set(DeviceAlarmClockRecordEntity::getModifyTime, new Date());
		if(entity.getIsDelete() != null){
			update = update.set(DeviceAlarmClockRecordEntity::getIsDelete, entity.getIsDelete());
		}
		if(entity.getDeviceId() != null){
			update = update.set(DeviceAlarmClockRecordEntity::getDeviceId, entity.getDeviceId());
		}
		if(entity.getUserId() != null){
			update = update.set(DeviceAlarmClockRecordEntity::getUserId, entity.getUserId());
		}
		if(entity.getTime() != null){
			update = update.set(DeviceAlarmClockRecordEntity::getTime, entity.getTime());
		}
		if(entity.getRepeat() != null){
			update = update.set(DeviceAlarmClockRecordEntity::getRepeat, entity.getRepeat());
		}
		if(StringUtils.isNotEmpty(entity.getRepeatDay())){
			update = update.set(DeviceAlarmClockRecordEntity::getRepeatDay, entity.getRepeatDay());
		}
		if(StringUtils.isNotEmpty(entity.getTitle())){
			update = update.set(DeviceAlarmClockRecordEntity::getTitle, entity.getTitle());
		}
		if(StringUtils.isNotEmpty(entity.getReadText())){
			update = update.set(DeviceAlarmClockRecordEntity::getReadText, entity.getReadText());
		}
		if(entity.getSoundType() != null){
			update = update.set(DeviceAlarmClockRecordEntity::getSoundType, entity.getSoundType());
		}
		if(StringUtils.isNotEmpty(entity.getSound())){
			update = update.set(DeviceAlarmClockRecordEntity::getSound, entity.getSound());
		}
		if(StringUtils.isNotEmpty(entity.getSoundName())){
			update = update.set(DeviceAlarmClockRecordEntity::getSoundName, entity.getSoundName());
		}
		return update.where(DeviceAlarmClockRecordEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverDeviceAlarmClockRecordById(DeviceAlarmClockRecordEntity entity){
		ReactiveUpdate<DeviceAlarmClockRecordEntity> update = createUpdate()
                .set(DeviceAlarmClockRecordEntity::getModifyTime, new Date());
		update = update.set(DeviceAlarmClockRecordEntity::getIsDelete, entity.getIsDelete());
		update = update.set(DeviceAlarmClockRecordEntity::getDeviceId, entity.getDeviceId());
		update = update.set(DeviceAlarmClockRecordEntity::getUserId, entity.getUserId());
		update = update.set(DeviceAlarmClockRecordEntity::getTime, entity.getTime());
		update = update.set(DeviceAlarmClockRecordEntity::getRepeat, entity.getRepeat());
		update = update.set(DeviceAlarmClockRecordEntity::getRepeatDay, entity.getRepeatDay());
		update = update.set(DeviceAlarmClockRecordEntity::getTitle, entity.getTitle());
		update = update.set(DeviceAlarmClockRecordEntity::getReadText, entity.getReadText());
		update = update.set(DeviceAlarmClockRecordEntity::getSoundType, entity.getSoundType());
		update = update.set(DeviceAlarmClockRecordEntity::getSound, entity.getSound());
		update = update.set(DeviceAlarmClockRecordEntity::getSoundName, entity.getSoundName());
		return update.where(DeviceAlarmClockRecordEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
