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



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


	public Mono<ThirdUserBindEntity> selectThirdUserBindByRequest(ThirdUserBindRequest request){
		ReactiveQuery<ThirdUserBindEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(ThirdUserBindRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(ThirdUserBindRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(ThirdUserBindRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(ThirdUserBindRequest::getModifyTime, request.getModifyTime());
		}
		if(request.getUserId() != null){
			reactiveQuery = reactiveQuery.and(ThirdUserBindRequest::getUserId, request.getUserId());
		}
		if(request.getThirdUserId() != null){
			reactiveQuery = reactiveQuery.and(ThirdUserBindRequest::getThirdUserId, request.getThirdUserId());
		}
		if(StringUtils.isNotEmpty(request.getThirdSecurity())){
			reactiveQuery = reactiveQuery.and(ThirdUserBindRequest::getThirdSecurity, request.getThirdSecurity());
		}
		if(request.getThirdId() != null){
			reactiveQuery = reactiveQuery.and(ThirdUserBindRequest::getThirdId, request.getThirdId());
		}
		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<ThirdUserBindEntity>> selectThirdUserBindsByRequest(ThirdUserBindRequest request){
		ReactiveQuery<ThirdUserBindEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(ThirdUserBindRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(ThirdUserBindRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(ThirdUserBindRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(ThirdUserBindRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(ThirdUserBindRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(ThirdUserBindRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(request.getUserId() != null){
			reactiveQuery = reactiveQuery.and(ThirdUserBindRequest::getUserId, request.getUserId());
		}
		if(request.getThirdUserId() != null){
			reactiveQuery = reactiveQuery.and(ThirdUserBindRequest::getThirdUserId, request.getThirdUserId());
		}
		if(StringUtils.isNotEmpty(request.getThirdSecurity())){
			reactiveQuery = reactiveQuery.$like$(ThirdUserBindRequest::getThirdSecurity, request.getThirdSecurity());
		}
		if(request.getThirdId() != null){
			reactiveQuery = reactiveQuery.and(ThirdUserBindRequest::getThirdId, request.getThirdId());
		}
		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<ThirdUserBindEntity> selectThirdUserBindById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



	public Mono<Integer> updateThirdUserBindById(ThirdUserBindEntity entity){
		ReactiveUpdate<ThirdUserBindEntity> update = createUpdate()
                .set(ThirdUserBindEntity::getModifyTime, new Date());
		if(entity.getIsDelete() != null){
			update = update.set(ThirdUserBindEntity::getIsDelete, entity.getIsDelete());
		}
		if(entity.getUserId() != null){
			update = update.set(ThirdUserBindEntity::getUserId, entity.getUserId());
		}
		if(entity.getThirdUserId() != null){
			update = update.set(ThirdUserBindEntity::getThirdUserId, entity.getThirdUserId());
		}
		if(StringUtils.isNotEmpty(entity.getThirdSecurity())){
			update = update.set(ThirdUserBindEntity::getThirdSecurity, entity.getThirdSecurity());
		}
		if(entity.getThirdId() != null){
			update = update.set(ThirdUserBindEntity::getThirdId, entity.getThirdId());
		}
		return update.where(ThirdUserBindEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverThirdUserBindById(ThirdUserBindEntity entity){
		ReactiveUpdate<ThirdUserBindEntity> update = createUpdate()
                .set(ThirdUserBindEntity::getModifyTime, new Date());
		update = update.set(ThirdUserBindEntity::getIsDelete, entity.getIsDelete());
		update = update.set(ThirdUserBindEntity::getUserId, entity.getUserId());
		update = update.set(ThirdUserBindEntity::getThirdUserId, entity.getThirdUserId());
		update = update.set(ThirdUserBindEntity::getThirdSecurity, entity.getThirdSecurity());
		update = update.set(ThirdUserBindEntity::getThirdId, entity.getThirdId());
		return update.where(ThirdUserBindEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
