package com.gree.mall.manager.controller.common;

import com.gree.mall.manager.annotation.ApiNotAuth;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.SysDict;
import com.gree.mall.manager.plus.service.SysDictService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.rmi.RemoteException;

@RequiredArgsConstructor
@Controller
public class RedirectController {


    private final CommonLogic commonLogic;
    private final SysDictService sysDictService;

    @ApiNotAuth
    @GetMapping("img/get")
    public void getImg(
            @RequestParam() String key,
            HttpServletResponse response
    ) throws IOException, RemoteServiceException {
        String file = commonLogic.getFile(key);
        response.sendRedirect(file);
    }

    @ApiOperation("帮助文档")
    @GetMapping("help")
    public void help(HttpServletResponse response) throws IOException {
        SysDict sysDict = sysDictService.lambdaQuery().eq(SysDict::getDictType, "HELP").last("limit 1").one();
        if(sysDict == null){
            throw new RemoteServiceException("暂无文档链接，请联系相关人员");
        }
        if(sysDict != null){
            response.sendRedirect(sysDict.getDictValue());
        }

    }

}
