package com.hotent.portal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hotent.base.conf.SaaSConfig;
import com.hotent.base.context.BaseContext;
import com.hotent.base.controller.BaseController;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.StringUtil;
import com.hotent.portal.model.ShorturlManage;
import com.hotent.portal.persistence.manager.ShorturlManageManager;

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

/**
 * 前端控制器
 *
 * @company 广州宏天软件股份有限公司
 * @author 超级管理员
 * @since 2020-12-01
 */
@RestController
@RequestMapping("/portal/shorturlManage/v1/")
public class ShorturlManageController extends BaseController<ShorturlManageManager, ShorturlManage> {
	@Autowired
	ShorturlManageManager shorturlManageManager;
	@Autowired
	SaaSConfig saaSConfig;
	@Autowired
	BaseContext baseContext;


	@GetMapping("/getLongUrlByShortUrl")
	@ApiOperation(value = "根据短连接获取短连接配置相关信息", httpMethod = "GET", notes = "根据短连接获取短连接配置相关信息")
	public CommonResult<ShorturlManage> getLongUrlByShortUrl(
			@ApiParam(name = "shortUrl", value = "用户账号") @RequestParam(required = true) String shortUrl)
			throws Exception {
		CommonResult<ShorturlManage> res=shorturlManageManager.getLongUrlByShortUrl(shortUrl);
		return res;
	}

	/**
	 * 根据长连接获取短连接，长链接已存在记录，则直接返回已有配置。不存在则生成新的短链接并保存
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/getShortUrlByLong")
	@ApiOperation(value = "根据长连接获取短连接，长链接已存在记录，则直接返回已有配置。不存在则生成新的短链接并保存", httpMethod = "POST", notes = "更新短链接对象")
	public CommonResult<ShorturlManage> getShortUrlByLong(@ApiParam(name = "getShortUrlByLong", value = "短链接对象", required = true) @RequestBody ShorturlManage vo)throws Exception {
		CommonResult<ShorturlManage> res=shorturlManageManager.getShortUrlByLong(vo);
		return res;
	}
	
	/**
	 * 更新链接配置对象
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/updateShorturlManage")
	@ApiOperation(value = "更新链接配置对象 ", httpMethod = "POST", notes = "更新链接配置对象")
	public CommonResult<ShorturlManage> updateShorturlManage(
			@ApiParam(name = "shorturlManage", value = "短链接对象", required = true) @RequestBody ShorturlManage vo)
			throws Exception {
		CommonResult<ShorturlManage> rtn = null;
		try {
			if (saaSConfig.isEnable() && StringUtil.isEmpty(vo.getTenantId())) {
				String currentTenantId = baseContext.getCurrentTenantId();
				Assert.isTrue(StringUtil.isNotEmpty(currentTenantId), "当前租户ID为空");
				vo.setTenantId(currentTenantId);
			}
			baseService.update(vo);
			rtn = new CommonResult<ShorturlManage>(true, "更新成功！",vo);
		} catch (Exception e) {
			e.printStackTrace();
			rtn = new CommonResult<ShorturlManage>(false, "更新失败!",null);
		}
		return rtn;
	}
}
