package com.vctgo.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vctgo.common.core.utils.poi.ExcelUtil;
import com.vctgo.common.core.web.controller.BaseController;
import com.vctgo.common.core.web.domain.AjaxResult;
import com.vctgo.common.security.annotation.Logical;
import com.vctgo.common.security.annotation.RequiresPermissions;
import com.vctgo.system.api.domain.dto.SysTagManagementDto;
import com.vctgo.system.domain.SysTagManagement;
import com.vctgo.system.service.ISysTagManagementService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;

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

/**
 * 标签管理Controller
 *
 * @author wanglonghua
 * @date 2023-08-14
 */
@RestController
@RequestMapping("/tag_manage")
public class SysTagManagementController extends BaseController
{
    @Autowired
    private ISysTagManagementService sysTagManagementService;

    /**
     * 查询标签管理分页列表
     */
    @RequiresPermissions("system:tag_manage:page")
    @GetMapping("/page")
    public AjaxResult pageList(SysTagManagement sysTagManagement)
    {
        IPage<SysTagManagement> pageList = sysTagManagementService.selectSysTagManagementPage(sysTagManagement);
        return AjaxResult.success(pageList);
    }

    /**
     * 导出标签管理列表
     */
    @RequiresPermissions("system:tag_manage:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTagManagement sysTagManagement)
    {
        List<SysTagManagement> list = sysTagManagementService.selectSysTagManagementList(sysTagManagement);
        ExcelUtil<SysTagManagement> util = new ExcelUtil<SysTagManagement>(SysTagManagement.class);
        util.exportExcel(response, list, "标签管理数据");
    }

    /**
     * 查询全部的标签管理列表
     */
    @RequiresPermissions(value = {"system:tag_manage:list","activity:tag_manage:list"}, logical = Logical.OR)
    @GetMapping("/list")
    public AjaxResult list(SysTagManagement sysTagManagement)
    {
        List<SysTagManagement> list = sysTagManagementService.selectSysTagManagementList(sysTagManagement);
        return AjaxResult.success(list);
    }

    /**
     * 查询全部的标签管理列表
     */
    @GetMapping("/external/list")
    public AjaxResult getTagList(@SpringQueryMap SysTagManagementDto sysTagManagementDto)
    {
        SysTagManagement sysTagManagement = new SysTagManagement();
        BeanUtils.copyProperties(sysTagManagementDto, sysTagManagement);
        if(CollectionUtils.isNotEmpty(sysTagManagementDto.getTagIdList())){
            sysTagManagement.getParams().put("tagIdList", sysTagManagementDto.getTagIdList());
        }
        List<SysTagManagement> list = sysTagManagementService.selectSysTagManagementList(sysTagManagement);
        return AjaxResult.success(list);
    }

    /**
     * 获取标签管理详细信息
     */
    @RequiresPermissions("system:tag_manage:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysTagManagementService.selectSysTagManagementById(id));
    }

    /**
     * 新增标签管理
     */
    @RequiresPermissions("system:tag_manage:add")
    @PostMapping
    public AjaxResult add(@RequestBody SysTagManagement sysTagManagement)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("tagName", sysTagManagement.getTagName());
        if(sysTagManagement.getId() != null){
            params.put("tagId", sysTagManagement.getId());
        }
        List<SysTagManagement> repeatTagManagementList = sysTagManagementService.selectTagListByParams(params);
        if(CollectionUtils.isNotEmpty(repeatTagManagementList)){
            return AjaxResult.error("标签名称已存在");
        }
        return toAjax(sysTagManagementService.insertSysTagManagement(sysTagManagement));
    }

    /**
     * 修改标签管理
     */
    @RequiresPermissions("system:tag_manage:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody SysTagManagement sysTagManagement)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("tagName", sysTagManagement.getTagName());
        if(sysTagManagement.getId() != null){
            params.put("tagId", sysTagManagement.getId());
        }
        List<SysTagManagement> repeatTagManagementList = sysTagManagementService.selectTagListByParams(params);
        if(CollectionUtils.isNotEmpty(repeatTagManagementList)){
            return AjaxResult.error("标签名称已存在");
        }
        return toAjax(sysTagManagementService.updateSysTagManagement(sysTagManagement));
    }

    /**
     * 修改标签管理
     */
    @RequiresPermissions("system:tag_manage:tag-status")
    @PutMapping("/tag-status")
    public AjaxResult changeTagStatus(@RequestBody SysTagManagement sysTagManagement)
    {
        return toAjax(sysTagManagementService.updateSysTagManagement(sysTagManagement));
    }

    /**
     * 删除标签管理
     */
    @RequiresPermissions("system:tag_manage:abandon")
	@PutMapping("/abandon/{ids}")
    public AjaxResult abandon(@PathVariable Long[] ids)
    {
        return toAjax(sysTagManagementService.logicalDeleteSysTagManagementByIds(ids));
    }

    /**
     * 删除标签管理
     */
    @RequiresPermissions("system:tag_manage:remove")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysTagManagementService.deleteSysTagManagementByIds(ids));
    }

    /**
     * 查询所有标签
     */
    @RequiresPermissions("system:tag_manage:query")
    @GetMapping(value = "/getAllTag")
    public AjaxResult getAllTag(SysTagManagement sysTagManagement) {
        List<SysTagManagement> list = sysTagManagementService.getAllTag(sysTagManagement);
        return AjaxResult.success(list);
    }
}
