|
|
@@ -4,25 +4,31 @@ import com.fs.common.annotation.Log;
|
|
|
import com.fs.common.core.controller.BaseController;
|
|
|
import com.fs.common.core.domain.AjaxResult;
|
|
|
import com.fs.common.core.domain.R;
|
|
|
+import com.fs.common.core.domain.entity.SysMenu;
|
|
|
+import com.fs.common.core.domain.entity.TenantCompanyMenu;
|
|
|
import com.fs.common.core.page.TableDataInfo;
|
|
|
import com.fs.common.enums.BusinessType;
|
|
|
import com.fs.common.utils.poi.ExcelUtil;
|
|
|
+import com.fs.framework.datasource.DynamicDataSourceContextHolder;
|
|
|
+import com.fs.framework.datasource.TenantDataSourceManager;
|
|
|
import com.fs.tenant.domain.TenantInfo;
|
|
|
+import com.fs.tenant.mapper.TenantInfoMapper;
|
|
|
import com.fs.tenant.service.TenantInfoService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.context.annotation.Profile;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 总后台租户管理控制器(SaaS 多租户版)
|
|
|
* 查询 tenant_info 表,不再查询 company 表
|
|
|
*/
|
|
|
+@Profile("admin")
|
|
|
@RestController
|
|
|
@RequestMapping("/admin/company")
|
|
|
public class CompanyAdminController extends BaseController {
|
|
|
@@ -30,6 +36,12 @@ public class CompanyAdminController extends BaseController {
|
|
|
@Autowired
|
|
|
private TenantInfoService tenantInfoService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private TenantInfoMapper tenantInfoMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TenantDataSourceManager tenantDataSourceManager;
|
|
|
+
|
|
|
/**
|
|
|
* 查询所有租户列表
|
|
|
*/
|
|
|
@@ -136,13 +148,138 @@ public class CompanyAdminController extends BaseController {
|
|
|
|
|
|
/**
|
|
|
* 租户充值/扣款
|
|
|
- * TODO 后续优化:需要切到租户库查询 company 表余额并操作充值/扣款
|
|
|
*/
|
|
|
@PreAuthorize("@ss.hasPermi('admin:company:edit')")
|
|
|
@Log(title = "租户管理", businessType = BusinessType.UPDATE)
|
|
|
@PostMapping("/{id}/recharge")
|
|
|
public AjaxResult rechargeCompany(@PathVariable String id, @RequestBody Map<String, Object> params) {
|
|
|
- // TODO 后续优化:切到租户库执行充值/扣款逻辑
|
|
|
- return AjaxResult.error("充值/扣款功能暂未对接多租户,后续优化");
|
|
|
+ String operateType = (String) params.get("operateType");
|
|
|
+ Object amountObj = params.get("amount");
|
|
|
+ String remark = params.get("remark") != null ? params.get("remark").toString() : "";
|
|
|
+
|
|
|
+ if (amountObj == null) {
|
|
|
+ return AjaxResult.error("金额不能为空");
|
|
|
+ }
|
|
|
+ java.math.BigDecimal amount;
|
|
|
+ try {
|
|
|
+ amount = new java.math.BigDecimal(amountObj.toString());
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ return AjaxResult.error("金额格式不正确");
|
|
|
+ }
|
|
|
+ if (amount.compareTo(java.math.BigDecimal.ZERO) <= 0) {
|
|
|
+ return AjaxResult.error("金额必须大于0");
|
|
|
+ }
|
|
|
+
|
|
|
+ java.math.BigDecimal delta = "deduct".equals(operateType) ? amount.negate() : amount;
|
|
|
+ int rows = tenantInfoService.updateBalance(Long.valueOf(id), delta);
|
|
|
+ if (rows > 0) {
|
|
|
+ return AjaxResult.success("recharge".equals(operateType) ? "充值成功" : "扣款成功");
|
|
|
+ }
|
|
|
+ return AjaxResult.error("recharge".equals(operateType) ? "充值失败,租户不存在" : "扣款失败,余额不足或租户不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 租户菜单编辑(切租户库) ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取租户管理端菜单树(合并模板菜单+租户已有菜单)
|
|
|
+ * flag=sys → 管理端菜单(tenant_sys_menu/租户库sys_menu)
|
|
|
+ * flag=com → 销售端菜单(tenant_company_menu/租户库company_menu)
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('admin:company:edit')")
|
|
|
+ @PostMapping("/{id}/menu")
|
|
|
+ public R tenantMenuChange(@PathVariable String id, @RequestBody Map<String, String> params) {
|
|
|
+ String flag = params.getOrDefault("flag", "sys");
|
|
|
+ TenantInfo tenantInfo = tenantInfoMapper.selectTenantInfoById(id);
|
|
|
+ if (tenantInfo == null) return R.error("租户不存在");
|
|
|
+ if (tenantInfo.getStatus() != null && tenantInfo.getStatus() == 2) return R.error("租户初始化中");
|
|
|
+
|
|
|
+ if ("sys".equals(flag)) {
|
|
|
+ List<SysMenu> sysMenus = tenantInfoMapper.selectMenuList(new SysMenu());
|
|
|
+ tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
+ return tenantInfoService.menuChange(flag, sysMenus, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<TenantCompanyMenu> companyMenus = tenantInfoMapper.selectCompanyMenuList(new TenantCompanyMenu());
|
|
|
+ tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
+ return tenantInfoService.menuChange(flag, null, companyMenus);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑租户菜单(勾选/取消勾选/新增)
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('admin:company:edit')")
|
|
|
+ @Log(title = "编辑租户菜单", businessType = BusinessType.UPDATE)
|
|
|
+ @PostMapping("/{id}/menu/edit")
|
|
|
+ public R tenantMenuEdit(@PathVariable String id, @RequestBody Map<String, Object> params) {
|
|
|
+ String flag = (String) params.getOrDefault("flag", "sys");
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ List<Long> selected = params.get("selected") != null ? (List<Long>) params.get("selected") : new ArrayList<>();
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ List<Long> unSelected = params.get("unSelected") != null ? (List<Long>) params.get("unSelected") : new ArrayList<>();
|
|
|
+
|
|
|
+ TenantInfo tenantInfo = tenantInfoMapper.selectTenantInfoById(id);
|
|
|
+ if (tenantInfo == null) return R.error("租户不存在");
|
|
|
+ if (tenantInfo.getStatus() != null && tenantInfo.getStatus() == 2) return R.error("租户初始化中");
|
|
|
+
|
|
|
+ if ("sys".equals(flag)) {
|
|
|
+ List<SysMenu> addSysMenu = getAddSysMenu(tenantInfo, selected);
|
|
|
+ tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
+ return tenantInfoService.menuEdit(selected, unSelected, flag, addSysMenu, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<TenantCompanyMenu> addCompanyMenu = getAddCompanyMenu(tenantInfo, selected);
|
|
|
+ tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
+ return tenantInfoService.menuEdit(selected, unSelected, flag, null, addCompanyMenu);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取租户库中需要新增的后台菜单
|
|
|
+ */
|
|
|
+ private List<SysMenu> getAddSysMenu(TenantInfo tenantInfo, List<Long> selected) {
|
|
|
+ tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
+ List<Long> existIds = tenantInfoMapper.selectExistMenuIds();
|
|
|
+ List<Long> needAddIds = selected.stream().filter(mid -> !existIds.contains(mid)).collect(Collectors.toList());
|
|
|
+ DynamicDataSourceContextHolder.setDataSourceType(com.fs.common.enums.DataSourceType.MASTER.name());
|
|
|
+ if (!needAddIds.isEmpty()) {
|
|
|
+ return tenantInfoMapper.getTenSysMenuByIds(needAddIds);
|
|
|
+ }
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取租户库中需要新增的销售菜单
|
|
|
+ */
|
|
|
+ private List<TenantCompanyMenu> getAddCompanyMenu(TenantInfo tenantInfo, List<Long> selected) {
|
|
|
+ tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
+ List<Long> existIds = tenantInfoMapper.selectExistComMenuIds();
|
|
|
+ List<Long> needAddIds = selected.stream().filter(mid -> !existIds.contains(mid)).collect(Collectors.toList());
|
|
|
+ DynamicDataSourceContextHolder.setDataSourceType(com.fs.common.enums.DataSourceType.MASTER.name());
|
|
|
+ if (!needAddIds.isEmpty()) {
|
|
|
+ return tenantInfoMapper.getTenComMenuByIds(needAddIds);
|
|
|
+ }
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重置租户管理员密码
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('admin:company:edit')")
|
|
|
+ @Log(title = "租户管理", businessType = BusinessType.UPDATE)
|
|
|
+ @PutMapping("/{id}/resetPwd")
|
|
|
+ public AjaxResult resetPwd(@PathVariable String id, @RequestBody Map<String, String> params) {
|
|
|
+ String password = params.get("password");
|
|
|
+ if (password == null || password.length() < 6) {
|
|
|
+ return AjaxResult.error("密码长度不能少于6位");
|
|
|
+ }
|
|
|
+ TenantInfo tenantInfo = tenantInfoMapper.selectTenantInfoById(id);
|
|
|
+ if (tenantInfo == null) {
|
|
|
+ return AjaxResult.error("租户不存在");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
+ return toAjax(tenantInfoService.resetTenantAdminPwd(Long.valueOf(id), password));
|
|
|
+ } finally {
|
|
|
+ tenantDataSourceManager.clear();
|
|
|
+ }
|
|
|
}
|
|
|
}
|