jinpengyong
2023-09-05 25a977a6408fe6ba20ddacf6f43bee2fe93eb17b
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
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.moral.api.entity.BenchmarkWindConfig;
import com.moral.api.mapper.BenchmarkWindConfigMapper;
import com.moral.api.service.BenchmarkWindConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.util.LogUtils;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author moral
 * @since 2022-08-09
 */
@Service
public class BenchmarkWindConfigServiceImpl extends ServiceImpl<BenchmarkWindConfigMapper, BenchmarkWindConfig> implements BenchmarkWindConfigService {
 
    @Autowired
    private BenchmarkWindConfigMapper benchmarkWindConfigMapper;
 
    @Override
    public Map<String, Object> insert(BenchmarkWindConfig benchmarkWindConfig) {
        Map<String, Object> resultMap = new HashMap<>();
 
        String benchmark = benchmarkWindConfig.getBenchmark();
        String preservation = benchmarkWindConfig.getPreservation();
        Map<String, String> benchmarkMap = mapStringToMap(benchmark);
        Map<String, String> preservationMap = mapStringToMap(preservation);
        benchmark = JSON.toJSONString(benchmarkMap);
        preservation = JSON.toJSONString(preservationMap);
        benchmarkWindConfig.setBenchmark(benchmark);
        benchmarkWindConfig.setPreservation(preservation);
        benchmarkWindConfigMapper.insert(benchmarkWindConfig);
 
        //操作日志
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        StringBuilder content = new StringBuilder();
        content.append("添加了基准风场配置:").append(benchmarkWindConfig.getName() + ";");
        LogUtils.saveOperationForManage(request,content.toString(),Constants.INSERT_OPERATE_TYPE);
 
        resultMap.put("code", ResponseCodeEnum.SUCCESS.getCode());
        resultMap.put("msg", ResponseCodeEnum.SUCCESS.getMsg());
        return resultMap;
    }
 
    /**
     * 将Map字符串转换为Map
     *
     * @param str Map字符串
     * @return Map
     */
    public static Map<String,String> mapStringToMap(String str){
        str = str.substring(1, str.length()-1);
        String[] strs = str.split(",");
        Map<String,String> map = new HashMap<String, String>();
        for (String string : strs) {
            String key = string.split("=")[0];
            String value = string.split("=")[1];
            // 去掉头部空格
            String key1 = key.trim();
            String value1 = value.trim();
            map.put(key1, value1);
        }
        return map;
    }
}