/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.system.expand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.entity.Tenant;
import org.springblade.system.expand.entity.TenantType;
import org.springblade.system.expand.mapper.TenantTypeMapper;
import org.springblade.system.expand.service.ITenantTypeService;
import org.springblade.system.expand.vo.TenantTypeVO;
import org.springblade.system.expand.vo.TenantVO;
import org.springblade.system.feign.ISysClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-03-29
 */
@Service
@AllArgsConstructor
public class TenantTypeServiceImpl extends BaseServiceImpl<TenantTypeMapper, TenantType> implements ITenantTypeService {

	private ISysClient sysClient;

	@Override
	public IPage<TenantTypeVO> selectTenantTypePage(IPage<TenantTypeVO> page, TenantTypeVO tenantType) {
		return page.setRecords(baseMapper.selectTenantTypePage(page, tenantType));
	}

	@Override
	@Transactional
	public boolean setTenantTypeAndSaveTenant(TenantVO tenantVO) {
		CacheUtil.clear("blade:sys");
		Tenant tenant = new Tenant();
		BeanUtil.copy(tenantVO,tenant);

		R<Tenant> tenantR;
		if (tenant.getId() == null){
			tenantR = sysClient.saveTenant(tenant);
		}else{
			tenantR = sysClient.updateTenant(tenant);
		}

		if (tenantR != null && tenantR.getData() != null){
			TenantType tenantType = new TenantType();
			tenantType.setTenantId(tenantR.getData().getTenantId());
			tenantType.setSystemName(tenantVO.getSystemName());
			tenantType.setPort(tenantVO.getPort());

			TenantType one = this.getOne(Condition.getQueryWrapper(tenantType));
			if (one != null) {
				one.setTenantType(tenantVO.getTenantType());
				return this.saveOrUpdate(one);
			} else {
				tenantType.setTenantType(tenantVO.getTenantType());
				return this.saveOrUpdate(tenantType);
			}
		}
		return false;
	}

	@Override
	@Transactional
	public boolean logicDelectRelate(String ids) {
		CacheUtil.clear("blade:sys");
		List<Long> longs = Func.toLongList(ids);
		List<Long> typeIds = new ArrayList<>();

		longs.forEach(item -> {
			TenantType tenantType = new TenantType();
			Tenant tenant = sysClient.getTenant(item).getData();
			tenantType.setTenantId(tenant.getTenantId());
			TenantType one = this.getOne(Condition.getQueryWrapper(tenantType));
			if (one != null) {
				typeIds.add(one.getId());
			}
		});
		if (typeIds.size() > 0){
			return sysClient.removeTenant(ids).getData() && this.deleteLogic(typeIds);
		}else{
			return sysClient.removeTenant(ids).getData();
		}
	}

	@Override
	public TenantType getByTenantId(String tenantId) {
		QueryWrapper<TenantType> queryWrapper = new QueryWrapper<>();
		LambdaQueryWrapper<TenantType> lambdaQueryWrapper = queryWrapper.lambda().eq(TenantType::getTenantId, tenantId);
		return baseMapper.selectOne(lambdaQueryWrapper);
	}

}
