package com.xingxi.web.controller.master.tag.controller;

import com.xingxi.common.annotation.Log;
import com.xingxi.common.core.controller.BaseController;
import com.xingxi.common.core.domain.AjaxResult;
import com.xingxi.common.core.page.TableDataInfo;
import com.xingxi.common.enums.BusinessType;
import com.xingxi.common.enums.EDelFlag;
import com.xingxi.common.utils.poi.ExcelUtil;
import com.xingxi.master.tag.domain.Tag;
import com.xingxi.master.tag.service.ITagService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 标签Controller
 *
 * @author xingxi
 * @date 2025-03-09
 */
@Controller
@RequestMapping("/master/tag")
public class TagController extends BaseController {
    private final String prefix = "master/tag";

    @Resource
    private ITagService tagService;

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

    /**
     * 查询标签列表
     */
    @RequiresPermissions("master:tag:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Tag tag) {
        startPage();
        List<Tag> list = tagService.selectTagList(tag);
        return getDataTable(list);
    }

    /**
     * 导出标签列表
     */
    @RequiresPermissions("master:tag:export")
    @Log(title = "标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Tag tag) {
        List<Tag> list = tagService.selectTagList(tag);
        ExcelUtil<Tag> util = new ExcelUtil<>(Tag.class);
        return util.exportExcel(list, "标签数据");
    }

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

    /**
     * 新增保存标签
     */
    @RequiresPermissions("master:tag:add")
    @Log(title = "标签", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Tag tag) {
        return toAjax(tagService.insertTag(tag));
    }

    /**
     * 修改标签
     */
    @RequiresPermissions("master:tag:edit")
    @GetMapping("/edit/{tagId}")
    public String edit(@PathVariable("tagId") Long tagId, ModelMap mMap) {
        Tag tag = tagService.selectTagByTagId(tagId);
        mMap.put("tag", tag);
        return prefix + "/edit";
    }

    /**
     * 修改保存标签
     */
    @RequiresPermissions("master:tag:edit")
    @Log(title = "标签", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Tag tag) {
        return toAjax(tagService.updateTag(tag));
    }

    /**
     * 删除标签
     */
    @RequiresPermissions("master:tag:remove")
    @Log(title = "标签", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tagService.logicDeleteTagByTagIds(ids));
    }
}
