package org.jeecg.modules.shorturl.controller;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.shorturl.entity.CsPtShorturl;
import org.jeecg.modules.shorturl.service.ICsPtShorturlService;
import org.jeecg.modules.util.ShortUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @Description: 短链接
* @Author: jeecg-boot
* @Date:   2023-11-05
* @Version: V1.0
*/
@Api(tags="短链接")
@RequestMapping("/s")
@Controller
@Slf4j
public class CsPtShorturlKzController extends JeecgController<CsPtShorturl, ICsPtShorturlService> {
   @Autowired
   private ICsPtShorturlService csPtShorturlService;

    @RequestMapping(value = "/{id}")
    public ModelAndView redirect(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") String id){

        CsPtShorturl csPtShorturl = csPtShorturlService.getById(ShortUrl.base62Decode(id));
        if(csPtShorturl == null){
            throw new JeecgBootException("请求链接不存在");
        }
        return new ModelAndView("redirect:" + csPtShorturl.getUrl());
    }

}
