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

import com.hotent.base.model.CommonResult;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.query.QueryOP;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.StringUtil;
import com.hotent.base.util.UniqueIdUtil;
import com.hotent.uc.api.impl.util.ContextUtil;
import com.hotent.uc.api.model.IGroup;
import org.springframework.stereotype.Service;

import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.portal.model.MsgTemplate;
import com.hotent.portal.persistence.dao.MsgTemplateDao;
import com.hotent.portal.persistence.manager.MsgTemplateManager;

/**
 * 对象功能:消息模版 Manager
 * @author zhaoxy
 * @company 广州宏天软件股份有限公司
 * @email zhxy@jee-soft.cn
 * @date 2018-06-06 14:20
 */
@Service("msgTemplateManager")
public class MsgTemplateManagerImpl extends BaseManagerImpl<MsgTemplateDao, MsgTemplate> implements MsgTemplateManager {

	@Override
	public MsgTemplate getByKey(String templateKey){
		return baseMapper.getByKey(templateKey);
	}

	@Override
	public MsgTemplate getDefault(String typeKey){
		return baseMapper.getDefault(typeKey);
	}

	@Override
	public void setDefault(String id) {
		MsgTemplate MsgTemplate = this.get(id);
		String typeKey = MsgTemplate.getTypeKey();
		baseMapper.setNotDefaultByType(typeKey);
		baseMapper.setDefault(id);
	}

	@Override
	public boolean isExistByKeyAndTypeKey(String key, String typeKey) {
		return baseMapper.isExistByKeyAndTypeKey(key,typeKey);
	}

	@Override
	public void setNotDefault(String id) {
		baseMapper.setNotDefaultById(id);
	}

	@Override
	public CommonResult<String> saveMsgTemplate(MsgTemplate msgTemplate) throws Exception {
		String resultMsg=null;
		String currentUserId = ContextUtil.getCurrentUserId();
		IGroup group =ContextUtil.getCurrentGroup();
		String currentGroupId = "0";
		if(group!= null){
			currentGroupId = group.getGroupId();
		}
		boolean isExist=false;
		if(StringUtil.isEmpty(msgTemplate.getId())){
			QueryFilter queryFilter=QueryFilter.build().withDefaultPage();
			queryFilter.addFilter("KEY_", msgTemplate.getKey(), QueryOP.EQUAL);
			queryFilter.addFilter("TYPE_KEY_", msgTemplate.getTypeKey(), QueryOP.EQUAL);
			PageList<MsgTemplate> query = this.query(queryFilter);
			isExist=query.getRows().size()>0;
			if (isExist) {
				resultMsg="消息模版业务键已经存在,添加失败!";
			}else {
				msgTemplate.setId(UniqueIdUtil.getSuid());
				msgTemplate.setCreateBy(currentUserId);
				msgTemplate.setCreateOrgId(currentGroupId);
				this.create(msgTemplate);
				resultMsg="添加消息模版成功";
			}
		}else{
			MsgTemplate sysMsgTemplateTemp = this.get(msgTemplate.getId());
			if (BeanUtils.isNotEmpty(sysMsgTemplateTemp)) {
				if(!sysMsgTemplateTemp.getKey().equals(msgTemplate.getKey())){
					isExist = this.isExistByKeyAndTypeKey(msgTemplate.getKey(),msgTemplate.getTypeKey());
				}
			}else {
				isExist = this.isExistByKeyAndTypeKey(msgTemplate.getKey(),msgTemplate.getTypeKey());
			}
			if (isExist) {
				resultMsg="消息模版业务键已经存在,更新失败!";
			}else {
				msgTemplate.setUpdateBy(currentUserId);
				this.update(msgTemplate);
				resultMsg="更新消息模版成功";
			}
		}
		return new CommonResult<String>(resultMsg);
	}
}
