|
@@ -1,7 +1,12 @@
|
|
|
package org.dromara.business.service.impl;
|
|
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.Items;
|
|
|
import org.dromara.business.domain.bo.ItemsBo;
|
|
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.domain.vo.ItemsVo;
|
|
|
import org.dromara.business.mapper.ItemsMapper;
|
|
import org.dromara.business.mapper.ItemsMapper;
|
|
|
import org.dromara.business.service.IItemsService;
|
|
import org.dromara.business.service.IItemsService;
|
|
@@ -17,6 +22,8 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
@@ -34,6 +41,8 @@ public class ItemsServiceImpl implements IItemsService {
|
|
|
|
|
|
|
|
private final ItemsMapper baseMapper;
|
|
private final ItemsMapper baseMapper;
|
|
|
|
|
|
|
|
|
|
+ // 在类中添加 ObjectMapper 实例
|
|
|
|
|
+ private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
/**
|
|
/**
|
|
|
* 查询道具
|
|
* 查询道具
|
|
|
*
|
|
*
|
|
@@ -42,7 +51,35 @@ public class ItemsServiceImpl implements IItemsService {
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
|
public ItemsVo queryById(Long id){
|
|
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) {
|
|
public Boolean insertByBo(ItemsBo bo) {
|
|
|
Items add = MapstructUtils.convert(bo, Items.class);
|
|
Items add = MapstructUtils.convert(bo, Items.class);
|
|
|
validEntityBeforeSave(add);
|
|
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;
|
|
boolean flag = baseMapper.insertItems(add) > 0;
|
|
|
if (flag) {
|
|
if (flag) {
|
|
|
bo.setId(add.getId());
|
|
bo.setId(add.getId());
|
|
@@ -110,6 +195,54 @@ public class ItemsServiceImpl implements IItemsService {
|
|
|
public Boolean updateByBo(ItemsBo bo) {
|
|
public Boolean updateByBo(ItemsBo bo) {
|
|
|
Items update = MapstructUtils.convert(bo, Items.class);
|
|
Items update = MapstructUtils.convert(bo, Items.class);
|
|
|
validEntityBeforeSave(update);
|
|
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;
|
|
return baseMapper.updateItemsById(update) > 0;
|
|
|
}
|
|
}
|
|
|
|
|
|