package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.TabBlog;
import com.ruoyi.project.system.service.ISysDictDataService;
import com.ruoyi.project.system.service.ISysUserService;
import com.ruoyi.project.system.service.TabBlogService;
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.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/blog")
public class TabBlogController extends BaseController {

    @Autowired
    private TabBlogService tabBlogService;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private ISysUserService userService;

    /**
     * 新增文章
     */
    @PreAuthorize("@ss.hasPermi('blog:blog:add')")
    @PostMapping
    public AjaxResult add(@RequestBody TabBlog tabBlog) {
        int i = tabBlogService.add(tabBlog);
        return toAjax(i);
    }

    @Autowired
    private ISysDictDataService dictDataService;
    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/dictType/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        return AjaxResult.success(dictDataService.selectDictDataByType(dictType));
    }

    /**
     * 新增文章
     */
    @PreAuthorize("@ss.hasPermi('blog:blog:query')")
    @GetMapping("/list")
    public TableDataInfo list(TabBlog tabBlog) {
        startPage();
        List<TabBlog> list = tabBlogService.list(tabBlog);
        return getDataTable(list);
    }

    /**
     * 查看文章
     */
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable Integer id) {
        TabBlog tabBlog = tabBlogService.get(id);
        return AjaxResult.success(tabBlog);
    }

    /**
     * 更新文章
     */
    @PreAuthorize("@ss.hasPermi('blog:blog:edit')")
    @PutMapping
    public AjaxResult update(@RequestBody TabBlog tabBlog) {
        int i = tabBlogService.update(tabBlog);
        return toAjax(i);
    }

    /**
     * 发布文章
     *
     * @param tabBlog
     * @return
     */
    @PreAuthorize("@ss.hasPermi('blog:blog:push')")
    @PutMapping("/status")
    public AjaxResult status(@RequestBody TabBlog tabBlog) {
        int i = tabBlogService.update(tabBlog);
        return toAjax(i);
    }

    /**
     * 批量删除文章
     *
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('blog:blog:remove')")
    @DeleteMapping
    public AjaxResult delete(@RequestBody List<Integer> ids) {
        int i = tabBlogService.delete(ids);
        return toAjax(i);
    }

    /**
     * 头像上传
     */
    @Log(title = "文件标题图片上传", businessType = BusinessType.UPDATE)
    @PostMapping("/titleImg")
    public AjaxResult titleImg(MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
            if (StringUtils.isNotEmpty(avatar)) {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", avatar);
                return ajax;
            } else {
                AjaxResult ajax = AjaxResult.error("上传失败!");
                return ajax;
            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }

}