package com.hotent.portal.persistence.manager.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.Base64;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.StringUtil;
import com.hotent.base.util.UniqueIdUtil;
import com.hotent.portal.model.PortalMeetingConfig;
import com.hotent.portal.model.SysIndexColumn;
import com.hotent.portal.persistence.dao.PortalMeetingConfigDao;
import com.hotent.portal.persistence.manager.PortalMeetingConfigManager;
import com.hotent.portal.persistence.manager.SysIndexColumnManager;
import com.hotent.portal.persistence.manager.SysIndexNewsColumnManager;
import com.hotent.sys.persistence.manager.SysAuthUserManager;
import com.hotent.uc.api.impl.util.ContextUtil;
import com.hotent.uc.api.model.IUser;

/**
 * 会议室配置 服务实现类
 *
 * @company 广州宏天软件股份有限公司
 * @author zhangxw
 * @email zhangxw@jee-soft.cn
 * @since 2021-06-30
 */
@Service
public class PortalMeetingConfigManagerImpl extends BaseManagerImpl<PortalMeetingConfigDao, PortalMeetingConfig> implements PortalMeetingConfigManager {

	@Autowired
	SysIndexColumnManager sysIndexColumnManager;
	@Resource
	SysIndexNewsColumnManager sysIndexNewsColumnManager;
	@Resource
	SysAuthUserManager sysAuthUserManager;

	
	@Override
	public PortalMeetingConfig getConfig() {
		List<PortalMeetingConfig> configList = super.getAll();
    	if(BeanUtils.isNotEmpty(configList)) {
    		return configList.get(0);
    	}
        return new PortalMeetingConfig();
	}
	
	@Override
	public CommonResult<String> issue(String id) throws Exception {
		SysIndexColumn sysIndexColumn=new SysIndexColumn();
		PortalMeetingConfig portalMeetingConfig = this.get(id);
		//判断是不是已经发布了
		String columnId = portalMeetingConfig.getColumnId();
		if(StringUtil.isNotEmpty(columnId)){
			SysIndexColumn sysIndexColumn2 = sysIndexColumnManager.get(columnId);
			if(BeanUtils.isNotEmpty(sysIndexColumn2)){
				return new CommonResult<String>(false,"已发布为首页栏目,无法再次发布!");
			}
		}
		sysIndexColumn.setAlias("meetingRoomApply");
		sysIndexColumn.setCatalog("31081");
		sysIndexColumn.setColHeight(470L);
		sysIndexColumn.setColType((short)0);
		sysIndexColumn.setIsPublic((short)2);
		sysIndexColumn.setNeedPage((short)0);
		sysIndexColumn.setCatalogName("默认分类");
		String suid = UniqueIdUtil.getSuid();
		sysIndexColumn.setId(suid);
		sysIndexColumn.setName("会议室预定");
		String template;
		template="<meeting-room />";
		String templateHtml = Base64.getBase64(template);
		sysIndexColumn.setTemplateHtml(templateHtml);
		sysIndexColumnManager.create(sysIndexColumn);
		portalMeetingConfig.setColumnId(suid);
		this.update(portalMeetingConfig);
		//发布成功后，将授权设置为所有人
		String indexColumnId = sysIndexColumn.getId();
		String objType = "indexColumn";
		String ownerNameJson = "[{\"type\":\"everyone\",\"title\":\"所有人\",\"checked\":true}]";
		sysAuthUserManager.saveRights(indexColumnId,objType,ownerNameJson);
		IUser currentUser = ContextUtil.getCurrentUser();
		sysAuthUserManager.delUserMenuCache(currentUser.getUserId());
		return new CommonResult<>();
	}
	
}
