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;
/**
*
* 服务实现类
*
*
* @author moral
* @since 2022-08-09
*/
@Service
public class BenchmarkWindConfigServiceImpl extends ServiceImpl implements BenchmarkWindConfigService {
@Autowired
private BenchmarkWindConfigMapper benchmarkWindConfigMapper;
@Override
public Map insert(BenchmarkWindConfig benchmarkWindConfig) {
Map resultMap = new HashMap<>();
String benchmark = benchmarkWindConfig.getBenchmark();
String preservation = benchmarkWindConfig.getPreservation();
Map benchmarkMap = mapStringToMap(benchmark);
Map 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 mapStringToMap(String str){
str = str.substring(1, str.length()-1);
String[] strs = str.split(",");
Map map = new HashMap();
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;
}
}