315 lines
11 KiB
Java
315 lines
11 KiB
Java
package com.gxwebsoft.common.system.controller;
|
|
|
|
import cn.hutool.core.lang.Validator;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.gxwebsoft.common.core.annotation.OperationLog;
|
|
import com.gxwebsoft.common.core.exception.BusinessException;
|
|
import com.gxwebsoft.common.core.utils.CommonUtil;
|
|
import com.gxwebsoft.common.core.utils.RedisUtil;
|
|
import com.gxwebsoft.common.core.web.*;
|
|
import com.gxwebsoft.common.system.entity.Company;
|
|
import com.gxwebsoft.common.system.entity.Tenant;
|
|
import com.gxwebsoft.common.system.entity.User;
|
|
import com.gxwebsoft.common.system.entity.UserCollection;
|
|
import com.gxwebsoft.common.system.mapper.CompanyMapper;
|
|
import com.gxwebsoft.common.system.mapper.TenantMapper;
|
|
import com.gxwebsoft.common.system.param.CompanyParam;
|
|
import com.gxwebsoft.common.system.service.CompanyService;
|
|
import com.gxwebsoft.common.system.service.TenantService;
|
|
import com.gxwebsoft.common.system.service.UserCollectionService;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import org.apache.ibatis.annotations.Param;
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
import org.springframework.transaction.annotation.Isolation;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.util.CollectionUtils;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.stream.Collectors;
|
|
|
|
/**
|
|
* 企业信息控制器
|
|
*
|
|
* @author 科技小王子
|
|
* @since 2023-05-27 14:57:34
|
|
*/
|
|
@Api(tags = "企业信息管理")
|
|
@RestController
|
|
@RequestMapping("/api/system/company")
|
|
public class CompanyController extends BaseController {
|
|
@Resource
|
|
private CompanyService companyService;
|
|
@Resource
|
|
private TenantService tenantService;
|
|
@Resource
|
|
private CompanyMapper companyMapper;
|
|
@Resource
|
|
private TenantMapper tenantMapper;
|
|
@Resource
|
|
private UserCollectionService userCollectionService;
|
|
@Resource
|
|
private RedisUtil redisUtil;
|
|
|
|
|
|
@ApiOperation("分页查询企业信息不限租户")
|
|
@GetMapping("/pageAll")
|
|
public ApiResult<PageResult<Company>> pageAll(CompanyParam param) {
|
|
|
|
final User loginUser = getLoginUser();
|
|
if(loginUser != null){
|
|
// 我的收藏
|
|
final List<UserCollection> myFocus = userCollectionService.list(new LambdaQueryWrapper<UserCollection>().eq(UserCollection::getUserId, getLoginUserId()));
|
|
if (!CollectionUtils.isEmpty(myFocus)) {
|
|
final Set<Integer> collect = myFocus.stream().map(UserCollection::getTid).collect(Collectors.toSet());
|
|
if (param.getVersion() != null) {
|
|
// 我的收藏
|
|
if (param.getVersion().equals(99)) {
|
|
param.setVersion(null);
|
|
param.setCompanyIds(collect);
|
|
}
|
|
}
|
|
final PageResult<Company> result = companyService.pageRelAll(param);
|
|
// System.out.println("collect = " + collect);
|
|
result.getList().forEach(d -> {
|
|
d.setCollection(collect.contains(d.getCompanyId()));
|
|
});
|
|
return success(result);
|
|
}
|
|
}
|
|
// 使用关联查询
|
|
return success(companyService.pageRelAll(param));
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('sys:company:list')")
|
|
@OperationLog
|
|
@ApiOperation("分页查询企业信息")
|
|
@GetMapping("/page")
|
|
public ApiResult<PageResult<Company>> page(CompanyParam param) {
|
|
// 使用关联查询
|
|
return success(companyService.pageRel(param));
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('sys:company:list')")
|
|
@OperationLog
|
|
@ApiOperation("查询全部企业信息")
|
|
@GetMapping()
|
|
public ApiResult<List<Company>> list(CompanyParam param) {
|
|
// 使用关联查询
|
|
return success(companyService.listRel(param));
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('sys:company:list')")
|
|
@OperationLog
|
|
@ApiOperation("根据id查询企业信息")
|
|
@GetMapping("/{id}")
|
|
public ApiResult<Company> get(@PathVariable("id") Integer id) {
|
|
// 使用关联查询
|
|
return success(companyService.getByIdRel(id));
|
|
}
|
|
|
|
@Transactional(rollbackFor = {Exception.class}, isolation = Isolation.SERIALIZABLE)
|
|
@PreAuthorize("hasAuthority('sys:company:save')")
|
|
@ApiOperation("添加企业信息")
|
|
@PostMapping()
|
|
public ApiResult<?> save(@RequestBody Company company) {
|
|
Tenant tenant = new Tenant();
|
|
// 记录当前登录用户id
|
|
User loginUser = getLoginUser();
|
|
if (loginUser != null) {
|
|
company.setUserId(loginUser.getUserId());
|
|
tenant.setUserId(loginUser.getUserId());
|
|
}
|
|
tenant.setTenantName(company.getShortName());
|
|
tenant.setTenantCode(CommonUtil.randomUUID16());
|
|
tenant.setComments(company.getComments());
|
|
tenantService.save(tenant);
|
|
company.setTenantId(tenant.getTenantId());
|
|
company.setTid(tenant.getTenantId());
|
|
company.setAuthoritative(true);
|
|
// 添加租户并初始化
|
|
final Company result = tenantService.initialization(company);
|
|
if (result != null) {
|
|
return success("添加成功",result);
|
|
}
|
|
return fail("添加失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('sys:company:update')")
|
|
@OperationLog
|
|
@ApiOperation("修改企业信息")
|
|
@PutMapping()
|
|
public ApiResult<?> update(@RequestBody Company company) {
|
|
if (companyService.updateById(company)) {
|
|
// 清除缓存
|
|
redisUtil.delete("TenantInfo:".concat(company.getTenantId().toString()));
|
|
return success("修改成功");
|
|
}
|
|
return fail("修改失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('sys:company:remove')")
|
|
@OperationLog
|
|
@ApiOperation("删除企业信息")
|
|
@DeleteMapping("/{id}")
|
|
public ApiResult<?> remove(@PathVariable("id") Integer id) {
|
|
if (companyService.removeById(id)) {
|
|
return success("删除成功");
|
|
}
|
|
return fail("删除失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('sys:company:save')")
|
|
@OperationLog
|
|
@ApiOperation("批量添加企业信息")
|
|
@PostMapping("/batch")
|
|
public ApiResult<?> saveBatch(@RequestBody List<Company> list) {
|
|
if (companyService.saveBatch(list)) {
|
|
return success("添加成功");
|
|
}
|
|
return fail("添加失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('sys:company:update')")
|
|
@OperationLog
|
|
@ApiOperation("批量修改企业信息")
|
|
@PutMapping("/batch")
|
|
public ApiResult<?> removeBatch(@RequestBody BatchParam<Company> batchParam) {
|
|
if (batchParam.update(companyService, "company_id")) {
|
|
return success("修改成功");
|
|
}
|
|
return fail("修改失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('sys:company:remove')")
|
|
@OperationLog
|
|
@ApiOperation("批量删除企业信息")
|
|
@DeleteMapping("/batch")
|
|
public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
|
|
if (companyService.removeByIds(ids)) {
|
|
return success("删除成功");
|
|
}
|
|
return fail("删除失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('sys:company:profile')")
|
|
@OperationLog
|
|
@ApiOperation("根据id查询企业信息")
|
|
@GetMapping("/profile")
|
|
public ApiResult<Company> profile() {
|
|
final CompanyParam param = new CompanyParam();
|
|
param.setTenantId(getTenantId());
|
|
param.setAuthoritative(true);
|
|
final List<Company> list = companyService.listRel(param);
|
|
if(!CollectionUtils.isEmpty(list)){
|
|
return success(list.get(0));
|
|
}
|
|
return fail("企业不存在",null);
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('sys:company:profile')")
|
|
@OperationLog
|
|
@ApiOperation("根据id查询企业信息不限租户")
|
|
@GetMapping("/profileAll/{companyId}")
|
|
public ApiResult<Company> profileAll(@PathVariable("companyId") Integer companyId) {
|
|
return success(companyMapper.getCompanyAll(companyId));
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('sys:company:remove')")
|
|
@OperationLog
|
|
@ApiOperation("销毁租户")
|
|
@DeleteMapping("/destruction/{id}")
|
|
public ApiResult<?> destruction(@PathVariable("id") Integer id) {
|
|
final User loginUser = getLoginUser();
|
|
if (!loginUser.getUsername().equals("admin")) {
|
|
throw new BusinessException("只有超级管理员才能操作");
|
|
}
|
|
final Integer tenantId = getTenantId();
|
|
if (tenantService.removeById(tenantId)) {
|
|
return success("删除成功",tenantId);
|
|
}
|
|
return fail("删除失败");
|
|
}
|
|
@ApiOperation("检查企业是否存在")
|
|
@GetMapping("/existence")
|
|
public ApiResult<?> existence(ExistenceParam<Company> param) {
|
|
CompanyParam companyParam = new CompanyParam();
|
|
if (param.getField().equals("shortName")) {
|
|
companyParam.setAppName(param.getValue());
|
|
List<Company> count = companyMapper.getCount(companyParam);
|
|
if (!CollectionUtils.isEmpty(count)) {
|
|
return success(param.getValue() + "已存在");
|
|
}
|
|
}
|
|
if (param.getField().equals("email")) {
|
|
companyParam.setEmail(param.getValue());
|
|
List<Company> count = companyMapper.getCount(companyParam);
|
|
if (!CollectionUtils.isEmpty(count)) {
|
|
return success(param.getValue() + "已存在");
|
|
}
|
|
}
|
|
if (param.getField().equals("phone")) {
|
|
companyParam.setPhone(param.getValue());
|
|
List<Company> count = companyMapper.getCount(companyParam);
|
|
if (!CollectionUtils.isEmpty(count)) {
|
|
return success(param.getValue() + "已存在");
|
|
}
|
|
}
|
|
return fail(param.getValue() + "不存在");
|
|
}
|
|
|
|
@ApiOperation("根据id查询企业信息不限租户不带token")
|
|
@GetMapping("/companyInfoAll/{companyId}")
|
|
public ApiResult<Company> companyInfoAll(@PathVariable("companyId") Integer companyId) {
|
|
return success(companyMapper.getCompanyAll(companyId));
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('sys:company:updateAll')")
|
|
@OperationLog
|
|
@ApiOperation("修改企业信息")
|
|
@PutMapping("/updateCompanyAll")
|
|
public ApiResult<?> updateCompanyAll(@RequestBody Company company) {
|
|
if (companyMapper.updateByIdAll(company)) {
|
|
return success("修改成功");
|
|
}
|
|
return fail("修改失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('sys:company:removeAll')")
|
|
@OperationLog
|
|
@ApiOperation("删除企业信息")
|
|
@DeleteMapping("/removeAll/{id}")
|
|
public ApiResult<?> removeAll(@PathVariable("id") Integer id) {
|
|
if (companyMapper.removeCompanyAll(id)) {
|
|
return success("删除成功");
|
|
}
|
|
return fail("删除失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('sys:company:removeAll')")
|
|
@OperationLog
|
|
@ApiOperation("恢复租户")
|
|
@DeleteMapping("/undeleteAll/{id}")
|
|
public ApiResult<?> undeleteAll(@PathVariable("id") Integer id) {
|
|
if (companyMapper.undeleteAll(id)) {
|
|
return success("恢复成功");
|
|
}
|
|
return fail("恢复失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('sys:company:destructionAll')")
|
|
@OperationLog
|
|
@ApiOperation("销毁租户")
|
|
@DeleteMapping("/destructionAll/{id}")
|
|
public ApiResult<?> destructionAll(@PathVariable("id") Integer id) {
|
|
if (tenantMapper.destructionAll(id)) {
|
|
return success("销毁成功");
|
|
}
|
|
return fail("销毁失败");
|
|
}
|
|
}
|