fix(budget): 新增预算编辑接口

This commit is contained in:
han0
2023-12-20 19:14:09 +08:00
parent c9175b929f
commit 964f7042fa
3 changed files with 79 additions and 24 deletions

View File

@@ -22,10 +22,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* <p>
@@ -62,26 +59,8 @@ public class BudgetController {
// 转换查询结果为键值对 材料id,月份 => 价格
var priceMap = pricePublishService.getPriceMapEntryByMaterialIdAndMonth(query);
// 各月份求和
var totalMap = new HashMap<String, Integer>();
var budgetItemList = new ArrayList<BudgetItem>();
for (var item: params.getItems()) {
var meta = new ArrayList<Map<String, Object>>();
for (var month: params.getMonths()) {
var key = item.getName() + month.toString();
var price = priceMap.getOrDefault(key, 0);
meta.add(Map.of(
"month", month,
"total", price * item.getQuantity(),
"price", price
)
);
var total = totalMap.getOrDefault(month.toString(), 0);
totalMap.put(month.toString(), total + price * item.getQuantity());
// 遍历月份 获取各材料各月份总数
}
var budgetItem = new BudgetItem(item, meta);
budgetItemList.add(budgetItem);
}
var totalMap = params.getTotalMap(priceMap);
var budgetItemList = params.getBudgetItemList(priceMap);
// 入库
var budget = new Budget(params, user, totalMap);
budgetService.save(budget);
@@ -138,5 +117,38 @@ public class BudgetController {
return FuHttpResponse.Builder().dataResponse(result).build();
}
@ApiOperation("预算编辑")
@PutMapping("/{id}")
public HttpResponse edit (
@ApiParam("id") @PathVariable String id,
@ApiParam("参数") @RequestBody BudgetCreateRequest params
) throws Exception {
params.check();
var user = StpLoginUserUtil.getLoginUser();
var budget = budgetService.getById(id);
if (budget == null) {
throw new NcHttpException(HttpErrorResponseEnum.BUDGET_NOT_FOUND);
}
// 查询各月份材料价格
LambdaQueryWrapper<PricePublish> query = pricePublishService.getQuery(params);
// 转换查询结果为键值对 材料id,月份 => 价格
var priceMap = pricePublishService.getPriceMapEntryByMaterialIdAndMonth(query);
// 各月份求和
var totalMap = params.getTotalMap(priceMap);
var budgetItemList = params.getBudgetItemList(priceMap);
// 入库
budget.update(params, user, totalMap);
budgetService.updateById(budget);
budgetItemService.remove(new LambdaQueryWrapper<BudgetItem>().eq(BudgetItem::getBudgetId, id));
for (var budgetItem: budgetItemList) {
budgetItem.setBudgetId(budget.getId());
budgetItemService.save(budgetItem);
}
return FuHttpResponse.Builder().dataResponse(budgetItemList).build();
}
// todo 在价格发布创建时补全材料id
}

View File

@@ -74,7 +74,11 @@ public class Budget extends Model<Budget> {
public Budget() {}
public Budget(BudgetCreateRequest params, SaBaseLoginUser user, HashMap<String, Integer> totalMap) {
public Budget(BudgetCreateRequest params, SaBaseLoginUser user, Map<String, Integer> totalMap) {
this.update(params, user, totalMap);
}
public void update(BudgetCreateRequest params, SaBaseLoginUser user, Map<String, Integer> totalMap) {
this.name = params.getName();
this.amount = params.getItems().stream().mapToInt(item -> item.getTotalPrice()).sum();

View File

@@ -3,9 +3,13 @@ package mjkf.xinke.main.model.vo;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import mjkf.xinke.main.model.db.BudgetItem;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
public class BudgetCreateRequest {
@@ -47,4 +51,39 @@ public class BudgetCreateRequest {
@JsonProperty(value = "total_price")
private Integer totalPrice;
}
public Map<String, Integer> getTotalMap(Map<String, Integer> priceMap) {
var params = this;
var totalMap = new HashMap<String, Integer>();
for (var item: params.getItems()) {
for (var month: params.getMonths()) {
var key = item.getName() + month.toString();
var price = priceMap.getOrDefault(key, 0);
var total = totalMap.getOrDefault(month.toString(), 0);
totalMap.put(month.toString(), total + price * item.getQuantity());
}
}
return totalMap;
}
public List<BudgetItem> getBudgetItemList(Map<String, Integer> priceMap) {
var params = this;
var budgetItemList = new ArrayList<BudgetItem>();
for (var item: params.getItems()) {
var meta = new ArrayList<Map<String, Object>>();
for (var month: params.getMonths()) {
var key = item.getName() + month.toString();
var price = priceMap.getOrDefault(key, 0);
meta.add(Map.of(
"month", month,
"total", price * item.getQuantity(),
"price", price
)
);
}
var budgetItem = new BudgetItem(item, meta);
budgetItemList.add(budgetItem);
}
return budgetItemList;
}
}