优化商品库存查询逻辑,在没有选择仓库的时候过滤出当前有权限的仓库

This commit is contained in:
季圣华
2023-03-07 22:06:10 +08:00
parent a84b9e8c12
commit 81c065ea5c
4 changed files with 48 additions and 32 deletions

View File

@@ -116,8 +116,8 @@ public interface DepotItemMapperEx {
@Param("beginTime") String beginTime, @Param("beginTime") String beginTime,
@Param("endTime") String endTime); @Param("endTime") String endTime);
DepotItemVo4Stock getSkuStockByParam( DepotItemVo4Stock getSkuStockByParamWithDepotList(
@Param("depotId") Long depotId, @Param("depotList") List<Long> depotList,
@Param("meId") Long meId, @Param("meId") Long meId,
@Param("beginTime") String beginTime, @Param("beginTime") String beginTime,
@Param("endTime") String endTime); @Param("endTime") String endTime);

View File

@@ -287,6 +287,21 @@ public class DepotService {
return id; return id;
} }
public List<Long> parseDepotList(Long depotId) throws Exception {
List<Long> depotList = new ArrayList<>();
if(depotId !=null) {
depotList.add(depotId);
} else {
//未选择仓库时默认为当前用户有权限的仓库
JSONArray depotArr = findDepotByCurrentUser();
for(Object obj: depotArr) {
JSONObject object = JSONObject.parseObject(obj.toString());
depotList.add(object.getLong("id"));
}
}
return depotList;
}
public JSONArray findDepotByCurrentUser() throws Exception { public JSONArray findDepotByCurrentUser() throws Exception {
JSONArray arr = new JSONArray(); JSONArray arr = new JSONArray();
String type = "UserDepot"; String type = "UserDepot";

View File

@@ -11,6 +11,7 @@ import com.jsh.erp.datasource.vo.DepotItemVo4Stock;
import com.jsh.erp.datasource.vo.DepotItemVoBatchNumberList; import com.jsh.erp.datasource.vo.DepotItemVoBatchNumberList;
import com.jsh.erp.exception.BusinessRunTimeException; import com.jsh.erp.exception.BusinessRunTimeException;
import com.jsh.erp.exception.JshException; import com.jsh.erp.exception.JshException;
import com.jsh.erp.service.depot.DepotService;
import com.jsh.erp.service.depotHead.DepotHeadService; import com.jsh.erp.service.depotHead.DepotHeadService;
import com.jsh.erp.service.materialExtend.MaterialExtendService; import com.jsh.erp.service.materialExtend.MaterialExtendService;
import com.jsh.erp.service.log.LogService; import com.jsh.erp.service.log.LogService;
@@ -62,6 +63,8 @@ public class DepotItemService {
@Resource @Resource
private SystemConfigService systemConfigService; private SystemConfigService systemConfigService;
@Resource @Resource
private DepotService depotService;
@Resource
private RoleService roleService; private RoleService roleService;
@Resource @Resource
private MaterialCurrentStockMapper materialCurrentStockMapper; private MaterialCurrentStockMapper materialCurrentStockMapper;
@@ -863,8 +866,9 @@ public class DepotItemService {
* @param endTime * @param endTime
* @return * @return
*/ */
public BigDecimal getSkuStockByParam(Long depotId, Long meId, String beginTime, String endTime){ public BigDecimal getSkuStockByParam(Long depotId, Long meId, String beginTime, String endTime) throws Exception {
DepotItemVo4Stock stockObj = depotItemMapperEx.getSkuStockByParam(depotId, meId, beginTime, endTime); List<Long> depotList = depotService.parseDepotList(depotId);
DepotItemVo4Stock stockObj = depotItemMapperEx.getSkuStockByParamWithDepotList(depotList, meId, beginTime, endTime);
BigDecimal stockSum = BigDecimal.ZERO; BigDecimal stockSum = BigDecimal.ZERO;
if(stockObj!=null) { if(stockObj!=null) {
BigDecimal inTotal = stockObj.getInTotal(); BigDecimal inTotal = stockObj.getInTotal();
@@ -889,11 +893,8 @@ public class DepotItemService {
* @param endTime * @param endTime
* @return * @return
*/ */
public BigDecimal getStockByParam(Long depotId, Long mId, String beginTime, String endTime){ public BigDecimal getStockByParam(Long depotId, Long mId, String beginTime, String endTime) throws Exception {
List<Long> depotList = new ArrayList<>(); List<Long> depotList = depotService.parseDepotList(depotId);
if(depotId != null) {
depotList.add(depotId);
}
return getStockByParamWithDepotList(depotList, mId, beginTime, endTime); return getStockByParamWithDepotList(depotList, mId, beginTime, endTime);
} }
@@ -970,7 +971,7 @@ public class DepotItemService {
* @param depotItem * @param depotItem
*/ */
@Transactional(value = "transactionManager", rollbackFor = Exception.class) @Transactional(value = "transactionManager", rollbackFor = Exception.class)
public void updateCurrentStock(DepotItem depotItem){ public void updateCurrentStock(DepotItem depotItem) throws Exception {
updateCurrentStockFun(depotItem.getMaterialId(), depotItem.getDepotId()); updateCurrentStockFun(depotItem.getMaterialId(), depotItem.getDepotId());
if(depotItem.getAnotherDepotId()!=null){ if(depotItem.getAnotherDepotId()!=null){
updateCurrentStockFun(depotItem.getMaterialId(), depotItem.getAnotherDepotId()); updateCurrentStockFun(depotItem.getMaterialId(), depotItem.getAnotherDepotId());
@@ -982,7 +983,7 @@ public class DepotItemService {
* @param mId * @param mId
* @param dId * @param dId
*/ */
public void updateCurrentStockFun(Long mId, Long dId) { public void updateCurrentStockFun(Long mId, Long dId) throws Exception {
if(mId!=null && dId!=null) { if(mId!=null && dId!=null) {
MaterialCurrentStockExample example = new MaterialCurrentStockExample(); MaterialCurrentStockExample example = new MaterialCurrentStockExample();
example.createCriteria().andMaterialIdEqualTo(mId).andDepotIdEqualTo(dId) example.createCriteria().andMaterialIdEqualTo(mId).andDepotIdEqualTo(dId)

View File

@@ -520,16 +520,16 @@
and ifnull(di.delete_flag,'0') !='1' and ifnull(di.delete_flag,'0') !='1'
</select> </select>
<select id="getSkuStockByParam" resultMap="stockMap"> <select id="getSkuStockByParamWithDepotList" resultMap="stockMap">
select select
sum(case when dh.type='入库' <if test="depotId != null">and di.depot_id=#{depotId}</if> then di.basic_number else 0 end) as inTotal, sum(case when dh.type='入库' <include refid="depotParam" /> then di.basic_number else 0 end) as inTotal,
sum(case when dh.type='出库' and dh.sub_type!='调拨' <if test="depotId != null">and di.depot_id=#{depotId}</if> then di.basic_number else 0 end) as outTotal, sum(case when dh.type='出库' and dh.sub_type!='调拨' <include refid="depotParam" /> then di.basic_number else 0 end) as outTotal,
sum(case when dh.sub_type='调拨' <if test="depotId != null">and di.another_depot_id=#{depotId}</if> then di.basic_number else 0 end) as transfInTotal, sum(case when dh.sub_type='调拨' <include refid="anotherDepotParam" /> then di.basic_number else 0 end) as transfInTotal,
sum(case when dh.sub_type='调拨' <if test="depotId != null">and di.depot_id=#{depotId}</if> then di.basic_number else 0 end) as transfOutTotal, sum(case when dh.sub_type='调拨' <include refid="depotParam" /> then di.basic_number else 0 end) as transfOutTotal,
sum(case when dh.sub_type='组装单' and di.material_type='组合件' <if test="depotId != null">and di.depot_id=#{depotId}</if> then di.basic_number else 0 end) as assemInTotal, sum(case when dh.sub_type='组装单' and di.material_type='组合件' <include refid="depotParam" /> then di.basic_number else 0 end) as assemInTotal,
sum(case when dh.sub_type='组装单' and di.material_type='普通子件' <if test="depotId != null">and di.depot_id=#{depotId}</if> then di.basic_number else 0 end) as assemOutTotal, sum(case when dh.sub_type='组装单' and di.material_type='普通子件' <include refid="depotParam" /> then di.basic_number else 0 end) as assemOutTotal,
sum(case when dh.sub_type='拆卸单' and di.material_type='普通子件' <if test="depotId != null">and di.depot_id=#{depotId}</if> then di.basic_number else 0 end) as disAssemInTotal, sum(case when dh.sub_type='拆卸单' and di.material_type='普通子件' <include refid="depotParam" /> then di.basic_number else 0 end) as disAssemInTotal,
sum(case when dh.sub_type='拆卸单' and di.material_type='组合件' <if test="depotId != null"> and di.depot_id=#{depotId}</if> then di.basic_number else 0 end) as disAssemOutTotal sum(case when dh.sub_type='拆卸单' and di.material_type='组合件' <include refid="depotParam" /> then di.basic_number else 0 end) as disAssemOutTotal
from from
jsh_depot_head dh jsh_depot_head dh
left join jsh_depot_item di on dh.id=di.header_id and ifnull(di.delete_flag,'0') !='1' left join jsh_depot_item di on dh.id=di.header_id and ifnull(di.delete_flag,'0') !='1'
@@ -545,18 +545,6 @@
</if> </if>
</select> </select>
<sql id="depotParam">
<if test="depotList.size()>0">
and di.depot_id in <foreach collection="depotList" item="item" index="index" separator="," open="(" close=")">#{item}</foreach>
</if>
</sql>
<sql id="anotherDepotParam">
<if test="depotList.size()>0">
and di.another_depot_id in <foreach collection="depotList" item="item" index="index" separator="," open="(" close=")">#{item}</foreach>
</if>
</sql>
<select id="getStockByParamWithDepotList" resultMap="stockMap"> <select id="getStockByParamWithDepotList" resultMap="stockMap">
select select
sum(case when dh.type='入库' <include refid="depotParam" /> then di.basic_number else 0 end) as inTotal, sum(case when dh.type='入库' <include refid="depotParam" /> then di.basic_number else 0 end) as inTotal,
@@ -581,6 +569,18 @@
</if> </if>
</select> </select>
<sql id="depotParam">
<if test="depotList.size()>0">
and di.depot_id in <foreach collection="depotList" item="item" index="index" separator="," open="(" close=")">#{item}</foreach>
</if>
</sql>
<sql id="anotherDepotParam">
<if test="depotList.size()>0">
and di.another_depot_id in <foreach collection="depotList" item="item" index="index" separator="," open="(" close=")">#{item}</foreach>
</if>
</sql>
<select id="findDepotItemListBydepotheadId" resultType="com.jsh.erp.datasource.entities.DepotItem"> <select id="findDepotItemListBydepotheadId" resultType="com.jsh.erp.datasource.entities.DepotItem">
select select
dep.id,dep.header_id,dep.material_id,dep.material_unit,dep.oper_number, dep.id,dep.header_id,dep.material_id,dep.material_unit,dep.oper_number,