RepairInvoiceServiceImpl.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.fdkankan.sale.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  7. import com.fdkankan.sale.common.CacheUtil;
  8. import com.fdkankan.sale.common.FilePath;
  9. import com.fdkankan.sale.common.ResultCode;
  10. import com.fdkankan.sale.entity.*;
  11. import com.fdkankan.sale.exception.BusinessException;
  12. import com.fdkankan.sale.mapper.IRepairInvoiceMapper;
  13. import com.fdkankan.sale.service.*;
  14. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  15. import com.fdkankan.sale.vo.request.RepairInvoiceParam;
  16. import com.fdkankan.sale.vo.response.RepairInvoiceVo;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.springframework.beans.BeanUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import java.io.File;
  22. /**
  23. * <p>
  24. * 服务实现类
  25. * </p>
  26. *
  27. * @author
  28. * @since 2023-02-22
  29. */
  30. @Service
  31. public class RepairInvoiceServiceImpl extends ServiceImpl<IRepairInvoiceMapper, RepairInvoice> implements IRepairInvoiceService {
  32. @Autowired
  33. IMailTemplateService mailTemplateService;
  34. @Autowired
  35. IRepairService repairService;
  36. @Autowired
  37. FYunFileServiceInterface fYunFileServiceInterface;
  38. @Autowired
  39. ICustomerService customerService;
  40. @Override
  41. public RepairInvoice getByRepairId(String repairId) {
  42. LambdaQueryWrapper<RepairInvoice> wrapper = new LambdaQueryWrapper<>();
  43. wrapper.eq(RepairInvoice::getRepairId,repairId);
  44. return this.getOne(wrapper);
  45. }
  46. @Override
  47. public Page<RepairInvoiceVo> pageList(RepairInvoiceParam param) {
  48. Page<RepairInvoiceVo> voList = this.getBaseMapper().pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
  49. return voList;
  50. }
  51. @Override
  52. public void invoiceRegister(RepairInvoice param, Long userId) {
  53. if(StringUtils.isBlank(param.getRepairId()) ){
  54. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  55. }
  56. Repair repair = repairService.getById(param.getRepairId());
  57. if(repair == null){
  58. throw new BusinessException(ResultCode.ORDER_PAY_NOT_EXITS);
  59. }
  60. RepairInvoice repairInvoice = this.getByRepairId(param.getRepairId());
  61. if(repairInvoice !=null){
  62. throw new BusinessException(ResultCode.ORDER_INVOICE_EXITS);
  63. }
  64. repairInvoice = new RepairInvoice();
  65. BeanUtils.copyProperties(param,repairInvoice);
  66. this.save(repairInvoice);
  67. repairService.updateRepairInvoiceStatus(repair.getRepairId(),1);
  68. }
  69. @Override
  70. public void open(RepairInvoice param, Long userId) {
  71. if(param.getInvoiceId() == null ){
  72. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  73. }
  74. RepairInvoice repairInvoice = this.getById(param.getInvoiceId());
  75. if(repairInvoice == null ){
  76. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  77. }
  78. if(repairInvoice.getStatus() == 1){
  79. throw new BusinessException(ResultCode.ORDER_INVOICE_OPEN);
  80. }
  81. if(repairInvoice.getInvoiceType() == 0){ //普通发票邮件发送,专用发票邮寄
  82. if(StringUtils.isBlank(param.getInvoiceNo()) || StringUtils.isBlank(param.getInvoiceImg())){
  83. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  84. }
  85. String userName = repairInvoice.getInvoiceEmail();
  86. Customer customer = customerService.getByRepairId(repairInvoice.getRepairId());
  87. if(customer != null){
  88. userName = customer.getCompanyName();
  89. }
  90. MailTemplate mailTemplate = this.setMailMsg(repairInvoice.getRepairId(),userName);
  91. String imagePath = param.getInvoiceImg().replaceAll(CacheUtil.host,"");
  92. String localPath = FilePath.file_path + imagePath;
  93. fYunFileServiceInterface.downloadFile(imagePath,localPath);
  94. Boolean mail = mailTemplateService.sendMail(repairInvoice.getInvoiceEmail(), mailTemplate,localPath);
  95. if(!mail){
  96. throw new BusinessException(ResultCode.MAIL_SEND_ERROR);
  97. }
  98. FileUtil.del(localPath);
  99. LambdaUpdateWrapper<RepairInvoice> wrapper = new LambdaUpdateWrapper<>();
  100. wrapper.eq(RepairInvoice::getInvoiceId,param.getInvoiceId());
  101. wrapper.set(RepairInvoice::getInvoiceNo,param.getInvoiceNo());
  102. wrapper.set(RepairInvoice::getInvoiceImg,param.getInvoiceImg());
  103. wrapper.set(RepairInvoice::getStatus,1);
  104. wrapper.set(RepairInvoice::getSysUserId,userId);
  105. this.update(wrapper);
  106. }
  107. if(repairInvoice.getInvoiceType() == 1){ //普通发票邮件发送,专用发票邮寄
  108. if(StringUtils.isBlank(param.getInvoiceNo()) || StringUtils.isBlank(param.getTrackingNum())){
  109. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  110. }
  111. LambdaUpdateWrapper<RepairInvoice> wrapper = new LambdaUpdateWrapper<>();
  112. wrapper.eq(RepairInvoice::getInvoiceId,param.getInvoiceId());
  113. wrapper.set(RepairInvoice::getTrackingNum,param.getTrackingNum());
  114. wrapper.set(RepairInvoice::getInvoiceNo,param.getInvoiceNo());
  115. wrapper.set(RepairInvoice::getStatus,1);
  116. wrapper.set(RepairInvoice::getSysUserId,userId);
  117. this.update(wrapper);
  118. }
  119. repairService.updateInvoiceStatus(param.getRepairId());
  120. }
  121. private MailTemplate setMailMsg(String orderNum, String userName) {
  122. MailTemplate mailTemplate = mailTemplateService.getById(1);
  123. if(mailTemplate != null){
  124. String subject = mailTemplate.getSubject();
  125. if(StringUtils.isNotBlank(orderNum)){
  126. subject = subject.replace("{orderNum}",orderNum);
  127. }
  128. mailTemplate.setSubject(subject);
  129. String msg = mailTemplate.getMsg();
  130. if(StringUtils.isNotBlank(orderNum)){
  131. msg = msg.replace("{orderNum}",orderNum);
  132. }
  133. if(StringUtils.isNotBlank(userName)){
  134. msg = msg.replace("{userName}",userName);
  135. }
  136. mailTemplate.setMsg(msg);
  137. }
  138. return mailTemplate;
  139. }
  140. }