
/**
 *
 */
package org.tio.sitexxx.im.common.bs.wx.group;

import lombok.Data;
import org.tio.sitexxx.service.model.main.*;
import org.tio.sitexxx.service.vo.Const;
import org.tio.sitexxx.service.vo.wx.*;
import org.tio.utils.SystemTimer;
import org.tio.utils.json.Json;

import java.io.Serializable;
import java.util.Objects;

/**
 * 新版群聊通知
 * @author xufei
 * 2020年2月13日 下午6:40:28
 */
@Data
public class WxGroupChatNtf implements Serializable {
	private static final long	serialVersionUID	= -7027325613034796001L;
	private String				c					= null;					//聊天内容
	private Long				g					= null;					//groupid
	private Short				d					= 1;					//主聊人员的设备类型（DeviceType），1：WS，2：安卓，3：IOS
	private Integer				f					= null;					//from的简写，聊天发起人信息
	private String				at					= null;					//被艾特的用户信息
	private Long				t					= SystemTimer.currTime;	//聊天消息时间
	private Short				ct					= null;					//WxGroupMsg.ContentType 1、普通文本消息，2、博客，3、文件，4、音频，5、视频，6：图片
	private Long				mid					= null;					//msg id 消息id，全局唯一
	private Short				sendbysys			= 2;					//是否是系统发出的消息，参见：Const.Sendbysys.YES，1：是，2或null：不是
	private Short				actflag				= 2;					//该消息是否是激活聊天消息
	private String				nick;										//发送方昵称
	private String				avatar;										//发送方头像
	private Long				chatlinkid;									//聊天列表id

	private String	sysmsgkey;	//系统消息模板key
	private String	opernick;	//系统消息操作者
	private String	tonicks;	//系统消息被操作者

	private Reference       r;     //引用内容
	private Video vc;			//视频内容
	private String			bc;			//博客内容
	private Audio ac;			//音频内容
	private File			fc;			//文件内容
	private Img				ic;			//图片内容
	private WxMsgCardVo cardc;		//名片内容
	private WxRedVo red;		//红包订单号
	private WxGroupApplyVo apply;		//红包订单号
	private WxTemplateMsgVo temp;		//模板消息
	private WxPositionVo position;		//位置消息
	private Short			msgfreeflag;

	private String	actname;					//激活时的名称
	private String	actavatar;					//激活时的头像
	private Short	joinnum;					//激活时告诉多少用户
	private Short	grouprole;					//激活时群的角色
	private Short	redflag	= Const.YesOrNo.NO;	//红包通知
    private String  forbiddenId;

	public void setForbiddenId(String forbiddenId) {
		this.forbiddenId = forbiddenId;
	}

	/**
	 * @param msg
	 * @return
	 * @author xufei
	 * 2020年2月13日 下午9:39:02
	 */
	public static WxGroupChatNtf from(WxGroupMsg msg, SysMsgVo sysMsgVo) {
		WxGroupChatNtf reslut = new WxGroupChatNtf();
		reslut.setNick(msg.getNick());
		reslut.setAvatar(msg.getAvatar());
		reslut.setC(msg.getText());
		reslut.setForbiddenId(msg.getSrctext());
		switch (msg.getContenttype()) {
		case Const.ContentType.TEXT:
			//保存缩略文字
			if (Objects.equals(msg.getSendbysys(), Const.YesOrNo.YES)) {
				if (sysMsgVo != null) {
					reslut.setOpernick(sysMsgVo.getOpernick());
					reslut.setSysmsgkey(sysMsgVo.getMsgkey());
					reslut.setTonicks(sysMsgVo.getTonicks());
				}
			}
			break;
		case Const.ContentType.AUDIO:
			Audio audio = Json.toBean(msg.getText(), Audio.class);
			//保存音频
			reslut.setAc(audio);
			break;
		case Const.ContentType.BLOG:
			//保存微博
			reslut.setBc(msg.getText());
			break;
		case Const.ContentType.FILE:
			File file = Json.toBean(msg.getText(), File.class);
			//保存文件
			reslut.setFc(file);
			break;
		case Const.ContentType.IMG:
			Img img = Json.toBean(msg.getText(), Img.class);
			//保存图片
			reslut.setIc(img);
			break;
		case Const.ContentType.VIDEO:
			Video video = Json.toBean(msg.getText(), Video.class);
			//保存视频
			reslut.setVc(video);
			break;
		case Const.ContentType.MSG_CARD:
			WxMsgCardVo cardVo = Json.toBean(msg.getText(), WxMsgCardVo.class);
			//保存名片
			reslut.setCardc(cardVo);
			break;
		case Const.ContentType.REDPACKET:
			WxRedVo redVo = Json.toBean(msg.getText(), WxRedVo.class);
			reslut.setRed(redVo);
			break;
		case Const.ContentType.GROUP_APPLY:
			WxGroupApplyVo applyVo = Json.toBean(msg.getText(), WxGroupApplyVo.class);
			reslut.setApply(applyVo);
			break;
		case Const.ContentType.TEMPLATE:
			WxTemplateMsgVo tempVo = Json.toBean(msg.getText(), WxTemplateMsgVo.class);
			reslut.setTemp(tempVo);
			break;
		case Const.ContentType.POSITION:
			WxPositionVo positionVo = Json.toBean(msg.getText(), WxPositionVo.class);
			reslut.setPosition(positionVo);
			break;
		case Const.ContentType.REFERENCE:
			Reference reference = Json.toBean(msg.getText(), Reference.class);
			reslut.setR(reference);
			break;
		default:
		}
		reslut.setCt(msg.getContenttype());
		reslut.setMid(msg.getId());
		reslut.setT(msg.getTime().getTime());
		reslut.setF(msg.getUid());
		reslut.setG(msg.getGroupid());
		reslut.setChatlinkid(-msg.getGroupid());
		reslut.setD((short) 0);
		reslut.setSendbysys(msg.getSendbysys());
		return reslut;
	}
}
