package com.playlet.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.playlet.common.core.domain.entity.SysUser;
import com.playlet.system.domain.PlayletPublicAccount;
import com.playlet.system.domain.PlayletPublicDetail;
import com.playlet.system.domain.PublicCommentResponse;
import com.playlet.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.playlet.common.annotation.Log;
import com.playlet.common.enums.BusinessType;
import com.playlet.system.domain.PublicDetailComment;
import com.playlet.common.core.controller.BaseController;
import com.playlet.common.core.domain.AjaxResult;
import com.playlet.common.utils.poi.ExcelUtil;
import com.playlet.common.core.page.TableDataInfo;

/**
 * 文章评论Controller
 *
 * @author ruoyi
 * @date 2024-06-18
 */
@Controller
@RequestMapping("/system/public/comment")
public class PublicDetailCommentController extends BaseController
{
    private String prefix = "system/public/comment";

    @Autowired
    private IPublicDetailCommentService publicDetailCommentService;
    @Autowired
    private IPlayletPublicDetailService publicDetailService;
    @Autowired
    private IPublicCommentResponseService publicCommentResponseService;
    @Autowired
    private IPlayletPublicAccountService playletPublicAccountService;

    @RequiresPermissions("public:comment:view")
    @GetMapping()
    public String comment()
    {
        return prefix + "/comment";
    }

    /**
     * 查询文章评论列表
     */
    @RequiresPermissions("public:comment:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PublicDetailComment publicDetailComment)
    {
        if(!this.getSysUser().isAdmin()){
            // 公众号
            List<PlayletPublicAccount> accounts = playletPublicAccountService.lambdaQuery().select(PlayletPublicAccount::getId)
                    .like(PlayletPublicAccount::getManagerIds, this.getUserId())
                    .list();
            if(CollectionUtil.isEmpty(accounts)){
                return getDataTable(new ArrayList<>());
            }
            // 公众号列表
            List<Long> ids = accounts.stream().map(PlayletPublicAccount::getId).collect(Collectors.toList());
            // 文章列表
            List<PlayletPublicDetail> publicDetails = publicDetailService.lambdaQuery().select(PlayletPublicDetail::getId)
                    .in(PlayletPublicDetail::getPublicId, ids).list();
            //
            if(CollectionUtil.isEmpty(publicDetails)){
                return getDataTable(new ArrayList<>());
            }
            List<Long> detailIds =  publicDetails.stream().map(PlayletPublicDetail::getId).collect(Collectors.toList());
            publicDetailComment.setDetailIds(detailIds);
        }
        startPage();
        List<PublicDetailComment> list = publicDetailCommentService.selectPublicDetailCommentList(publicDetailComment);
        list.forEach(model->{
            PlayletPublicDetail playletPublicDetail = publicDetailService.selectPlayletPublicDetailById(model.getDetailId());
            if(playletPublicDetail != null){
                model.setDetailName(playletPublicDetail.getTitle());
                model.setAuthorAlias(playletPublicDetail.getAuthorAlias());
            }else {
                model.setDetailName("已删除文章");
            }
            PublicCommentResponse publicCommentResponse = publicCommentResponseService.lambdaQuery()
                    .eq(PublicCommentResponse::getCommentId, model.getId()).one();
            if(publicCommentResponse != null){
                model.setCommentResponseContent(publicCommentResponse.getResponseContent());
                model.setIsResponse("02");
            }else {
                model.setIsResponse("01");
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出文章评论列表
     */
    @RequiresPermissions("public:comment:export")
    @Log(title = "文章评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PublicDetailComment publicDetailComment)
    {
        List<PublicDetailComment> list = publicDetailCommentService.selectPublicDetailCommentList(publicDetailComment);
        ExcelUtil<PublicDetailComment> util = new ExcelUtil<PublicDetailComment>(PublicDetailComment.class);
        return util.exportExcel(list, "文章评论数据");
    }

    /**
     * 新增文章评论
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存文章评论
     */
    @RequiresPermissions("public:comment:add")
    @Log(title = "文章评论", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PublicDetailComment publicDetailComment)
    {
        return toAjax(publicDetailCommentService.insertPublicDetailComment(publicDetailComment));
    }

    /**
     * 修改文章评论
     */
    @GetMapping("/responseComment/{id}")
    public String responseComment(@PathVariable("id") Long id, ModelMap mmap)
    {
        PublicDetailComment publicDetailComment = publicDetailCommentService.selectPublicDetailCommentById(id);
        PublicCommentResponse publicCommentResponse = publicCommentResponseService.lambdaQuery()
                .eq(PublicCommentResponse::getCommentId, id).one();
        if(publicCommentResponse == null){
            publicCommentResponse = new PublicCommentResponse();
            publicCommentResponse.setResponseContent("");
        }
        mmap.put("comment", publicDetailComment);
        mmap.put("commentResponse", publicCommentResponse);
        return prefix + "/responseComment";
    }

    /**
     * 修改保存文章评论
     */
    @RequiresPermissions("public:comment:edit")
    @Log(title = "文章评论", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PublicDetailComment publicDetailComment)
    {
        return toAjax(publicDetailCommentService.updatePublicDetailComment(publicDetailComment));
    }

    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(PublicDetailComment publicDetailComment)
    {
        // 查询评论
        PublicDetailComment model = publicDetailCommentService.getById(publicDetailComment.getId());
        // 查询是否已经有置顶的评论
        if("02".equals(publicDetailComment.getTopStatus())){
            PublicDetailComment topModel = publicDetailCommentService.lambdaQuery()
                    .eq(PublicDetailComment::getDetailId, model.getDetailId())
                    .eq(PublicDetailComment::getTopStatus, "02")
                    .one();
            if(topModel != null){
                topModel.setTopStatus("01");
                publicDetailCommentService.updateById(topModel);
            }
        }
        model.setTopStatus(publicDetailComment.getTopStatus());
        publicDetailCommentService.updateById(model);
        return AjaxResult.success();
    }

    /**
     * 删除文章评论
     */
    @RequiresPermissions("public:comment:remove")
    @Log(title = "文章评论", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(publicDetailCommentService.deletePublicDetailCommentByIds(ids));
    }
}
