/*
 *      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.tenanttype.service.impl;

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

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

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

	private ITenantService tenantService;

	@Override
	public IPage<TenantVO> selectTenantPage(IPage<TenantVO> page, TenantVO tenantVO) {
		return page.setRecords(baseMapper.selectTenantTypePage(page, tenantVO));
	}

	@Override
	@Transactional
	public boolean setTenantTypeAndSaveTenant(TenantVO tenantVO) {
		CacheUtil.clear("blade:sys");
		if (this.tenantService.saveTenant(tenantVO)){
			TenantType tenantType = new TenantType();
			tenantType.setTenantId(tenantVO.getTenantId());

			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 = tenantService.getById(item);
			tenantType.setTenantId(tenant.getTenantId());
			TenantType one = this.getOne(Condition.getQueryWrapper(tenantType));
			if (one != null) {
				typeIds.add(one.getId());
			}
		});

		return this.tenantService.removeTenant(longs) && this.deleteLogic(typeIds);
	}

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


}
