feat: 增加库存查询条件

This commit is contained in:
2024-11-23 21:45:01 +08:00
parent 0a018b46a5
commit 0dac827b16
2 changed files with 171 additions and 150 deletions

View File

@@ -41,8 +41,16 @@ public class WarehouseReportRequest {
private String organizationId;
/**
* 是否按照货架汇总
*/
private Boolean shelveSummary = false;
private Boolean excludeEmpty = false;
/**
* 是否包含合计
*/
private Boolean includeSum = true;
}

View File

@@ -39,6 +39,7 @@ import jakarta.persistence.criteria.Root;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -188,8 +189,7 @@ public class WarehouseReceiptService
request.setRuleCode(RuleCodeConstant.KF_RKD);
request.setTime(receipt0.getReceiptDate());
receipt0.setReceiptNo(
orderNoRuleService.genOrderNo(
request, Ctx.currentUser().getOrganizationId()));
orderNoRuleService.genOrderNo(request, Ctx.currentUser().getOrganizationId()));
entity.setReceipt0(receipt0);
this.warehouseReceiptDetailRepository.saveAll(receipt0.getDetail());
@@ -213,8 +213,7 @@ public class WarehouseReceiptService
request.setRuleCode(RuleCodeConstant.KF_CKD);
request.setTime(receipt1.getReceiptDate());
receipt1.setReceiptNo(
orderNoRuleService.genOrderNo(
request, Ctx.currentUser().getOrganizationId()));
orderNoRuleService.genOrderNo(request, Ctx.currentUser().getOrganizationId()));
entity.setReceipt1(receipt1);
this.save(receipt1);
@@ -424,11 +423,7 @@ public class WarehouseReceiptService
public void delete(IdRequest dto) {
/**
* 获取所有单据
* 1。 用户选中的
* 2. 用户选中的单据的子单据
*/
/** 获取所有单据 1。 用户选中的 2. 用户选中的单据的子单据 */
List<WarehouseReceiptEntity> all =
this.repository.findAll(
new Specification<WarehouseReceiptEntity>() {
@@ -444,9 +439,7 @@ public class WarehouseReceiptService
}
});
/**
* 获取用户选中单据中的子单据
*/
/** 获取用户选中单据中的子单据 */
List<WarehouseReceiptEntity> ch =
this.repository.findAll(
new Specification<WarehouseReceiptEntity>() {
@@ -559,159 +552,173 @@ public class WarehouseReceiptService
"""
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.id as warehouse_id,
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
with bq as (select distinct on ( gs.id, w.id
<#if shelveSummary>
, d.shelve_id
</#if>
) 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,
<#if supplierIds??>
and d.supplier_id in :supplierIds
</#if>
gu.name as unit_name,
gu.code as unit_code,
gb.name as brand_name,
gb.code as brand_code,
w.id as warehouse_id,
w.name as warehouse_name,
w.code as warehouse_code,
gc.code as category_code,
gc.name as category_name,
<#if shelveSummary>
ws.id as shelve_id,
ws.name as shelve_name,
ws.code as shelve_code,
</#if>
<#if organizationId??>
and gs.organization_id = :organizationId
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,
</#if>
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
<#if categoryIds?has_content>
and gs.category_id in :categoryIds
</#if>
<#else>
<#if brandIds?has_content>
and gs.brand_id in :brandIds
</#if>
left
<#if unitIds?has_content>
and gs.unit_id in :unitIds
</#if>
</#if>
<#if shelveIds?has_content>
and d.shelve_id in :shelveIds
</#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
<#if shelveSummary>
left join t_warehouse_shelve ws on d.shelve_id = ws.id
</#if>
<#if warehouseIds?has_content>
where 1=1
<#if startTime??>
and re.receipt_date >= :startTime
</#if>
<#if endTime??>
and re.receipt_date <= :endTime
</#if>
and d.warehouse_id in :warehouseIds
and w.id in :warehouseIds
<#if supplierIds??>
and d.supplier_id in :supplierIds
</#if>
</#if>
<#if organizationId??>
and gs.organization_id = :organizationId
<#if goodsIds?has_content>
and gs.id in :goodsIds
</#if>
</#if>
<#if barCode??>
and gs.bar_code like '%' || :barCode || '%'
</#if>
<#if code??>
and gs.code like '%' || :code || '%'
</#if>
<#if categoryIds?has_content>
and gs.category_id in :categoryIds
</#if>
<#if brandIds?has_content>
and gs.brand_id in :brandIds
</#if>
<#if unitIds?has_content>
and gs.unit_id in :unitIds
</#if>
<#if shelveIds?has_content>
and d.shelve_id in :shelveIds
</#if>
<#if warehouseIds?has_content>
and d.warehouse_id in :warehouseIds
and w.id in :warehouseIds
</#if>
<#if goodsIds?has_content>
and gs.id in :goodsIds
</#if>
<#if barCode??>
and gs.bar_code like '%' || :barCode || '%'
</#if>
<#if code??>
and gs.code like '%' || :code || '%'
</#if>
<#if goodsName??>
<#if goodsName??>
and gs.name like '%' || :goodsName || '%'
and gs.name like '%' || :goodsName || '%'
</#if>
</#if>
)
)
select * from bq
select * from bq
""",
""",
request);
var countSql = "select count(1) from (" + sql + ") as t";
@@ -724,19 +731,25 @@ public class WarehouseReceiptService
+ " offset "
+ request.getPageNo() * request.getPageSize();
var sumSql = """
var sumSql =
"""
select sum(type0_number) as type0_number,
sum(type1_number) as type1_number,
sum(type0_amount) as type0_amount,
sum(type1_amount) as type1_amount
from (
""" + sql + ") as t";
"""
+ sql
+ ") as t";
Integer count = JpaUtils.execNativeQuery(em, countSql, request, Integer.class).get(0);
List<Tuple> data = JpaUtils.execNativeQuery(em, selectSql, request, Tuple.class);
List<Tuple> sumData = JpaUtils.execNativeQuery(em, sumSql, request, Tuple.class);
List<Tuple> sumData =
BooleanUtils.isTrue(request.getIncludeSum())
? JpaUtils.execNativeQuery(em, sumSql, request, Tuple.class)
: new ArrayList<>();
var resultList = JpaUtils.convertTuplesToMap(data);
@@ -766,7 +779,8 @@ from (
var sumResultList = JpaUtils.convertTuplesToMap(sumData);
var sumAns = sumResultList.stream()
var sumAns =
sumResultList.stream()
.map(
x ->
((Map) x)
@@ -814,7 +828,7 @@ from (
newRequest.setEndTime(request.getStartTime().minusSeconds(1));
content = ((PageImpl) report(newRequest)).getContent();
}else {
} else {
content =
ans.stream()
.map(
@@ -908,8 +922,7 @@ from (
"prevPrice",
NumberUtils.round2(prevNumber == 0 ? 0 : prevAmount / prevNumber));
currMap.put("sum", sumAns.get(0));
currMap.put("sum", sumAns.isEmpty() ? null : sumAns.get(0));
}
}