Przeglądaj źródła

20230718-完善资源service

iaun 3 lat temu
rodzic
commit
0c6cbeb9ff

+ 0 - 36
src/main/java/cn/iaun/it/config/TokenConfig.java

@@ -1,36 +0,0 @@
-package cn.iaun.it.config;
-
-import org.springframework.boot.context.properties.ConfigurationProperties;
-import org.springframework.stereotype.Component;
-
-@ConfigurationProperties(prefix = "config.token") // 从配置读取变量
-@Component
-public class TokenConfig {
-    private String secret;
-    private Long expire;
-    private String header;
-
-    public Long getExpire() {
-        return expire;
-    }
-
-    public void setExpire(Long expire) {
-        this.expire = expire;
-    }
-
-    public String getHeader() {
-        return header;
-    }
-
-    public void setHeader(String header) {
-        this.header = header;
-    }
-
-    public String getSecret() {
-        return secret;
-    }
-
-    public void setSecret(String secret) {
-        this.secret = secret;
-    }
-}

+ 16 - 0
src/main/java/cn/iaun/it/controller/ItResourceController.java

@@ -1,9 +1,25 @@
 package cn.iaun.it.controller;
 
+import cn.iaun.it.model.ItResourceType;
+import cn.iaun.it.model.ItResourceValue;
+import cn.iaun.it.service.ItResourceService;
+import cn.iaun.it.util.Result;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 @RestController
 @RequestMapping("/resource")
 public class ItResourceController {
+    private final ItResourceService itResourceService;
+
+    public ItResourceController(ItResourceService itResourceService) {
+        this.itResourceService = itResourceService;
+    }
+
+    @PostMapping(value = "/type/get")
+    public Result<ItResourceType> getItResourceType() {
+        return null;
+    }
 }

+ 9 - 0
src/main/java/cn/iaun/it/mapper/ItResourceAttributeMapper.java

@@ -0,0 +1,9 @@
+package cn.iaun.it.mapper;
+
+import cn.iaun.it.model.ItResourceAttribute;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface ItResourceAttributeMapper extends BaseMapper<ItResourceAttribute> {
+}

+ 9 - 0
src/main/java/cn/iaun/it/mapper/ItResourceGroupMapper.java

@@ -0,0 +1,9 @@
+package cn.iaun.it.mapper;
+
+import cn.iaun.it.model.ItResourceGroup;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface ItResourceGroupMapper extends BaseMapper<ItResourceGroup> {
+}

+ 9 - 0
src/main/java/cn/iaun/it/mapper/ItResourceMapper.java

@@ -0,0 +1,9 @@
+package cn.iaun.it.mapper;
+
+import cn.iaun.it.model.ItResource;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface ItResourceMapper extends BaseMapper<ItResource> {
+}

+ 9 - 0
src/main/java/cn/iaun/it/mapper/ItResourceTypeMapper.java

@@ -0,0 +1,9 @@
+package cn.iaun.it.mapper;
+
+import cn.iaun.it.model.ItResourceType;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface ItResourceTypeMapper extends BaseMapper<ItResourceType> {
+}

+ 13 - 0
src/main/java/cn/iaun/it/mapper/ItResourceValueMapper.java

@@ -0,0 +1,13 @@
+package cn.iaun.it.mapper;
+
+import cn.iaun.it.model.ItResourceValue;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Mapper;
+
+import java.util.List;
+
+@Mapper
+public interface ItResourceValueMapper extends BaseMapper<ItResourceValue> {
+    // 一次添加多条resource value
+    int insertManyResourceValue(List<ItResourceValue> itResourceValue);
+}

+ 35 - 9
src/main/java/cn/iaun/it/service/ItResourceService.java

@@ -1,27 +1,53 @@
 package cn.iaun.it.service;
 
-import cn.iaun.it.model.ItResourceGroup;
-import cn.iaun.it.model.ItResourceType;
+import cn.iaun.it.model.*;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 
 import java.util.List;
 
 public interface ItResourceService {
-    List<ItResourceGroup> getResourceGroup();
+    // 资源组
+    List<ItResourceGroup> getResourceGroup(); // 获取全部资源组
 
-    boolean addResourceGroup(Long id);
+    int addResourceGroup(ItResourceGroup itResourceGroup);
 
-    boolean delResourceGroup(Long id);
+    int delResourceGroup(Long id);
 
-    boolean setResourceGroup(Long id, ItResourceType itResourceType);
+    int setResourceGroup(ItResourceGroup itResourceGroup);
 
+    // 资源类型
     List<ItResourceType> getResourceType();
 
-    boolean addResourceType(Long id);
+    int addResourceType(ItResourceType itResourceType);
 
-    boolean delResourceType(Long id);
+    int delResourceType(Long id);
 
-    boolean setResourceType(Long id, ItResourceType itResourceType);
+    int setResourceType(ItResourceType itResourceType);
 
+    // 资源属性
+    IPage<ItResourceAttribute> getResourceAttribute(Integer pageNo, Integer pageSize, Long groupId);
 
+    int addResourceAttribute(ItResourceAttribute itResourceAttribute);
+
+    int delResourceAttribute(Long id);
+
+    int setResourceAttribute(ItResourceAttribute itResourceAttribute);
+
+    // 资源
+    IPage<ItResource> getResource(Integer pageNo, Integer pageSize, Long resourceTypeId);
+
+    int addResource(ItResource itResource);
+
+    int delResource(Long id);
+
+    int setResource(ItResource itResource);
+
+    // 资源值
+    List<ItResourceValue> getResourceValues(Long resourceId);
+
+    int addResourceValues(List<ItResourceValue> itResourceValue);
+
+    int delResourceValueByResourceId(Long resourceId);
+
+    int setResourceValueById(ItResourceValue itResourceValue);
 }

+ 139 - 0
src/main/java/cn/iaun/it/service/impl/ItResourceServiceImpl.java

@@ -0,0 +1,139 @@
+package cn.iaun.it.service.impl;
+
+import cn.iaun.it.mapper.*;
+import cn.iaun.it.model.*;
+import cn.iaun.it.service.ItResourceService;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+@Service
+public class ItResourceServiceImpl implements ItResourceService {
+    private final ItResourceGroupMapper itResourceGroupMapper;
+    private final ItResourceTypeMapper itResourceTypeMapper;
+    private final ItResourceAttributeMapper itResourceAttributeMapper;
+    private final ItResourceMapper itResourceMapper;
+    private final ItResourceValueMapper itResourceValueMapper;
+
+    public ItResourceServiceImpl(ItResourceGroupMapper itResourceGroupMapper, ItResourceTypeMapper itResourceTypeMapper, ItResourceAttributeMapper itResourceAttributeMapper, ItResourceMapper itResourceMapper, ItResourceValueMapper itResourceValueMapper) {
+        this.itResourceGroupMapper = itResourceGroupMapper;
+        this.itResourceTypeMapper = itResourceTypeMapper;
+        this.itResourceAttributeMapper = itResourceAttributeMapper;
+        this.itResourceMapper = itResourceMapper;
+        this.itResourceValueMapper = itResourceValueMapper;
+    }
+
+    @Override
+    public List<ItResourceGroup> getResourceGroup() {
+        return itResourceGroupMapper.selectList(null);
+    }
+
+    @Override
+    public int addResourceGroup(ItResourceGroup itResourceGroup) {
+        return itResourceGroupMapper.insert(itResourceGroup);
+
+    }
+
+    @Override
+    public int delResourceGroup(Long id) {
+        return itResourceGroupMapper.deleteById(id);
+    }
+
+    @Override
+    public int setResourceGroup(ItResourceGroup itResourceGroup) {
+        return itResourceGroupMapper.updateById(itResourceGroup);
+    }
+
+    @Override
+    public List<ItResourceType> getResourceType() {
+        return itResourceTypeMapper.selectList(null);
+    }
+
+    @Override
+    public int addResourceType(ItResourceType itResourceType) {
+        return itResourceTypeMapper.insert(itResourceType);
+    }
+
+    @Override
+    public int delResourceType(Long id) {
+        return itResourceTypeMapper.deleteById(id);
+    }
+
+    @Override
+    public int setResourceType(ItResourceType itResourceType) {
+        return itResourceTypeMapper.updateById(itResourceType);
+    }
+
+    @Override
+    public IPage<ItResourceAttribute> getResourceAttribute(Integer pageNo, Integer pageSize, Long groupId) {
+        Page<ItResourceAttribute> page = new Page<>(pageNo, pageSize);
+        QueryWrapper<ItResourceAttribute> queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq("group_id", groupId);
+        return itResourceAttributeMapper.selectPage(page, queryWrapper);
+    }
+
+    @Override
+    public int addResourceAttribute(ItResourceAttribute itResourceAttribute) {
+        return itResourceAttributeMapper.insert(itResourceAttribute);
+    }
+
+    @Override
+    public int delResourceAttribute(Long id) {
+        return itResourceAttributeMapper.deleteById(id);
+    }
+
+    @Override
+    public int setResourceAttribute(ItResourceAttribute itResourceAttribute) {
+        return itResourceAttributeMapper.updateById(itResourceAttribute);
+    }
+
+    @Override
+    public IPage<ItResource> getResource(Integer pageNo, Integer pageSize, Long resourceTypeId) {
+        Page<ItResource> itResourcePage = new Page<>(pageNo, pageSize);
+        QueryWrapper<ItResource> itResourceQueryWrapper = new QueryWrapper<>();
+        itResourceQueryWrapper.eq("type_id", resourceTypeId);
+        return itResourceMapper.selectPage(itResourcePage, itResourceQueryWrapper);
+    }
+
+    @Override
+    public int addResource(ItResource itResource) {
+        return itResourceMapper.insert(itResource);
+    }
+
+    @Override
+    public int delResource(Long id) {
+        return itResourceMapper.deleteById(id);
+    }
+
+    @Override
+    public int setResource(ItResource itResource) {
+        return itResourceMapper.updateById(itResource);
+    }
+
+    @Override
+    public List<ItResourceValue> getResourceValues(Long resourceId) {
+        QueryWrapper<ItResourceValue> itResourceValueQueryWrapper = new QueryWrapper<>();
+        itResourceValueQueryWrapper.eq("resource_id", resourceId);
+        return itResourceValueMapper.selectList(itResourceValueQueryWrapper);
+    }
+
+    @Override
+    public int addResourceValues(List<ItResourceValue> itResourceValue) {
+        return itResourceValueMapper.insertManyResourceValue(itResourceValue);
+    }
+
+    @Override
+    public int delResourceValueByResourceId(Long resourceId) {
+        QueryWrapper<ItResourceValue> itResourceValueQueryWrapper = new QueryWrapper<>();
+        itResourceValueQueryWrapper.eq("resource_id", resourceId);
+        return itResourceValueMapper.delete(itResourceValueQueryWrapper);
+    }
+
+    @Override
+    public int setResourceValueById(ItResourceValue itResourceValue) {
+        return itResourceValueMapper.updateById(itResourceValue);
+    }
+}

+ 3 - 7
src/main/resources/application.yml

@@ -19,15 +19,11 @@ spring:
   jackson:
     time-zone: GMT+8
 
-
-config:
-  token:
-    secret: abcdef
-    expire: 1
-    header: 1
-
 logging:
   level:
     cn.iaun.it.mapper: trace
 mybatis-plus:
   mapper-locations: classpath*:/mapper/**/*Mapper.xml
+  global-config:
+    db-config:
+      update-strategy: ignored

+ 16 - 16
src/main/resources/db/data.sql

@@ -63,29 +63,29 @@ delete
 from `it_resource_attribute`;
 insert into `it_resource_attribute`
 (attribute_id, group_id, attribute_name, display_name, attribute_type, value_type, default_value, options)
-values (1, 1, 'name', '服务器名称', 0, 0, null, null),
-       (2, 1, 'model', '型号', 0, 0, null, null),
-       (3, 1, 'location', '服务器位置', 0, 0, null, null),
-       (4, 1, 'hostname', '主机名', 0, 0, null, null),
-       (5, 1, 'cpu', 'CPU型号', 0, 0, null, null),
-       (6, 1, 'cpu_cores', 'CPU核心数量', 0, 1, null, null),
-       (7, 1, 'memory', '内存大小(GB)', 0, 1, null, null),
-       (9, 2, 'name', '交换机名称', 0, 0, null, null),
-       (10, 2, 'model', '交换机型号', 0, 0, null, null),
-       (11, 2, 'port', '端口', 1, null, null, null),
-       (12, 3, 'name', '存储名称', 0, 0, null, null),
-       (13, 3, 'company', '厂商', 2, null, null, 'DELL,HP,HUAWEI'),
-       (14, 3, 'expire', '是否过保', 2, null, '否', '是,否'),
-       (15, 3, 'disk', '磁盘', 1, null, null, null)
+values (1, 1, 'name', '服务器名称', 0, 0, '', ''),
+       (2, 1, 'model', '型号', 0, 0, '', ''),
+       (3, 1, 'location', '服务器位置', 0, 0, '', ''),
+       (4, 1, 'hostname', '主机名', 0, 0, '', ''),
+       (5, 1, 'cpu', 'CPU型号', 0, 0, '', ''),
+       (6, 1, 'cpu_cores', 'CPU核心数量', 0, 1, '', ''),
+       (7, 1, 'memory', '内存大小(GB)', 0, 1, '', ''),
+       (9, 2, 'name', '交换机名称', 0, 0, '', ''),
+       (10, 2, 'model', '交换机型号', 0, 0, '', ''),
+       (11, 2, 'port', '端口', 1, 0, '', ''),
+       (12, 3, 'name', '存储名称', 0, 0, '', ''),
+       (13, 3, 'company', '厂商', 2, 0, '', 'DELL,HP,HUAWEI'),
+       (14, 3, 'expire', '是否过保', 2, 0, '否', '是,否'),
+       (15, 3, 'disk', '磁盘', 1, 0, '', '')
 ;
 
 delete
 from `it_resource_type`;
 insert into `it_resource_type` (type_id, type_name, parent_type_id)
-values (1, '基础设备', null),
+values (1, '基础设备', -1),
        (2, '服务器', 1),
        (3, '交换机', 1),
-       (4, '存储设备', null),
+       (4, '存储设备', -1),
        (5, '分布式存储', 4)
 ;
 

+ 4 - 4
src/main/resources/db/schema.sql

@@ -51,9 +51,9 @@ create table `it_resource_attribute`
     `attribute_name` varchar(255) not null comment '属性名称',
     `display_name`   varchar(255) not null comment '显示名称',
     `attribute_type` tinyint      not null comment '资源属性类型:0普通(文本、数字等),1表格,2下拉选项',
-    `value_type`     tinyint      null default 0 comment '值类型;仅属性类型=0有效:0文本,1数字',
-    `default_value`  varchar(255) null default '' comment '默认值;仅属性类型=0或2有效',
-    `options`        varchar(255) null default null comment '下拉选项;仅属性类型=2有效',
+    `value_type`     tinyint      not null default 0 comment '值类型;仅属性类型=0有效:0文本,1数字',
+    `default_value`  varchar(255) not null default '' comment '默认值;仅属性类型=0或2有效',
+    `options`        varchar(255) not null default '' comment '下拉选项;仅属性类型=2有效',
     primary key (`attribute_id`)
 ) comment ='资源属性';
 
@@ -62,7 +62,7 @@ create table `it_resource_type`
 (
     `type_id`        bigint(20)   not null auto_increment comment '资源类型ID',
     `type_name`      varchar(255) not null comment '资源名称',
-    `parent_type_id` bigint(20)   null default null comment '父类型ID',
+    `parent_type_id` bigint(20)   not null default -1 comment '父类型ID',
     primary key (`type_id`)
 ) comment ='资源类型';
 

+ 12 - 0
src/main/resources/mapper/ItResourceValueMapper.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="cn.iaun.it.mapper.ItResourceValueMapper">
+
+    <insert id="insertManyResourceValue">
+        insert into `it_resource_value` (`resource_id`, `attribute_id`, `attribute_value`)
+        values
+        <foreach index="index" collection="list" item="item" separator=",">
+            (#{item.resourceId}, #{item.attributeId}, #{item.attributeValue})
+        </foreach>
+    </insert>
+</mapper>

+ 36 - 8
src/test/java/cn/iaun/it/SampleTest.java

@@ -1,14 +1,18 @@
 package cn.iaun.it;
 
 import cn.iaun.it.mapper.ItUserMapper;
+import cn.iaun.it.model.ItResourceAttribute;
+import cn.iaun.it.model.ItResourceValue;
 import cn.iaun.it.model.ItUser;
 import cn.iaun.it.model.vo.user.ItUserLoginInfoVo;
-import cn.iaun.it.config.TokenConfig;
+import cn.iaun.it.service.ItResourceService;
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 
+import java.util.ArrayList;
 import java.util.List;
 
 @SpringBootTest
@@ -16,13 +20,13 @@ public class SampleTest {
     @Autowired
     private ItUserMapper itUserMapper;
     @Autowired
-    private TokenConfig tokenConfig;
+    private ItResourceService itResourceService;
 
     @Test
     public void testSelect() {
         System.out.println("--- select all method test ---");
         List<ItUser> userList = itUserMapper.selectList(null);
-        Assertions.assertEquals(1, userList.size());
+//        Assertions.assertEquals(1, userList.size());
         userList.forEach(System.out::println);
     }
 
@@ -33,10 +37,34 @@ public class SampleTest {
     }
 
     @Test
-    public void testGetSecret() {
-        System.out.println(tokenConfig.getSecret());
-        System.out.println(tokenConfig.getHeader());
-        System.out.println(tokenConfig.getExpire());
-    }
+    public void testItResource() {
+//        IPage<ItResourceAttribute> itResourceAttributeIPage = itResourceService.getResourceAttribute(3, 2, 1L);
+//        System.out.println(itResourceAttributeIPage.getTotal());
+//        System.out.println(itResourceAttributeIPage.getRecords());
+
+        List<ItResourceValue> list = new ArrayList<>();
 
+        ItResourceValue value1 = new ItResourceValue();
+        value1.setResourceId(801L);
+        value1.setAttributeValue("test1");
+        value1.setAttributeId(701L);
+        ItResourceValue value2 = new ItResourceValue();
+        value2.setResourceId(802L);
+        value2.setAttributeValue("test2");
+        value2.setAttributeId(702L);
+        ItResourceValue value3 = new ItResourceValue();
+        value3.setResourceId(803L);
+        value3.setAttributeValue("test3");
+        value3.setAttributeId(703L);
+//        ItResourceValue value4 = new ItResourceValue();
+//        value4.setResourceId(803L);
+//        value4.setAttributeValue("test3");
+//        value4.setAttributeId(703L);
+
+        list.add(value1);
+        list.add(value2);
+        list.add(value3);
+//        list.add(value4);
+        System.out.println(itResourceService.addResourceValues(list));
+    }
 }