/**
 * 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.foreign.util.ApiEncryptUtil;
import com.lightinit.hsdatagateway.modules.sys.utils.UserUtils;
import org.apache.commons.codec.binary.Base64;
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.modules.gateway.entity.BizSign;
import com.lightinit.hsdatagateway.modules.gateway.service.BizSignService;

import java.security.KeyPair;
import java.util.Date;

/**
 * 签名管理Controller
 * @Author yangrui
 * @version 2019-03-05
 */
@Controller
@RequestMapping(value = "${adminPath}/bizSign")
public class BizSignController extends BaseController {

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

	@RequiresPermissions("bizSign:bizSignList:view")
	@RequestMapping(value = "form")
	public String form(BizSign bizSign, Model model){
		if(bizSign.getId() == null){
			KeyPair keyPair = null;
			try {
				keyPair = ApiEncryptUtil.getKeyPair();
			} catch (Exception e) {
				e.printStackTrace();
			}
			String privateKey = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded()));
            String publicKey = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded()));
			bizSign.setPrivateKey(privateKey);
			bizSign.setPublicKey(publicKey);
		}
		model.addAttribute("bizSign", bizSign);
		return "modules/gateway/bizsign/bizSignForm";
	}

	@RequiresPermissions("bizSign:bizSignList:edit")
	@RequestMapping(value = "save")
	public String save(BizSign bizSign, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizSign)){
			return form(bizSign, model);
		}
		if(bizSign.getId() == null){
			bizSign.setCreatedAt(new Date());
			bizSign.setCreatedBy(UserUtils.getUser().getName());
		}
		String message = "应用ID、用户ID重复，操作失败";
		if(bizSignService.isRepetition(bizSign)){
			bizSignService.save(bizSign);
			message = "保存签名管理成功";
		}

		addMessage(redirectAttributes, message);
		return "redirect:"+Global.getAdminPath()+"/bizSign/?repage";
	}
	
	@RequiresPermissions("bizSign:bizSignList:edit")
	@RequestMapping(value = "delete")
	public String delete(BizSign bizSign, RedirectAttributes redirectAttributes) {
		bizSign.setDeletedAt(new Date());
		bizSign.setDeletedBy(UserUtils.getUser().getName());
		bizSignService.delete(bizSign);
		addMessage(redirectAttributes, "删除签名管理成功");
		return "redirect:"+Global.getAdminPath()+"/bizSign/?repage";
	}

}