package com.engine.meeting.cmd.meetingvideo;

import com.alibaba.fastjson.JSONObject;
import com.api.meeting.util.MeetingPrmUtil;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.biz.SimpleBizLogger;
import com.engine.common.constant.BizLogType;
import com.engine.common.constant.ParamConstant;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.meeting.video.util.BizLogSmallType4VideoMeeting;
import weaver.meeting.video.util.VideoMeetingUtil;
import weaver.systeminfo.SystemEnv;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/*
 * 按钮显示逻辑
 * @author yp
 * @date 2020/4/28
 * @param
 * @return
 */
public class getVideoMeetingBtnCmd extends AbstractCommonCommand<Map<String, Object>> {

    private SimpleBizLogger logger;
    private BizLogContext bizLogContext;

    public getVideoMeetingBtnCmd(User user, Map<String, Object> params) {
        this.user = user;
        this.params = params;
        this.logger = new SimpleBizLogger();
        this.bizLogContext = new BizLogContext();
    }


    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        boolean canedit = false;
        Map ret = new HashMap();
        VideoMeetingUtil vmt = new VideoMeetingUtil();
        String frommould = Util.null2String(params.get("frommould"));
        String mouldkey = Util.null2String(params.get("mouldkey"));
        String _ec_os = Util.null2String(params.get("_ec_os"));//移动端os类型
        String ismobile = Util.null2String(params.get("ismobile"));//是否伟移动端请求
        RecordSet recordSet = new RecordSet();
        if("MEETING".equals(frommould)){//新视频会议应用，frommould和mouldkey分别是MobileVideoMeeting和id字段。修改一下sql
            recordSet.executeQuery("select * from meeting_videolist where videomtstatus in (0,1) and frommould = ? and mouldkey = ?", frommould, mouldkey);
        }else{
            recordSet.executeQuery("select * from meeting_videolist where videomtstatus in (0,1) and id = ?", mouldkey);
        }
        if (recordSet.next()) {
            JSONObject json = new JSONObject();
            json.put("sharetypename", SystemEnv.getHtmlLabelName(2103,user.getLanguage()));
            json.put("content",recordSet.getString("videomtname"));
            json.put("sysid","");
            json.put("desc",recordSet.getString("videomtname"));
            json.put("jcpiurl",recordSet.getString("videomtjoinurl"));
            json.put("canforword",0);
            json.put("opentype",1);
            json.put("opentype_pc",1);
            json.put("shareid",recordSet.getString("videomtid"));
            String general_pwd = "";
            //如果存在数据，看下是查看会议，还是加入会议
            if (Util.null2String(user.getUID()).equals(recordSet.getString("videomtcreater"))) {
                ret.put("type", "view");
                ret.put("joinurl", recordSet.getString("videomtstarturl"));
                ret.put("joinType",1);
                ret.put("mtid",recordSet.getString("videomtid"));
                ret.put("manufacturers",recordSet.getString("manufacturers"));
                ret.put("mttitle",recordSet.getString("videomtname"));
                if("huaweicloud".equals(recordSet.getString("manufacturers"))){
                    ret.put("mtpwd", JSONObject.parseObject(recordSet.getString("mtpassword")).getString("chair_pwd"));
                }
                if("xylink".equals(recordSet.getString("manufacturers"))){
                    ret.put("joinurl", vmt.getXylinkUrl(recordSet.getString("videomtid"),user.getUID()+"",JSONObject.parseObject(recordSet.getString("mtpassword")).getString("chair_pwd")));
                }
                if ("yealink".equals(recordSet.getString("manufacturers"))) {
                    ret.put("mtpwd", Util.null2String(recordSet.getString("mtpassword")));
                    ret.put("mtnum", recordSet.getString("oamtid"));
                    ret.put("phone", com.api.meeting.cusvideo.util.YealinkVideoUtil.getUserMobile(user.getUID()));
                }
            } else {
                ret.put("joinurl", recordSet.getString("videomtjoinurl"));
                ret.put("type", "join");
                ret.put("joinType",2);
                ret.put("mtid",recordSet.getString("videomtid"));
                ret.put("manufacturers",recordSet.getString("manufacturers"));
                ret.put("mttitle",recordSet.getString("videomtname"));
                if("huaweicloud".equals(recordSet.getString("manufacturers"))){
                    general_pwd = JSONObject.parseObject(recordSet.getString("mtpassword")).getString("general_pwd");
                    ret.put("mtpwd", general_pwd);
                }
                if("xylink".equals(recordSet.getString("manufacturers"))){
                    ret.put("joinurl", vmt.getXylinkUrl(recordSet.getString("videomtid"),user.getUID()+"",JSONObject.parseObject(recordSet.getString("mtpassword")).getString("general_pwd")));
                }else if("InfowareLab".equals(recordSet.getString("manufacturers")) && "1".equals(ismobile)){
                    ret.put("joinurl",vmt.getHSSLinkUrl(user.getUID()+"",_ec_os,recordSet.getString("videomtid"),recordSet.getString("mtpassword"),false));
                }
                if ("yealink".equals(recordSet.getString("manufacturers"))) {
                    ret.put("mtpwd", Util.null2String(recordSet.getString("mtpassword")));
                    ret.put("mtnum", recordSet.getString("oamtid"));
                    ret.put("phone", com.api.meeting.cusvideo.util.YealinkVideoUtil.getUserMobile(user.getUID()));
                }
            }
            json.put("linkurl","meetingid="+recordSet.getString("videomtid")+"&manufacturers="+recordSet.getString("manufacturers")+"&pwd="+general_pwd);
            ret.put("msgInfo",json);
            ret.put("msgType",10);
        } else {
            //如果不存在，那么就是新建会议
            //如果所属模块是会议，那么要根据创建人权限来展示的。
            if ("MEETING".equals(frommould)) {
                MeetingPrmUtil mpu = new MeetingPrmUtil(user, mouldkey);
                //不根据权限了。只要是这三个人，都可以
                if (mpu.getIscaller() || mpu.getIscontacter() || mpu.getIscreater()) {
                    ret.put("type", "create");
                }
            } else {
                ret.put("type", "create");
            }
        }
        boolean showBtn = false;
        String meetingid = Util.null2String(params.get("meetingid"));
        RecordSet rs2 = new RecordSet();
        rs2.executeQuery("select * from meeting where id = ?", meetingid);
        if (rs2.next() && "2".equals(rs2.getString("meetingstatus"))) {
            rs2.execute("SELECT * FROM meeting_videoset");
            String allowvideoMeeting = "";
            while (rs2.next()) {
                if ("allowvideoMeeting".equals(rs2.getString("keyname"))) {
                    allowvideoMeeting = Util.null2String(rs2.getString("keyvalue"));
                }
            }
            if ("1".equals(allowvideoMeeting)) {
                rs2.executeQuery("select * from meeting_videolist where videomtstatus in (0,1) and frommould = ? and mouldkey = ? ", "MEETING", meetingid);
                if (rs2.next()) {
                    showBtn = true;
                } else {
                    MeetingPrmUtil mpu = new MeetingPrmUtil(user, meetingid);
                    //不根据权限了。只要是这三个人，都可以
                    if (mpu.getIscaller() || mpu.getIscontacter() || mpu.getIscreater()) {
                        showBtn = true;
                    }
                }
            }
        }
        ret.put("isShowVideoBtn",showBtn);
        return ret;
    }

    /**
     * 日志操作记录
     */
    public void beforeLog() {
        bizLogContext.setDateObject(new Date());
        bizLogContext.setUserid(user.getUID());
        bizLogContext.setUsertype(Util.getIntValue(user.getLogintype()));
        bizLogContext.setTargetId("1");
        //bizLogContext.setTargetName("应用设置");
        bizLogContext.setLogType(BizLogType.MEETING_ENGINE);
        bizLogContext.setBelongType(BizLogSmallType4VideoMeeting.MEETING_VideMeeting);
        bizLogContext.setLogSmallType(BizLogSmallType4VideoMeeting.MEETING_VideMeeting);
        //bizLogContext.setBelongTypeTargetName(""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005423,weaver.general.ThreadVarLanguage.getLang())+"");
        bizLogContext.setParams(params);
        bizLogContext.setClientIp(Util.null2String(params.get(ParamConstant.PARAM_IP)));
        logger.setUser(user);//当前操作人
        logger.setParams(params);//request请求参数

        RecordSet recordSet = new RecordSet();
        String mainSql = "select * from meeting_videoset ";
        logger.setMainSql(mainSql, "id");

        //logger.setMainTargetNameColumn(""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005423,weaver.general.ThreadVarLanguage.getLang())+"");
        logger.before(bizLogContext);
    }

    /**
     * 生成logContext对象
     *
     * @return BizLogContext对象
     */
    @Override
    public BizLogContext getLogContext() {
        return logger.getBizLogContext();
    }
}
