chenrui
2025-05-13 fe1845ba21d8e9908077ab0bb5a9a8137942a50b
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
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.basic.dto.SupplierManageDto;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.vo.FileVo;
import com.ruoyi.sales.dto.InvoiceLedgerDto;
import com.ruoyi.sales.excel.InvoiceLedgerExcelDto;
import com.ruoyi.sales.mapper.InvoiceLedgerFileMapper;
import com.ruoyi.sales.mapper.InvoiceLedgerMapper;
import com.ruoyi.sales.pojo.InvoiceLedger;
import com.ruoyi.sales.pojo.InvoiceLedgerFile;
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.util.List;
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;
 
    /**
     * 开票台账新增
     * @param invoiceLedgerDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int invoiceLedgerSaveOrUpdate(InvoiceLedgerDto invoiceLedgerDto) {
        InvoiceLedger invoiceLedger = new InvoiceLedger();
        BeanUtils.copyProperties(invoiceLedgerDto, invoiceLedger);
        int result;
        if(invoiceLedgerDto.getId() == null){
            result = invoiceLedgerMapper.insert(invoiceLedger);
        }else {
            result = invoiceLedgerMapper.updateById(invoiceLedger);
            //删除所有附件关联
            LambdaQueryWrapper<InvoiceLedgerFile> delWrapper = new LambdaQueryWrapper<>();
            delWrapper.eq(InvoiceLedgerFile::getInvoiceLedgerId, invoiceLedgerDto.getId());
            invoiceLedgerFileMapper.delete(delWrapper);
        }
        List<FileVo> fileList = invoiceLedgerDto.getFileList();
        if(CollectionUtils.isNotEmpty(fileList)){
            fileList.forEach(fileVo -> {
                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);
        });
    }
 
}