package com.fs.company.controller.qw;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.qw.domain.QwTag;
import com.fs.qw.domain.QwTagGroup;
import com.fs.qw.param.QwTagParam;
import com.fs.qw.service.IQwTagService;
import com.fs.qw.service.IQwTagGroupService;
import com.fs.qw.vo.QwTagGroupListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企微客户标签Controller
 *
 * @author fs
 * @date 2024-06-20
 */
@RestController
@RequestMapping("/qw/tag")
public class QwTagController extends BaseController
{
    @Autowired
    private IQwTagService qwTagService;
    @Autowired
    private IQwTagGroupService qwTagGroupService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询企微客户标签列表
     */
    @GetMapping("/list")
    public TableDataInfo list(QwTag qwTag)
    {
        startPage();

        List<QwTag> list = qwTagService.selectQwTagList(qwTag);
        return getDataTable(list);
    }

    /**
     * 查询标签组列表（包含子标签）- 多级筛选
     * 支持按标签组名称查询
     */
    @GetMapping("/tagGroupList")
    public TableDataInfo tagGroupList(QwTagGroup qwTagGroup)
    {
        startPage();
        List<QwTagGroupListVO> list = qwTagGroupService.selectQwTagGroupListVO(qwTagGroup);
        return getDataTable(list);
    }

    @PostMapping("/searchTags")
    public TableDataInfo  searchTags(@RequestBody QwTagParam tagParam)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        tagParam.setCompanyId(loginUser.getCompany().getCompanyId());
        return getDataTable(qwTagService.searchTags(tagParam));
    }
    /**
     * 导出企微客户标签列表
     */
    @PreAuthorize("@ss.hasPermi('qw:tag:export')")
    @Log(title = "企微客户标签", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwTag qwTag)
    {
        List<QwTag> list = qwTagService.selectQwTagList(qwTag);
        ExcelUtil<QwTag> util = new ExcelUtil<QwTag>(QwTag.class);
        return util.exportExcel(list, "企微客户标签数据");
    }

    /**
     * 获取企微客户标签详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:tag:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qwTagService.selectQwTagById(id));
    }

    /**
     * 新增企微客户标签
     */
    @PreAuthorize("@ss.hasPermi('qw:tag:add')")
    @Log(title = "企微客户标签", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QwTag qwTag)
    {
        return toAjax(qwTagService.insertQwTag(qwTag));
    }

    /**
     * 修改企微客户标签
     */
    @PreAuthorize("@ss.hasPermi('qw:tag:edit')")
    @Log(title = "企微客户标签", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QwTag qwTag)
    {
        return toAjax(qwTagService.updateQwTag(qwTag));
    }

    /**
     * 删除企微客户标签
     */
    @PreAuthorize("@ss.hasPermi('qw:tag:remove')")
    @Log(title = "企微客户标签", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qwTagService.deleteQwTagByIds(ids));
    }
}
