/**
 * 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.common.utils.CacheUtils;
import com.lightinit.hsdatagateway.common.utils.SpringContextHolder;
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.BizApp;
import com.lightinit.hsdatagateway.modules.gateway.service.BizAppService;

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

/**
 * 应用管理Controller
 * @Author yangrui
 * @version 2019-03-05
 */
@Controller
@RequestMapping(value = "${adminPath}/bizApp")
public class BizAppController extends BaseController {

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

	@RequiresPermissions("bizApp:bizAppList:view")
	@RequestMapping(value = "form")
	public String form(BizApp bizApp, Model model) {
		if(StringUtils.isBlank(bizApp.getAppId())){
			Calendar calendar = Calendar.getInstance();
			//当前时间毫秒数
			String millis = String.valueOf(calendar.getTimeInMillis());
			//6位随机整数
			int newNum = (int)((Math.random()*9+1)*100000);
			String random = String.valueOf(newNum);
			String appId = "app"+millis+random;
			bizApp.setAppId(appId);
		}
		model.addAttribute("bizApp", bizApp);
		return "modules/gateway/bizapp/bizAppForm";
	}

	@RequiresPermissions("bizApp:bizAppList:edit")
	@RequestMapping(value = "save")
	public String save(BizApp bizApp, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizApp)){
			return form(bizApp, model);
		}
		if(bizApp.getId() == null){
			bizApp.setCreatedAt(new Date());
			bizApp.setCreatedBy(UserUtils.getUser().getName());
		}
		String message = "应用ID重复，操作失败";
		if(bizAppService.isRepetition(bizApp)){
			bizAppService.save(bizApp);
			message = "保存应用管理成功";
		}
		addMessage(redirectAttributes, message);
		return "redirect:"+Global.getAdminPath()+"/bizApp/?repage";
	}
	
	@RequiresPermissions("bizApp:bizAppList:edit")
	@RequestMapping(value = "delete")
	public String delete(BizApp bizApp, RedirectAttributes redirectAttributes) {
	    bizApp.setDeletedBy(UserUtils.getUser().getName());
	    bizApp.setDeletedAt(new Date());
		bizAppService.delete(bizApp);
		addMessage(redirectAttributes, "删除应用管理成功");
		return "redirect:"+Global.getAdminPath()+"/bizApp/?repage";
	}


	/**
	 * 更新缓存
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping("/updateCache")
	public String updateCache(RedirectAttributes redirectAttributes){
		BizAppService service = SpringContextHolder.getBean(BizAppService.class);
		List<BizApp> allList = service.findAllList();
		CacheUtils.put("app",allList);
		addMessage(redirectAttributes, "更新缓存成功");
		return "redirect:"+Global.getAdminPath()+"/bizApp/?repage";

	}



}