|
|
@@ -0,0 +1,259 @@
|
|
|
+package com.fs.admin.controller.company;
|
|
|
+
|
|
|
+
|
|
|
+import com.fs.common.BeanCopyUtils;
|
|
|
+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.page.TableDataInfo;
|
|
|
+import com.fs.common.enums.BusinessType;
|
|
|
+import com.fs.framework.datasource.TenantDataSourceContextHelper;
|
|
|
+import com.fs.his.domain.SysRedpacketConfigMore;
|
|
|
+import com.fs.his.param.UpdateChangeMchIdParam;
|
|
|
+import com.fs.his.service.ISysRedpacketConfigMoreService;
|
|
|
+import com.fs.tenant.domain.TenantInfo;
|
|
|
+import com.fs.tenant.service.TenantInfoService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+
|
|
|
+/**
|
|
|
+ * 多商户配置Controller
|
|
|
+ *
|
|
|
+ * @author fs
|
|
|
+ * @date 2025-11-27
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/redPacket/more")
|
|
|
+public class SysRedpacketConfigMoreController extends BaseController
|
|
|
+{
|
|
|
+ @Autowired
|
|
|
+ private ISysRedpacketConfigMoreService sysRedpacketConfigMoreService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TenantInfoService tenantInfoService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TenantDataSourceContextHelper tenantContextHelper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询多商户配置列表
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('redPacket:more:list')")
|
|
|
+ @GetMapping("/list")
|
|
|
+ public TableDataInfo list(SysRedpacketConfigMore sysRedpacketConfigMore)
|
|
|
+ {
|
|
|
+ startPage();
|
|
|
+ List<SysRedpacketConfigMore> list = sysRedpacketConfigMoreService.selectSysRedpacketConfigMoreList(sysRedpacketConfigMore);
|
|
|
+ return getDataTable(list);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取多商户配置详细信息
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('redPacket:more:query')")
|
|
|
+ @GetMapping(value = "/{id}")
|
|
|
+ public AjaxResult getInfo(@PathVariable("id") Long id)
|
|
|
+ {
|
|
|
+ return AjaxResult.success(sysRedpacketConfigMoreService.selectSysRedpacketConfigMoreById(id));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前发红包的商户号
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('redPacket:more:editConfig')")
|
|
|
+ @GetMapping(value = "/getRedPacketConfig")
|
|
|
+ public AjaxResult getRedPacketConFig()
|
|
|
+ {
|
|
|
+ return AjaxResult.success(sysRedpacketConfigMoreService.getRedPacketConFig());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增多商户配置
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('redPacket:more:add')")
|
|
|
+ @Log(title = "多商户配置", businessType = BusinessType.INSERT)
|
|
|
+ @PostMapping
|
|
|
+ public AjaxResult add(@RequestBody SysRedpacketConfigMore sysRedpacketConfigMore)
|
|
|
+ {
|
|
|
+ int result = sysRedpacketConfigMoreService.insertSysRedpacketConfigMore(sysRedpacketConfigMore);
|
|
|
+
|
|
|
+ // 如果指定了租户,同步到租户库
|
|
|
+ if (result > 0 && sysRedpacketConfigMore.getTenantId() != null) {
|
|
|
+ try {
|
|
|
+ syncToTenant(sysRedpacketConfigMore, sysRedpacketConfigMore.getTenantId());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("同步商户配置到租户库失败, tenantId={}, mchId={}",
|
|
|
+ sysRedpacketConfigMore.getTenantId(), sysRedpacketConfigMore.getMchId(), e);
|
|
|
+ // 不影响主流程,记录日志即可
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return toAjax(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改发商户号的商户
|
|
|
+ */
|
|
|
+ @Log(title = "修改发商户号的商户", businessType = BusinessType.UPDATE)
|
|
|
+ @PostMapping("/updateChangeMchId")
|
|
|
+ public R updateChangeMchId(@RequestBody UpdateChangeMchIdParam param)
|
|
|
+ {
|
|
|
+ return sysRedpacketConfigMoreService.updateChangeMchId(param);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清空当前红包商户号配置
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('redPacket:more:editConfig')")
|
|
|
+ @Log(title = "清空红包商户号配置", businessType = BusinessType.UPDATE)
|
|
|
+ @PostMapping("/clearRedPacketMchId")
|
|
|
+ public R clearRedPacketMchId()
|
|
|
+ {
|
|
|
+ return sysRedpacketConfigMoreService.clearRedPacketMchId();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改多商户配置
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('redPacket:more:edit')")
|
|
|
+ @Log(title = "多商户配置", businessType = BusinessType.UPDATE)
|
|
|
+ @PutMapping
|
|
|
+ public AjaxResult edit(@RequestBody SysRedpacketConfigMore sysRedpacketConfigMore)
|
|
|
+ {
|
|
|
+ // 查询原数据,获取原租户ID
|
|
|
+ SysRedpacketConfigMore oldConfig = sysRedpacketConfigMoreService.selectSysRedpacketConfigMoreById(sysRedpacketConfigMore.getId());
|
|
|
+ Long oldTenantId = oldConfig != null ? oldConfig.getTenantId() : null;
|
|
|
+ Long newTenantId = sysRedpacketConfigMore.getTenantId();
|
|
|
+ String mchId = sysRedpacketConfigMore.getMchId();
|
|
|
+
|
|
|
+ int result = sysRedpacketConfigMoreService.updateSysRedpacketConfigMore(sysRedpacketConfigMore);
|
|
|
+
|
|
|
+ if (result > 0) {
|
|
|
+ try {
|
|
|
+ // 如果原租户存在且与新租户不同,从原租户库删除
|
|
|
+ if (oldTenantId != null && !oldTenantId.equals(newTenantId)) {
|
|
|
+ deleteFromTenant(mchId, oldTenantId);
|
|
|
+ }
|
|
|
+ // 如果指定了新租户,同步到新租户库
|
|
|
+ if (newTenantId != null) {
|
|
|
+ syncToTenant(sysRedpacketConfigMore, newTenantId);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("同步商户配置到租户库失败, mchId={}, oldTenantId={}, newTenantId={}",
|
|
|
+ mchId, oldTenantId, newTenantId, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return toAjax(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除多商户配置
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('redPacket:more:remove')")
|
|
|
+ @Log(title = "多商户配置", businessType = BusinessType.DELETE)
|
|
|
+ @DeleteMapping("/{ids}")
|
|
|
+ public AjaxResult remove(@PathVariable Long[] ids)
|
|
|
+ {
|
|
|
+ // 先查询要删除的数据,获取租户信息
|
|
|
+ for (Long id : ids) {
|
|
|
+ SysRedpacketConfigMore config = sysRedpacketConfigMoreService.selectSysRedpacketConfigMoreById(id);
|
|
|
+ if (config != null && config.getTenantId() != null) {
|
|
|
+ try {
|
|
|
+ deleteFromTenant(config.getMchId(), config.getTenantId());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("从租户库删除商户配置失败, tenantId={}, mchId={}",
|
|
|
+ config.getTenantId(), config.getMchId(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return toAjax(sysRedpacketConfigMoreService.deleteSysRedpacketConfigMoreByIds(ids));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取租户列表(用于下拉选择)
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('redPacket:more:list')")
|
|
|
+ @GetMapping("/tenantList")
|
|
|
+ public AjaxResult tenantList()
|
|
|
+ {
|
|
|
+ TenantInfo query = new TenantInfo();
|
|
|
+ query.setStatus(1); // 只查询启用的租户
|
|
|
+ List<TenantInfo> list = tenantInfoService.selectTenantInfoList(query);
|
|
|
+ return AjaxResult.success(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步商户配置到租户库
|
|
|
+ */
|
|
|
+ private void syncToTenant(SysRedpacketConfigMore config, Long tenantId) {
|
|
|
+ if (tenantId == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ tenantContextHelper.runInTenant(tenantId, () -> {
|
|
|
+ try {
|
|
|
+ // 查询租户库是否已存在该商户号的配置
|
|
|
+ SysRedpacketConfigMore existConfig = sysRedpacketConfigMoreService.getOne(
|
|
|
+ new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<SysRedpacketConfigMore>()
|
|
|
+ .eq("mch_id", config.getMchId())
|
|
|
+ );
|
|
|
+
|
|
|
+ // 复制一份数据用于租户库
|
|
|
+ SysRedpacketConfigMore tenantConfig = new SysRedpacketConfigMore();
|
|
|
+ BeanCopyUtils.copy(config, tenantConfig);
|
|
|
+
|
|
|
+ if (existConfig != null) {
|
|
|
+ // 更新现有记录
|
|
|
+ tenantConfig.setId(existConfig.getId());
|
|
|
+ sysRedpacketConfigMoreService.updateSysRedpacketConfigMore(tenantConfig);
|
|
|
+ log.info("同步商户配置到租户库-更新, tenantId={}, mchId={}", tenantId, config.getMchId());
|
|
|
+ } else {
|
|
|
+ // 插入新记录,清空ID让数据库自增
|
|
|
+ tenantConfig.setId(null);
|
|
|
+ sysRedpacketConfigMoreService.insertSysRedpacketConfigMore(tenantConfig);
|
|
|
+ log.info("同步商户配置到租户库-新增, tenantId={}, mchId={}", tenantId, config.getMchId());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("同步商户配置到租户库异常, tenantId={}, mchId={}", tenantId, config.getMchId(), e);
|
|
|
+ throw new RuntimeException("同步到租户库失败: " + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从租户库删除商户配置
|
|
|
+ */
|
|
|
+ private void deleteFromTenant(String mchId, Long tenantId) {
|
|
|
+ if (tenantId == null || mchId == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ tenantContextHelper.runInTenant(tenantId, () -> {
|
|
|
+ try {
|
|
|
+ // 查询租户库中的记录
|
|
|
+ SysRedpacketConfigMore config = sysRedpacketConfigMoreService.getOne(
|
|
|
+ new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<SysRedpacketConfigMore>()
|
|
|
+ .eq("mch_id", mchId)
|
|
|
+ );
|
|
|
+ if (config != null) {
|
|
|
+ sysRedpacketConfigMoreService.deleteSysRedpacketConfigMoreById(config.getId());
|
|
|
+ log.info("从租户库删除商户配置, tenantId={}, mchId={}", tenantId, mchId);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("从租户库删除商户配置异常, tenantId={}, mchId={}", tenantId, mchId, e);
|
|
|
+ throw new RuntimeException("从租户库删除失败: " + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|