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.domain.R;
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.QwMaterial;
import com.fs.qw.param.QwMaterialParam;
import com.fs.qw.service.IQwMaterialService;
import com.fs.qw.service.IQwUserService;
import com.fs.qw.vo.QwMaterialVO;
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/material")
public class QwMaterialController extends BaseController
{
    @Autowired
    private IQwMaterialService qwMaterialService;

    @Autowired
    private IQwUserService qwUserService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询我的素材库列表
     */
    @PreAuthorize("@ss.hasPermi('qw:material:myList')")
    @GetMapping("/myList")
    public TableDataInfo myList(QwMaterialParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getUser().getCompanyId());
        param.setCreateUserId(loginUser.getUser().getUserId());
        List<QwMaterialVO> list = qwMaterialService.selectQwMaterialList(param);
        return getDataTable(list);
    }

    /**
     * 查询素材库列表
     */
    @PreAuthorize("@ss.hasPermi('qw:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwMaterialParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getUser().getCompanyId());
        List<QwMaterialVO> list = qwMaterialService.selectQwMaterialList(param);
        return getDataTable(list);

    }

    /**
     * 导出素材库列表
     */
    @PreAuthorize("@ss.hasPermi('qw:material:export')")
    @Log(title = "素材库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwMaterialParam param )
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getUser().getCompanyId());
        List<QwMaterialVO> list = qwMaterialService.selectQwMaterialList(param);
        ExcelUtil<QwMaterialVO> util = new ExcelUtil<QwMaterialVO>(QwMaterialVO.class);
        return util.exportExcel(list, "素材库数据");
    }

    /**
     * 获取素材库详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:material:query')")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getInfo(@PathVariable("materialId") Long materialId)
    {
        return AjaxResult.success(qwMaterialService.selectQwMaterialByMaterialId(materialId));
    }

    /**
     * 新增素材库
     */
    @PreAuthorize("@ss.hasPermi('qw:material:add')")
    @Log(title = "素材库", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody QwMaterial qwMaterial) throws Exception {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        qwMaterial.setCreateUserId(loginUser.getUser().getUserId());
        qwMaterial.setCompanyId(loginUser.getUser().getCompanyId());

        return qwMaterialService.insertQwMaterial(qwMaterial);
    }

    /**
     * 修改素材库
     */
    @PreAuthorize("@ss.hasPermi('qw:material:edit')")
    @Log(title = "素材库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QwMaterial qwMaterial)
    {
        return toAjax(qwMaterialService.updateQwMaterial(qwMaterial));
    }

    /**
     * 删除素材库
     */
    @PreAuthorize("@ss.hasPermi('qw:material:remove')")
    @Log(title = "素材库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materialId}")
    public AjaxResult remove(@PathVariable Long materialId)
    {
        return toAjax(qwMaterialService.deleteQwMaterialByMaterialId(materialId));
    }
}
