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;
/**
*
* 集团管理 前端控制器
*
*
* @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> 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.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("集团名称已存在");
}
TGroup update = new TGroup();
update.setId(group.getId());
update.setGroupName(groupName);
groupService.updateById(update);
return AjaxResult.success();
}
/**
* 删除集团(集团下存在企业时禁止删除)
*/
@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();
}
}