package com.hotent.base.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import com.hotent.base.util.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.dynamic.datasource.toolkit.CryptoUtils;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.cache.CacheManager;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.time.DateUtil;
import com.hotent.base.vo.DruidEncryptDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/base/tools/v1/")
@Api(tags="工具接口")
@ApiGroup(group= {ApiGroupConsts.GROUP_BPM,ApiGroupConsts.GROUP_FORM,ApiGroupConsts.GROUP_PORTAL,ApiGroupConsts.GROUP_UC})
public class ToolsController{
	@Value("${spring.profiles.version:''}")
	String platformVersion;

	@RequestMapping(value = "getPinyin", method = RequestMethod.GET, produces={"application/json; charset=utf-8" })
	@ApiOperation(value = "获取拼音", httpMethod = "GET", notes = "获取拼音")
	public CommonResult<String> getPinyin(
			@ApiParam(required = true, name = "chinese", value = "中文内容") @RequestParam Optional<String> chinese,
			@ApiParam(name = "type", value = "类型是1 则为全拼，否则为首字母") @RequestParam Optional<Integer> type
			) throws AuthenticationException {
		String pinying = "";
		if (StringUtil.isEmpty(chinese.orElse(""))) {
			return new CommonResult<String>(true,"获取拼音成功！",pinying);
		}
		Integer ptype = type.orElse(1);	
		//如果是类型是1 则为全拼，否则为首字母
		if(ptype == 1){
			pinying = PinyinUtil.getPinyin(chinese.get());
		}else{
			pinying = PinyinUtil.getPinYinHeadChar(chinese.get());
		}
		return new CommonResult<String>(true,"获取拼音成功！",pinying);
	}
	
	@RequestMapping(value = "getPublicKey", method = RequestMethod.GET, produces={"application/json; charset=utf-8" })
	@ApiOperation(value = "获取RSA加密的公钥", httpMethod = "GET", notes = "获取RSA加密的公钥")
	public CommonResult<String> getPublicKey() throws Exception {
		return CommonResult.<String>ok().value(RSAUtil.publicKey);
	}
	
	@RequestMapping(value = "getPlatformVersion", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取平台版本", httpMethod = "GET", notes = "获取平台版本")
	public CommonResult<String> getPlatformVersion() {
		return new CommonResult<String>(true, "", platformVersion);
	}

	@RequestMapping(value = "encryptDbPassword", method = RequestMethod.POST, produces={"application/json; charset=utf-8" })
	@ApiOperation(value = "获取数据库密码加密字符串", httpMethod = "POST", notes = "获取数据库密码加密信息")
	public CommonResult<DruidEncryptDTO> encryptDbPassword(@ApiParam(required = true, name = "password", value = "待加密密码") @RequestBody Map<String,String> map) throws Exception {
        String[] arr = CryptoUtils.genKeyPair(512);
        DruidEncryptDTO druidEncryptDTO = new DruidEncryptDTO();
        druidEncryptDTO.setPassword(CryptoUtils.encrypt(arr[0], map.get("password")));
        druidEncryptDTO.setPublicKey(arr[1]);
		return new CommonResult<DruidEncryptDTO>(true,"获取拼音成功！",druidEncryptDTO);
	}
	
	
	@RequestMapping(value = "clearCacheByKey", method = RequestMethod.GET, produces={"application/json; charset=utf-8" })
	@ApiOperation(value = "根据缓存key清除缓存", httpMethod = "GET", notes = "根据缓存key清除缓存")
	public CommonResult<String> clearCacheByKey(@ApiParam(required = true, name = "key", value = "缓存key") @RequestParam String key) throws AuthenticationException {
		CacheManager manager = AppUtil.getBean(CacheManager.class);
		manager.clearCascadeByKey(key);
		return new CommonResult<String>(true,"操作成功！");
	}
	
	@RequestMapping(value = "getYmlValueByName", method = RequestMethod.GET, produces={"application/json; charset=utf-8" })
	@ApiOperation(value = "根据key获取value", httpMethod = "GET", notes = "多个逗号分隔")
	public CommonResult<Map<String,String>> getYmlValueByName(@ApiParam(required = true, name = "keys", value = "keys") @RequestParam String keys) throws AuthenticationException {
		Environment environment = AppUtil.getApplicaitonContext().getEnvironment();
		String[] split = keys.split(",");
		Map<String,String> map = new HashMap<String, String>();
		for (String key : split) {
			String value = environment.getProperty(key);
			map.put(key, value);
		}
		CommonResult<Map<String, String>> commonResult = new CommonResult<Map<String,String>>(true,"获取成功");
		commonResult.setValue(map);
		return commonResult;
	}

	
    @RequestMapping(value = "getCurrentDate", method = RequestMethod.GET, produces={"application/json; charset=utf-8" })
    @ApiOperation(value = "获取当前服务器的时间", httpMethod = "GET", notes = "获取当前服务器的时间")
    public CommonResult<String> getCurrentDate(@ApiParam(required = true, name = "valFormat", value = "日期格式") @RequestParam String valFormat) throws AuthenticationException {
        String date = DateUtil.getCurrentTime(valFormat);//取当前系统日期，并按指定格式或者是默认格式返回
        return new CommonResult<String>(true,"获取成功",date);
    }

    @RequestMapping(value = "getCurrentDbType", method = RequestMethod.GET, produces={"application/json; charset=utf-8" })
    @ApiOperation(value = "取得当前数据源的数据库类型", httpMethod = "GET", notes = "取得当前数据源的数据库类型")
    public CommonResult<String> getCurrentDbType() {
        return new CommonResult<String>(true,"获取成功", SQLUtil.getDbType());
    }
}