puzhibing
2026-09-30 ab268d1525563fc8f22af808a58bc13f125f2796
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.ruoyi.other.controller;
 
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.Logical;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.other.api.domain.TGroup;
import com.ruoyi.other.api.domain.TGroupCompany;
import com.ruoyi.other.service.TGroupCompanyService;
import com.ruoyi.other.service.TGroupService;
import com.ruoyi.other.vo.GroupTreeVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.util.List;
 
/**
 * <p>
 * 集团管理 前端控制器
 * </p>
 *
 * @author mxcd
 * @since 2026-09-22
 */
@RestController
@RequestMapping("/group")
public class TGroupController {
 
    @Resource
    private TGroupService groupService;
 
    @Resource
    private TGroupCompanyService groupCompanyService;
 
    /**
     * 获取组织树(第一层集团,第二层企业)
     */
    @RequiresPermissions(value = {"/companyManagement/list"}, logical = Logical.OR)
    @GetMapping("/tree")
    @ApiOperation(value = "获取组织树", tags = {"管理后台-公司管理"})
    public AjaxResult<List<GroupTreeVo>> tree() {
        return AjaxResult.success(groupService.groupTree());
    }
 
    /**
     * 添加集团
     */
    @RequiresPermissions(value = {"/companyManagement/list"}, logical = Logical.OR)
    @PostMapping("/add")
    @ApiOperation(value = "添加集团", tags = {"管理后台-公司管理"})
    @Log(title = "【公司管理】添加集团", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody TGroup group) {
        if (!StringUtils.hasText(group.getGroupName())) {
            return AjaxResult.error("集团名称不能为空");
        }
        String groupName = group.getGroupName().trim();
        long count = groupService.lambdaQuery().eq(TGroup::getGroupName, groupName).count();
        if (count > 0) {
            return AjaxResult.error("集团名称已存在");
        }
        group.setId(null);
        group.setGroupName(groupName);
        group.setContactName(trimToNull(group.getContactName()));
        group.setContactPhone(trimToNull(group.getContactPhone()));
        group.setDelFlag(false);
        groupService.save(group);
        return AjaxResult.success();
    }
 
    /**
     * 编辑集团
     */
    @RequiresPermissions(value = {"/companyManagement/list"}, logical = Logical.OR)
    @PostMapping("/edit")
    @ApiOperation(value = "编辑集团", tags = {"管理后台-公司管理"})
    @Log(title = "【公司管理】编辑集团", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody TGroup group) {
        if (group.getId() == null) {
            return AjaxResult.error("集团ID不能为空");
        }
        if (!StringUtils.hasText(group.getGroupName())) {
            return AjaxResult.error("集团名称不能为空");
        }
        String groupName = group.getGroupName().trim();
        long count = groupService.lambdaQuery()
                .eq(TGroup::getGroupName, groupName)
                .ne(TGroup::getId, group.getId())
                .count();
        if (count > 0) {
            return AjaxResult.error("集团名称已存在");
        }
        groupService.lambdaUpdate()
                .eq(TGroup::getId, group.getId())
                .set(TGroup::getGroupName, groupName)
                .set(TGroup::getContactName, trimToNull(group.getContactName()))
                .set(TGroup::getContactPhone, trimToNull(group.getContactPhone()))
                .update();
        return AjaxResult.success();
    }
 
    /**
     * 联系人/联系电话去除首尾空格,空值统一写 null(显式 set 可清空)
     */
    private String trimToNull(String value) {
        return StringUtils.hasText(value) ? value.trim() : null;
    }
 
    /**
     * 删除集团(集团下存在企业时禁止删除)
     */
    @RequiresPermissions(value = {"/companyManagement/list"}, logical = Logical.OR)
    @DeleteMapping("/del")
    @ApiOperation(value = "删除集团", tags = {"管理后台-公司管理"})
    @Log(title = "【公司管理】删除集团", businessType = BusinessType.DELETE)
    public AjaxResult del(@RequestParam("id") Integer id) {
        if (id == null) {
            return AjaxResult.error("集团ID不能为空");
        }
        long companyCount = groupCompanyService.lambdaQuery().eq(TGroupCompany::getGroupId, id).count();
        if (companyCount > 0) {
            return AjaxResult.error("该集团下存在企业,无法删除");
        }
        groupService.removeById(id);
        return AjaxResult.success();
    }
 
}