package enterprise_msg

import (
	enterprise "git.rosy.net.cn/baseapi/platformapi/enterprise_wechat"
	"git.rosy.net.cn/jx-callback/globals"
	"git.rosy.net.cn/jx-callback/globals/api"
)

// 取不到写死的
var enterpriseUserIdMap = map[string]string{
	"18080188338":        "ShiFeng",
	"18048531223":        "ShiFeng2",
	"17208297174":        "ciEnCheng",
	"JingXiCaiShi-Jiang": "15680515506",
	"18982250714":        "LaoZhaoTongXue",
	"15680070110":        "LiRongWei",
	"17366937206":        "LuGang",
	"18780171617":        "TianQinXin",
	"17381914617":        "TianQinXin2",
	"15183779263":        "FengCheng",
	"18011597879":        "YunYing7879",
	"13608076295":        "ZhangJiaLin",
	"18981810340":        "LiuLei",
	"18744776542":        "WuTingQi",
	"15928865396":        "HeJiaMeng2",
	"15881105234":        "WenShiQi",
	"13547995434":        "LiJun",
}

// SendUserMessage 发送文本卡片
func SendUserMessage(phone, title, description, url string) error {
	// 根据电话号码获取用户touserId
	var enterpriseUserId = ""
	var err error = nil
	if id, ok := enterpriseUserIdMap[phone]; ok {
		enterpriseUserId = id
	} else {
		userList, err := api.EnterpriseChatMin.GetEnterpriseStaffInfo(enterprise.ParentDepartmentId)
		if err != nil {
			globals.SugarLogger.Errorf("获取企业微信全职人员信息错误 : %v", err)
			return err
		}
		for _, v := range userList {
			//if v.Mobile == "" {
			//	api.EnterpriseChatMin.SendMsgToUserTypeText(&enterprise.SendTextMsgReq{
			//		Touser:  "ShiFeng",
			//		Msgtype: enterprise.MsgTypeText,
			//		Agentid: enterprise.EnterpriseAgentid,
			//		Text: struct {
			//			Content string `json:"content"`
			//		}{Content: fmt.Sprintf("用户[%s]未添加企业微信联系电话,请联系添加电话号码[企业微信联系电话与京西系统保持一致!]", v.Name)},
			//		Safe:                   0,
			//		EnableIdTrans:          0,
			//		EnableDuplicateCheck:   0,
			//		DuplicateCheckInterval: 0,
			//	})
			//	continue
			//}
			if v.Mobile == phone {
				enterpriseUserId = v.Userid
			}
			enterpriseUserIdMap[v.Mobile] = v.Userid
		}
	}

	err = api.EnterpriseChatMin.SendMsgToUserTypeText(&enterprise.SendTextMsgReq{
		Touser:  enterpriseUserId,
		Msgtype: enterprise.MsgTypeText,
		Agentid: enterprise.EnterpriseAgentid,
		Text: struct {
			Content string `json:"content"`
		}{Content: title + ":" + description},
		Safe:                   0,
		EnableIdTrans:          0,
		EnableDuplicateCheck:   0,
		DuplicateCheckInterval: 0,
	})
	return err
}

func SendEnterpriseUserMessage(msgType, phone, title, content string) (err error) {
	// 根据电话号码获取用户touserId
	enterpriseUserId, err := api.EnterpriseChatMin.GetUserIdByMobile(phone)
	if err != nil {
		globals.SugarLogger.Errorf("SendEnterpriseUserMessage err1 %v", err)
		return err
	}
	if msgType == enterprise.MsgTypeText {
		err = api.EnterpriseChatMin.SendMsgToUserTypeText(&enterprise.SendTextMsgReq{
			Touser:  enterpriseUserId,
			Msgtype: enterprise.MsgTypeText,
			Agentid: enterprise.EnterpriseAgentid,
			Text: struct {
				Content string `json:"content"`
			}{Content: title + ":" + content},
			Safe:                   0,
			EnableIdTrans:          0,
			EnableDuplicateCheck:   0,
			DuplicateCheckInterval: 0,
		})
	} else if msgType == enterprise.MsgTypeMarkdown {
		err = api.EnterpriseChatMin.SendMsgToUserMarkdown(&enterprise.SendMarkdownMsgReq{
			Touser:  enterpriseUserId,
			Msgtype: enterprise.MsgTypeMarkdown,
			Agentid: enterprise.EnterpriseAgentid,
			Markdown: struct {
				Content string `json:"content"`
			}{Content: title + ":" + content},
			Safe:                   0,
			EnableIdTrans:          0,
			EnableDuplicateCheck:   0,
			DuplicateCheckInterval: 0,
		})
	}
	globals.SugarLogger.Errorf("SendEnterpriseUserMessage err2 %v", err)
	return err
}
