package com.ruoyi.web.controller.dxp.system.base;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.dxp.domain.Tag;
import com.ruoyi.dxp.service.ITagService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dxp.domain.TagGroup;
import com.ruoyi.dxp.service.ITagGroupService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 标签分组Controller
 * 
 * @author jy
 * @date 2022-11-06
 */
@RestController
@RequestMapping("/system/base/taggroup")
public class TagGroupController extends BaseController
{
    @Autowired
    private ITagGroupService tagGroupService;

    @Autowired
    private ITagService tagService;

    /**
     * 查询标签分组列表
     */
    @PreAuthorize("@ss.hasPermi('system/base:tag:list')")
    @GetMapping("/list")
    public AjaxResult list(TagGroup tagGroup)
    {
        List<TagGroup> list = tagGroupService.selectTagGroupList(tagGroup);
        return success(list);
    }

    /**
     * 导出标签分组列表
     */
    @PreAuthorize("@ss.hasPermi('system/base:tag:export')")
    @Log(title = "标签分组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TagGroup tagGroup)
    {
        List<TagGroup> list = tagGroupService.selectTagGroupList(tagGroup);
        ExcelUtil<TagGroup> util = new ExcelUtil<TagGroup>(TagGroup.class);
        util.exportExcel(response, list, "标签分组数据");
    }

    /**
     * 获取标签分组详细信息
     */
    @PreAuthorize("@ss.hasPermi('system/base:tag:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tagGroupService.selectTagGroupById(id));
    }

    /**
     * 新增标签分组
     */
    @PreAuthorize("@ss.hasPermi('system/base:tag:add')")
    @Log(title = "标签分组", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody TagGroup tagGroup)
    {
        return toAjax(tagGroupService.insertTagGroup(tagGroup));
    }

    /**
     * 修改标签分组
     */
    @PreAuthorize("@ss.hasPermi('system/base:tag:edit')")
    @Log(title = "标签分组", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@RequestBody TagGroup tagGroup)
    {
        return toAjax(tagGroupService.updateTagGroup(tagGroup));
    }

    /**
     * 删除标签分组
     */
    @PreAuthorize("@ss.hasPermi('system/base:tag:remove')")
    @Log(title = "标签分组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        // 检查是否有挂在在当前分组下的标签
        Tag req = new Tag();
        req.setGroupId(ids[0]);
        List<Tag> tags = tagService.selectTagList(req);
        if (null != tags && tags.size() > 0) {
            return error("当前分组下存在标签");
        }
        return toAjax(tagGroupService.deleteTagGroupByIds(ids));
    }
}
