|
@@ -0,0 +1,106 @@
|
|
|
|
+package com.miaxis.record.service.impl;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.miaxis.common.constant.Constants;
|
|
|
|
+import com.miaxis.common.core.domain.Response;
|
|
|
|
+import com.miaxis.common.core.domain.entity.UserInfo;
|
|
|
|
+import com.miaxis.common.core.redis.RedisCache;
|
|
|
|
+import com.miaxis.common.exception.CustomException;
|
|
|
|
+import com.miaxis.customer.mapper.CustomerInfoMapper;
|
|
|
|
+import com.miaxis.customer.vo.BrowseRecordCustomerInfoVo;
|
|
|
|
+import com.miaxis.record.domain.BrowseRecordInfo;
|
|
|
|
+import com.miaxis.record.mapper.BrowseRecordInfoMapper;
|
|
|
|
+import com.miaxis.record.service.IBrowseRecordInfoService;
|
|
|
|
+import com.miaxis.record.vo.BrowseRecordCustomerVo;
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
+import lombok.SneakyThrows;
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 浏览记录Service业务层处理
|
|
|
|
+ *
|
|
|
|
+ * @author miaxis
|
|
|
|
+ * @date 2021-03-23
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+@RequiredArgsConstructor
|
|
|
|
+public class BrowseRecordInfoServiceImpl extends ServiceImpl<BrowseRecordInfoMapper, BrowseRecordInfo> implements IBrowseRecordInfoService {
|
|
|
|
+
|
|
|
|
+ private final BrowseRecordInfoMapper browseRecordInfoMapper;
|
|
|
|
+
|
|
|
|
+ private final CustomerInfoMapper customerInfoMapper;
|
|
|
|
+
|
|
|
|
+ private final RedisTemplate redisTemplate;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 查询浏览记录列表
|
|
|
|
+ * @param userInfo
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public List<BrowseRecordCustomerInfoVo> getBrowseRecordInfoList(UserInfo userInfo) {
|
|
|
|
+ //从缓存获取
|
|
|
|
+ List<String> values = redisTemplate.opsForHash().values(Constants.BROWSE_RECORD_KEY + userInfo.getId());
|
|
|
|
+ ArrayList<BrowseRecordCustomerInfoVo> infoVos = new ArrayList<>();
|
|
|
|
+ for (String value : values) {
|
|
|
|
+ BrowseRecordCustomerInfoVo customerInfoVo = JSONObject.parseObject(value).toJavaObject(BrowseRecordCustomerInfoVo.class);
|
|
|
|
+ infoVos.add(customerInfoVo);
|
|
|
|
+ }
|
|
|
|
+ return infoVos;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 新增浏览记录
|
|
|
|
+ * @param userInfo 当前用户
|
|
|
|
+ * @param id 商家id
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ @SneakyThrows
|
|
|
|
+ public Response insertBrowseRecord(UserInfo userInfo, Long id) {
|
|
|
|
+
|
|
|
|
+ //判断用户是否有浏览此商家记录
|
|
|
|
+ if (redisTemplate.opsForHash().hasKey(Constants.BROWSE_RECORD_KEY +userInfo.getId(),id)){
|
|
|
|
+ String jsonStr = (String) redisTemplate.opsForHash().get(Constants.BROWSE_RECORD_KEY + userInfo.getId(), id);
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(jsonStr);
|
|
|
|
+ BrowseRecordCustomerInfoVo customerInfoVo = jsonObject.toJavaObject(BrowseRecordCustomerInfoVo.class);
|
|
|
|
+ customerInfoVo.setBrowseTime(new Date());
|
|
|
|
+ redisTemplate.opsForHash().put(Constants.BROWSE_RECORD_KEY+userInfo.getId(),id,JSON.toJSONString(customerInfoVo));
|
|
|
|
+ }else {
|
|
|
|
+ //商家详情
|
|
|
|
+ BrowseRecordCustomerInfoVo customerDetail = customerInfoMapper.getCustomerDetailsById(id);
|
|
|
|
+ customerDetail.setBrowseTime(new Date());
|
|
|
|
+ //添加缓存
|
|
|
|
+ redisTemplate.opsForHash().put(Constants.BROWSE_RECORD_KEY+userInfo.getId(),id,JSON.toJSONString(customerDetail));
|
|
|
|
+ }
|
|
|
|
+ return Response.success();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 删除浏览记录
|
|
|
|
+ * @param userInfo 当前用户
|
|
|
|
+ * @param ids 商家id
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public Response removeBrowseRecord(UserInfo userInfo, Long[] ids) {
|
|
|
|
+ try {
|
|
|
|
+ //删除浏览记录缓存
|
|
|
|
+ redisTemplate.opsForHash().delete(Constants.BROWSE_RECORD_KEY + userInfo.getId(), ids);
|
|
|
|
+ return Response.success();
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ throw new CustomException("系统异常");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+}
|