package com.fs.qw.controller;

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.exception.file.OssException;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyUser;
import com.fs.company.service.ICompanyUserService;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.qw.domain.QwAccount;
import com.fs.qw.domain.QwMaterial;
import com.fs.qw.dto.QwMaterialDTO;
import com.fs.qw.service.IQwAccountService;
import com.fs.qw.service.IQwMaterialService;
import com.fs.qw.vo.QwAccountListVO;
import com.fs.store.domain.FsMaterial;
import com.fs.system.oss.CloudStorageService;
import com.fs.system.oss.OSSFactory;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 企微功能账号管理Controller
 *
 * @author fs
 * @date 2023-08-23
 */
@RestController
@RequestMapping("/qw/material")
public class QwMaterialController extends BaseController
{
    @Autowired
    private IQwMaterialService qwMaterialService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询企微素材库列表
     */
    @PreAuthorize("@ss.hasPermi('system:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwMaterial qwMaterial)
    {
        startPage();
        List<QwMaterial> list = qwMaterialService.selectQwMaterialList(qwMaterial);
        return getDataTable(list);
    }

    /**
     * 导出企微素材库列表
     */
    @PreAuthorize("@ss.hasPermi('system:material:export')")
    @Log(title = "企微素材库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwMaterial qwMaterial)
    {
        List<QwMaterial> list = qwMaterialService.selectQwMaterialList(qwMaterial);
        ExcelUtil<QwMaterial> util = new ExcelUtil<QwMaterial>(QwMaterial.class);
        return util.exportExcel(list, "material");
    }

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

    /**
     * 新增素材库
     */
    @Log(title = "素材库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QwMaterialDTO qwMaterialDTO) throws Exception{
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        qwMaterialDTO.setCompanyUserId(loginUser.getUser().getUserId());
        return toAjax(qwMaterialService.insertQwsMaterials(qwMaterialDTO));
    }

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

    /**
     * 删除企微素材库
     */
    @PreAuthorize("@ss.hasPermi('system:material:remove')")
    @Log(title = "企微素材库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qwMaterialService.deleteQwMaterialByIds(ids));
    }

}
