admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/service/impl/order/msg/MsgOrderDetailServiceImpl.java
@@ -1,153 +1,172 @@
package com.yeshi.fanli.service.impl.order.msg;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.yeshi.fanli.dao.mybatis.msg.MsgOrderDetailMapper;
import com.yeshi.fanli.entity.bus.msg.MsgExtra;
import com.yeshi.fanli.entity.bus.msg.MsgOrderDetail;
import com.yeshi.fanli.entity.bus.msg.MsgOrderDetail.MsgTypeOrderTypeEnum;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.exception.msg.MsgOrderDetailException;
import com.yeshi.fanli.service.inter.msg.MsgExtraService;
import com.yeshi.fanli.service.inter.msg.UserMsgReadStateService;
import com.yeshi.fanli.service.inter.order.msg.MsgOrderDetailService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Service
public class MsgOrderDetailServiceImpl implements MsgOrderDetailService {
   @Resource
   private MsgOrderDetailMapper msgOrderDetailMapper;
   @Resource
   private UserMsgReadStateService userMsgReadStateService;
   @Resource
   private JedisPool jedisPool;
   @Resource
   private MsgExtraService msgExtraService;
   @Override
   public void addMsgOrderDetail(MsgOrderDetail detail, boolean needNotify) throws MsgOrderDetailException {
      if (detail == null)
         throw new MsgOrderDetailException(1, "消息为空");
      if (detail.getOrderId() == null || detail.getType() == null   || detail.getUser() == null
            || StringUtil.isNullOrEmpty(detail.getExtraInfo()))
         throw new MsgOrderDetailException(2, "消息不完整");
      if (detail.getType() == MsgTypeOrderTypeEnum.businessRunning) {
         MsgOrderDetail msgOrderDetail = msgOrderDetailMapper.getByUniqueKey(detail.getUniquekey());
         if (msgOrderDetail != null) {
            return;
         }
      }
      // 锁住订单号
      Jedis jedis = jedisPool.getResource();
      try {
         String key = "rs-order-" + detail.getOrderId();
         if (jedis.setnx(key, "1") > 0) {
            jedis.expire(key, 60);
            MsgOrderDetail old = msgOrderDetailMapper.selectByUidAndOrderId(detail.getUser().getId(),
                  detail.getOrderId());
            if (old == null) {
               detail.setCreateTime(new Date());
               detail.setUpdateTime(new Date());
               detail.setRead(false);
               msgOrderDetailMapper.insertSelective(detail);
               // 消息内容
               msgExtraService.addMsgExtra(detail.getId(), detail.getExtraInfo(), MsgExtra.MSG_TYPE_ORDER);
            } else {
               MsgOrderDetail update = new MsgOrderDetail();
               update.setId(old.getId());
               update.setUpdateTime(new Date());
               update.setState(detail.getState());
               update.setPayMoney(detail.getPayMoney());
               update.setHongBaoMoney(detail.getHongBaoMoney());
               update.setRead(false);
               update.setBeiZhu(detail.getBeiZhu());
               msgOrderDetailMapper.updateByPrimaryKeySelective(update);
               msgExtraService.addMsgExtra(update.getId(), detail.getExtraInfo(), MsgExtra.MSG_TYPE_ORDER);
            }
            if (needNotify)
               userMsgReadStateService.addOrderMsgUnReadCount(detail.getUser().getId(), 1);
            jedis.del(key);
         }
      } finally {
         jedis.close();
      }
   }
   @Override
   public List<MsgOrderDetail> listMsgOrderDetail(Long uid, int page) {
      return msgOrderDetailMapper.listByUid(uid, (page - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE);
   }
   @Override
   public long countMsgOrderDetail(Long uid) {
      return msgOrderDetailMapper.countByUid(uid);
   }
   @Override
   public void readMsgByUid(Long uid) {
      msgOrderDetailMapper.setMsgReadByUid(uid);
   }
   @Override
   public void updateMsgOrderDetail(MsgOrderDetail detail, boolean needNotify) throws MsgOrderDetailException {
      if (detail == null)
         throw new MsgOrderDetailException(1, "消息为空");
      if (detail.getOrderId() == null || detail.getState() == null || detail.getUser() == null)
         throw new MsgOrderDetailException(2, "消息不完整");
      MsgOrderDetail msg = msgOrderDetailMapper.selectByUidAndOrderId(detail.getUser().getId(), detail.getOrderId());
      if (msg == null)
         return;
      if (msg.getState() == CommonOrder.STATE_WQ)
         return;
      MsgOrderDetail update = new MsgOrderDetail();
      update.setId(msg.getId());
      update.setHongBaoMoney(detail.getHongBaoMoney());
      update.setPayMoney(detail.getPayMoney());
      if (detail.getState().intValue() != msg.getState()) {
         update.setState(detail.getState());
         // update.setUpdateTime(new Date());
         if (needNotify)
            userMsgReadStateService.addOrderMsgUnReadCount(detail.getUser().getId(), 1);
      }
      msgOrderDetailMapper.updateByPrimaryKeySelective(update);
   }
   @Override
   public List<MsgOrderDetail> listMsgOrderByOrderId(String orderId) {
      return msgOrderDetailMapper.listByOrderId(orderId);
   }
   @Override
   public MsgOrderDetail selectByPrimaryKey(Long id) {
      return msgOrderDetailMapper.selectByPrimaryKey(id);
   }
   @Override
   public void deleteByPrimaryKey(Long id) {
      msgOrderDetailMapper.deleteByPrimaryKey(id);
      msgExtraService.deleteByPidAndType(id, MsgExtra.MSG_TYPE_ORDER);
   }
}
package com.yeshi.fanli.service.impl.order.msg;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import com.yeshi.fanli.service.inter.msg.MsgOverViewsService;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.factory.msg.MsgOverViewsFactory;
import org.springframework.stereotype.Service;
import com.yeshi.fanli.dao.mybatis.msg.MsgOrderDetailMapper;
import com.yeshi.fanli.entity.bus.msg.MsgExtra;
import com.yeshi.fanli.entity.bus.msg.MsgOrderDetail;
import com.yeshi.fanli.entity.bus.msg.MsgOrderDetail.MsgTypeOrderTypeEnum;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.exception.msg.MsgOrderDetailException;
import com.yeshi.fanli.service.inter.msg.MsgExtraService;
import com.yeshi.fanli.service.inter.msg.UserMsgReadStateService;
import com.yeshi.fanli.service.inter.order.msg.MsgOrderDetailService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Service
public class MsgOrderDetailServiceImpl implements MsgOrderDetailService {
    @Resource
    private MsgOrderDetailMapper msgOrderDetailMapper;
    @Resource
    private UserMsgReadStateService userMsgReadStateService;
    @Resource
    private RedisManager redisManager;
    @Resource
    private MsgExtraService msgExtraService;
    @Resource
    private MsgOverViewsService msgOverViewsService;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addMsgOrderDetail(MsgOrderDetail detail, boolean needNotify) throws MsgOrderDetailException {
        if (detail == null)
            throw new MsgOrderDetailException(1, "消息为空");
        if (detail.getOrderId() == null || detail.getType() == null || detail.getUser() == null
                || StringUtil.isNullOrEmpty(detail.getExtraInfo()))
            throw new MsgOrderDetailException(2, "消息不完整");
        if (detail.getType() == MsgTypeOrderTypeEnum.businessRunning) {
            MsgOrderDetail msgOrderDetail = msgOrderDetailMapper.getByUniqueKey(detail.getUniquekey());
            if (msgOrderDetail != null) {
                return;
            }
        }
        // 锁住订单号
        Jedis jedis = redisManager.getJedis();
        try {
            String key = "rs-order-" + detail.getOrderId();
            if (jedis.setnx(key, "1") > 0) {
                jedis.expire(key, 60);
                MsgOrderDetail old = msgOrderDetailMapper.selectByUidAndOrderId(detail.getUser().getId(),
                        detail.getOrderId());
                if (old == null) {
                    detail.setCreateTime(new Date());
                    detail.setUpdateTime(new Date());
                    detail.setRead(false);
                    msgOrderDetailMapper.insertSelective(detail);
                    //加入消息索引
                    msgOverViewsService.save(MsgOverViewsFactory.create(detail));
                    // 消息内容
                    msgExtraService.addMsgExtra(detail.getId(), detail.getExtraInfo(), MsgExtra.MSG_TYPE_ORDER);
                } else {
                    MsgOrderDetail update = new MsgOrderDetail();
                    update.setId(old.getId());
                    update.setUpdateTime(new Date());
                    update.setState(detail.getState());
                    update.setPayMoney(detail.getPayMoney());
                    update.setHongBaoMoney(detail.getHongBaoMoney());
                    update.setRead(false);
                    update.setBeiZhu(detail.getBeiZhu());
                    msgOrderDetailMapper.updateByPrimaryKeySelective(update);
                    //更新消息索引时间
                    update.setUser(old.getUser());
                    msgOverViewsService.updateTime(MsgOverViewsFactory.create(update).getId(), update.getUpdateTime());
                    msgExtraService.addMsgExtra(update.getId(), detail.getExtraInfo(), MsgExtra.MSG_TYPE_ORDER);
                }
                if (needNotify)
                    userMsgReadStateService.addOrderMsgUnReadCount(detail.getUser().getId(), 1);
                jedis.del(key);
            }
        } finally {
            jedis.close();
        }
    }
    @Override
    public List<MsgOrderDetail> listMsgOrderDetail(Long uid, int page) {
        return msgOrderDetailMapper.listByUid(uid, (page - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE);
    }
    @Override
    public List<MsgOrderDetail> listDetail(List<Long> ids) {
        return msgOrderDetailMapper.listByPrimaryKeys(ids);
    }
    @Override
    public long countMsgOrderDetail(Long uid) {
        return msgOrderDetailMapper.countByUid(uid);
    }
    @Override
    public void readMsgByUid(Long uid) {
        msgOrderDetailMapper.setMsgReadByUid(uid);
    }
    @Override
    public void updateMsgOrderDetail(MsgOrderDetail detail, boolean needNotify) throws MsgOrderDetailException {
        if (detail == null)
            throw new MsgOrderDetailException(1, "消息为空");
        if (detail.getOrderId() == null || detail.getState() == null || detail.getUser() == null)
            throw new MsgOrderDetailException(2, "消息不完整");
        MsgOrderDetail msg = msgOrderDetailMapper.selectByUidAndOrderId(detail.getUser().getId(), detail.getOrderId());
        if (msg == null)
            return;
        if (msg.getState() == CommonOrder.STATE_WQ)
            return;
        MsgOrderDetail update = new MsgOrderDetail();
        update.setId(msg.getId());
        update.setHongBaoMoney(detail.getHongBaoMoney());
        update.setPayMoney(detail.getPayMoney());
        if (detail.getState().intValue() != msg.getState()) {
            update.setState(detail.getState());
            // update.setUpdateTime(new Date());
            if (needNotify)
                userMsgReadStateService.addOrderMsgUnReadCount(detail.getUser().getId(), 1);
        }
        msgOrderDetailMapper.updateByPrimaryKeySelective(update);
    }
    @Override
    public List<MsgOrderDetail> listMsgOrderByOrderId(String orderId) {
        return msgOrderDetailMapper.listByOrderId(orderId);
    }
    @Override
    public MsgOrderDetail selectByPrimaryKey(Long id) {
        return msgOrderDetailMapper.selectByPrimaryKey(id);
    }
    @Override
    public void deleteByPrimaryKey(Long id) {
        msgOrderDetailMapper.deleteByPrimaryKey(id);
        msgExtraService.deleteByPidAndType(id, MsgExtra.MSG_TYPE_ORDER);
    }
}