Explorar el Código

feat(business): 增加票赛奖项处理功能

- 在 Items 模型中添加 itemJson 和 itemTypeCode 字段
- 在 ItemsBo 和 ItemsVo 中添加相关字段和方法
- 实现票赛奖项的解析和生成逻辑- 更新数据库映射和查询语句
fugui001 hace 3 meses
padre
commit
5c89786afb

+ 4 - 0
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/business/domain/Items.java

@@ -56,4 +56,8 @@ public class Items extends BaseEntity {
 
     private Long itemValue;
 
+    private String itemJson;
+
+    private String itemTypeCode;
+
 }

+ 9 - 0
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/business/domain/bo/ItemsBo.java

@@ -1,6 +1,7 @@
 package org.dromara.business.domain.bo;
 
 import org.dromara.business.domain.Items;
+import org.dromara.business.domain.dto.ItemsPrizeDto;
 import org.dromara.common.mybatis.core.domain.BaseEntity;
 import org.dromara.common.core.validate.AddGroup;
 import org.dromara.common.core.validate.EditGroup;
@@ -9,6 +10,7 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import jakarta.validation.constraints.*;
 import java.util.Date;
+import java.util.List;
 
 /**
  * 道具业务对象 items
@@ -62,4 +64,11 @@ public class ItemsBo extends BaseEntity {
 
 
     private Long itemValue;
+
+
+    private List<ItemsPrizeDto> itemsPrizeList;
+
+    private String itemTypeCode;
+
+
 }

+ 9 - 0
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/business/domain/vo/ItemsVo.java

@@ -6,8 +6,11 @@ import cn.idev.excel.annotation.ExcelProperty;
 import org.dromara.business.domain.Items;
 import io.github.linpeilie.annotations.AutoMapper;
 import lombok.Data;
+import org.dromara.business.domain.dto.ItemsPrizeDto;
+
 import java.io.Serial;
 import java.io.Serializable;
+import java.util.List;
 
 
 /**
@@ -67,4 +70,10 @@ public class ItemsVo implements Serializable {
     private Date updatedAt;
 
 
+    private String itemJson;
+
+    private String itemTypeCode;
+
+    private List<ItemsPrizeDto> itemsPrizeList;
+
 }

+ 134 - 1
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/business/service/impl/ItemsServiceImpl.java

@@ -1,7 +1,12 @@
 package org.dromara.business.service.impl;
 
+import cn.hutool.json.JSONObject;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import org.dromara.business.domain.Items;
 import org.dromara.business.domain.bo.ItemsBo;
+import org.dromara.business.domain.dto.ItemsPrizeDto;
 import org.dromara.business.domain.vo.ItemsVo;
 import org.dromara.business.mapper.ItemsMapper;
 import org.dromara.business.service.IItemsService;
@@ -17,6 +22,8 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
 
+import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Collection;
@@ -34,6 +41,8 @@ public class ItemsServiceImpl implements IItemsService {
 
     private final ItemsMapper baseMapper;
 
+    // 在类中添加 ObjectMapper 实例
+    private static final ObjectMapper objectMapper = new ObjectMapper();
     /**
      * 查询道具
      *
@@ -42,7 +51,35 @@ public class ItemsServiceImpl implements IItemsService {
      */
     @Override
     public ItemsVo queryById(Long id){
-        return baseMapper.selectVoByIdInfo(id);
+        ItemsVo itemsVo = baseMapper.selectVoByIdInfo(id);
+        if(itemsVo!=null){
+            String itemJson = itemsVo.getItemJson();
+            if(StringUtils.isNotBlank(itemJson)){
+                List<ItemsPrizeDto> itemsPrizeList=new ArrayList<>();
+                try {
+                    // 将 JSON 字符串解析为 JsonNode 树结构
+                    JsonNode rootNode = objectMapper.readTree(itemJson);
+                    // 确保是数组
+                    if (rootNode.isArray()) {
+                        for (JsonNode node : rootNode) {
+                            ItemsPrizeDto itemsPrizeDto = new ItemsPrizeDto();
+                            // 提取 id 和 quantity
+                            Long id2 = node.get("id").asLong();           // 自动转为 long
+                            Long quantity = node.get("quantity").asLong();
+                            itemsPrizeDto.setItemId(id2);
+                            itemsPrizeDto.setQuantity(quantity);
+                            itemsPrizeList.add(itemsPrizeDto);
+                        }
+                    }
+                    itemsVo.setItemsPrizeList(itemsPrizeList);
+                } catch (IOException e) {
+                    e.printStackTrace();
+                    // 处理解析异常(格式错误等)
+                }
+            }
+        }
+
+        return itemsVo;
     }
 
     /**
@@ -93,6 +130,54 @@ public class ItemsServiceImpl implements IItemsService {
     public Boolean insertByBo(ItemsBo bo) {
         Items add = MapstructUtils.convert(bo, Items.class);
         validEntityBeforeSave(add);
+        //todo如果是票赛
+        if(bo.getItemTypeCode().equals("2")){
+            List<ItemsPrizeDto> itemsPrizeList = bo.getItemsPrizeList();
+
+            StringBuilder result = new StringBuilder();
+            Long totalValue = 0L; // 用于累加 itemValue * quantity
+            ArrayList itemsPrizeList1 = new ArrayList<>();
+            for (int i = 0; i < itemsPrizeList.size(); i++) {
+                ItemsPrizeDto dto = itemsPrizeList.get(i);
+                Long quantity = dto.getQuantity();
+                Long itemId = dto.getItemId();
+                // ====== 判断条件:根据 quantity 或 itemId 跳过某些项 ======
+                if (quantity == null || quantity <= 0) {
+                    continue; // 数量不合法,跳过,执行下一次循环
+                }
+                if (itemId == null || itemId <= 0) {
+                    continue; // itemId 不合法,跳过
+                }
+                ItemsVo itemsVo = baseMapper.selectVoByIdInfo(itemId);
+                String itemName = itemsVo.getName();
+                Long itemValue = itemsVo.getItemValue(); // 每个物品的单位价值
+
+                // 拼接名称*数量
+                if (i > 0) {
+                    result.append("+");
+                }
+                result.append(itemName).append("*").append(quantity);
+
+                // 累加总价值:itemValue × quantity
+                totalValue += itemValue * quantity;
+
+                JSONObject jsonObject = new JSONObject();
+                jsonObject.put("id", itemId);
+                jsonObject.put("quantity", quantity);
+                itemsPrizeList1.add(jsonObject);
+            }
+            try {
+                String jsonString = objectMapper.writeValueAsString(itemsPrizeList1);
+                add.setItemJson(jsonString);
+            } catch (JsonProcessingException e) {
+                throw new RuntimeException(e);
+            }
+            // 设置结果
+            String itemDesc = result.toString();
+            add.setItemDesc(itemDesc);         // 如:三湘杯资格卡*1+高级参赛卡*6
+            add.setItemValue(totalValue);     // 假设 add 对象有 setTotalValue 方法,存储总价值
+            add.setItemTypeCode(bo.getItemTypeCode());
+       }
         boolean flag = baseMapper.insertItems(add) > 0;
         if (flag) {
             bo.setId(add.getId());
@@ -110,6 +195,54 @@ public class ItemsServiceImpl implements IItemsService {
     public Boolean updateByBo(ItemsBo bo) {
         Items update = MapstructUtils.convert(bo, Items.class);
         validEntityBeforeSave(update);
+        //todo如果是票赛
+        if(bo.getItemTypeCode().equals("2")){
+            List<ItemsPrizeDto> itemsPrizeList = bo.getItemsPrizeList();
+
+            StringBuilder result = new StringBuilder();
+            Long totalValue = 0L; // 用于累加 itemValue * quantity
+            ArrayList itemsPrizeList1 = new ArrayList<>();
+            for (int i = 0; i < itemsPrizeList.size(); i++) {
+                ItemsPrizeDto dto = itemsPrizeList.get(i);
+                Long quantity = dto.getQuantity();
+                Long itemId = dto.getItemId();
+                // ====== 判断条件:根据 quantity 或 itemId 跳过某些项 ======
+                if (quantity == null || quantity <= 0) {
+                    continue; // 数量不合法,跳过,执行下一次循环
+                }
+                if (itemId == null || itemId <= 0) {
+                    continue; // itemId 不合法,跳过
+                }
+                ItemsVo itemsVo = baseMapper.selectVoByIdInfo(itemId);
+                String itemName = itemsVo.getName();
+                Long itemValue = itemsVo.getItemValue(); // 每个物品的单位价值
+
+                // 拼接名称*数量
+                if (i > 0) {
+                    result.append("+");
+                }
+                result.append(itemName).append("*").append(quantity);
+
+                // 累加总价值:itemValue × quantity
+                totalValue += itemValue * quantity;
+
+                JSONObject jsonObject = new JSONObject();
+                jsonObject.put("id", itemId);
+                jsonObject.put("quantity", quantity);
+                itemsPrizeList1.add(jsonObject);
+            }
+            try {
+                String jsonString = objectMapper.writeValueAsString(itemsPrizeList1);
+                update.setItemJson(jsonString);
+            } catch (JsonProcessingException e) {
+                throw new RuntimeException(e);
+            }
+            // 设置结果
+            String itemDesc = result.toString();
+            update.setItemDesc(itemDesc);         // 如:三湘杯资格卡*1+高级参赛卡*6
+            update.setItemValue(totalValue);     // 假设 add 对象有 setTotalValue 方法,存储总价值
+        }
+        update.setItemTypeCode(bo.getItemTypeCode());
         return baseMapper.updateItemsById(update) > 0;
     }
 

+ 11 - 3
ruoyi-modules/ruoyi-system/src/main/resources/mapper/business/ItemsMapper.xml

@@ -6,17 +6,17 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 
 
     <select id="selectVoPage" resultType="org.dromara.business.domain.vo.ItemsVo">
-        SELECT id, name, item_type,item_desc, created_at, updated_at,item_value
+        SELECT id, name, item_type,item_desc, created_at, updated_at,item_value,item_json,item_type_code
         FROM items ${ew.customSqlSegment}
     </select>
 
 
     <select id="selectItemsList" resultType="org.dromara.business.domain.vo.ItemsVo">
-        SELECT  id, name, item_type,item_desc, created_at, updated_at,item_value FROM items  ${ew.customSqlSegment}
+        SELECT  id, name, item_type,item_desc, created_at, updated_at,item_value,item_json,item_type_code FROM items  ${ew.customSqlSegment}
     </select>
 
     <select id="selectVoByIdInfo" resultType="org.dromara.business.domain.vo.ItemsVo">
-        SELECT  id, name, item_type,item_desc, created_at, updated_at,item_value FROM items WHERE id =  #{id}
+        SELECT  id, name, item_type,item_desc, created_at, updated_at,item_value,item_json,item_type_code FROM items WHERE id =  #{id}
     </select>
 
     <update id="updateItemsById">
@@ -34,6 +34,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="itemValue != null and itemValue != ''">
                 item_value = #{itemValue},
             </if>
+            item_json = #{itemJson},
+            <if test="itemTypeCode != null and itemTypeCode != ''">
+                item_type_code = #{itemTypeCode},
+            </if>
             updated_at = NOW()
         </set>
         WHERE id = #{id}
@@ -47,12 +51,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="itemType != null and itemType != ''">item_type,</if>
             <if test="itemDesc != null and itemDesc != ''">item_desc,</if>
             <if test="itemValue != null and itemValue != ''">item_value,</if>
+            <if test="itemJson != null and itemJson != ''">item_json,</if>
+            <if test="itemTypeCode != null and itemTypeCode != ''">item_type_code,</if>
         </trim>
         <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
             <if test="name != null and name != ''">#{name},</if>
             <if test="itemType != null and itemType != ''">#{itemType},</if>
             <if test="itemDesc != null and itemDesc != ''">#{itemDesc},</if>
             <if test="itemValue != null and itemValue != ''">#{itemValue},</if>
+            <if test="itemJson != null and itemJson != ''">#{itemJson},</if>
+            <if test="itemTypeCode != null and itemTypeCode != ''">#{itemTypeCode},</if>
         </trim>
     </insert>