package com.wzj.soopin.member.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.therapi.runtimejavadoc.repack.com.eclipsesource.json.JsonObject;
import com.wzj.soopin.member.convert.FeedbackConvert;
import com.wzj.soopin.member.convert.MemberConvert;
import com.wzj.soopin.member.domain.po.Feedback;
import com.wzj.soopin.member.domain.bo.FeedbackBO;
import com.wzj.soopin.member.domain.po.Member;
import com.wzj.soopin.member.domain.vo.FeedbackVO;
import com.wzj.soopin.member.mapper.FeedbackMapper;
import com.wzj.soopin.member.mapper.MemberMapper;
import com.wzj.soopin.member.service.IFeedbackService;
import com.wzj.soopin.member.util.MemberCacheHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.mq.domain.MQMessage;
import org.dromara.common.mq.enums.MessageActionEnum;
import org.dromara.common.mq.utils.MqUtil;
import org.dromara.common.redis.redis.RedisCache;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 意见反馈Service业务层处理
 *
 * @author zcc
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements IFeedbackService {


    private final MemberMapper memberMapper;

    private final MemberConvert memberConvert;

    private final FeedbackConvert feedbackConvert;

    private final MemberCacheHelper memberCacheHelper;


    /**
     * 查询意见反馈列表
     *
     * @param query 查询条件
     * @param page  分页条件
     * @return 意见反馈
     */
    public IPage<Feedback> selectList(FeedbackBO query, IPage page) {

        return baseMapper.selectPage(page, query.toWrapper());
    }


    @Override
    public FeedbackVO selectById(Serializable id) {
        Feedback feedback = super.getById(id);

        if (feedback == null) {
            return null;
        }
        FeedbackVO vo = feedbackConvert.toVO(feedback);
        //判断是投诉对象是什么类型
        if (feedback.getAimType() != null) {
            //        如果是人员，则取人员信息
//            0 会员 1 群组 2 评论 3 视频  4 聊天
            switch (feedback.getAimType()) {
                case 0:
                    Member member = memberMapper.selectById(feedback.getAimId());
                    break;
            }
//            如果是订单，则取订单信息
//            如果是视频，则取视频信息
        }


        //从redis中获取用户信息
        Member memberInfo = memberCacheHelper.getMemberInfo(feedback.getMemberId());
        vo.setMember(memberConvert.toVO(memberInfo));
        return vo;
    }

    public void handle(Feedback feedback) {
        LambdaUpdateWrapper<Feedback> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Feedback::getRemark, feedback.getRemark())
            .set(Feedback::getStatus, feedback.getStatus())
            .set(Feedback::getResultContent, feedback.getResultContent())
            .eq(Feedback::getId, feedback.getId());
        baseMapper.update(null, updateWrapper);

        MqUtil.sendIMMessage(buildMessage(feedback));
    }

    private MQMessage buildMessage(Feedback aim){

        Map<String,Object> params=new HashMap<>();
        LoginUser loginUser= LoginHelper.getLoginUser();
        params.put("userId", loginUser.getUserId());
        params.put("nickname", loginUser.getNickname() == null ? "" : loginUser.getNickname());
        params.put("faceUrl", loginUser.getAvatar()  );
        params.put("action", MessageActionEnum.INTERACTION_LIKE.getAccount());
        params.put("toUserId",aim.getMemberId());

        MQMessage mqMessage = new MQMessage();
        mqMessage.setTag(MessageActionEnum.INTERACTION_LIKE.getAccount());
        mqMessage.setData(params);
        return mqMessage;
    }

    public Integer changeStatus(Feedback dto) {
        UpdateWrapper<Feedback> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", dto.getId());
        wrapper.set("handle_status", dto.getStatus())
            .set("handle_time", LocalDateTime.now());
        return baseMapper.update(null, wrapper);
    }
}
