GoldenOrderServiceImpl.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package com.crm.custom.service.impl;
  2. import com.crm.custom.dao.mapper.GoldenOrderMapper;
  3. import com.crm.custom.dao.repository.*;
  4. import com.crm.custom.service.GoldenOrderService;
  5. import com.crm.rely.backend.core.constant.Constants;
  6. import com.crm.rely.backend.core.constant.EmailSendEnum;
  7. import com.crm.rely.backend.core.constant.MapConstants;
  8. import com.crm.rely.backend.core.dto.base.PageDto;
  9. import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
  10. import com.crm.rely.backend.core.entity.system.email.SysEmailSendEntity;
  11. import com.crm.rely.backend.core.exception.ServiceException;
  12. import com.crm.rely.backend.model.constant.ConfigConstants;
  13. import com.crm.rely.backend.model.constant.EmailTemplateConstants;
  14. import com.crm.rely.backend.model.constant.PayConstants;
  15. import com.crm.rely.backend.model.dto.golden.order.GoldenOrderDetailsDto;
  16. import com.crm.rely.backend.model.dto.golden.order.GoldenOrderListDto;
  17. import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
  18. import com.crm.rely.backend.model.entity.golden.order.GoldenOrderAddEntity;
  19. import com.crm.rely.backend.model.entity.golden.order.GoldenOrderSearchEntity;
  20. import com.crm.rely.backend.model.pojo.table.*;
  21. import com.crm.rely.backend.service.EmailService;
  22. import com.crm.rely.backend.service.MqSendService;
  23. import com.crm.rely.backend.util.DateUtil;
  24. import com.crm.rely.backend.util.GetRandom;
  25. import org.springframework.beans.BeanUtils;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.transaction.annotation.Transactional;
  29. import java.math.BigDecimal;
  30. import java.util.*;
  31. @Service
  32. public class GoldenOrderServiceImpl implements GoldenOrderService {
  33. @Autowired
  34. private GoldenOrderMapper mapper;
  35. @Autowired
  36. private GoldenOrderRepository repository;
  37. @Autowired
  38. private CustomInfoRepository customInfoRepository;
  39. @Autowired
  40. private FinanceDepositRepository financeDepositRepository;
  41. @Autowired
  42. private GoldenOrderDetailsRepository goldenOrderDetailsRepository;
  43. @Autowired
  44. private GoldenGoodsRepository goldenGoodsRepository;
  45. @Autowired
  46. private MqSendService mqSendService;
  47. @Autowired
  48. private EmailService emailService;
  49. @Override
  50. public ResultWithPagerDto<GoldenOrderListDto> searchPageList(GoldenOrderSearchEntity entity) throws ServiceException {
  51. Integer count = mapper.countList(entity);
  52. PageDto pageDto = PageDto.format(entity, count);
  53. if (count == null || count <= 0) {
  54. return ResultWithPagerDto.success(pageDto);
  55. }
  56. List<GoldenOrderTable> tables = mapper.pageList(entity);
  57. if (tables == null || tables.size() <= 0) {
  58. throw new ServiceException(Constants.SYSTEM_ERROR);
  59. }
  60. List<String> serialList = new ArrayList<>();
  61. for (GoldenOrderTable table : tables) {
  62. serialList.add(table.getSerial());
  63. }
  64. Map<String,List<GoldenOrderDetailsDto>> map = new HashMap<>();
  65. List<GoldenOrderDetailsDto> detailsDtos = mapper.getListBySerial(serialList);
  66. for (GoldenOrderDetailsDto dto : detailsDtos) {
  67. List<GoldenOrderDetailsDto> dtos = null;
  68. if (map.containsKey(dto.getSerial())) {
  69. dtos = map.get(dto.getSerial());
  70. }else {
  71. dtos = new ArrayList<>();
  72. }
  73. dtos.add(dto);
  74. map.put(dto.getSerial(), dtos);
  75. }
  76. List<GoldenOrderListDto> dtos = new ArrayList<>(tables.size());
  77. for (GoldenOrderTable table : tables) {
  78. GoldenOrderListDto dto = transformSearchListDto(table);
  79. dto.setDetails(map.get(table.getSerial()));
  80. dtos.add(dto);
  81. }
  82. return ResultWithPagerDto.success(pageDto, dtos);
  83. }
  84. @Override
  85. @Transactional(rollbackFor = Exception.class)
  86. public void completeOrder(Long id) throws ServiceException {
  87. GoldenOrderTable table = repository.findFirstById(id);
  88. if (table == null) {
  89. throw ServiceException.exception(Constants.INFO_NOT_FOUND);
  90. }
  91. if (table.getStatus() != PayConstants.ORDER_UP_PAY_STATUS) {
  92. throw ServiceException.exception("不能确认订单");
  93. }
  94. CustomInfoTable customTable = customInfoRepository.findFirstById(table.getCustomId());
  95. if (customTable == null) {
  96. throw ServiceException.exception(Constants.INFO_NOT_FOUND);
  97. }
  98. FinanceDepositTable depositTable = financeDepositRepository.findFirstBySerial(table.getSerial());
  99. if (depositTable == null) {
  100. throw ServiceException.exception(Constants.INFO_NOT_FOUND);
  101. }
  102. table.setStatus(PayConstants.ORDER_PAY_STATUS);
  103. table.setPayTime(new Date());
  104. table.setModifyTime(new Date());
  105. repository.save(table);
  106. depositTable.setStatus(PayConstants.MANAGER_COMPLETED_STATUS);
  107. depositTable.setModifyTime(new Date());
  108. financeDepositRepository.save(depositTable);
  109. BigDecimal totalSpendingAmount = customTable.getTotalSpendingAmount() == null ? BigDecimal.ZERO : customTable.getTotalSpendingAmount();
  110. customTable.setTotalSpendingAmount(totalSpendingAmount.add(table.getAmount()));
  111. customInfoRepository.save(customTable);
  112. List<GoldenOrderDetailsTable> detailsTables = goldenOrderDetailsRepository.getBySerial(table.getSerial());
  113. for (GoldenOrderDetailsTable detailsTable : detailsTables) {
  114. sendCustomGoldenOrderEmail(table.getPayName(),detailsTable.getDownload(),customTable.getEmail());
  115. }
  116. if (customTable.getTotalSpendingAmount().compareTo(new BigDecimal(5000)) >= 0) {
  117. mqSendService.send(ConfigConstants.CUSTOM_REWARD_RECORD, customTable.getId());
  118. }
  119. }
  120. @Override
  121. @Transactional(rollbackFor = Exception.class)
  122. public void cancelOrder(Long id) throws ServiceException {
  123. GoldenOrderTable table = repository.findFirstById(id);
  124. if (table == null) {
  125. throw ServiceException.exception(Constants.INFO_NOT_FOUND);
  126. }
  127. if (table.getStatus() != PayConstants.ORDER_UP_PAY_STATUS) {
  128. throw ServiceException.exception("cancel_error");
  129. }
  130. table.setStatus(PayConstants.ORDER_PAY_CANCEL_STATUS);
  131. table.setModifyTime(new Date());
  132. repository.save(table);
  133. }
  134. @Override
  135. public void generated(GoldenOrderAddEntity entity, CustomInfoEntity infoEntity) throws ServiceException {
  136. GoldenGoodsTable goodsTable = goldenGoodsRepository.getFirstById(entity.getGoodsId());
  137. if (goodsTable == null) {
  138. throw ServiceException.exception(Constants.INFO_NOT_FOUND);
  139. }
  140. if (goodsTable.getGoodsPrice().compareTo(BigDecimal.ZERO) > 0) {
  141. throw ServiceException.exception("请下单购买!");
  142. }
  143. GoldenOrderDetailsTable details = mapper.getByCustomIdAndGoodsId(infoEntity.getId(),entity.getGoodsId());
  144. if (details != null) {
  145. throw ServiceException.exception("课程已购买");
  146. }
  147. String orderNo = GetRandom.getOrderNo(PayConstants.DEPOSIT_HEAD);
  148. GoldenOrderTable table = new GoldenOrderTable();
  149. table.setSerial(orderNo);
  150. table.setCId(infoEntity.getCId());
  151. table.setCustomId(infoEntity.getId());
  152. table.setStatus(PayConstants.ORDER_PAY_STATUS);
  153. table.setAmount(BigDecimal.ZERO);
  154. table.setCurrency("USD");
  155. table.setTransformAmount(BigDecimal.ZERO);
  156. table.setTransformCurrency("USD");
  157. table.setPayName(infoEntity.getName());
  158. table.setPayPhone(infoEntity.getPhone());
  159. table.setPayTime(new Date());
  160. table.setAddTime(new Date());
  161. repository.save(table);
  162. GoldenOrderDetailsTable detailsTable = new GoldenOrderDetailsTable();
  163. BeanUtils.copyProperties(goodsTable, detailsTable);
  164. detailsTable.setId(null);
  165. detailsTable.setGoodsId(goodsTable.getId());
  166. detailsTable.setSerial(orderNo);
  167. detailsTable.setCustomId(infoEntity.getId());
  168. goldenOrderDetailsRepository.save(detailsTable);
  169. sendCustomGoldenOrderEmail(infoEntity.getName(), detailsTable.getDownload(), infoEntity.getEmail());
  170. }
  171. private GoldenOrderListDto transformSearchListDto(GoldenOrderTable table) {
  172. GoldenOrderListDto dto = new GoldenOrderListDto();
  173. BeanUtils.copyProperties(table, dto);
  174. return dto;
  175. }
  176. private void sendCustomGoldenOrderEmail(String name, String download, String email) throws ServiceException {
  177. //构建map 用于后面邮件模版替换
  178. Map<String, String> map = new HashMap<>(3);
  179. map.put(MapConstants.NAME, name);
  180. map.put(ConfigConstants.DOWNLOAD_URL, download);
  181. map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
  182. SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
  183. sysEmailSendEntity.setSubject("Trading Education");
  184. sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.CUSTOM_ORDER_SUCCESS);
  185. sysEmailSendEntity.setMap(map);
  186. sysEmailSendEntity.setTemplateName(EmailTemplateConstants.CUSTOM_ORDER_SUCCESS_CN);
  187. sysEmailSendEntity.setUsers(email);
  188. try {
  189. emailService.sendEmail(sysEmailSendEntity);
  190. } catch (Exception e) {
  191. e.printStackTrace();
  192. }
  193. }
  194. }