package com.fs.app.controller;

import com.fs.app.annotation.Login;
import com.fs.common.annotation.Log;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.enums.BusinessType;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.bean.BeanUtils;
import com.fs.course.domain.*;
import com.fs.course.mapper.FsUserCourseMapper;
import com.fs.course.param.*;
import com.fs.course.service.*;
import com.fs.course.vo.FsUserCourseCommentListUVO;
import com.fs.course.vo.FsUserCourseMyNoteListUVO;
import com.fs.course.vo.FsUserCourseNoteListUVO;
import com.fs.course.vo.FsUserCourseNoteUVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 课堂评论Controller
 *
 * @author fs
 * @date 2024-05-15
 */
@Api("课堂评论接口")
@RestController
@RequestMapping(value="/app/course/note/")
public class CourseNoteController extends AppBaseController
{
    @Autowired
    private IFsUserCourseNoteService courseNoteService;

    @Autowired
    private IFsUserCourseNoteLikeService courseNoteLikeService;

    @Autowired
    private IFsUserCourseService courseService;
    @Autowired
    private IFsUserCourseNoteSaveService courseNoteSaveService;

    @Login
    @ApiOperation("我的笔记列表")
    @GetMapping("/getMyCourseNoteList")
    public R getMyCourseNoteList(FsUserCourseNoteUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsUserCourseMyNoteListUVO> list1=  courseNoteService.selectFsUserCourseMyNoteListUVO(param);
        List<FsUserCourseMyNoteListUVO> list2 = courseNoteService.selectSaveNoteListUVO(param);
        List<FsUserCourseMyNoteListUVO> list = new ArrayList<>(list1);
        list.addAll(list2);
        PageInfo<FsUserCourseMyNoteListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @Login
    @ApiOperation("笔记详情")
    @GetMapping("/getMyCourseNoteByNoteId")
    public R getMyCourseNoteByNoteId(@RequestParam("noteId") Long noteId, HttpServletRequest request)
    {
        FsUserCourseNoteUVO noteUVO = courseNoteService.selectFsUserCourseNoteUVOByNoteId(noteId);
        return R.ok().put("data",noteUVO);
    }

    @Login
    @ApiOperation("课堂笔记列表")
    @GetMapping("/getCourseNoteList")
    public R getCourseNoteList(FsUserCourseNoteUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsUserCourseNoteListUVO> list=  courseNoteService.selectFsUserCourseNoteListUVO(param);
        PageInfo<FsUserCourseNoteListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @Login
    @ApiOperation("编辑笔记")
    @PostMapping("/editCourseNote")
    public R editCourseNote(@Validated @RequestBody FsUserCourseNoteEditParam param, HttpServletRequest request)
    {
        FsUserCourseNote note = new FsUserCourseNote();
        BeanUtils.copyProperties(param, note);
        int a = courseNoteService.updateFsUserCourseNote(note);
        if (a>0){
            return R.ok("编辑成功");
        }
        return R.error("保存失败，请稍后再试");
    }

    @Login
    @ApiOperation("添加笔记")
    @PostMapping("/addCourseNote")
    public R addCourseNote(@Validated @RequestBody FsUserCourseNoteAddParam param, HttpServletRequest request)
    {
        FsUserCourseNote note = new FsUserCourseNote();
        note.setUserId(Long.parseLong(getUserId()));
        BeanUtils.copyProperties(param, note);
        FsUserCourse course = courseService.selectFsUserCourseByCourseId(param.getCourseId());
        if (course!=null){
            note.setNoteCateId(course.getCateId());
        }
        note.setStatus(2);
        int a = courseNoteService.insertFsUserCourseNote(note);
        if (a>0){
            return R.ok("发布成功");
        }
        return R.error("发布失败");
    }

    @Login
    @ApiOperation("点赞")
    @PostMapping("/doLike")
    @Transactional
    @Synchronized
    public R doLike(@RequestBody FsUserCourseNoteLikeUParam param, HttpServletRequest request){
        R r=courseNoteLikeService.checkLike(param.getNoteId(),Long.parseLong(getUserId()));
        if(r.get("code").equals(200)){
            //取消点赞
            courseNoteLikeService.deleteLike(param.getNoteId(),Long.parseLong(getUserId()));
            courseNoteService.updateLikes(param.getNoteId(),-1);
            return R.ok("已取消喜欢");
        }
        else{
            //关注
            FsUserCourseNoteLike map=new FsUserCourseNoteLike();
            map.setNoteId(param.getNoteId());
            map.setUserId(Long.parseLong(getUserId()));
            map.setCreateTime(new Date());
            courseNoteLikeService.insertFsUserCourseNoteLike(map);
            courseNoteService.updateLikes(param.getNoteId(),1);
            return R.ok("已喜欢");
        }
    }

    @Login
    @ApiOperation("转存")
    @PostMapping("/save")
    @Transactional
    @Synchronized
    public R save(@RequestBody FsUserCourseNoteLikeUParam param, HttpServletRequest request){
        R r=courseNoteSaveService.checkSave(param.getNoteId(),Long.parseLong(getUserId()));
        if(r.get("code").equals(200)){
            courseNoteSaveService.deleteSave(param.getNoteId(),Long.parseLong(getUserId()));
            return R.ok("已取消转存");
        }
        else{
            FsUserCourseNoteSave map=new FsUserCourseNoteSave();
            map.setNoteId(param.getNoteId());
            map.setUserId(Long.parseLong(getUserId()));
            map.setCreateTime(new Date());
            courseNoteSaveService.insertFsUserCourseNoteSave(map);
            return R.ok("保存成功");
        }
    }

    @Login
    @ApiOperation("删除")
    @PostMapping("/delCourseNote")
    public R delCourseNote(@RequestBody FsUserCourseNoteLikeUParam param, HttpServletRequest request)
    {
        courseNoteService.deleteFsUserCourseNoteByNoteId(param.getNoteId());
        return R.ok();
    }
}
