package com.playlet.web.controller.system;

import java.util.List;
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.PublicDeptTags;
import com.playlet.system.service.IPublicDeptTagsService;
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-08-14
 */
@Controller
@RequestMapping("/system/tags")
public class PublicDeptTagsController extends BaseController
{
    private String prefix = "system/tags";

    @Autowired
    private IPublicDeptTagsService publicDeptTagsService;

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

    /**
     * 查询文章标签列表
     */
//    @RequiresPermissions("system:tags:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PublicDeptTags publicDeptTags)
    {
        if(!this.getSysUser().isAdmin()){
            publicDeptTags.setDeptId(this.getSysUser().getDeptId());
        }
        startPage();
        List<PublicDeptTags> list = publicDeptTagsService.selectPublicDeptTagsList(publicDeptTags);
        return getDataTable(list);
    }

    /**
     * 导出文章标签列表
     */
//    @RequiresPermissions("system:tags:export")
    @Log(title = "文章标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PublicDeptTags publicDeptTags)
    {
        List<PublicDeptTags> list = publicDeptTagsService.selectPublicDeptTagsList(publicDeptTags);
        ExcelUtil<PublicDeptTags> util = new ExcelUtil<PublicDeptTags>(PublicDeptTags.class);
        return util.exportExcel(list, "文章标签数据");
    }

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

    /**
     * 新增保存文章标签
     */
//    @RequiresPermissions("system:tags:add")
    @Log(title = "文章标签", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PublicDeptTags publicDeptTags)
    {
        long count = publicDeptTagsService.lambdaQuery().eq(PublicDeptTags::getDeptId, this.getSysUser().getDeptId()).count();
        if(count > 0){
            return AjaxResult.error("请在已存在标签上做修改!");
        }
        publicDeptTags.setDeptId(this.getSysUser().getDeptId());
        return toAjax(publicDeptTagsService.insertPublicDeptTags(publicDeptTags));
    }

    /**
     * 修改文章标签
     */
//    @RequiresPermissions("system:tags:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PublicDeptTags publicDeptTags = publicDeptTagsService.selectPublicDeptTagsById(id);
        mmap.put("publicDeptTags", publicDeptTags);
        return prefix + "/edit";
    }

    /**
     * 修改保存文章标签
     */
//    @RequiresPermissions("system:tags:edit")
    @Log(title = "文章标签", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PublicDeptTags publicDeptTags)
    {
        publicDeptTags.setDeptId(this.getSysUser().getDeptId());
        return toAjax(publicDeptTagsService.updatePublicDeptTags(publicDeptTags));
    }

    /**
     * 删除文章标签
     */
//    @RequiresPermissions("system:tags:remove")
    @Log(title = "文章标签", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(publicDeptTagsService.deletePublicDeptTagsByIds(ids));
    }
}
