package com.sky.ioc.service.notice.impl;

import com.alibaba.fastjson.JSONObject;
import com.sky.ioc.entity.domain.notice.Feedback;
import com.sky.ioc.entity.params.notice.FeedBackParam;
import com.sky.ioc.entity.params.notice.NoticeParam;
import com.sky.ioc.entity.result.system.LoginUserVo;
import com.sky.ioc.mapper.notice.FeedBackMapper;
import com.sky.ioc.service.notice.FeedBackService;
import com.sky.ioc.tool.JwtUtil;
import com.sky.ioc.tool.RedisUtil;
import com.sky.ioc.tool.ReturnMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class FeedBackServiceImpl implements FeedBackService {

    @Autowired
    FeedBackMapper feedBackMapper;

    @Autowired
    private RedisUtil redisUtil;


    @Override
    public ReturnMsg listPage(FeedBackParam feedBackParam) {
        long count = feedBackMapper.countPage(feedBackParam);
        List<Map<String, Object>> lists = feedBackMapper.listPage(feedBackParam);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",count);
        jsonObject.put("data",lists);
        return ReturnMsg.ok(jsonObject);
    }

    @Override
    public ReturnMsg readFeedBack(List<Integer> ids) {
        if(ids.size()==0||ids==null){
            return ReturnMsg.fail("参数错误");
        }else{
            feedBackMapper.updateStatusByIds(ids);
            return ReturnMsg.ok();
        }
    }

    @Override
    public ReturnMsg delFeedBack(List<Integer> ids) {
        if(ids.size()==0||ids==null){
            return ReturnMsg.fail("参数错误");
        }else{
            feedBackMapper.delByIds(ids);
            return ReturnMsg.ok();
        }
    }

    @Override
    public ReturnMsg getFeedBack(Integer id) {
        Feedback feedBack = feedBackMapper.selectById(id);
        return ReturnMsg.ok(feedBack);
    }

    @Override
    public ReturnMsg addFeedBack(Feedback feedback,String token) {
        if (StringUtils.hasText(token)) {
            String username = JwtUtil.parseJWT(token) ;
            Object o = redisUtil.get("login:" + username);
            if (!Objects.isNull(o)) {
                feedback.setCreator(((LoginUserVo) o).getUserName());
                //return joinPoint.proceed(joinPoint.getArgs());
            }
        }
        feedback.setCreateTime(new Date()+"");
        feedBackMapper.insert(feedback);
        return ReturnMsg.ok();
    }
}
