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

import com.lightinit.hsdatagateway.common.config.Global;
import com.lightinit.hsdatagateway.common.web.BaseController;
import com.lightinit.hsdatagateway.modules.foreign.util.ApiEncryptUtil;
import com.lightinit.hsdatagateway.modules.gateway.entity.*;
import com.lightinit.hsdatagateway.modules.gateway.service.*;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

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

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

	public static final String YES = "y" ;
	public static final String ROUTE = "route" ;
	public static final String APP = "app" ;
	public static final String SIGNSTATUS = "n" ;
	public static final String FREE = "free" ;

	@Autowired
	private BizRouteService bizRouteService;

	@Autowired
	private BizAppService bizAppService;

	@Autowired
	private BizSignService bizSignService;

	@Autowired
	private AuthService authService;

	@Autowired
	private ChargeService chargeService;


	public String getRandomId(String type){
		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 randomId = type+millis+random;
		return randomId ;
	}

	// 添加路由接口
	@RequiresPermissions("bizRoute:bizRouteList:edit")
	@RequestMapping(value = "routesave")
	@ResponseBody
	public String routesave(BizRoute bizRoute, Model model, RedirectAttributes redirectAttributes) {
		String randomId = getRandomId(ROUTE);
		bizRoute.setRouteId(randomId);
		if(bizRoute.getId() == null){
            bizRoute.setCreatedBy(UserUtils.getUser().getName());
            bizRoute.setCreatedAt(new Date());

        }
		bizRoute.setRouteStatus(YES);
		bizRouteService.save(bizRoute);
		return randomId ;
	}

	// 添加应用接口
	@RequiresPermissions("bizApp:bizAppList:edit")
	@RequestMapping(value = "appsave")
	@ResponseBody
	public String appsave(BizApp bizApp, Model model, RedirectAttributes redirectAttributes) {
		String randomId = getRandomId(APP);
		bizApp.setAppId(randomId);
		if(bizApp.getId() == null){
			bizApp.setCreatedAt(new Date());
			bizApp.setCreatedBy(UserUtils.getUser().getName());
		}
		bizApp.setAppStatus(YES);
		bizAppService.save(bizApp);
		return randomId ;
	}

	// 添加签名
	@RequiresPermissions("bizSign:bizSignList:edit")
	@RequestMapping(value = "signsave")
	@ResponseBody
	public void signsave(BizSign bizSign, Model model, RedirectAttributes redirectAttributes) {
		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);
		}
		if(bizSign.getId() == null){
			bizSign.setCreatedAt(new Date());
			bizSign.setCreatedBy(UserUtils.getUser().getName());
		}
		bizSign.setSignStatus(SIGNSTATUS);
		bizSignService.save(bizSign);
	}


	// 添加鉴权
	@RequiresPermissions("gateway:auth:edit")
	@RequestMapping(value = "authsave")
	@ResponseBody
	public void authsave(Auth auth, Model model, RedirectAttributes redirectAttributes) {
		if (auth.getId() == null) {
			auth.setCreatedBy(UserUtils.getUser().getName());
		}
		authService.save(auth);
	}

	// 添加计费
	@RequiresPermissions("gateway:charge:edit")
	@RequestMapping(value = "chargesave")
	public void chargesave(Charge charge, Model model, RedirectAttributes redirectAttributes) {
		if (charge.getId() == null) {
			charge.setCreatedBy(UserUtils.getUser().getName());
			charge.setCreatedAt(new Date());
			charge.setUsedValue(0L);
			charge.setRestValue(charge.getChargeValue());
		}
		charge.setChargeMethod(FREE);
		chargeService.save(charge);
	}

}