feat(后台管理): 代码优化

master
wayn 4 years ago
parent 5c7d2ec026
commit cc56f80879

@ -2,6 +2,9 @@ package com.wayn.common.base.service;
import com.alibaba.fastjson.JSON;
import com.wayn.common.base.entity.ElasticEntity;
import com.wayn.common.constant.SysConstants;
import com.wayn.common.core.domain.shop.Goods;
import com.wayn.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
@ -31,9 +34,7 @@ import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.*;
@Slf4j
@Component
@ -170,8 +171,9 @@ public class BaseElasticService {
/**
*
*
* @param idxName
* @param id ID
* @param id ID
* @return boolean
*/
public boolean delete(String idxName, String id) {
@ -277,4 +279,28 @@ public class BaseElasticService {
throw new RuntimeException(e);
}
}
/**
* es
*
* @param goods
*/
public void syncGoods2Es(Goods goods) {
// 同步es
ElasticEntity elasticEntity = new ElasticEntity();
elasticEntity.setId(goods.getId().toString());
Map<String, Object> map = new HashMap<>();
map.put("id", goods.getId());
map.put("name", goods.getName());
map.put("countPrice", goods.getCounterPrice());
map.put("retailPrice", goods.getRetailPrice());
map.put("keyword", Objects.isNull(goods.getKeywords()) ? Collections.emptyList() : goods.getKeywords().split(","));
map.put("isOnSale", goods.getIsOnSale());
map.put("createTime", goods.getCreateTime());
elasticEntity.setData(map);
boolean one = insertOrUpdateOne(SysConstants.ES_GOODS_INDEX, elasticEntity);
if (!one) {
throw new BusinessException("商品同步es失败");
}
}
}

@ -0,0 +1,14 @@
package com.wayn.common.core.service.shop;
public interface IMailService {
/**
*
* @param subject
* @param content
* @param tos
* @param notifyUrl
*/
void sendEmail(String subject, String content, String tos, String notifyUrl);
}

@ -9,16 +9,11 @@ import com.wayn.common.core.domain.shop.Order;
import com.wayn.common.core.domain.shop.OrderGoods;
import com.wayn.common.core.domain.vo.ShipVO;
import com.wayn.common.core.mapper.shop.AdminOrderMapper;
import com.wayn.common.core.service.shop.IAdminOrderService;
import com.wayn.common.core.service.shop.IGoodsProductService;
import com.wayn.common.core.service.shop.IMemberService;
import com.wayn.common.core.service.shop.IOrderGoodsService;
import com.wayn.common.core.service.tool.IMailConfigService;
import com.wayn.common.core.service.shop.*;
import com.wayn.common.core.util.OrderUtil;
import com.wayn.common.util.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@ -34,8 +29,6 @@ import java.util.Map;
@Service
public class AdminOrderServiceImpl extends ServiceImpl<AdminOrderMapper, Order> implements IAdminOrderService {
@Autowired
RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法
@Autowired
private AdminOrderMapper adminOrderMapper;
// @Autowired
@ -47,7 +40,7 @@ public class AdminOrderServiceImpl extends ServiceImpl<AdminOrderMapper, Order>
@Autowired
private IMemberService iMemberService;
@Autowired
private IMailConfigService mailConfigService;
private IMailService iMailService;
@Value("${wayn.adminUrl}")
private String adminUrl;
@ -130,7 +123,7 @@ public class AdminOrderServiceImpl extends ServiceImpl<AdminOrderMapper, Order>
// 注意订单号只发后6位
String email = iMemberService.getById(order.getUserId()).getEmail();
if (StringUtils.isNotEmpty(email)) {
sendEmail("订单已经退款", order.getOrderSn().substring(8, 14), email, adminUrl);
iMailService.sendEmail("订单已经退款", order.getOrderSn().substring(8, 14), email, adminUrl);
}
// logHelper.logOrderSucceed("退款", "订单编号 " + order.getOrderSn());
return R.success();
@ -162,7 +155,7 @@ public class AdminOrderServiceImpl extends ServiceImpl<AdminOrderMapper, Order>
// "您的订单已经发货,快递公司 {1},快递单 {2} ,请注意查收"
String email = iMemberService.getById(order.getUserId()).getEmail();
if (StringUtils.isNotEmpty(email)) {
sendEmail("您的订单已经发货,快递公司 申通,快递单 " + order.getOrderSn().substring(8, 14) + ",请注意查收", order.getOrderSn().substring(8, 14), email, adminUrl);
iMailService.sendEmail("您的订单已经发货,快递公司 申通,快递单 " + order.getOrderSn().substring(8, 14) + ",请注意查收", order.getOrderSn().substring(8, 14), email, adminUrl);
}
return R.success();
}
@ -182,20 +175,5 @@ public class AdminOrderServiceImpl extends ServiceImpl<AdminOrderMapper, Order>
return R.success().add("data", data);
}
/**
*
*
* @param subject
* @param content
* @param tos
*/
private void sendEmail(String subject, String content, String tos, String notifyUrl) {
Map<String, Object> map = new HashMap<>();
map.put("subject", subject);
map.put("content", content);
map.put("tos", tos);
map.put("notifyUrl", notifyUrl);
// 异步发送邮件
rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
}
}

@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wayn.common.base.entity.ElasticEntity;
import com.wayn.common.base.service.BaseElasticService;
import com.wayn.common.constant.SysConstants;
import com.wayn.common.core.domain.shop.*;
@ -141,22 +140,7 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
// 保存商品货品
iGoodsProductService.saveBatch(Arrays.asList(products));
// 同步es
ElasticEntity elasticEntity = new ElasticEntity();
elasticEntity.setId(goods.getId().toString());
Map<String, Object> map = new HashMap<>();
map.put("id", goods.getId());
map.put("name", goods.getName());
map.put("countPrice", goods.getCounterPrice());
map.put("retailPrice", goods.getRetailPrice());
map.put("keyword", goods.getKeywords().split(","));
map.put("isOnSale", goods.getIsOnSale());
map.put("createTime", goods.getCreateTime());
elasticEntity.setData(map);
boolean one = baseElasticService.insertOrUpdateOne(SysConstants.ES_GOODS_INDEX, elasticEntity);
if (!one) {
throw new BusinessException("创建商品同步es失败");
}
baseElasticService.syncGoods2Es(goods);
return R.success();
}
@ -238,22 +222,8 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
iGoodsAttributeService.saveBatch(insertAttributes);
// 更新商品货品
iGoodsProductService.updateBatchById(Arrays.asList(products));
// 同步es
ElasticEntity elasticEntity = new ElasticEntity();
elasticEntity.setId(goods.getId().toString());
Map<String, Object> map = new HashMap<>();
map.put("id", goods.getId());
map.put("name", goods.getName());
map.put("countPrice", goods.getCounterPrice());
map.put("retailPrice", goods.getRetailPrice());
map.put("keyword", Objects.isNull(goods.getKeywords()) ? Collections.emptyList() : goods.getKeywords().split(","));
map.put("isOnSale", goods.getIsOnSale());
map.put("createTime", goods.getCreateTime());
elasticEntity.setData(map);
boolean one = baseElasticService.insertOrUpdateOne(SysConstants.ES_GOODS_INDEX, elasticEntity);
if (!one) {
throw new BusinessException("创建商品同步es失败");
}
baseElasticService.syncGoods2Es(goods);
return R.success();
}

@ -0,0 +1,26 @@
package com.wayn.common.core.service.shop.impl;
import com.wayn.common.core.service.shop.IMailService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class IMailServiceImpl implements IMailService {
@Autowired
RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法
@Override
public void sendEmail(String subject, String content, String tos, String notifyUrl) {
Map<String, Object> map = new HashMap<>();
map.put("subject", subject);
map.put("content", content);
map.put("tos", tos);
map.put("notifyUrl", notifyUrl);
// 异步发送邮件
rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
}
}

@ -30,13 +30,13 @@ public class AddressController {
if (address.getIsDefault()) {
iAddressService.update().eq("member_id", memberId).set("is_default", false).update();
}
if (Objects.isNull(address.getId())) {
address.setMemberId(memberId);
address.setCreateTime(new Date());
return R.result(iAddressService.save(address));
} else {
if (Objects.nonNull(address.getId())) {
address.setUpdateTime(new Date());
return R.result(iAddressService.updateById(address));
}
address.setMemberId(memberId);
address.setCreateTime(new Date());
return R.result(iAddressService.save(address));
}
@DeleteMapping("{addressId}")

@ -19,7 +19,6 @@ import java.util.List;
@RequestMapping("diamond")
public class DiamondController extends BaseController {
@Autowired
private IDiamondService iDiamondService;

@ -15,11 +15,7 @@ import com.github.binarywang.wxpay.service.WxPayService;
import com.wayn.common.constant.SysConstants;
import com.wayn.common.core.domain.shop.*;
import com.wayn.common.core.domain.vo.OrderVO;
import com.wayn.common.core.service.shop.IAddressService;
import com.wayn.common.core.service.shop.IGoodsProductService;
import com.wayn.common.core.service.shop.IMemberService;
import com.wayn.common.core.service.shop.IOrderGoodsService;
import com.wayn.common.core.service.tool.IMailConfigService;
import com.wayn.common.core.service.shop.*;
import com.wayn.common.core.util.OrderHandleOption;
import com.wayn.common.core.util.OrderUtil;
import com.wayn.common.exception.BusinessException;
@ -39,7 +35,6 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -63,8 +58,6 @@ import java.util.*;
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
@Autowired
RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法
@Autowired
private RedisCache redisCache;
@Autowired
@ -80,11 +73,11 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
@Autowired
private IMemberService iMemberService;
@Autowired
private IMailConfigService mailConfigService;
@Autowired
private OrderMapper orderMapper;
@Autowired
private TaskService taskService;
@Autowired
private IMailService iMailService;
@Override
public R selectListPage(IPage<Order> page, Integer showType) {
@ -369,6 +362,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
}
log.info("处理腾讯支付平台的订单支付");
assert result != null;
log.info(result.getReturnMsg());
String orderSn = result.getOutTradeNo();
@ -402,7 +396,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
// 订单支付成功以后,会发送短信给用户,以及发送邮件给管理员
String email = iMemberService.getById(order.getUserId()).getEmail();
if (StringUtils.isNotBlank(email)) {
sendEmail("新订单通知", order.toString(), email, WaynConfig.getMobileUrl());
iMailService.sendEmail("新订单通知", order.toString(), email, WaynConfig.getMobileUrl());
}
// 删除redis中订单id
redisCache.deleteZsetObject("order_zset", order.getId());
@ -434,7 +428,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
// 订单支付成功以后,会发送短信给用户,以及发送邮件给管理员
String email = iMemberService.getById(order.getUserId()).getEmail();
if (StringUtils.isNotBlank(email)) {
sendEmail("新订单通知", order.toString(), email, WaynConfig.getMobileUrl());
iMailService.sendEmail("新订单通知", order.toString(), email, WaynConfig.getMobileUrl());
}
// 删除redis中订单id
@ -502,7 +496,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
String email = iMemberService.getById(order.getUserId()).getEmail();
if (StringUtils.isNotEmpty(email)) {
if (StringUtils.isNotBlank(email)) {
sendEmail("订单正在退款", order.toString(), email, WaynConfig.getMobileUrl());
iMailService.sendEmail("订单正在退款", order.toString(), email, WaynConfig.getMobileUrl());
}
}
@ -565,21 +559,4 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
}
return SysConstants.STRING_TRUE;
}
/**
*
*
* @param subject
* @param content
* @param tos
*/
private void sendEmail(String subject, String content, String tos, String notifyUrl) {
Map<String, Object> map = new HashMap<>();
map.put("subject", subject);
map.put("content", content);
map.put("tos", tos);
map.put("notifyUrl", notifyUrl);
// 异步发送邮件
rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
}
}

Loading…
Cancel
Save