package com.keyware.shandan.control.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.keyware.shandan.common.controller.BaseController;
import com.keyware.shandan.common.entity.Result;
import com.keyware.shandan.system.entity.OauthClientDetails;
import com.keyware.shandan.system.entity.SysFile;
import com.keyware.shandan.system.service.DesktopService;
import com.keyware.shandan.system.service.OauthClientDetailsService;
import com.keyware.shandan.system.service.SysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

/**
 * Oauth2客户端前端控制器
 *
 * @author Guoxin
 * @since 2021/11/30
 */
@RestController
@RequestMapping("/sys/oauth/client")
public class OauthClientController extends BaseController<OauthClientDetailsService, OauthClientDetails, Object> {

    private final static String ICON_FILE_TYPE = "APP-ICON";

    @Autowired
    private DesktopService desktopService;

    @Autowired
    private SysFileService sysFileService;


    /**
     * 跳转客户端管理首页
     *
     * @param mov
     * @return
     */
    @GetMapping("/")
    public ModelAndView client(ModelAndView mov) {
        mov.setViewName("client/client");
        return mov;
    }


    /**
     * 跳转客户端编辑页
     *
     * @param
     * @return
     */
    @GetMapping("/edit")
    public ModelAndView edit(ModelAndView modelAndView) {
        modelAndView.setViewName("client/clientEdit");
        return modelAndView;
    }


    /**
     * 上传图标
     *
     * @param file
     * @return
     */
    @PostMapping("/upload/icon")
    public Result<Object> iconUpload(MultipartFile file) {
        try {
            SysFile icon = new SysFile();
            icon.setFileType(ICON_FILE_TYPE);
            sysFileService.uploadFiles(file, icon);
            return Result.of(icon);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.of(null, false, "上传失败");
        }
    }

    @GetMapping("/upload/icon/list")
    public Result<Object> getUploadIconList() {
        QueryWrapper<SysFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("FILE_TYPE", ICON_FILE_TYPE);
        return Result.of(sysFileService.list(queryWrapper));
    }
}
