/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lightinit.hsdatagateway.modules.gateway.web;

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

import com.lightinit.hsdatagateway.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lightinit.hsdatagateway.common.config.Global;
import com.lightinit.hsdatagateway.common.persistence.Page;
import com.lightinit.hsdatagateway.common.web.BaseController;
import com.lightinit.hsdatagateway.common.utils.StringUtils;
import com.lightinit.hsdatagateway.modules.gateway.entity.BizRoute;
import com.lightinit.hsdatagateway.modules.gateway.service.BizRouteService;

import java.util.Calendar;
import java.util.Date;

/**
 * 路由管理Controller
 * @Author yangrui
 * @version 2019-03-05
 */
@Controller
@RequestMapping(value = "${adminPath}/bizRoute")
public class BizRouteController extends BaseController {

	@Autowired
	private BizRouteService bizRouteService;
	
	@ModelAttribute
	public BizRoute get(@RequestParam(required=false) Long id) {
		BizRoute entity = null;
		if (id != null){
			entity = bizRouteService.get(id);
		}
		if (entity == null){
			entity = new BizRoute();
		}
		return entity;
	}
	
	@RequiresPermissions("bizRoute:bizRouteList:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizRoute bizRoute, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BizRoute> page = bizRouteService.findPage(new Page<BizRoute>(request, response), bizRoute); 
		model.addAttribute("page", page);
		return "modules/gateway/bigroute/bizRouteList";
	}

	@RequiresPermissions("bizRoute:bizRouteList:view")
	@RequestMapping(value = "form")
	public String form(BizRoute bizRoute, Model model) {
		if(StringUtils.isBlank(bizRoute.getRouteId())){
			Calendar calendar = Calendar.getInstance();
			//当前时间毫秒数
			String millis = String.valueOf(calendar.getTimeInMillis());
			//随机生成六位整数
			int newNum = (int)((Math.random()*9+1)*100000);
			String random = String.valueOf(newNum);
			String routeId = "route"+millis+random;
			bizRoute.setRouteId(routeId);
		}
		model.addAttribute("bizRoute", bizRoute);
		return "modules/gateway/bigroute/bizRouteForm";
	}

	@RequiresPermissions("bizRoute:bizRouteList:edit")
	@RequestMapping(value = "save")
	public String save(BizRoute bizRoute, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizRoute)){
			return form(bizRoute, model);
		}
		if(bizRoute.getId() == null){
            bizRoute.setCreatedBy(UserUtils.getUser().getName());
            bizRoute.setCreatedAt(new Date());

        }
        String message = "路由ID重复，操作失败";
		if(bizRouteService.isRepetition(bizRoute)){
			bizRouteService.save(bizRoute);
			message = "保存路由管理成功";
		}
		addMessage(redirectAttributes, message);
		return "redirect:"+Global.getAdminPath()+"/bizRoute/?repage";
	}

	@RequiresPermissions("bizRoute:bizRouteList:edit")
	@RequestMapping(value = "delete")
	public String delete(BizRoute bizRoute, RedirectAttributes redirectAttributes) {
		bizRoute.setDeletedBy(UserUtils.getUser().getName());
		bizRoute.setDeletedAt(new Date());
		bizRouteService.delete(bizRoute);
		addMessage(redirectAttributes, "删除路由管理成功");
		return "redirect:"+Global.getAdminPath()+"/bizRoute/?repage";
	}

}