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


import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.third.ThirdConfigInfoEntity;
import com.qiuguo.iot.data.request.third.ThirdConfigInfoRequest;
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 ThirdConfigInfoService extends GenericReactiveCrudService<ThirdConfigInfoEntity, Long> {


	public Mono<ThirdConfigInfoEntity> selectThirdConfigInfoByRequest(ThirdConfigInfoRequest request){
		ReactiveQuery<ThirdConfigInfoEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(ThirdConfigInfoRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(ThirdConfigInfoRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(ThirdConfigInfoRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(ThirdConfigInfoRequest::getModifyTime, request.getModifyTime());
		}
		if(request.getAppId() != null){
			reactiveQuery = reactiveQuery.and(ThirdConfigInfoRequest::getAppId, request.getAppId());
		}
		if(StringUtils.isNotEmpty(request.getAppKey())){
			reactiveQuery = reactiveQuery.and(ThirdConfigInfoRequest::getAppKey, request.getAppKey());
		}
		if(StringUtils.isNotEmpty(request.getAppSecurity())){
			reactiveQuery = reactiveQuery.and(ThirdConfigInfoRequest::getAppSecurity, request.getAppSecurity());
		}
		if(StringUtils.isNotEmpty(request.getOpenName())){
			reactiveQuery = reactiveQuery.and(ThirdConfigInfoRequest::getOpenName, request.getOpenName());
		}
		if(StringUtils.isNotEmpty(request.getOpenCompany())){
			reactiveQuery = reactiveQuery.and(ThirdConfigInfoRequest::getOpenCompany, request.getOpenCompany());
		}
		if(request.getOpenType() != null){
			reactiveQuery = reactiveQuery.and(ThirdConfigInfoRequest::getOpenType, request.getOpenType());
		}
		if(StringUtils.isNotEmpty(request.getSandboxUrl())){
			reactiveQuery = reactiveQuery.and(ThirdConfigInfoRequest::getSandboxUrl, request.getSandboxUrl());
		}
		if(StringUtils.isNotEmpty(request.getProductUrl())){
			reactiveQuery = reactiveQuery.and(ThirdConfigInfoRequest::getProductUrl, request.getProductUrl());
		}
		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<ThirdConfigInfoEntity>> selectThirdConfigInfosByRequest(ThirdConfigInfoRequest request){
		ReactiveQuery<ThirdConfigInfoEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(ThirdConfigInfoRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(ThirdConfigInfoRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(ThirdConfigInfoRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(ThirdConfigInfoRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(ThirdConfigInfoRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(ThirdConfigInfoRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(request.getAppId() != null){
			reactiveQuery = reactiveQuery.and(ThirdConfigInfoRequest::getAppId, request.getAppId());
		}
		if(StringUtils.isNotEmpty(request.getAppKey())){
			reactiveQuery = reactiveQuery.$like$(ThirdConfigInfoRequest::getAppKey, request.getAppKey());
		}
		if(StringUtils.isNotEmpty(request.getAppSecurity())){
			reactiveQuery = reactiveQuery.$like$(ThirdConfigInfoRequest::getAppSecurity, request.getAppSecurity());
		}
		if(StringUtils.isNotEmpty(request.getOpenName())){
			reactiveQuery = reactiveQuery.$like$(ThirdConfigInfoRequest::getOpenName, request.getOpenName());
		}
		if(StringUtils.isNotEmpty(request.getOpenCompany())){
			reactiveQuery = reactiveQuery.$like$(ThirdConfigInfoRequest::getOpenCompany, request.getOpenCompany());
		}
		if(request.getOpenType() != null){
			reactiveQuery = reactiveQuery.and(ThirdConfigInfoRequest::getOpenType, request.getOpenType());
		}
		if(StringUtils.isNotEmpty(request.getSandboxUrl())){
			reactiveQuery = reactiveQuery.$like$(ThirdConfigInfoRequest::getSandboxUrl, request.getSandboxUrl());
		}
		if(StringUtils.isNotEmpty(request.getProductUrl())){
			reactiveQuery = reactiveQuery.$like$(ThirdConfigInfoRequest::getProductUrl, request.getProductUrl());
		}
		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<ThirdConfigInfoEntity> selectThirdConfigInfoById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



	public Mono<Integer> updateThirdConfigInfoById(ThirdConfigInfoEntity entity){
		ReactiveUpdate<ThirdConfigInfoEntity> update = createUpdate()
                .set(ThirdConfigInfoEntity::getModifyTime, new Date());
		if(entity.getIsDelete() != null){
			update = update.set(ThirdConfigInfoEntity::getIsDelete, entity.getIsDelete());
		}
		if(entity.getAppId() != null){
			update = update.set(ThirdConfigInfoEntity::getAppId, entity.getAppId());
		}
		if(StringUtils.isNotEmpty(entity.getAppKey())){
			update = update.set(ThirdConfigInfoEntity::getAppKey, entity.getAppKey());
		}
		if(StringUtils.isNotEmpty(entity.getAppSecurity())){
			update = update.set(ThirdConfigInfoEntity::getAppSecurity, entity.getAppSecurity());
		}
		if(StringUtils.isNotEmpty(entity.getOpenName())){
			update = update.set(ThirdConfigInfoEntity::getOpenName, entity.getOpenName());
		}
		if(StringUtils.isNotEmpty(entity.getOpenCompany())){
			update = update.set(ThirdConfigInfoEntity::getOpenCompany, entity.getOpenCompany());
		}
		if(entity.getOpenType() != null){
			update = update.set(ThirdConfigInfoEntity::getOpenType, entity.getOpenType());
		}
		if(StringUtils.isNotEmpty(entity.getSandboxUrl())){
			update = update.set(ThirdConfigInfoEntity::getSandboxUrl, entity.getSandboxUrl());
		}
		if(StringUtils.isNotEmpty(entity.getProductUrl())){
			update = update.set(ThirdConfigInfoEntity::getProductUrl, entity.getProductUrl());
		}
		return update.where(ThirdConfigInfoEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverThirdConfigInfoById(ThirdConfigInfoEntity entity){
		ReactiveUpdate<ThirdConfigInfoEntity> update = createUpdate()
                .set(ThirdConfigInfoEntity::getModifyTime, new Date());
		update = update.set(ThirdConfigInfoEntity::getIsDelete, entity.getIsDelete());
		update = update.set(ThirdConfigInfoEntity::getAppId, entity.getAppId());
		update = update.set(ThirdConfigInfoEntity::getAppKey, entity.getAppKey());
		update = update.set(ThirdConfigInfoEntity::getAppSecurity, entity.getAppSecurity());
		update = update.set(ThirdConfigInfoEntity::getOpenName, entity.getOpenName());
		update = update.set(ThirdConfigInfoEntity::getOpenCompany, entity.getOpenCompany());
		update = update.set(ThirdConfigInfoEntity::getOpenType, entity.getOpenType());
		update = update.set(ThirdConfigInfoEntity::getSandboxUrl, entity.getSandboxUrl());
		update = update.set(ThirdConfigInfoEntity::getProductUrl, entity.getProductUrl());
		return update.where(ThirdConfigInfoEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
