From 0f8f4c5de1d817aa87c4c79e966d3cfbd0997aeb Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期三, 23 七月 2025 16:55:56 +0800
Subject: [PATCH] 1.设备领用添加记录 2.采购添加运费

---
 main-business/src/main/java/com/ruoyi/business/service/impl/SalesRecordServiceImpl.java |  121 +++++++++++++++++++++++++++++++++++++++-
 1 files changed, 117 insertions(+), 4 deletions(-)

diff --git a/main-business/src/main/java/com/ruoyi/business/service/impl/SalesRecordServiceImpl.java b/main-business/src/main/java/com/ruoyi/business/service/impl/SalesRecordServiceImpl.java
index 2f162a7..1a9c849 100644
--- a/main-business/src/main/java/com/ruoyi/business/service/impl/SalesRecordServiceImpl.java
+++ b/main-business/src/main/java/com/ruoyi/business/service/impl/SalesRecordServiceImpl.java
@@ -4,10 +4,12 @@
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.basic.entity.CoalInfo;
 import com.ruoyi.basic.entity.Customer;
+import com.ruoyi.basic.entity.Supply;
 import com.ruoyi.basic.mapper.CoalInfoMapper;
 import com.ruoyi.basic.mapper.CustomerMapper;
 import com.ruoyi.business.dto.SalesRecordDto;
@@ -17,11 +19,16 @@
 import com.ruoyi.business.mapper.OfficialInventoryMapper;
 import com.ruoyi.business.mapper.SalesRecordMapper;
 import com.ruoyi.business.service.SalesRecordService;
+import com.ruoyi.business.vo.SalesRecordExportVo;
 import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.common.exception.base.BaseException;
+import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.bean.BeanUtils;
+import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.system.mapper.SysUserMapper;
+import jakarta.servlet.http.HttpServletResponse;
 import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -50,6 +57,7 @@
  * @since 2025-06-11
  */
 @Service
+@Slf4j
 @RequiredArgsConstructor
 public class SalesRecordServiceImpl extends ServiceImpl<SalesRecordMapper, SalesRecord> implements SalesRecordService {
 
@@ -67,6 +75,29 @@
     public IPage<SalesRecordDto> selectSalesRecordList(Page<SalesRecord> page, SalesRecordDto salesRecordDto) {
         // 1. 鍒涘缓鏌ヨ鏉′欢锛屾寜鍒涘缓鏃堕棿鍊掑簭鎺掑簭
         LambdaQueryWrapper<SalesRecord> queryWrapper = new LambdaQueryWrapper<>();
+
+        // 鎸夋棩鏈熸煡璇�
+        if (salesRecordDto.getSaleDate() != null) {
+            queryWrapper.eq(SalesRecord::getSaleDate, salesRecordDto.getSaleDate());
+        }
+
+        // 鎸夌叅绉嶅悕绉版煡璇�
+        if (StringUtils.isNotBlank(salesRecordDto.getCoal())) {
+            LambdaQueryWrapper<CoalInfo> coalQueryWrapper = new LambdaQueryWrapper<>();
+            coalQueryWrapper.like(CoalInfo::getCoal, salesRecordDto.getCoal());
+            List<CoalInfo> coalInfos = coalInfoMapper.selectList(coalQueryWrapper);
+
+            if (!coalInfos.isEmpty()) {
+                List<Long> coalIds = coalInfos.stream()
+                        .map(CoalInfo::getId)
+                        .collect(Collectors.toList());
+                queryWrapper.in(SalesRecord::getCoalId, coalIds);
+            } else {
+                // 濡傛灉娌℃湁鍖归厤鐨勭叅绉嶏紝鐩存帴杩斿洖绌虹粨鏋�
+                queryWrapper.eq(SalesRecord::getCoalId, -1L); // 浣跨敤涓嶅彲鑳藉瓨鍦ㄧ殑ID
+            }
+        }
+
         queryWrapper.orderByDesc(SalesRecord::getCreateTime);
 
         // 2. 鑾峰彇鍒嗛〉鐨勯攢鍞褰�
@@ -131,7 +162,10 @@
             throw new BaseException("閿�鍞暟閲忎笉鑳藉ぇ浜庡簱瀛樻暟閲�");
         }
         officialInventory.setInventoryQuantity(officialInventory.getInventoryQuantity().subtract(salesRecordDto.getSaleQuantity()));
-        officialInventory.setPendingReplenishment(salesRecordDto.getSaleQuantity());
+
+        if (salesRecordDto.isAdd()){
+            officialInventory.setPendingReplenishment(salesRecordDto.getSaleQuantity());
+        }
         officialInventoryMapper.updateById(officialInventory);
 
         // 鏋勫缓閿�鍞褰曞疄浣�
@@ -169,7 +203,7 @@
         if (registrant == null) {
             throw new BaseException("鐧昏浜轰俊鎭笉瀛樺湪");
         }
-        record.setRegistrant(registrant.getUserName());
+        record.setRegistrant(registrant.getNickName());
 
         // 璁剧疆瀹㈡埛淇℃伅
         Customer customer = customerMapper.selectById(dto.getCustomerId());
@@ -228,6 +262,59 @@
                 .set("deleted", 1);  // 璁剧疆 deleted 涓� 1 琛ㄧず宸插垹闄�
         // 鎵ц鎵归噺閫昏緫鍒犻櫎
         return salesRecordMapper.update(null, updateWrapper);
+    }
+
+    @Override
+    public void salesRecordExport(HttpServletResponse response, SalesRecordDto salesRecordDto) {
+        List<Long> ids = salesRecordDto.getExportIds();
+        List<SalesRecord> list;
+        if (ids != null && !ids.isEmpty()) {
+            list = salesRecordMapper.selectByIds(ids);
+        } else {
+            list = salesRecordMapper.selectList(null);
+        }
+        List<SalesRecordExportVo> exportData = convertToExportVo(list);
+        ExcelUtil<SalesRecordExportVo> util = new ExcelUtil<>(SalesRecordExportVo.class);
+        util.exportExcel(response, exportData, "閿�鍞嚭搴撴暟鎹�");
+    }
+
+    private List<SalesRecordExportVo> convertToExportVo(List<SalesRecord> list) {
+        // 1. 鎻愬墠鏀堕泦鎵�鏈夐渶瑕佹煡璇㈢殑coalId锛岄伩鍏峃+1鏌ヨ闂
+        Set<Long> coalIds = list.stream()
+                .filter(Objects::nonNull)
+                .map(SalesRecord::getCoalId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+
+        // 2. 鎵归噺鏌ヨcoalInfo鏁版嵁
+        Map<Long, CoalInfo> coalInfoMap = CollectionUtils.isEmpty(coalIds)
+                ? Collections.emptyMap()
+                : coalInfoMapper.selectByIds(coalIds).stream()
+                .filter(Objects::nonNull)
+                .collect(Collectors.toMap(CoalInfo::getId, Function.identity()));
+
+        // 3. 杞崲鏁版嵁
+        return list.stream()
+                .filter(Objects::nonNull)
+                .map(record -> {
+                    try {
+                        SalesRecordExportVo vo = new SalesRecordExportVo();
+
+                        // 鎷疯礉鍩虹灞炴��
+                        BeanUtils.copyProperties(record, vo);
+
+                        // 璁剧疆鍏宠仈鐨刢oal淇℃伅
+                        Optional.ofNullable(record.getCoalId())
+                                .map(coalInfoMap::get)
+                                .ifPresent(coalInfo -> vo.setCoal(coalInfo.getCoal()));
+                        return vo;
+                    } catch (Exception e) {
+                        log.error("杞崲閿�鍞褰昖O寮傚父锛岃褰旾D: {}", record.getId(), e);
+                        return null;
+                    }
+                })
+                .filter(Objects::nonNull)
+                .collect(Collectors.toList());
     }
 
     @Override
@@ -482,7 +569,7 @@
 
         // 4. 鎵归噺鏌ヨ鐓ょ淇℃伅骞跺~鍏呭埌缁撴灉涓�
         if (!coalIds.isEmpty()) {
-            List<CoalInfo> coalInfos = coalInfoMapper.selectBatchIds(coalIds);
+            List<CoalInfo> coalInfos = coalInfoMapper.selectByIds(coalIds);
             for (CoalInfo coalInfo : coalInfos) {
                 Map<String, Object> record = resultMap.get(coalInfo.getId());
                 if (record != null) {
@@ -491,8 +578,33 @@
             }
         }
 
-        // 鏈�缁堢粨鏋滄槸涓�涓狶ist<Map>锛屾瘡涓狹ap鍖呭惈鍚堝苟鍚庣殑閿�鍞暟鎹拰瀵瑰簲鐨勭叅绉嶄俊鎭�
         List<Map<String, Object>> results = new ArrayList<>(resultMap.values());
+
+        //鏈堝害鏁版嵁
+        //鏌ヨ鎵�鏈夌叅绉嶄俊鎭�
+        List<CoalInfo> allCoalTypes = coalInfoMapper.selectList(
+                new QueryWrapper<CoalInfo>().orderByAsc("id")
+        );
+        //棰勮绠楅攢閲忥細鎸塩oalId鍒嗙粍缁熻鎬婚攢閲�
+        Map<Long, BigDecimal> salesByCoalId = salesRecords.stream()
+                .collect(Collectors.groupingBy(
+                        SalesRecord::getCoalId,
+                        Collectors.reducing(
+                                BigDecimal.ZERO,
+                                SalesRecord::getSaleQuantity,
+                                BigDecimal::add
+                        )
+                ));
+
+        // 2. 鍒涘缓 resultMouth锛屽瓨鍌ㄧ叅绉嶅強鍏堕攢閲忥紙Map 缁撴瀯锛�
+        Map<String, BigDecimal> resultMouthMap = new LinkedHashMap<>();
+        for (CoalInfo coal : allCoalTypes) {
+            resultMouthMap.put(
+                    coal.getCoal(),  // 鐓ょ鍚嶇О锛堝 "鏃犵儫鐓�"锛�
+                    salesByCoalId.getOrDefault(coal.getId(), BigDecimal.valueOf(0)) // 閿�閲�
+            );
+        }
+
         result.put("revenueAmount", revenueAmount.setScale(2, RoundingMode.HALF_UP));
         result.put("changeRate", changeRate);
         result.put("trend", trend);
@@ -501,6 +613,7 @@
         result.put("trendQuantity", trendQuantity);
         result.put("revenueDistribution", revenueDistribution);
         result.put("salesResults", results);
+        result.put("resultMouth", resultMouthMap);
 
         return result;
     }

--
Gitblit v1.9.3