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


import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.device.DeviceUserBindEntity;
import com.qiuguo.iot.data.request.device.DeviceUserBindRequest;
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 DeviceUserBindService extends GenericReactiveCrudService<DeviceUserBindEntity, Long> {


	public Mono<DeviceUserBindEntity> selectDeviceUserBindByRequest(DeviceUserBindRequest request){
		ReactiveQuery<DeviceUserBindEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getModifyTime, request.getModifyTime());
		}
		if(request.getUserId() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getUserId, request.getUserId());
		}
		if(request.getDeviceId() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getDeviceId, request.getDeviceId());
		}
		if(request.getDeviceType() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getDeviceType, request.getDeviceType());
		}
		if(request.getIsMain() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getIsMain, request.getIsMain());
		}
		if(StringUtils.isNotEmpty(request.getBindName())){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getBindName, request.getBindName());
		}
		if(StringUtils.isNotEmpty(request.getCountry())){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getCountry, request.getCountry());
		}
		if(StringUtils.isNotEmpty(request.getProvince())){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getProvince, request.getProvince());
		}
		if(StringUtils.isNotEmpty(request.getCity())){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getCity, request.getCity());
		}
		if(StringUtils.isNotEmpty(request.getCounty())){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getCounty, request.getCounty());
		}
		if(StringUtils.isNotEmpty(request.getVillage())){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getVillage, request.getVillage());
		}
		if(StringUtils.isNotEmpty(request.getAddress())){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getAddress, request.getAddress());
		}
		if(StringUtils.isNotEmpty(request.getCategoryCode())){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getCategoryCode, request.getCategoryCode());
		}
		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<DeviceUserBindEntity>> selectDeviceUserBindsByRequest(DeviceUserBindRequest request){
		ReactiveQuery<DeviceUserBindEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceUserBindRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceUserBindRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceUserBindRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceUserBindRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(request.getUserId() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getUserId, request.getUserId());
		}
		if(request.getDeviceId() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getDeviceId, request.getDeviceId());
		}
		if(request.getDeviceType() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getDeviceType, request.getDeviceType());
		}
		if(request.getIsMain() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserBindRequest::getIsMain, request.getIsMain());
		}
		if(StringUtils.isNotEmpty(request.getBindName())){
			reactiveQuery = reactiveQuery.$like$(DeviceUserBindRequest::getBindName, request.getBindName());
		}
		if(StringUtils.isNotEmpty(request.getCountry())){
			reactiveQuery = reactiveQuery.$like$(DeviceUserBindRequest::getCountry, request.getCountry());
		}
		if(StringUtils.isNotEmpty(request.getProvince())){
			reactiveQuery = reactiveQuery.$like$(DeviceUserBindRequest::getProvince, request.getProvince());
		}
		if(StringUtils.isNotEmpty(request.getCity())){
			reactiveQuery = reactiveQuery.$like$(DeviceUserBindRequest::getCity, request.getCity());
		}
		if(StringUtils.isNotEmpty(request.getCounty())){
			reactiveQuery = reactiveQuery.$like$(DeviceUserBindRequest::getCounty, request.getCounty());
		}
		if(StringUtils.isNotEmpty(request.getVillage())){
			reactiveQuery = reactiveQuery.$like$(DeviceUserBindRequest::getVillage, request.getVillage());
		}
		if(StringUtils.isNotEmpty(request.getAddress())){
			reactiveQuery = reactiveQuery.$like$(DeviceUserBindRequest::getAddress, request.getAddress());
		}
		if(StringUtils.isNotEmpty(request.getCategoryCode())){
			reactiveQuery = reactiveQuery.$like$(DeviceUserBindRequest::getCategoryCode, request.getCategoryCode());
		}
		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<DeviceUserBindEntity> selectDeviceUserBindById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



	public Mono<Integer> updateDeviceUserBindById(DeviceUserBindEntity entity){
		ReactiveUpdate<DeviceUserBindEntity> update = createUpdate()
                .set(DeviceUserBindEntity::getModifyTime, new Date());
		if(entity.getIsDelete() != null){
			update = update.set(DeviceUserBindEntity::getIsDelete, entity.getIsDelete());
		}
		if(entity.getUserId() != null){
			update = update.set(DeviceUserBindEntity::getUserId, entity.getUserId());
		}
		if(entity.getDeviceId() != null){
			update = update.set(DeviceUserBindEntity::getDeviceId, entity.getDeviceId());
		}
		if(entity.getDeviceType() != null){
			update = update.set(DeviceUserBindEntity::getDeviceType, entity.getDeviceType());
		}
		if(entity.getIsMain() != null){
			update = update.set(DeviceUserBindEntity::getIsMain, entity.getIsMain());
		}
		if(StringUtils.isNotEmpty(entity.getBindName())){
			update = update.set(DeviceUserBindEntity::getBindName, entity.getBindName());
		}
		if(StringUtils.isNotEmpty(entity.getCountry())){
			update = update.set(DeviceUserBindEntity::getCountry, entity.getCountry());
		}
		if(StringUtils.isNotEmpty(entity.getProvince())){
			update = update.set(DeviceUserBindEntity::getProvince, entity.getProvince());
		}
		if(StringUtils.isNotEmpty(entity.getCity())){
			update = update.set(DeviceUserBindEntity::getCity, entity.getCity());
		}
		if(StringUtils.isNotEmpty(entity.getCounty())){
			update = update.set(DeviceUserBindEntity::getCounty, entity.getCounty());
		}
		if(StringUtils.isNotEmpty(entity.getVillage())){
			update = update.set(DeviceUserBindEntity::getVillage, entity.getVillage());
		}
		if(StringUtils.isNotEmpty(entity.getAddress())){
			update = update.set(DeviceUserBindEntity::getAddress, entity.getAddress());
		}
		if(StringUtils.isNotEmpty(entity.getCategoryCode())){
			update = update.set(DeviceUserBindEntity::getCategoryCode, entity.getCategoryCode());
		}
		return update.where(DeviceUserBindEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverDeviceUserBindById(DeviceUserBindEntity entity){
		ReactiveUpdate<DeviceUserBindEntity> update = createUpdate()
                .set(DeviceUserBindEntity::getModifyTime, new Date());
		update = update.set(DeviceUserBindEntity::getIsDelete, entity.getIsDelete());
		update = update.set(DeviceUserBindEntity::getUserId, entity.getUserId());
		update = update.set(DeviceUserBindEntity::getDeviceId, entity.getDeviceId());
		update = update.set(DeviceUserBindEntity::getDeviceType, entity.getDeviceType());
		update = update.set(DeviceUserBindEntity::getIsMain, entity.getIsMain());
		update = update.set(DeviceUserBindEntity::getBindName, entity.getBindName());
		update = update.set(DeviceUserBindEntity::getCountry, entity.getCountry());
		update = update.set(DeviceUserBindEntity::getProvince, entity.getProvince());
		update = update.set(DeviceUserBindEntity::getCity, entity.getCity());
		update = update.set(DeviceUserBindEntity::getCounty, entity.getCounty());
		update = update.set(DeviceUserBindEntity::getVillage, entity.getVillage());
		update = update.set(DeviceUserBindEntity::getAddress, entity.getAddress());
		update = update.set(DeviceUserBindEntity::getCategoryCode, entity.getCategoryCode());
		return update.where(DeviceUserBindEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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

	/***
	 * 设置同类型设备为非主设备
	 * @param userId
	 * @param code
	 */
    public Mono<Integer> setNoMain(Long userId, Integer code) {
		return createUpdate()
				.set("is_main", 0)
				.set("modify_time", new Date())
				.where("user_id", userId).and("is_main", 1).and("is_delete", code)
				.execute();
    }

}
