|
@@ -8,10 +8,12 @@ import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
+import com.mchange.lang.DoubleUtils;
|
|
|
import com.ruoyi.asset.domain.TbAssetInformation;
|
|
|
import com.ruoyi.asset.domain.TbLocation;
|
|
|
import com.ruoyi.asset.mapper.TbAssetInformationMapper;
|
|
|
import com.ruoyi.asset.mapper.TbLocationMapper;
|
|
|
+import com.ruoyi.common.utils.StringUtils;
|
|
|
import com.ruoyi.inventory.domain.TbInventoryDetail;
|
|
|
import com.ruoyi.inventory.domain.dto.*;
|
|
|
import com.ruoyi.inventory.mapper.TbInventoryDetailMapper;
|
|
@@ -466,7 +468,7 @@ public class TbAssetInventoryServiceImpl implements ITbAssetInventoryService
|
|
|
* @param depId
|
|
|
* @return
|
|
|
*/
|
|
|
- public List<DepAssetLossResultDto> inventoryDepAssetById(String depId){
|
|
|
+ public List<DepAssetLossResultDto> inventoryDepAssetById(String depId) throws Exception {
|
|
|
|
|
|
/* 本想这样写sql的:
|
|
|
select inventory_metadata metadata
|
|
@@ -478,120 +480,210 @@ public class TbAssetInventoryServiceImpl implements ITbAssetInventoryService
|
|
|
order by inventory_date desc
|
|
|
limit 0,2)
|
|
|
但是,语法不支持,会报错:This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
|
|
|
- 暂时选择以下方法解决,但要连接四次数据库,很可能需要优化
|
|
|
+ 暂时选择以下方法解决,要连接四次数据库,很可能需要优化
|
|
|
*/
|
|
|
|
|
|
+ List<String> childDeptIdList = tbAssetInventoryMapper.getChildDeptId(depId);
|
|
|
+ if(childDeptIdList == null || childDeptIdList.size()==0){
|
|
|
+ throw new Exception("不存在该部门id");
|
|
|
+ }
|
|
|
+ List<DepAssetLossResultDto> resultDtoList = new ArrayList<>();
|
|
|
//历史盘点计划编码和日期
|
|
|
- List<AssetInvOrderDto> invs = tbAssetInventoryMapper.getOrderNumberByDepId(depId);
|
|
|
- //前前一次盘点计划编码和日期
|
|
|
-// TbAssetInventoryDTO old_inv = tbAssetInventoryMapper.getOrderNumberByDepId(depId);
|
|
|
+ List<AssetInvOrderDto> invs;
|
|
|
+ for (String ch_depId : childDeptIdList) {
|
|
|
+ invs = tbAssetInventoryMapper.getOrderNumberByDepId(ch_depId);
|
|
|
+ if (ObjectUtil.isEmpty(invs) || invs.size()==0){
|
|
|
+ continue;
|
|
|
+ };
|
|
|
|
|
|
- if (ObjectUtil.isEmpty(invs) || invs.size()==0) return null;
|
|
|
+ AssetInvOrderDto new_inv = invs.get(0);
|
|
|
+// System.out.println("new-盘点计划:"+new_inv);
|
|
|
+ AssetInvOrderDto old_inv = null;
|
|
|
+ if (invs.size()>1) old_inv = invs.get(1);
|
|
|
|
|
|
- AssetInvOrderDto new_inv = invs.get(0);
|
|
|
- System.out.println("new-盘点计划:"+new_inv);
|
|
|
- AssetInvOrderDto old_inv = null;
|
|
|
- if (invs.size()>1) old_inv = invs.get(1);
|
|
|
+ DepAssetLossResultDto resultDto = null;
|
|
|
|
|
|
- DepAssetLossResultDto old_resultDto = null;
|
|
|
- DepAssetLossResultDto new_resultDto = null;
|
|
|
- if (new_inv!=null){
|
|
|
- new_resultDto = lossResult(new_inv);
|
|
|
+ if (!ObjectUtil.isNull(new_inv)) {
|
|
|
+ resultDto = compareResult(new_inv,old_inv);
|
|
|
+ resultDto.setDeptName(new_inv.getDeptName());
|
|
|
+ }
|
|
|
+ resultDtoList.add(resultDto);
|
|
|
}
|
|
|
- if (!ObjectUtil.isNull(old_inv)) old_resultDto = lossResult(old_inv);
|
|
|
|
|
|
- List<DepAssetLossResultDto> results = new ArrayList<>();
|
|
|
- results.add(old_resultDto);
|
|
|
- results.add(new_resultDto);
|
|
|
- return results;
|
|
|
+
|
|
|
+ //前前一次盘点计划编码和日期
|
|
|
+// TbAssetInventoryDTO old_inv = tbAssetInventoryMapper.getOrderNumberByDepId(depId);
|
|
|
+
|
|
|
+ return resultDtoList;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 处理资产信息,统计每一样资产的损耗情况
|
|
|
* @param assetInfos
|
|
|
* @param assetMap
|
|
|
- * @param lossValMap
|
|
|
*/
|
|
|
private void handleAssetInfo(List<TbAssetInformation> assetInfos,
|
|
|
Map<String, DepAssetLossDto> assetMap,
|
|
|
- Map<String, Double> lossValMap){
|
|
|
+ Map<String, Integer> invQuantity_map){
|
|
|
for (TbAssetInformation asset : assetInfos) {
|
|
|
- if (!assetMap.containsKey(asset.getName())){
|
|
|
+ //资产位置
|
|
|
+ String loc = asset.getAncestorLocationName()+asset.getParentLocationName()+asset.getLocationName();
|
|
|
+
|
|
|
+ if (!assetMap.containsKey(asset.getName()+"-"+loc)){
|
|
|
|
|
|
DepAssetLossDto lossDto = new DepAssetLossDto();
|
|
|
lossDto.setAssetName(asset.getName());
|
|
|
lossDto.setCategory(asset.getCategoryName());
|
|
|
+ lossDto.setUnits(asset.getUnits());
|
|
|
+ lossDto.setLocation(loc);
|
|
|
+ //账面数量
|
|
|
lossDto.setNowQuantity(asset.getQuantity());
|
|
|
-
|
|
|
- List<String> locations = new ArrayList<>();
|
|
|
- if (asset.getManageStatus()==7) {
|
|
|
- lossDto.setLossQuantity(asset.getQuantity());
|
|
|
- locations.add(asset.getAncestorLocationName()+asset.getParentLocationName()+asset.getLocationName());
|
|
|
- lossDto.setLocations(locations);
|
|
|
- lossValMap.put(asset.getName(),(asset.getOriginalValue()-asset.getNetValue())*asset.getQuantity());
|
|
|
+ //实点数量
|
|
|
+ lossDto.setInvQuantity(invQuantity_map.get(asset.getName()+"-"+loc));
|
|
|
+ //损耗数量 = 账面数量 - 实点数量
|
|
|
+ lossDto.setLossQuantity(lossDto.getNowQuantity()-lossDto.getInvQuantity());
|
|
|
+ //损耗值
|
|
|
+ Double a = asset.getOriginalValue(); //原值
|
|
|
+ Double b = asset.getNetValue(); //净值
|
|
|
+ Double c = asset.getResidualValue(); //残值
|
|
|
+ if (lossDto.getLossQuantity()>0){
|
|
|
+ if (ObjectUtil.isNull(a) || ObjectUtil.isNull(b) || ObjectUtil.isNull(c)) {
|
|
|
+ lossDto.setLossVal(0);
|
|
|
+ } else {
|
|
|
+ lossDto.setLossVal((a-b)*invQuantity_map.get(asset.getName()+"-"+loc)
|
|
|
+ + (a-c)*lossDto.getLossQuantity());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (ObjectUtil.isNull(a) || ObjectUtil.isNull(b)){
|
|
|
+ lossDto.setLossVal(0);
|
|
|
+ } else {
|
|
|
+ lossDto.setLossVal((a-b)*invQuantity_map.get(asset.getName()+"-"+loc));
|
|
|
+ }
|
|
|
}
|
|
|
- assetMap.put(asset.getName(),lossDto);
|
|
|
+ assetMap.put(asset.getName()+"-"+loc,lossDto);
|
|
|
} else {
|
|
|
- DepAssetLossDto lossDto = assetMap.get(asset.getName());
|
|
|
+ DepAssetLossDto lossDto = assetMap.get(asset.getName()+"-"+loc);
|
|
|
+ //账面数量
|
|
|
lossDto.setNowQuantity(lossDto.getNowQuantity()+asset.getQuantity());
|
|
|
- if (asset.getManageStatus()==7) {
|
|
|
- lossDto.setLossQuantity(lossDto.getLossQuantity()+asset.getQuantity());
|
|
|
- lossDto.getLocations()
|
|
|
- .add(asset.getAncestorLocationName()+asset.getParentLocationName()+asset.getLocationName());
|
|
|
- lossValMap.put(asset.getName(),
|
|
|
- Double.sum(lossValMap.get(asset.getName()),
|
|
|
- (asset.getOriginalValue()-asset.getNetValue())*asset.getQuantity()));
|
|
|
+ //损耗数量
|
|
|
+ lossDto.setLossQuantity(lossDto.getNowQuantity()-lossDto.getInvQuantity());
|
|
|
+ //损耗值
|
|
|
+ Double a = asset.getOriginalValue(); //原值
|
|
|
+ Double b = asset.getNetValue(); //净值
|
|
|
+ Double c = asset.getResidualValue(); //残值
|
|
|
+ if (lossDto.getLossQuantity()>0){
|
|
|
+ if (ObjectUtil.isNull(a) || ObjectUtil.isNull(b) || ObjectUtil.isNull(c)) {
|
|
|
+ lossDto.setLossVal(0);
|
|
|
+ } else {
|
|
|
+ lossDto.setLossVal((a-b)*invQuantity_map.get(asset.getName()+"-"+loc)
|
|
|
+ + (a-c)*lossDto.getLossQuantity());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (ObjectUtil.isNull(a) || ObjectUtil.isNull(b)){
|
|
|
+ lossDto.setLossVal(0);
|
|
|
+ } else {
|
|
|
+ lossDto.setLossVal((a-b)*invQuantity_map.get(asset.getName()+"-"+loc));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 根据盘点计划的编号查询出资产元数据,并统计损耗
|
|
|
- * @param invPlan
|
|
|
+ * 统计并对比两次盘点的资产数据
|
|
|
+ * @param new_invPlan
|
|
|
* @return
|
|
|
*/
|
|
|
- private DepAssetLossResultDto lossResult(AssetInvOrderDto invPlan){
|
|
|
-
|
|
|
- List<String> asset_list = tbAssetInventoryMapper.getDepAssetByOrderNumber(invPlan.getOrderNumber());
|
|
|
- //盘点资产详细信息-元数据
|
|
|
- List<TbAssetInformation> assetInfos = new ArrayList<>();
|
|
|
- //json数据 --> java对象
|
|
|
- for (String asset : asset_list) {
|
|
|
- System.out.println(asset);
|
|
|
- assetInfos.add(JSONUtil.toBean(asset, TbAssetInformation.class));
|
|
|
+ private DepAssetLossResultDto compareResult(AssetInvOrderDto new_invPlan, AssetInvOrderDto old_invPlan){
|
|
|
+
|
|
|
+ //----------------------最近一次---------------------------
|
|
|
+ //资产统计
|
|
|
+ Map<String, DepAssetLossDto> new_assetMap = new HashMap<>();
|
|
|
+ //记录实点数量
|
|
|
+ Map<String, Integer> new_invQuantity_map = new HashMap<>();
|
|
|
+
|
|
|
+ getInvResult(new_invPlan, new_assetMap, new_invQuantity_map);
|
|
|
+
|
|
|
+ //---------------------前前一次----------------------------
|
|
|
+ //资产统计
|
|
|
+ Map<String, DepAssetLossDto> old_assetMap = new HashMap<>();
|
|
|
+ //记录实点数量
|
|
|
+ Map<String, Integer> old_invQuantity_map = new HashMap<>();
|
|
|
+
|
|
|
+ boolean result = getInvResult(old_invPlan, old_assetMap, old_invQuantity_map);
|
|
|
+ //如果只有一次盘点,则直接返回资产数据
|
|
|
+ if (!result){
|
|
|
+ ArrayList<DepAssetLossDto> resultList = new ArrayList<>();
|
|
|
+ Set<Map.Entry<String, DepAssetLossDto>> assets = new_assetMap.entrySet();
|
|
|
+ for (Map.Entry<String, DepAssetLossDto> asset : assets) {
|
|
|
+ resultList.add(asset.getValue());
|
|
|
+ }
|
|
|
+ DepAssetLossResultDto resultDto = new DepAssetLossResultDto();
|
|
|
+ resultDto.setCompareResultList(resultList);
|
|
|
+ resultDto.setLastFirstDate(new_invPlan.getInventDate());
|
|
|
+ resultDto.setLastSecondDate(null);
|
|
|
+ return resultDto;
|
|
|
}
|
|
|
+ //比较两次盘点(最近一次的减去前前次的),并将比较结果转到list
|
|
|
+ ArrayList<DepAssetLossDto> compareResultList = new ArrayList<>();
|
|
|
+ Set<Map.Entry<String, DepAssetLossDto>> assets = new_assetMap.entrySet();
|
|
|
+ for (Map.Entry<String, DepAssetLossDto> asset : assets) {
|
|
|
+
|
|
|
+ DepAssetLossDto new_ = asset.getValue();
|
|
|
+ if (!old_assetMap.containsKey(asset.getKey())) {
|
|
|
+ compareResultList.add(new_);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ DepAssetLossDto old_ = old_assetMap.get(asset.getKey());
|
|
|
+ new_.setNowQuantity(new_.getNowQuantity()-old_.getNowQuantity());
|
|
|
+ new_.setInvQuantity(new_.getInvQuantity()-old_.getInvQuantity());
|
|
|
+ new_.setLossQuantity(new_.getLossQuantity()-old_.getLossQuantity());
|
|
|
+ new_.setLossVal(new_.getLossVal()-old_.getLossVal());
|
|
|
|
|
|
- Map<String, DepAssetLossDto> assetMap = new HashMap<>();
|
|
|
- //各资产损耗值map
|
|
|
- HashMap<String, Double> lossValMap = new HashMap<>();
|
|
|
- //统计两次盘点计划每一样资产(同资产名)的相关信息
|
|
|
- handleAssetInfo(assetInfos,assetMap,lossValMap);
|
|
|
- //将map中统计好的资产信息转到list
|
|
|
- ArrayList<DepAssetLossDto> assetResultList = new ArrayList<>();
|
|
|
- Set<Map.Entry<String, DepAssetLossDto>> entrySet = assetMap.entrySet();
|
|
|
- for (Map.Entry<String, DepAssetLossDto> entry : entrySet) {
|
|
|
- DepAssetLossDto dto = entry.getValue();
|
|
|
- if (lossValMap.containsKey(dto.getAssetName()))dto.setLossVal(lossValMap.get(dto.getAssetName()));
|
|
|
- assetResultList.add(dto);
|
|
|
+ compareResultList.add(new_);
|
|
|
}
|
|
|
- //计算损耗总值
|
|
|
- Set<Map.Entry<String, Double>> lossValSet = lossValMap.entrySet();
|
|
|
- double lossVal = 0.0;
|
|
|
- for (Map.Entry<String, Double> entry : lossValSet) {
|
|
|
- lossVal+=entry.getValue();
|
|
|
- }
|
|
|
-
|
|
|
-// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
//封装结果
|
|
|
DepAssetLossResultDto resultDto = new DepAssetLossResultDto();
|
|
|
- resultDto.setTotalLossVal(lossVal);
|
|
|
- resultDto.setAssetResultList(assetResultList);
|
|
|
- resultDto.setDate(invPlan.getInventDate());
|
|
|
+// resultDto.setTotalLossVal(lossVal);
|
|
|
+ resultDto.setCompareResultList(compareResultList);
|
|
|
+ resultDto.setLastFirstDate(new_invPlan.getInventDate());
|
|
|
+ resultDto.setLastSecondDate(old_invPlan.getInventDate());
|
|
|
|
|
|
return resultDto;
|
|
|
}
|
|
|
|
|
|
+ //统计
|
|
|
+ private boolean getInvResult(AssetInvOrderDto invPlan,
|
|
|
+ Map<String, DepAssetLossDto> assetMap,
|
|
|
+ Map<String, Integer> invQuantity_map){
|
|
|
+
|
|
|
+ if (ObjectUtil.isNull(invPlan)) return false;
|
|
|
+
|
|
|
+ List<DepAssetDetailDto> asset_list = tbAssetInventoryMapper.getDepAssetByOrderNumber(invPlan.getOrderNumber());
|
|
|
+ //盘点资产详细信息-元数据
|
|
|
+ List<TbAssetInformation> assetInfos = new ArrayList<>();
|
|
|
+
|
|
|
+ //json数据 --> java对象
|
|
|
+ for (DepAssetDetailDto asset : asset_list) {
|
|
|
+ TbAssetInformation info = JSONUtil.toBean(asset.getAssetData(), TbAssetInformation.class);
|
|
|
+ assetInfos.add(info);
|
|
|
+ //位置
|
|
|
+ String loc = info.getAncestorLocationName()+info.getParentLocationName()+info.getLocationName();
|
|
|
+
|
|
|
+ if (!invQuantity_map.containsKey(info.getName()+"-"+loc)) {
|
|
|
+ invQuantity_map.put(info.getName() + "-" + loc, asset.getInventoryQuantity());
|
|
|
+ } else {
|
|
|
+ invQuantity_map.put(
|
|
|
+ info.getName() + "-" + loc,
|
|
|
+ invQuantity_map.get(info.getName()+"-"+loc) + asset.getInventoryQuantity()
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //统计盘点计划每一样资产(同资产名且同位置)的相关信息
|
|
|
+ handleAssetInfo(assetInfos,assetMap,invQuantity_map);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
+
|