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.PortalMeetingRoom;
import com.hotent.portal.persistence.manager.PortalMeetingRoomManager;

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-25
 */
@RestController
@Api(tags="会议室资源管理")
@ApiGroup(group= {ApiGroupConsts.GROUP_PORTAL})
@RequestMapping("/portalMeetingRoom/v1/")
public class PortalMeetingRoomController extends BaseController<PortalMeetingRoomManager, PortalMeetingRoom> {
	@Resource
	PortalMeetingRoomManager portalMeetingRoomManager;
	
	@RequestMapping(value="save",method= {RequestMethod.POST,RequestMethod.PUT}, produces = {"application/json; charset=utf-8" })
	@ApiOperation("添加会议室")
	public CommonResult<String> save(@ApiParam(name="newsTag", value="会议室信息") @RequestBody PortalMeetingRoom newsTag) {
		try {
			PortalMeetingRoom tag = portalMeetingRoomManager.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="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{
		PortalMeetingRoom tag = portalMeetingRoomManager.getByAlias(alias);
		if(BeanUtils.isEmpty(tag)) {
			return new CommonResult<Boolean>(false, "别名不存在。");
		}else {
			return new CommonResult<Boolean>(true, "别名已存在。");
		}
	}
	
	@RequestMapping(value="getByAlias",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "查询会议室别名是否已存在", httpMethod = "GET", notes = "查询会议室别名是否已存在")
	public CommonResult<PortalMeetingRoom> getByAlias(@ApiParam(name="alias",value="会议室别名")@RequestParam(required=true) String alias) throws Exception{
		PortalMeetingRoom tag = portalMeetingRoomManager.getByAlias(alias);
		if(BeanUtils.isEmpty(tag)) {
			return new CommonResult<PortalMeetingRoom>(false, "别名不存在。");
		}
		return new CommonResult<PortalMeetingRoom>(true, "获取成功。", tag);
	}
	
	@RequestMapping(value="getAll",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取所有会议室", httpMethod = "GET", notes = "获取所有会议室")
	public List<PortalMeetingRoom> getAll() throws Exception{
		return portalMeetingRoomManager.getAll();
	}
}
