package com.fdkankan.sale.service.impl;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.fyun.face.FYunFileServiceInterface;
import com.fdkankan.sale.common.CacheUtil;
import com.fdkankan.sale.common.FilePath;
import com.fdkankan.sale.common.ResultCode;
import com.fdkankan.sale.entity.*;
import com.fdkankan.sale.exception.BusinessException;
import com.fdkankan.sale.mapper.IRepairInvoiceMapper;
import com.fdkankan.sale.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.sale.vo.request.RepairInvoiceParam;
import com.fdkankan.sale.vo.response.RepairInvoiceVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
/**
*
* 服务实现类
*
*
* @author
* @since 2023-02-22
*/
@Service
public class RepairInvoiceServiceImpl extends ServiceImpl implements IRepairInvoiceService {
@Autowired
IMailTemplateService mailTemplateService;
@Autowired
IRepairService repairService;
@Autowired
FYunFileServiceInterface fYunFileServiceInterface;
@Autowired
ICustomerService customerService;
@Override
public RepairInvoice getByRepairId(String repairId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(RepairInvoice::getRepairId,repairId);
return this.getOne(wrapper);
}
@Override
public Page pageList(RepairInvoiceParam param) {
Page voList = this.getBaseMapper().pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
return voList;
}
@Override
public void invoiceRegister(RepairInvoice param, Long userId) {
if(StringUtils.isBlank(param.getRepairId()) ){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
Repair repair = repairService.getById(param.getRepairId());
if(repair == null){
throw new BusinessException(ResultCode.ORDER_PAY_NOT_EXITS);
}
RepairInvoice repairInvoice = this.getByRepairId(param.getRepairId());
if(repairInvoice !=null){
throw new BusinessException(ResultCode.ORDER_INVOICE_EXITS);
}
repairInvoice = new RepairInvoice();
BeanUtils.copyProperties(param,repairInvoice);
this.save(repairInvoice);
repairService.updateRepairInvoiceStatus(repair.getRepairId(),1);
}
@Override
public void open(RepairInvoice param, Long userId) {
if(param.getInvoiceId() == null ){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
RepairInvoice repairInvoice = this.getById(param.getInvoiceId());
if(repairInvoice == null ){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
if(repairInvoice.getStatus() == 1){
throw new BusinessException(ResultCode.ORDER_INVOICE_OPEN);
}
if(repairInvoice.getInvoiceType() == 0){ //普通发票邮件发送,专用发票邮寄
if(StringUtils.isBlank(param.getInvoiceNo()) || StringUtils.isBlank(param.getInvoiceImg())){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
String userName = repairInvoice.getInvoiceEmail();
Customer customer = customerService.getByRepairId(repairInvoice.getRepairId());
if(customer != null){
userName = customer.getCompanyName();
}
MailTemplate mailTemplate = this.setMailMsg(repairInvoice.getRepairId(),userName);
String imagePath = param.getInvoiceImg().replaceAll(CacheUtil.host,"");
String localPath = FilePath.file_path + imagePath;
fYunFileServiceInterface.downloadFile(imagePath,localPath);
Boolean mail = mailTemplateService.sendMail(repairInvoice.getInvoiceEmail(), mailTemplate,localPath);
if(!mail){
throw new BusinessException(ResultCode.MAIL_SEND_ERROR);
}
FileUtil.del(localPath);
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(RepairInvoice::getInvoiceId,param.getInvoiceId());
wrapper.set(RepairInvoice::getInvoiceNo,param.getInvoiceNo());
wrapper.set(RepairInvoice::getInvoiceImg,param.getInvoiceImg());
wrapper.set(RepairInvoice::getStatus,1);
wrapper.set(RepairInvoice::getSysUserId,userId);
this.update(wrapper);
}
if(repairInvoice.getInvoiceType() == 1){ //普通发票邮件发送,专用发票邮寄
if(StringUtils.isBlank(param.getInvoiceNo()) || StringUtils.isBlank(param.getTrackingNum())){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(RepairInvoice::getInvoiceId,param.getInvoiceId());
wrapper.set(RepairInvoice::getTrackingNum,param.getTrackingNum());
wrapper.set(RepairInvoice::getInvoiceNo,param.getInvoiceNo());
wrapper.set(RepairInvoice::getStatus,1);
wrapper.set(RepairInvoice::getSysUserId,userId);
this.update(wrapper);
}
repairService.updateInvoiceStatus(param.getRepairId());
}
private MailTemplate setMailMsg(String orderNum, String userName) {
MailTemplate mailTemplate = mailTemplateService.getById(1);
if(mailTemplate != null){
String subject = mailTemplate.getSubject();
if(StringUtils.isNotBlank(orderNum)){
subject = subject.replace("{orderNum}",orderNum);
}
mailTemplate.setSubject(subject);
String msg = mailTemplate.getMsg();
if(StringUtils.isNotBlank(orderNum)){
msg = msg.replace("{orderNum}",orderNum);
}
if(StringUtils.isNotBlank(userName)){
msg = msg.replace("{userName}",userName);
}
mailTemplate.setMsg(msg);
}
return mailTemplate;
}
}