|
@@ -1 +1,202 @@
|
|
|
package customerService
|
|
|
+
|
|
|
+import (
|
|
|
+ "config"
|
|
|
+ "errors"
|
|
|
+ "log"
|
|
|
+ qutil "qfw/util"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "sync"
|
|
|
+ "task"
|
|
|
+ "time"
|
|
|
+ "util"
|
|
|
+)
|
|
|
+
|
|
|
+func CustomSendMsg(param *customMsg, sendStatus int, loginUserName string, loginUserId int) (int, error) {
|
|
|
+ var msgLogId string
|
|
|
+ if param.SendMode == 2 {
|
|
|
+ param.SendTime = time.Now().Format(qutil.Date_Full_Layout)
|
|
|
+ }
|
|
|
+ // 更新消息
|
|
|
+ if param.Id != 0 {
|
|
|
+ msgLogId = strconv.Itoa(param.Id)
|
|
|
+ ok := util.JysqlDB.Update("message_send_log", map[string]interface{}{"id": param.Id}, map[string]interface{}{
|
|
|
+ "send_usergroup_id": "",
|
|
|
+ "send_usergroup_name": "",
|
|
|
+ "receive_user_id": param.UserIds,
|
|
|
+ "msg_type": param.MsgType,
|
|
|
+ "title": param.Title,
|
|
|
+ "content": param.Content,
|
|
|
+ "send_mode": param.SendMode,
|
|
|
+ "send_time": param.SendTime,
|
|
|
+ "send_status": sendStatus,
|
|
|
+ "update_time": time.Now().Format(qutil.Date_Full_Layout),
|
|
|
+ "link": param.Link,
|
|
|
+ "isdel": 1,
|
|
|
+ "send_userid": loginUserId,
|
|
|
+ })
|
|
|
+ if !ok {
|
|
|
+ return 0, errors.New("发送消息出错")
|
|
|
+ }
|
|
|
+ if param.SendMode == 1 {
|
|
|
+ now := time.Now()
|
|
|
+ sendTime := param.SendTime
|
|
|
+ _, err := time.ParseInLocation(qutil.Date_Full_Layout, sendTime, now.Location())
|
|
|
+ if err != nil {
|
|
|
+ sendTime = param.SendTime + ":00"
|
|
|
+ }
|
|
|
+ key := strconv.Itoa(param.Id)
|
|
|
+ if _, t := util.OnTimeSendMap.Load(key + sendTime); !t {
|
|
|
+ sendTime2, _ := time.ParseInLocation(qutil.Date_Full_Layout, sendTime, now.Location())
|
|
|
+ execTime := sendTime2.Unix() - now.Unix()
|
|
|
+ log.Println(execTime, sendTime, now.Format(qutil.Date_Full_Layout), sendTime2.Unix(), now.Unix())
|
|
|
+ util.OnTimeSendMap.Store(key+sendTime, true)
|
|
|
+ time.AfterFunc(time.Duration(execTime)*time.Second, func() {
|
|
|
+ // 执行任务
|
|
|
+ log.Println("创建、修改添加定时任务")
|
|
|
+ log.Println(param.Id, sendTime)
|
|
|
+ task.Task(param.Id, sendTime, param.AndroidUrl, param.IosUrl)
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 1, nil
|
|
|
+ } else {
|
|
|
+ //保存发送的消息
|
|
|
+ log.Println("发送消息发送时间:", param.SendTime)
|
|
|
+ msgId := util.JysqlDB.Insert("message_send_log", map[string]interface{}{
|
|
|
+ "send_usergroup_id": "",
|
|
|
+ "send_usergroup_name": "",
|
|
|
+ // "receive_user_id": userIdStr,
|
|
|
+ "msg_type": param.MsgType,
|
|
|
+ "title": param.Title,
|
|
|
+ "content": param.Content,
|
|
|
+ "send_mode": param.SendMode,
|
|
|
+ "send_time": param.SendTime,
|
|
|
+ "send_status": sendStatus,
|
|
|
+ "update_time": time.Now().Format(qutil.Date_Full_Layout),
|
|
|
+ "createtime": time.Now().Format(qutil.Date_Full_Layout),
|
|
|
+ "link": param.Link,
|
|
|
+ "isdel": 1,
|
|
|
+ "send_userid": loginUserId,
|
|
|
+ })
|
|
|
+ msgLogId = strconv.FormatInt(msgId, 10)
|
|
|
+ //如果是定时发送
|
|
|
+ if param.SendMode == 1 {
|
|
|
+ if msgId > 0 {
|
|
|
+ msgId := qutil.IntAll(msgId)
|
|
|
+ sendTime, _ := time.ParseInLocation(qutil.Date_Full_Layout, param.SendTime+":00", time.Now().Location())
|
|
|
+ now := time.Now()
|
|
|
+ days := sendTime.Sub(now)
|
|
|
+ log.Println("时间:", days)
|
|
|
+ util.OnTimeSendMap.Store(strconv.Itoa(msgId)+param.SendTime+":00", true)
|
|
|
+ time.AfterFunc(days, func() {
|
|
|
+ // 执行任务
|
|
|
+ //log.Println("创建、修改添加定时任务")
|
|
|
+ log.Println(msgId, sendTime)
|
|
|
+ task.Task(msgId, sendTime.Format("2006-01-02 15:04:05"), param.AndroidUrl, param.IosUrl)
|
|
|
+ })
|
|
|
+ return 1, nil
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.Println("发送消息记录id*********", msgLogId)
|
|
|
+ //立即发送
|
|
|
+ if param.SendMode == 2 {
|
|
|
+ projectIdMap := sync.Map{}
|
|
|
+ orm := util.Tidb.NewSession()
|
|
|
+ err := orm.Begin()
|
|
|
+ if err != nil {
|
|
|
+ log.Println("简历数据库连接出错:", err)
|
|
|
+ }
|
|
|
+ log.Println("--------", sendStatus)
|
|
|
+ userNames := ""
|
|
|
+ userIds := ""
|
|
|
+ i := 0
|
|
|
+ msgType := strconv.Itoa(param.MsgType)
|
|
|
+ msg := map[string]interface{}{
|
|
|
+ "sendUserId": loginUserId,
|
|
|
+ "sendName": loginUserName,
|
|
|
+ "title": param.Title,
|
|
|
+ "content": param.Content,
|
|
|
+ "msgType": msgType,
|
|
|
+ "link": param.Link,
|
|
|
+ "appid": util.AppId,
|
|
|
+ "msgLogId": msgLogId,
|
|
|
+ }
|
|
|
+ userIdArr := strings.Split(param.UserIds, ",")
|
|
|
+ for _, val := range userIdArr {
|
|
|
+ i++
|
|
|
+ userId := val
|
|
|
+ if config.SysConfigs.UserIdMap[userId] != "" {
|
|
|
+ userId = config.SysConfigs.UserIdMap[userId]
|
|
|
+ }
|
|
|
+ if _, ok := projectIdMap.Load(userId); ok {
|
|
|
+ log.Println("########################已发送,本次跳过。。。。。。。。。。。。。", userId)
|
|
|
+ continue
|
|
|
+ } else {
|
|
|
+ projectIdMap.Store(userId, true)
|
|
|
+ }
|
|
|
+ userIds += userId + ","
|
|
|
+
|
|
|
+ //查询mongo库用户信息
|
|
|
+ userData := &map[string]interface{}{}
|
|
|
+ ok := false
|
|
|
+ var otherPushId, jgPushId, phoneType, name = "", "", "", ""
|
|
|
+ appVersion := ""
|
|
|
+ userData, ok = util.MQFW.FindById("user", userId, `"s_name":1,"s_opushid":1,"s_jpushid":1,"s_appponetype":1,"s_appversion":1`)
|
|
|
+ if userData != nil && len(*userData) > 0 && ok {
|
|
|
+ otherPushId = qutil.ObjToString((*userData)["s_opushid"])
|
|
|
+ jgPushId = qutil.ObjToString((*userData)["s_jpushid"])
|
|
|
+ phoneType = qutil.ObjToString((*userData)["s_appponetype"])
|
|
|
+ name = qutil.ObjToString((*userData)["s_name"])
|
|
|
+ appVersion = qutil.ObjToString((*userData)["s_appversion"])
|
|
|
+ }
|
|
|
+ userNames += name + ","
|
|
|
+ appPushUrl := "/jyapp/frontPage/messageCenter/sess/index"
|
|
|
+ if strings.Contains(phoneType, "iPhone") {
|
|
|
+ if param.IosUrl != "" {
|
|
|
+ appPushUrl = param.IosUrl
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if param.AndroidUrl != "" {
|
|
|
+ appPushUrl = param.AndroidUrl
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //实时发送发送时间为当前时间
|
|
|
+ param.SendTime = time.Now().Format(qutil.Date_Full_Layout)
|
|
|
+ dt := map[string]interface{}{
|
|
|
+ "receiveUserId": userId,
|
|
|
+ "receiveName": name,
|
|
|
+ "sendUserId": loginUserId,
|
|
|
+ "sendName": loginUserName,
|
|
|
+ "title": param.Title,
|
|
|
+ "content": param.Content,
|
|
|
+ "msgType": msgType,
|
|
|
+ "link": param.Link,
|
|
|
+ "appid": util.AppId,
|
|
|
+ }
|
|
|
+ //推送消息
|
|
|
+ if appVersion > "3.0.3" {
|
|
|
+ go util.AppGrpcPush(dt, otherPushId, jgPushId, phoneType, appPushUrl)
|
|
|
+ }
|
|
|
+ if i == 100 {
|
|
|
+ //调用消息中台
|
|
|
+ util.MultipleSaveMessage(msg, userIds, userNames)
|
|
|
+ userNames = ""
|
|
|
+ userIds = ""
|
|
|
+ i = 0
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if i > 0 {
|
|
|
+ //调用中台接口
|
|
|
+ util.MultipleSaveMessage(msg, userIds, userNames)
|
|
|
+ userNames = ""
|
|
|
+ userIds = ""
|
|
|
+ i = 0
|
|
|
+ }
|
|
|
+ return 1, nil
|
|
|
+ }
|
|
|
+ return 0, errors.New("发送消息出错")
|
|
|
+}
|