|
@@ -0,0 +1,196 @@
|
|
|
+package com.sooka.jnb.quotations.service.impl;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+import com.ruoyi.common.utils.DateUtils;
|
|
|
+import com.ruoyi.common.utils.SecurityUtils;
|
|
|
+import com.sooka.jnb.quotations.domain.JnbQuotationsConfig;
|
|
|
+import com.sooka.jnb.quotations.domain.vo.JnbQuotationsTreeVO;
|
|
|
+import com.sooka.jnb.quotations.mapper.JnbQuotationsConfigMapper;
|
|
|
+import com.sooka.jnb.quotations.service.IJnbQuotationsConfigService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 行情产品配置Service业务层处理
|
|
|
+ *
|
|
|
+ * @author ruoyi
|
|
|
+ * @date 2024-03-01
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class JnbQuotationsConfigServiceImpl implements IJnbQuotationsConfigService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private JnbQuotationsConfigMapper jnbQuotationsConfigMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate redisTemplate;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询行情产品配置
|
|
|
+ *
|
|
|
+ * @param id 行情产品配置主键
|
|
|
+ * @return 行情产品配置
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public JnbQuotationsConfig selectJnbQuotationsConfigById(Long id) {
|
|
|
+ return jnbQuotationsConfigMapper.selectJnbQuotationsConfigById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 三级分类树
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public JnbQuotationsTreeVO<JnbQuotationsConfig> quotationsTree(JnbQuotationsConfig jnbQuotations) {
|
|
|
+// //方法开始,先检查redis中是否包含这个key
|
|
|
+// if (Boolean.TRUE.equals(redisTemplate.hasKey(QUOTATIONS_TREE))) {
|
|
|
+// //如果包含,直接从redis中获取并返回
|
|
|
+// return (JnbQuotationsTreeVO<JnbQuotationsConfig>) redisTemplate.opsForValue().get(QUOTATIONS_TREE);
|
|
|
+// }
|
|
|
+ List<JnbQuotationsConfig> jnbQuotationsConfigs = jnbQuotationsConfigMapper.selectAllQuotationsConfig(jnbQuotations);
|
|
|
+ JnbQuotationsTreeVO<JnbQuotationsConfig> jnbQuotationsConfig = initTree(jnbQuotationsConfigs);
|
|
|
+// redisTemplate.boundValueOps(QUOTATIONS_TREE).set(jnbQuotationsConfig, 1, TimeUnit.HOURS);
|
|
|
+ return jnbQuotationsConfig;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 树结构生成
|
|
|
+ * @param jnbQuotationsConfigs
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private JnbQuotationsTreeVO initTree(List<JnbQuotationsConfig> jnbQuotationsConfigs) {
|
|
|
+ //创建一个map,map中使用父分类id作为key,遍历当前参数集合,将相同父分类id的对象存在一个key下
|
|
|
+ Map<Long, List<JnbQuotationsConfig>> map = new HashMap<>();
|
|
|
+ //遍历数据库中查询出来的所有分类对象
|
|
|
+ for (JnbQuotationsConfig jnbQuotationsConfig : jnbQuotationsConfigs) {
|
|
|
+ //因为后面会多次用到当前对象的父分类id,所以先取出来
|
|
|
+ Long parentId = jnbQuotationsConfig.getParentId();
|
|
|
+ //判断当前map中是否存在这个父分类id的key
|
|
|
+ if (map.containsKey(parentId)) {
|
|
|
+ //如果已经存在,直接将当前对象添加到这个key对应的list中
|
|
|
+ map.get(parentId).add(jnbQuotationsConfig);
|
|
|
+ } else {
|
|
|
+ //如果当前map中还没有这个key,就创建一个新的key-value元素
|
|
|
+ // 要先把value准备好,既实例化list,并将分类对象添加到集合中
|
|
|
+ List<JnbQuotationsConfig> list = new ArrayList<>();
|
|
|
+ list.add(jnbQuotationsConfig);
|
|
|
+ // 然后在map中新增元素
|
|
|
+ map.put(parentId, list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //构建三级分类树,将子分类集合添加到对应的父分类对象的children属性中
|
|
|
+ List<JnbQuotationsConfig> firstLevels = map.get(0L);
|
|
|
+ //从一级分类开始,我们程序设计父分类为0的就是一级分类
|
|
|
+ if (firstLevels == null || firstLevels.isEmpty()) {
|
|
|
+ //判断fristLevels是否为null(或元素个数为0)
|
|
|
+ //必须先判空,否则容易出现空指针异常
|
|
|
+ throw new RuntimeException("行情产品配置数据异常");
|
|
|
+ }
|
|
|
+ for (JnbQuotationsConfig firstLevel : firstLevels) {
|
|
|
+ // 一级分类对象的id就是二级分类对象的父分类id
|
|
|
+ Long secondLevelParentId = firstLevel.getId();
|
|
|
+ // 根据二级分类的父分类id,获取当前分类包含的二级分类集合
|
|
|
+ List<JnbQuotationsConfig> secondLevels = map.get(secondLevelParentId);
|
|
|
+ //判断二级分类集合是否为null(或元素个数为0)
|
|
|
+ if (secondLevels == null || secondLevels.isEmpty()) {
|
|
|
+ //二级分类集合如果为null,不需要抛异常,只需在日志中输出警告即可
|
|
|
+ log.warn("{}号分类对象的二级分类没有内容", secondLevelParentId);
|
|
|
+ // 跳过当前循环,继续下一次循环
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //二级分类铀元素,就便利二级分类对象
|
|
|
+ for (JnbQuotationsConfig secondLevel : secondLevels) {
|
|
|
+ Long thirdLevelParentId = secondLevel.getId(); //getId!!!!!
|
|
|
+ //根据三级分类的父分类id获取三级分类集合
|
|
|
+ List<JnbQuotationsConfig> thirdLevels = map.get(thirdLevelParentId);
|
|
|
+ //判断三级分类集合是否为null(或元素个数为0)
|
|
|
+ if (thirdLevels == null || thirdLevels.isEmpty()) {
|
|
|
+ //三级分类集合如果为null,不需要抛日常,只需在日志中输出警告即可
|
|
|
+ log.warn("{}号分类对象的三级分类没有内容", thirdLevelParentId);
|
|
|
+ // 跳过当前循环,继续下一次循环
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 将包含内容的三级分类集合赋值到二级分类对象的children属性中
|
|
|
+ secondLevel.setChildren(thirdLevels);
|
|
|
+ }
|
|
|
+ //内层循环结束,secondLevels中的每个元素,就都包含他的所有子分类对象了
|
|
|
+ // secondLevels赋值到一级分类对象oneLevel的children属性中
|
|
|
+ firstLevel.setChildren(secondLevels);
|
|
|
+ }
|
|
|
+ //循环结束后,所有的分类对象都包含自己对应的子分类集合了
|
|
|
+ //最后需要实例化FrontCategoryTreeVO对象,将firstLevels赋值给quotations属性
|
|
|
+ JnbQuotationsTreeVO jnbQuotationsTreeVO = new JnbQuotationsTreeVO();
|
|
|
+ jnbQuotationsTreeVO.setQuotations(firstLevels);
|
|
|
+ return jnbQuotationsTreeVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询行情产品配置列表
|
|
|
+ *
|
|
|
+ * @param jnbQuotationsConfig 行情产品配置
|
|
|
+ * @return 行情产品配置
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<JnbQuotationsConfig> selectJnbQuotationsConfigList(JnbQuotationsConfig jnbQuotationsConfig) {
|
|
|
+ return jnbQuotationsConfigMapper.selectJnbQuotationsConfigList(jnbQuotationsConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增行情产品配置
|
|
|
+ *
|
|
|
+ * @param jnbQuotationsConfig 行情产品配置
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int insertJnbQuotationsConfig(JnbQuotationsConfig jnbQuotationsConfig) {
|
|
|
+ int i = jnbQuotationsConfigMapper.selectCount(jnbQuotationsConfig);
|
|
|
+ if (i > 0) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ jnbQuotationsConfig.setCreateBy(SecurityUtils.getUsername());
|
|
|
+ jnbQuotationsConfig.setCreateTime(DateUtils.getNowDate());
|
|
|
+ return jnbQuotationsConfigMapper.insertJnbQuotationsConfig(jnbQuotationsConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改行情产品配置
|
|
|
+ *
|
|
|
+ * @param jnbQuotationsConfig 行情产品配置
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int updateJnbQuotationsConfig(JnbQuotationsConfig jnbQuotationsConfig) {
|
|
|
+ int i = jnbQuotationsConfigMapper.selectCount(jnbQuotationsConfig);
|
|
|
+ if (i > 0) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ jnbQuotationsConfig.setUpdateBy(SecurityUtils.getUsername());
|
|
|
+ jnbQuotationsConfig.setUpdateTime(DateUtils.getNowDate());
|
|
|
+ return jnbQuotationsConfigMapper.updateJnbQuotationsConfig(jnbQuotationsConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量删除行情产品配置
|
|
|
+ *
|
|
|
+ * @param ids 需要删除的行情产品配置主键
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int deleteJnbQuotationsConfigByIds(Long id) {
|
|
|
+ int i = jnbQuotationsConfigMapper.selectJnbQuotationsConfigByParentId(id);
|
|
|
+ if (i > 0) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ return jnbQuotationsConfigMapper.deleteJnbQuotationsConfigByIds(id);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|