package com.wzj.soopin.member.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.member.convert.FeedbackConvert;
import com.wzj.soopin.member.convert.MemberConvert;
import com.wzj.soopin.member.domain.bo.FeedbackBO;
import com.wzj.soopin.member.domain.po.Feedback;
import com.wzj.soopin.member.domain.po.Member;
import com.wzj.soopin.member.domain.vo.FeedbackVO;
import com.wzj.soopin.member.service.IFeedbackService;
import com.wzj.soopin.member.service.IMemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.constant.CacheConstants;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.redis.utils.RedisUtils;
import org.springframework.web.bind.annotation.*;

import java.util.stream.Collectors;

/**
 * 意见反馈Controller
 *
 * @author zcc
 * @date 2024-02-26
 */
@Tag(name = "意见反馈接口列表")
@RestController
@RequestMapping("/ums/feedback")
@RequiredArgsConstructor
public class FeedbackController {

    private final IFeedbackService service;
    private final FeedbackConvert convert;
    private final MemberConvert memberConvert;

    private final IMemberService memberService;

//    private final VlogService vlogService;


    @Operation(summary = "查询意见反馈列表")
    @PostMapping("/page")
    public R<IPage<FeedbackVO>> list(@RequestBody FeedbackBO query, @RequestBody Page page) {
        Page<Feedback> feedBackPage = service.page(page, query.toWrapper());
        Page<FeedbackVO> feedbackVOList = convert.toVO(feedBackPage);

        //从redis中获取用户信息和投诉对象信息

        feedbackVOList.setRecords(feedBackPage.getRecords().stream().map(feedback -> {

            //构建用户信息
            FeedbackVO feedbackVO = convert.toVO(feedback);
            Member member = memberService.getById(feedback.getMemberId());
            feedbackVO.setMember(memberConvert.toVO(member));

            //构建对象信息
            switch (feedback.getAimType()) {
                case 0://会员
                    feedbackVO.setAimObject(memberService.getById(feedback.getAimId()));
                    break;
                case 1://群组
                    feedbackVO.setAimObject(RedisUtils.getCacheObject(CacheConstants.GROUP + feedback.getAimId()));
                    break;
                case 2://评论
                    feedbackVO.setAimObject(RedisUtils.getCacheObject(
                        CacheConstants.COMMENT + feedback.getAimId()
                    ));
                    break;
                case 3://视频
                    feedbackVO.setAimObject(RedisUtils.getCacheObject(CacheConstants.VLOG + feedback.getAimId()));
                    break;
                case 4://聊天
                    feedbackVO.setAimObject(RedisUtils.getCacheObject(
                        CacheConstants.CHAT + feedback.getAimId()
                    ));
                    break;

            }


            return feedbackVO;

        }).collect(Collectors.toList()));


        return R.ok(feedbackVOList);
    }


    @Operation(summary = "修改意见反馈备注信息")
    @Log(title = "意见反馈", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public R editMark(@RequestBody FeedbackBO feedback) {
        return R.ok(service.save(convert.toPo(feedback)));
    }

    @Operation(summary = "新增意见反馈")
    @Log(title = "新增意见反馈", businessType = BusinessType.UPDATE)
    @PostMapping("/add")
    public R add(@RequestBody FeedbackBO feedback) {
        return R.ok(service.save(convert.toPo(feedback)));
    }

    @Operation(summary = "获取详细信息")
    @GetMapping(value = "/{id}")
    public R<FeedbackVO> getInfo(@PathVariable("id") Long id) {
        return R.ok(service.selectById(id));
    }

    @Operation(summary = "处理意见反馈")
    @Log(title = "意见反馈", businessType = BusinessType.UPDATE)
    @PostMapping("/handle")
    public R changeStatus(@RequestBody FeedbackBO bo) {
        service.handle(convert.toPo(bo));
        return R.ok();
    }


    @Operation(summary = "删除意见反馈")
    @Log(title = "意见反馈", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Object> remove(@PathVariable Long id) {
        return R.ok(service.removeById(id));
    }
}
