Commit 339e4605fa2dc3ade5a7c22c17749f277062edca

Authored by 云中非
2 parents 4a67c19c b270a3da

Merge remote-tracking branch 'origin/master' into ljl1207

  1 +package org.thingsboard.server.config.yunteng;
  2 +
  3 +import com.baomidou.mybatisplus.annotation.DbType;
  4 +import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
  5 +import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
  6 +import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
  7 +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
  8 +import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
  9 +import org.springframework.context.annotation.Bean;
  10 +import org.springframework.context.annotation.Configuration;
  11 +
  12 +import java.util.UUID;
  13 +
  14 +@Configuration
  15 +public class AppConfig {
  16 + @Bean
  17 + public MybatisPlusPropertiesCustomizer plusPropertiesCustomizer() {
  18 + return plusProperties ->
  19 + plusProperties
  20 + .getGlobalConfig()
  21 + .setIdentifierGenerator(
  22 + new IdentifierGenerator() {
  23 + @Override
  24 + public Number nextId(Object entity) {
  25 + return new DefaultIdentifierGenerator().nextId(entity);
  26 + }
  27 +
  28 + @Override
  29 + public String nextUUID(Object entity) {
  30 + return UUID.randomUUID().toString();
  31 + }
  32 + });
  33 + }
  34 +
  35 + @Bean
  36 + public MybatisPlusInterceptor mybatisPlusInterceptor() {
  37 + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  38 + interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.POSTGRE_SQL));
  39 + return interceptor;
  40 + }
  41 +}
... ...
  1 +package org.thingsboard.server.config.yunteng;
  2 +
  3 +import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
  4 +import lombok.extern.slf4j.Slf4j;
  5 +import org.apache.ibatis.reflection.MetaObject;
  6 +import org.springframework.security.core.Authentication;
  7 +import org.springframework.security.core.context.SecurityContextHolder;
  8 +import org.springframework.stereotype.Component;
  9 +import org.thingsboard.server.common.data.yunteng.constant.ModelConstants;
  10 +import org.thingsboard.server.service.security.model.SecurityUser;
  11 +
  12 +import java.time.LocalDateTime;
  13 +
  14 +@Slf4j
  15 +@Component
  16 +public class MyBatisMetaObjectHandler implements MetaObjectHandler {
  17 + public MyBatisMetaObjectHandler() {}
  18 + private SecurityUser user = null;
  19 + public void insertFill(MetaObject metaObject) {
  20 + try {
  21 + user = getCurrentUser();
  22 + if (user != null) {
  23 + this.strictInsertFill(
  24 + metaObject,
  25 + ModelConstants.TablePropertyMapping.CREATOR,
  26 + String.class,
  27 + user.getCurrentUserId());
  28 + this.strictInsertFill(
  29 + metaObject,
  30 + ModelConstants.TablePropertyMapping.CREATE_TIME,
  31 + LocalDateTime.class,
  32 + LocalDateTime.now());
  33 + }
  34 + } catch (Exception e) {
  35 + // do nothing
  36 + }
  37 + }
  38 +
  39 + public void updateFill(MetaObject metaObject) {
  40 + user = getCurrentUser();
  41 + if (user != null) {
  42 + this.strictUpdateFill(
  43 + metaObject, ModelConstants.TablePropertyMapping.UPDATER, String.class, user.getCurrentUserId());
  44 + this.strictUpdateFill(
  45 + metaObject,
  46 + ModelConstants.TablePropertyMapping.UPDATE_TIME,
  47 + LocalDateTime.class,
  48 + LocalDateTime.now());
  49 + }
  50 + }
  51 + private SecurityUser getCurrentUser(){
  52 + SecurityUser securityUser = null;
  53 + Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  54 + if (authentication != null && authentication.getPrincipal() instanceof SecurityUser){
  55 + securityUser = (SecurityUser) authentication.getPrincipal();
  56 + }
  57 + return securityUser;
  58 + }
  59 +}
... ...
  1 +package org.thingsboard.server.controller.yunteng;
  2 +
  3 +import lombok.RequiredArgsConstructor;
  4 +import org.springframework.http.HttpStatus;
  5 +import org.springframework.http.ResponseEntity;
  6 +import org.springframework.security.access.prepost.PreAuthorize;
  7 +import org.springframework.util.Assert;
  8 +import org.springframework.web.bind.annotation.*;
  9 +import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
  10 +import org.thingsboard.server.common.data.exception.ThingsboardException;
  11 +import org.thingsboard.server.common.data.yunteng.core.exception.DataValidationException;
  12 +import org.thingsboard.server.common.data.yunteng.core.exception.FastIotException;
  13 +import org.thingsboard.server.common.data.yunteng.dto.MenuDTO;
  14 +import org.thingsboard.server.controller.BaseController;
  15 +import org.thingsboard.server.dao.yunteng.service.MenuService;
  16 +
  17 +import java.net.URI;
  18 +import java.util.List;
  19 +import java.util.Optional;
  20 +import java.util.Set;
  21 +
  22 +@RestController
  23 +@RequiredArgsConstructor
  24 +@RequestMapping("api/yt/menu")
  25 +public class YtMenuController extends BaseController {
  26 +
  27 + private final MenuService menuService;
  28 +
  29 + @GetMapping("/me/menus")
  30 + public List<MenuDTO> getMyMenus() throws ThingsboardException {
  31 + return menuService.getMyMenus(
  32 + getCurrentUser().getCurrentTenantId(),
  33 + getCurrentUser().getCurrentUserId(),
  34 + getCurrentUser().isPtSysadmin(),
  35 + getCurrentUser().isPtTenantAdmin());
  36 + }
  37 +
  38 + @GetMapping("getMenuIdsByRoleId/{roleId}")
  39 + public Set<String> getMenuIdsByRoleId(@PathVariable("roleId") String roleId) {
  40 + return menuService.getMenuIdsByRoleId(roleId);
  41 + }
  42 +
  43 + @PutMapping("/manage/assign/{roleId}")
  44 + public void assignMenuToRole(
  45 + @RequestBody String[] menuIds, @PathVariable("roleId") String roleId) {
  46 + menuService.assignMenuToRole(menuIds, roleId);
  47 + }
  48 +
  49 + @GetMapping("{id}")
  50 + public ResponseEntity<MenuDTO> getMenus(@PathVariable("id") String id) {
  51 + return ResponseEntity.of(menuService.getMenu(id));
  52 + }
  53 +
  54 + @PutMapping
  55 + // @PreAuthorize("@iot.check('menu:update')")
  56 + public MenuDTO updateMenu(@RequestBody MenuDTO menuDTO) throws ThingsboardException {
  57 + Assert.notNull(menuDTO.getId(), "menuId cannot be null");
  58 + return menuService.updateMenu(
  59 + getCurrentUser().getCurrentTenantId(), getCurrentUser().isPtSysadmin(), menuDTO);
  60 + }
  61 +
  62 + @PostMapping
  63 + @PreAuthorize("hasAnyRole('SYS_ADMIN','TENANT_ADMIN')")
  64 + public ResponseEntity<MenuDTO> saveMenu(@RequestBody MenuDTO menuDTO)
  65 + throws FastIotException, ThingsboardException {
  66 + MenuDTO newMenuDTO =
  67 + menuService.saveMenu(
  68 + getCurrentUser().getCurrentTenantId(), getCurrentUser().isPtSysadmin(), menuDTO);
  69 + return Optional.ofNullable(newMenuDTO)
  70 + .map(
  71 + dto -> {
  72 + URI location =
  73 + ServletUriComponentsBuilder.fromCurrentRequest()
  74 + .path("/{id}")
  75 + .buildAndExpand(newMenuDTO.getId())
  76 + .toUri();
  77 + return ResponseEntity.created(location).body(newMenuDTO);
  78 + })
  79 + .orElse(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build());
  80 + }
  81 +
  82 + @DeleteMapping
  83 + @PreAuthorize("hasAnyRole('SYS_ADMIN','TENANT_ADMIN')")
  84 + public void deleteMenus(@RequestBody String[] ids) throws ThingsboardException {
  85 + if (ids.length == 0) {
  86 + throw new DataValidationException("please provide menu ids to delete");
  87 + }
  88 + menuService.deleteMenus(getCurrentUser().getCurrentTenantId(), ids);
  89 + }
  90 +}
... ...
... ... @@ -10,7 +10,9 @@ import org.springframework.web.bind.annotation.RestController;
10 10 import org.thingsboard.server.dao.yunteng.entities.SysTown;
11 11 import org.thingsboard.server.dao.yunteng.service.SysTownService;
12 12
  13 +import java.util.LinkedList;
13 14 import java.util.List;
  15 +import java.util.Map;
14 16
15 17 /**
16 18 * @author: 徐浩然
... ... @@ -36,4 +38,10 @@ public class YtTownController {
36 38 @PathVariable("variable") String variable, @PathVariable("value") String value) {
37 39 return ResponseEntity.ok(sysTownService.findChilds(variable, value));
38 40 }
  41 +
  42 + @GetMapping("tree")
  43 + @ApiOperation("查询城市树形结构")
  44 + public ResponseEntity<LinkedList<Map<String, Object>>> tree() {
  45 + return ResponseEntity.ok(sysTownService.tree());
  46 + }
39 47 }
... ...
1 1 package org.thingsboard.server.dao.yunteng.impl;
2 2
  3 +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
3 4 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4 5 import lombok.RequiredArgsConstructor;
5 6 import lombok.extern.slf4j.Slf4j;
... ... @@ -9,8 +10,7 @@ import org.thingsboard.server.dao.yunteng.mapper.SysTownMapper;
9 10 import org.thingsboard.server.dao.yunteng.service.SysTownService;
10 11
11 12 import java.lang.reflect.Field;
12   -import java.util.List;
13   -import java.util.Stack;
  13 +import java.util.*;
14 14
15 15 /**
16 16 * @author: 徐浩然
... ... @@ -24,6 +24,11 @@ public class SysTownServiceImpl implements SysTownService {
24 24
25 25 private final SysTownMapper sysTownMapper;
26 26
  27 + private final String PID = "pid";
  28 + private final String ID = "id";
  29 + private final String NAME = "name";
  30 + private final String CHILD = "child";
  31 +
27 32 @Override
28 33 public List<SysTown> findChilds(String variable, String value) {
29 34 Field[] fields = SysTown.class.getDeclaredFields();
... ... @@ -47,13 +52,10 @@ public class SysTownServiceImpl implements SysTownService {
47 52 // 组成需要groupby的字段
48 53 .toArray(String[]::new);
49 54 return sysTownMapper.selectList(
50   - new QueryWrapper<SysTown>()
51   - .select(groupBy)
52   - .eq(to_(variable), value)
53   - .groupBy(groupBy));
  55 + new QueryWrapper<SysTown>().select(groupBy).eq(to_(variable), value).groupBy(groupBy));
54 56 }
55 57
56   - private String to_(String s){
  58 + private String to_(String s) {
57 59 return s.replaceAll("[A-Z]", "_$0").toLowerCase();
58 60 }
59 61
... ... @@ -67,4 +69,108 @@ public class SysTownServiceImpl implements SysTownService {
67 69 public List<SysTown> getCityList() {
68 70 return sysTownMapper.getCityList();
69 71 }
  72 +
  73 + @Override
  74 + public LinkedList<Map<String, Object>> tree() {
  75 + // 查询有序数据
  76 + List<SysTown> cityList =
  77 + sysTownMapper.selectList(
  78 + new LambdaQueryWrapper<SysTown>()
  79 + .orderByAsc(
  80 + SysTown::getCodeProv,
  81 + SysTown::getCodeCity,
  82 + SysTown::getCodeCoun,
  83 + SysTown::getCodeTown));
  84 + // 省
  85 + LinkedList<Map<String, Object>> prov = new LinkedList<>();
  86 + // 市
  87 + LinkedList<Map<String, Object>> city = new LinkedList<>();
  88 + // 区
  89 + LinkedList<Map<String, Object>> coun = new LinkedList<>();
  90 + // 县
  91 + LinkedList<Map<String, Object>> town = new LinkedList<>();
  92 + for (SysTown sysTown : cityList) {
  93 + if (coun.size() != 0 && !coun.getLast().get(ID).equals(sysTown.getCodeCoun())) {
  94 + // 切换区则县链改变
  95 + town = new LinkedList<>();
  96 + }
  97 + // 添加县
  98 + addTown(town, sysTown);
  99 +
  100 + if (city.size() != 0 && !city.getLast().get(ID).equals(sysTown.getCodeCity())) {
  101 + // 切换市则区链改变
  102 + coun = new LinkedList<>();
  103 + }
  104 + // 添加区
  105 + addCoun(coun, town, sysTown);
  106 + if (prov.size() != 0 && !prov.getLast().get(ID).equals(sysTown.getCodeProv())) {
  107 + // 切换省则市链改变
  108 + city = new LinkedList<>();
  109 + }
  110 + // 添加市
  111 + addCity(city, coun, sysTown);
  112 + // 添加省
  113 + addProv(prov, city, sysTown);
  114 + }
  115 + return prov;
  116 + }
  117 +
  118 + private void addTown(LinkedList<Map<String, Object>> town, SysTown sysTown) {
  119 + if (town.size() == 0 || !town.getLast().get(ID).equals(sysTown.getCodeTown())) {
  120 + town.addLast(
  121 + new HashMap<>() {
  122 + {
  123 + put(PID, sysTown.getCodeCoun());
  124 + put(ID, sysTown.getCodeTown());
  125 + put(NAME, sysTown.getNameTown());
  126 + // put(CHILD, null);
  127 + }
  128 + });
  129 + }
  130 + }
  131 +
  132 + private void addCoun(
  133 + LinkedList<Map<String, Object>> coun, LinkedList<Map<String, Object>> town, SysTown sysTown) {
  134 + if (coun.size() == 0 || !coun.getLast().get(ID).equals(sysTown.getCodeCoun())) {
  135 + coun.addLast(
  136 + new HashMap<>() {
  137 + {
  138 + put(PID, sysTown.getCodeCity());
  139 + put(ID, sysTown.getCodeCoun());
  140 + put(NAME, sysTown.getNameCoun());
  141 + put(CHILD, town);
  142 + }
  143 + });
  144 + }
  145 + }
  146 +
  147 + private void addCity(
  148 + LinkedList<Map<String, Object>> city, LinkedList<Map<String, Object>> coun, SysTown sysTown) {
  149 + if (city.size() == 0 || !city.getLast().get(ID).equals(sysTown.getCodeCity())) {
  150 + city.addLast(
  151 + new HashMap<>() {
  152 + {
  153 + put(PID, sysTown.getCodeProv());
  154 + put(ID, sysTown.getCodeCity());
  155 + put(NAME, sysTown.getNameCity());
  156 + put(CHILD, coun);
  157 + }
  158 + });
  159 + }
  160 + }
  161 +
  162 + private void addProv(
  163 + LinkedList<Map<String, Object>> prov, LinkedList<Map<String, Object>> city, SysTown sysTown) {
  164 + if (prov.size() == 0 || !prov.getLast().get(ID).equals(sysTown.getCodeProv())) {
  165 + prov.addLast(
  166 + new HashMap<>() {
  167 + {
  168 + // put(PID, null);
  169 + put(ID, sysTown.getCodeProv());
  170 + put(NAME, sysTown.getNameProv());
  171 + put(CHILD, city);
  172 + }
  173 + });
  174 + }
  175 + }
70 176 }
... ...
... ... @@ -8,6 +8,7 @@ import org.apache.commons.lang3.StringUtils;
8 8 import org.springframework.beans.BeanUtils;
9 9 import org.springframework.stereotype.Service;
10 10 import org.springframework.transaction.annotation.Transactional;
  11 +import org.thingsboard.server.common.data.id.EntityId;
11 12 import org.thingsboard.server.common.data.yunteng.core.exception.DataValidationException;
12 13 import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
13 14 import org.thingsboard.server.common.data.yunteng.dto.TenantDTO;
... ... @@ -54,7 +55,7 @@ public class YtTenantServiceImpl extends AbstractBaseService<TenantMapper, Tenan
54 55 public PageData<TenantDTO> page(Map<String, Object> queryMap) {
55 56 IPage<Tenant> tenantIPage = getPage(queryMap, "create_time", true);
56 57 IPage<TenantDTO> userPage =
57   - baseMapper.getTenantPage(tenantIPage, (String) queryMap.get("tenantName"));
  58 + baseMapper.getTenantPage(tenantIPage, (String) queryMap.get("tenantName"), EntityId.NULL_UUID.toString());
58 59 PageData<TenantDTO> pageData = getPageData(userPage, TenantDTO.class);
59 60 determineTenantStatus(pageData.getItems());
60 61 return pageData;
... ...
... ... @@ -14,5 +14,8 @@ import java.util.Set;
14 14 public interface TenantMapper extends BaseMapper<Tenant> {
15 15 Set<String> getTenantIdsByTenantIds(@Param("tenantIds") Collection<String> tenantIds);
16 16
17   - IPage<TenantDTO> getTenantPage(IPage<?> page, @Param("tenantName") String tenantName);
  17 + IPage<TenantDTO> getTenantPage(
  18 + IPage<?> page,
  19 + @Param("tenantName") String tenantName,
  20 + @Param("defaultTenantId") String defaultTenantId);
18 21 }
... ...
1 1 package org.thingsboard.server.dao.yunteng.service;
2 2
3   -
4   -
5 3 import org.thingsboard.server.common.data.yunteng.dto.MenuDTO;
6   -
7 4 import java.util.List;
8 5 import java.util.Optional;
9 6 import java.util.Set;
10 7
11 8 public interface MenuService {
12   -
13 9 /**
14 10 * sysadmin获取所有的menu,但是不获取租户自己创建的menu
15 11 *
... ... @@ -22,17 +18,18 @@ public interface MenuService {
22 18 *
23 19 * @return list 树状menu
24 20 */
25   - List<MenuDTO> getMyMenus(String tenantId,String userId,boolean isSysAdmin,boolean isTenantAdmin);
  21 + List<MenuDTO> getMyMenus(
  22 + String tenantId, String userId, boolean isPtSysAdmin, boolean isPtTenantAdmin);
26 23
27   - MenuDTO saveMenu(String tenantId,boolean isSysAdmin,MenuDTO menuDTO);
  24 + MenuDTO saveMenu(String tenantId, boolean isPtSysAdmin, MenuDTO menuDTO);
28 25
29 26 Optional<MenuDTO> getMenu(String id);
30 27
31   - MenuDTO updateMenu(String tenantId,boolean isSysAdmin,MenuDTO menuDTO);
  28 + MenuDTO updateMenu(String tenantId, boolean isPtSysAdmin, MenuDTO menuDTO);
32 29
33   - boolean deleteMenus(String tenantId,String[] ids);
  30 + boolean deleteMenus(String tenantId, String[] ids);
34 31
35   - boolean assignMenuToTenant(String tenantId,String[] menuId);
  32 + boolean assignMenuToTenant(String tenantId, String[] menuId);
36 33
37 34 boolean assignMenuToRole(String[] menuId, String roleId);
38 35
... ...
... ... @@ -2,7 +2,9 @@ package org.thingsboard.server.dao.yunteng.service;
2 2
3 3 import org.thingsboard.server.dao.yunteng.entities.SysTown;
4 4
  5 +import java.util.LinkedList;
5 6 import java.util.List;
  7 +import java.util.Map;
6 8
7 9 /**
8 10 * @author: 徐浩然
... ... @@ -16,4 +18,6 @@ public interface SysTownService {
16 18 SysTown findParent(String variable, String value);
17 19
18 20 List<SysTown> getCityList();
  21 +
  22 + LinkedList<Map<String, Object>> tree();
19 23 }
... ...
... ... @@ -24,7 +24,7 @@
24 24 SELECT id, tenant_id,name, enabled, description, default_config, tenant_expire_time, create_time,icon
25 25 FROM sys_tenant
26 26 <where>
27   - tenant_id != 'DEFAULT_SYS_ADMIN_TENANT_CODE'
  27 + tenant_id != #{defaultTenantId}
28 28 <if test="tenantName!=null">
29 29 AND name LIKE CONCAT('%',#{tenantName}::TEXT,'%')
30 30 </if>
... ...