package cn.cslg.pas.service.common; import cn.cslg.pas.common.core.base.RedisConf; import cn.cslg.pas.common.model.cronModel.PersonnelVO; import cn.cslg.pas.common.utils.*; import cn.cslg.pas.common.dto.AddSelfFieldDTO; import cn.cslg.pas.common.vo.PersonSelfFieldVO; import cn.cslg.pas.exception.UnLoginException; import cn.cslg.pas.exception.XiaoShiException; import cn.cslg.pas.service.business.CommonService; import cn.hutool.crypto.SecureUtil; import com.alibaba.fastjson.JSON; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @Service public class PersonFieldService { @Autowired private RedisUtil redisUtil; @Autowired private CacheUtils cacheUtils; @Autowired private LoginUtils loginUtils; public List getCustomField(String tableName) { //根据登录人id和type查询 PersonnelVO personnelVO = new PersonnelVO(); try { personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); } catch (Exception e) { throw new UnLoginException("未登录"); } String userId = personnelVO.getId(); String key = SecureUtil.md5(tableName + RedisConf.SYMBOL_COLON + userId); String json = redisUtil.get(RedisConf.USER_FIELD + RedisConf.SYMBOL_COLON + key); //如果查到 if (StringUtils.isNotEmpty(json)) { return JsonUtils.jsonToList(json, PersonSelfFieldVO.class); } String fieldJson = ""; //如果没查询到,则获取所有栏位添加到redis里 try { fieldJson = CommonService.readJsonFile(tableName + ".json"); } catch (Exception e) { throw new XiaoShiException("不存在此表"); } List fieldVOS = JSON.parseArray(fieldJson, PersonSelfFieldVO.class); if (fieldVOS == null) { throw new XiaoShiException("不存在此表"); } fieldVOS = fieldVOS.stream().filter(item -> item.getIfShow().equals(true)).collect(Collectors.toList()); if (fieldVOS == null || fieldVOS.size() == 0) { throw new XiaoShiException("表中无字段"); } fieldVOS.forEach(item->{item.setCreateType(1);}); //装载顺序 Integer order = 0; for (PersonSelfFieldVO item : fieldVOS) { item.setOrder(order); if(item.getDefaultHidden()!=null&&item.getDefaultHidden().equals(true)) { item.setIfHidden(true);} else{ item.setIfHidden(false);} order++; } //保存到redis redisUtil.set(RedisConf.USER_FIELD + RedisConf.SYMBOL_COLON + key, JsonUtils.objectToJson(fieldVOS)); return fieldVOS; } public List setCustomField(AddSelfFieldDTO addSelfFieldDTO) { PersonnelVO personnelVO = new PersonnelVO(); try { personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); } catch (Exception e) { throw new UnLoginException("未登录"); } String userId = personnelVO.getId(); String key = SecureUtil.md5(addSelfFieldDTO.getTableName() + RedisConf.SYMBOL_COLON + userId); String fieldJson = CommonService.readJsonFile(addSelfFieldDTO.getTableName() + ".json"); List fieldVOS = JSON.parseArray(fieldJson, PersonSelfFieldVO.class); fieldVOS = fieldVOS.stream().filter(item -> item.getIfShow().equals(true)).collect(Collectors.toList()); for(PersonSelfFieldVO item:fieldVOS){ PersonSelfFieldVO temVO= addSelfFieldDTO.getValue().stream().filter(tem->tem.getValue().equals(item.getValue())).findFirst().orElse(null); if(temVO==null){ item.setIfHidden(true); } else { item.setIfHidden(temVO.getIfHidden()); } } Integer order = 0; for (PersonSelfFieldVO item : fieldVOS) { item.setOrder(order); order++; } redisUtil.set(RedisConf.USER_FIELD + RedisConf.SYMBOL_COLON + key, JsonUtils.objectToJson(fieldVOS)); return fieldVOS; } }