package com.hotent.portal.controller;


import java.util.List;

import javax.annotation.Resource;

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.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.controller.BaseController;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.BeanUtils;
import com.hotent.portal.model.PortalNewsTag;
import com.hotent.portal.persistence.manager.PortalNewsTagManager;

import dm.jdbc.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 新闻公告标签 前端控制器
 *
 * @company 广州宏天软件股份有限公司
 * @author zhangxw
 * @email zhangxw@jee-soft.cn
 * @since 2021-06-17
 */
@RestController
@Api(tags="新闻公告标签管理")
@ApiGroup(group= {ApiGroupConsts.GROUP_PORTAL})
@RequestMapping("/portal/portalNewsTag/v1/")
public class PortalNewsTagController extends BaseController<PortalNewsTagManager, PortalNewsTag> {

	@Resource
	PortalNewsTagManager portalNewsTagManager;
	
	@RequestMapping(value="save",method= {RequestMethod.POST,RequestMethod.PUT}, produces = {"application/json; charset=utf-8" })
	@ApiOperation("添加标签")
	public CommonResult<String> save(@ApiParam(name="newsTag", value="标签信息") @RequestBody PortalNewsTag newsTag) {
		try {
			PortalNewsTag tag = portalNewsTagManager.getByAlias(newsTag.getAlias());
			if(BeanUtils.isNotEmpty(tag)) {
				if(StringUtil.isEmpty(newsTag.getId()) || (StringUtil.isNotEmpty(newsTag.getId()) && !tag.getId().equals(newsTag.getId()))) {
					return new CommonResult<String>(false, "保存标签失败：标签别名【"+newsTag.getAlias()+"】已存在。");
				}
			}
		} catch (Exception e) {
			return new CommonResult<String>(false, "保存标签失败："+e.getMessage());
		}
		if(StringUtil.isNotEmpty(newsTag.getId())){
			return super.updateById(newsTag);
		}else {
			return super.create(newsTag);
		}
	}
	
	@RequestMapping(value="portalNewsTag/isExist",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "查询标签别名是否已存在", httpMethod = "GET", notes = "查询标签别名是否已存在")
	public CommonResult<Boolean> isExist(@ApiParam(name="alias",value="标签别名")@RequestParam(required=true) String alias) throws Exception{
		PortalNewsTag tag = portalNewsTagManager.getByAlias(alias);
		if(BeanUtils.isEmpty(tag)) {
			return new CommonResult<Boolean>(false, "别名不存在。");
		}else {
			return new CommonResult<Boolean>(true, "别名已存在。");
		}
	}
	
	@RequestMapping(value="portalNewsTag/getByAlias",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "查询标签别名是否已存在", httpMethod = "GET", notes = "查询标签别名是否已存在")
	public CommonResult<PortalNewsTag> getByAlias(@ApiParam(name="alias",value="标签别名")@RequestParam(required=true) String alias) throws Exception{
		PortalNewsTag tag = portalNewsTagManager.getByAlias(alias);
		if(BeanUtils.isEmpty(tag)) {
			return new CommonResult<PortalNewsTag>(false, "别名不存在。");
		}
		return new CommonResult<PortalNewsTag>(true, "获取成功。", tag);
	}
	
	@RequestMapping(value="getAll",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取所有标签", httpMethod = "GET", notes = "获取所有标签")
	public List<PortalNewsTag> getAll() throws Exception{
		return portalNewsTagManager.getAll();
	}
}
