|
@@ -2,8 +2,21 @@ package roadshow
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "github.com/rdlucklib/rdluck_tools/http"
|
|
|
+ "hongze/hongze_task/models"
|
|
|
+ "hongze/hongze_task/models/cygx"
|
|
|
"hongze/hongze_task/models/roadshow"
|
|
|
+ "hongze/hongze_task/models/system"
|
|
|
+ "hongze/hongze_task/services/alarm_msg"
|
|
|
"hongze/hongze_task/utils"
|
|
|
+ "io/ioutil"
|
|
|
+ netHttp "net/http"
|
|
|
+ "net/url"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
"sync"
|
|
|
"time"
|
|
|
)
|
|
@@ -38,3 +51,730 @@ func ModifyRsCalendarResearcherStatus(cont context.Context) (err error) {
|
|
|
lock.Unlock()
|
|
|
return err
|
|
|
}
|
|
|
+
|
|
|
+// ShResponse 上海数据返回结构体
|
|
|
+type ShResponse struct {
|
|
|
+ Code int `json:"code" description:"1:正常返回;4001:token失效"`
|
|
|
+ Msg string `json:"msg" description:"文字描述"`
|
|
|
+ Time int `json:"time" description:"时间戳"`
|
|
|
+ Data interface{} `json:"data" description:"业务数据"`
|
|
|
+}
|
|
|
+
|
|
|
+// getCurl get请求上海接口
|
|
|
+func getCurl(urlStr string, params url.Values, num int) (body []byte, err error) {
|
|
|
+ if err != nil {
|
|
|
+ go alarm_msg.SendAlarmMsg("get请求上海接口失败,ERR:"+err.Error(), 3)
|
|
|
+ //go utils.SendEmail(utils.APPNAME+"get请求上海接口失败:"+time.Now().Format("2006-01-02 15:04:05"), "get请求上海接口失败:"+err.Error(), utils.EmailSendToUsers)
|
|
|
+ }
|
|
|
+ token, err := GetAccessToken(false)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ params.Add("access_token", token)
|
|
|
+ getUrl := urlStr + "?" + params.Encode()
|
|
|
+ body, err = http.Get(getUrl)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info(fmt.Sprint("get Err:", err.Error(), ";url:", getUrl, ";params:", params.Encode(), ";response:", string(body)))
|
|
|
+ err = errors.New("NewRequest Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var response ShResponse
|
|
|
+ err = json.Unmarshal(body, &response)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info(fmt.Sprint("get Err:", err.Error(), ";url:", getUrl, ";params:", params.Encode(), ";response:", string(body)))
|
|
|
+ err = errors.New("Unmarshal Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //如果是token失效,同时只是第一次请求(没有尝试强制刷新token,那么重新请求)
|
|
|
+ if response.Code == 4001 && num <= 0 {
|
|
|
+ utils.FileLog.Info(fmt.Sprint("get data err", ";url:", getUrl, ";params:", params.Encode(), ";response:", string(body)))
|
|
|
+
|
|
|
+ //token失效
|
|
|
+ _, tmpErr := refreshAccessToken()
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ }
|
|
|
+ num++
|
|
|
+ params.Del("access_token")
|
|
|
+ return getCurl(urlStr, params, num)
|
|
|
+ } else if response.Code != 1 {
|
|
|
+ utils.FileLog.Info(fmt.Sprint("get data err", ";url:", getUrl, ";params:", params.Encode(), ";response:", string(body)))
|
|
|
+ err = errors.New(response.Msg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// postCurl post请求上海接口
|
|
|
+func postCurl(urlStr string, form url.Values, num int) (body []byte, err error, errMsg string) {
|
|
|
+ logMsg := ``
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ if logMsg != `` {
|
|
|
+ errMsg = logMsg
|
|
|
+ go alarm_msg.SendAlarmMsg("post请求上海接口失败,ERR:"+err.Error()+";errMsg:"+errMsg, 3)
|
|
|
+ //go utils.SendEmail(utils.APPNAME+"post请求上海接口失败:"+time.Now().Format("2006-01-02 15:04:05"), "post请求上海接口失败:"+errMsg, utils.EmailSendToUsers)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ token, err := GetAccessToken(false)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ finalUrl := urlStr + "?access_token=" + token
|
|
|
+
|
|
|
+ //发送创建请求
|
|
|
+ resp, err := netHttp.PostForm(finalUrl, form)
|
|
|
+ if err != nil {
|
|
|
+ err = errors.New("NewRequest Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ //解析resp并且存入关联表
|
|
|
+ body, err = ioutil.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ logMsg = fmt.Sprint("post err; request:", form.Encode(), "; errMsg:", err.Error())
|
|
|
+ utils.FileLog.Info(logMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ logMsg = fmt.Sprint("post request:", form.Encode(), "; response:", string(body))
|
|
|
+ utils.FileLog.Info(logMsg)
|
|
|
+
|
|
|
+ var response ShResponse
|
|
|
+ err = json.Unmarshal(body, &response)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("post Err:", err.Error(), ";url:", finalUrl, ";params:", form.Encode(), ";response:", string(body))
|
|
|
+ err = errors.New("Unmarshal Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ utils.FileLog.Info(fmt.Sprint("post request url:", finalUrl, ";params:", form.Encode(), ";response:", string(body)))
|
|
|
+
|
|
|
+ //如果是token失效,同时只是第一次请求(没有尝试强制刷新token,那么重新请求)
|
|
|
+ if response.Code == 4001 && num <= 0 {
|
|
|
+ //token失效
|
|
|
+ _, tmpErr := refreshAccessToken()
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ }
|
|
|
+ num++
|
|
|
+ return postCurl(urlStr, form, num)
|
|
|
+ } else if response.Code != 1 {
|
|
|
+ utils.FileLog.Info(fmt.Sprint("post data err", ";url:", finalUrl, ";params:", form.Encode(), ";response:", string(body)))
|
|
|
+ err = errors.New(response.Msg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetAccessToken 获取accessToken
|
|
|
+func GetAccessToken(isRefresh bool) (token string, err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ go alarm_msg.SendAlarmMsg("获取上海的token失败,ERR:"+err.Error(), 3)
|
|
|
+ //go utils.SendEmail(utils.APPNAME+"获取上海的token失败:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ token, redisErr := utils.Rc.RedisString("SH_ACCESS_TOKEN")
|
|
|
+ //如果从redis中accessToken 获取失败或者token为空了,再或者需要强制刷新了,那么重新获取accessToken
|
|
|
+ if redisErr != nil || token == `` || isRefresh {
|
|
|
+ return refreshAccessToken()
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// getAccessToken token内部请求接口
|
|
|
+func getAccessToken() (tokenData roadshow.TokenData, err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ go alarm_msg.SendAlarmMsg("更新上海的token失败;ERR:"+err.Error(), 3)
|
|
|
+ //go utils.SendEmail(utils.APPNAME+"更新上海的token失败:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ getUrl := fmt.Sprintf(utils.CRM_OPEN_API_URL+"/v1/auth/getAccessToken?app_key=%s&app_secret=%s", utils.CRM_OPEN_API_APP_KEY, utils.CRM_OPEN_API_APP_SECRET)
|
|
|
+ body, err := http.Get(getUrl)
|
|
|
+ if err != nil {
|
|
|
+ err = errors.New("NewRequest Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var tokenResp roadshow.GetTokenResp
|
|
|
+ err = json.Unmarshal(body, &tokenResp)
|
|
|
+ if err != nil {
|
|
|
+ err = errors.New("Unmarshal Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if tokenResp.Code != 1 {
|
|
|
+ err = errors.New("getAccessToken err:" + tokenResp.Msg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ tokenData = tokenResp.TokenData
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// refreshAccessToken 强制刷新获取accessToken
|
|
|
+func refreshAccessToken() (token string, err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ go alarm_msg.SendAlarmMsg("刷新上海的token失败;ERR:"+err.Error(), 3)
|
|
|
+ //go utils.SendEmail(utils.APPNAME+"刷新上海的token失败:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ tokenInfo, tmpErr := getAccessToken()
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ token = tokenInfo.AccessToken
|
|
|
+
|
|
|
+ //token存入redis
|
|
|
+ err = utils.Rc.Put("SH_ACCESS_TOKEN", token, time.Duration(tokenInfo.ExpireIn-600)*time.Second)
|
|
|
+ if err != nil {
|
|
|
+ go alarm_msg.SendAlarmMsg("获取上海的token失败;上海token存入redis失败,ERR:"+err.Error(), 3)
|
|
|
+ //go utils.SendEmail(utils.APPNAME+"获取上海的token失败:"+time.Now().Format("2006-01-02 15:04:05"), "上海token存入redis失败:", utils.EmailSendToUsers)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// getCalendarFrom 获取上海方的路演活动列表
|
|
|
+func getCalendarFrom(userPhone, startDate, endDate string) (list []roadshow.UserCalendar, err error) {
|
|
|
+ exUrl := utils.CRM_OPEN_API_URL + "/v1/calendar/userCalendarList"
|
|
|
+ form := url.Values{
|
|
|
+ "user_phone": {userPhone},
|
|
|
+ "start_time": {startDate},
|
|
|
+ "end_time": {endDate},
|
|
|
+ }
|
|
|
+ body, err := getCurl(exUrl, form, 0)
|
|
|
+ if err != nil {
|
|
|
+ err = errors.New("NewRequest Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var userCalendarList roadshow.UserCalendarList
|
|
|
+ err = json.Unmarshal(body, &userCalendarList)
|
|
|
+ if err != nil {
|
|
|
+ err = errors.New("Unmarshal Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ list = userCalendarList.Data
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+//func init() {
|
|
|
+// SyncCalendarFromShanghai()
|
|
|
+//}
|
|
|
+
|
|
|
+// SyncCalendarFromShanghai 上海路演数据同步到自系统
|
|
|
+func SyncCalendarFromShanghai(cont context.Context) (err error) {
|
|
|
+ errMsgList := make([]string, 0)
|
|
|
+ defer func() {
|
|
|
+ if len(errMsgList) > 0 {
|
|
|
+ //fmt.Println("err:", errMsg)
|
|
|
+ if err != nil {
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprint("ERR:"+err.Error(), ";"))
|
|
|
+ }
|
|
|
+ go alarm_msg.SendAlarmMsg("上海路演数据同步到自系统失败;errMsg:"+strings.Join(errMsgList, "\n"), 3)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ var mobiles []string
|
|
|
+ mobilesMap := make(map[string]bool)
|
|
|
+ researcherListAdmin, tmpErr := roadshow.GetResearcher() // ficc研究员
|
|
|
+ if tmpErr != nil {
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprint("获取研究员信息失败:GetResearcher", ";err:"+tmpErr.Error(), ";"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, v := range researcherListAdmin {
|
|
|
+ if v.Mobile == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if mobilesMap[v.Mobile] {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ mobiles = append(mobiles, v.Mobile)
|
|
|
+ mobilesMap[v.Mobile] = true
|
|
|
+ }
|
|
|
+ askUserList, tmpErr := cygx.GetAskEmailList() //权益研究员
|
|
|
+ if tmpErr != nil {
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprint("获取研究员信息失败:GetAskEmailList", ";err:"+tmpErr.Error(), ";"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, v := range askUserList {
|
|
|
+ if v.Mobile == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if mobilesMap[v.Mobile] {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ mobiles = append(mobiles, v.Mobile)
|
|
|
+ mobilesMap[v.Mobile] = true
|
|
|
+ }
|
|
|
+
|
|
|
+ adminList, tmpErr := models.GetAdminByAdminAllList() //查询管理员信息列表
|
|
|
+ if tmpErr != nil {
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprint("查询管理员信息列表 失败:GetAdminByAdminAllList", ";err:"+tmpErr.Error(), ";"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ mapUserInfo := make(map[string]*system.AdminItem)
|
|
|
+ mapAdminIdMobil := make(map[int]string) // 研究员ID与手机号的关系
|
|
|
+ for _, v := range adminList {
|
|
|
+ if v.Mobile == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ mapUserInfo[v.Mobile] = v
|
|
|
+ mapAdminIdMobil[v.AdminId] = v.Mobile
|
|
|
+ }
|
|
|
+ //获取前后三十天的信息
|
|
|
+ startDate := time.Now().AddDate(0, 0, -30).Format(utils.FormatDate)
|
|
|
+ endDate := time.Now().AddDate(0, 0, 30).Format(utils.FormatDate)
|
|
|
+ var userPhone string
|
|
|
+ var list []roadshow.UserCalendar
|
|
|
+ //获取指定日期内所有研究员的路演信息
|
|
|
+ for _, v := range mobiles {
|
|
|
+ if v == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ time.Sleep(200 * time.Millisecond) // 加一个延迟0.2秒
|
|
|
+ userPhone = v
|
|
|
+ //以当前日期作为起始日期去同步
|
|
|
+ listSh, tmpErr := getCalendarFrom(userPhone, startDate, endDate)
|
|
|
+
|
|
|
+ if tmpErr != nil && tmpErr.Error() != "NewRequest Err:该用户不存在" {
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprint("获取第三方路演日历数据失败,", "userPhone:", userPhone, ";currDay:", startDate, ";endDate:", endDate, ";err:"+tmpErr.Error()))
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ for _, vSh := range listSh {
|
|
|
+ list = append(list, vSh)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据研究员和开始/结束日期获取上海的活动路演
|
|
|
+ rsCalendarResearcherList, tmpErr := roadshow.GetRsCalendarResearcherInfoIByResearcherIdAndDate(startDate, endDate)
|
|
|
+ if tmpErr != nil {
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprint("根据研究员和开始/结束日期获取上海的活动路演 失败:时间段:", startDate, endDate, ";err:"+tmpErr.Error(), ";"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //return
|
|
|
+ //待删除的活动路演
|
|
|
+ deleteRsCalendarResearcherMap := make(map[int][]*roadshow.RsCalendarResearcherRelationInfo)
|
|
|
+ for _, v := range rsCalendarResearcherList {
|
|
|
+ tmpList, ok := deleteRsCalendarResearcherMap[v.ThirdCalendarId]
|
|
|
+ if !ok {
|
|
|
+ tmpList = make([]*roadshow.RsCalendarResearcherRelationInfo, 0)
|
|
|
+ }
|
|
|
+ tmpList = append(tmpList, v)
|
|
|
+
|
|
|
+ deleteRsCalendarResearcherMap[v.ThirdCalendarId] = tmpList
|
|
|
+ }
|
|
|
+
|
|
|
+ thirdIdList := make([]int, 0)
|
|
|
+ mapRsCalendar := make(map[int]*roadshow.RsCalendar)
|
|
|
+
|
|
|
+ if len(list) > 0 {
|
|
|
+ for _, v := range list {
|
|
|
+ thirdIdList = append(thirdIdList, v.ID)
|
|
|
+ //移除还存在的活动,保留不存在的活动
|
|
|
+ delete(deleteRsCalendarResearcherMap, v.ID)
|
|
|
+ }
|
|
|
+ rsCalendarRelationList, tmpErr := roadshow.GetRsCalendarRelationListByThirdIds(thirdIdList)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ errMsgList = append(errMsgList, "获取关联列表失败,err:"+tmpErr.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //已存在的三方日历信息,做对比使用
|
|
|
+ rsCalendarRelationMap := make(map[int]*roadshow.RsCalendarRelation)
|
|
|
+ var selfCalendarIds []int
|
|
|
+ for _, rsCalendarRelation := range rsCalendarRelationList {
|
|
|
+ rsCalendarRelationMap[rsCalendarRelation.ThirdCalendarId] = rsCalendarRelation
|
|
|
+ selfCalendarIds = append(selfCalendarIds, rsCalendarRelation.SelfCalendarId)
|
|
|
+ }
|
|
|
+ rsCalendarResearcherInfoList, tmpErr := roadshow.GetRsCalendarResearcherByIds(selfCalendarIds)
|
|
|
+ if tmpErr != nil {
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", selfCalendarIds, "获取路演研究员信息失败;err:"+tmpErr.Error(), ";"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ maprsCalendarResearcherInfo := make(map[int]*roadshow.RsCalendarResearcher)
|
|
|
+
|
|
|
+ var rsCalendarIds []int
|
|
|
+ for _, v := range rsCalendarResearcherInfoList {
|
|
|
+ maprsCalendarResearcherInfo[v.RsCalendarResearcherId] = v
|
|
|
+ rsCalendarIds = append(rsCalendarIds, v.RsCalendarId)
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取研究员map数据信息
|
|
|
+ rsCalendarResearcherListDate, tmpErr := roadshow.GetRsCalendarResearcherListByRsCalendarIds(rsCalendarIds)
|
|
|
+ if tmpErr != nil {
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprint("获取路演活动中的研究员列表失败,路演活动ID:", rsCalendarIds, ";err:"+tmpErr.Error(), ";"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ maprsCalendarResearcherList := make(map[int][]*roadshow.RsCalendarResearcher)
|
|
|
+ for _, v := range rsCalendarResearcherListDate {
|
|
|
+ maprsCalendarResearcherList[v.RsCalendarId] = append(maprsCalendarResearcherList[v.RsCalendarId], v)
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取路演map信息
|
|
|
+ rsCalendarInfoList, tmpErr := roadshow.GetRsCalendarByIds(rsCalendarIds)
|
|
|
+ if tmpErr != nil {
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprint("日历ID:", rsCalendarIds, "获取路演信息失败;err:"+tmpErr.Error(), ";"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, v := range rsCalendarInfoList {
|
|
|
+ mapRsCalendar[v.RsCalendarId] = v
|
|
|
+ }
|
|
|
+ for _, v := range list {
|
|
|
+ //展示优先级:1、customer_name 2、project_name 3、title 需求池953
|
|
|
+ if v.CustomerName != "" {
|
|
|
+ v.Title = v.CustomerName
|
|
|
+ } else if v.ProjectName != "" {
|
|
|
+ v.Title = v.ProjectName
|
|
|
+ }
|
|
|
+ if rsCalendarRelation, ok := rsCalendarRelationMap[v.ID]; ok {
|
|
|
+ //存在的话,那么就去查找对应的信息
|
|
|
+ if rsCalendarRelation.CalendarType == 1 {
|
|
|
+ if maprsCalendarResearcherInfo[rsCalendarRelation.SelfCalendarId] == nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ rsCalendarResearcherInfo := maprsCalendarResearcherInfo[rsCalendarRelation.SelfCalendarId]
|
|
|
+ if mapRsCalendar[rsCalendarResearcherInfo.RsCalendarId] == nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ rsCalendarInfo := mapRsCalendar[rsCalendarResearcherInfo.RsCalendarId]
|
|
|
+ if rsCalendarInfo.Source == 0 { //自系统创建的路演活动,不需要依靠上海方来修改
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ //是否去同步变更数据库
|
|
|
+ isUpdateSync := false
|
|
|
+
|
|
|
+ //是否变更
|
|
|
+ isUpdate := false
|
|
|
+ if v.StartTime != rsCalendarRelation.StartTime {
|
|
|
+ isUpdate = true
|
|
|
+ }
|
|
|
+ if v.EndTime != rsCalendarRelation.EndTime {
|
|
|
+ isUpdate = true
|
|
|
+ }
|
|
|
+ if v.Title != rsCalendarRelation.Title {
|
|
|
+ isUpdate = true
|
|
|
+ }
|
|
|
+ if rsCalendarResearcherInfo.Status == 4 { // 被删除了,现在是需要更新
|
|
|
+ isUpdate = true
|
|
|
+ }
|
|
|
+
|
|
|
+ researcherList := make([]*system.AdminItem, 0)
|
|
|
+ researcherMap := make(map[int]*system.AdminItem)
|
|
|
+
|
|
|
+ addResearcherList := make([]*system.AdminItem, 0) // 需要新增的研究员
|
|
|
+ delResearcherIdList := make([]int, 0) //需要删除的路演活动与研究员的关系id
|
|
|
+ updateResearcherList := make([]*roadshow.RsCalendarResearcher, 0) //待更新的路演活动中研究员信息
|
|
|
+
|
|
|
+ if v.ResearcherMobile != rsCalendarRelation.ResearcherMobile {
|
|
|
+ //研究员变更了,需要去改表
|
|
|
+ isUpdateSync = true
|
|
|
+ researcherMobileList := strings.Split(v.ResearcherMobile, ",")
|
|
|
+ if len(researcherMobileList) > 0 {
|
|
|
+ for _, mobile := range researcherMobileList {
|
|
|
+ if mapUserInfo[mobile] == nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ researcherInfo := mapUserInfo[mobile]
|
|
|
+ if researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_RESEARCHR ||
|
|
|
+ researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RESEARCHR ||
|
|
|
+ researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_RESEARCHR ||
|
|
|
+ researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT ||
|
|
|
+ researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT ||
|
|
|
+ researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER ||
|
|
|
+ researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
|
|
|
+ researcherList = append(researcherList, researcherInfo)
|
|
|
+ researcherMap[researcherInfo.AdminId] = researcherInfo
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //没有研究员
|
|
|
+ if len(researcherList) <= 0 {
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, ";对方研究员信息失败;"))
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if len(maprsCalendarResearcherList[rsCalendarInfo.RsCalendarId]) == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ rsCalendarResearcherListUpdate := maprsCalendarResearcherList[rsCalendarInfo.RsCalendarId] // 活动中包含的多个研究员
|
|
|
+
|
|
|
+ //现有活动中的研究员
|
|
|
+ rsCalendarResearcherMap := make(map[int]*roadshow.RsCalendarResearcher)
|
|
|
+ for _, rsCalendarResearcher := range rsCalendarResearcherListUpdate {
|
|
|
+ if _, ok := researcherMap[rsCalendarResearcher.ResearcherId]; ok {
|
|
|
+ if isUpdate {
|
|
|
+ updateResearcherList = append(updateResearcherList, rsCalendarResearcher)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ delResearcherIdList = append(delResearcherIdList, rsCalendarResearcher.RsCalendarResearcherId)
|
|
|
+ }
|
|
|
+
|
|
|
+ rsCalendarResearcherMap[rsCalendarResearcher.ResearcherId] = rsCalendarResearcher
|
|
|
+ }
|
|
|
+
|
|
|
+ //校验是否需要新增研究员
|
|
|
+ for adminId, researcherInfo := range researcherMap {
|
|
|
+ //如果
|
|
|
+ if _, ok := rsCalendarResearcherMap[adminId]; !ok {
|
|
|
+ addResearcherList = append(addResearcherList, researcherInfo)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if isUpdate { //如果有字段更新,那么需要将所有的研究员信息更新
|
|
|
+ isUpdateSync = true
|
|
|
+ if len(maprsCalendarResearcherList[rsCalendarInfo.RsCalendarId]) == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ rsCalendarResearcherListUpdate := maprsCalendarResearcherList[rsCalendarInfo.RsCalendarId]
|
|
|
+ for _, rsCalendarResearcher := range rsCalendarResearcherListUpdate {
|
|
|
+ updateResearcherList = append(updateResearcherList, rsCalendarResearcher)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if isUpdateSync {
|
|
|
+ time.Sleep(200 * time.Millisecond) // 加一个延迟0.2秒
|
|
|
+ tmpErr = roadshow.UpdateSyncRsCalendarRelation(v, rsCalendarInfo, rsCalendarRelation, updateResearcherList, delResearcherIdList, addResearcherList)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "修改关联关系失败;err:"+tmpErr.Error(), ";"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //事项
|
|
|
+ //事项都是由自系统创建的,不需要依靠上海方来修改
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //fmt.Println("add")
|
|
|
+ if mapUserInfo[v.UserPhone] == nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ createUser := mapUserInfo[v.UserPhone]
|
|
|
+ //研究员列表
|
|
|
+ researcherList := make([]*system.AdminItem, 0)
|
|
|
+ researcherMobileList := strings.Split(v.ResearcherMobile, ",")
|
|
|
+ if len(researcherMobileList) > 0 {
|
|
|
+ for _, mobile := range researcherMobileList {
|
|
|
+ if mapUserInfo[mobile] == nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ researcherInfo := mapUserInfo[mobile]
|
|
|
+ if researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_RESEARCHR ||
|
|
|
+ researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RESEARCHR ||
|
|
|
+ researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_RESEARCHR ||
|
|
|
+ researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT ||
|
|
|
+ researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT ||
|
|
|
+ researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER ||
|
|
|
+ researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN ||
|
|
|
+ researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
|
|
|
+ researcherList = append(researcherList, researcherInfo)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //没有研究员
|
|
|
+ if len(researcherList) <= 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ //数据入库
|
|
|
+ time.Sleep(200 * time.Millisecond) // 加一个延迟0.2秒
|
|
|
+ tmpErr = roadshow.SyncRsCalendarRelation(v, createUser, researcherList)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "绑定关联关系失败;err:"+tmpErr.Error(), ";"))
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var delRsCalendarResearcherIds []int
|
|
|
+ ////上海那边已经删除了路演,这边也要同步删除
|
|
|
+ for _, deleteRsCalendarResearcherList := range deleteRsCalendarResearcherMap {
|
|
|
+ for _, deleteRsCalendarResearcher := range deleteRsCalendarResearcherList {
|
|
|
+ delRsCalendarResearcherIds = append(delRsCalendarResearcherIds, deleteRsCalendarResearcher.RsCalendarResearcherId)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(delRsCalendarResearcherIds) > 0 {
|
|
|
+ tmpErr := roadshow.ModifyRsCalendarResearcherStatusDel(delRsCalendarResearcherIds)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ errMsgList = append(errMsgList, fmt.Sprint("删除第三方日历ID:", delRsCalendarResearcherIds, "删除关联关系失败;err:"+tmpErr.Error(), ";"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ mapShangHaiResearcher := make(map[string][]string)
|
|
|
+ for _, v := range list {
|
|
|
+ researcherMobileList := strings.Split(v.ResearcherMobile, ",")
|
|
|
+ for _, vm := range researcherMobileList {
|
|
|
+ mapShangHaiResearcher[vm] = append(mapShangHaiResearcher[vm], time.Unix(int64(v.StartTime), 0).Format(utils.FormatDateTime))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, v := range rsCalendarResearcherList {
|
|
|
+ mobileResearcher := mapAdminIdMobil[v.ResearcherId] //研究员手机号
|
|
|
+ if mobileResearcher == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ //如果手机号对应的开始时间不存在,就把这场活动同步到上海那边
|
|
|
+ if !utils.InArrayByStr(mapShangHaiResearcher[mobileResearcher], fmt.Sprint(v.StartDate, " ", v.StartTime)) {
|
|
|
+ if mapRsCalendar[v.RsCalendarId] == nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ CalendarToSH(mapRsCalendar[v.RsCalendarId], v.ResearcherId)
|
|
|
+ time.Sleep(1000 * time.Millisecond) // 延迟1秒
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// CalendarToSH 创建活动时同步上海的前置函数
|
|
|
+func CalendarToSH(rsCalendar *roadshow.RsCalendar, researcherId int) {
|
|
|
+ var err error
|
|
|
+ errMsg := ``
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ errMsg = err.Error() + ";" + errMsg
|
|
|
+ fmt.Println(errMsg)
|
|
|
+ go alarm_msg.SendAlarmMsg("新建上海研究员日历失败,ERR:"+err.Error()+";errMsg:"+errMsg, 3)
|
|
|
+ //go utils.SendEmail(utils.APPNAME+"新建上海研究员日历失败:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ //redis获取创建者及研究员信息
|
|
|
+ userInfo, err := getAdminInfoById(rsCalendar.SysUserId)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("getAdminInfoById err: " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ researcher, err := roadshow.GetRsCalendarResearcherByRsCalendarIdAndResearcherId(rsCalendar.RsCalendarId, researcherId)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("getAdminInfoById err: " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ researcherInfo, err := getAdminInfoById(researcher.ResearcherId)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("getAdminInfoById err: " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ sTime, _ := time.ParseInLocation(utils.FormatDateTime, researcher.StartDate+" "+researcher.StartTime, time.Now().Location())
|
|
|
+ startTime := sTime.Format("2006-01-02 15:04")
|
|
|
+ eTime, _ := time.ParseInLocation(utils.FormatDateTime, researcher.EndDate+" "+researcher.EndTime, time.Now().Location())
|
|
|
+ endTime := eTime.Format("2006-01-02 15:04")
|
|
|
+
|
|
|
+ // 创建上海路演日程
|
|
|
+ err, errMsg = creatSHCalendar(userInfo.Mobile, researcherInfo.Mobile, rsCalendar.Title, startTime, endTime, 1, researcher.RsCalendarResearcherId)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("CreatSHCalendar err: " + err.Error())
|
|
|
+ fmt.Println(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新路演与研究员关系表的同步字段
|
|
|
+ whereParams := make(map[string]interface{})
|
|
|
+ updateParams := make(map[string]interface{})
|
|
|
+ whereParams["rs_calendar_researcher_id"] = researcher.RsCalendarResearcherId
|
|
|
+ updateParams["is_synced"] = 1
|
|
|
+ updateParams["modify_time"] = time.Now()
|
|
|
+ err = roadshow.UpdateCalendarResearcher(whereParams, updateParams)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("UpdateCalendarResearcher err: " + err.Error())
|
|
|
+ fmt.Println("UpdateCalendarResearcher err: " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新路演的同步字段
|
|
|
+ calWhereParams := make(map[string]interface{})
|
|
|
+ calWhereParams["rs_calendar_id"] = rsCalendar.RsCalendarId
|
|
|
+ err = roadshow.UpdateRsCalendar(calWhereParams, updateParams)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("UpdateRsCalendar err: " + err.Error())
|
|
|
+ fmt.Println("UpdateRsCalendar err: " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// creatSHCalendar 新增上海日历活动
|
|
|
+func creatSHCalendar(userPhone, toUserPhone, content, startTime, endTime string, calendarType int8, selfCalendarId int) (err error, errMsg string) {
|
|
|
+ logMsg := ``
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ if logMsg != `` {
|
|
|
+ errMsg = logMsg
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ finalUrl := utils.CRM_OPEN_API_URL + "/v1/Calendar/create"
|
|
|
+
|
|
|
+ form := url.Values{
|
|
|
+ "user_phone": {userPhone},
|
|
|
+ "to_user_phone": {toUserPhone},
|
|
|
+ "content": {content},
|
|
|
+ "start_time": {startTime},
|
|
|
+ "end_time": {endTime},
|
|
|
+ }
|
|
|
+ //发送创建请求
|
|
|
+ body, err, logMsg := postCurl(finalUrl, form, 0)
|
|
|
+ if err != nil {
|
|
|
+ err = errors.New("NewRequest Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var creatSHCalendarResp roadshow.CreatSHCalendarResp
|
|
|
+ err = json.Unmarshal(body, &creatSHCalendarResp)
|
|
|
+ if err != nil {
|
|
|
+ err = errors.New("Unmarshal Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ calendar := creatSHCalendarResp.Data
|
|
|
+
|
|
|
+ //上海系统id
|
|
|
+ sThirdId := calendar.CalendarID
|
|
|
+ thirdId, err := strconv.Atoi(sThirdId)
|
|
|
+ if err != nil {
|
|
|
+ err = errors.New("string to int Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 添加自系统与上海系统的路演关系
|
|
|
+ relationItem := roadshow.RsCalendarRelation{
|
|
|
+ UserPhone: userPhone,
|
|
|
+ CalendarType: calendarType,
|
|
|
+ SelfCalendarId: selfCalendarId,
|
|
|
+ ThirdCalendarId: thirdId,
|
|
|
+ Title: content,
|
|
|
+ ResearcherMobile: toUserPhone,
|
|
|
+ ModifyTime: time.Now(),
|
|
|
+ CreateTime: time.Now(),
|
|
|
+ }
|
|
|
+ _, err = roadshow.AddRsCalendarRelation(&relationItem)
|
|
|
+ if err != nil {
|
|
|
+ err = errors.New("AddRsCalendarRelation Err:" + err.Error())
|
|
|
+ fmt.Println(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func getAdminInfoById(sysId int) (adminInfo system.AdminItem, err error) {
|
|
|
+ adminMap := make(map[int]system.AdminItem)
|
|
|
+ list, err := system.GetSysuserList("", []interface{}{}, 0, 1000)
|
|
|
+ // GetSysuserList
|
|
|
+ for _, tmpAdminInfo := range list {
|
|
|
+ adminMap[tmpAdminInfo.AdminId] = *tmpAdminInfo
|
|
|
+ }
|
|
|
+
|
|
|
+ adminInfo, ok := adminMap[sysId]
|
|
|
+ if !ok {
|
|
|
+ err = fmt.Errorf("找不到该用户id,sysId:" + fmt.Sprint(sysId))
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|