MrShi
5 小时以前 eb7a808aaf7dd0a6dd2ff70f9ef3f8ce0b1e31d1
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package com.doumee.service.business.impl;
 
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.extension.plugins.pagination.Page;
import com.doumee.core.constants.Constants;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.utils.FtpUtil;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.InvoiceRecordMapper;
import com.doumee.dao.business.OrdersMapper;
import com.doumee.dao.business.model.InvoiceRecord;
import com.doumee.dao.business.model.Orders;
import com.doumee.dao.dto.ApplyInvoiceDTO;
import com.doumee.dao.dto.invoice.InvoiceA0001DTO;
import com.doumee.dao.dto.invoice.InvoiceA0001ItemDTO;
import com.doumee.dao.dto.invoice.InvoiceA0001Request;
import com.doumee.dao.vo.InvoiceRecordSummaryVO;
import com.doumee.service.business.InvoiceRecordService;
import com.doumee.service.common.EmailService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.io.File;
import java.io.FileOutputStream;
import java.util.*;
 
 
/**
 * 发票申请记录Service实现
 *
 * @author rk
 * @date 2026/05/18
 */
@Service
public class InvoiceRecordServiceImpl implements InvoiceRecordService {
 
    @Autowired
    private InvoiceRecordMapper invoiceRecordMapper;
 
    @Autowired
    private OrdersMapper ordersMapper;
 
    @Autowired
    private EmailService emailService;
 
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
 
    @Override
    public Integer create(InvoiceRecord invoiceRecord) {
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        invoiceRecord.setDeleted(Constants.ZERO);
        invoiceRecord.setCreateTime(new Date());
        invoiceRecord.setCreateUser(loginUserInfo.getId());
        invoiceRecord.setUpdateTime(new Date());
        invoiceRecord.setUpdateUser(loginUserInfo.getId());
        invoiceRecordMapper.insert(invoiceRecord);
        return invoiceRecord.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        invoiceRecordMapper.update(new UpdateWrapper<InvoiceRecord>().lambda()
                .set(InvoiceRecord::getDeleted, Constants.ONE)
                .eq(InvoiceRecord::getId, id));
    }
 
    @Override
    public void delete(InvoiceRecord invoiceRecord) {
        UpdateWrapper<InvoiceRecord> deleteWrapper = new UpdateWrapper<>(invoiceRecord);
        invoiceRecordMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        for (Integer id : ids) {
            this.deleteById(id);
        }
    }
 
    @Override
    public void updateById(InvoiceRecord invoiceRecord) {
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        invoiceRecord.setUpdateTime(new Date());
        invoiceRecord.setUpdateUser(loginUserInfo.getId());
        invoiceRecordMapper.updateById(invoiceRecord);
    }
 
    @Override
    public void updateByIdInBatch(List<InvoiceRecord> invoiceRecords) {
        if (CollectionUtils.isEmpty(invoiceRecords)) {
            return;
        }
        for (InvoiceRecord invoiceRecord : invoiceRecords) {
            this.updateById(invoiceRecord);
        }
    }
 
    @Override
    public InvoiceRecord findById(Integer id) {
        InvoiceRecord invoiceRecord = invoiceRecordMapper.selectById(id);
        if (Objects.isNull(invoiceRecord)) {
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        return invoiceRecord;
    }
 
    @Override
    public InvoiceRecord findOne(InvoiceRecord invoiceRecord) {
        QueryWrapper<InvoiceRecord> wrapper = new QueryWrapper<>(invoiceRecord);
        return invoiceRecordMapper.selectOne(wrapper);
    }
 
    @Override
    public List<InvoiceRecord> findList(InvoiceRecord invoiceRecord) {
        QueryWrapper<InvoiceRecord> wrapper = new QueryWrapper<>(invoiceRecord);
        return invoiceRecordMapper.selectList(wrapper);
    }
 
    @Override
    public PageData<InvoiceRecord> findPage(PageWrap<InvoiceRecord> pageWrap) {
        IPage<InvoiceRecord> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<InvoiceRecord> qw = buildAdminQueryWrapper(pageWrap.getModel());
        qw.lambda().orderByDesc(InvoiceRecord::getCreateTime);
        return PageData.from(invoiceRecordMapper.selectPage(page, qw));
    }
 
    @Override
    public long count(InvoiceRecord invoiceRecord) {
        QueryWrapper<InvoiceRecord> wrapper = new QueryWrapper<>(invoiceRecord);
        return invoiceRecordMapper.selectCount(wrapper);
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class, BusinessException.class})
    public void applyInvoice(ApplyInvoiceDTO dto, Integer memberId) {
        // 查询订单
        Orders order = ordersMapper.selectById(dto.getOrderId());
        if (order == null || Constants.equalsInteger(order.getDeleted(), Constants.ONE)) {
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "订单不存在");
        }
        if (!Constants.equalsInteger(order.getMemberId(), memberId)) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "无权操作该订单");
        }
 
        // 校验发票状态:只有1=可申请、99=开具失败可以申请
        if (order.getInvoiceStatus() == null
                || (!Constants.equalsInteger(order.getInvoiceStatus(), Constants.ONE)
                && !Constants.equalsInteger(order.getInvoiceStatus(), 99))) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "当前订单不支持开票申请");
        }
 
        // 个人/事业单位只能开具电子普通发票
        if (Constants.equalsInteger(dto.getOrgType(), Constants.ZERO)
                && !Constants.equalsInteger(dto.getInvoiceType(), Constants.ZERO)) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "个人/事业单位只能开具电子普通发票");
        }
 
        // 企业类型:税号必填
        if (Constants.equalsInteger(dto.getOrgType(), Constants.ONE)
                && StringUtils.isBlank(dto.getTaxId())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "企业类型税号不能为空");
        }
 
        // 电子专用发票:开户银行、银行账号、企业地址、企业电话必填
        if (Constants.equalsInteger(dto.getInvoiceType(), Constants.ONE)) {
            if (StringUtils.isAnyBlank(dto.getBankName(), dto.getBankAccount(),
                    dto.getCompanyAddr(), dto.getCompanyPhone())) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "电子专用发票需填写开户银行、银行账号、企业地址、企业电话");
            }
        }
 
        // 计算开票金额
        long payAmount = order.getPayAmount() != null ? order.getPayAmount() : 0L;
        long refundAmount = order.getRefundAmount() != null ? order.getRefundAmount() : 0L;
        long invoiceAmount = payAmount - refundAmount;
 
        // 创建发票申请记录
        InvoiceRecord record = new InvoiceRecord();
        record.setOrderId(order.getId());
        record.setOrderNo(order.getCode());
        record.setMemberId(memberId);
        record.setOrgType(dto.getOrgType());
        record.setInvoiceType(dto.getInvoiceType());
        record.setName(dto.getName());
        record.setTaxId(dto.getTaxId());
        record.setBankName(dto.getBankName());
        record.setBankAccount(dto.getBankAccount());
        record.setCompanyAddr(dto.getCompanyAddr());
        record.setCompanyPhone(dto.getCompanyPhone());
        record.setInvoiceAmount(invoiceAmount);
        record.setStatus(Constants.ZERO); // 0=申请中
        record.setDeleted(Constants.ZERO);
        record.setCreateTime(new Date());
        invoiceRecordMapper.insert(record);
 
        InvoiceA0001Request invoiceA0001Request = new InvoiceA0001Request();
        invoiceA0001Request.setAccess_token("");//请求token
        invoiceA0001Request.setServiceKey("ebi_InvoiceHandle_newBlueInvoice");
 
 
        InvoiceA0001DTO invoiceA0001DTO  = new InvoiceA0001DTO();
        invoiceA0001DTO.setData_resources("API");
        invoiceA0001DTO.setItype("026");
        invoiceA0001DTO.setNsrsbh("");//销售方纳税人识别号
        invoiceA0001DTO.setOrder_num("");//业务单据号;必须是唯一的
        invoiceA0001DTO.setZsfs("0");
        invoiceA0001DTO.setTspz("00");
        invoiceA0001DTO.setXsf_yhzh("");//销售方开户行名称与银行账号  自贡市XX银行XX街支行 8888888888
        invoiceA0001DTO.setXsf_mc("");//销售方名称
        invoiceA0001DTO.setXsf_nsrsbh("");//销售方纳税人识别号
        invoiceA0001DTO.setXsf_dzdh("");//销售方地址、电话 自贡市XX街6号悠悠大厦D8幢8单元8层 0830-66008888
        invoiceA0001DTO.setGmf_mc(""); //购买方名称
        invoiceA0001DTO.setKpr(""); //开票人
        invoiceA0001DTO.setJshj("");//价税合计;单位:元(2位小数) 价税合计=合计金额(不含税)+合计税额 注意:不能使用商品的单价、数量、税率、税额来进行累加,最后四舍五入,只能是总合计金额+合计税额
        invoiceA0001DTO.setHjje("");//合计金额 注意:不含税,单位:元(2位小数)
        invoiceA0001DTO.setHjse("");//合计税额单位:元(2位小数)
 
        List<InvoiceA0001ItemDTO> common_fpkj_xmxx = new ArrayList<>();
        InvoiceA0001ItemDTO invoiceA0001ItemDTO = new InvoiceA0001ItemDTO();
        invoiceA0001ItemDTO.setFphxz("0");
        invoiceA0001ItemDTO.setXmmc("");//项目名称
        invoiceA0001ItemDTO.setXmdj("");//项目单价 小数点后6位 注意:单价是含税单价,大于0的数字
        invoiceA0001ItemDTO.setXmsl("1");
        invoiceA0001ItemDTO.setXmje("");
        invoiceA0001ItemDTO.setSe("");
        invoiceA0001ItemDTO.setSl("");
        common_fpkj_xmxx.add(invoiceA0001ItemDTO);
        invoiceA0001DTO.setCommon_fpkj_xmxx(common_fpkj_xmxx);
        invoiceA0001Request.setData(invoiceA0001DTO);
        // 更新订单发票状态为申请中
        order.setInvoiceStatus(Constants.TWO);
        order.setUpdateTime(new Date());
        ordersMapper.updateById(order);
    }
 
    @Override
    public PageData<InvoiceRecord> findMemberInvoicePage(PageWrap<InvoiceRecord> pageWrap, Integer memberId) {
        IPage<InvoiceRecord> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<InvoiceRecord> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(InvoiceRecord::getDeleted, Constants.ZERO)
                .eq(InvoiceRecord::getMemberId, memberId)
//                .eq(InvoiceRecord::getStatus, Constants.ONE) // 只查已开具成功
                .orderByDesc(InvoiceRecord::getCreateTime);
        return PageData.from(invoiceRecordMapper.selectPage(page, qw));
    }
 
    @Override
    public void sendInvoiceEmail(Integer memberId, Integer invoiceRecordId, String email) {
        InvoiceRecord record = invoiceRecordMapper.selectById(invoiceRecordId);
        if (record == null || Constants.equalsInteger(record.getDeleted(), Constants.ONE)) {
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "发票记录不存在");
        }
        if (!Constants.equalsInteger(record.getMemberId(), memberId)) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "无权操作该发票记录");
        }
        if (!Constants.equalsInteger(record.getStatus(), Constants.ONE)) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "发票尚未开具成功");
        }
        if (StringUtils.isBlank(record.getFileAddr())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "发票文件不存在");
        }
 
        // 拼接文件完整URL
        String fullUrl = record.getFileAddr();
        if (!fullUrl.startsWith("http")) {
            try {
                String prefix = systemDictDataBiz.queryByCode(Constants.OSS, Constants.RESOURCE_PATH).getCode();
                fullUrl = prefix + fullUrl;
            } catch (Exception e) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "文件地址配置缺失");
            }
        }
 
        // 下载发票文件
        byte[] fileBytes;
        try {
            fileBytes = new FtpUtil().getOnlineInputsteam(fullUrl);
        } catch (Exception e) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "发票文件下载失败");
        }
        if (fileBytes == null || fileBytes.length == 0) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "发票文件为空");
        }
 
        // 写入临时文件
        File tempFile = null;
        try {
            tempFile = File.createTempFile("invoice_", ".pdf");
            try (FileOutputStream fos = new FileOutputStream(tempFile)) {
                fos.write(fileBytes);
            }
 
            // 构建附件列表
            String fileName = "发票_" + record.getOrderNo() + ".pdf";
            Map<String, Object> attachment = new HashMap<>();
            attachment.put("name", fileName);
            attachment.put("file", tempFile);
 
            boolean success = emailService.sendWithAttachment(email, "您的电子发票", "您好,您的发票详见附件,请查收。", Collections.singletonList(attachment));
            if (!success) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "邮件发送失败");
            }
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "邮件发送异常");
        } finally {
            if (tempFile != null && tempFile.exists()) {
                tempFile.delete();
            }
        }
    }
 
    /**
     * 构建 admin 端发票查询条件(findPage 和 summary 共用)
     */
    private QueryWrapper<InvoiceRecord> buildAdminQueryWrapper(InvoiceRecord model) {
        Utils.MP.blankToNull(model);
        model.setDeleted(Constants.ZERO);
        QueryWrapper<InvoiceRecord> qw = new QueryWrapper<>();
        qw.lambda().eq(InvoiceRecord::getDeleted, Constants.ZERO);
        if (model.getInvoiceNo() != null && StringUtils.isNotBlank(model.getInvoiceNo())) {
            qw.lambda().like(InvoiceRecord::getInvoiceNo, model.getInvoiceNo());
        }
        if (model.getInvoiceType() != null) {
            qw.lambda().eq(InvoiceRecord::getInvoiceType, model.getInvoiceType());
        }
        if (model.getMemberId() != null) {
            qw.lambda().eq(InvoiceRecord::getMemberId, model.getMemberId());
        }
        if (model.getOrgType() != null) {
            qw.lambda().eq(InvoiceRecord::getOrgType, model.getOrgType());
        }
        if (model.getName() != null && StringUtils.isNotBlank(model.getName())) {
            qw.lambda().like(InvoiceRecord::getName, model.getName());
        }
        if (model.getStatus() != null) {
            qw.lambda().eq(InvoiceRecord::getStatus, model.getStatus());
        }
        if (model.getStartDate() != null) {
            qw.lambda().ge(InvoiceRecord::getCreateTime, model.getStartDate());
        }
        if (model.getEndDate() != null) {
            qw.lambda().le(InvoiceRecord::getCreateTime, Utils.Date.getEnd(model.getEndDate()));
        }
        return qw;
    }
 
    @Override
    public InvoiceRecordSummaryVO findPageSummary(PageWrap<InvoiceRecord> pageWrap) {
        QueryWrapper<InvoiceRecord> baseQw = buildAdminQueryWrapper(pageWrap.getModel());
 
        // 开票总额:符合条件的所有记录
        QueryWrapper<InvoiceRecord> totalQw = baseQw.clone();
        totalQw.select("IFNULL(SUM(INVOICE_AMOUNT), 0) as invoiceAmount");
        Map<String, Object> totalResult = invoiceRecordMapper.selectMaps(totalQw).stream().findFirst().orElse(null);
        long totalAmount = totalResult != null && totalResult.get("invoiceAmount") != null
                ? Long.parseLong(totalResult.get("invoiceAmount").toString()) : 0L;
 
        // 已开票总额:status=1
        QueryWrapper<InvoiceRecord> issuedQw = baseQw.clone();
        issuedQw.eq("STATUS", Constants.ONE);
        issuedQw.select("IFNULL(SUM(INVOICE_AMOUNT), 0) as invoiceAmount");
        Map<String, Object> issuedResult = invoiceRecordMapper.selectMaps(issuedQw).stream().findFirst().orElse(null);
        long issuedAmount = issuedResult != null && issuedResult.get("invoiceAmount") != null
                ? Long.parseLong(issuedResult.get("invoiceAmount").toString()) : 0L;
 
        InvoiceRecordSummaryVO vo = new InvoiceRecordSummaryVO();
        vo.setTotalAmount(totalAmount);
        vo.setIssuedAmount(issuedAmount);
        return vo;
    }
}