InvoiceServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. package com.fdkankan.ucenter.service.impl;
  2. import cn.hutool.core.date.DateUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  7. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  8. import com.fdkankan.common.constant.AppConstant;
  9. import com.fdkankan.common.constant.ErrorCode;
  10. import com.fdkankan.common.constant.ServerCode;
  11. import com.fdkankan.common.constant.TbStatus;
  12. import com.fdkankan.common.exception.BusinessException;
  13. import com.fdkankan.ucenter.common.FileUtil;
  14. import com.fdkankan.ucenter.common.OssPath;
  15. import com.fdkankan.ucenter.common.PageInfo;
  16. import com.fdkankan.ucenter.common.RandomCodeUtil;
  17. import com.fdkankan.ucenter.common.constants.NacosProperty;
  18. import com.fdkankan.ucenter.config.ThreadPoolConfig;
  19. import com.fdkankan.ucenter.entity.*;
  20. import com.fdkankan.ucenter.mapper.IInvoiceMapper;
  21. import com.fdkankan.ucenter.service.*;
  22. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  23. import com.fdkankan.ucenter.util.MyExcelUtil;
  24. import com.fdkankan.ucenter.vo.request.InvoiceParam;
  25. import com.fdkankan.ucenter.vo.request.PlaceOrderParam;
  26. import com.fdkankan.ucenter.vo.response.OrderVo;
  27. import com.sun.xml.bind.v2.model.core.ID;
  28. import lombok.extern.slf4j.Slf4j;
  29. import org.apache.commons.lang3.StringUtils;
  30. import org.aspectj.weaver.ast.Or;
  31. import org.springframework.beans.BeanUtils;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.stereotype.Service;
  34. import java.math.BigDecimal;
  35. import java.text.SimpleDateFormat;
  36. import java.util.Date;
  37. import java.util.HashMap;
  38. import java.util.List;
  39. import java.util.Set;
  40. /**
  41. * <p>
  42. * 发票表 服务实现类
  43. * </p>
  44. *
  45. * @author
  46. * @since 2022-07-13
  47. */
  48. @Service
  49. @Slf4j
  50. public class InvoiceServiceImpl extends ServiceImpl<IInvoiceMapper, Invoice> implements IInvoiceService {
  51. @Autowired
  52. IUserService userService;
  53. @Autowired
  54. IOrderService orderService;
  55. @Autowired
  56. IIncrementOrderService incrementOrderService;
  57. @Autowired
  58. IVirtualOrderService virtualOrderService;
  59. @Autowired
  60. IExpansionOrderService expansionOrderService;
  61. @Autowired
  62. IDownloadOrderService downloadOrderService;
  63. @Autowired
  64. IMailTemplateService mailTemplateService;
  65. @Override
  66. public Invoice getByOrderId(Long orderId) {
  67. LambdaQueryWrapper<Invoice> wrapper = new LambdaQueryWrapper<>();
  68. wrapper.eq(Invoice::getOrderId,orderId);
  69. List<Invoice> list = this.list(wrapper);
  70. if(list != null && list.size() >0){
  71. return list.get(0);
  72. }
  73. return null;
  74. }
  75. @Override
  76. public Invoice getByIncrementOrderId(Long incrementOrderId) {
  77. LambdaQueryWrapper<Invoice> wrapper = new LambdaQueryWrapper<>();
  78. wrapper.eq(Invoice::getIncrementOrderId,incrementOrderId);
  79. List<Invoice> list = this.list(wrapper);
  80. if(list != null && list.size() >0){
  81. return list.get(0);
  82. }
  83. return null;
  84. }
  85. @Override
  86. public HashMap<Long, Invoice> getByIncrementOrderIds(Set<Long> ids) {
  87. HashMap<Long, Invoice> map = new HashMap<>();
  88. if(ids.size() >0){
  89. LambdaQueryWrapper<Invoice> wrapper = new LambdaQueryWrapper<>();
  90. wrapper.in(Invoice::getIncrementOrderId,ids);
  91. List<Invoice> list = this.list(wrapper);
  92. list.forEach(entity ->map.put(entity.getIncrementOrderId(),entity));
  93. }
  94. return map;
  95. }
  96. @Override
  97. public Invoice getByDownId(Long downId) {
  98. LambdaQueryWrapper<Invoice> wrapper = new LambdaQueryWrapper<>();
  99. wrapper.eq(Invoice::getDownloadOrderId,downId);
  100. List<Invoice> list = this.list(wrapper);
  101. if(list != null && list.size() >0){
  102. return list.get(0);
  103. }
  104. return null;
  105. }
  106. @Override
  107. public void saveByOrder(Long userId, PlaceOrderParam orderParam) {
  108. InvoiceParam param = orderParam.getInvoice();
  109. Invoice invoiceEntity = new Invoice();
  110. invoiceEntity.setUserId(userId);
  111. invoiceEntity.setType(Integer.valueOf(param.getInvoiceType()));
  112. invoiceEntity.setTitle(param.getTitle());
  113. invoiceEntity.setCode(param.getCode());
  114. invoiceEntity.setEmailAddress(param.getEmailAddress());
  115. if (3 == invoiceEntity.getType()){
  116. invoiceEntity.setOrganizedAddress(param.getOrganizedAddress());
  117. invoiceEntity.setRegisterPhone(param.getRegisterPhone());
  118. invoiceEntity.setBankName(param.getBankName());
  119. invoiceEntity.setBankAccount(param.getBankAccount());
  120. }
  121. invoiceEntity.setConsumeType(0);
  122. invoiceEntity.setOrderId(orderParam.getOrderId());
  123. invoiceEntity.setShipName(orderParam.getReceiver().getShipName());
  124. invoiceEntity.setShipMobile(orderParam.getReceiver().getShipMobile());
  125. invoiceEntity.setShipAddress(orderParam.getReceiver().getShipAddress());
  126. invoiceEntity.setShipAreaPath(orderParam.getReceiver().getShipAreaPath());
  127. invoiceEntity.setEmailAddress(param.getEmailAddress());
  128. invoiceEntity.setMoney(new BigDecimal(param.getAmount()));
  129. this.save(invoiceEntity);
  130. }
  131. @Override
  132. public void checkInvoice(Long orderId, Integer consumeType) {
  133. LambdaQueryWrapper<Invoice> wrapper = new LambdaQueryWrapper<>();
  134. switch (consumeType){
  135. case 0 : wrapper.eq(Invoice::getOrderId,orderId); break;
  136. case 1 : wrapper.eq(Invoice::getVirtualOrderId,orderId); break;
  137. case 2 :
  138. case 3 : wrapper.eq(Invoice::getIncrementOrderId,orderId); break;
  139. }
  140. long count = this.count(wrapper);
  141. if(count >0){
  142. throw new BusinessException(AppConstant.FAILURE_CODE_4021, AppConstant.FAILURE_MSG_4021);
  143. }
  144. }
  145. public void getMoney(Long orderId, Integer consumeType,Invoice dbEntity) {
  146. dbEntity.setMoney(BigDecimal.ZERO);
  147. switch (consumeType){
  148. case 0 :
  149. Order order = orderService.getById(orderId);
  150. if(order.getPaymentStatus().equals("paid")){
  151. dbEntity.setMoney(order.getTotalAmount());
  152. dbEntity.setOrderSn(order.getOrderSn());
  153. dbEntity.setProductCount(order.getGoodsTotalCount());
  154. dbEntity.setProductName(order.getTradeNum());
  155. dbEntity.setPayType(order.getOrderSn());
  156. }
  157. break;
  158. case 1 :
  159. VirtualOrder virtualOrder = virtualOrderService.getById(orderId);
  160. if(virtualOrder.getPayStatus() == 1){
  161. dbEntity.setMoney(virtualOrder.getAmount());
  162. dbEntity.setOrderSn(virtualOrder.getOrderSn());
  163. dbEntity.setProductCount(virtualOrder.getPoints());
  164. dbEntity.setProductName(virtualOrder.getNumber());
  165. dbEntity.setPayType(virtualOrder.getOrderSn());
  166. }
  167. break;
  168. case 2 :
  169. IncrementOrder incrementOrder = incrementOrderService.getById(orderId);
  170. if(incrementOrder.getPayStatus() == 1){
  171. dbEntity.setMoney(incrementOrder.getAmount());
  172. dbEntity.setOrderSn(incrementOrder.getOrderSn());
  173. dbEntity.setProductCount(incrementOrder.getCount());
  174. if("SE".equals(incrementOrder.getMemberLevels())){
  175. dbEntity.setProductName("Senior (Monthly) *" +incrementOrder.getMonthQy());
  176. }else {
  177. dbEntity.setProductName("Premium (Yearly) *" +incrementOrder.getMonthQy());
  178. }
  179. if(incrementOrder.getPayType() ==2 ){
  180. dbEntity.setPayType("paypal");
  181. }
  182. if(incrementOrder.getPayType() ==6 ){
  183. dbEntity.setPayType("stripe");
  184. }
  185. }
  186. break;
  187. case 3 :
  188. DownloadOrder downloadOrder = downloadOrderService.getById(orderId);
  189. if(downloadOrder.getPayStatus() == 1){
  190. dbEntity.setMoney(downloadOrder.getAmount());
  191. dbEntity.setOrderSn(downloadOrder.getOrderSn());
  192. dbEntity.setProductCount(downloadOrder.getCount());
  193. dbEntity.setProductName("Scene Download");
  194. if(downloadOrder.getPayType() ==2 ){
  195. dbEntity.setPayType("paypal");
  196. }
  197. if(downloadOrder.getPayType() ==6 ){
  198. dbEntity.setPayType("stripe");
  199. }
  200. }
  201. break;
  202. }
  203. }
  204. @Override
  205. public Invoice openInvoice(InvoiceParam param, String token) {
  206. User user = userService.getByToken(token);
  207. //验证是否已经开票
  208. this.checkInvoice(param.getOrderId(),param.getConsumeType());
  209. //获取开票金额
  210. Invoice dbEntity = new Invoice();
  211. this.getMoney(param.getOrderId(),param.getConsumeType(),dbEntity);
  212. BeanUtils.copyProperties(param, dbEntity);
  213. dbEntity.setOrderId(null);
  214. switch (param.getConsumeType()){
  215. case 0: dbEntity.setOrderId(param.getOrderId()); break;
  216. case 1: dbEntity.setVirtualOrderId(param.getOrderId()); break;
  217. case 2: dbEntity.setIncrementOrderId(param.getOrderId());break;
  218. case 3: dbEntity.setDownloadOrderId(param.getOrderId()); break;
  219. }
  220. if (param.getCameraId() != null){
  221. dbEntity.setCameraId(param.getCameraId());
  222. }
  223. dbEntity.setUserId(user.getId());
  224. this.save(dbEntity);
  225. String invoiceNumber = this.updateInvoiceNumber(dbEntity.getId());
  226. dbEntity.setInvoiceNumber(invoiceNumber);
  227. sendEmail(dbEntity);
  228. return dbEntity;
  229. }
  230. @Autowired
  231. ThreadPoolConfig threadPoolConfig;
  232. private void sendEmail(Invoice dbEntity) {
  233. //国际服直接发送邮件
  234. threadPoolConfig.taskExecutor().execute(() -> {
  235. log.info("异步任务执行,当前线程:" + Thread.currentThread().getName());
  236. if("aws".equals(NacosProperty.uploadType) && dbEntity.getEmailAddress() != null){
  237. MailTemplate mailTemplate = mailTemplateService.getTemplate(14, "en");
  238. dbEntity.setCreateTime(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
  239. String filePath = MyExcelUtil.excelTemplteToPdf(OssPath.localPath +"/invoice", "invoiceTemplate", dbEntity);
  240. mailTemplateService.sendMail(dbEntity.getEmailAddress(),mailTemplate,filePath);
  241. FileUtil.delFile(filePath);
  242. FileUtil.delFile(filePath+".sy");
  243. FileUtil.delFile(filePath.replace(".pdf",".xlsx"));
  244. }
  245. });
  246. }
  247. private String updateInvoiceNumber(Long id) {
  248. String format1 = new SimpleDateFormat("yyMMdd").format(new Date());
  249. String invoiceNumber = "W-"+format1 + id;
  250. LambdaUpdateWrapper<Invoice> wrapper = new LambdaUpdateWrapper<>();
  251. wrapper.eq(Invoice::getId,id);
  252. wrapper.set(Invoice::getInvoiceNumber,invoiceNumber);
  253. this.update(wrapper);
  254. return invoiceNumber;
  255. }
  256. private synchronized String genInVoiceNumber() {
  257. String invoiceNumber = RandomCodeUtil.generateCode("W-", 8);
  258. LambdaQueryWrapper<Invoice> wrapper = new LambdaQueryWrapper<>();
  259. wrapper.eq(Invoice::getInvoiceNumber,invoiceNumber);
  260. List<Invoice> list = this.list(wrapper);
  261. if(list == null || list.isEmpty()){
  262. return invoiceNumber;
  263. }else {
  264. return genInVoiceNumber();
  265. }
  266. }
  267. @Override
  268. public JSONObject getMaxInvoice(InvoiceParam param) {
  269. JSONObject object = new JSONObject();
  270. int maxInvoice = this.getMaxInvoice(param.getCameraId());
  271. object.put("maxInvoice", maxInvoice < 0 ? 0 : maxInvoice);
  272. return object;
  273. }
  274. private Integer getMaxInvoice(Long cameraId){
  275. List<VirtualOrder> virtualOrderList = virtualOrderService.getByCameraId(cameraId);
  276. int maxInvoice = 0;
  277. for (VirtualOrder virtualOrderEntity : virtualOrderList){
  278. if (virtualOrderEntity.getStatus() == 1 && virtualOrderEntity.getPayStatus() == 1){
  279. maxInvoice += virtualOrderEntity.getPoints();
  280. }else if (virtualOrderEntity.getStatus() == -2){
  281. maxInvoice -= virtualOrderEntity.getPoints();
  282. }
  283. }
  284. List<ExpansionOrder> expansionOrderList = expansionOrderService.getByCameraId(cameraId);
  285. for(ExpansionOrder expansionOrderEntity : expansionOrderList){
  286. if(expansionOrderEntity.getPayStatus() == 1){
  287. maxInvoice += expansionOrderEntity.getAmount().intValue();
  288. }else if (expansionOrderEntity.getPayStatus() == -2){
  289. maxInvoice -= expansionOrderEntity.getAmount().intValue();
  290. }
  291. }
  292. LambdaQueryWrapper<Invoice> wrapper = new LambdaQueryWrapper<>();
  293. wrapper.eq(Invoice::getCameraId,cameraId);
  294. wrapper.eq(Invoice::getConsumeType,1);
  295. List<Invoice> list = this.list(wrapper);
  296. for (Invoice entity : list){
  297. if (entity.getMoney() != null){
  298. maxInvoice -= entity.getMoney().intValue();
  299. }
  300. }
  301. return maxInvoice;
  302. }
  303. @Override
  304. public PageInfo getInvoices(String token, InvoiceParam param) {
  305. LambdaQueryWrapper<Invoice> wrapper = new LambdaQueryWrapper<>();
  306. if(StringUtils.isNotBlank(token)){
  307. User user = userService.getByToken(token);
  308. wrapper.eq(Invoice::getUserId,user.getId());
  309. }
  310. if(param.getCameraId() != null){
  311. wrapper.eq(Invoice::getCameraId,param.getCameraId());
  312. }
  313. if(param.getType() != null){
  314. if(param.getType() != 0){
  315. wrapper.ne(Invoice::getType,0);
  316. }else {
  317. wrapper.eq(Invoice::getType,1);
  318. }
  319. }
  320. wrapper.ne(Invoice::getConsumeType ,-1);
  321. wrapper.orderByDesc(Invoice::getCreateTime);
  322. Page<Invoice> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
  323. return PageInfo.PageInfo(page);
  324. }
  325. @Override
  326. public Invoice getMyInvoiceInfo(String token, InvoiceParam param) {
  327. User user = userService.getByToken(token);
  328. return getMineInvoice(user.getId(),param.getInvoiceType());
  329. }
  330. private Invoice getMineInvoice(Long userId,String invoiceType){
  331. LambdaQueryWrapper<Invoice> wrapper = new LambdaQueryWrapper<>();
  332. wrapper.eq(Invoice::getUserId,userId);
  333. wrapper.eq(Invoice::getConsumeType ,-1);
  334. wrapper.eq(Invoice::getType ,invoiceType);
  335. wrapper.last("LIMIT 1");
  336. List<Invoice> list = this.list(wrapper);
  337. if(list == null || list.size()<=0){
  338. return null;
  339. }
  340. return list.get(0);
  341. }
  342. @Override
  343. public void saveInvoice(String token, InvoiceParam param) {
  344. User user = userService.getByToken(token);
  345. Invoice dbEntity = getMineInvoice(user.getId(),param.getInvoiceType());
  346. if (dbEntity == null){
  347. dbEntity = new Invoice();
  348. }
  349. if (param.getCameraId() != null){
  350. dbEntity.setCameraId(param.getCameraId());
  351. }
  352. dbEntity.setType(Integer.valueOf(param.getInvoiceType()));
  353. dbEntity.setUserId(user.getId());
  354. dbEntity.setBankAccount(param.getBankAccount());
  355. dbEntity.setBankName(param.getBankName());
  356. dbEntity.setRegisterPhone(param.getRegisterPhone());
  357. dbEntity.setOrganizedAddress(param.getOrganizedAddress());
  358. dbEntity.setCode(param.getCode());
  359. dbEntity.setTitle(param.getTitle());
  360. dbEntity.setEmailAddress(param.getEmailAddress());
  361. dbEntity.setConsumeType(-1);
  362. if(!this.saveOrUpdate(dbEntity)){
  363. throw new BusinessException(ErrorCode.ERROR_MSG);
  364. }
  365. }
  366. @Override
  367. public void addInvoice(String token, InvoiceParam param) {
  368. User user = userService.getByToken(token);
  369. if(param.getAmount() == null){
  370. throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
  371. }
  372. int maxInvoice = this.getMaxInvoice(param.getCameraId());
  373. if(Integer.parseInt(param.getAmount()) > maxInvoice){
  374. throw new BusinessException(ErrorCode.FAILURE_CODE_8003);
  375. }
  376. saveOrUpdateInvoice(user.getId(),param,null);
  377. }
  378. @Override
  379. public void updateInvoice(String token, InvoiceParam param) {
  380. User user = userService.getByToken(token);
  381. if(param.getInvoiceId() != null){
  382. Invoice invoiceEntity = this.getById(param.getInvoiceId());
  383. if(invoiceEntity == null){
  384. throw new BusinessException(ErrorCode.NOT_RECORD);
  385. }
  386. this.saveOrUpdateInvoice(user.getId(), param,invoiceEntity);
  387. }else {
  388. OrderVo order = orderService.getOrderDetail(param.getOrderId());
  389. if(order == null || order.getTotalAmount() == null){
  390. throw new BusinessException(ErrorCode.ERROR_MSG);
  391. }
  392. param.setAmount(order.getTotalAmount().toString());
  393. this.saveOrUpdateInvoice(user.getId(),param,null);
  394. }
  395. }
  396. private void saveOrUpdateInvoice(Long userId, InvoiceParam param, Invoice invoiceEntity){
  397. if(invoiceEntity == null){
  398. invoiceEntity = new Invoice();
  399. invoiceEntity.setUserId(userId);
  400. invoiceEntity.setOrderId(param.getOrderId());
  401. }
  402. invoiceEntity.setType(Integer.valueOf(param.getInvoiceType()));
  403. invoiceEntity.setTitle(param.getTitle());
  404. invoiceEntity.setCode(param.getCode());
  405. if (3 == invoiceEntity.getType()){
  406. invoiceEntity.setOrganizedAddress(param.getOrganizedAddress());
  407. invoiceEntity.setRegisterPhone(param.getRegisterPhone());
  408. invoiceEntity.setBankName(param.getBankName());
  409. invoiceEntity.setBankAccount(param.getBankAccount());
  410. }
  411. invoiceEntity.setCameraId(param.getCameraId());
  412. invoiceEntity.setMoney(new BigDecimal(param.getAmount()));
  413. invoiceEntity.setConsumeType(param.getConsumeType() == null? 1 : param.getConsumeType());
  414. invoiceEntity.setShipName(param.getShipName());
  415. invoiceEntity.setShipMobile(param.getShipMobile());
  416. invoiceEntity.setShipAddress(param.getShipAddress());
  417. invoiceEntity.setShipAreaPath(param.getShipAreaPath());
  418. invoiceEntity.setCameraId(param.getCameraId());
  419. invoiceEntity.setEmailAddress(param.getEmailAddress());
  420. if(!this.saveOrUpdate(invoiceEntity)){
  421. throw new BusinessException(ErrorCode.ERROR_MSG);
  422. }
  423. }
  424. }