Commit c70344d7 by YANGYANG

刷新json字典文件提交

parent 88caf8d2
package com.founder.commonutils.bzptEntity;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="字典码对象", description="字典码对象")
public class DictItem implements Serializable {
private String id = "";
private String label = "";
private String parentId = "";
private List<DictItem> children = new ArrayList<>();
}
package com.founder.commonutils.bzptEntity;
import com.alibaba.fastjson.annotation.JSONField;
public class ZTreeNode {
@JSONField(alternateNames = {"ids","code"})
private Object ids;
private String name;
@JSONField(alternateNames = { "codeType","groupid"})
private String codeType;
private String text;
private String pId;
private String id;
private String yycj1;
private String yycj2;
private String yycj3;
private String yycj4;
private String yycj5;
private String yycj6;
private String yycj7;
private String yycj8;
private String yycj9;
private Boolean isParent=false;
private Boolean open=false;
private String parentFlag = "";
private String startcode;
private String endcode;
private String description;
private String state;
public ZTreeNode(Object ids, String text, String pId, String description){
this.ids=ids;
this.text=text;
this.pId=pId;
this.description=description;
}
public ZTreeNode(Object ids, String text, String pId, boolean open, boolean isParent, String yycj1, String yycj2, String yycj3, String yycj4, String yycj5, String yycj6, String yycj7, String yycj8, String yycj9, String description){
this.ids=ids;
this.text=text;
this.pId=pId;
this.open=open;
this.isParent=isParent;
this.yycj1=yycj1;
this.yycj2=yycj2;
this.yycj3=yycj3;
this.yycj4=yycj4;
this.yycj5=yycj5;
this.yycj6=yycj6;
this.yycj7=yycj7;
this.yycj8=yycj8;
this.yycj9=yycj9;
this.description=description;
}
public ZTreeNode(){}
public Object getIds() {
return ids;
}
public void setIds(Object ids) {
this.ids = ids;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getpId() {
return pId;
}
public void setpId(String pId) {
this.pId = pId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public boolean isIsParent() {
if("true".equals(this.getParentFlag())){
this.isParent = true;
}
return isParent;
}
public void setIsParent(boolean isParent) {
if(this.getParentFlag().equals("true")){
this.isParent = true;
}
}
public boolean isOpen() {
return open;
}
public void setOpen(boolean open) {
this.open = open;
}
public String getParentFlag() {
return parentFlag;
}
public void setParentFlag(String parentFlag) {
this.parentFlag = parentFlag;
}
public String getYycj1() {
return yycj1;
}
public void setYycj1(String yycj1) {
this.yycj1 = yycj1;
}
public String getYycj2() {
return yycj2;
}
public void setYycj2(String yycj2) {
this.yycj2 = yycj2;
}
public String getYycj3() {
return yycj3;
}
public void setYycj3(String yycj3) {
this.yycj3 = yycj3;
}
public String getYycj4() {
return yycj4;
}
public void setYycj4(String yycj4) {
this.yycj4 = yycj4;
}
public String getStartcode() {
return startcode;
}
public void setStartcode(String startcode) {
this.startcode = startcode;
}
public String getEndcode() {
return endcode;
}
public void setEndcode(String endcode) {
this.endcode = endcode;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getYycj5() {
return yycj5;
}
public void setYycj5(String yycj5) {
this.yycj5 = yycj5;
}
public String getYycj6() {
return yycj6;
}
public void setYycj6(String yycj6) {
this.yycj6 = yycj6;
}
public String getYycj7() {
return yycj7;
}
public void setYycj7(String yycj7) {
this.yycj7 = yycj7;
}
public String getYycj8() {
return yycj8;
}
public void setYycj8(String yycj8) {
this.yycj8 = yycj8;
}
public String getYycj9() {
return yycj9;
}
public void setYycj9(String yycj9) {
this.yycj9 = yycj9;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCodeType() {
return codeType;
}
public void setCodeType(String codeType) {
this.codeType = codeType;
}
@Override
public String toString() {
return "ZTreeNode{" +
"ids=" + ids +
", name='" + name + '\'' +
", codeType='" + codeType + '\'' +
", text='" + text + '\'' +
", pId='" + pId + '\'' +
", id='" + id + '\'' +
", yycj1='" + yycj1 + '\'' +
", yycj2='" + yycj2 + '\'' +
", yycj3='" + yycj3 + '\'' +
", yycj4='" + yycj4 + '\'' +
", yycj5='" + yycj5 + '\'' +
", yycj6='" + yycj6 + '\'' +
", yycj7='" + yycj7 + '\'' +
", yycj8='" + yycj8 + '\'' +
", yycj9='" + yycj9 + '\'' +
", isParent=" + isParent +
", open=" + open +
", parentFlag='" + parentFlag + '\'' +
", startcode='" + startcode + '\'' +
", endcode='" + endcode + '\'' +
", description='" + description + '\'' +
", state='" + state + '\'' +
'}';
}
}
This source diff could not be displayed because it is too large. You can view the blob instead.
package com.founder.bzpt.controller;
import com.alibaba.fastjson.JSONObject;
import com.founder.bzpt.service.SysDictitemService;
import com.founder.commonutils.bzptEntity.DictItem;
import com.founder.commonutils.publicEntity.MapRestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;
@RestController
@RequestMapping("/")
@Api(tags = "Redis控制类")
public class RedisController {
@Autowired
private SysDictitemService dicItemService;
@ApiOperation(value = "根据类别刷新字典码 type=all刷新所有")
@GetMapping(value = "/refreCaCheCode")
public MapRestResult refreCaCheCode(@ApiParam(name = "type", value = "GROUPID", required = true)
@RequestParam String type){
dicItemService.refreCaCheCode(type);
return MapRestResult.ok();
}
@ApiOperation(value = "根据类别获取字典码")
@GetMapping(value = "/queryTreeDataByType")
public MapRestResult queryTreeDataByType(@ApiParam(name = "type", value = "GROUPID", required = true)
@RequestParam String type,
@ApiParam(name = "startId", value = "startId")
@RequestParam(required = false) String startId){
List<DictItem> list = dicItemService.queryTreeDataByType(type,startId);
return MapRestResult.build(200,"成功",list.size(),list);
}
@ApiOperation(value = "根据GROUPID刷新静态字典")
@RequestMapping(value = "/reloadAjlb", method = RequestMethod.POST)
@ResponseBody
public String reloadAjlb(HttpServletRequest request, @RequestParam(value = "type" , required = false) String type) {
if(type==null||"".equals(type)){
setAjlb("CODE_GABAJZLB","CODE_GABAJZLB.json",request);
setAjlb("CODE_GABAJXZLB","CODE_GABAJXZLB.json",request);
}else{
if("CODE_GABAJZLB".equals(type)){
setAjlb("CODE_GABAJZLB","CODE_GABAJZLB.json",request);
}else if("CODE_GABAJXZLB".equals(type)){
setAjlb("CODE_GABAJXZLB","CODE_GABAJXZLB.json",request);
}
}
return "success";
}
private void setAjlb(String type,String typeJson,HttpServletRequest request){
String jsonStr = "";
List<DictItem> list = dicItemService.queryTreeDataByType(type,"");
jsonStr = JSONObject.toJSONString(list);
try {
String filePath = /*request.getSession().getServletContext().getRealPath("/") + */"I:\\dist\\JsonData\\"+typeJson;
File file = new File(filePath);
String encoding = "utf-8";
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file), encoding));
out.write(jsonStr); // \r\n即为换行
out.flush(); // 把缓存区内容压入文件
out.close(); // 最后记得关闭文件
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.founder.bzpt.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.founder.commonutils.bzptEntity.SysDictitem;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* <p>
* 系统字典信息 Mapper 接口
* </p>
*
* @author yangyang
* @since 2021-07-09
*/
@Mapper
@Component
public interface SysDictitemMapper extends BaseMapper<SysDictitem> {
List<String> getAllGroupid();
}
<?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="com.founder.bzpt.mapper.SysDictitemMapper">
<select id="getAllGroupid" resultType="java.lang.String">
select GROUPID from SYS_DICTITEM WHERE SCBZ = '0' GROUP BY GROUPID
</select>
</mapper>
package com.founder.bzpt.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.founder.commonutils.bzptEntity.DictItem;
import com.founder.commonutils.bzptEntity.SysDictitem;
import java.util.List;
/**
* <p>
* 系统字典信息 服务类
* </p>
*
* @author yangyang
* @since 2020-07-09
*/
public interface SysDictitemService extends IService<SysDictitem> {
List<DictItem> queryTreeDataByType(String type, String startId);
void refreCaCheCode(String type);
List<DictItem> queryCatList(String type);
String getNameByCode(String groupId, String code);
}
package com.founder.bzpt.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.founder.bzpt.mapper.SysDictitemMapper;
import com.founder.bzpt.service.SysDictitemService;
import com.founder.commonutils.bzptEntity.DictItem;
import com.founder.commonutils.bzptEntity.SysDictitem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
* <p>
* 系统字典信息 服务实现类
* </p>
*
* @author yangyang
* @since 2021-07-09
*/
@Service
public class SysDictitemServiceImpl extends
ServiceImpl<SysDictitemMapper, SysDictitem> implements SysDictitemService {
@Autowired
private SysDictitemMapper dictitemMapper;
@Value("${ssdw}")
private String ssdw;
@Autowired
private StringRedisTemplate redisTemplate;
@Override
public List<DictItem> queryTreeDataByType(String type, String startId) {
List<DictItem> list = new ArrayList<>();
//获取符合条件的key
String keys = ssdw.substring(0, 2) + ":" + type;
Set<String> set = redisTemplate.keys(keys);
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String str = iterator.next();
list = JSON.parseArray(redisTemplate.opsForValue().get(str), DictItem.class);
}
if (StringUtils.isNotBlank(startId)) {
Iterator<DictItem> iterators = list.iterator();
while (iterators.hasNext()) {
if (!iterators.next().getId().startsWith(startId)) {
iterators.remove();
}
}
}
return list;
}
@Override
public void refreCaCheCode(String type) {
if (StringUtils.isNotBlank(type) && !"all".equals(type)) {
addRedisCaChe(type);
} else if ("all".equals(type)) {
// 获取全部groupid
List<String> typeList = dictitemMapper.getAllGroupid();
for (String types : typeList) {
addRedisCaChe(types);
}
}
}
private void addRedisCaChe(String type) {
List<DictItem> list = queryCatList(type);
String key = ssdw.substring(0, 2) + ":" + type;
//先删除指定key缓存,再加入缓存
Set<String> dictitemKeys = redisTemplate.keys(key);
redisTemplate.delete(dictitemKeys);
// 设置页面所有要展示的元素
redisTemplate.opsForValue().set(key, JSON.toJSONString(list,
SerializerFeature.WRITE_MAP_NULL_FEATURES));
}
/**
* 获取所有分类
*
* @return
*/
@Override
public List<DictItem> queryCatList(String type) {
// 用boot获取分类的数据 selectAll在springboot中是获取数据表里的所有数据
QueryWrapper<SysDictitem> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("SCBZ", "0");
queryWrapper.eq("GROUPID", type);
queryWrapper.select("PID", "CODE", "NAME");
queryWrapper.orderByAsc("CODE");
List<SysDictitem> data = dictitemMapper.selectList(queryWrapper);
//定义新的list
List<DictItem> dictItemList = new ArrayList<>();
//先找到所有的一级分类
for (SysDictitem sysDictitem : data) {
if (StringUtils.isBlank(sysDictitem.getPid())) {
DictItem dictItem = new DictItem();
dictItem.setId(sysDictitem.getCode());
dictItem.setLabel(sysDictitem.getName() + "[" +
sysDictitem.getCode() + "]");
dictItem.setParentId(sysDictitem.getPid());
dictItemList.add(dictItem);
} else {
DictItem dictItem = new DictItem();
dictItem.setId(sysDictitem.getCode());
dictItem.setLabel(sysDictitem.getName() + "[" +
sysDictitem.getCode() + "]");
dictItem.setParentId(sysDictitem.getPid());
dictItemList.add(dictItem);
}
}
// 为一级菜单设置子菜单,getChild是递归调用的
for (DictItem dictItem : dictItemList) {
if (StringUtils.isNotBlank(dictItem.getId())) {
dictItem.setChildren(getChilde(dictItem.getId(), data));
}
}
return dictItemList;
}
@Override
public String getNameByCode(String groupId, String code) {
QueryWrapper<SysDictitem> wrapper = new QueryWrapper<>();
wrapper.select("NAME");
wrapper.eq("SCBZ", "0");
wrapper.eq("CODE", code);
wrapper.eq("GROUPID", groupId);
SysDictitem sysDictitem = dictitemMapper.selectOne(wrapper);
if (sysDictitem != null) {
return sysDictitem.getName();
}
return "";
}
/**
* 递归查找子菜单
*
* @param id 当前菜单id
* @param rootList 要查找的列表
* @return
*/
private List<DictItem> getChilde(String id, List<SysDictitem> rootList) {
//子菜单
List<DictItem> childList = new ArrayList<>();
for (SysDictitem sysDictitem : rootList) {
// 遍历所有节点,将父菜单id与传过来的id比较
if (id.equals(sysDictitem.getPid())) {
DictItem dictItem = new DictItem();
dictItem.setId(sysDictitem.getCode());
dictItem.setLabel(sysDictitem.getName() + "[" +
sysDictitem.getCode() + "]");
dictItem.setParentId(sysDictitem.getPid());
childList.add(dictItem);
}
}
// 把子菜单的子菜单再循环一遍
for (DictItem dictItem : childList) {
dictItem.setChildren(getChilde(dictItem.getId(), rootList));
}
// 递归退出条件
if (childList.size() == 0) {
return null;
}
return childList;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment