一个系统中,一些设置,可以局部自定义。默认情况下,数据库没有设置的。
表结构如下:
-- ---------------------------- -- Table structure for status -- ---------------------------- DROP TABLE IF EXISTS `status`; CREATE TABLE `status` ( `id` int(11) NOT NULL AUTO_INCREMENT, `label` varchar(255) DEFAULT NULL, `value` varchar(255) DEFAULT NULL, `update_time` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `label` (`label`) USING BTREE, KEY `value` (`value`) USING BTREE ) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of status -- ---------------------------- INSERT INTO `status` VALUES ('1', 'risk.level', '{\"rule\":{\"province\":[40,60,100,450,1000],\"city\":[5,10,40,60,94],\"area\":[5,10,15,20,25]},\"colorThreshold\":[0,9,99,499,999,4999],\"colors\":[\"#69ae5d\",\"#fdea61\",\"#f19d39\",\"#e25141\",\"#901d14\"]}', '2020-02-26 10:49:54'); INSERT INTO `status` VALUES ('2', 'risk.weight.confirm', '80', '2020-02-28 19:03:16'); INSERT INTO `status` VALUES ('3', 'risk.weight.dead', '15', '2020-02-28 19:03:16'); INSERT INTO `status` VALUES ('4', 'risk.weight.heal', '5', '2020-02-28 19:03:16'); INSERT INTO `status` VALUES ('5', 'risk.updateTime', '{\'lastUpdateTime\':\'2020-02-23 13:43:18\'}', '2020-02-23 14:18:38'); INSERT INTO `status` VALUES ('6', 'risk.people.threshold', '[0,9,99,499,999,4999]', null); INSERT INTO `status` VALUES ('7', 'risk.color.level0', '#69ae5d', '2020-02-27 10:22:01'); INSERT INTO `status` VALUES ('8', 'risk.color.level1', '#fdeb61', '2020-02-27 10:22:01'); INSERT INTO `status` VALUES ('9', 'risk.color.level2', '#f19e39', '2020-02-27 10:22:01'); INSERT INTO `status` VALUES ('10', 'risk.color.level3', '#e25141', '2020-02-27 10:22:01'); INSERT INTO `status` VALUES ('11', 'risk.color.level4', '#901c14', '2020-02-27 10:22:01'); INSERT INTO `status` VALUES ('12', 'risk.people.province', '[40, 60, 100, 450, 1000]', '2020-02-27 17:48:13'); INSERT INTO `status` VALUES ('13', 'risk.people.city', '[5, 10, 40, 60, 94]', '2020-02-27 17:48:13'); INSERT INTO `status` VALUES ('14', 'risk.people.area', '[5, 10, 15, 20, 25]', '2020-02-27 17:48:13'); INSERT INTO `status` VALUES ('15', 'risk.color', '{\"level0\":\"#b6d7a8\",\"level1\":\"#fdeb61\",\"level2\":\"#f19e39\",\"level3\":\"#e25141\",\"level4\":\"#901c14\"}', '2020-02-28 19:03:39'); INSERT INTO `status` VALUES ('16', 'lastUpdateTime', '2020-02-23 13:43:18', null);
其中一个json如下
{ "rule": { "province": [ 40, 60, 100, 450, 1000 ], "city": [ 5, 10, 40, 60, 94 ], "area": [ 5, 10, 15, 20, 25 ] }, "colorThreshold": [ 0, 9, 99, 499, 999, 4999 ], "colors": [ "#69ae5d", "#fdea61", "#f19d39", "#e25141", "#901d14" ] }
以上json为例,在类中设计如下:
List<String> defaultColors = Arrays.asList("#69ae5d","#fdea61","#f19d39","#e25141","#901d14"); List<Integer> areaDefaultThreshold = Arrays.asList(5,10,15,20,25); List<Integer> cityDefaultThreshold = Arrays.asList(5,10,40,60,94); List<Integer> provinceDefaultThreshold = Arrays.asList(40,60,100,450,1000); List<Integer> colorThreshold = Arrays.asList(0,9,99,499,999,4999); @PostConstruct public void init() { defaultConfig = RiskLevel.builder() .colors(defaultColors) .colorThreshold(colorThreshold) .rule(Rule.builder() .area(areaDefaultThreshold) .city(cityDefaultThreshold) .province(provinceDefaultThreshold) .build()) .build(); }
前端传来的json进行局部更新时,操作代码如下
@PostMapping("saveColors") public ResponseEntity<Map> saveColors(@RequestBody RiskLevel riskLevel) { List<String> colors = riskLevel.getColors(); if(!CollectionUtils.isEmpty(colors)) { int cntColors = colors.size(); for (int i = 0; i < cntColors; i++) { String color = colors.get(i); if (!StringUtils.isEmpty(color)) { defaultConfig.getColors().set(i, color); } } } Rule rule = riskLevel.getRule(); if(Objects.nonNull(rule)) { List<Integer> provinces = rule.getProvince(); if(!CollectionUtils.isEmpty(provinces)) { int cntProvinces = provinces.size(); for (int i = 0; i < cntProvinces; i++) { Integer province = provinces.get(i); if(Objects.nonNull(province)) { rule.getProvince().set(i,province); } } } List<Integer> cities = rule.getCity(); if(!CollectionUtils.isEmpty(cities)) { int cntCities = cities.size(); for (int i = 0; i < cntCities; i++) { Integer city = cities.get(i); if(Objects.nonNull(city)) { rule.getCity().set(i, city); } } } List<Integer> areas = rule.getArea(); if(!CollectionUtils.isEmpty(areas)) { int cntAreas = areas.size(); for (int i = 0; i < cntAreas; i++) { Integer city = areas.get(i); if(Objects.nonNull(city)) { rule.getArea().set(i, city); } } } defaultConfig.setRule(rule); } riskService.updateLevel(defaultConfig); byteDanceDataService.updateWeight(); return ResponseEntity.status(HttpStatus.OK).body(Collections.singletonMap("message","保存成功")); }
前端局部更新的javascript
在需要更新的颜色相对索引填入颜色数据,其他地方填入空字符串“”
$("#save").on('click',function () { var total = 0; var json = {}; var rule= {}; json.colors=[]; $('.risklevel').each(function (index,elem){ var hex = rgbToHex($("#color-picker"+(index+1)).css("background-color")); json.colors.splice(index,0,hex); total++; }); // var length = rule.colors.length; // // for (var i = 0; i < length; i++) // { // console.log(rule.colors[i]); // } var dists= []; var thresholds =[]; $('.threshold').each(function (index,elem){ var dist = $(this).val(); dists.push(dist); }); $('.value').each(function (index,elem){ var threshold = $(this).val(); thresholds.push(threshold); }); ////{"rule":{"province":[40,60,100,451,1000],"city":[5,10,40,60,94],"area":[5,10,15,20,25]},"colors":["#69ae5d","#fdea61","#f19d39","#e25141","#901d14"]} var province=[]; var city=[]; var area=[]; for (var i= 0; i < total * 3; i+=3) { province.push(thresholds[i]); city.push(thresholds[i+1]); area.push(thresholds[i+2]); } rule.province = province; rule.city = city; rule.area = area; json.rule= rule; console.log(JSON.stringify(json)); $.ajax({ url: "/admin/saveColors", data: JSON.stringify(json), type: "post", contentType: "application/json", dataType: "json", success: function(data) { alert(data.message); } }); });
相关java类
import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data; import lombok.*; import java.util.ArrayList; import java.util.List; @Data @Builder @EqualsAndHashCode(callSuper = false) @NoArgsConstructor @AllArgsConstructor public class RiskLevel { @JsonIgnore private Integer id; private Rule rule; private List<Integer> colorThreshold = new ArrayList<>(0); private List<String> colors = new ArrayList<>(0); }
import lombok.Data; import lombok.*; import java.util.ArrayList; import java.util.List; @Data @Builder @EqualsAndHashCode(callSuper = false) @NoArgsConstructor @AllArgsConstructor public class Rule { private List<Integer> province = new ArrayList<>(0); private List<Integer> city = new ArrayList<>(0); private List<Integer> area = new ArrayList<>(0); }
有疑问留意交流
本博客文章绝大多数为原创,少量为转载,代码经过测试验证,如果有疑问直接留言或者私信我。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。