package com.dgtly.api.controller;

import com.dgtly.common.annotation.ApiNoCheckSign;
import com.dgtly.common.annotation.ApiPassToken;
import com.dgtly.common.config.Global;
import com.dgtly.common.config.ServerConfig;
import com.dgtly.common.constant.Constants;
import com.dgtly.common.core.controller.ApiBaseController;
import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.core.domain.ParameterObject;
import com.dgtly.common.utils.StringUtils;
import com.dgtly.common.utils.file.FileUploadUtils;
import com.dgtly.common.utils.file.FileUtils;
import com.dgtly.system.service.ISysDictDataService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用请求处理
 * 
 * @author ruoyi
 */
@ApiNoCheckSign
@ApiPassToken
@RestController()
@RequestMapping("/common")
public class CommonController extends ApiBaseController
{
    private static final Logger log = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private ISysDictDataService dictDataService;


    @ApiOperation(value = "获取字典")
    @ApiImplicitParam(name = "params" , paramType = "body")
    @PostMapping("/dict")
    public Object getDict(){
        ParameterObject obj = getParameterObject();
        obj.checkParameterNotNull("dictType");
        String dictType = obj.getString("dictType");
        return AjaxResult.success().putKV("list", dictDataService.selectSimpleDictDataByType(dictType));
    }


    /**
     * 通用上传请求
     */
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception{
        try
        {
            // 上传文件路径
            String filePath = Global.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.putKV("fileName", fileName);
            ajax.putKV("url", url);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }



}
