|
@@ -3,15 +3,24 @@ package com.fjhx.service.apply.impl;
|
|
|
import cn.hutool.core.convert.Convert;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.fjhx.base.Condition;
|
|
|
+import com.fjhx.base.StorageBaseEntity;
|
|
|
+import com.fjhx.constants.StatusConstant;
|
|
|
import com.fjhx.entity.apply.ApplyPurchase;
|
|
|
+import com.fjhx.entity.stock.StockTag;
|
|
|
+import com.fjhx.entity.water.WaterDetail;
|
|
|
import com.fjhx.mapper.apply.ApplyPurchaseMapper;
|
|
|
import com.fjhx.service.apply.ApplyPurchaseService;
|
|
|
+import com.fjhx.service.stock.StockTagService;
|
|
|
+import com.fjhx.service.water.WaterDetailService;
|
|
|
import com.fjhx.utils.BigDecimalUtil;
|
|
|
+import com.fjhx.utils.UserClientUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
@@ -29,6 +38,12 @@ import java.util.stream.Collectors;
|
|
|
@Service
|
|
|
public class ApplyPurchaseServiceImpl extends ServiceImpl<ApplyPurchaseMapper, ApplyPurchase> implements ApplyPurchaseService {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private StockTagService stockTagService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WaterDetailService waterDetailService;
|
|
|
+
|
|
|
@Override
|
|
|
public String getCode() {
|
|
|
return "PB-" + DateUtil.format(new Date(), "yyMMdd-HHmmss-SSS");
|
|
@@ -152,73 +167,79 @@ public class ApplyPurchaseServiceImpl extends ServiceImpl<ApplyPurchaseMapper, A
|
|
|
.orderByAsc("pc.PurContractState")
|
|
|
.orderByDesc("ap.CreatedTime");
|
|
|
|
|
|
-// Page<Map<String, Object>> page = baseMapper.getPage(createPage(condition), wrapper);
|
|
|
-//
|
|
|
-// List<Map<String, Object>> records = page.getRecords();
|
|
|
-// if (records.size() == 0) {
|
|
|
-// return page;
|
|
|
-// }
|
|
|
-
|
|
|
-// // 获取物料编码集合
|
|
|
-// List<String> materialCodeList = records.stream()
|
|
|
-// .map(item -> item.get("materialCode").toString()).collect(Collectors.toList());
|
|
|
-//
|
|
|
-// // 查询库存,最长滞留时间和滞留超过30天数量
|
|
|
-// Date date = new Date();
|
|
|
-// DateTime dateTime = DateUtil.offsetDay(date, -30);
|
|
|
-// Map<String, Map<String, Object>> delayMap = baseMapper.selectDelay(
|
|
|
-// dateTime,
|
|
|
-// Wrappers.query().in("sd.MaterialCode", materialCodeList).groupBy("sd.MaterialCode")
|
|
|
-// );
|
|
|
-//
|
|
|
-// // 近30天消耗量
|
|
|
-// Map<String, Object> consumeMap = stockWaterService.listMaps(
|
|
|
-// Wrappers.<StockWater>query().select("sum(TotalQty) overUseQty", "MaterialCode materialCode")
|
|
|
-// .lambda()
|
|
|
-// .in(StockWater::getStockchangetype, 20, 23)
|
|
|
-// .gt(StockWater::getCreatedtime, dateTime)
|
|
|
-// .groupBy(StockWater::getMaterialcode)
|
|
|
-// ).stream().collect(Collectors.toMap(
|
|
|
-// item -> item.get("materialCode").toString(),
|
|
|
-// item -> item.get("overUseQty")
|
|
|
-// ));
|
|
|
-//
|
|
|
-// for (Map<String, Object> record : records) {
|
|
|
-// String materialCode = record.get("materialCode").toString();
|
|
|
-// Map<String, Object> map = delayMap.get(materialCode);
|
|
|
-//
|
|
|
-// if (map != null) {
|
|
|
-// record.putAll(map);
|
|
|
-// record.put("maxDelayTime", DateUtil.betweenDay(date, (Date) map.get("maxDelayTime"), true));
|
|
|
-// }
|
|
|
-//
|
|
|
-// record.putIfAbsent("stockQty", 0);
|
|
|
-// record.putIfAbsent("maxDelayTime", 0);
|
|
|
-// record.putIfAbsent("delayQuantity", 0);
|
|
|
-//
|
|
|
-// Object overUseQty = consumeMap.get(materialCode);
|
|
|
-// record.put("overUseQty", overUseQty == null ? BigDecimal.ZERO : overUseQty);
|
|
|
-//
|
|
|
-// record.put("purchaseQty", BigDecimalUtil.keepDecimals(record.get("purchaseQty")));
|
|
|
-// record.put("onWayQuantity", BigDecimalUtil.keepDecimals(record.get("onWayQuantity")));
|
|
|
-//
|
|
|
-// int purContractState = Integer.parseInt(record.get("purContractState").toString());
|
|
|
-// switch (purContractState) {
|
|
|
-// case 0:
|
|
|
-// record.put("purContractState", "待确认");
|
|
|
-// break;
|
|
|
-// case 1:
|
|
|
-// record.put("purContractState", "审批中");
|
|
|
-// break;
|
|
|
-// case 2:
|
|
|
-// record.put("purContractState", "进行中");
|
|
|
-// break;
|
|
|
-// case 3:
|
|
|
-// record.put("purContractState", "已完成");
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
- return null;
|
|
|
+ Page<Map<String, Object>> page = baseMapper.getPage(createPage(condition), wrapper);
|
|
|
+
|
|
|
+ List<Map<String, Object>> records = page.getRecords();
|
|
|
+ if (records.size() == 0) {
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取物料id
|
|
|
+ List<Long> materialIdList = records.stream().map(item -> Convert.toLong(item.get("materialId")))
|
|
|
+ .distinct().collect(Collectors.toList());
|
|
|
+
|
|
|
+ Date date = new Date();
|
|
|
+
|
|
|
+ // 最长滞留时间
|
|
|
+ Map<Long, Long> maximumDetentionTimeMap = getMaximumDetentionTime(date, materialIdList);
|
|
|
+
|
|
|
+ // 滞留超过30天数量
|
|
|
+ Map<Long, BigDecimal> moreThanOneMonthMap = getMoreThanOneMonth(date, materialIdList);
|
|
|
+
|
|
|
+ // 近30天消耗量
|
|
|
+ Map<Long, BigDecimal> useOneMonthMap = getUseOneMonth(date, materialIdList);
|
|
|
+
|
|
|
+ for (Map<String, Object> record : records) {
|
|
|
+
|
|
|
+ Long materialId = Convert.toLong(record.get("materialId"));
|
|
|
+
|
|
|
+ // 赋值最长滞留周期
|
|
|
+ Long maxDelayTime = maximumDetentionTimeMap.get(materialId);
|
|
|
+ record.put("maxDelayTime", maxDelayTime == null ? 0 : maxDelayTime);
|
|
|
+
|
|
|
+ // 赋值滞留超过30天数量
|
|
|
+ BigDecimal delayQuantity = moreThanOneMonthMap.get(materialId);
|
|
|
+ record.put("delayQuantity", delayQuantity == null ? BigDecimal.ZERO : delayQuantity);
|
|
|
+
|
|
|
+ // 赋值近30天消耗量
|
|
|
+ BigDecimal useQuantity = useOneMonthMap.get(materialId);
|
|
|
+ record.put("useQuantity", useQuantity == null ? BigDecimal.ZERO : delayQuantity);
|
|
|
+ }
|
|
|
+
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Page<Map<String, Object>> getApplyPurchasePage(Condition condition) {
|
|
|
+ String keyword = condition.getKeyword();
|
|
|
+
|
|
|
+ LambdaQueryWrapper<ApplyPurchase> wrapper = Wrappers.<ApplyPurchase>lambdaQuery()
|
|
|
+ .like(ObjectUtil.isNotEmpty(keyword), ApplyPurchase::getCode, keyword);
|
|
|
+
|
|
|
+ Page<Map<String, Object>> page = pageMaps(condition, wrapper);
|
|
|
+
|
|
|
+ List<Map<String, Object>> records = page.getRecords();
|
|
|
+ if (records.size() == 0) {
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<Long, Map<String, Object>> createUser = UserClientUtil
|
|
|
+ .getUserNameAndPostFunctionLong(records, item -> Convert.toLong(item.get("createUser")));
|
|
|
+
|
|
|
+ for (Map<String, Object> record : records) {
|
|
|
+ Long createUserId = Convert.toLong(record.get("createUser"));
|
|
|
+ Map<String, Object> map = createUser.get(createUserId);
|
|
|
+ if (map != null) {
|
|
|
+ record.putAll(map);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Map<String, Object>> getApplyPurchaseDetailsList(Condition condition) {
|
|
|
+ String applyPurchaseId = condition.getStr("applyPurchaseId", "申购单id不能为空");
|
|
|
+ return baseMapper.getApplyPurchaseDetailsList(createPage(condition), applyPurchaseId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -258,4 +279,82 @@ public class ApplyPurchaseServiceImpl extends ServiceImpl<ApplyPurchaseMapper, A
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取最长滞留时间
|
|
|
+ *
|
|
|
+ * @param date 当前时间
|
|
|
+ * @param materialIdList 物料id列表
|
|
|
+ * @return map<物料id, 最长滞留时间>
|
|
|
+ */
|
|
|
+ private Map<Long, Long> getMaximumDetentionTime(Date date, List<Long> materialIdList) {
|
|
|
+
|
|
|
+ List<Map<String, Object>> list = stockTagService.listMaps(Wrappers.<StockTag>query()
|
|
|
+ .select("min(create_time) createTime")
|
|
|
+ .lambda()
|
|
|
+ .select(StockTag::getMaterialId)
|
|
|
+ .eq(StockTag::getInHouse, StatusConstant.YES)
|
|
|
+ .in(StockTag::getMaterialId, materialIdList)
|
|
|
+ .groupBy(StockTag::getMaterialId)
|
|
|
+ );
|
|
|
+
|
|
|
+ return list.stream().collect(Collectors.toMap(
|
|
|
+ item -> Convert.toLong(item.get("materialId")),
|
|
|
+ item -> DateUtil.betweenDay(date, Convert.toDate(item.get("createTime")), true)
|
|
|
+ ));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 滞留超过30天数量
|
|
|
+ *
|
|
|
+ * @param date 当前时间
|
|
|
+ * @param materialIdList 物料id列表
|
|
|
+ * @return map<物料id, 滞留超过30天数量>
|
|
|
+ */
|
|
|
+ private Map<Long, BigDecimal> getMoreThanOneMonth(Date date, List<Long> materialIdList) {
|
|
|
+
|
|
|
+
|
|
|
+ List<Map<String, Object>> list = stockTagService.listMaps(Wrappers.<StockTag>query()
|
|
|
+ .select("sum(quantity) delayQuantity")
|
|
|
+ .lambda()
|
|
|
+ .select(StockTag::getMaterialId)
|
|
|
+ .eq(StockTag::getInHouse, StatusConstant.YES)
|
|
|
+ .le(StorageBaseEntity::getCreateTime, DateUtil.offsetDay(date, -30))
|
|
|
+ .in(StockTag::getMaterialId, materialIdList)
|
|
|
+ .groupBy(StockTag::getMaterialId)
|
|
|
+ );
|
|
|
+
|
|
|
+ return list.stream().collect(Collectors.toMap(
|
|
|
+ item -> Convert.toLong(item.get("materialId")),
|
|
|
+ item -> Convert.toBigDecimal(item.get("delayQuantity"))
|
|
|
+ ));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 近30天使用量
|
|
|
+ *
|
|
|
+ * @param date 当前时间
|
|
|
+ * @param materialIdList 物料id列表
|
|
|
+ * @return map<物料id, 滞留超过30天数量>
|
|
|
+ */
|
|
|
+ private Map<Long, BigDecimal> getUseOneMonth(Date date, List<Long> materialIdList) {
|
|
|
+
|
|
|
+ List<Map<String, Object>> list = waterDetailService.listMaps(Wrappers.<WaterDetail>query()
|
|
|
+ .select("sum(quantity) useQuantity")
|
|
|
+ .lambda()
|
|
|
+ .select(WaterDetail::getMaterialId)
|
|
|
+ .ge(StorageBaseEntity::getCreateTime, DateUtil.offsetDay(date, -30))
|
|
|
+ .in(WaterDetail::getType, 20, 23)
|
|
|
+ .in(WaterDetail::getMaterialId, materialIdList)
|
|
|
+ .groupBy(WaterDetail::getMaterialId)
|
|
|
+ );
|
|
|
+
|
|
|
+ return list.stream().collect(Collectors.toMap(
|
|
|
+ item -> Convert.toLong(item.get("materialId")),
|
|
|
+ item -> Convert.toBigDecimal(item.get("useQuantity"))
|
|
|
+ ));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|