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



import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.system.SystemAddressEntity;
import com.qiuguo.iot.data.request.system.SystemAddressRequest;
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-09-26
*/

@Service
@Slf4j
public class SystemAddressService extends GenericReactiveCrudService<SystemAddressEntity, Long> {


	public Mono<SystemAddressEntity> selectSystemAddressByRequest(SystemAddressRequest request){
		ReactiveQuery<SystemAddressEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getModifyTime, request.getModifyTime());
		}
		if(request.getParentId() != null){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getParentId, request.getParentId());
		}
		if(request.getAdcode() != null){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getAdcode, request.getAdcode());
		}
		if(request.getPadcode() != null){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getPadcode, request.getPadcode());
		}
		if(StringUtils.isNotEmpty(request.getFirst())){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getFirst, request.getFirst());
		}
		if(StringUtils.isNotEmpty(request.getShortName())){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getShortName, request.getShortName());
		}
		if(StringUtils.isNotEmpty(request.getName())){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getName, request.getName());
		}
		if(StringUtils.isNotEmpty(request.getAdcodeLink())){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getAdcodeLink, request.getAdcodeLink());
		}
		if(StringUtils.isNotEmpty(request.getNameLink())){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getNameLink, request.getNameLink());
		}
		if(request.getLevel() != null){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getLevel, request.getLevel());
		}
		if(StringUtils.isNotEmpty(request.getPinyin())){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getPinyin, request.getPinyin());
		}
		if(StringUtils.isNotEmpty(request.getPostcode())){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getPostcode, request.getPostcode());
		}
		if(request.getStatus() != null){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getStatus, request.getStatus());
		}
		if(StringUtils.isNotEmpty(request.getLng())){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getLng, request.getLng());
		}
		if(StringUtils.isNotEmpty(request.getLat())){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getLat, request.getLat());
		}
		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<SystemAddressEntity>> selectSystemAddresssByRequest(SystemAddressRequest request){
		ReactiveQuery<SystemAddressEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(SystemAddressRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(SystemAddressRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(SystemAddressRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(SystemAddressRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(request.getParentId() != null){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getParentId, request.getParentId());
		}
		if(request.getAdcode() != null){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getAdcode, request.getAdcode());
		}
		if(request.getPadcode() != null){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getPadcode, request.getPadcode());
		}
		if(StringUtils.isNotEmpty(request.getFirst())){
			reactiveQuery = reactiveQuery.$like$(SystemAddressRequest::getFirst, request.getFirst());
		}
		if(StringUtils.isNotEmpty(request.getShortName())){
			reactiveQuery = reactiveQuery.$like$(SystemAddressRequest::getShortName, request.getShortName());
		}
		if(StringUtils.isNotEmpty(request.getName())){
			reactiveQuery = reactiveQuery.$like$(SystemAddressRequest::getName, request.getName());
		}
		if(StringUtils.isNotEmpty(request.getAdcodeLink())){
			reactiveQuery = reactiveQuery.$like$(SystemAddressRequest::getAdcodeLink, request.getAdcodeLink());
		}
		if(StringUtils.isNotEmpty(request.getNameLink())){
			reactiveQuery = reactiveQuery.$like$(SystemAddressRequest::getNameLink, request.getNameLink());
		}
		if(request.getLevel() != null){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getLevel, request.getLevel());
		}
		if(StringUtils.isNotEmpty(request.getPinyin())){
			reactiveQuery = reactiveQuery.$like$(SystemAddressRequest::getPinyin, request.getPinyin());
		}
		if(StringUtils.isNotEmpty(request.getPostcode())){
			reactiveQuery = reactiveQuery.$like$(SystemAddressRequest::getPostcode, request.getPostcode());
		}
		if(request.getStatus() != null){
			reactiveQuery = reactiveQuery.and(SystemAddressRequest::getStatus, request.getStatus());
		}
		if(StringUtils.isNotEmpty(request.getLng())){
			reactiveQuery = reactiveQuery.$like$(SystemAddressRequest::getLng, request.getLng());
		}
		if(StringUtils.isNotEmpty(request.getLat())){
			reactiveQuery = reactiveQuery.$like$(SystemAddressRequest::getLat, request.getLat());
		}
		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<SystemAddressEntity> selectSystemAddressById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



	public Mono<Integer> insertSystemAddress(SystemAddressEntity entity){
		entity.setId(null);
		entity.setCreateTime(null);
		entity.setModifyTime(null);
		return insert(entity);
	}



	public Mono<Integer> updateSystemAddressById(SystemAddressEntity entity){
		ReactiveUpdate<SystemAddressEntity> update = createUpdate()
                .set(SystemAddressEntity::getModifyTime, new Date());
		if(entity.getIsDelete() != null){
			update = update.set(SystemAddressEntity::getIsDelete, entity.getIsDelete());
		}
		if(entity.getParentId() != null){
			update = update.set(SystemAddressEntity::getParentId, entity.getParentId());
		}
		if(entity.getAdcode() != null){
			update = update.set(SystemAddressEntity::getAdcode, entity.getAdcode());
		}
		if(entity.getPadcode() != null){
			update = update.set(SystemAddressEntity::getPadcode, entity.getPadcode());
		}
		if(StringUtils.isNotEmpty(entity.getFirst())){
			update = update.set(SystemAddressEntity::getFirst, entity.getFirst());
		}
		if(StringUtils.isNotEmpty(entity.getShortName())){
			update = update.set(SystemAddressEntity::getShortName, entity.getShortName());
		}
		if(StringUtils.isNotEmpty(entity.getName())){
			update = update.set(SystemAddressEntity::getName, entity.getName());
		}
		if(StringUtils.isNotEmpty(entity.getAdcodeLink())){
			update = update.set(SystemAddressEntity::getAdcodeLink, entity.getAdcodeLink());
		}
		if(StringUtils.isNotEmpty(entity.getNameLink())){
			update = update.set(SystemAddressEntity::getNameLink, entity.getNameLink());
		}
		if(entity.getLevel() != null){
			update = update.set(SystemAddressEntity::getLevel, entity.getLevel());
		}
		if(StringUtils.isNotEmpty(entity.getPinyin())){
			update = update.set(SystemAddressEntity::getPinyin, entity.getPinyin());
		}
		if(StringUtils.isNotEmpty(entity.getPostcode())){
			update = update.set(SystemAddressEntity::getPostcode, entity.getPostcode());
		}
		if(entity.getStatus() != null){
			update = update.set(SystemAddressEntity::getStatus, entity.getStatus());
		}
		if(StringUtils.isNotEmpty(entity.getLng())){
			update = update.set(SystemAddressEntity::getLng, entity.getLng());
		}
		if(StringUtils.isNotEmpty(entity.getLat())){
			update = update.set(SystemAddressEntity::getLat, entity.getLat());
		}
		return update.where(SystemAddressEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverSystemAddressById(SystemAddressEntity entity){
		ReactiveUpdate<SystemAddressEntity> update = createUpdate()
                .set(SystemAddressEntity::getModifyTime, new Date());
		update = update.set(SystemAddressEntity::getIsDelete, entity.getIsDelete());
		update = update.set(SystemAddressEntity::getParentId, entity.getParentId());
		update = update.set(SystemAddressEntity::getAdcode, entity.getAdcode());
		update = update.set(SystemAddressEntity::getPadcode, entity.getPadcode());
		update = update.set(SystemAddressEntity::getFirst, entity.getFirst());
		update = update.set(SystemAddressEntity::getShortName, entity.getShortName());
		update = update.set(SystemAddressEntity::getName, entity.getName());
		update = update.set(SystemAddressEntity::getAdcodeLink, entity.getAdcodeLink());
		update = update.set(SystemAddressEntity::getNameLink, entity.getNameLink());
		update = update.set(SystemAddressEntity::getLevel, entity.getLevel());
		update = update.set(SystemAddressEntity::getPinyin, entity.getPinyin());
		update = update.set(SystemAddressEntity::getPostcode, entity.getPostcode());
		update = update.set(SystemAddressEntity::getStatus, entity.getStatus());
		update = update.set(SystemAddressEntity::getLng, entity.getLng());
		update = update.set(SystemAddressEntity::getLat, entity.getLat());
		return update.where(SystemAddressEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
