|
|
@@ -18,6 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import shop.alien.entity.SecondVideoTask;
|
|
|
import shop.alien.entity.second.*;
|
|
|
@@ -634,27 +635,40 @@ public class SecondGoodsServiceImpl extends ServiceImpl<SecondGoodsMapper, Secon
|
|
|
* 创建商品审核记录
|
|
|
* @param goods 商品信息
|
|
|
*/
|
|
|
+// @Transactional(rollbackFor = Exception.class)
|
|
|
private void approveAndListGoods(SecondGoods goods) {
|
|
|
- // 如果所有审核都通过,设置为上架状态
|
|
|
- goods.setGoodsStatus(SecondGoodsStatusEnum.LISTED.getCode()); // 上架
|
|
|
- goods.setFailedReason("");
|
|
|
- goods.setReleaseTime(new Date()); // 上架时间
|
|
|
- updateById(goods);
|
|
|
- // 插入审核记录
|
|
|
- createGoodsAudit(goods, "", Constants.AuditStatus.PASSED);
|
|
|
- // 发送审核成功消息
|
|
|
- sendMessage(goods);
|
|
|
- // 上架 记录商品操作历史
|
|
|
- String operationName = "";
|
|
|
- QueryWrapper<SecondGoodsRecord> queryWrapper = new QueryWrapper<>();
|
|
|
- queryWrapper.eq("goods_id", goods.getId());
|
|
|
- List<SecondGoodsRecord> recordList = secondGoodsRecordMapper.selectList(queryWrapper);
|
|
|
- if (CollectionUtil.isNotEmpty(recordList)){
|
|
|
- operationName = "重新发布";
|
|
|
- }else {
|
|
|
- operationName = "首次发布";
|
|
|
+ boolean isNotified = false;
|
|
|
+ try {
|
|
|
+ // 如果所有审核都通过,设置为上架状态
|
|
|
+ goods.setGoodsStatus(SecondGoodsStatusEnum.LISTED.getCode()); // 上架
|
|
|
+ goods.setFailedReason("");
|
|
|
+ goods.setReleaseTime(new Date()); // 上架时间
|
|
|
+ updateById(goods);
|
|
|
+ // 插入审核记录
|
|
|
+ createGoodsAudit(goods, "", Constants.AuditStatus.PASSED);
|
|
|
+ // 发送审核成功消息
|
|
|
+ sendMessage(goods);
|
|
|
+ isNotified = true; // 标记通知已发送
|
|
|
+ // 上架 记录商品操作历史
|
|
|
+ String operationName = "";
|
|
|
+ QueryWrapper<SecondGoodsRecord> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("goods_id", goods.getId());
|
|
|
+ List<SecondGoodsRecord> recordList = secondGoodsRecordMapper.selectList(queryWrapper);
|
|
|
+ if (CollectionUtil.isNotEmpty(recordList)){
|
|
|
+ operationName = "重新发布";
|
|
|
+ }else {
|
|
|
+ operationName = "首次发布";
|
|
|
+ }
|
|
|
+ recordGoodsOperation(goods, operationName);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("商品上架过程中发生异常,执行回滚", e);
|
|
|
+ // 如果通知已发送但后续操作失败,需要补偿
|
|
|
+ if (isNotified) {
|
|
|
+ // 发送补偿消息,比如撤销通知或标记为异常状态
|
|
|
+// sendCompensationMessage(goods);
|
|
|
+ }
|
|
|
+ throw e;
|
|
|
}
|
|
|
- recordGoodsOperation(goods, operationName);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -1765,4 +1779,73 @@ public class SecondGoodsServiceImpl extends ServiceImpl<SecondGoodsMapper, Secon
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下架商品
|
|
|
+ * @param secondGoods 商品信息
|
|
|
+ * @return 是否下架成功
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public boolean shelveSecondGoods(SecondGoodsVo secondGoods) {
|
|
|
+ log.info("SecondGoodsServiceImpl.shelveSecondGoods?secondGoods={}", secondGoods.toString());
|
|
|
+ // 修改商品状态为4 - 已下架
|
|
|
+ secondGoods.setGoodsStatus(4);
|
|
|
+ boolean updateResult = updateById(secondGoods);
|
|
|
+ if (updateResult) {
|
|
|
+ // 获取最新的商品信息并记录操作历史
|
|
|
+ SecondGoods updatedGoods = getById(secondGoods.getId());
|
|
|
+ recordGoodsOperation(updatedGoods, "下架商品");
|
|
|
+ // 发送系统通知
|
|
|
+ sendShelveMessage(updatedGoods);
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送商品下架消息
|
|
|
+ * @param goods 商品信息
|
|
|
+ */
|
|
|
+ private void sendShelveMessage(SecondGoods goods) {
|
|
|
+ try {
|
|
|
+ // 根据 goods.getUserId() 获取用户信息
|
|
|
+ LifeUser lifeUser = lifeUserMapper.selectById(goods.getUserId());
|
|
|
+ String phone = lifeUser.getUserPhone();
|
|
|
+ // 调取feign接口 发送消息 life_notice表
|
|
|
+ LifeNotice lifeNotice = new LifeNotice();
|
|
|
+ lifeNotice.setSenderId("system");
|
|
|
+ lifeNotice.setReceiverId("user_"+ phone);
|
|
|
+ lifeNotice.setBusinessId(goods.getId());
|
|
|
+ lifeNotice.setTitle("商品下架通知");
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("goodsId", goods.getId());
|
|
|
+// jsonObject.put("status", "true");
|
|
|
+ jsonObject.put("message", "您的商品:"+ goods.getTitle() + "已下架");
|
|
|
+ lifeNotice.setContext(jsonObject.toJSONString());
|
|
|
+ lifeNotice.setNoticeType(Constants.Notice.SYSTEM_NOTICE); // 系统通知
|
|
|
+ lifeNotice.setIsRead(0);
|
|
|
+ lifeNoticeMapper.insert(lifeNotice);
|
|
|
+ sendShelveNotice("user_"+ phone, lifeNotice);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("发送消息通知失败,goods: {}", goods, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void sendShelveNotice(String receiverId, LifeNotice lifeNotice) {
|
|
|
+ try {
|
|
|
+ WebSocketVo webSocketVo = new WebSocketVo();
|
|
|
+ webSocketVo.setSenderId("system");
|
|
|
+ webSocketVo.setReceiverId(receiverId);
|
|
|
+ webSocketVo.setCategory("notice");
|
|
|
+ webSocketVo.setNoticeType("1");
|
|
|
+ webSocketVo.setIsRead(0);
|
|
|
+ webSocketVo.setText(JSONObject.from(lifeNotice).toJSONString());
|
|
|
+ alienStoreFeign.sendMsgToClientByPhoneId(receiverId, JSONObject.from(webSocketVo).toJSONString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("发送消息通知失败,receiverId: {}", receiverId, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|