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


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


	public Mono<DeviceProductFactoryInfoEntity> selectDeviceProductFactoryInfoByRequest(DeviceProductFactoryInfoRequest request){
		ReactiveQuery<DeviceProductFactoryInfoEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getModifyTime, request.getModifyTime());
		}
		if(StringUtils.isNotEmpty(request.getName())){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getName, request.getName());
		}
		if(StringUtils.isNotEmpty(request.getLegalPerson())){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getLegalPerson, request.getLegalPerson());
		}
		if(StringUtils.isNotEmpty(request.getLegalPersonPhone())){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getLegalPersonPhone, request.getLegalPersonPhone());
		}
		if(StringUtils.isNotEmpty(request.getContacts())){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getContacts, request.getContacts());
		}
		if(StringUtils.isNotEmpty(request.getContactsPhone())){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getContactsPhone, request.getContactsPhone());
		}
		if(StringUtils.isNotEmpty(request.getCountry())){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getCountry, request.getCountry());
		}
		if(StringUtils.isNotEmpty(request.getProvince())){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getProvince, request.getProvince());
		}
		if(StringUtils.isNotEmpty(request.getCity())){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getCity, request.getCity());
		}
		if(StringUtils.isNotEmpty(request.getCounty())){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getCounty, request.getCounty());
		}
		if(StringUtils.isNotEmpty(request.getVillage())){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getVillage, request.getVillage());
		}
		if(StringUtils.isNotEmpty(request.getAddress())){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getAddress, request.getAddress());
		}
		if(StringUtils.isNotEmpty(request.getFactoryCode())){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getFactoryCode, request.getFactoryCode());
		}
		if(request.getFactoryGrade() != null){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getFactoryGrade, request.getFactoryGrade());
		}
		if(StringUtils.isNotEmpty(request.getRemark())){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::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<DeviceProductFactoryInfoEntity>> selectDeviceProductFactoryInfosByRequest(DeviceProductFactoryInfoRequest request){
		ReactiveQuery<DeviceProductFactoryInfoEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceProductFactoryInfoRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceProductFactoryInfoRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceProductFactoryInfoRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceProductFactoryInfoRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(StringUtils.isNotEmpty(request.getName())){
			reactiveQuery = reactiveQuery.$like$(DeviceProductFactoryInfoRequest::getName, request.getName());
		}
		if(StringUtils.isNotEmpty(request.getLegalPerson())){
			reactiveQuery = reactiveQuery.$like$(DeviceProductFactoryInfoRequest::getLegalPerson, request.getLegalPerson());
		}
		if(StringUtils.isNotEmpty(request.getLegalPersonPhone())){
			reactiveQuery = reactiveQuery.$like$(DeviceProductFactoryInfoRequest::getLegalPersonPhone, request.getLegalPersonPhone());
		}
		if(StringUtils.isNotEmpty(request.getContacts())){
			reactiveQuery = reactiveQuery.$like$(DeviceProductFactoryInfoRequest::getContacts, request.getContacts());
		}
		if(StringUtils.isNotEmpty(request.getContactsPhone())){
			reactiveQuery = reactiveQuery.$like$(DeviceProductFactoryInfoRequest::getContactsPhone, request.getContactsPhone());
		}
		if(StringUtils.isNotEmpty(request.getCountry())){
			reactiveQuery = reactiveQuery.$like$(DeviceProductFactoryInfoRequest::getCountry, request.getCountry());
		}
		if(StringUtils.isNotEmpty(request.getProvince())){
			reactiveQuery = reactiveQuery.$like$(DeviceProductFactoryInfoRequest::getProvince, request.getProvince());
		}
		if(StringUtils.isNotEmpty(request.getCity())){
			reactiveQuery = reactiveQuery.$like$(DeviceProductFactoryInfoRequest::getCity, request.getCity());
		}
		if(StringUtils.isNotEmpty(request.getCounty())){
			reactiveQuery = reactiveQuery.$like$(DeviceProductFactoryInfoRequest::getCounty, request.getCounty());
		}
		if(StringUtils.isNotEmpty(request.getVillage())){
			reactiveQuery = reactiveQuery.$like$(DeviceProductFactoryInfoRequest::getVillage, request.getVillage());
		}
		if(StringUtils.isNotEmpty(request.getAddress())){
			reactiveQuery = reactiveQuery.$like$(DeviceProductFactoryInfoRequest::getAddress, request.getAddress());
		}
		if(StringUtils.isNotEmpty(request.getFactoryCode())){
			reactiveQuery = reactiveQuery.$like$(DeviceProductFactoryInfoRequest::getFactoryCode, request.getFactoryCode());
		}
		if(request.getFactoryGrade() != null){
			reactiveQuery = reactiveQuery.and(DeviceProductFactoryInfoRequest::getFactoryGrade, request.getFactoryGrade());
		}
		if(StringUtils.isNotEmpty(request.getRemark())){
			reactiveQuery = reactiveQuery.$like$(DeviceProductFactoryInfoRequest::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<DeviceProductFactoryInfoEntity> selectDeviceProductFactoryInfoById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



	public Mono<Integer> updateDeviceProductFactoryInfoById(DeviceProductFactoryInfoEntity entity){
		ReactiveUpdate<DeviceProductFactoryInfoEntity> update = createUpdate()
                .set(DeviceProductFactoryInfoEntity::getModifyTime, new Date());
		if(entity.getIsDelete() != null){
			update = update.set(DeviceProductFactoryInfoEntity::getIsDelete, entity.getIsDelete());
		}
		if(StringUtils.isNotEmpty(entity.getName())){
			update = update.set(DeviceProductFactoryInfoEntity::getName, entity.getName());
		}
		if(StringUtils.isNotEmpty(entity.getLegalPerson())){
			update = update.set(DeviceProductFactoryInfoEntity::getLegalPerson, entity.getLegalPerson());
		}
		if(StringUtils.isNotEmpty(entity.getLegalPersonPhone())){
			update = update.set(DeviceProductFactoryInfoEntity::getLegalPersonPhone, entity.getLegalPersonPhone());
		}
		if(StringUtils.isNotEmpty(entity.getContacts())){
			update = update.set(DeviceProductFactoryInfoEntity::getContacts, entity.getContacts());
		}
		if(StringUtils.isNotEmpty(entity.getContactsPhone())){
			update = update.set(DeviceProductFactoryInfoEntity::getContactsPhone, entity.getContactsPhone());
		}
		if(StringUtils.isNotEmpty(entity.getCountry())){
			update = update.set(DeviceProductFactoryInfoEntity::getCountry, entity.getCountry());
		}
		if(StringUtils.isNotEmpty(entity.getProvince())){
			update = update.set(DeviceProductFactoryInfoEntity::getProvince, entity.getProvince());
		}
		if(StringUtils.isNotEmpty(entity.getCity())){
			update = update.set(DeviceProductFactoryInfoEntity::getCity, entity.getCity());
		}
		if(StringUtils.isNotEmpty(entity.getCounty())){
			update = update.set(DeviceProductFactoryInfoEntity::getCounty, entity.getCounty());
		}
		if(StringUtils.isNotEmpty(entity.getVillage())){
			update = update.set(DeviceProductFactoryInfoEntity::getVillage, entity.getVillage());
		}
		if(StringUtils.isNotEmpty(entity.getAddress())){
			update = update.set(DeviceProductFactoryInfoEntity::getAddress, entity.getAddress());
		}
		if(StringUtils.isNotEmpty(entity.getFactoryCode())){
			update = update.set(DeviceProductFactoryInfoEntity::getFactoryCode, entity.getFactoryCode());
		}
		if(entity.getFactoryGrade() != null){
			update = update.set(DeviceProductFactoryInfoEntity::getFactoryGrade, entity.getFactoryGrade());
		}
		if(StringUtils.isNotEmpty(entity.getRemark())){
			update = update.set(DeviceProductFactoryInfoEntity::getRemark, entity.getRemark());
		}
		return update.where(DeviceProductFactoryInfoEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverDeviceProductFactoryInfoById(DeviceProductFactoryInfoEntity entity){
		ReactiveUpdate<DeviceProductFactoryInfoEntity> update = createUpdate()
                .set(DeviceProductFactoryInfoEntity::getModifyTime, new Date());
		update = update.set(DeviceProductFactoryInfoEntity::getIsDelete, entity.getIsDelete());
		update = update.set(DeviceProductFactoryInfoEntity::getName, entity.getName());
		update = update.set(DeviceProductFactoryInfoEntity::getLegalPerson, entity.getLegalPerson());
		update = update.set(DeviceProductFactoryInfoEntity::getLegalPersonPhone, entity.getLegalPersonPhone());
		update = update.set(DeviceProductFactoryInfoEntity::getContacts, entity.getContacts());
		update = update.set(DeviceProductFactoryInfoEntity::getContactsPhone, entity.getContactsPhone());
		update = update.set(DeviceProductFactoryInfoEntity::getCountry, entity.getCountry());
		update = update.set(DeviceProductFactoryInfoEntity::getProvince, entity.getProvince());
		update = update.set(DeviceProductFactoryInfoEntity::getCity, entity.getCity());
		update = update.set(DeviceProductFactoryInfoEntity::getCounty, entity.getCounty());
		update = update.set(DeviceProductFactoryInfoEntity::getVillage, entity.getVillage());
		update = update.set(DeviceProductFactoryInfoEntity::getAddress, entity.getAddress());
		update = update.set(DeviceProductFactoryInfoEntity::getFactoryCode, entity.getFactoryCode());
		update = update.set(DeviceProductFactoryInfoEntity::getFactoryGrade, entity.getFactoryGrade());
		update = update.set(DeviceProductFactoryInfoEntity::getRemark, entity.getRemark());
		return update.where(DeviceProductFactoryInfoEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
