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



import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.ota.OtaRecordEntity;
import com.qiuguo.iot.data.request.ota.OtaRecordRequest;
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>
* OTA升级记录服务类
* </p>
*
* @author wulin
* @since 2023-08-08
*/

@Service
@Slf4j
public class OtaRecordService extends GenericReactiveCrudService<OtaRecordEntity, Long> {


	public Mono<OtaRecordEntity> selectOtaRecordByRequest(OtaRecordRequest request){
		ReactiveQuery<OtaRecordEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(OtaRecordRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(OtaRecordRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(OtaRecordRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(OtaRecordRequest::getModifyTime, request.getModifyTime());
		}
		if(request.getDeviceId() != null){
			reactiveQuery = reactiveQuery.and(OtaRecordRequest::getDeviceId, request.getDeviceId());
		}
		if(request.getUserId() != null){
			reactiveQuery = reactiveQuery.and(OtaRecordRequest::getUserId, request.getUserId());
		}
		if(request.getOtaId() != null){
			reactiveQuery = reactiveQuery.and(OtaRecordRequest::getOtaId, request.getOtaId());
		}
		if(request.getOtaType() != null){
			reactiveQuery = reactiveQuery.and(OtaRecordRequest::getOtaType, request.getOtaType());
		}
		if(request.getFinishTime() != null){
			reactiveQuery = reactiveQuery.and(OtaRecordRequest::getFinishTime, request.getFinishTime());
		}
		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<OtaRecordEntity>> selectOtaRecordsByRequest(OtaRecordRequest request){
		ReactiveQuery<OtaRecordEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(OtaRecordRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(OtaRecordRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(OtaRecordRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(OtaRecordRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(OtaRecordRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(OtaRecordRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(request.getDeviceId() != null){
			reactiveQuery = reactiveQuery.and(OtaRecordRequest::getDeviceId, request.getDeviceId());
		}
		if(request.getUserId() != null){
			reactiveQuery = reactiveQuery.and(OtaRecordRequest::getUserId, request.getUserId());
		}
		if(request.getOtaId() != null){
			reactiveQuery = reactiveQuery.and(OtaRecordRequest::getOtaId, request.getOtaId());
		}
		if(request.getOtaType() != null){
			reactiveQuery = reactiveQuery.and(OtaRecordRequest::getOtaType, request.getOtaType());
		}
		if(request.getFinishTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(OtaRecordRequest::getFinishTime, request.getFinishTimeStart());
		}
		if(request.getFinishTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(OtaRecordRequest::getFinishTime, request.getFinishTimeEnd());
		}
		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<OtaRecordEntity> selectOtaRecordById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



	public Mono<Integer> updateOtaRecordById(OtaRecordEntity entity){
		ReactiveUpdate<OtaRecordEntity> update = createUpdate()
                .set(OtaRecordEntity::getModifyTime, new Date());
		if(entity.getIsDelete() != null){
			update = update.set(OtaRecordEntity::getIsDelete, entity.getIsDelete());
		}
		if(entity.getDeviceId() != null){
			update = update.set(OtaRecordEntity::getDeviceId, entity.getDeviceId());
		}
		if(entity.getUserId() != null){
			update = update.set(OtaRecordEntity::getUserId, entity.getUserId());
		}
		if(entity.getOtaId() != null){
			update = update.set(OtaRecordEntity::getOtaId, entity.getOtaId());
		}
		if(entity.getOtaType() != null){
			update = update.set(OtaRecordEntity::getOtaType, entity.getOtaType());
		}
		if(entity.getFinishTime() != null){
			update = update.set(OtaRecordEntity::getFinishTime, entity.getFinishTime());
		}
		return update.where(OtaRecordEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverOtaRecordById(OtaRecordEntity entity){
		ReactiveUpdate<OtaRecordEntity> update = createUpdate()
                .set(OtaRecordEntity::getModifyTime, new Date());
		update = update.set(OtaRecordEntity::getIsDelete, entity.getIsDelete());
		update = update.set(OtaRecordEntity::getDeviceId, entity.getDeviceId());
		update = update.set(OtaRecordEntity::getUserId, entity.getUserId());
		update = update.set(OtaRecordEntity::getOtaId, entity.getOtaId());
		update = update.set(OtaRecordEntity::getOtaType, entity.getOtaType());
		update = update.set(OtaRecordEntity::getFinishTime, entity.getFinishTime());
		return update.where(OtaRecordEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
