成本模块
This commit is contained in:
@@ -0,0 +1,272 @@
|
||||
package com.klp.controller;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Arrays;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.constraints.*;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import com.klp.common.annotation.RepeatSubmit;
|
||||
import com.klp.common.annotation.Log;
|
||||
import com.klp.common.core.controller.BaseController;
|
||||
import com.klp.common.core.domain.PageQuery;
|
||||
import com.klp.common.core.domain.R;
|
||||
import com.klp.common.core.validate.AddGroup;
|
||||
import com.klp.common.core.validate.EditGroup;
|
||||
import com.klp.common.enums.BusinessType;
|
||||
import com.klp.common.utils.poi.ExcelUtil;
|
||||
import com.klp.domain.vo.CostCoilDailyVo;
|
||||
import com.klp.domain.bo.CostCoilDailyBo;
|
||||
import com.klp.service.ICostCoilDailyService;
|
||||
import com.klp.common.core.page.TableDataInfo;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
/**
|
||||
* 钢卷日成本记录表
|
||||
*
|
||||
* @author klp
|
||||
* @date 2025-11-25
|
||||
*/
|
||||
@Validated
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping("/wms/cost/coil")
|
||||
public class CostCoilDailyController extends BaseController {
|
||||
|
||||
private final ICostCoilDailyService iCostCoilDailyService;
|
||||
|
||||
/**
|
||||
* 查询钢卷日成本记录表列表
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
public TableDataInfo<CostCoilDailyVo> list(CostCoilDailyBo bo, PageQuery pageQuery) {
|
||||
return iCostCoilDailyService.queryPageList(bo, pageQuery);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出钢卷日成本记录表列表
|
||||
*/
|
||||
@Log(title = "钢卷日成本记录表", businessType = BusinessType.EXPORT)
|
||||
@PostMapping("/export")
|
||||
public void export(CostCoilDailyBo bo, HttpServletResponse response) {
|
||||
List<CostCoilDailyVo> list = iCostCoilDailyService.queryList(bo);
|
||||
ExcelUtil.exportExcel(list, "钢卷日成本记录表", CostCoilDailyVo.class, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取钢卷日成本记录表详细信息
|
||||
*
|
||||
* @param costId 主键
|
||||
*/
|
||||
@GetMapping("/{costId}")
|
||||
public R<CostCoilDailyVo> getInfo(@NotNull(message = "主键不能为空")
|
||||
@PathVariable Long costId) {
|
||||
return R.ok(iCostCoilDailyService.queryById(costId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增钢卷日成本记录表
|
||||
*/
|
||||
@Log(title = "钢卷日成本记录表", businessType = BusinessType.INSERT)
|
||||
@RepeatSubmit()
|
||||
@PostMapping()
|
||||
public R<Void> add(@Validated(AddGroup.class) @RequestBody CostCoilDailyBo bo) {
|
||||
return toAjax(iCostCoilDailyService.insertByBo(bo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改钢卷日成本记录表
|
||||
*/
|
||||
@Log(title = "钢卷日成本记录表", businessType = BusinessType.UPDATE)
|
||||
@RepeatSubmit()
|
||||
@PutMapping()
|
||||
public R<Void> edit(@Validated(EditGroup.class) @RequestBody CostCoilDailyBo bo) {
|
||||
return toAjax(iCostCoilDailyService.updateByBo(bo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除钢卷日成本记录表
|
||||
*
|
||||
* @param costIds 主键串
|
||||
*/
|
||||
@Log(title = "钢卷日成本记录表", businessType = BusinessType.DELETE)
|
||||
@DeleteMapping("/{costIds}")
|
||||
public R<Void> remove(@NotEmpty(message = "主键不能为空")
|
||||
@PathVariable Long[] costIds) {
|
||||
return toAjax(iCostCoilDailyService.deleteWithValidByIds(Arrays.asList(costIds), true));
|
||||
}
|
||||
|
||||
/**
|
||||
* 实时计算指定钢卷的成本
|
||||
*
|
||||
* @param coilId 钢卷ID(可选)
|
||||
* @param calcTime 计算时间点(可选,默认当前时间)
|
||||
*/
|
||||
@PostMapping("/calculate")
|
||||
public R<Map<String, Object>> calculateCost(@RequestParam(required = false) Long coilId,
|
||||
@RequestParam(required = false) LocalDateTime calcTime) {
|
||||
Map<String, Object> result = iCostCoilDailyService.calculateCost(coilId, calcTime);
|
||||
if (result.containsKey("error")) {
|
||||
return R.fail(result.get("error").toString());
|
||||
}
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量计算多个钢卷的成本
|
||||
*
|
||||
* @param coilIds 钢卷ID列表(支持逗号分隔的字符串或数组)
|
||||
* @param calcTime 计算时间点(可选,默认当前时间)
|
||||
*/
|
||||
@PostMapping("/batchCalculateCost")
|
||||
public R<List<Map<String, Object>>> batchCalculateCost(@RequestParam String coilIds,
|
||||
@RequestParam(required = false) LocalDateTime calcTime) {
|
||||
// 解析coilIds字符串为Long列表
|
||||
List<Long> coilIdList = new java.util.ArrayList<>();
|
||||
if (coilIds != null && !coilIds.trim().isEmpty()) {
|
||||
String[] ids = coilIds.split(",");
|
||||
for (String id : ids) {
|
||||
try {
|
||||
Long coilId = Long.parseLong(id.trim());
|
||||
coilIdList.add(coilId);
|
||||
} catch (NumberFormatException e) {
|
||||
// 忽略无效的ID
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<Map<String, Object>> results = iCostCoilDailyService.batchCalculateCost(coilIdList, calcTime);
|
||||
return R.ok(results);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量计算钢卷成本(定时任务使用)
|
||||
*
|
||||
* @param calcDate 计算日期(可选,默认前一日)
|
||||
*/
|
||||
@PostMapping("/batchCalculate")
|
||||
@Log(title = "批量计算钢卷成本", businessType = BusinessType.OTHER)
|
||||
public R<Integer> batchCalculate(@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate calcDate) {
|
||||
if (calcDate == null) {
|
||||
calcDate = LocalDate.now().minusDays(1);
|
||||
}
|
||||
int count = iCostCoilDailyService.calculateDailyCost(calcDate);
|
||||
return R.ok(count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询成本统计报表
|
||||
*
|
||||
* @param startDate 开始日期
|
||||
* @param endDate 结束日期
|
||||
* @param groupBy 分组维度(warehouse/itemType/materialType)
|
||||
* @param warehouseId 库区ID(可选)
|
||||
*/
|
||||
@GetMapping("/report/summary")
|
||||
public R<Map<String, Object>> queryCostSummary(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
|
||||
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
|
||||
@RequestParam(required = false) String groupBy,
|
||||
@RequestParam(required = false) Long warehouseId) {
|
||||
Map<String, Object> result = iCostCoilDailyService.queryCostSummary(startDate, endDate, groupBy, warehouseId);
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询成本趋势分析
|
||||
*
|
||||
* @param startDate 开始日期
|
||||
* @param endDate 结束日期
|
||||
*/
|
||||
@GetMapping("/report/trend")
|
||||
public R<List<Map<String, Object>>> queryCostTrend(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
|
||||
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
|
||||
List<Map<String, Object>> result = iCostCoilDailyService.queryCostTrend(startDate, endDate);
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按入场钢卷号维度计算成本
|
||||
*
|
||||
* @param enterCoilNo 入场钢卷号
|
||||
* @param calcDate 计算日期(可选,默认当前日期)
|
||||
*/
|
||||
@PostMapping("/calculateByEnterCoilNo")
|
||||
public R<Map<String, Object>> calculateCostByEnterCoilNo(@RequestParam String enterCoilNo,
|
||||
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate calcDate) {
|
||||
Map<String, Object> result = iCostCoilDailyService.calculateCostByEnterCoilNo(enterCoilNo, calcDate);
|
||||
if (result.containsKey("error")) {
|
||||
return R.fail(result.get("error").toString());
|
||||
}
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 现算成本检索(基于 wms_material_coil)
|
||||
*/
|
||||
@GetMapping("/search/material")
|
||||
public R<Map<String, Object>> searchMaterialCost(@RequestParam(required = false) String enterCoilNo,
|
||||
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate calcDate,
|
||||
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
|
||||
@RequestParam(required = false, defaultValue = "20") Integer pageSize) {
|
||||
Map<String, Object> result = iCostCoilDailyService.searchMaterialCost(enterCoilNo, calcDate, pageNum, pageSize);
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量按入场钢卷号维度计算成本(定时任务使用)
|
||||
*
|
||||
* @param calcDate 计算日期(可选,默认前一日)
|
||||
*/
|
||||
@PostMapping("/batchCalculateByEnterCoilNo")
|
||||
@Log(title = "批量按入场钢卷号计算成本", businessType = BusinessType.OTHER)
|
||||
public R<Integer> batchCalculateByEnterCoilNo(@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate calcDate) {
|
||||
if (calcDate == null) {
|
||||
calcDate = LocalDate.now().minusDays(1);
|
||||
}
|
||||
int count = iCostCoilDailyService.calculateDailyCostByEnterCoilNo(calcDate);
|
||||
return R.ok(count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询按入场钢卷号统计的成本报表
|
||||
*
|
||||
* @param startDate 开始日期
|
||||
* @param endDate 结束日期
|
||||
* @param enterCoilNo 入场钢卷号(可选)
|
||||
*/
|
||||
@GetMapping("/report/byEnterCoilNo")
|
||||
public R<List<Map<String, Object>>> queryCostByEnterCoilNo(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
|
||||
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
|
||||
@RequestParam(required = false) String enterCoilNo) {
|
||||
List<Map<String, Object>> result = iCostCoilDailyService.queryCostByEnterCoilNo(startDate, endDate, enterCoilNo);
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 囤积成本页数据(分页 + 汇总)
|
||||
*/
|
||||
@GetMapping("/stockpile")
|
||||
public R<Map<String, Object>> queryStockpile(@RequestParam(required = false) String enterCoilNo,
|
||||
@RequestParam(required = false) String currentCoilNo,
|
||||
PageQuery pageQuery) {
|
||||
Map<String, Object> result = iCostCoilDailyService.queryStockpileCostList(enterCoilNo, currentCoilNo, pageQuery);
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 成本模块首页概览
|
||||
* 统计当前「现存且未发货」钢卷的总成本、总净重、总毛重以及平均在库天数
|
||||
*/
|
||||
@GetMapping("/overview")
|
||||
public R<Map<String, Object>> overview() {
|
||||
Map<String, Object> result = iCostCoilDailyService.queryOverview();
|
||||
return R.ok(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
package com.klp.controller;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Arrays;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.constraints.*;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import com.klp.common.annotation.RepeatSubmit;
|
||||
import com.klp.common.annotation.Log;
|
||||
import com.klp.common.core.controller.BaseController;
|
||||
import com.klp.common.core.domain.PageQuery;
|
||||
import com.klp.common.core.domain.R;
|
||||
import com.klp.common.core.validate.AddGroup;
|
||||
import com.klp.common.core.validate.EditGroup;
|
||||
import com.klp.common.enums.BusinessType;
|
||||
import com.klp.common.utils.poi.ExcelUtil;
|
||||
import com.klp.domain.vo.CostStandardConfigVo;
|
||||
import com.klp.domain.bo.CostStandardConfigBo;
|
||||
import com.klp.service.ICostStandardConfigService;
|
||||
import com.klp.common.core.page.TableDataInfo;
|
||||
|
||||
/**
|
||||
* 成本标准配置表
|
||||
*
|
||||
* @author klp
|
||||
* @date 2025-11-25
|
||||
*/
|
||||
@Validated
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping("/wms/cost/standard")
|
||||
public class CostStandardConfigController extends BaseController {
|
||||
|
||||
private final ICostStandardConfigService iCostStandardConfigService;
|
||||
|
||||
/**
|
||||
* 查询成本标准配置表列表
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
public TableDataInfo<CostStandardConfigVo> list(CostStandardConfigBo bo, PageQuery pageQuery) {
|
||||
return iCostStandardConfigService.queryPageList(bo, pageQuery);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出成本标准配置表列表
|
||||
*/
|
||||
@Log(title = "成本标准配置表", businessType = BusinessType.EXPORT)
|
||||
@PostMapping("/export")
|
||||
public void export(CostStandardConfigBo bo, HttpServletResponse response) {
|
||||
List<CostStandardConfigVo> list = iCostStandardConfigService.queryList(bo);
|
||||
ExcelUtil.exportExcel(list, "成本标准配置表", CostStandardConfigVo.class, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取成本标准配置表详细信息
|
||||
*
|
||||
* @param configId 主键
|
||||
*/
|
||||
@GetMapping("/{configId}")
|
||||
public R<CostStandardConfigVo> getInfo(@NotNull(message = "主键不能为空")
|
||||
@PathVariable Long configId) {
|
||||
return R.ok(iCostStandardConfigService.queryById(configId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询当前有效的成本标准
|
||||
*/
|
||||
@GetMapping("/current")
|
||||
public R<CostStandardConfigVo> getCurrent() {
|
||||
return R.ok(iCostStandardConfigService.queryCurrentEffective());
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增成本标准配置表
|
||||
*/
|
||||
@Log(title = "成本标准配置表", businessType = BusinessType.INSERT)
|
||||
@RepeatSubmit()
|
||||
@PostMapping()
|
||||
public R<Void> add(@Validated(AddGroup.class) @RequestBody CostStandardConfigBo bo) {
|
||||
return toAjax(iCostStandardConfigService.insertByBo(bo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改成本标准配置表
|
||||
*/
|
||||
@Log(title = "成本标准配置表", businessType = BusinessType.UPDATE)
|
||||
@RepeatSubmit()
|
||||
@PutMapping()
|
||||
public R<Void> edit(@Validated(EditGroup.class) @RequestBody CostStandardConfigBo bo) {
|
||||
return toAjax(iCostStandardConfigService.updateByBo(bo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除成本标准配置表
|
||||
*
|
||||
* @param configIds 主键串
|
||||
*/
|
||||
@Log(title = "成本标准配置表", businessType = BusinessType.DELETE)
|
||||
@DeleteMapping("/{configIds}")
|
||||
public R<Void> remove(@NotEmpty(message = "主键不能为空")
|
||||
@PathVariable Long[] configIds) {
|
||||
return toAjax(iCostStandardConfigService.deleteWithValidByIds(Arrays.asList(configIds), true));
|
||||
}
|
||||
}
|
||||
|
||||
85
klp-wms/src/main/java/com/klp/domain/CostCoilDaily.java
Normal file
85
klp-wms/src/main/java/com/klp/domain/CostCoilDaily.java
Normal file
@@ -0,0 +1,85 @@
|
||||
package com.klp.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.klp.common.core.domain.BaseEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* 钢卷日成本记录表对象 cost_coil_daily
|
||||
*
|
||||
* @author klp
|
||||
* @date 2025-11-25
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("cost_coil_daily")
|
||||
public class CostCoilDaily extends BaseEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 主键ID
|
||||
*/
|
||||
@TableId(value = "cost_id")
|
||||
private Long costId;
|
||||
|
||||
/**
|
||||
* 钢卷ID(关联wms_material_coil.coil_id)
|
||||
*/
|
||||
private Long coilId;
|
||||
|
||||
/**
|
||||
* 当前钢卷号
|
||||
*/
|
||||
private String currentCoilNo;
|
||||
|
||||
/**
|
||||
* 计算日期
|
||||
*/
|
||||
private LocalDate calcDate;
|
||||
|
||||
/**
|
||||
* 净重(吨)
|
||||
*/
|
||||
private BigDecimal netWeight;
|
||||
|
||||
/**
|
||||
* 单位成本(元/吨/天)
|
||||
*/
|
||||
private BigDecimal unitCost;
|
||||
|
||||
/**
|
||||
* 日成本(元)
|
||||
*/
|
||||
private BigDecimal dailyCost;
|
||||
|
||||
/**
|
||||
* 累计在库天数
|
||||
*/
|
||||
private Integer storageDays;
|
||||
|
||||
/**
|
||||
* 累计成本(元)
|
||||
*/
|
||||
private BigDecimal totalCost;
|
||||
|
||||
/**
|
||||
* 所在库区ID
|
||||
*/
|
||||
private Long warehouseId;
|
||||
|
||||
/**
|
||||
* 物品类型(raw_material/product)
|
||||
*/
|
||||
private String itemType;
|
||||
|
||||
/**
|
||||
* 材料类型
|
||||
*/
|
||||
private String materialType;
|
||||
}
|
||||
|
||||
61
klp-wms/src/main/java/com/klp/domain/CostStandardConfig.java
Normal file
61
klp-wms/src/main/java/com/klp/domain/CostStandardConfig.java
Normal file
@@ -0,0 +1,61 @@
|
||||
package com.klp.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.klp.common.core.domain.BaseEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* 成本标准配置表对象 cost_standard_config
|
||||
*
|
||||
* @author klp
|
||||
* @date 2025-11-25
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("cost_standard_config")
|
||||
public class CostStandardConfig extends BaseEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 主键ID
|
||||
*/
|
||||
@TableId(value = "config_id")
|
||||
private Long configId;
|
||||
|
||||
/**
|
||||
* 单位成本(元/吨/天)
|
||||
*/
|
||||
private BigDecimal unitCost;
|
||||
|
||||
/**
|
||||
* 生效日期
|
||||
*/
|
||||
private LocalDate effectiveDate;
|
||||
|
||||
/**
|
||||
* 失效日期(NULL表示当前有效)
|
||||
*/
|
||||
private LocalDate expireDate;
|
||||
|
||||
/**
|
||||
* 状态(0=失效,1=有效)
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 删除标志(0=正常,1=已删除)
|
||||
*/
|
||||
@TableLogic
|
||||
private Integer delFlag;
|
||||
|
||||
/**
|
||||
* 备注说明
|
||||
*/
|
||||
private String remark;
|
||||
}
|
||||
|
||||
90
klp-wms/src/main/java/com/klp/domain/bo/CostCoilDailyBo.java
Normal file
90
klp-wms/src/main/java/com/klp/domain/bo/CostCoilDailyBo.java
Normal file
@@ -0,0 +1,90 @@
|
||||
package com.klp.domain.bo;
|
||||
|
||||
import com.klp.common.core.domain.BaseEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* 钢卷日成本记录表业务对象 cost_coil_daily
|
||||
*
|
||||
* @author klp
|
||||
* @date 2025-11-25
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class CostCoilDailyBo extends BaseEntity {
|
||||
|
||||
/**
|
||||
* 主键ID
|
||||
*/
|
||||
private Long costId;
|
||||
|
||||
/**
|
||||
* 钢卷ID(关联wms_material_coil.coil_id)
|
||||
*/
|
||||
private Long coilId;
|
||||
|
||||
/**
|
||||
* 当前钢卷号
|
||||
*/
|
||||
private String currentCoilNo;
|
||||
|
||||
/**
|
||||
* 计算日期
|
||||
*/
|
||||
private LocalDate calcDate;
|
||||
|
||||
/**
|
||||
* 净重(吨)
|
||||
*/
|
||||
private BigDecimal netWeight;
|
||||
|
||||
/**
|
||||
* 单位成本(元/吨/天)
|
||||
*/
|
||||
private BigDecimal unitCost;
|
||||
|
||||
/**
|
||||
* 日成本(元)
|
||||
*/
|
||||
private BigDecimal dailyCost;
|
||||
|
||||
/**
|
||||
* 累计在库天数
|
||||
*/
|
||||
private Integer storageDays;
|
||||
|
||||
/**
|
||||
* 累计成本(元)
|
||||
*/
|
||||
private BigDecimal totalCost;
|
||||
|
||||
/**
|
||||
* 所在库区ID
|
||||
*/
|
||||
private Long warehouseId;
|
||||
|
||||
/**
|
||||
* 物品类型(raw_material/product)
|
||||
*/
|
||||
private String itemType;
|
||||
|
||||
/**
|
||||
* 材料类型
|
||||
*/
|
||||
private String materialType;
|
||||
|
||||
/**
|
||||
* 开始日期(用于查询)
|
||||
*/
|
||||
private LocalDate startDate;
|
||||
|
||||
/**
|
||||
* 结束日期(用于查询)
|
||||
*/
|
||||
private LocalDate endDate;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.klp.domain.bo;
|
||||
|
||||
import com.klp.common.core.domain.BaseEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* 成本标准配置表业务对象 cost_standard_config
|
||||
*
|
||||
* @author klp
|
||||
* @date 2025-11-25
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class CostStandardConfigBo extends BaseEntity {
|
||||
|
||||
/**
|
||||
* 主键ID
|
||||
*/
|
||||
private Long configId;
|
||||
|
||||
/**
|
||||
* 单位成本(元/吨/天)
|
||||
*/
|
||||
private BigDecimal unitCost;
|
||||
|
||||
/**
|
||||
* 生效日期
|
||||
*/
|
||||
private LocalDate effectiveDate;
|
||||
|
||||
/**
|
||||
* 失效日期(NULL表示当前有效)
|
||||
*/
|
||||
private LocalDate expireDate;
|
||||
|
||||
/**
|
||||
* 状态(0=失效,1=有效)
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 备注说明
|
||||
*/
|
||||
private String remark;
|
||||
}
|
||||
|
||||
98
klp-wms/src/main/java/com/klp/domain/vo/CostCoilDailyVo.java
Normal file
98
klp-wms/src/main/java/com/klp/domain/vo/CostCoilDailyVo.java
Normal file
@@ -0,0 +1,98 @@
|
||||
package com.klp.domain.vo;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* 钢卷日成本记录表视图对象 cost_coil_daily
|
||||
*
|
||||
* @author klp
|
||||
* @date 2025-11-25
|
||||
*/
|
||||
@Data
|
||||
@ExcelIgnoreUnannotated
|
||||
public class CostCoilDailyVo {
|
||||
|
||||
/**
|
||||
* 主键ID
|
||||
*/
|
||||
@ExcelProperty(value = "成本记录ID")
|
||||
private Long costId;
|
||||
|
||||
/**
|
||||
* 钢卷ID(关联wms_material_coil.coil_id)
|
||||
*/
|
||||
@ExcelProperty(value = "钢卷ID")
|
||||
private Long coilId;
|
||||
|
||||
/**
|
||||
* 当前钢卷号
|
||||
*/
|
||||
@ExcelProperty(value = "钢卷号")
|
||||
private String currentCoilNo;
|
||||
|
||||
/**
|
||||
* 计算日期
|
||||
*/
|
||||
@ExcelProperty(value = "计算日期")
|
||||
private LocalDate calcDate;
|
||||
|
||||
/**
|
||||
* 净重(吨)
|
||||
*/
|
||||
@ExcelProperty(value = "净重(吨)")
|
||||
private BigDecimal netWeight;
|
||||
|
||||
/**
|
||||
* 单位成本(元/吨/天)
|
||||
*/
|
||||
@ExcelProperty(value = "单位成本(元/吨/天)")
|
||||
private BigDecimal unitCost;
|
||||
|
||||
/**
|
||||
* 日成本(元)
|
||||
*/
|
||||
@ExcelProperty(value = "日成本(元)")
|
||||
private BigDecimal dailyCost;
|
||||
|
||||
/**
|
||||
* 累计在库天数
|
||||
*/
|
||||
@ExcelProperty(value = "累计在库天数")
|
||||
private Integer storageDays;
|
||||
|
||||
/**
|
||||
* 累计成本(元)
|
||||
*/
|
||||
@ExcelProperty(value = "累计成本(元)")
|
||||
private BigDecimal totalCost;
|
||||
|
||||
/**
|
||||
* 所在库区ID
|
||||
*/
|
||||
@ExcelProperty(value = "库区ID")
|
||||
private Long warehouseId;
|
||||
|
||||
/**
|
||||
* 库区名称
|
||||
*/
|
||||
@ExcelProperty(value = "库区名称")
|
||||
private String warehouseName;
|
||||
|
||||
/**
|
||||
* 物品类型(raw_material/product)
|
||||
*/
|
||||
@ExcelProperty(value = "物品类型")
|
||||
private String itemType;
|
||||
|
||||
/**
|
||||
* 材料类型
|
||||
*/
|
||||
@ExcelProperty(value = "材料类型")
|
||||
private String materialType;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.klp.domain.vo;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* 成本标准配置表视图对象 cost_standard_config
|
||||
*
|
||||
* @author klp
|
||||
* @date 2025-11-25
|
||||
*/
|
||||
@Data
|
||||
@ExcelIgnoreUnannotated
|
||||
public class CostStandardConfigVo {
|
||||
|
||||
/**
|
||||
* 主键ID
|
||||
*/
|
||||
@ExcelProperty(value = "配置ID")
|
||||
private Long configId;
|
||||
|
||||
/**
|
||||
* 单位成本(元/吨/天)
|
||||
*/
|
||||
@ExcelProperty(value = "单位成本(元/吨/天)")
|
||||
private BigDecimal unitCost;
|
||||
|
||||
/**
|
||||
* 生效日期
|
||||
*/
|
||||
@ExcelProperty(value = "生效日期")
|
||||
private LocalDate effectiveDate;
|
||||
|
||||
/**
|
||||
* 失效日期(NULL表示当前有效)
|
||||
*/
|
||||
@ExcelProperty(value = "失效日期")
|
||||
private LocalDate expireDate;
|
||||
|
||||
/**
|
||||
* 状态(0=失效,1=有效)
|
||||
*/
|
||||
@ExcelProperty(value = "状态")
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 备注说明
|
||||
*/
|
||||
@ExcelProperty(value = "备注")
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
@ExcelProperty(value = "创建人")
|
||||
private String createBy;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@ExcelProperty(value = "创建时间")
|
||||
private java.util.Date createTime;
|
||||
|
||||
/**
|
||||
* 更新人
|
||||
*/
|
||||
@ExcelProperty(value = "更新人")
|
||||
private String updateBy;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@ExcelProperty(value = "更新时间")
|
||||
private java.util.Date updateTime;
|
||||
}
|
||||
|
||||
150
klp-wms/src/main/java/com/klp/mapper/CostCoilDailyMapper.java
Normal file
150
klp-wms/src/main/java/com/klp/mapper/CostCoilDailyMapper.java
Normal file
@@ -0,0 +1,150 @@
|
||||
package com.klp.mapper;
|
||||
|
||||
import com.klp.common.core.mapper.BaseMapperPlus;
|
||||
import com.klp.domain.CostCoilDaily;
|
||||
import com.klp.domain.vo.CostCoilDailyVo;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 钢卷日成本记录表Mapper接口
|
||||
*
|
||||
* @author klp
|
||||
* @date 2025-11-25
|
||||
*/
|
||||
public interface CostCoilDailyMapper extends BaseMapperPlus<CostCoilDailyMapper, CostCoilDaily, CostCoilDailyVo> {
|
||||
|
||||
/**
|
||||
* 查询成本统计汇总
|
||||
*
|
||||
* @param startDate 开始日期
|
||||
* @param endDate 结束日期
|
||||
* @param warehouseId 库区ID(可选)
|
||||
* @param itemType 物品类型(可选)
|
||||
* @param materialType 材料类型(可选)
|
||||
* @return 统计结果
|
||||
*/
|
||||
Map<String, Object> selectCostSummary(@Param("startDate") LocalDate startDate,
|
||||
@Param("endDate") LocalDate endDate,
|
||||
@Param("warehouseId") Long warehouseId,
|
||||
@Param("itemType") String itemType,
|
||||
@Param("materialType") String materialType);
|
||||
|
||||
/**
|
||||
* 按库区统计成本
|
||||
*
|
||||
* @param startDate 开始日期
|
||||
* @param endDate 结束日期
|
||||
* @return 统计结果列表
|
||||
*/
|
||||
List<Map<String, Object>> selectCostByWarehouse(@Param("startDate") LocalDate startDate,
|
||||
@Param("endDate") LocalDate endDate);
|
||||
|
||||
/**
|
||||
* 按物品类型统计成本
|
||||
*
|
||||
* @param startDate 开始日期
|
||||
* @param endDate 结束日期
|
||||
* @return 统计结果列表
|
||||
*/
|
||||
List<Map<String, Object>> selectCostByItemType(@Param("startDate") LocalDate startDate,
|
||||
@Param("endDate") LocalDate endDate);
|
||||
|
||||
/**
|
||||
* 检查指定日期和钢卷的成本记录是否存在
|
||||
*
|
||||
* @param coilId 钢卷ID
|
||||
* @param calcDate 计算日期
|
||||
* @return 记录数量
|
||||
*/
|
||||
int countByCoilIdAndDate(@Param("coilId") Long coilId, @Param("calcDate") LocalDate calcDate);
|
||||
|
||||
/**
|
||||
* 按入场钢卷号统计成本
|
||||
*
|
||||
* @param startDate 开始日期
|
||||
* @param endDate 结束日期
|
||||
* @param enterCoilNo 入场钢卷号(可选)
|
||||
* @return 统计结果列表
|
||||
*/
|
||||
List<Map<String, Object>> selectCostByEnterCoilNo(@Param("startDate") LocalDate startDate,
|
||||
@Param("endDate") LocalDate endDate,
|
||||
@Param("enterCoilNo") String enterCoilNo);
|
||||
|
||||
/**
|
||||
* 删除指定日期的成本记录
|
||||
*
|
||||
* @param calcDate 计算日期
|
||||
* @return 删除数量
|
||||
*/
|
||||
int deleteByCalcDate(@Param("calcDate") LocalDate calcDate);
|
||||
|
||||
/**
|
||||
* 囤积成本(按入场钢卷号聚合)列表查询
|
||||
*
|
||||
* @param calcDate 计算日期
|
||||
* @param enterCoilNo 入场钢卷号(可选,支持精确匹配或前缀匹配,可在 SQL 中自行处理)
|
||||
* @param offset 分页偏移量
|
||||
* @param pageSize 分页大小
|
||||
* @return 聚合后的列表
|
||||
*/
|
||||
List<Map<String, Object>> selectStockpileByEnterCoilNo(@Param("calcDate") LocalDate calcDate,
|
||||
@Param("enterCoilNo") String enterCoilNo,
|
||||
@Param("offset") int offset,
|
||||
@Param("pageSize") int pageSize);
|
||||
|
||||
/**
|
||||
* 囤积成本(按入场钢卷号聚合)列表总数
|
||||
*
|
||||
* @param calcDate 计算日期
|
||||
* @param enterCoilNo 入场钢卷号(可选)
|
||||
* @return 入场钢卷号分组条数
|
||||
*/
|
||||
long countStockpileByEnterCoilNo(@Param("calcDate") LocalDate calcDate,
|
||||
@Param("enterCoilNo") String enterCoilNo);
|
||||
|
||||
/**
|
||||
* 囤积成本(按入场钢卷号聚合)汇总
|
||||
*
|
||||
* @param calcDate 计算日期
|
||||
* @param enterCoilNo 入场钢卷号(可选)
|
||||
* @return 汇总数据
|
||||
*/
|
||||
Map<String, Object> selectStockpileSummaryByEnterCoilNo(@Param("calcDate") LocalDate calcDate,
|
||||
@Param("enterCoilNo") String enterCoilNo);
|
||||
|
||||
/**
|
||||
* 首页概览:直接基于 wms_material_coil 现算当日成本
|
||||
*/
|
||||
Map<String, Object> selectOverviewFromMaterialCoil();
|
||||
|
||||
/**
|
||||
* 成本趋势(按日汇总)
|
||||
*/
|
||||
List<Map<String, Object>> selectCostTrend(@Param("startDate") LocalDate startDate,
|
||||
@Param("endDate") LocalDate endDate);
|
||||
|
||||
/**
|
||||
* 成本检索(基于 wms_material_coil 实时计算)- 列表
|
||||
*/
|
||||
List<Map<String, Object>> selectMaterialCostCards(@Param("enterCoilNo") String enterCoilNo,
|
||||
@Param("calcDate") LocalDate calcDate,
|
||||
@Param("offset") int offset,
|
||||
@Param("pageSize") int pageSize);
|
||||
|
||||
/**
|
||||
* 成本检索(基于 wms_material_coil 实时计算)- 总数
|
||||
*/
|
||||
long countMaterialCostCards(@Param("enterCoilNo") String enterCoilNo,
|
||||
@Param("calcDate") LocalDate calcDate);
|
||||
|
||||
/**
|
||||
* 成本检索(基于 wms_material_coil 实时计算)- 汇总
|
||||
*/
|
||||
Map<String, Object> selectMaterialCostSummary(@Param("enterCoilNo") String enterCoilNo,
|
||||
@Param("calcDate") LocalDate calcDate);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.klp.mapper;
|
||||
|
||||
import com.klp.domain.CostStandardConfig;
|
||||
import com.klp.domain.vo.CostStandardConfigVo;
|
||||
import com.klp.common.core.mapper.BaseMapperPlus;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* 成本标准配置表Mapper接口
|
||||
*
|
||||
* @author klp
|
||||
* @date 2025-11-25
|
||||
*/
|
||||
public interface CostStandardConfigMapper extends BaseMapperPlus<CostStandardConfigMapper, CostStandardConfig, CostStandardConfigVo> {
|
||||
|
||||
/**
|
||||
* 查询指定日期的有效成本标准
|
||||
*
|
||||
* @param date 日期
|
||||
* @return 成本标准配置
|
||||
*/
|
||||
CostStandardConfigVo selectEffectiveByDate(@Param("date") LocalDate date);
|
||||
|
||||
/**
|
||||
* 查询当前有效的成本标准
|
||||
*
|
||||
* @return 成本标准配置
|
||||
*/
|
||||
CostStandardConfigVo selectCurrentEffective();
|
||||
}
|
||||
|
||||
156
klp-wms/src/main/java/com/klp/service/ICostCoilDailyService.java
Normal file
156
klp-wms/src/main/java/com/klp/service/ICostCoilDailyService.java
Normal file
@@ -0,0 +1,156 @@
|
||||
package com.klp.service;
|
||||
|
||||
import com.klp.domain.vo.CostCoilDailyVo;
|
||||
import com.klp.domain.bo.CostCoilDailyBo;
|
||||
import com.klp.common.core.page.TableDataInfo;
|
||||
import com.klp.common.core.domain.PageQuery;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 钢卷日成本记录表Service接口
|
||||
*
|
||||
* @author klp
|
||||
* @date 2025-11-25
|
||||
*/
|
||||
public interface ICostCoilDailyService {
|
||||
|
||||
/**
|
||||
* 查询钢卷日成本记录表
|
||||
*/
|
||||
CostCoilDailyVo queryById(Long costId);
|
||||
|
||||
/**
|
||||
* 查询钢卷日成本记录表列表
|
||||
*/
|
||||
TableDataInfo<CostCoilDailyVo> queryPageList(CostCoilDailyBo bo, PageQuery pageQuery);
|
||||
|
||||
/**
|
||||
* 查询钢卷日成本记录表列表
|
||||
*/
|
||||
List<CostCoilDailyVo> queryList(CostCoilDailyBo bo);
|
||||
|
||||
/**
|
||||
* 新增钢卷日成本记录表
|
||||
*/
|
||||
Boolean insertByBo(CostCoilDailyBo bo);
|
||||
|
||||
/**
|
||||
* 修改钢卷日成本记录表
|
||||
*/
|
||||
Boolean updateByBo(CostCoilDailyBo bo);
|
||||
|
||||
/**
|
||||
* 校验并批量删除钢卷日成本记录表信息
|
||||
*/
|
||||
Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid);
|
||||
|
||||
/**
|
||||
* 实时计算指定钢卷的成本
|
||||
*
|
||||
* @param coilId 钢卷ID(可选,不传则计算所有现存钢卷)
|
||||
* @param calcTime 计算时间点(可选,默认当前时间)
|
||||
* @return 成本计算结果
|
||||
*/
|
||||
Map<String, Object> calculateCost(Long coilId, java.time.LocalDateTime calcTime);
|
||||
|
||||
/**
|
||||
* 批量计算多个钢卷的成本
|
||||
*
|
||||
* @param coilIds 钢卷ID列表
|
||||
* @param calcTime 计算时间点(可选,默认当前时间)
|
||||
* @return 成本计算结果列表,每个元素包含一个钢卷的成本信息
|
||||
*/
|
||||
List<Map<String, Object>> batchCalculateCost(List<Long> coilIds, java.time.LocalDateTime calcTime);
|
||||
|
||||
/**
|
||||
* 批量计算钢卷成本(定时任务使用)
|
||||
*
|
||||
* @param calcDate 计算日期(前一日)
|
||||
* @return 计算成功的数量
|
||||
*/
|
||||
int calculateDailyCost(LocalDate calcDate);
|
||||
|
||||
/**
|
||||
* 查询成本统计报表
|
||||
*
|
||||
* @param startDate 开始日期
|
||||
* @param endDate 结束日期
|
||||
* @param groupBy 分组维度(warehouse/itemType/materialType)
|
||||
* @param warehouseId 库区ID(可选)
|
||||
* @return 统计结果
|
||||
*/
|
||||
Map<String, Object> queryCostSummary(LocalDate startDate, LocalDate endDate, String groupBy, Long warehouseId);
|
||||
|
||||
/**
|
||||
* 查询成本趋势分析
|
||||
*
|
||||
* @param startDate 开始日期
|
||||
* @param endDate 结束日期
|
||||
* @return 趋势数据
|
||||
*/
|
||||
List<Map<String, Object>> queryCostTrend(LocalDate startDate, LocalDate endDate);
|
||||
|
||||
/**
|
||||
* 按入场钢卷号维度计算成本
|
||||
* 同一个入场钢卷号可能被分卷成多个钢卷,需要汇总计算
|
||||
* 未发货的:计算到当日;已发货的:计算到发货前一天
|
||||
*
|
||||
* @param enterCoilNo 入场钢卷号
|
||||
* @param calcDate 计算日期(可选,默认当前日期)
|
||||
* @return 成本计算结果(包含各子卷明细和汇总)
|
||||
*/
|
||||
Map<String, Object> calculateCostByEnterCoilNo(String enterCoilNo, LocalDate calcDate);
|
||||
|
||||
/**
|
||||
* 批量按入场钢卷号维度计算成本(定时任务使用)
|
||||
* 按入场钢卷号分组,计算每个入场钢卷号的总成本
|
||||
*
|
||||
* @param calcDate 计算日期(可选,默认前一日)
|
||||
* @return 计算成功的入场钢卷号数量
|
||||
*/
|
||||
int calculateDailyCostByEnterCoilNo(LocalDate calcDate);
|
||||
|
||||
/**
|
||||
* 现算成本检索(基于 wms_material_coil)
|
||||
*
|
||||
* @param enterCoilNo 入场钢卷号(支持前缀匹配)
|
||||
* @param calcDate 计算日期(可选,默认当前日期)
|
||||
* @param pageNum 页码
|
||||
* @param pageSize 每页条数
|
||||
* @return 汇总与分页明细
|
||||
*/
|
||||
Map<String, Object> searchMaterialCost(String enterCoilNo, LocalDate calcDate, int pageNum, int pageSize);
|
||||
|
||||
/**
|
||||
* 查询按入场钢卷号统计的成本报表
|
||||
*
|
||||
* @param startDate 开始日期
|
||||
* @param endDate 结束日期
|
||||
* @param enterCoilNo 入场钢卷号(可选,用于精确查询)
|
||||
* @return 统计结果列表
|
||||
*/
|
||||
List<Map<String, Object>> queryCostByEnterCoilNo(LocalDate startDate, LocalDate endDate, String enterCoilNo);
|
||||
|
||||
/**
|
||||
* 成本模块首页概览数据
|
||||
* 统计当前「现存且未发货」钢卷的总成本、总净重、总毛重以及平均在库天数
|
||||
*
|
||||
* @return 概览数据:totalCost、totalNetWeight、totalGrossWeight、avgStorageDays、totalCoils
|
||||
*/
|
||||
Map<String, Object> queryOverview();
|
||||
|
||||
/**
|
||||
* 囤积成本页数据(后台统一计算)
|
||||
*
|
||||
* @param enterCoilNo 入场钢卷号(可选)
|
||||
* @param currentCoilNo 当前钢卷号(可选)
|
||||
* @param pageQuery 分页参数
|
||||
* @return rows+total+summary 结果
|
||||
*/
|
||||
Map<String, Object> queryStockpileCostList(String enterCoilNo, String currentCoilNo, PageQuery pageQuery);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.klp.service;
|
||||
|
||||
import com.klp.domain.vo.CostStandardConfigVo;
|
||||
import com.klp.domain.bo.CostStandardConfigBo;
|
||||
import com.klp.common.core.page.TableDataInfo;
|
||||
import com.klp.common.core.domain.PageQuery;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 成本标准配置表Service接口
|
||||
*
|
||||
* @author klp
|
||||
* @date 2025-11-25
|
||||
*/
|
||||
public interface ICostStandardConfigService {
|
||||
|
||||
/**
|
||||
* 查询成本标准配置表
|
||||
*/
|
||||
CostStandardConfigVo queryById(Long configId);
|
||||
|
||||
/**
|
||||
* 查询成本标准配置表列表
|
||||
*/
|
||||
TableDataInfo<CostStandardConfigVo> queryPageList(CostStandardConfigBo bo, PageQuery pageQuery);
|
||||
|
||||
/**
|
||||
* 查询成本标准配置表列表
|
||||
*/
|
||||
List<CostStandardConfigVo> queryList(CostStandardConfigBo bo);
|
||||
|
||||
/**
|
||||
* 新增成本标准配置表
|
||||
*/
|
||||
Boolean insertByBo(CostStandardConfigBo bo);
|
||||
|
||||
/**
|
||||
* 修改成本标准配置表
|
||||
*/
|
||||
Boolean updateByBo(CostStandardConfigBo bo);
|
||||
|
||||
/**
|
||||
* 校验并批量删除成本标准配置表信息
|
||||
*/
|
||||
Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid);
|
||||
|
||||
/**
|
||||
* 查询当前有效的成本标准
|
||||
*
|
||||
* @return 成本标准配置
|
||||
*/
|
||||
CostStandardConfigVo queryCurrentEffective();
|
||||
|
||||
/**
|
||||
* 查询指定日期的有效成本标准
|
||||
*
|
||||
* @param date 日期
|
||||
* @return 成本标准配置
|
||||
*/
|
||||
CostStandardConfigVo queryEffectiveByDate(LocalDate date);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,946 @@
|
||||
package com.klp.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.klp.common.core.page.TableDataInfo;
|
||||
import com.klp.common.core.domain.PageQuery;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import com.klp.domain.bo.CostCoilDailyBo;
|
||||
import com.klp.domain.vo.CostCoilDailyVo;
|
||||
import com.klp.domain.CostCoilDaily;
|
||||
import com.klp.domain.WmsMaterialCoil;
|
||||
import com.klp.domain.vo.CostStandardConfigVo;
|
||||
import com.klp.mapper.CostCoilDailyMapper;
|
||||
import com.klp.mapper.WmsMaterialCoilMapper;
|
||||
import com.klp.service.ICostCoilDailyService;
|
||||
import com.klp.service.ICostStandardConfigService;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* 钢卷日成本记录表Service业务层处理
|
||||
*
|
||||
* @author klp
|
||||
* @date 2025-11-25
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class CostCoilDailyServiceImpl implements ICostCoilDailyService {
|
||||
|
||||
private final CostCoilDailyMapper baseMapper;
|
||||
private final WmsMaterialCoilMapper coilMapper;
|
||||
private final ICostStandardConfigService costStandardConfigService;
|
||||
|
||||
/**
|
||||
* 查询钢卷日成本记录表
|
||||
*/
|
||||
@Override
|
||||
public CostCoilDailyVo queryById(Long costId) {
|
||||
return baseMapper.selectVoById(costId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询钢卷日成本记录表列表
|
||||
*/
|
||||
@Override
|
||||
public TableDataInfo<CostCoilDailyVo> queryPageList(CostCoilDailyBo bo, PageQuery pageQuery) {
|
||||
LambdaQueryWrapper<CostCoilDaily> lqw = buildQueryWrapper(bo);
|
||||
Page<CostCoilDailyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
||||
return TableDataInfo.build(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询钢卷日成本记录表列表
|
||||
*/
|
||||
@Override
|
||||
public List<CostCoilDailyVo> queryList(CostCoilDailyBo bo) {
|
||||
LambdaQueryWrapper<CostCoilDaily> lqw = buildQueryWrapper(bo);
|
||||
return baseMapper.selectVoList(lqw);
|
||||
}
|
||||
|
||||
private LambdaQueryWrapper<CostCoilDaily> buildQueryWrapper(CostCoilDailyBo bo) {
|
||||
LambdaQueryWrapper<CostCoilDaily> lqw = Wrappers.lambdaQuery();
|
||||
lqw.eq(bo.getCostId() != null, CostCoilDaily::getCostId, bo.getCostId());
|
||||
lqw.eq(bo.getCoilId() != null, CostCoilDaily::getCoilId, bo.getCoilId());
|
||||
lqw.like(bo.getCurrentCoilNo() != null, CostCoilDaily::getCurrentCoilNo, bo.getCurrentCoilNo());
|
||||
lqw.eq(bo.getCalcDate() != null, CostCoilDaily::getCalcDate, bo.getCalcDate());
|
||||
lqw.eq(bo.getWarehouseId() != null, CostCoilDaily::getWarehouseId, bo.getWarehouseId());
|
||||
lqw.eq(bo.getItemType() != null, CostCoilDaily::getItemType, bo.getItemType());
|
||||
lqw.eq(bo.getMaterialType() != null, CostCoilDaily::getMaterialType, bo.getMaterialType());
|
||||
if (bo.getStartDate() != null) {
|
||||
lqw.ge(CostCoilDaily::getCalcDate, bo.getStartDate());
|
||||
}
|
||||
if (bo.getEndDate() != null) {
|
||||
lqw.le(CostCoilDaily::getCalcDate, bo.getEndDate());
|
||||
}
|
||||
lqw.orderByDesc(CostCoilDaily::getCalcDate);
|
||||
lqw.orderByDesc(CostCoilDaily::getCostId);
|
||||
return lqw;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增钢卷日成本记录表
|
||||
*/
|
||||
@Override
|
||||
public Boolean insertByBo(CostCoilDailyBo bo) {
|
||||
CostCoilDaily add = BeanUtil.toBean(bo, CostCoilDaily.class);
|
||||
validEntityBeforeSave(add);
|
||||
return baseMapper.insert(add) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改钢卷日成本记录表
|
||||
*/
|
||||
@Override
|
||||
public Boolean updateByBo(CostCoilDailyBo bo) {
|
||||
CostCoilDaily update = BeanUtil.toBean(bo, CostCoilDaily.class);
|
||||
validEntityBeforeSave(update);
|
||||
return baseMapper.updateById(update) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存前的数据校验
|
||||
*/
|
||||
private void validEntityBeforeSave(CostCoilDaily entity) {
|
||||
// TODO 做一些数据校验,如唯一约束
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验并批量删除钢卷日成本记录表信息
|
||||
*/
|
||||
@Override
|
||||
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
|
||||
if (isValid) {
|
||||
// TODO 做一些业务上的校验,判断是否需要校验
|
||||
}
|
||||
return baseMapper.deleteBatchIds(ids) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 实时计算指定钢卷的成本
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> calculateCost(Long coilId, LocalDateTime calcTime) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
// 1. 查询钢卷信息
|
||||
WmsMaterialCoil coil = coilMapper.selectById(coilId);
|
||||
if (coil == null) {
|
||||
result.put("error", "钢卷不存在");
|
||||
return result;
|
||||
}
|
||||
|
||||
// 2. 验证是否为计算对象(data_type=1, export_time IS NULL)
|
||||
if (coil.getDataType() == null || coil.getDataType() != 1) {
|
||||
result.put("error", "该钢卷不是现存数据,不在计算范围内");
|
||||
return result;
|
||||
}
|
||||
|
||||
if (coil.getExportTime() != null) {
|
||||
result.put("error", "该钢卷已发货,不在计算范围内");
|
||||
return result;
|
||||
}
|
||||
|
||||
WeightContext weightContext = resolveWeightContext(coil);
|
||||
if (!weightContext.isValid()) {
|
||||
result.put("error", "钢卷毛重与净重均为空或为0,无法计算成本");
|
||||
return result;
|
||||
}
|
||||
|
||||
// 3. 计算在库天数
|
||||
LocalDate startDate = coil.getCreateTime() != null
|
||||
? coil.getCreateTime().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate()
|
||||
: LocalDate.now();
|
||||
LocalDate endDate = calcTime != null ? calcTime.toLocalDate() : LocalDate.now();
|
||||
|
||||
long days = ChronoUnit.DAYS.between(startDate, endDate) + 1;
|
||||
if (days < 1) {
|
||||
days = 1; // 不足一天按一天计算
|
||||
}
|
||||
|
||||
// 4. 获取成本标准(使用入库日期的成本标准)
|
||||
CostStandardConfigVo costStandard = costStandardConfigService.queryEffectiveByDate(startDate);
|
||||
if (costStandard == null) {
|
||||
// 如果没有找到对应日期的标准,使用当前有效的标准
|
||||
costStandard = costStandardConfigService.queryCurrentEffective();
|
||||
}
|
||||
|
||||
if (costStandard == null || costStandard.getUnitCost() == null) {
|
||||
result.put("error", "未找到有效的成本标准配置");
|
||||
return result;
|
||||
}
|
||||
|
||||
BigDecimal unitCost = costStandard.getUnitCost();
|
||||
|
||||
// 5. 计算成本(按毛重优先)
|
||||
BigDecimal dailyCost = weightContext.getCostTon().multiply(unitCost).setScale(2, RoundingMode.HALF_UP);
|
||||
BigDecimal totalCost = dailyCost.multiply(BigDecimal.valueOf(days)).setScale(2, RoundingMode.HALF_UP);
|
||||
|
||||
// 6. 返回结果
|
||||
result.put("coilId", coil.getCoilId());
|
||||
result.put("coilNo", coil.getCurrentCoilNo());
|
||||
result.put("netWeight", weightContext.getNetTon()); // 吨
|
||||
result.put("netWeightKg", weightContext.getNetKg()); // 千克
|
||||
result.put("grossWeightKg", weightContext.getGrossKg());
|
||||
result.put("grossWeightTon", weightContext.getGrossTon());
|
||||
result.put("costWeightTon", weightContext.getCostTon());
|
||||
result.put("weightBasis", weightContext.useGross() ? "gross" : "net");
|
||||
result.put("storageDays", days);
|
||||
result.put("unitCost", unitCost);
|
||||
result.put("dailyCost", dailyCost);
|
||||
result.put("totalCost", totalCost);
|
||||
result.put("warehouseId", coil.getWarehouseId());
|
||||
result.put("itemType", coil.getItemType());
|
||||
result.put("materialType", coil.getMaterialType());
|
||||
result.put("createTime", coil.getCreateTime());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量计算多个钢卷的成本
|
||||
*/
|
||||
@Override
|
||||
public List<Map<String, Object>> batchCalculateCost(List<Long> coilIds, LocalDateTime calcTime) {
|
||||
List<Map<String, Object>> results = new ArrayList<>();
|
||||
|
||||
if (coilIds == null || coilIds.isEmpty()) {
|
||||
return results;
|
||||
}
|
||||
|
||||
// 批量查询钢卷信息
|
||||
QueryWrapper<WmsMaterialCoil> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.in("coil_id", coilIds);
|
||||
List<WmsMaterialCoil> coils = coilMapper.selectList(queryWrapper);
|
||||
|
||||
if (coils == null || coils.isEmpty()) {
|
||||
return results;
|
||||
}
|
||||
|
||||
// 获取成本标准(统一使用当前有效的标准,因为批量计算时可能跨多个日期)
|
||||
LocalDate calcDate = calcTime != null ? calcTime.toLocalDate() : LocalDate.now();
|
||||
CostStandardConfigVo costStandard = costStandardConfigService.queryEffectiveByDate(calcDate);
|
||||
if (costStandard == null) {
|
||||
costStandard = costStandardConfigService.queryCurrentEffective();
|
||||
}
|
||||
|
||||
BigDecimal unitCost = null;
|
||||
if (costStandard != null && costStandard.getUnitCost() != null) {
|
||||
unitCost = costStandard.getUnitCost();
|
||||
}
|
||||
|
||||
// 遍历计算每个钢卷的成本
|
||||
for (WmsMaterialCoil coil : coils) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
// 验证是否为计算对象(data_type=1, export_time IS NULL)
|
||||
if (coil.getDataType() == null || coil.getDataType() != 1) {
|
||||
result.put("coilId", coil.getCoilId());
|
||||
result.put("coilNo", coil.getCurrentCoilNo());
|
||||
result.put("error", "该钢卷不是现存数据,不在计算范围内");
|
||||
results.add(result);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (coil.getExportTime() != null) {
|
||||
result.put("coilId", coil.getCoilId());
|
||||
result.put("coilNo", coil.getCurrentCoilNo());
|
||||
result.put("error", "该钢卷已发货,不在计算范围内");
|
||||
results.add(result);
|
||||
continue;
|
||||
}
|
||||
|
||||
WeightContext weightContext = resolveWeightContext(coil);
|
||||
if (!weightContext.isValid()) {
|
||||
result.put("coilId", coil.getCoilId());
|
||||
result.put("coilNo", coil.getCurrentCoilNo());
|
||||
result.put("error", "钢卷毛重与净重均为空或为0,无法计算成本");
|
||||
results.add(result);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 计算在库天数
|
||||
LocalDate startDate = coil.getCreateTime() != null
|
||||
? coil.getCreateTime().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate()
|
||||
: LocalDate.now();
|
||||
LocalDate endDate = calcTime != null ? calcTime.toLocalDate() : LocalDate.now();
|
||||
|
||||
long days = ChronoUnit.DAYS.between(startDate, endDate) + 1;
|
||||
if (days < 1) {
|
||||
days = 1; // 不足一天按一天计算
|
||||
}
|
||||
|
||||
// 如果统一标准为空,尝试使用入库日期的标准
|
||||
BigDecimal finalUnitCost = unitCost;
|
||||
if (finalUnitCost == null) {
|
||||
CostStandardConfigVo startDateStandard = costStandardConfigService.queryEffectiveByDate(startDate);
|
||||
if (startDateStandard != null && startDateStandard.getUnitCost() != null) {
|
||||
finalUnitCost = startDateStandard.getUnitCost();
|
||||
} else {
|
||||
CostStandardConfigVo currentStandard = costStandardConfigService.queryCurrentEffective();
|
||||
if (currentStandard != null && currentStandard.getUnitCost() != null) {
|
||||
finalUnitCost = currentStandard.getUnitCost();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (finalUnitCost == null) {
|
||||
result.put("coilId", coil.getCoilId());
|
||||
result.put("coilNo", coil.getCurrentCoilNo());
|
||||
result.put("error", "未找到有效的成本标准配置");
|
||||
results.add(result);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 计算成本(毛重优先)
|
||||
BigDecimal dailyCost = weightContext.getCostTon().multiply(finalUnitCost).setScale(2, RoundingMode.HALF_UP);
|
||||
BigDecimal totalCost = dailyCost.multiply(BigDecimal.valueOf(days)).setScale(2, RoundingMode.HALF_UP);
|
||||
|
||||
// 返回结果
|
||||
result.put("coilId", coil.getCoilId());
|
||||
result.put("coilNo", coil.getCurrentCoilNo());
|
||||
result.put("netWeight", weightContext.getNetTon()); // 吨
|
||||
result.put("netWeightKg", weightContext.getNetKg()); // 千克
|
||||
result.put("grossWeightKg", weightContext.getGrossKg());
|
||||
result.put("grossWeightTon", weightContext.getGrossTon());
|
||||
result.put("costWeightTon", weightContext.getCostTon());
|
||||
result.put("weightBasis", weightContext.useGross() ? "gross" : "net");
|
||||
result.put("storageDays", days);
|
||||
result.put("unitCost", finalUnitCost);
|
||||
result.put("dailyCost", dailyCost);
|
||||
result.put("totalCost", totalCost);
|
||||
result.put("warehouseId", coil.getWarehouseId());
|
||||
result.put("itemType", coil.getItemType());
|
||||
result.put("materialType", coil.getMaterialType());
|
||||
result.put("createTime", coil.getCreateTime());
|
||||
|
||||
results.add(result);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量计算钢卷成本(定时任务使用)
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int calculateDailyCost(LocalDate calcDate) {
|
||||
log.info("开始计算日期 {} 的钢卷成本", calcDate);
|
||||
|
||||
// 0. 防止重复记录,先清理当日历史
|
||||
int deleted = baseMapper.deleteByCalcDate(calcDate);
|
||||
log.info("已删除日期 {} 的历史成本记录 {} 条", calcDate, deleted);
|
||||
|
||||
// 1. 查询所有现存且未发货的钢卷
|
||||
QueryWrapper<WmsMaterialCoil> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("data_type", 1) // 现存数据
|
||||
.isNull("export_time") // 未发货(export_time IS NULL)
|
||||
.eq("del_flag", 0); // 未删除
|
||||
|
||||
List<WmsMaterialCoil> coils = coilMapper.selectList(queryWrapper);
|
||||
log.info("找到 {} 个需要计算成本的钢卷", coils.size());
|
||||
|
||||
// 2. 获取成本标准
|
||||
CostStandardConfigVo costStandard = costStandardConfigService.queryEffectiveByDate(calcDate);
|
||||
if (costStandard == null) {
|
||||
costStandard = costStandardConfigService.queryCurrentEffective();
|
||||
}
|
||||
|
||||
if (costStandard == null || costStandard.getUnitCost() == null) {
|
||||
log.error("未找到日期 {} 的有效成本标准配置", calcDate);
|
||||
return 0;
|
||||
}
|
||||
|
||||
BigDecimal unitCost = costStandard.getUnitCost();
|
||||
int successCount = 0;
|
||||
|
||||
// 3. 遍历计算每个钢卷的成本
|
||||
for (WmsMaterialCoil coil : coils) {
|
||||
try {
|
||||
// 计算在库天数(从入库到计算日期)
|
||||
LocalDate createDate = coil.getCreateTime() != null
|
||||
? coil.getCreateTime().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate()
|
||||
: LocalDate.now();
|
||||
long days = ChronoUnit.DAYS.between(createDate, calcDate) + 1;
|
||||
if (days < 1) {
|
||||
days = 1;
|
||||
}
|
||||
|
||||
// 计算成本(毛重优先)
|
||||
WeightContext weightContext = resolveWeightContext(coil);
|
||||
if (!weightContext.isValid()) {
|
||||
log.warn("钢卷 {} 缺少有效重量,跳过", coil.getCurrentCoilNo());
|
||||
continue;
|
||||
}
|
||||
BigDecimal dailyCost = weightContext.getCostTon().multiply(unitCost).setScale(2, RoundingMode.HALF_UP);
|
||||
BigDecimal totalCost = dailyCost.multiply(BigDecimal.valueOf(days)).setScale(2, RoundingMode.HALF_UP);
|
||||
|
||||
// 4. 保存成本记录
|
||||
CostCoilDaily costRecord = new CostCoilDaily();
|
||||
costRecord.setCoilId(coil.getCoilId());
|
||||
costRecord.setCurrentCoilNo(coil.getCurrentCoilNo());
|
||||
costRecord.setCalcDate(calcDate);
|
||||
costRecord.setNetWeight(weightContext.getCostTon());
|
||||
costRecord.setUnitCost(unitCost);
|
||||
costRecord.setDailyCost(dailyCost);
|
||||
costRecord.setStorageDays((int) days);
|
||||
costRecord.setTotalCost(totalCost);
|
||||
costRecord.setWarehouseId(coil.getWarehouseId());
|
||||
costRecord.setItemType(coil.getItemType());
|
||||
costRecord.setMaterialType(coil.getMaterialType());
|
||||
|
||||
baseMapper.insert(costRecord);
|
||||
successCount++;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("计算钢卷 {} 的成本时发生错误: {}", coil.getCurrentCoilNo(), e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
log.info("完成计算日期 {} 的钢卷成本,成功计算 {} 条记录", calcDate, successCount);
|
||||
return successCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询成本统计报表
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> queryCostSummary(LocalDate startDate, LocalDate endDate, String groupBy, Long warehouseId) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
// 查询汇总数据
|
||||
Map<String, Object> summary = baseMapper.selectCostSummary(startDate, endDate, warehouseId, null, null);
|
||||
result.put("summary", summary);
|
||||
|
||||
// 根据分组维度查询明细
|
||||
List<Map<String, Object>> details = new ArrayList<>();
|
||||
if ("warehouse".equals(groupBy)) {
|
||||
details = baseMapper.selectCostByWarehouse(startDate, endDate);
|
||||
} else if ("itemType".equals(groupBy)) {
|
||||
details = baseMapper.selectCostByItemType(startDate, endDate);
|
||||
}
|
||||
result.put("details", details);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询成本趋势分析
|
||||
*/
|
||||
@Override
|
||||
public List<Map<String, Object>> queryCostTrend(LocalDate startDate, LocalDate endDate) {
|
||||
LocalDate effectiveStart = startDate != null ? startDate : LocalDate.now().minusDays(30);
|
||||
LocalDate effectiveEnd = endDate != null ? endDate : LocalDate.now();
|
||||
return baseMapper.selectCostTrend(effectiveStart, effectiveEnd);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按入场钢卷号维度计算成本
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> calculateCostByEnterCoilNo(String enterCoilNo, LocalDate calcDate) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("enterCoilNo", enterCoilNo);
|
||||
|
||||
if (calcDate == null) {
|
||||
calcDate = LocalDate.now();
|
||||
}
|
||||
|
||||
// 1. 查询该入场钢卷号下的所有钢卷(包括已发货和未发货的)
|
||||
QueryWrapper<WmsMaterialCoil> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("enter_coil_no", enterCoilNo)
|
||||
.eq("data_type", 1) // 现存数据
|
||||
.eq("del_flag", 0); // 未删除
|
||||
|
||||
List<WmsMaterialCoil> coils = coilMapper.selectList(queryWrapper);
|
||||
if (coils.isEmpty()) {
|
||||
result.put("error", "未找到入场钢卷号 " + enterCoilNo + " 的相关钢卷");
|
||||
return result;
|
||||
}
|
||||
|
||||
List<Map<String, Object>> coilDetails = new ArrayList<>();
|
||||
BigDecimal totalCost = BigDecimal.ZERO;
|
||||
BigDecimal totalNetWeight = BigDecimal.ZERO;
|
||||
BigDecimal totalGrossWeight = BigDecimal.ZERO;
|
||||
int unshippedCount = 0;
|
||||
int shippedCount = 0;
|
||||
|
||||
// 2. 遍历每个钢卷计算成本
|
||||
for (WmsMaterialCoil coil : coils) {
|
||||
Map<String, Object> coilDetail = new HashMap<>();
|
||||
coilDetail.put("coilId", coil.getCoilId());
|
||||
coilDetail.put("currentCoilNo", coil.getCurrentCoilNo());
|
||||
coilDetail.put("isShipped", coil.getExportTime() != null);
|
||||
|
||||
// 计算在库天数
|
||||
LocalDate startDate = coil.getCreateTime() != null
|
||||
? coil.getCreateTime().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate()
|
||||
: LocalDate.now();
|
||||
|
||||
// 确定结束日期:已发货的计算到发货前一天,未发货的计算到当日
|
||||
LocalDate endDate;
|
||||
if (coil.getExportTime() != null) {
|
||||
// 已发货:计算到发货前一天
|
||||
endDate = coil.getExportTime().toInstant()
|
||||
.atZone(java.time.ZoneId.systemDefault())
|
||||
.toLocalDate()
|
||||
.minusDays(1);
|
||||
shippedCount++;
|
||||
} else {
|
||||
// 未发货:计算到当日
|
||||
endDate = calcDate;
|
||||
unshippedCount++;
|
||||
}
|
||||
|
||||
// 确保结束日期不早于开始日期
|
||||
if (endDate.isBefore(startDate)) {
|
||||
endDate = startDate;
|
||||
}
|
||||
|
||||
long days = ChronoUnit.DAYS.between(startDate, endDate) + 1;
|
||||
if (days < 1) {
|
||||
days = 1;
|
||||
}
|
||||
|
||||
// 获取成本标准
|
||||
CostStandardConfigVo costStandard = costStandardConfigService.queryEffectiveByDate(startDate);
|
||||
if (costStandard == null) {
|
||||
costStandard = costStandardConfigService.queryCurrentEffective();
|
||||
}
|
||||
|
||||
if (costStandard == null || costStandard.getUnitCost() == null) {
|
||||
coilDetail.put("error", "未找到有效的成本标准配置");
|
||||
coilDetails.add(coilDetail);
|
||||
continue;
|
||||
}
|
||||
|
||||
BigDecimal unitCost = costStandard.getUnitCost();
|
||||
|
||||
// 计算成本(毛重优先)
|
||||
WeightContext weightContext = resolveWeightContext(coil);
|
||||
if (!weightContext.isValid()) {
|
||||
coilDetail.put("error", "钢卷缺少有效重量,无法计算");
|
||||
coilDetails.add(coilDetail);
|
||||
continue;
|
||||
}
|
||||
|
||||
BigDecimal dailyCost = weightContext.getCostTon().multiply(unitCost).setScale(2, RoundingMode.HALF_UP);
|
||||
BigDecimal coilTotalCost = dailyCost.multiply(BigDecimal.valueOf(days)).setScale(2, RoundingMode.HALF_UP);
|
||||
|
||||
coilDetail.put("netWeightTon", weightContext.getNetTon());
|
||||
coilDetail.put("grossWeightTon", weightContext.getGrossTon());
|
||||
coilDetail.put("weightBasis", weightContext.useGross() ? "gross" : "net");
|
||||
coilDetail.put("storageDays", days);
|
||||
coilDetail.put("unitCost", unitCost);
|
||||
coilDetail.put("dailyCost", dailyCost);
|
||||
coilDetail.put("totalCost", coilTotalCost);
|
||||
coilDetail.put("startDate", startDate);
|
||||
coilDetail.put("endDate", endDate);
|
||||
coilDetail.put("exportTime", coil.getExportTime());
|
||||
|
||||
coilDetails.add(coilDetail);
|
||||
|
||||
// 累计总成本和总净重
|
||||
totalCost = totalCost.add(coilTotalCost);
|
||||
totalNetWeight = totalNetWeight.add(weightContext.getNetTon());
|
||||
totalGrossWeight = totalGrossWeight.add(weightContext.getGrossTon());
|
||||
}
|
||||
|
||||
// 3. 汇总结果
|
||||
result.put("coilDetails", coilDetails);
|
||||
result.put("totalCoils", coils.size());
|
||||
result.put("shippedCount", shippedCount);
|
||||
result.put("unshippedCount", unshippedCount);
|
||||
result.put("totalNetWeight", totalNetWeight.setScale(3, RoundingMode.HALF_UP));
|
||||
result.put("totalGrossWeight", totalGrossWeight.setScale(3, RoundingMode.HALF_UP));
|
||||
result.put("totalCost", totalCost.setScale(2, RoundingMode.HALF_UP));
|
||||
result.put("calcDate", calcDate);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量按入场钢卷号维度计算成本(定时任务使用)
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int calculateDailyCostByEnterCoilNo(LocalDate calcDate) {
|
||||
if (calcDate == null) {
|
||||
calcDate = LocalDate.now().minusDays(1);
|
||||
}
|
||||
|
||||
log.info("开始按入场钢卷号维度计算日期 {} 的成本", calcDate);
|
||||
|
||||
// 清理当日历史记录,防止重复计算
|
||||
int deleted = baseMapper.deleteByCalcDate(calcDate);
|
||||
log.info("已删除日期 {} 的历史成本记录 {} 条(按入场卷号维度重算)", calcDate, deleted);
|
||||
|
||||
// 1. 查询所有需要计算的入场钢卷号(去重)
|
||||
QueryWrapper<WmsMaterialCoil> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.select("DISTINCT enter_coil_no")
|
||||
.eq("data_type", 1)
|
||||
.eq("del_flag", 0)
|
||||
.isNotNull("enter_coil_no");
|
||||
|
||||
List<WmsMaterialCoil> distinctCoils = coilMapper.selectList(queryWrapper);
|
||||
log.info("找到 {} 个需要计算成本的入场钢卷号", distinctCoils.size());
|
||||
|
||||
int successCount = 0;
|
||||
|
||||
// 2. 遍历每个入场钢卷号计算成本
|
||||
for (WmsMaterialCoil distinctCoil : distinctCoils) {
|
||||
try {
|
||||
String enterCoilNo = distinctCoil.getEnterCoilNo();
|
||||
if (enterCoilNo == null || enterCoilNo.trim().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 计算该入场钢卷号的成本
|
||||
Map<String, Object> costResult = calculateCostByEnterCoilNo(enterCoilNo, calcDate);
|
||||
|
||||
if (costResult.containsKey("error")) {
|
||||
log.warn("计算入场钢卷号 {} 的成本时发生错误: {}", enterCoilNo, costResult.get("error"));
|
||||
continue;
|
||||
}
|
||||
|
||||
// 获取该入场钢卷号下的所有钢卷
|
||||
QueryWrapper<WmsMaterialCoil> coilQueryWrapper = new QueryWrapper<>();
|
||||
coilQueryWrapper.eq("enter_coil_no", enterCoilNo)
|
||||
.eq("data_type", 1)
|
||||
.eq("del_flag", 0);
|
||||
|
||||
List<WmsMaterialCoil> coils = coilMapper.selectList(coilQueryWrapper);
|
||||
|
||||
// 为每个钢卷生成成本记录
|
||||
for (WmsMaterialCoil coil : coils) {
|
||||
// 确定计算结束日期
|
||||
LocalDate startDate = coil.getCreateTime() != null
|
||||
? coil.getCreateTime().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate()
|
||||
: LocalDate.now();
|
||||
|
||||
LocalDate endDate;
|
||||
if (coil.getExportTime() != null) {
|
||||
// 已发货:计算到发货前一天
|
||||
endDate = coil.getExportTime().toInstant()
|
||||
.atZone(java.time.ZoneId.systemDefault())
|
||||
.toLocalDate()
|
||||
.minusDays(1);
|
||||
} else {
|
||||
// 未发货:计算到计算日期
|
||||
endDate = calcDate;
|
||||
}
|
||||
|
||||
// 对于已发货的钢卷,只计算到发货前一天,如果计算日期在发货日期之后,跳过
|
||||
if (coil.getExportTime() != null) {
|
||||
LocalDate exportDate = coil.getExportTime().toInstant()
|
||||
.atZone(java.time.ZoneId.systemDefault())
|
||||
.toLocalDate();
|
||||
// 如果计算日期在发货日期之后,不需要计算
|
||||
if (calcDate.isAfter(exportDate)) {
|
||||
continue;
|
||||
}
|
||||
// 如果计算日期等于发货日期,计算到前一天
|
||||
if (calcDate.isEqual(exportDate)) {
|
||||
endDate = exportDate.minusDays(1);
|
||||
}
|
||||
}
|
||||
|
||||
// 确保结束日期不早于开始日期
|
||||
if (endDate.isBefore(startDate)) {
|
||||
endDate = startDate;
|
||||
}
|
||||
|
||||
// 计算在库天数(到结束日期)
|
||||
long days = ChronoUnit.DAYS.between(startDate, endDate) + 1;
|
||||
if (days < 1) {
|
||||
days = 1;
|
||||
}
|
||||
|
||||
// 获取成本标准
|
||||
CostStandardConfigVo costStandard = costStandardConfigService.queryEffectiveByDate(startDate);
|
||||
if (costStandard == null) {
|
||||
costStandard = costStandardConfigService.queryCurrentEffective();
|
||||
}
|
||||
|
||||
if (costStandard == null || costStandard.getUnitCost() == null) {
|
||||
log.warn("未找到入场钢卷号 {} 钢卷 {} 的有效成本标准", enterCoilNo, coil.getCurrentCoilNo());
|
||||
continue;
|
||||
}
|
||||
|
||||
BigDecimal unitCost = costStandard.getUnitCost();
|
||||
|
||||
// 计算成本
|
||||
WeightContext weightContext = resolveWeightContext(coil);
|
||||
if (!weightContext.isValid()) {
|
||||
log.warn("入场卷号 {} 下的钢卷 {} 缺少有效重量,跳过", enterCoilNo, coil.getCurrentCoilNo());
|
||||
continue;
|
||||
}
|
||||
BigDecimal dailyCost = weightContext.getCostTon().multiply(unitCost).setScale(2, RoundingMode.HALF_UP);
|
||||
BigDecimal totalCost = dailyCost.multiply(BigDecimal.valueOf(days)).setScale(2, RoundingMode.HALF_UP);
|
||||
|
||||
// 保存成本记录
|
||||
CostCoilDaily costRecord = new CostCoilDaily();
|
||||
costRecord.setCoilId(coil.getCoilId());
|
||||
costRecord.setCurrentCoilNo(coil.getCurrentCoilNo());
|
||||
costRecord.setCalcDate(calcDate);
|
||||
costRecord.setNetWeight(weightContext.getCostTon());
|
||||
costRecord.setUnitCost(unitCost);
|
||||
costRecord.setDailyCost(dailyCost);
|
||||
costRecord.setStorageDays((int) days);
|
||||
costRecord.setTotalCost(totalCost);
|
||||
costRecord.setWarehouseId(coil.getWarehouseId());
|
||||
costRecord.setItemType(coil.getItemType());
|
||||
costRecord.setMaterialType(coil.getMaterialType());
|
||||
|
||||
baseMapper.insert(costRecord);
|
||||
}
|
||||
|
||||
successCount++;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("按入场钢卷号维度计算成本时发生错误: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
log.info("完成按入场钢卷号维度计算日期 {} 的成本,成功计算 {} 个入场钢卷号", calcDate, successCount);
|
||||
return successCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> searchMaterialCost(String enterCoilNo, LocalDate calcDate, int pageNum, int pageSize) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
int safePageNum = pageNum > 0 ? pageNum : 1;
|
||||
int safePageSize = pageSize > 0 ? Math.min(pageSize, 100) : 20;
|
||||
LocalDate effectiveCalcDate = calcDate != null ? calcDate : LocalDate.now();
|
||||
String trimmedEnterCoilNo = StringUtils.isNotBlank(enterCoilNo) ? enterCoilNo.trim() : null;
|
||||
|
||||
result.put("enterCoilNo", trimmedEnterCoilNo);
|
||||
result.put("calcDate", effectiveCalcDate);
|
||||
result.put("pageNum", safePageNum);
|
||||
result.put("pageSize", safePageSize);
|
||||
result.put("records", Collections.emptyList());
|
||||
result.put("total", 0L);
|
||||
|
||||
Map<String, Object> summary = buildEmptySearchSummary(trimmedEnterCoilNo, effectiveCalcDate);
|
||||
if (trimmedEnterCoilNo == null) {
|
||||
result.put("summary", summary);
|
||||
return result;
|
||||
}
|
||||
|
||||
long total = baseMapper.countMaterialCostCards(trimmedEnterCoilNo, effectiveCalcDate);
|
||||
if (total > 0) {
|
||||
Map<String, Object> dbSummary = baseMapper.selectMaterialCostSummary(trimmedEnterCoilNo, effectiveCalcDate);
|
||||
summary = normalizeMaterialCostSummary(dbSummary, trimmedEnterCoilNo, effectiveCalcDate);
|
||||
|
||||
int offset = (safePageNum - 1) * safePageSize;
|
||||
List<Map<String, Object>> cards = baseMapper.selectMaterialCostCards(trimmedEnterCoilNo, effectiveCalcDate, offset, safePageSize);
|
||||
result.put("records", cards);
|
||||
result.put("total", total);
|
||||
} else {
|
||||
result.put("total", 0L);
|
||||
}
|
||||
|
||||
result.put("summary", summary);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询按入场钢卷号统计的成本报表
|
||||
*/
|
||||
@Override
|
||||
public List<Map<String, Object>> queryCostByEnterCoilNo(LocalDate startDate, LocalDate endDate, String enterCoilNo) {
|
||||
return baseMapper.selectCostByEnterCoilNo(startDate, endDate, enterCoilNo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 囤积成本页数据(后台统一计算)
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> queryStockpileCostList(String enterCoilNo, String currentCoilNo, PageQuery pageQuery) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
// 使用 SQL 直接按入场钢卷号聚合,避免在 Service 层做大循环
|
||||
LocalDate calcDate = LocalDate.now();
|
||||
int pageNum = pageQuery.getPageNum() != null ? pageQuery.getPageNum() : 1;
|
||||
int pageSize = pageQuery.getPageSize() != null ? pageQuery.getPageSize() : 50;
|
||||
int offset = (pageNum - 1) * pageSize;
|
||||
|
||||
String trimmedEnterCoilNo = StringUtils.isNotBlank(enterCoilNo) ? enterCoilNo.trim() : null;
|
||||
|
||||
List<Map<String, Object>> rows = baseMapper.selectStockpileByEnterCoilNo(calcDate, trimmedEnterCoilNo, offset, pageSize);
|
||||
long total = baseMapper.countStockpileByEnterCoilNo(calcDate, trimmedEnterCoilNo);
|
||||
Map<String, Object> summary = baseMapper.selectStockpileSummaryByEnterCoilNo(calcDate, trimmedEnterCoilNo);
|
||||
|
||||
if (summary == null) {
|
||||
summary = new HashMap<>();
|
||||
summary.put("totalNetWeight", BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
|
||||
summary.put("totalGrossWeight", BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
|
||||
summary.put("totalCost", BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP));
|
||||
summary.put("avgStorageDays", BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP));
|
||||
}
|
||||
|
||||
// 兼容前端字段命名
|
||||
summary.putIfAbsent("totalCoils", total);
|
||||
|
||||
result.put("rows", rows);
|
||||
result.put("total", total);
|
||||
result.put("summary", summary);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 成本模块首页概览数据
|
||||
* 统计当前「现存且未发货」钢卷的总成本、总净重、总毛重以及平均在库天数
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> queryOverview() {
|
||||
Map<String, Object> db = baseMapper.selectOverviewFromMaterialCoil();
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
if (db == null || db.isEmpty()) {
|
||||
result.put("totalCost", BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP));
|
||||
result.put("totalNetWeight", BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
|
||||
result.put("totalGrossWeight", BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
|
||||
result.put("avgStorageDays", BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP));
|
||||
result.put("totalCoils", 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
BigDecimal totalCost = toBigDecimal(db.get("totalCost"), 2);
|
||||
BigDecimal totalNetWeight = toBigDecimal(db.get("totalNetWeight"), 3);
|
||||
BigDecimal totalGrossWeight = toBigDecimal(db.get("totalGrossWeight"), 3);
|
||||
BigDecimal avgStorageDays = toBigDecimal(db.get("avgStorageDays"), 2);
|
||||
Number totalCoils = db.get("totalCoils") instanceof Number ? (Number) db.get("totalCoils") : 0;
|
||||
|
||||
result.put("totalCost", totalCost);
|
||||
result.put("totalNetWeight", totalNetWeight);
|
||||
result.put("totalGrossWeight", totalGrossWeight);
|
||||
result.put("avgStorageDays", avgStorageDays);
|
||||
result.put("totalCoils", totalCoils.intValue());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private BigDecimal toBigDecimal(Object value, int scale) {
|
||||
if (value == null) {
|
||||
return BigDecimal.ZERO.setScale(scale, RoundingMode.HALF_UP);
|
||||
}
|
||||
if (value instanceof BigDecimal) {
|
||||
return ((BigDecimal) value).setScale(scale, RoundingMode.HALF_UP);
|
||||
}
|
||||
if (value instanceof Number) {
|
||||
return BigDecimal.valueOf(((Number) value).doubleValue()).setScale(scale, RoundingMode.HALF_UP);
|
||||
}
|
||||
try {
|
||||
return new BigDecimal(value.toString()).setScale(scale, RoundingMode.HALF_UP);
|
||||
} catch (Exception e) {
|
||||
return BigDecimal.ZERO.setScale(scale, RoundingMode.HALF_UP);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Object> buildEmptySearchSummary(String enterCoilNo, LocalDate calcDate) {
|
||||
Map<String, Object> summary = new HashMap<>();
|
||||
summary.put("enterCoilNo", enterCoilNo);
|
||||
summary.put("calcDate", calcDate);
|
||||
summary.put("totalCoils", 0);
|
||||
summary.put("shippedCount", 0);
|
||||
summary.put("unshippedCount", 0);
|
||||
summary.put("totalCost", BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP));
|
||||
summary.put("totalNetWeight", BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
|
||||
summary.put("totalGrossWeight", BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
|
||||
summary.put("avgStorageDays", BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP));
|
||||
return summary;
|
||||
}
|
||||
|
||||
private Map<String, Object> normalizeMaterialCostSummary(Map<String, Object> db, String enterCoilNo, LocalDate calcDate) {
|
||||
Map<String, Object> summary = buildEmptySearchSummary(enterCoilNo, calcDate);
|
||||
if (db == null || db.isEmpty()) {
|
||||
return summary;
|
||||
}
|
||||
summary.put("totalCoils", toBigDecimal(db.get("totalCoils"), 0).intValue());
|
||||
summary.put("shippedCount", toBigDecimal(db.get("shippedCount"), 0).intValue());
|
||||
summary.put("unshippedCount", toBigDecimal(db.get("unshippedCount"), 0).intValue());
|
||||
summary.put("totalCost", toBigDecimal(db.get("totalCost"), 2));
|
||||
summary.put("totalNetWeight", toBigDecimal(db.get("totalNetWeight"), 3));
|
||||
summary.put("totalGrossWeight", toBigDecimal(db.get("totalGrossWeight"), 3));
|
||||
summary.put("avgStorageDays", toBigDecimal(db.get("avgStorageDays"), 2));
|
||||
return summary;
|
||||
}
|
||||
|
||||
private WeightContext resolveWeightContext(WmsMaterialCoil coil) {
|
||||
BigDecimal netWeightKg = coil.getNetWeight() == null ? BigDecimal.ZERO : coil.getNetWeight();
|
||||
BigDecimal grossWeightKg = coil.getGrossWeight() == null ? BigDecimal.ZERO : coil.getGrossWeight();
|
||||
BigDecimal costWeightKg = grossWeightKg.compareTo(BigDecimal.ZERO) > 0 ? grossWeightKg : netWeightKg;
|
||||
return new WeightContext(netWeightKg, grossWeightKg, costWeightKg);
|
||||
}
|
||||
|
||||
|
||||
private static class WeightContext {
|
||||
private static final BigDecimal THOUSAND = BigDecimal.valueOf(1000);
|
||||
private final BigDecimal netKg;
|
||||
private final BigDecimal grossKg;
|
||||
private final BigDecimal costKg;
|
||||
private final BigDecimal netTon;
|
||||
private final BigDecimal grossTon;
|
||||
private final BigDecimal costTon;
|
||||
|
||||
WeightContext(BigDecimal netKg, BigDecimal grossKg, BigDecimal costKg) {
|
||||
this.netKg = netKg;
|
||||
this.grossKg = grossKg;
|
||||
this.costKg = costKg;
|
||||
this.netTon = convertToTon(netKg);
|
||||
this.grossTon = convertToTon(grossKg);
|
||||
this.costTon = convertToTon(costKg);
|
||||
}
|
||||
|
||||
private BigDecimal convertToTon(BigDecimal kg) {
|
||||
if (kg == null || kg.compareTo(BigDecimal.ZERO) <= 0) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
return kg.divide(THOUSAND, 3, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
boolean isValid() {
|
||||
return costKg != null && costKg.compareTo(BigDecimal.ZERO) > 0;
|
||||
}
|
||||
|
||||
boolean useGross() {
|
||||
return grossKg != null && grossKg.compareTo(BigDecimal.ZERO) > 0;
|
||||
}
|
||||
|
||||
BigDecimal getNetKg() {
|
||||
return netKg;
|
||||
}
|
||||
|
||||
BigDecimal getGrossKg() {
|
||||
return grossKg;
|
||||
}
|
||||
|
||||
BigDecimal getNetTon() {
|
||||
return netTon;
|
||||
}
|
||||
|
||||
BigDecimal getGrossTon() {
|
||||
return grossTon;
|
||||
}
|
||||
|
||||
BigDecimal getCostTon() {
|
||||
return costTon;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
package com.klp.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.klp.common.core.page.TableDataInfo;
|
||||
import com.klp.common.core.domain.PageQuery;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import com.klp.domain.bo.CostStandardConfigBo;
|
||||
import com.klp.domain.vo.CostStandardConfigVo;
|
||||
import com.klp.domain.CostStandardConfig;
|
||||
import com.klp.mapper.CostStandardConfigMapper;
|
||||
import com.klp.service.ICostStandardConfigService;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 成本标准配置表Service业务层处理
|
||||
*
|
||||
* @author klp
|
||||
* @date 2025-11-25
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class CostStandardConfigServiceImpl implements ICostStandardConfigService {
|
||||
|
||||
private final CostStandardConfigMapper baseMapper;
|
||||
|
||||
/**
|
||||
* 查询成本标准配置表
|
||||
*/
|
||||
@Override
|
||||
public CostStandardConfigVo queryById(Long configId) {
|
||||
return baseMapper.selectVoById(configId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询成本标准配置表列表
|
||||
*/
|
||||
@Override
|
||||
public TableDataInfo<CostStandardConfigVo> queryPageList(CostStandardConfigBo bo, PageQuery pageQuery) {
|
||||
LambdaQueryWrapper<CostStandardConfig> lqw = buildQueryWrapper(bo);
|
||||
Page<CostStandardConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
||||
return TableDataInfo.build(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询成本标准配置表列表
|
||||
*/
|
||||
@Override
|
||||
public List<CostStandardConfigVo> queryList(CostStandardConfigBo bo) {
|
||||
LambdaQueryWrapper<CostStandardConfig> lqw = buildQueryWrapper(bo);
|
||||
return baseMapper.selectVoList(lqw);
|
||||
}
|
||||
|
||||
private LambdaQueryWrapper<CostStandardConfig> buildQueryWrapper(CostStandardConfigBo bo) {
|
||||
LambdaQueryWrapper<CostStandardConfig> lqw = Wrappers.lambdaQuery();
|
||||
lqw.eq(bo.getConfigId() != null, CostStandardConfig::getConfigId, bo.getConfigId());
|
||||
lqw.eq(bo.getUnitCost() != null, CostStandardConfig::getUnitCost, bo.getUnitCost());
|
||||
lqw.eq(bo.getEffectiveDate() != null, CostStandardConfig::getEffectiveDate, bo.getEffectiveDate());
|
||||
lqw.eq(bo.getStatus() != null, CostStandardConfig::getStatus, bo.getStatus());
|
||||
lqw.orderByDesc(CostStandardConfig::getEffectiveDate);
|
||||
return lqw;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增成本标准配置表
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean insertByBo(CostStandardConfigBo bo) {
|
||||
// 如果新标准立即生效,需要将当前有效的标准标记为失效
|
||||
if (bo.getEffectiveDate() != null && !bo.getEffectiveDate().isAfter(LocalDate.now())) {
|
||||
// 将当前有效的标准标记为失效
|
||||
CostStandardConfigVo current = queryCurrentEffective();
|
||||
if (current != null) {
|
||||
CostStandardConfig update = new CostStandardConfig();
|
||||
update.setConfigId(current.getConfigId());
|
||||
update.setExpireDate(LocalDate.now().minusDays(1));
|
||||
update.setStatus(0);
|
||||
baseMapper.updateById(update);
|
||||
}
|
||||
}
|
||||
|
||||
CostStandardConfig add = BeanUtil.toBean(bo, CostStandardConfig.class);
|
||||
if (add.getStatus() == null) {
|
||||
add.setStatus(1); // 默认有效
|
||||
}
|
||||
validEntityBeforeSave(add);
|
||||
return baseMapper.insert(add) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改成本标准配置表
|
||||
*/
|
||||
@Override
|
||||
public Boolean updateByBo(CostStandardConfigBo bo) {
|
||||
CostStandardConfig update = BeanUtil.toBean(bo, CostStandardConfig.class);
|
||||
validEntityBeforeSave(update);
|
||||
return baseMapper.updateById(update) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存前的数据校验
|
||||
*/
|
||||
private void validEntityBeforeSave(CostStandardConfig entity) {
|
||||
// TODO 做一些数据校验,如唯一约束
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验并批量删除成本标准配置表信息
|
||||
*/
|
||||
@Override
|
||||
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
|
||||
if (isValid) {
|
||||
// TODO 做一些业务上的校验,判断是否需要校验
|
||||
}
|
||||
return baseMapper.deleteBatchIds(ids) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询当前有效的成本标准
|
||||
*/
|
||||
@Override
|
||||
public CostStandardConfigVo queryCurrentEffective() {
|
||||
return baseMapper.selectCurrentEffective();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询指定日期的有效成本标准
|
||||
*/
|
||||
@Override
|
||||
public CostStandardConfigVo queryEffectiveByDate(LocalDate date) {
|
||||
return baseMapper.selectEffectiveByDate(date);
|
||||
}
|
||||
}
|
||||
|
||||
54
klp-wms/src/main/java/com/klp/task/CostCoilDailyTask.java
Normal file
54
klp-wms/src/main/java/com/klp/task/CostCoilDailyTask.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package com.klp.task;
|
||||
|
||||
import com.klp.service.ICostCoilDailyService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* 成本日统计定时任务
|
||||
*
|
||||
* 每天凌晨 2 点执行:
|
||||
* 1. 按钢卷维度生成前一日的日成本记录;
|
||||
* 2. 按入场钢卷号维度生成前一日的日成本记录。
|
||||
*
|
||||
* 说明:
|
||||
* - 具体的计算逻辑在 {@link ICostCoilDailyService#calculateDailyCost(LocalDate)} 和
|
||||
* {@link ICostCoilDailyService#calculateDailyCostByEnterCoilNo(LocalDate)} 中实现。
|
||||
* - 当前任务只负责在固定时间触发这两个方法。
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
@Component
|
||||
public class CostCoilDailyTask {
|
||||
|
||||
private final ICostCoilDailyService costCoilDailyService;
|
||||
|
||||
/**
|
||||
* 每天凌晨 2:00 计算前一日钢卷日成本(按钢卷维度)
|
||||
*/
|
||||
@Scheduled(cron = "0 0 2 * * ?")
|
||||
public void calculateDailyCostByCoil() {
|
||||
LocalDate calcDate = LocalDate.now().minusDays(1);
|
||||
log.info("[成本定时任务] 开始按钢卷维度计算日期 {} 的成本", calcDate);
|
||||
int count = costCoilDailyService.calculateDailyCost(calcDate);
|
||||
log.info("[成本定时任务] 按钢卷维度计算日期 {} 的成本完成,成功 {} 条", calcDate, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 每天凌晨 2:10 计算前一日钢卷日成本(按入场钢卷号维度)
|
||||
* 与上一个任务错开几分钟,避免同时占用资源。
|
||||
*/
|
||||
@Scheduled(cron = "0 10 2 * * ?")
|
||||
public void calculateDailyCostByEnterCoilNo() {
|
||||
LocalDate calcDate = LocalDate.now().minusDays(1);
|
||||
log.info("[成本定时任务] 开始按入场钢卷号维度计算日期 {} 的成本", calcDate);
|
||||
int count = costCoilDailyService.calculateDailyCostByEnterCoilNo(calcDate);
|
||||
log.info("[成本定时任务] 按入场钢卷号维度计算日期 {} 的成本完成,成功 {} 个入场钢卷号", calcDate, count);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
523
klp-wms/src/main/resources/mapper/klp/CostCoilDailyMapper.xml
Normal file
523
klp-wms/src/main/resources/mapper/klp/CostCoilDailyMapper.xml
Normal file
@@ -0,0 +1,523 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.klp.mapper.CostCoilDailyMapper">
|
||||
|
||||
<resultMap type="com.klp.domain.CostCoilDaily" id="CostCoilDailyResult">
|
||||
<result property="costId" column="cost_id"/>
|
||||
<result property="coilId" column="coil_id"/>
|
||||
<result property="currentCoilNo" column="current_coil_no"/>
|
||||
<result property="calcDate" column="calc_date"/>
|
||||
<result property="netWeight" column="net_weight"/>
|
||||
<result property="unitCost" column="unit_cost"/>
|
||||
<result property="dailyCost" column="daily_cost"/>
|
||||
<result property="storageDays" column="storage_days"/>
|
||||
<result property="totalCost" column="total_cost"/>
|
||||
<result property="warehouseId" column="warehouse_id"/>
|
||||
<result property="itemType" column="item_type"/>
|
||||
<result property="materialType" column="material_type"/>
|
||||
<result property="createTime" column="create_time"/>
|
||||
</resultMap>
|
||||
|
||||
<resultMap type="com.klp.domain.vo.CostCoilDailyVo" id="CostCoilDailyVoResult">
|
||||
<result property="costId" column="cost_id"/>
|
||||
<result property="coilId" column="coil_id"/>
|
||||
<result property="currentCoilNo" column="current_coil_no"/>
|
||||
<result property="calcDate" column="calc_date"/>
|
||||
<result property="netWeight" column="net_weight"/>
|
||||
<result property="unitCost" column="unit_cost"/>
|
||||
<result property="dailyCost" column="daily_cost"/>
|
||||
<result property="storageDays" column="storage_days"/>
|
||||
<result property="totalCost" column="total_cost"/>
|
||||
<result property="warehouseId" column="warehouse_id"/>
|
||||
<result property="warehouseName" column="warehouse_name"/>
|
||||
<result property="itemType" column="item_type"/>
|
||||
<result property="materialType" column="material_type"/>
|
||||
</resultMap>
|
||||
|
||||
<!-- 自定义查询,关联库区名称 -->
|
||||
<select id="selectVoPage" resultType="com.klp.domain.vo.CostCoilDailyVo">
|
||||
SELECT
|
||||
ccd.cost_id,
|
||||
ccd.coil_id,
|
||||
ccd.current_coil_no,
|
||||
ccd.calc_date,
|
||||
ccd.net_weight,
|
||||
ccd.unit_cost,
|
||||
ccd.daily_cost,
|
||||
ccd.storage_days,
|
||||
ccd.total_cost,
|
||||
ccd.warehouse_id,
|
||||
w.warehouse_name,
|
||||
ccd.item_type,
|
||||
ccd.material_type
|
||||
FROM cost_coil_daily ccd
|
||||
LEFT JOIN wms_warehouse w ON ccd.warehouse_id = w.warehouse_id AND w.del_flag = 0
|
||||
${ew.customSqlSegment}
|
||||
</select>
|
||||
|
||||
<select id="selectVoList" resultType="com.klp.domain.vo.CostCoilDailyVo">
|
||||
SELECT
|
||||
ccd.cost_id,
|
||||
ccd.coil_id,
|
||||
ccd.current_coil_no,
|
||||
ccd.calc_date,
|
||||
ccd.net_weight,
|
||||
ccd.unit_cost,
|
||||
ccd.daily_cost,
|
||||
ccd.storage_days,
|
||||
ccd.total_cost,
|
||||
ccd.warehouse_id,
|
||||
w.warehouse_name,
|
||||
ccd.item_type,
|
||||
ccd.material_type
|
||||
FROM cost_coil_daily ccd
|
||||
LEFT JOIN wms_warehouse w ON ccd.warehouse_id = w.warehouse_id AND w.del_flag = 0
|
||||
${ew.customSqlSegment}
|
||||
</select>
|
||||
|
||||
<select id="selectVoById" resultType="com.klp.domain.vo.CostCoilDailyVo">
|
||||
SELECT
|
||||
ccd.cost_id,
|
||||
ccd.coil_id,
|
||||
ccd.current_coil_no,
|
||||
ccd.calc_date,
|
||||
ccd.net_weight,
|
||||
ccd.unit_cost,
|
||||
ccd.daily_cost,
|
||||
ccd.storage_days,
|
||||
ccd.total_cost,
|
||||
ccd.warehouse_id,
|
||||
w.warehouse_name,
|
||||
ccd.item_type,
|
||||
ccd.material_type
|
||||
FROM cost_coil_daily ccd
|
||||
LEFT JOIN wms_warehouse w ON ccd.warehouse_id = w.warehouse_id AND w.del_flag = 0
|
||||
WHERE ccd.cost_id = #{costId}
|
||||
</select>
|
||||
|
||||
<!-- 查询成本统计汇总(基于当前 wms_material_coil 快照) -->
|
||||
<select id="selectCostSummary" resultType="java.util.HashMap">
|
||||
SELECT
|
||||
COUNT(*) AS totalCoils,
|
||||
SUM(t.net_weight_ton) AS totalNetWeight,
|
||||
SUM(t.total_cost_amount) AS totalCost,
|
||||
AVG(t.storage_days) AS avgStorageDays,
|
||||
SUM(t.daily_cost_amount) AS totalDailyCost
|
||||
FROM (
|
||||
SELECT
|
||||
m.coil_id,
|
||||
IFNULL(m.net_weight, 0) / 1000 AS net_weight_ton,
|
||||
(DATEDIFF(CURDATE(), IFNULL(m.create_time, CURDATE())) + 1) AS storage_days,
|
||||
CASE
|
||||
WHEN cs.unit_cost IS NULL THEN 0
|
||||
ELSE (
|
||||
CASE
|
||||
WHEN IFNULL(m.gross_weight, 0) > 0 THEN m.gross_weight
|
||||
ELSE IFNULL(m.net_weight, 0)
|
||||
END
|
||||
) / 1000 * cs.unit_cost
|
||||
END AS daily_cost_amount,
|
||||
CASE
|
||||
WHEN cs.unit_cost IS NULL THEN 0
|
||||
ELSE (
|
||||
CASE
|
||||
WHEN IFNULL(m.gross_weight, 0) > 0 THEN m.gross_weight
|
||||
ELSE IFNULL(m.net_weight, 0)
|
||||
END
|
||||
) / 1000 * cs.unit_cost * (DATEDIFF(CURDATE(), IFNULL(m.create_time, CURDATE())) + 1)
|
||||
END AS total_cost_amount
|
||||
FROM wms_material_coil m
|
||||
LEFT JOIN cost_standard_config cs
|
||||
ON cs.status = 1
|
||||
AND DATE(m.create_time) >= cs.effective_date
|
||||
AND (cs.expire_date IS NULL OR DATE(m.create_time) <= cs.expire_date)
|
||||
WHERE m.data_type = 1
|
||||
AND m.del_flag = 0
|
||||
AND m.export_time IS NULL
|
||||
<if test="warehouseId != null">
|
||||
AND m.warehouse_id = #{warehouseId}
|
||||
</if>
|
||||
<if test="itemType != null and itemType != ''">
|
||||
AND m.item_type = #{itemType}
|
||||
</if>
|
||||
<if test="materialType != null and materialType != ''">
|
||||
AND m.material_type = #{materialType}
|
||||
</if>
|
||||
) t
|
||||
</select>
|
||||
|
||||
<!-- 按库区统计成本(基于当前 wms_material_coil 快照) -->
|
||||
<select id="selectCostByWarehouse" resultType="java.util.HashMap">
|
||||
SELECT
|
||||
t.warehouse_id AS warehouseId,
|
||||
COALESCE(w.warehouse_name, '-') AS warehouseName,
|
||||
COUNT(*) AS coilCount,
|
||||
SUM(t.total_cost_amount) AS totalCost,
|
||||
AVG(t.storage_days) AS avgStorageDays,
|
||||
SUM(t.net_weight_ton) AS totalNetWeight
|
||||
FROM (
|
||||
SELECT
|
||||
m.coil_id,
|
||||
m.warehouse_id,
|
||||
IFNULL(m.net_weight, 0) / 1000 AS net_weight_ton,
|
||||
(DATEDIFF(CURDATE(), IFNULL(m.create_time, CURDATE())) + 1) AS storage_days,
|
||||
CASE
|
||||
WHEN cs.unit_cost IS NULL THEN 0
|
||||
ELSE (
|
||||
CASE
|
||||
WHEN IFNULL(m.gross_weight, 0) > 0 THEN m.gross_weight
|
||||
ELSE IFNULL(m.net_weight, 0)
|
||||
END
|
||||
) / 1000 * cs.unit_cost * (DATEDIFF(CURDATE(), IFNULL(m.create_time, CURDATE())) + 1)
|
||||
END AS total_cost_amount
|
||||
FROM wms_material_coil m
|
||||
LEFT JOIN cost_standard_config cs
|
||||
ON cs.status = 1
|
||||
AND DATE(m.create_time) >= cs.effective_date
|
||||
AND (cs.expire_date IS NULL OR DATE(m.create_time) <= cs.expire_date)
|
||||
WHERE m.data_type = 1
|
||||
AND m.del_flag = 0
|
||||
AND m.export_time IS NULL
|
||||
) t
|
||||
LEFT JOIN wms_warehouse w ON t.warehouse_id = w.warehouse_id
|
||||
GROUP BY t.warehouse_id, w.warehouse_name
|
||||
ORDER BY totalCost DESC
|
||||
</select>
|
||||
|
||||
<!-- 按物品类型统计成本(基于当前 wms_material_coil 快照) -->
|
||||
<select id="selectCostByItemType" resultType="java.util.HashMap">
|
||||
SELECT
|
||||
t.item_type AS itemType,
|
||||
COUNT(*) AS coilCount,
|
||||
SUM(t.total_cost_amount) AS totalCost,
|
||||
AVG(t.storage_days) AS avgStorageDays,
|
||||
SUM(t.net_weight_ton) AS totalNetWeight
|
||||
FROM (
|
||||
SELECT
|
||||
m.coil_id,
|
||||
m.item_type,
|
||||
IFNULL(m.net_weight, 0) / 1000 AS net_weight_ton,
|
||||
(DATEDIFF(CURDATE(), IFNULL(m.create_time, CURDATE())) + 1) AS storage_days,
|
||||
CASE
|
||||
WHEN cs.unit_cost IS NULL THEN 0
|
||||
ELSE (
|
||||
CASE
|
||||
WHEN IFNULL(m.gross_weight, 0) > 0 THEN m.gross_weight
|
||||
ELSE IFNULL(m.net_weight, 0)
|
||||
END
|
||||
) / 1000 * cs.unit_cost * (DATEDIFF(CURDATE(), IFNULL(m.create_time, CURDATE())) + 1)
|
||||
END AS total_cost_amount
|
||||
FROM wms_material_coil m
|
||||
LEFT JOIN cost_standard_config cs
|
||||
ON cs.status = 1
|
||||
AND DATE(m.create_time) >= cs.effective_date
|
||||
AND (cs.expire_date IS NULL OR DATE(m.create_time) <= cs.expire_date)
|
||||
WHERE m.data_type = 1
|
||||
AND m.del_flag = 0
|
||||
AND m.export_time IS NULL
|
||||
) t
|
||||
GROUP BY t.item_type
|
||||
ORDER BY totalCost DESC
|
||||
</select>
|
||||
|
||||
<!-- 检查指定日期和钢卷的成本记录是否存在 -->
|
||||
<select id="countByCoilIdAndDate" resultType="int">
|
||||
SELECT COUNT(1)
|
||||
FROM cost_coil_daily
|
||||
WHERE coil_id = #{coilId}
|
||||
AND calc_date = #{calcDate}
|
||||
</select>
|
||||
|
||||
<!-- 按入场钢卷号统计成本 -->
|
||||
<select id="selectCostByEnterCoilNo" resultType="java.util.HashMap">
|
||||
SELECT
|
||||
mc.enter_coil_no AS enterCoilNo,
|
||||
COUNT(DISTINCT mc.coil_id) AS coilCount,
|
||||
COUNT(DISTINCT CASE WHEN mc.export_time IS NULL THEN mc.coil_id END) AS unshippedCount,
|
||||
COUNT(DISTINCT CASE WHEN mc.export_time IS NOT NULL THEN mc.coil_id END) AS shippedCount,
|
||||
SUM(ccd.total_cost) AS totalCost,
|
||||
SUM(ccd.net_weight) AS totalNetWeight,
|
||||
AVG(ccd.storage_days) AS avgStorageDays,
|
||||
MAX(ccd.calc_date) AS lastCalcDate
|
||||
FROM cost_coil_daily ccd
|
||||
INNER JOIN wms_material_coil mc ON ccd.coil_id = mc.coil_id
|
||||
WHERE ccd.calc_date BETWEEN #{startDate} AND #{endDate}
|
||||
AND mc.data_type = 1
|
||||
AND mc.del_flag = 0
|
||||
<if test="enterCoilNo != null and enterCoilNo != ''">
|
||||
AND mc.enter_coil_no = #{enterCoilNo}
|
||||
</if>
|
||||
GROUP BY mc.enter_coil_no
|
||||
ORDER BY totalCost DESC
|
||||
</select>
|
||||
|
||||
<select id="selectStockpileByEnterCoilNo" resultType="java.util.HashMap">
|
||||
SELECT
|
||||
t.enter_coil_no AS enterCoilNo,
|
||||
COUNT(*) AS coilCount,
|
||||
SUM(t.net_weight_ton) AS totalNetWeight,
|
||||
SUM(t.gross_weight_ton) AS totalGrossWeight,
|
||||
SUM(t.cost_amount) AS totalCost,
|
||||
AVG(t.storage_days) AS avgStorageDays,
|
||||
MAX(t.storage_days) AS maxStorageDays
|
||||
FROM (
|
||||
SELECT
|
||||
m.enter_coil_no,
|
||||
IFNULL(m.net_weight, 0) / 1000 AS net_weight_ton,
|
||||
IFNULL(m.gross_weight, 0) / 1000 AS gross_weight_ton,
|
||||
CASE
|
||||
WHEN cs.unit_cost IS NULL THEN 0
|
||||
ELSE (
|
||||
CASE
|
||||
WHEN IFNULL(m.gross_weight, 0) > 0 THEN m.gross_weight
|
||||
ELSE IFNULL(m.net_weight, 0)
|
||||
END
|
||||
) / 1000 * cs.unit_cost * (DATEDIFF(CURDATE(), IFNULL(m.create_time, CURDATE())) + 1)
|
||||
END AS cost_amount,
|
||||
(DATEDIFF(CURDATE(), IFNULL(m.create_time, CURDATE())) + 1) AS storage_days
|
||||
FROM wms_material_coil m
|
||||
LEFT JOIN cost_standard_config cs
|
||||
ON cs.status = 1
|
||||
AND DATE(m.create_time) >= cs.effective_date
|
||||
AND (cs.expire_date IS NULL OR DATE(m.create_time) <= cs.expire_date)
|
||||
WHERE m.data_type = 1
|
||||
AND m.del_flag = 0
|
||||
AND m.export_time IS NULL
|
||||
<if test="enterCoilNo != null and enterCoilNo != ''">
|
||||
AND m.enter_coil_no LIKE CONCAT(#{enterCoilNo}, '%')
|
||||
</if>
|
||||
) t
|
||||
GROUP BY t.enter_coil_no
|
||||
ORDER BY t.enter_coil_no DESC
|
||||
LIMIT #{offset}, #{pageSize}
|
||||
</select>
|
||||
|
||||
<!-- 囤积成本(按入场钢卷号聚合)列表总数 -->
|
||||
<select id="countStockpileByEnterCoilNo" resultType="long">
|
||||
SELECT
|
||||
COUNT(*) AS cnt
|
||||
FROM (
|
||||
SELECT m.enter_coil_no
|
||||
FROM wms_material_coil m
|
||||
WHERE m.data_type = 1
|
||||
AND m.del_flag = 0
|
||||
AND m.export_time IS NULL
|
||||
<if test="enterCoilNo != null and enterCoilNo != ''">
|
||||
AND m.enter_coil_no LIKE CONCAT(#{enterCoilNo}, '%')
|
||||
</if>
|
||||
GROUP BY m.enter_coil_no
|
||||
) t
|
||||
</select>
|
||||
|
||||
<!-- 囤积成本(按入场钢卷号聚合)汇总 -->
|
||||
<select id="selectStockpileSummaryByEnterCoilNo" resultType="java.util.HashMap">
|
||||
SELECT
|
||||
SUM(t.net_weight_ton) AS totalNetWeight,
|
||||
SUM(t.gross_weight_ton) AS totalGrossWeight,
|
||||
SUM(t.cost_amount) AS totalCost,
|
||||
AVG(t.storage_days) AS avgStorageDays
|
||||
FROM (
|
||||
SELECT
|
||||
IFNULL(m.net_weight, 0) / 1000 AS net_weight_ton,
|
||||
IFNULL(m.gross_weight, 0) / 1000 AS gross_weight_ton,
|
||||
CASE
|
||||
WHEN cs.unit_cost IS NULL THEN 0
|
||||
ELSE (
|
||||
CASE
|
||||
WHEN IFNULL(m.gross_weight, 0) > 0 THEN m.gross_weight
|
||||
ELSE IFNULL(m.net_weight, 0)
|
||||
END
|
||||
) / 1000 * cs.unit_cost * (DATEDIFF(CURDATE(), IFNULL(m.create_time, CURDATE())) + 1)
|
||||
END AS cost_amount,
|
||||
(DATEDIFF(CURDATE(), IFNULL(m.create_time, CURDATE())) + 1) AS storage_days
|
||||
FROM wms_material_coil m
|
||||
LEFT JOIN cost_standard_config cs
|
||||
ON cs.status = 1
|
||||
AND DATE(m.create_time) >= cs.effective_date
|
||||
AND (cs.expire_date IS NULL OR DATE(m.create_time) <= cs.expire_date)
|
||||
WHERE m.data_type = 1
|
||||
AND m.del_flag = 0
|
||||
AND m.export_time IS NULL
|
||||
<if test="enterCoilNo != null and enterCoilNo != ''">
|
||||
AND m.enter_coil_no LIKE CONCAT(#{enterCoilNo}, '%')
|
||||
</if>
|
||||
) t
|
||||
</select>
|
||||
|
||||
<select id="selectOverviewFromMaterialCoil" resultType="java.util.HashMap">
|
||||
SELECT
|
||||
COUNT(*) AS totalCoils,
|
||||
SUM(t.net_weight_ton) AS totalNetWeight,
|
||||
SUM(t.gross_weight_ton) AS totalGrossWeight,
|
||||
AVG(t.storage_days) AS avgStorageDays,
|
||||
SUM(t.cost_amount) AS totalCost
|
||||
FROM (
|
||||
SELECT
|
||||
IFNULL(m.net_weight, 0) / 1000 AS net_weight_ton,
|
||||
IFNULL(m.gross_weight, 0) / 1000 AS gross_weight_ton,
|
||||
(DATEDIFF(CURDATE(), IFNULL(m.create_time, CURDATE())) + 1) AS storage_days,
|
||||
CASE
|
||||
WHEN cs.unit_cost IS NULL THEN 0
|
||||
ELSE (
|
||||
CASE
|
||||
WHEN IFNULL(m.gross_weight, 0) > 0 THEN m.gross_weight
|
||||
ELSE IFNULL(m.net_weight, 0)
|
||||
END
|
||||
) / 1000 * cs.unit_cost * (DATEDIFF(CURDATE(), IFNULL(m.create_time, CURDATE())) + 1)
|
||||
END AS cost_amount
|
||||
FROM wms_material_coil m
|
||||
LEFT JOIN cost_standard_config cs
|
||||
ON cs.status = 1
|
||||
AND DATE(m.create_time) >= cs.effective_date
|
||||
AND (cs.expire_date IS NULL OR DATE(m.create_time) <= cs.expire_date)
|
||||
WHERE m.data_type = 1
|
||||
AND m.del_flag = 0
|
||||
AND m.export_time IS NULL
|
||||
) t
|
||||
</select>
|
||||
|
||||
<!-- 成本趋势(按日统计 cost_coil_daily) -->
|
||||
<select id="selectCostTrend" resultType="java.util.HashMap">
|
||||
SELECT
|
||||
ccd.calc_date AS date,
|
||||
SUM(ccd.total_cost) AS totalCost,
|
||||
SUM(ccd.net_weight) AS totalNetWeight,
|
||||
SUM(ccd.daily_cost) AS totalDailyCost
|
||||
FROM cost_coil_daily ccd
|
||||
WHERE 1 = 1
|
||||
<if test="startDate != null">
|
||||
AND ccd.calc_date >= #{startDate}
|
||||
</if>
|
||||
<if test="endDate != null">
|
||||
AND ccd.calc_date <= #{endDate}
|
||||
</if>
|
||||
GROUP BY ccd.calc_date
|
||||
ORDER BY ccd.calc_date
|
||||
</select>
|
||||
|
||||
<!-- ==================== 成本检索(实时计算) ==================== -->
|
||||
<sql id="MaterialCoilCostBase">
|
||||
SELECT
|
||||
m.enter_coil_no,
|
||||
m.coil_id,
|
||||
m.current_coil_no,
|
||||
IFNULL(m.net_weight, 0) / 1000 AS net_weight_ton,
|
||||
IFNULL(m.gross_weight, 0) / 1000 AS gross_weight_ton,
|
||||
CASE
|
||||
WHEN IFNULL(m.gross_weight, 0) > 0 THEN 'gross'
|
||||
ELSE 'net'
|
||||
END AS weight_basis,
|
||||
CASE
|
||||
WHEN IFNULL(m.gross_weight, 0) > 0 THEN IFNULL(m.gross_weight, 0)
|
||||
ELSE IFNULL(m.net_weight, 0)
|
||||
END / 1000 AS charge_weight_ton,
|
||||
DATE(m.create_time) AS start_date,
|
||||
CASE
|
||||
WHEN m.export_time IS NOT NULL THEN DATE_SUB(DATE(m.export_time), INTERVAL 1 DAY)
|
||||
WHEN #{calcDate} IS NOT NULL THEN #{calcDate}
|
||||
ELSE CURDATE()
|
||||
END AS raw_end_date,
|
||||
CASE
|
||||
WHEN m.export_time IS NULL THEN 0
|
||||
ELSE 1
|
||||
END AS is_shipped,
|
||||
IFNULL(cs.unit_cost, 0) AS unit_cost,
|
||||
m.warehouse_id,
|
||||
COALESCE(w.warehouse_name, '-') AS warehouse_name
|
||||
FROM wms_material_coil m
|
||||
LEFT JOIN cost_standard_config cs
|
||||
ON cs.status = 1
|
||||
AND DATE(m.create_time) >= cs.effective_date
|
||||
AND (cs.expire_date IS NULL OR DATE(m.create_time) <= cs.expire_date)
|
||||
LEFT JOIN wms_warehouse w ON w.warehouse_id = m.warehouse_id AND w.del_flag = 0
|
||||
WHERE m.data_type = 1
|
||||
AND m.del_flag = 0
|
||||
AND m.enter_coil_no IS NOT NULL
|
||||
<if test="enterCoilNo != null and enterCoilNo != ''">
|
||||
AND m.enter_coil_no LIKE CONCAT(#{enterCoilNo}, '%')
|
||||
</if>
|
||||
</sql>
|
||||
|
||||
<sql id="MaterialCoilCostCalc">
|
||||
SELECT
|
||||
base.*,
|
||||
CASE
|
||||
WHEN base.raw_end_date < base.start_date THEN base.start_date
|
||||
ELSE base.raw_end_date
|
||||
END AS end_date,
|
||||
CASE
|
||||
WHEN DATEDIFF(
|
||||
CASE
|
||||
WHEN base.raw_end_date < base.start_date THEN base.start_date
|
||||
ELSE base.raw_end_date
|
||||
END,
|
||||
base.start_date
|
||||
) < 0 THEN 1
|
||||
ELSE DATEDIFF(
|
||||
CASE
|
||||
WHEN base.raw_end_date < base.start_date THEN base.start_date
|
||||
ELSE base.raw_end_date
|
||||
END,
|
||||
base.start_date
|
||||
) + 1
|
||||
END AS storage_days
|
||||
FROM (
|
||||
<include refid="MaterialCoilCostBase"/>
|
||||
) base
|
||||
</sql>
|
||||
|
||||
<select id="selectMaterialCostSummary" resultType="java.util.HashMap">
|
||||
SELECT
|
||||
COUNT(*) AS totalCoils,
|
||||
IFNULL(SUM(calc_data.net_weight_ton), 0) AS totalNetWeight,
|
||||
IFNULL(SUM(calc_data.gross_weight_ton), 0) AS totalGrossWeight,
|
||||
IFNULL(SUM(calc_data.charge_weight_ton * calc_data.unit_cost * calc_data.storage_days), 0) AS totalCost,
|
||||
IFNULL(AVG(calc_data.storage_days), 0) AS avgStorageDays,
|
||||
IFNULL(SUM(CASE WHEN calc_data.is_shipped = 1 THEN 1 ELSE 0 END), 0) AS shippedCount,
|
||||
IFNULL(SUM(CASE WHEN calc_data.is_shipped = 0 THEN 1 ELSE 0 END), 0) AS unshippedCount
|
||||
FROM (
|
||||
<include refid="MaterialCoilCostCalc"/>
|
||||
) calc_data
|
||||
</select>
|
||||
|
||||
<select id="countMaterialCostCards" resultType="long">
|
||||
SELECT COUNT(1)
|
||||
FROM (
|
||||
<include refid="MaterialCoilCostCalc"/>
|
||||
) calc_data
|
||||
</select>
|
||||
|
||||
<select id="selectMaterialCostCards" resultType="java.util.HashMap">
|
||||
SELECT
|
||||
calc_data.enter_coil_no AS enterCoilNo,
|
||||
calc_data.coil_id AS coilId,
|
||||
calc_data.current_coil_no AS currentCoilNo,
|
||||
calc_data.net_weight_ton AS netWeightTon,
|
||||
calc_data.gross_weight_ton AS grossWeightTon,
|
||||
calc_data.weight_basis AS weightBasis,
|
||||
calc_data.storage_days AS storageDays,
|
||||
ROUND(calc_data.unit_cost, 2) AS unitCost,
|
||||
ROUND(calc_data.charge_weight_ton * calc_data.unit_cost, 2) AS dailyCost,
|
||||
ROUND(calc_data.charge_weight_ton * calc_data.unit_cost * calc_data.storage_days, 2) AS totalCost,
|
||||
calc_data.is_shipped AS isShipped,
|
||||
calc_data.start_date AS startDate,
|
||||
calc_data.end_date AS endDate,
|
||||
calc_data.warehouse_id AS warehouseId,
|
||||
calc_data.warehouse_name AS warehouseName
|
||||
FROM (
|
||||
<include refid="MaterialCoilCostCalc"/>
|
||||
) calc_data
|
||||
ORDER BY calc_data.start_date DESC, calc_data.current_coil_no DESC
|
||||
LIMIT #{offset}, #{pageSize}
|
||||
</select>
|
||||
|
||||
<!-- 删除指定日期的成本记录 -->
|
||||
<delete id="deleteByCalcDate">
|
||||
DELETE FROM cost_coil_daily
|
||||
WHERE calc_date = #{calcDate}
|
||||
</delete>
|
||||
|
||||
</mapper>
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.klp.mapper.CostStandardConfigMapper">
|
||||
|
||||
<resultMap type="com.klp.domain.CostStandardConfig" id="CostStandardConfigResult">
|
||||
<result property="configId" column="config_id"/>
|
||||
<result property="unitCost" column="unit_cost"/>
|
||||
<result property="effectiveDate" column="effective_date"/>
|
||||
<result property="expireDate" column="expire_date"/>
|
||||
<result property="status" column="status"/>
|
||||
<result property="delFlag" column="del_flag"/>
|
||||
<result property="remark" column="remark"/>
|
||||
<result property="createTime" column="create_time"/>
|
||||
<result property="createBy" column="create_by"/>
|
||||
<result property="updateTime" column="update_time"/>
|
||||
<result property="updateBy" column="update_by"/>
|
||||
</resultMap>
|
||||
|
||||
<resultMap type="com.klp.domain.vo.CostStandardConfigVo" id="CostStandardConfigVoResult">
|
||||
<result property="configId" column="config_id"/>
|
||||
<result property="unitCost" column="unit_cost"/>
|
||||
<result property="effectiveDate" column="effective_date"/>
|
||||
<result property="expireDate" column="expire_date"/>
|
||||
<result property="status" column="status"/>
|
||||
<result property="remark" column="remark"/>
|
||||
<result property="createTime" column="create_time"/>
|
||||
<result property="createBy" column="create_by"/>
|
||||
<result property="updateTime" column="update_time"/>
|
||||
<result property="updateBy" column="update_by"/>
|
||||
</resultMap>
|
||||
|
||||
<!-- 查询指定日期的有效成本标准 -->
|
||||
<select id="selectEffectiveByDate" resultMap="CostStandardConfigVoResult">
|
||||
SELECT *
|
||||
FROM cost_standard_config
|
||||
WHERE del_flag = 0
|
||||
AND status = 1
|
||||
AND effective_date <= #{date}
|
||||
AND (expire_date IS NULL OR expire_date >= #{date})
|
||||
ORDER BY effective_date DESC
|
||||
LIMIT 1
|
||||
</select>
|
||||
|
||||
<!-- 查询当前有效的成本标准 -->
|
||||
<select id="selectCurrentEffective" resultMap="CostStandardConfigVoResult">
|
||||
SELECT *
|
||||
FROM cost_standard_config
|
||||
WHERE del_flag = 0
|
||||
AND status = 1
|
||||
AND effective_date <= CURDATE()
|
||||
AND (expire_date IS NULL OR expire_date >= CURDATE())
|
||||
ORDER BY effective_date DESC
|
||||
LIMIT 1
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
|
||||
Reference in New Issue
Block a user