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


import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.ota.OtaVersionInfoEntity;
import com.qiuguo.iot.data.request.ota.OtaVersionInfoRequest;
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 OtaVersionInfoService extends GenericReactiveCrudService<OtaVersionInfoEntity, Long> {


	public Mono<OtaVersionInfoEntity> selectOtaVersionInfoByRequest(OtaVersionInfoRequest request){
		ReactiveQuery<OtaVersionInfoEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getModifyTime, request.getModifyTime());
		}
		if(StringUtils.isNotEmpty(request.getVersion())){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getVersion, request.getVersion());
		}
		if(request.getDeviceType() != null){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getDeviceType, request.getDeviceType());
		}
		if(request.getDeviceBathId() != null){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getDeviceBathId, request.getDeviceBathId());
		}
		if(StringUtils.isNotEmpty(request.getMaxVersion())){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getMaxVersion, request.getMaxVersion());
		}
		if(StringUtils.isNotEmpty(request.getMinVersion())){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getMinVersion, request.getMinVersion());
		}
		if(request.getIsForce() != null){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getIsForce, request.getIsForce());
		}
		if(StringUtils.isNotEmpty(request.getDowloadUrl())){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getDowloadUrl, request.getDowloadUrl());
		}
		if(StringUtils.isNotEmpty(request.getMd5())){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getMd5, request.getMd5());
		}
		if(StringUtils.isNotEmpty(request.getRemark())){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getRemark, request.getRemark());
		}
		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<OtaVersionInfoEntity>> selectOtaVersionInfosByRequest(OtaVersionInfoRequest request){
		ReactiveQuery<OtaVersionInfoEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(OtaVersionInfoRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(OtaVersionInfoRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(OtaVersionInfoRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(OtaVersionInfoRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(StringUtils.isNotEmpty(request.getVersion())){
			reactiveQuery = reactiveQuery.$like$(OtaVersionInfoRequest::getVersion, request.getVersion());
		}
		if(request.getDeviceType() != null){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getDeviceType, request.getDeviceType());
		}
		if(request.getDeviceBathId() != null){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getDeviceBathId, request.getDeviceBathId());
		}
		if(StringUtils.isNotEmpty(request.getMaxVersion())){
			reactiveQuery = reactiveQuery.$like$(OtaVersionInfoRequest::getMaxVersion, request.getMaxVersion());
		}
		if(StringUtils.isNotEmpty(request.getMinVersion())){
			reactiveQuery = reactiveQuery.$like$(OtaVersionInfoRequest::getMinVersion, request.getMinVersion());
		}
		if(request.getIsForce() != null){
			reactiveQuery = reactiveQuery.and(OtaVersionInfoRequest::getIsForce, request.getIsForce());
		}
		if(StringUtils.isNotEmpty(request.getDowloadUrl())){
			reactiveQuery = reactiveQuery.$like$(OtaVersionInfoRequest::getDowloadUrl, request.getDowloadUrl());
		}
		if(StringUtils.isNotEmpty(request.getMd5())){
			reactiveQuery = reactiveQuery.$like$(OtaVersionInfoRequest::getMd5, request.getMd5());
		}
		if(StringUtils.isNotEmpty(request.getRemark())){
			reactiveQuery = reactiveQuery.$like$(OtaVersionInfoRequest::getRemark, request.getRemark());
		}
		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<OtaVersionInfoEntity> selectOtaVersionInfoById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



	public Mono<Integer> updateOtaVersionInfoById(OtaVersionInfoEntity entity){
		ReactiveUpdate<OtaVersionInfoEntity> update = createUpdate()
                .set(OtaVersionInfoEntity::getModifyTime, new Date());
		if(entity.getIsDelete() != null){
			update = update.set(OtaVersionInfoEntity::getIsDelete, entity.getIsDelete());
		}
		if(StringUtils.isNotEmpty(entity.getVersion())){
			update = update.set(OtaVersionInfoEntity::getVersion, entity.getVersion());
		}
		if(entity.getDeviceType() != null){
			update = update.set(OtaVersionInfoEntity::getDeviceType, entity.getDeviceType());
		}
		if(entity.getDeviceBathId() != null){
			update = update.set(OtaVersionInfoEntity::getDeviceBathId, entity.getDeviceBathId());
		}
		if(StringUtils.isNotEmpty(entity.getMaxVersion())){
			update = update.set(OtaVersionInfoEntity::getMaxVersion, entity.getMaxVersion());
		}
		if(StringUtils.isNotEmpty(entity.getMinVersion())){
			update = update.set(OtaVersionInfoEntity::getMinVersion, entity.getMinVersion());
		}
		if(entity.getIsForce() != null){
			update = update.set(OtaVersionInfoEntity::getIsForce, entity.getIsForce());
		}
		if(StringUtils.isNotEmpty(entity.getDowloadUrl())){
			update = update.set(OtaVersionInfoEntity::getDowloadUrl, entity.getDowloadUrl());
		}
		if(StringUtils.isNotEmpty(entity.getMd5())){
			update = update.set(OtaVersionInfoEntity::getMd5, entity.getMd5());
		}
		if(StringUtils.isNotEmpty(entity.getRemark())){
			update = update.set(OtaVersionInfoEntity::getRemark, entity.getRemark());
		}
		return update.where(OtaVersionInfoEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverOtaVersionInfoById(OtaVersionInfoEntity entity){
		ReactiveUpdate<OtaVersionInfoEntity> update = createUpdate()
                .set(OtaVersionInfoEntity::getModifyTime, new Date());
		update = update.set(OtaVersionInfoEntity::getIsDelete, entity.getIsDelete());
		update = update.set(OtaVersionInfoEntity::getVersion, entity.getVersion());
		update = update.set(OtaVersionInfoEntity::getDeviceType, entity.getDeviceType());
		update = update.set(OtaVersionInfoEntity::getDeviceBathId, entity.getDeviceBathId());
		update = update.set(OtaVersionInfoEntity::getMaxVersion, entity.getMaxVersion());
		update = update.set(OtaVersionInfoEntity::getMinVersion, entity.getMinVersion());
		update = update.set(OtaVersionInfoEntity::getIsForce, entity.getIsForce());
		update = update.set(OtaVersionInfoEntity::getDowloadUrl, entity.getDowloadUrl());
		update = update.set(OtaVersionInfoEntity::getMd5, entity.getMd5());
		update = update.set(OtaVersionInfoEntity::getRemark, entity.getRemark());
		return update.where(OtaVersionInfoEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
