package com.jxy.marketall.service.impl;

import com.jxy.common.core.text.Convert;
import com.jxy.common.core.web.Result.AjaxResult;
import com.jxy.common.security.utils.SecurityUtils;
import com.jxy.marketall.dto.SubscriptionContentDto;
import com.jxy.marketall.mapper.MsgSubMapper;
import com.jxy.marketall.service.MsgSubService;
import com.jxy.marketall.vo.IdVo;
import com.jxy.marketall.vo.PageVo;
import com.jxy.marketall.vo.SubscriptionContentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 指标订阅业务处理
 * @Date 2022/6/2 9:37
 * @Author 杜懿
 */
@Service("msgSubService")
public class MsgSubServiceImpl implements MsgSubService {

    @Autowired
    private MsgSubMapper msgSubMapper;


    @Override
    public AjaxResult subscriptionMsg(SubscriptionContentVo vo) {
        Long userId = SecurityUtils.getUserId();
        if(userId  <= 0){
            System.out.println("userId有误："+userId+"，loginUser"+SecurityUtils.getLoginUser());
            return AjaxResult.error("获取用户登录信息错误");
        }
        String email = SecurityUtils.getLoginUser().getSysUser().getEmail();
        try {
            //判断是否已有重复数据 有则先删除再新增
            long id = Convert.toLong(msgSubMapper.checkSubIfExist(userId, vo),0L);
            if(id > 0){
                System.out.println("已有数据、执行删除，id："+id);
                msgSubMapper.cancelSubscription(id);
            }

            if(msgSubMapper.subscripition(userId,email , vo)){
                return AjaxResult.success("订阅成功");
            }else {
                return AjaxResult.error("系统异常，订阅失败");
            }

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("数据库异常");
        }

    }

    @Override
    public AjaxResult cancelSubscription(IdVo vo){
        Long userId = SecurityUtils.getUserId();
        if(userId  <= 0){
            System.out.println("userId有误："+userId+"，loginUser"+SecurityUtils.getLoginUser());
            return AjaxResult.error("获取用户登录信息错误");
        }
        if(vo.getId()  <= 0){
            System.out.println("收到id："+vo);
            return AjaxResult.error("参数错误");
        }
        if(!msgSubMapper.checkSubByUserIdAndSubId(userId,vo.getId())){
            //根据用户id和数据id未查询到数据
            return AjaxResult.error("参数错误");
        }

        try {
            if(msgSubMapper.cancelSubscription(vo.getId())){
                return AjaxResult.success("已取消订阅该指标");
            }else {
                return AjaxResult.error("数据库异常");
            }
        }catch (Exception e){
            return AjaxResult.error("数据库异常");
        }

    }

    @Override
    public List<SubscriptionContentDto> getUserSubscriptList(PageVo vo) {
        System.out.println("vo"+vo);
        Long userId = SecurityUtils.getUserId();
        if(userId  <= 0){
            System.out.println("userId有误："+userId+"，loginUser"+SecurityUtils.getLoginUser());
            return new ArrayList<>();
        }
        List<SubscriptionContentDto> list = msgSubMapper.getSubscriptionListByUserId(userId);
        return list;
    }

    @Override
    public List<SubscriptionContentDto> getSubListByChannel(String channel) {
        List<SubscriptionContentDto> list = msgSubMapper.getSubListByChannel(channel);
        return list;
    }
}
