package cn.ey.biz.module.system.controller.admin.wiki;

import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.framework.security.core.LoginUser;
import cn.ey.biz.framework.security.core.util.SecurityFrameworkUtils;
import cn.ey.biz.module.system.controller.admin.wiki.vo.*;
import cn.ey.biz.module.system.service.wiki.WikiSpaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.ey.biz.common.pojo.R.success;

@Api(tags = "管理后台 -空间")
@RestController
@RequestMapping("/system/wiki/space")
@Validated
public class WikiSpaceController {

    @Resource
    private WikiSpaceService wikiSpaceService;

    @PostMapping("/create")
    @ApiOperation("创建空间")
    @PreAuthorize("@ss.hasPermission('system:wiki-space:create')")
    public R<Void> createWikiSpace(@Valid @RequestPart("req") WikiSpaceCreateReqVO createReqVO, @RequestParam(name = "file", required = false) MultipartFile file) {
        createReqVO.setUploadFile(file);
        wikiSpaceService.createWikiSpace(createReqVO);
        return success(null);
    }

    @PutMapping("/update")
    @ApiOperation("更新空间")
    @PreAuthorize("@ss.hasPermission('system:wiki-space:update')")
    public R<Boolean> updateWikiSpace(@RequestPart("req") WikiSpaceUpdateReqVO updateReqVO, @RequestParam(name = "file", required = false) MultipartFile file) {
        updateReqVO.setUploadFile(file);
        wikiSpaceService.updateWikiSpace(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除空间")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:wiki-space:delete')")
    public R<Boolean> deleteWikiSpace(@RequestParam("id") Long id){
        wikiSpaceService.deleteWikiSpace(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得空间")
    @ApiImplicitParam(name = "id", value = "主键", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:wiki-space:query')")
    public R<WikiSpaceRespVO> getWikiSpace(@RequestParam("id") Long id) {
        WikiSpaceRespVO wikiSpace=wikiSpaceService.getWikiSpace(id);
        return success(wikiSpace);
    }

    @GetMapping("/page")
    @ApiOperation("获得空间分页")
    @PreAuthorize("@ss.hasPermission('system:wiki-space:query')")
    public R<PageResult<WikiSpaceRespVO>> getWikiSpacePage(@Valid WikiSpacePageReqVO pageVO) {
        PageResult<WikiSpaceRespVO> wikiSpacePage =wikiSpaceService.getWikiSpacePage(pageVO);
        return success(wikiSpacePage);
    }

    @PostMapping("/release")
    @ApiOperation("发布")
    @PreAuthorize("@ss.hasPermission('system:wiki-space:update')")
    public R<Boolean> release(@RequestParam("id") Long id) {
        wikiSpaceService.release(id);
        return success(true);
    }

    @GetMapping("/get-list-by-type")
    @ApiOperation("根据app and type 查询")
    @PreAuthorize("@ss.hasPermission('system:wiki-space:query')")
    public R<List<WikiSpaceRespVO>> getListByType(@Valid WikiSpaceTypeReqVO reqVO) {
        List<WikiSpaceRespVO> wikiSpaceList=wikiSpaceService.getListByType(reqVO);
        return success(wikiSpaceList);
    }

    @GetMapping("/get-image-list")
    @ApiOperation("根据id查询图片")
    @PreAuthorize("@ss.hasPermission('system:wiki-space:query')")
    public R <List<String>> getImageByIdAndType(@RequestParam("id") Long id) {
        List<String> imageByIdList = wikiSpaceService.getImageById(id);
        return success(imageByIdList);
    }

}
