PersonFieldService.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package cn.cslg.pas.service.common;
  2. import cn.cslg.pas.common.core.base.RedisConf;
  3. import cn.cslg.pas.common.model.cronModel.PersonnelVO;
  4. import cn.cslg.pas.common.utils.*;
  5. import cn.cslg.pas.common.dto.AddSelfFieldDTO;
  6. import cn.cslg.pas.common.vo.PersonSelfFieldVO;
  7. import cn.cslg.pas.exception.UnLoginException;
  8. import cn.cslg.pas.exception.XiaoShiException;
  9. import cn.cslg.pas.service.business.CommonService;
  10. import cn.hutool.crypto.SecureUtil;
  11. import com.alibaba.fastjson.JSON;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Service;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import java.util.stream.Collectors;
  17. @Service
  18. public class PersonFieldService {
  19. @Autowired
  20. private RedisUtil redisUtil;
  21. @Autowired
  22. private CacheUtils cacheUtils;
  23. @Autowired
  24. private LoginUtils loginUtils;
  25. public List<PersonSelfFieldVO> getCustomField(String tableName) {
  26. //根据登录人id和type查询
  27. PersonnelVO personnelVO = new PersonnelVO();
  28. try {
  29. personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  30. } catch (Exception e) {
  31. throw new UnLoginException("未登录");
  32. }
  33. String userId = personnelVO.getId();
  34. String key = SecureUtil.md5(tableName + RedisConf.SYMBOL_COLON + userId);
  35. String json = redisUtil.get(RedisConf.USER_FIELD + RedisConf.SYMBOL_COLON + key);
  36. //如果查到
  37. if (StringUtils.isNotEmpty(json)) {
  38. return JsonUtils.jsonToList(json, PersonSelfFieldVO.class);
  39. }
  40. String fieldJson = "";
  41. //如果没查询到,则获取所有栏位添加到redis里
  42. try {
  43. fieldJson = CommonService.readJsonFile(tableName + ".json");
  44. } catch (Exception e) {
  45. throw new XiaoShiException("不存在此表");
  46. }
  47. List<PersonSelfFieldVO> fieldVOS = JSON.parseArray(fieldJson, PersonSelfFieldVO.class);
  48. if (fieldVOS == null) {
  49. throw new XiaoShiException("不存在此表");
  50. }
  51. fieldVOS = fieldVOS.stream().filter(item -> item.getIfShow().equals(true)).collect(Collectors.toList());
  52. if (fieldVOS == null || fieldVOS.size() == 0) {
  53. throw new XiaoShiException("表中无字段");
  54. }
  55. fieldVOS.forEach(item->{item.setCreateType(1);});
  56. //装载顺序
  57. Integer order = 0;
  58. for (PersonSelfFieldVO item : fieldVOS) {
  59. item.setOrder(order);
  60. if(item.getDefaultHidden()!=null&&item.getDefaultHidden().equals(true))
  61. { item.setIfHidden(true);}
  62. else{
  63. item.setIfHidden(false);}
  64. order++;
  65. }
  66. //保存到redis
  67. redisUtil.set(RedisConf.USER_FIELD + RedisConf.SYMBOL_COLON + key, JsonUtils.objectToJson(fieldVOS));
  68. return fieldVOS;
  69. }
  70. public List<PersonSelfFieldVO> setCustomField(AddSelfFieldDTO addSelfFieldDTO) {
  71. PersonnelVO personnelVO = new PersonnelVO();
  72. try {
  73. personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  74. } catch (Exception e) {
  75. throw new UnLoginException("未登录");
  76. }
  77. String userId = personnelVO.getId();
  78. String key = SecureUtil.md5(addSelfFieldDTO.getTableName() + RedisConf.SYMBOL_COLON + userId);
  79. String fieldJson = CommonService.readJsonFile(addSelfFieldDTO.getTableName() + ".json");
  80. List<PersonSelfFieldVO> fieldVOS = JSON.parseArray(fieldJson, PersonSelfFieldVO.class);
  81. fieldVOS = fieldVOS.stream().filter(item -> item.getIfShow().equals(true)).collect(Collectors.toList());
  82. for(PersonSelfFieldVO item:fieldVOS){
  83. PersonSelfFieldVO temVO= addSelfFieldDTO.getValue().stream().filter(tem->tem.getValue().equals(item.getValue())).findFirst().orElse(null);
  84. if(temVO==null){
  85. item.setIfHidden(true);
  86. }
  87. else {
  88. item.setIfHidden(temVO.getIfHidden());
  89. }
  90. }
  91. Integer order = 0;
  92. for (PersonSelfFieldVO item : fieldVOS) {
  93. item.setOrder(order);
  94. order++;
  95. }
  96. redisUtil.set(RedisConf.USER_FIELD + RedisConf.SYMBOL_COLON + key, JsonUtils.objectToJson(fieldVOS));
  97. return fieldVOS;
  98. }
  99. }