mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 15:55:18 +08:00
feat: 增加库存查询新接口
This commit is contained in:
@@ -7,6 +7,7 @@ import cn.lihongjie.coal.base.dto.IdRequest;
|
||||
import cn.lihongjie.coal.warehouseReceipt.dto.CreateWarehouseReceiptDto;
|
||||
import cn.lihongjie.coal.warehouseReceipt.dto.UpdateWarehouseReceiptDto;
|
||||
import cn.lihongjie.coal.warehouseReceipt.dto.WarehouseReceiptDto;
|
||||
import cn.lihongjie.coal.warehouseReceipt.dto.WarehouseReportRequest;
|
||||
import cn.lihongjie.coal.warehouseReceipt.service.WarehouseReceiptService;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -64,4 +65,11 @@ public class WarehouseReceiptController {
|
||||
this.service.unarchive(request);
|
||||
return true;
|
||||
}
|
||||
|
||||
@PostMapping("/report")
|
||||
public Object report(@RequestBody WarehouseReportRequest request) {
|
||||
return this.service.report(request);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package cn.lihongjie.coal.warehouseReceipt.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
@Data
|
||||
public class WarehouseReportRequest {
|
||||
|
||||
|
||||
|
||||
private LocalDateTime startTime;
|
||||
private LocalDateTime endTime;
|
||||
|
||||
|
||||
private String barCode;
|
||||
|
||||
private String goodsName;
|
||||
|
||||
|
||||
private List<String> warehouseIds;
|
||||
|
||||
private List<String> categoryIds;
|
||||
|
||||
private List<String> brandIds;
|
||||
|
||||
private List<String> unitIds;
|
||||
|
||||
private List<String> shelveIds;
|
||||
|
||||
private List<String> supplierIds;
|
||||
|
||||
private List<String> goodsIds;
|
||||
|
||||
|
||||
|
||||
private Integer pageNo = 0;
|
||||
private Integer pageSize = Integer.MAX_VALUE;
|
||||
|
||||
private String organizationId;
|
||||
|
||||
private Boolean excludeEmpty = false;
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import cn.lihongjie.coal.base.dto.CommonQuery;
|
||||
import cn.lihongjie.coal.base.dto.IdRequest;
|
||||
import cn.lihongjie.coal.base.service.BaseService;
|
||||
import cn.lihongjie.coal.common.Ctx;
|
||||
import cn.lihongjie.coal.common.FreeMakerUtils;
|
||||
import cn.lihongjie.coal.common.JpaUtils;
|
||||
import cn.lihongjie.coal.common.NumberUtils;
|
||||
import cn.lihongjie.coal.exception.BizException;
|
||||
import cn.lihongjie.coal.orderNoRule.service.OrderNoRuleService;
|
||||
@@ -14,12 +16,18 @@ import cn.lihongjie.coal.warehouseGoods.service.WarehouseGoodsService;
|
||||
import cn.lihongjie.coal.warehouseReceipt.dto.CreateWarehouseReceiptDto;
|
||||
import cn.lihongjie.coal.warehouseReceipt.dto.UpdateWarehouseReceiptDto;
|
||||
import cn.lihongjie.coal.warehouseReceipt.dto.WarehouseReceiptDto;
|
||||
import cn.lihongjie.coal.warehouseReceipt.dto.WarehouseReportRequest;
|
||||
import cn.lihongjie.coal.warehouseReceipt.entity.WarehouseReceiptEntity;
|
||||
import cn.lihongjie.coal.warehouseReceipt.mapper.WarehouseReceiptMapper;
|
||||
import cn.lihongjie.coal.warehouseReceipt.repository.WarehouseReceiptRepository;
|
||||
import cn.lihongjie.coal.warehouseReceiptDetail.entity.WarehouseReceiptDetailEntity;
|
||||
import cn.lihongjie.coal.warehouseReceiptDetail.repository.WarehouseReceiptDetailRepository;
|
||||
|
||||
import com.google.common.base.CaseFormat;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import jakarta.persistence.Tuple;
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.CriteriaQuery;
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
@@ -33,6 +41,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
@@ -44,6 +53,8 @@ import java.math.RoundingMode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@@ -515,4 +526,207 @@ public class WarehouseReceiptService
|
||||
|
||||
this.repository.unArchive(dto);
|
||||
}
|
||||
|
||||
@PersistenceContext EntityManager em;
|
||||
|
||||
public Object report(WarehouseReportRequest request){
|
||||
|
||||
request.setOrganizationId(Ctx.currentUser().getOrganizationId());
|
||||
|
||||
String sql =
|
||||
FreeMakerUtils.render(
|
||||
"""
|
||||
|
||||
|
||||
with bq as (select distinct on ( gs.id, w.id) gs.id,
|
||||
gs.create_time,
|
||||
gs.create_user_id,
|
||||
gs.file_ids,
|
||||
gs.update_time,
|
||||
gs.update_user_id,
|
||||
gs.code,
|
||||
gs.name,
|
||||
gs.remarks,
|
||||
gs.sort_key,
|
||||
gs.status,
|
||||
gs.organization_id,
|
||||
gs.bar_code,
|
||||
gs.price1,
|
||||
gs.price2,
|
||||
gs.price3,
|
||||
gs.price4,
|
||||
gs.price5,
|
||||
gs.size,
|
||||
gs.volume,
|
||||
gs.weight,
|
||||
gs.brand_id,
|
||||
gs.category_id,
|
||||
gs.unit_id,
|
||||
gs.parent_id,
|
||||
gs.spec,
|
||||
gs.default_shelve_id,
|
||||
gs.default_warehouse_id,
|
||||
gs.enable_attribute,
|
||||
gs.attributes,
|
||||
gs.enable_shelf_life,
|
||||
gs.shelf_life,
|
||||
gs.default_supplier_id,
|
||||
gs.model,
|
||||
gu.name as unit_name,
|
||||
gu.code as unit_code,
|
||||
gb.name as brand_name,
|
||||
gb.code as brand_code,
|
||||
w.name as warehouse_name,
|
||||
w.code as warehouse_code,
|
||||
gc.code as category_code,
|
||||
gc.name as category_name,
|
||||
|
||||
round((sum(d.number)
|
||||
filter (where d.receipt_type = '0') over (partition by w.id, gs.id))::::numeric,
|
||||
2) as type0_number,
|
||||
round((sum(d.number)
|
||||
filter (where d.receipt_type = '1') over (partition by w.id, gs.id))::::numeric,
|
||||
2) as type1_number,
|
||||
round((sum(d.amount)
|
||||
filter (where d.receipt_type = '0') over (partition by w.id, gs.id))::::numeric,
|
||||
2) as type0_amount,
|
||||
round((sum(d.amount)
|
||||
filter (where d.receipt_type = '1') over (partition by w.id, gs.id))::::numeric,
|
||||
2) as type1_amount,
|
||||
round((sum(d.amount)
|
||||
filter (where d.receipt_type = '0') over (partition by w.id, gs.id) /
|
||||
sum(d.number)
|
||||
filter (where d.receipt_type = '0') over (partition by w.id, gs.id))::::numeric,
|
||||
2) as type0_price,
|
||||
|
||||
round((sum(d.amount)
|
||||
filter (where d.receipt_type = '1') over (partition by w.id, gs.id) /
|
||||
sum(d.number)
|
||||
filter (where d.receipt_type = '1') over (partition by w.id, gs.id))::::numeric,
|
||||
2) as type1_price
|
||||
from t_warehouse_goods gs
|
||||
inner join t_warehouse w on 1 = 1
|
||||
<#if excludeEmpty>
|
||||
inner
|
||||
|
||||
<#else>
|
||||
|
||||
left
|
||||
|
||||
</#if>
|
||||
|
||||
join t_warehouse_receipt_detail d on d.goods_id = gs.id
|
||||
left join t_warehouse_receipt re on d.receipt_id = re.id
|
||||
left join t_warehouse_goods_unit gu on gs.unit_id = gu.id
|
||||
left join t_warehouse_goods_brand gb on gs.brand_id = gb.id
|
||||
left join t_warehouse_goods_category gc on gs.category_id = gc.id
|
||||
|
||||
|
||||
where 1=1
|
||||
<#if startTime??>
|
||||
and re.receipt_date >= :startTime
|
||||
</#if>
|
||||
<#if endTime??>
|
||||
and re.receipt_date <= :endTime
|
||||
</#if>
|
||||
|
||||
<#if supplierIds??>
|
||||
and d.supplier_id in :supplierIds
|
||||
</#if>
|
||||
|
||||
<#if organizationId??>
|
||||
and gs.organization_id = :organizationId
|
||||
|
||||
</#if>
|
||||
|
||||
<#if categoryIds??>
|
||||
and gs.category_id in :categoryIds
|
||||
</#if>
|
||||
|
||||
<#if brandIds??>
|
||||
and gs.brand_id in :brandIds
|
||||
</#if>
|
||||
|
||||
<#if unitIds??>
|
||||
and gs.unit_id in :unitIds
|
||||
</#if>
|
||||
|
||||
<#if shelveIds??>
|
||||
and d.shelve_id in :shelveIds
|
||||
</#if>
|
||||
|
||||
<#if warehouseIds??>
|
||||
|
||||
and d.warehouse_id in :warehouseIds
|
||||
|
||||
</#if>
|
||||
|
||||
<#if goodsIds??>
|
||||
and gs.id in :goodsIds
|
||||
</#if>
|
||||
|
||||
<#if barCode??>
|
||||
and gs.bar_code like :barCode
|
||||
</#if>
|
||||
|
||||
<#if goodsName??>
|
||||
|
||||
and gs.name like :goodsName
|
||||
|
||||
</#if>
|
||||
|
||||
)
|
||||
|
||||
select * from bq
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
""",
|
||||
request);
|
||||
|
||||
var countSql = "select count(1) from (" + sql + ") as t";
|
||||
|
||||
var selectSql =
|
||||
"select * from ("
|
||||
+ sql
|
||||
+ ") as t limit "
|
||||
+ request.getPageSize()
|
||||
+ " offset "
|
||||
+ request.getPageNo() * request.getPageSize();
|
||||
|
||||
Integer count = JpaUtils.execNativeQuery(em, countSql, request, Integer.class).get(0);
|
||||
|
||||
List<Tuple> data = JpaUtils.execNativeQuery(em, selectSql, request, Tuple.class);
|
||||
|
||||
var resultList = JpaUtils.convertTuplesToMap(data);
|
||||
|
||||
var ans =
|
||||
resultList.stream()
|
||||
.map(
|
||||
x ->
|
||||
((Map) x)
|
||||
.entrySet().stream()
|
||||
.collect(
|
||||
Collectors.toMap(
|
||||
(Map.Entry e) ->
|
||||
CaseFormat
|
||||
.LOWER_UNDERSCORE
|
||||
.to(
|
||||
CaseFormat
|
||||
.LOWER_CAMEL,
|
||||
e.getKey()
|
||||
.toString()),
|
||||
e ->
|
||||
ObjectUtils
|
||||
.defaultIfNull(
|
||||
e
|
||||
.getValue(),
|
||||
""))))
|
||||
.toList();
|
||||
|
||||
return new PageImpl<>(ans, PageRequest.of(0, request.getPageSize()), count);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user