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;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 浏览记录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) {
        ArrayList<BrowseRecordCustomerInfoVo> infoVos = new ArrayList<>();
        String str = Constants.BROWSE_RECORD_KEY + userInfo.getId();
        //获取以key前缀所有的key
        Set keys = redisTemplate.keys(str + "*");
        if (!keys.isEmpty()){
            for (Object key : keys) {
                String jsonStr = (String) redisTemplate.opsForValue().get(key);
                BrowseRecordCustomerInfoVo customerInfoVo = JSONObject.parseObject(jsonStr).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.hasKey(Constants.BROWSE_RECORD_KEY + userInfo.getId())){
            String jsonStr = (String) redisTemplate.opsForValue().get(Constants.BROWSE_RECORD_KEY + userInfo.getId() + ":" + id);
            BrowseRecordCustomerInfoVo customerInfoVo = JSONObject.parseObject(jsonStr).toJavaObject(BrowseRecordCustomerInfoVo.class);
            customerInfoVo.setBrowseTime(new Date());
            redisTemplate.opsForValue().set(Constants.BROWSE_RECORD_KEY + userInfo.getId() + ":" + id,JSON.toJSONString(customerInfoVo),30, TimeUnit.DAYS);
        }else {
            //商家详情
            BrowseRecordCustomerInfoVo customerDetail = customerInfoMapper.getCustomerDetailsById(id);
            customerDetail.setBrowseTime(new Date());
            //添加缓存
            redisTemplate.opsForValue().set(Constants.BROWSE_RECORD_KEY + userInfo.getId() + ":" + id,JSON.toJSONString(customerDetail),30, TimeUnit.DAYS);
        }
        return Response.success();
    }

    /**
     * 删除浏览记录
     * @param userInfo 当前用户
     * @param ids 商家id
     * @return
     */
    @Override
    public Response removeBrowseRecord(UserInfo userInfo, Long[] ids) {
        try {
            for (Long id : ids) {
                //删除浏览记录缓存
                redisTemplate.delete(Constants.BROWSE_RECORD_KEY + userInfo.getId() + ":"+id);
            }
            return Response.success();
        }catch (Exception e){
            throw new CustomException("系统异常");
        }
    }


}
