package com.playlet.web.controller.system;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.playlet.common.utils.StringUtils;
import com.playlet.system.domain.PublicDetailComment;
import com.playlet.system.service.IPublicDetailCommentService;
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.PublicCommentResponse;
import com.playlet.system.service.IPublicCommentResponseService;
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-20
 */
@Controller
@RequestMapping("/system/public/response")
public class PublicCommentResponseController extends BaseController
{
    private String prefix = "system/public/response";

    @Autowired
    private IPublicCommentResponseService publicCommentResponseService;
    @Autowired
    private IPublicDetailCommentService publicDetailCommentService;

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

    /**
     * 查询作者回复评论记录列表
     */
    @RequiresPermissions("public:response:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PublicCommentResponse publicCommentResponse)
    {
        startPage();
        List<PublicCommentResponse> list = publicCommentResponseService.selectPublicCommentResponseList(publicCommentResponse);
        return getDataTable(list);
    }

    /**
     * 导出作者回复评论记录列表
     */
    @RequiresPermissions("public:response:export")
    @Log(title = "作者回复评论记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PublicCommentResponse publicCommentResponse)
    {
        List<PublicCommentResponse> list = publicCommentResponseService.selectPublicCommentResponseList(publicCommentResponse);
        ExcelUtil<PublicCommentResponse> util = new ExcelUtil<PublicCommentResponse>(PublicCommentResponse.class);
        return util.exportExcel(list, "作者回复评论记录数据");
    }

    /**
     * 新增作者回复评论记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存作者回复评论记录
     */
    @RequiresPermissions("public:response:add")
    @Log(title = "作者回复评论记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PublicCommentResponse publicCommentResponse)
    {
        PublicCommentResponse model = publicCommentResponseService.lambdaQuery()
                .eq(PublicCommentResponse::getCommentId, publicCommentResponse.getCommentId())
                .one();
        if(model != null){
            // 置空默认为删除
            if(StringUtils.isEmpty(publicCommentResponse.getResponseContent())){
                publicCommentResponseService.removeById(model.getId());
            }
            model.setResponseContent(publicCommentResponse.getResponseContent());
            model.setCreateTime(new Date());
            publicCommentResponseService.updateById(model);
        }else {
            publicCommentResponse.setCreateBy(getLoginName());
            publicCommentResponseService.insertPublicCommentResponse(publicCommentResponse);
        }
        PublicDetailComment comment = publicDetailCommentService.getById(publicCommentResponse.getCommentId());
        if(comment != null){
            comment.setRemark(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            if(StringUtils.isEmpty(publicCommentResponse.getResponseContent())){
                comment.setRemark("");
            }
            publicDetailCommentService.updateById(comment);
        }
        return AjaxResult.success();
    }

    /**
     * 修改作者回复评论记录
     */
    @RequiresPermissions("public:response:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PublicCommentResponse publicCommentResponse = publicCommentResponseService.selectPublicCommentResponseById(id);
        mmap.put("publicCommentResponse", publicCommentResponse);
        return prefix + "/edit";
    }

    /**
     * 修改保存作者回复评论记录
     */
    @RequiresPermissions("public:response:edit")
    @Log(title = "作者回复评论记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PublicCommentResponse publicCommentResponse)
    {
        return toAjax(publicCommentResponseService.updatePublicCommentResponse(publicCommentResponse));
    }

    /**
     * 删除作者回复评论记录
     */
    @RequiresPermissions("public:response:remove")
    @Log(title = "作者回复评论记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(publicCommentResponseService.deletePublicCommentResponseByIds(ids));
    }
}
