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.QwMaterialGroup;
import com.fs.qw.service.IQwMaterialGroupService;
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/materialGroup")
public class QwMaterialGroupController extends BaseController
{
    @Autowired
    private IQwMaterialGroupService qwMaterialGroupService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询企业微信素材分组列表
     */
    @PreAuthorize("@ss.hasPermi('qw:materialGroup:myList')")
    @GetMapping("/myList")
    public TableDataInfo myList(QwMaterialGroup qwMaterialGroup)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        qwMaterialGroup.setCompanyId(loginUser.getUser().getCompanyId());
        qwMaterialGroup.setCreateUserId(loginUser.getUser().getUserId());
        List<QwMaterialGroup> list = qwMaterialGroupService.selectQwMaterialGroupList(qwMaterialGroup);
        return getDataTable(list);
    }


    /**
     * 查询企业微信素材分组列表
     */
    @PreAuthorize("@ss.hasPermi('qw:materialGroup:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwMaterialGroup qwMaterialGroup)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        qwMaterialGroup.setCompanyId(loginUser.getUser().getCompanyId());
        List<QwMaterialGroup> list = qwMaterialGroupService.selectQwMaterialGroupList(qwMaterialGroup);
        return getDataTable(list);
    }

    /**
     * 导出企业微信素材分组列表
     */
    @PreAuthorize("@ss.hasPermi('qw:materialGroup:export')")
    @Log(title = "企业微信素材分组", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwMaterialGroup qwMaterialGroup)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        qwMaterialGroup.setCompanyId(loginUser.getUser().getCompanyId());
        List<QwMaterialGroup> list = qwMaterialGroupService.selectQwMaterialGroupList(qwMaterialGroup);
        ExcelUtil<QwMaterialGroup> util = new ExcelUtil<QwMaterialGroup>(QwMaterialGroup.class);
        return util.exportExcel(list, "企业微信素材分组数据");
    }

    /**
     * 获取企业微信素材分组详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:materialGroup:query')")
    @GetMapping(value = "/{materialGroupId}")
    public AjaxResult getInfo(@PathVariable("materialGroupId") Long materialGroupId)
    {
        return AjaxResult.success(qwMaterialGroupService.selectQwMaterialGroupByMaterialGroupId(materialGroupId));
    }

    /**
     * 新增企业微信素材分组
     */
    @PreAuthorize("@ss.hasPermi('qw:materialGroup:add')")
    @Log(title = "企业微信素材分组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QwMaterialGroup qwMaterialGroup)
    {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        //公司id
        qwMaterialGroup.setCompanyId(loginUser.getCompany().getCompanyId());
        qwMaterialGroup.setCreateUserId(loginUser.getUser().getUserId());

        return toAjax(qwMaterialGroupService.insertQwMaterialGroup(qwMaterialGroup));
    }

    /**
     * 修改企业微信素材分组
     */
    @PreAuthorize("@ss.hasPermi('qw:materialGroup:edit')")
    @Log(title = "企业微信素材分组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QwMaterialGroup qwMaterialGroup)
    {
        return toAjax(qwMaterialGroupService.updateQwMaterialGroup(qwMaterialGroup));
    }

    /**
     * 删除企业微信素材分组
     */
    @PreAuthorize("@ss.hasPermi('qw:materialGroup:remove')")
    @Log(title = "企业微信素材分组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materialGroupId}")
    public AjaxResult remove(@PathVariable Long materialGroupId)
    {
        return toAjax(qwMaterialGroupService.deleteQwMaterialGroupByMaterialGroupId(materialGroupId));
    }
}
