chenrui
6 天以前 63ccaee5545740122a9d58983aa75d9da9de7530
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package com.ruoyi.sales.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.vo.FileVo;
import com.ruoyi.sales.dto.InvoiceLedgerDto;
import com.ruoyi.sales.dto.InvoiceRegistrationProductDto;
import com.ruoyi.sales.excel.InvoiceLedgerExcelDto;
import com.ruoyi.sales.mapper.InvoiceLedgerFileMapper;
import com.ruoyi.sales.mapper.InvoiceLedgerMapper;
import com.ruoyi.sales.mapper.InvoiceRegistrationProductMapper;
import com.ruoyi.sales.mapper.ReceiptPaymentMapper;
import com.ruoyi.sales.pojo.InvoiceLedger;
import com.ruoyi.sales.pojo.InvoiceLedgerFile;
import com.ruoyi.sales.pojo.InvoiceRegistrationProduct;
import com.ruoyi.sales.pojo.ReceiptPayment;
import com.ruoyi.sales.service.InvoiceLedgerService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
 
@Service
public class InvoiceLedgerServiceImpl extends ServiceImpl<InvoiceLedgerMapper, InvoiceLedger> implements InvoiceLedgerService {
 
    @Value("${ruoyi.profile}")
    private String uploadFile;
 
    @Autowired
    private InvoiceLedgerMapper invoiceLedgerMapper;
 
    @Autowired
    private InvoiceLedgerFileMapper invoiceLedgerFileMapper;
 
    @Autowired
    private InvoiceRegistrationProductMapper invoiceRegistrationProductMapper;
 
    /**
     * 开票台账新增
     * @param productDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int invoiceLedgerSaveOrUpdate(InvoiceRegistrationProductDto productDto) {
        // 判断是否已经新增开票台账
        QueryWrapper<InvoiceLedger> ledgerQueryWrapper = new QueryWrapper<>();
        ledgerQueryWrapper.eq("invoice_registration_product_id", productDto.getId());
        InvoiceLedger invoiceLedger = invoiceLedgerMapper.selectOne(ledgerQueryWrapper);
        int result;
        if(ObjectUtils.isEmpty(invoiceLedger)){
            invoiceLedger = new InvoiceLedger();
            invoiceLedger.setInvoiceRegistrationProductId(productDto.getId());
            invoiceLedger.setInvoiceNo(productDto.getInvoiceNo());
            invoiceLedger.setInvoiceTotal(productDto.getInvoiceTotal());
            invoiceLedger.setInvoiceDate(productDto.getInvoiceDate());
            invoiceLedger.setInvoicePerson(productDto.getInvoicePerson());
            result = invoiceLedgerMapper.insert(invoiceLedger);
        }else {
            invoiceLedger.setInvoiceNo(productDto.getInvoiceNo());
            invoiceLedger.setInvoiceTotal(productDto.getInvoiceTotal());
            invoiceLedger.setInvoiceDate(productDto.getInvoiceDate());
            invoiceLedger.setInvoicePerson(productDto.getInvoicePerson());
            result = invoiceLedgerMapper.updateById(invoiceLedger);
            //删除所有附件关联
            LambdaQueryWrapper<InvoiceLedgerFile> delWrapper = new LambdaQueryWrapper<>();
            delWrapper.eq(InvoiceLedgerFile::getInvoiceLedgerId, invoiceLedger.getId());
            invoiceLedgerFileMapper.delete(delWrapper);
        }
        List<FileVo> fileList = productDto.getFileList();
        if(CollectionUtils.isNotEmpty(fileList)){
            for (FileVo fileVo : fileList) {
                InvoiceLedgerFile invoiceLedgerFile = new InvoiceLedgerFile();
                BeanUtils.copyProperties(fileVo, invoiceLedgerFile);
                invoiceLedgerFile.setInvoiceLedgerId(invoiceLedger.getId());
                invoiceLedgerFileMapper.insert(invoiceLedgerFile);
            }
        }
        return result;
    }
 
    /**
     * 开票台账删除
     * @param ids
     * @return
     */
    @Override
    public int invoiceLedgerDel(List<Integer> ids) {
        LambdaQueryWrapper<InvoiceLedger> delWrapper = new LambdaQueryWrapper<>();
        delWrapper.in(InvoiceLedger::getId, ids);
        return invoiceLedgerMapper.delete(delWrapper);
    }
 
    /**
     * 开票台账分页查询
     * @param page
     * @param invoiceLedgerDto
     * @return
     */
    @Override
    public IPage<InvoiceLedgerDto> invoiceLedgerPage(Page page, InvoiceLedgerDto invoiceLedgerDto) {
        return invoiceLedgerMapper.invoiceLedgerPage(page, invoiceLedgerDto);
    }
 
    /**
     * 开票台账文件查询
     * @param invoiceLedgerId
     * @return
     */
    @Override
    public List<InvoiceLedgerFile> invoiceLedgerFileList(Integer invoiceLedgerId) {
        LambdaQueryWrapper<InvoiceLedgerFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InvoiceLedgerFile::getInvoiceLedgerId, invoiceLedgerId);
        return invoiceLedgerFileMapper.selectList(queryWrapper);
    }
 
    /**
     * 开票台账文件上传
     * @param file
     * @return
     */
    @Override
    public FileVo invoiceLedgerUploadFile(MultipartFile file) {
        FileVo fileVo = new FileVo();
        try {
            String baseDir = uploadFile + File.separatorChar + "invoiceLedger";
            File dirFile = new File(baseDir);
            if(!dirFile.exists()){
                dirFile.mkdirs();
            }
            String filePath = baseDir + File.separatorChar + UUID.randomUUID() + "_" + file.getOriginalFilename();
            file.transferTo(new File(filePath));
            fileVo.setName(file.getOriginalFilename());
            fileVo.setUrl(filePath);
            fileVo.setFileSize((int)file.getSize());
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("文件上传失败");
        }
        return fileVo;
    }
 
    /**
     * 附件下载
     * @param response
     * @param invoiceLedgerDto
     * @return
     */
    @Override
    public void invoiceLedgerExport(HttpServletResponse response, InvoiceLedgerDto invoiceLedgerDto) {
        List<InvoiceLedgerDto> invoiceLedgerDtoList = invoiceLedgerMapper.invoiceLedgerList(invoiceLedgerDto);
        List<InvoiceLedgerExcelDto> invoiceLedgerExcelDtoList = invoiceLedgerDtoList.stream().map(item -> {
            InvoiceLedgerExcelDto invoiceLedgerExcelDto = new InvoiceLedgerExcelDto();
            BeanUtils.copyProperties(item, invoiceLedgerExcelDto);
            return invoiceLedgerExcelDto;
        }).collect(Collectors.toList());
        ExcelUtil<InvoiceLedgerExcelDto> util = new ExcelUtil<InvoiceLedgerExcelDto>(InvoiceLedgerExcelDto.class);
        util.exportExcel(response, invoiceLedgerExcelDtoList, "供应商导出");
    }
 
    /**
     * 开票台账详情
     * @param id
     * @return
     */
    @Override
    public InvoiceLedgerDto invoiceLedgerDetail(Integer id) {
        InvoiceLedgerDto invoiceLedgerDto  = invoiceLedgerMapper.invoiceLedgerInfo(id);
        if(ObjectUtils.isEmpty(invoiceLedgerDto)){
            throw new RuntimeException("开票台账查找失败");
        }
        // 查询附件
        QueryWrapper<InvoiceLedgerFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("invoice_ledger_id", id);
        List<InvoiceLedgerFile> invoiceLedgerFileList = invoiceLedgerFileMapper.selectList(queryWrapper);
        List<FileVo> fileList = invoiceLedgerFileList.stream().map(item -> {
            FileVo fileVo = new FileVo();
            BeanUtils.copyProperties(item, fileVo);
            return fileVo;
        }).collect(Collectors.toList());
        invoiceLedgerDto.setFileList(fileList);
        return invoiceLedgerDto;
    }
 
    /**
     * 附件提交
     * @param invoiceLedgerDto
     * @return
     */
    @Override
    public void invoiceLedgerCommitFile(InvoiceLedgerDto invoiceLedgerDto) {
        if(null == invoiceLedgerDto.getId()){
            throw new RuntimeException("缺少发票台账主键");
        }
        if(CollectionUtils.isEmpty(invoiceLedgerDto.getFileList())){
            throw new RuntimeException("缺少文件信息");
        }
        List<FileVo> fileList = invoiceLedgerDto.getFileList();
        fileList.forEach(fileVo -> {
            InvoiceLedgerFile invoiceLedgerFile = new InvoiceLedgerFile();
            BeanUtils.copyProperties(fileVo, invoiceLedgerFile);
            invoiceLedgerFile.setInvoiceLedgerId(invoiceLedgerDto.getId());
            invoiceLedgerFileMapper.insert(invoiceLedgerFile);
        });
    }
 
    /**
     * 开票台账查询
     * @param invoiceLedgerDto
     * @return
     */
    @Override
    public List<InvoiceLedgerDto> invoiceLedgerList(InvoiceLedgerDto invoiceLedgerDto) {
        return invoiceLedgerMapper.invoiceLedgerList(invoiceLedgerDto);
    }
 
    /**
     * 客户销售记录
     * @param page
     * @param invoiceLedgerDto
     * @return
     */
    @Override
    public IPage<InvoiceLedgerDto> invoiceLedgerSalesAccount(Page page, InvoiceLedgerDto invoiceLedgerDto) {
        IPage<InvoiceLedgerDto> invoiceLedgerDtoIPage = invoiceLedgerMapper.invoiceLedgerSalesAccount(page, invoiceLedgerDto);
        return invoiceLedgerDtoIPage;
    }
 
    @Override
    public BigDecimal getInvoiceAmount() {
        LocalDate now = LocalDate.now();
        YearMonth currentMonth = YearMonth.from(now);
 
        // 创建LambdaQueryWrapper
        LambdaQueryWrapper<InvoiceLedger> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(InvoiceLedger::getInvoiceDate, currentMonth.atDay(1).atStartOfDay())  // 大于等于本月第一天
                .lt(InvoiceLedger::getInvoiceDate, currentMonth.plusMonths(1).atDay(1).atStartOfDay()); // 小于下月第一天
 
        // 执行查询并计算总和
        List<InvoiceLedger> invoiceLedgers = invoiceLedgerMapper.selectList(queryWrapper);
 
//        BigDecimal totalContractAmount = invoiceLedgers.stream()
//                .map(InvoiceLedger::getInvoiceAmount)
//                .filter(Objects::nonNull)
//                .reduce(BigDecimal.ZERO, BigDecimal::add);
//
//        return totalContractAmount;
        return null;
    }
 
    /**
     * 开票登记产品分页查询
     * @param page
     * @param registrationProductDto
     * @return
     */
    @Override
    public IPage<InvoiceRegistrationProductDto> registrationProductPage(Page page, InvoiceRegistrationProductDto registrationProductDto) {
        return invoiceRegistrationProductMapper.invoiceRegistrationProductPage(page,registrationProductDto);
    }
 
    /**
     * 产品开票台账详情
     * @param id
     * @return
     */
    @Override
    public InvoiceRegistrationProductDto invoiceLedgerProductDetail(Integer id) {
        InvoiceRegistrationProductDto invoiceRegistrationProductDto = invoiceLedgerMapper.invoiceLedgerProductInfo(id);
        if(ObjectUtils.isEmpty(invoiceRegistrationProductDto)){
            throw new RuntimeException("产品开票台账查找失败");
        }
        // 查询附件
        QueryWrapper<InvoiceLedgerFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("invoice_ledger_id", invoiceRegistrationProductDto.getInvoiceLedgerId());
        List<InvoiceLedgerFile> invoiceLedgerFileList = invoiceLedgerFileMapper.selectList(queryWrapper);
        List<FileVo> fileList = invoiceLedgerFileList.stream().map(item -> {
            FileVo fileVo = new FileVo();
            BeanUtils.copyProperties(item, fileVo);
            return fileVo;
        }).collect(Collectors.toList());
        invoiceRegistrationProductDto.setFileList(fileList);
        return invoiceRegistrationProductDto;
    }
 
}