package ink.wgink.service.user.controller.route;

import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.interfaces.user.IUserExpandBaseService;
import ink.wgink.interfaces.user.IUserExpandOptionButton;
import ink.wgink.util.ResourceUtil;
import ink.wgink.util.request.StaticResourceRequestUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: UserRouteController
 * @Description: 用户路由
 * @Author: wanggeng
 * @Date: 2021/2/5 7:50 下午
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "用户路由接口")
@Controller
@RequestMapping(ISystemConstant.ROUTE_PREFIX + "/user")
public class UserRouteController {

    @Autowired(required = false)
    private IUserExpandBaseService userExpandBaseService;
    @Autowired(required = false)
    private IUserExpandOptionButton userExpandOptionButton;

    @GetMapping("list")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("user/list");
        if (userExpandBaseService != null) {
            mv.addObject("userExpand", userExpandBaseService.getRoute());
        }
        if (userExpandOptionButton != null) {
            Map<String, Object> expandOption = new HashMap<>();
            expandOption.put("btns", userExpandOptionButton.listBtn());
            expandOption.put("width", userExpandOptionButton.getWidth());
            expandOption.put("title", userExpandOptionButton.getTitle());
            expandOption.put("fixed", userExpandOptionButton.getFixed());
            expandOption.put("align", userExpandOptionButton.getAlign());
            mv.addObject("expandOption", expandOption);
        }
        return mv;
    }

    @GetMapping("save")
    public ModelAndView save() {
        return new ModelAndView("user/save");
    }

    @GetMapping("update")
    public ModelAndView update() {
        return new ModelAndView("user/update");
    }

    @GetMapping("rest-password")
    public ModelAndView restPassword() {
        return new ModelAndView("user/rest-password");
    }

    @GetMapping("update-username")
    public ModelAndView updateUsername() {
        return new ModelAndView("user/update-username");
    }

    @GetMapping("update-password")
    public ModelAndView updatePassword() {
        return new ModelAndView("user/update-password");
    }

    @GetMapping("update-expired-date")
    public ModelAndView updateExpiredDate() {
        return new ModelAndView("user/update-expired-date");
    }

    @GetMapping("upload/upload-excel")
    public ModelAndView uploadExcel() {
        return new ModelAndView("user/upload/upload-excel");
    }

    @GetMapping("upload/upload-excel-template")
    public void excelTemplate(HttpServletResponse response) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("templates/user/upload/upload-excel-template.xls");
        StaticResourceRequestUtil.download(response, inputStream, "导入模板.xls");
    }

}
