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



import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.u3d.U3dActionEntity;
import com.qiuguo.iot.data.request.u3d.U3dActionRequest;
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>
* U3D动作配置服务类
* </p>
*
* @author wulin
* @since 2023-09-05
*/

@Service
@Slf4j
public class U3dActionService extends GenericReactiveCrudService<U3dActionEntity, Long> {


	public Mono<U3dActionEntity> selectU3dActionByRequest(U3dActionRequest request){
		ReactiveQuery<U3dActionEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(U3dActionRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(U3dActionRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(U3dActionRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(U3dActionRequest::getModifyTime, request.getModifyTime());
		}
		if(StringUtils.isNotEmpty(request.getName())){
			reactiveQuery = reactiveQuery.and(U3dActionRequest::getName, request.getName());
		}
		if(request.getType() != null){
			reactiveQuery = reactiveQuery.and(U3dActionRequest::getType, request.getType());
		}
		if(StringUtils.isNotEmpty(request.getAction())){
			reactiveQuery = reactiveQuery.and(U3dActionRequest::getAction, request.getAction());
		}
		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<U3dActionEntity>> selectU3dActionsByRequest(U3dActionRequest request){
		ReactiveQuery<U3dActionEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(U3dActionRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(U3dActionRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(U3dActionRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(U3dActionRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(U3dActionRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(U3dActionRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(StringUtils.isNotEmpty(request.getName())){
			reactiveQuery = reactiveQuery.$like$(U3dActionRequest::getName, request.getName());
		}
		if(request.getType() != null){
			reactiveQuery = reactiveQuery.and(U3dActionRequest::getType, request.getType());
		}
		if(StringUtils.isNotEmpty(request.getAction())){
			reactiveQuery = reactiveQuery.$like$(U3dActionRequest::getAction, request.getAction());
		}
		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<U3dActionEntity> selectU3dActionById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



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



	public Mono<Integer> updateCoverU3dActionById(U3dActionEntity entity){
		ReactiveUpdate<U3dActionEntity> update = createUpdate()
                .set(U3dActionEntity::getModifyTime, new Date());
		update = update.set(U3dActionEntity::getIsDelete, entity.getIsDelete());
		update = update.set(U3dActionEntity::getName, entity.getName());
		update = update.set(U3dActionEntity::getType, entity.getType());
		update = update.set(U3dActionEntity::getAction, entity.getAction());
		return update.where(U3dActionEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
