package taskmanagement import ( "appPlatform/api/version1/customerform" "appPlatform/api/version1/workWechat" "appPlatform/models/modelshr" "appPlatform/overall" "appPlatform/overall/publicmethod" "encoding/json" "fmt" "strconv" "strings" "time" ) /* * @ 作者: 秦东 @ 时间: 2025-03-28 09:17:29 @ 功能: 发送企业微信消息 @ 参数 #sendConfig //发送消息配置信息 #msgTitle //消息标题 #cureeTime //创建时间 #fieldVal //提交得数据已表字段 #subUnit //表单组件属性 @ 返回值 #msgid 消息id,用于撤回应用消息 #err 信息 @ 方法原型 # */ func (s *SendMsgInfo) SendMsg(userCont modelshr.ManCont, msgTitle string, uuid, cureeTime int64, fieldVal, masrWriteMap map[string]interface{}, subUnit map[string]customerform.MasterStruct) (msgid string, err error) { if s.Open { //开启发送信息 var sendMsg workWechat.SendMessage sendUserCount := 0 var sendUserAll []string isSend := true //Step 1: 获取消息的发送范围 switch s.SendRange { case 1: //全集团 sendMsg.Touser = "@all" case 2: //本公司 var myCompany modelshr.AdministrativeOrganization myCompany.GetCont(map[string]interface{}{"id": userCont.Company}, "`wechat_organization_id`") if myCompany.WechatOrganizationId != 0 { sendMsg.Toparty = strconv.FormatInt(myCompany.WechatOrganizationId, 10) } else { if userCont.Company != 0 { //获取该组织得所以下级 sendUserAll = GainOrgManWechat(userCont.Company) sendUserCount = len(sendUserAll) if sendUserCount <= 1000 && sendUserCount > 0 { sendMsg.Touser = strings.Join(sendUserAll, "|") } else { if sendUserCount == 0 { isSend = false } } // sendMsg.Touser = } else { if userCont.WorkWechat != "" { sendMsg.Touser = userCont.WorkWechat } else if userCont.Wechat != "" { sendMsg.Touser = userCont.Wechat } else { sendMsg.Touser = "@all" } } } case 3: //本部门 if userCont.MainDeparment != 0 { var myCompany modelshr.AdministrativeOrganization myCompany.GetCont(map[string]interface{}{"id": userCont.MainDeparment}, "`wechat_organization_id`") if myCompany.WechatOrganizationId != 0 { sendMsg.Toparty = strconv.FormatInt(myCompany.WechatOrganizationId, 10) } else { //获取该组织得所以下级 sendUserAll = GainOrgManWechat(userCont.MainDeparment) sendUserCount = len(sendUserAll) if sendUserCount <= 1000 && sendUserCount > 0 { sendMsg.Touser = strings.Join(sendUserAll, "|") } else { if sendUserCount == 0 { isSend = false } } } } else { if userCont.Wechat == "" && userCont.WorkWechat == "" { isSend = false } else { if userCont.WorkWechat != "" { sendMsg.Touser = userCont.WorkWechat } else { sendMsg.Touser = userCont.Wechat } } } case 4: //本行政组织 if userCont.AdminOrg != 0 { var myCompany modelshr.AdministrativeOrganization myCompany.GetCont(map[string]interface{}{"id": userCont.AdminOrg}, "`wechat_organization_id`") if myCompany.WechatOrganizationId != 0 { sendMsg.Toparty = strconv.FormatInt(myCompany.WechatOrganizationId, 10) } else { //获取该组织得所以下级 sendUserAll = GainOrgManWechat(userCont.AdminOrg) sendUserCount = len(sendUserAll) if sendUserCount <= 1000 && sendUserCount > 0 { sendMsg.Touser = strings.Join(sendUserAll, "|") } else { if sendUserCount == 0 { isSend = false } } } } else { if userCont.Wechat == "" && userCont.WorkWechat == "" { isSend = false } else { if userCont.WorkWechat != "" { sendMsg.Touser = userCont.WorkWechat } else { sendMsg.Touser = userCont.Wechat } } } case 5: //表格中指定 sendUserAll = s.TableFieldsSendMsg(fieldVal, userCont) sendUserCount = len(sendUserAll) if sendUserCount <= 1000 && sendUserCount > 0 { sendMsg.Touser = strings.Join(sendUserAll, "|") } else { if sendUserCount == 0 { isSend = false } } case 6: //自定义 sendUserAll = s.CustomizeSendMsg() sendUserCount = len(sendUserAll) if sendUserCount <= 1000 && sendUserCount > 0 { sendMsg.Touser = strings.Join(sendUserAll, "|") } else { if sendUserCount == 0 { isSend = false } } default: isSend = false } if isSend { switch s.Types { case "textcard": sendMsg.Msgtype = "textcard" sendMsg.Textcard.Title = s.SendMsgInfo(s.Title, msgTitle, fieldVal, masrWriteMap, subUnit, 1) sendMsg.Textcard.Description = fmt.Sprintf("
%v
%v
请尽快阅读此信息
", publicmethod.UnixTimeToDay(time.Now().Unix(), 11), s.SendMsgInfo(s.Content, msgTitle, fieldVal, masrWriteMap, subUnit, 2)) sendMsg.Textcard.Url = fmt.Sprintf("%v/#/work_wechat?masters_key=%v", overall.CONSTANT_CONFIG.Appsetup.WebKpiUrl, uuid) sendMsg.Textcard.Btntxt = "前往查看" case "news": sendMsg.Msgtype = "news" //图文内容 var imgMsgInfo workWechat.ArticlesList imgMsgInfo.Title = s.SendMsgInfo(s.Title, msgTitle, fieldVal, masrWriteMap, subUnit, 1) imgMsgInfo.Description = s.SendMsgInfo(s.Content, msgTitle, fieldVal, masrWriteMap, subUnit, 2) imgMsgInfo.Url = fmt.Sprintf("%v/#/work_wechat?masters_key=%v", overall.CONSTANT_CONFIG.Appsetup.WebKpiUrl, uuid) imgMsgInfo.PicUrl = s.GainOneField(s.Icon, msgTitle, fieldVal, subUnit) sendMsg.NewsMsg.Articles = append(sendMsg.NewsMsg.Articles, imgMsgInfo) // sendMsg.NewsMsg.Articles = s.SendMsgInfo(s.Title, msgTitle, fieldVal, subUnit) default: sendMsg.Msgtype = "text" sendMsg.TextMsg.Content = s.SendMsgInfo(s.Content, msgTitle, fieldVal, masrWriteMap, subUnit, 2) } sendInitInfouser, _ := json.Marshal(sendUserAll) fmt.Printf("\n\n这是查看接收人信息-----》%v\n\n", string(sendInitInfouser)) sendInitInfo, _ := json.Marshal(sendMsg) fmt.Printf("\n\n这是查看输入信息-----》%v\n\n", string(sendInitInfo)) //判断当前人数是否曹组 if sendUserCount >= 1000 { fmt.Printf("\n\n--1---》%v\n\n", string(sendInitInfouser)) var msgidAry []string var msgToUser []string for i, v := range sendUserAll { if (i+1)%1000 == 0 { if len(msgToUser) > 0 { sendMsg.Touser = strings.Join(msgToUser, "|") msgidSte, errStr := sendMsg.SendMsg("stzl", 1) msgToUser = []string{} msgidAry = append(msgidAry, msgidSte) if errStr != nil { err = errStr } } } else { msgToUser = append(msgToUser, v) } } if len(msgToUser) > 0 { sendMsg.Touser = strings.Join(msgToUser, "|") msgidSte, errStr := sendMsg.SendMsg("stzl", 1) fmt.Printf("\n\n--3---》%v\n\n%v\n\n", string(sendInitInfouser), errStr) msgToUser = []string{} msgidAry = append(msgidAry, msgidSte) if errStr != nil { err = errStr } } if len(msgidAry) > 0 { msgid = strings.Join(msgidAry, ",") } } else { msgid, err = sendMsg.SendMsg("stzl", 1) fmt.Printf("\n\n--2---》%v\n\n%v\n\n", string(sendInitInfouser), err) } } } else { err = nil } return } /* * @ 作者: 秦东 @ 时间: 2025-06-06 13:21:24 @ 功能: @ 参数 #userCont 当前操作人 #uuid 任务唯一识别符 #cureeTime 创建时间 #msgTitle 默认消息标题 #fieldVal 数据表字段及对应值 #masrWriteMap 主表对应数据 #tableUnit 表单组件 @ 返回值 # @ 方法原型 # */ func (s *SendMsgInfo) NewSendMsg(userCont modelshr.ManCont, uuid, cureeTime int64, msgTitle string, fieldVal, masrWriteMap map[string]interface{}, tableUnit map[string]customerform.MasterStruct) (msgid string, err error) { //Step1:判断是否发起消息推送 if s.Open { //开启消息推送 var sendMsg workWechat.SendMessage //消息通用结构体 isSend := true var sendTyleList SendMsgNumber //判断是否具备发送消息 //Step2:获取消息发送范围 switch s.SendRange { case 1: //全集团 sendTyleList.Class = 1 sendTyleList.SendList = []string{"@all"} // sendMsg.Touser = "@all" case 2: //本公司 sendTyleList.Class = 2 var myCompany modelshr.AdministrativeOrganization //获取当前执行人的行政组织对应的企业微信ID myCompany.GetCont(map[string]interface{}{"id": userCont.Company}, "`wechat_organization_id`") if myCompany.WechatOrganizationId != 0 { //当企业行政组织微信ID存在时进行行政组织发送 // sendMsg.Toparty = strconv.FormatInt(myCompany.WechatOrganizationId, 10) sendTyleList.SendList = []string{strconv.FormatInt(myCompany.WechatOrganizationId, 10)} } else { if userCont.Company != 0 { //当前执行人存在公司ID时执行 var sunOrg publicmethod.GetOrgAllParent sunOrg.GetOrgSonAllId(userCont.Company) sunOrg.Id = append(sunOrg.Id, userCont.Company) var allOrgWorkId []int64 overall.CONSTANT_DB_HR.Model(&modelshr.AdministrativeOrganization{}).Select("`wechat_organization_id`").Where("`id` IN ?", sunOrg.Id).Find(&allOrgWorkId) if len(allOrgWorkId) > 0 { var watchId []string for _, v := range allOrgWorkId { if v != 0 { watchIdStr := strconv.FormatInt(v, 10) if !publicmethod.IsInTrue[string](watchIdStr, watchId) { watchId = append(watchId, watchIdStr) } } } if len(watchId) > 0 { var guoDu []string for i := 1; i < len(watchId); i++ { if i%100 == 0 { sendTyleList.SendList = append(sendTyleList.SendList, strings.Join(guoDu, "|")) guoDu = []string{} guoDu = append(guoDu, watchId[i-1]) } else { guoDu = append(guoDu, watchId[i-1]) } } if len(guoDu) > 0 { sendTyleList.SendList = append(sendTyleList.SendList, strings.Join(guoDu, "|")) guoDu = []string{} } // sendMsg.Toparty = strings.Join(watchId, "|") } else { isSend = false } } else { isSend = false } } else { //不存在时 isSend = false } } case 3: //本部门 sendTyleList.Class = 3 if userCont.MainDeparment != 0 { var myCompany modelshr.AdministrativeOrganization myCompany.GetCont(map[string]interface{}{"id": userCont.MainDeparment}, "`wechat_organization_id`") if myCompany.WechatOrganizationId != 0 { //当企业行政组织微信ID存在时进行行政组织发送 // sendMsg.Toparty = strconv.FormatInt(myCompany.WechatOrganizationId, 10) sendTyleList.SendList = []string{strconv.FormatInt(myCompany.WechatOrganizationId, 10)} } else { var sunOrg publicmethod.GetOrgAllParent sunOrg.GetOrgSonAllId(userCont.MainDeparment) sunOrg.Id = append(sunOrg.Id, userCont.MainDeparment) var allOrgWorkId []int64 overall.CONSTANT_DB_HR.Model(&modelshr.AdministrativeOrganization{}).Select("`wechat_organization_id`").Where("`id` IN ?", sunOrg.Id).Find(&allOrgWorkId) if len(allOrgWorkId) > 0 { var watchId []string for _, v := range allOrgWorkId { if v != 0 { watchIdStr := strconv.FormatInt(v, 10) if !publicmethod.IsInTrue[string](watchIdStr, watchId) { watchId = append(watchId, watchIdStr) } } } if len(watchId) > 0 { var guoDu []string for i := 1; i < len(watchId); i++ { if i%100 == 0 { sendTyleList.SendList = append(sendTyleList.SendList, strings.Join(guoDu, "|")) guoDu = []string{} guoDu = append(guoDu, watchId[i-1]) } else { guoDu = append(guoDu, watchId[i-1]) } } if len(guoDu) > 0 { sendTyleList.SendList = append(sendTyleList.SendList, strings.Join(guoDu, "|")) guoDu = []string{} } // sendMsg.Toparty = strings.Join(watchId, "|") } else { isSend = false } } else { isSend = false } } } else { isSend = false } case 4: //本行政组织 sendTyleList.Class = 4 if userCont.AdminOrg != 0 { var myCompany modelshr.AdministrativeOrganization myCompany.GetCont(map[string]interface{}{"id": userCont.AdminOrg}, "`wechat_organization_id`") if myCompany.WechatOrganizationId != 0 { sendTyleList.SendList = []string{strconv.FormatInt(myCompany.WechatOrganizationId, 10)} } else { var sunOrg publicmethod.GetOrgAllParent sunOrg.GetOrgSonAllId(userCont.AdminOrg) sunOrg.Id = append(sunOrg.Id, userCont.AdminOrg) var allOrgWorkId []int64 overall.CONSTANT_DB_HR.Model(&modelshr.AdministrativeOrganization{}).Select("`wechat_organization_id`").Where("`id` IN ?", sunOrg.Id).Find(&allOrgWorkId) if len(allOrgWorkId) > 0 { var watchId []string for _, v := range allOrgWorkId { if v != 0 { watchIdStr := strconv.FormatInt(v, 10) if !publicmethod.IsInTrue[string](watchIdStr, watchId) { watchId = append(watchId, watchIdStr) } } } if len(watchId) > 0 { var guoDu []string for i := 1; i < len(watchId); i++ { if i%100 == 0 { sendTyleList.SendList = append(sendTyleList.SendList, strings.Join(guoDu, "|")) guoDu = []string{} guoDu = append(guoDu, watchId[i-1]) } else { guoDu = append(guoDu, watchId[i-1]) } } if len(guoDu) > 0 { sendTyleList.SendList = append(sendTyleList.SendList, strings.Join(guoDu, "|")) guoDu = []string{} } // sendMsg.Toparty = strings.Join(watchId, "|") } else { isSend = false } } else { isSend = false } } } else { isSend = false } case 5: //表格中指定 sendTyleList.Class = 5 sendUserAll := s.TableFieldsSendMsg(fieldVal, userCont) sendUserCount := len(sendUserAll) if sendUserCount > 0 { var guoDu []string for i := 1; i < sendUserCount; i++ { if i%100 == 0 { sendTyleList.SendList = append(sendTyleList.SendList, strings.Join(guoDu, "|")) guoDu = []string{} guoDu = append(guoDu, sendUserAll[i-1]) } else { guoDu = append(guoDu, sendUserAll[i-1]) } } if len(guoDu) > 0 { sendTyleList.SendList = append(sendTyleList.SendList, strings.Join(guoDu, "|")) guoDu = []string{} } } else { isSend = false } case 6: //自定义 sendTyleList.Class = 6 sendUserAll := s.CustomizeSendMsg() sendUserCount := len(sendUserAll) if sendUserCount > 0 { var guoDu []string for i := 1; i < sendUserCount; i++ { if i%100 == 0 { sendTyleList.SendList = append(sendTyleList.SendList, strings.Join(guoDu, "|")) guoDu = []string{} guoDu = append(guoDu, sendUserAll[i-1]) } else { guoDu = append(guoDu, sendUserAll[i-1]) } } if len(guoDu) > 0 { sendTyleList.SendList = append(sendTyleList.SendList, strings.Join(guoDu, "|")) guoDu = []string{} } } else { isSend = false } default: isSend = false } //Step3:判断是否具备发送消息条件 if isSend { //Step4:组装发射内容 switch s.Types { case "textcard": sendMsg.Msgtype = "textcard" sendMsg.Textcard.Title = s.SendMsgInfo(s.Title, msgTitle, fieldVal, masrWriteMap, tableUnit, 1) sendMsg.Textcard.Description = fmt.Sprintf("
%v
%v
请尽快阅读此信息
", publicmethod.UnixTimeToDay(time.Now().Unix(), 11), s.SendMsgInfo(s.Content, msgTitle, fieldVal, masrWriteMap, tableUnit, 2)) sendMsg.Textcard.Url = fmt.Sprintf("%v/#/work_wechat?masters_key=%v", overall.CONSTANT_CONFIG.Appsetup.WebKpiUrl, uuid) sendMsg.Textcard.Btntxt = "前往查看" case "news": sendMsg.Msgtype = "news" //图文内容 var imgMsgInfo workWechat.ArticlesList imgMsgInfo.Title = s.SendMsgInfo(s.Title, msgTitle, fieldVal, masrWriteMap, tableUnit, 1) imgMsgInfo.Description = s.SendMsgInfo(s.Content, msgTitle, fieldVal, masrWriteMap, tableUnit, 2) imgMsgInfo.Url = fmt.Sprintf("%v/#/work_wechat?masters_key=%v", overall.CONSTANT_CONFIG.Appsetup.WebKpiUrl, uuid) imgMsgInfo.PicUrl = s.GainOneField(s.Icon, msgTitle, fieldVal, tableUnit) sendMsg.NewsMsg.Articles = append(sendMsg.NewsMsg.Articles, imgMsgInfo) default: sendMsg.Msgtype = "text" sendMsg.TextMsg.Content = s.SendMsgInfo(s.Content, msgTitle, fieldVal, masrWriteMap, tableUnit, 2) } //Step5:组装发射人员或行政组织 var msgidAry []string switch sendTyleList.Class { case 1: for _, v := range sendTyleList.SendList { sendMsg.Touser = v msgidSte, errStr := sendMsg.SendMsg("stzl", 1) msgidAry = append(msgidAry, msgidSte) if errStr != nil { err = errStr } } case 2: for _, v := range sendTyleList.SendList { sendMsg.Toparty = v msgidSte, errStr := sendMsg.SendMsg("stzl", 1) msgidAry = append(msgidAry, msgidSte) if errStr != nil { err = errStr } } case 3: for _, v := range sendTyleList.SendList { sendMsg.Toparty = v msgidSte, errStr := sendMsg.SendMsg("stzl", 1) msgidAry = append(msgidAry, msgidSte) if errStr != nil { err = errStr } } case 4: for _, v := range sendTyleList.SendList { sendMsg.Toparty = v msgidSte, errStr := sendMsg.SendMsg("stzl", 1) msgidAry = append(msgidAry, msgidSte) if errStr != nil { err = errStr } } case 5: for _, v := range sendTyleList.SendList { sendMsg.Touser = v msgidSte, errStr := sendMsg.SendMsg("stzl", 1) msgidAry = append(msgidAry, msgidSte) if errStr != nil { err = errStr } } case 6: for _, v := range sendTyleList.SendList { sendMsg.Touser = v msgidSte, errStr := sendMsg.SendMsg("stzl", 1) msgidAry = append(msgidAry, msgidSte) if errStr != nil { err = errStr } } default: } if len(msgidAry) > 0 { msgid = strings.Join(msgidAry, ",") } } } else { //不开启推送消息 err = nil } return }