calendar.go 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. package roadshow
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/http"
  7. "hongze/hz_crm_api/models/roadshow"
  8. "hongze/hz_crm_api/models/system"
  9. "hongze/hz_crm_api/services/alarm_msg"
  10. "hongze/hz_crm_api/utils"
  11. "io/ioutil"
  12. netHttp "net/http"
  13. "net/url"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. type SyncCalendarFromShanghaiRecord struct {
  19. UserPhone string `description:"手机号"`
  20. StartDate string `description:"开始时间"`
  21. EndDate string `description:"结束时间"`
  22. }
  23. // 上海路演数据同步到自系统 从Redis获取
  24. func InsertSyncCalendarFromShanghai() {
  25. var err error
  26. defer func() {
  27. if err != nil {
  28. go alarm_msg.SendAlarmMsg("上海路演数据同步到自系统 失败;InsertSyncCalendarFromShanghai ERR:"+err.Error(), 3)
  29. }
  30. }()
  31. for {
  32. utils.Rc.Brpop(utils.CACHE_KEY_SHANG_HAI_RS_CALENDAR_API, func(b []byte) {
  33. record := new(SyncCalendarFromShanghaiRecord)
  34. if err := json.Unmarshal(b, &record); err != nil {
  35. fmt.Println("json unmarshal wrong!")
  36. }
  37. //fmt.Println(record.UserPhone)
  38. _ = SyncCalendarFromShanghai(record.UserPhone, record.StartDate, record.EndDate)
  39. })
  40. }
  41. }
  42. // 上海路演数据同步到自系统请求放到Redis
  43. func InsertSyncCalendarFromShanghaiLPush(userPhone, startDate, endDate string) bool {
  44. log := &SyncCalendarFromShanghaiRecord{UserPhone: userPhone, StartDate: startDate, EndDate: endDate}
  45. if utils.Re == nil {
  46. err := utils.Rc.LPush(utils.CACHE_KEY_SHANG_HAI_RS_CALENDAR_API, log)
  47. if err != nil {
  48. fmt.Println(err)
  49. go alarm_msg.SendAlarmMsg("上海路演数据同步到自系统 失败;InsertSyncCalendarFromShanghaiLPush ERR:"+err.Error(), 3)
  50. }
  51. return true
  52. }
  53. return false
  54. }
  55. // getAccessToken token内部请求接口
  56. func getAccessToken() (tokenData roadshow.TokenData, err error) {
  57. defer func() {
  58. if err != nil {
  59. go alarm_msg.SendAlarmMsg("更新上海的token失败;ERR:"+err.Error(), 3)
  60. //go utils.SendEmail(utils.APPNAME+"更新上海的token失败:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
  61. }
  62. }()
  63. 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)
  64. body, err := http.Get(getUrl)
  65. if err != nil {
  66. err = errors.New("NewRequest Err:" + err.Error())
  67. return
  68. }
  69. var tokenResp roadshow.GetTokenResp
  70. err = json.Unmarshal(body, &tokenResp)
  71. if err != nil {
  72. err = errors.New("Unmarshal Err:" + err.Error())
  73. return
  74. }
  75. if tokenResp.Code != 1 {
  76. err = errors.New("getAccessToken err:" + tokenResp.Msg)
  77. return
  78. }
  79. tokenData = tokenResp.TokenData
  80. return
  81. }
  82. // refreshAccessToken 强制刷新获取accessToken
  83. func refreshAccessToken() (token string, err error) {
  84. defer func() {
  85. if err != nil {
  86. go alarm_msg.SendAlarmMsg("刷新上海的token失败;ERR:"+err.Error(), 3)
  87. //go utils.SendEmail(utils.APPNAME+"刷新上海的token失败:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
  88. }
  89. }()
  90. tokenInfo, tmpErr := getAccessToken()
  91. if tmpErr != nil {
  92. err = tmpErr
  93. return
  94. }
  95. token = tokenInfo.AccessToken
  96. //token存入redis
  97. err = utils.Rc.Put("SH_ACCESS_TOKEN", token, time.Duration(tokenInfo.ExpireIn-600)*time.Second)
  98. if err != nil {
  99. go alarm_msg.SendAlarmMsg("获取上海的token失败;上海token存入redis失败,ERR:"+err.Error(), 3)
  100. //go utils.SendEmail(utils.APPNAME+"获取上海的token失败:"+time.Now().Format("2006-01-02 15:04:05"), "上海token存入redis失败:", utils.EmailSendToUsers)
  101. }
  102. return
  103. }
  104. // getCalendarFrom 获取上海方的路演活动列表
  105. func getCalendarFrom(userPhone, startDate, endDate string) (list []roadshow.UserCalendar, err error) {
  106. exUrl := utils.CRM_OPEN_API_URL + "/v1/calendar/userCalendarList"
  107. form := url.Values{
  108. "user_phone": {userPhone},
  109. "start_time": {startDate},
  110. "end_time": {endDate},
  111. }
  112. body, err := getCurl(exUrl, form, 0)
  113. if err != nil {
  114. err = errors.New("NewRequest Err:" + err.Error())
  115. return
  116. }
  117. var userCalendarList roadshow.UserCalendarList
  118. err = json.Unmarshal(body, &userCalendarList)
  119. if err != nil {
  120. err = errors.New("Unmarshal Err:" + err.Error())
  121. return
  122. }
  123. list = userCalendarList.Data
  124. //fmt.Println(response)
  125. //userCalendarList := response.([]interface{})
  126. //
  127. //for _, userCalendar := range userCalendarList {
  128. // fmt.Println(userCalendar)
  129. // fmt.Printf("type :%T;\n", userCalendar)
  130. // //list = append(list, userCalendar.(roadshow.UserCalendar))
  131. //}
  132. //fmt.Println(userCalendarList)
  133. //list = userCalendarList
  134. return
  135. }
  136. // creatSHCalendar 新增上海日历活动
  137. func creatSHCalendar(userPhone, toUserPhone, content, startTime, endTime string, calendarType int8, selfCalendarId int) (err error, errMsg string) {
  138. logMsg := ``
  139. defer func() {
  140. if err != nil {
  141. if logMsg != `` {
  142. errMsg = logMsg
  143. }
  144. }
  145. }()
  146. finalUrl := utils.CRM_OPEN_API_URL + "/v1/Calendar/create"
  147. form := url.Values{
  148. "user_phone": {userPhone},
  149. "to_user_phone": {toUserPhone},
  150. "content": {content},
  151. "start_time": {startTime},
  152. "end_time": {endTime},
  153. }
  154. //发送创建请求
  155. body, err, logMsg := postCurl(finalUrl, form, 0)
  156. if err != nil {
  157. err = errors.New("NewRequest Err:" + err.Error())
  158. return
  159. }
  160. var creatSHCalendarResp roadshow.CreatSHCalendarResp
  161. err = json.Unmarshal(body, &creatSHCalendarResp)
  162. if err != nil {
  163. err = errors.New("Unmarshal Err:" + err.Error())
  164. return
  165. }
  166. calendar := creatSHCalendarResp.Data
  167. //上海系统id
  168. sThirdId := calendar.CalendarID
  169. thirdId, err := strconv.Atoi(sThirdId)
  170. if err != nil {
  171. err = errors.New("string to int Err:" + err.Error())
  172. return
  173. }
  174. // 添加自系统与上海系统的路演关系
  175. relationItem := roadshow.RsCalendarRelation{
  176. UserPhone: userPhone,
  177. CalendarType: calendarType,
  178. SelfCalendarId: selfCalendarId,
  179. ThirdCalendarId: thirdId,
  180. Title: content,
  181. ResearcherMobile: toUserPhone,
  182. ModifyTime: time.Now(),
  183. CreateTime: time.Now(),
  184. }
  185. _, err = roadshow.AddRsCalendarRelation(&relationItem)
  186. if err != nil {
  187. err = errors.New("AddRsCalendarRelation Err:" + err.Error())
  188. fmt.Println(err)
  189. return
  190. }
  191. return
  192. }
  193. // updateSHCalendar 内部函数,只用于发送修改上海日历的http请求
  194. func updateSHCalendar(thirdCalendarId, researcherMobile, content, startTime, endTime string) (calendarResp roadshow.CreatSHCalendarResp, err error) {
  195. finalUrl := utils.CRM_OPEN_API_URL + "/v1/Calendar/update"
  196. form := url.Values{
  197. "calendar_id": {thirdCalendarId},
  198. "to_user_phone": {researcherMobile},
  199. "content": {content},
  200. "start_time": {startTime},
  201. "end_time": {endTime},
  202. }
  203. //发送创建请求
  204. _, err, _ = postCurl(finalUrl, form, 0)
  205. if err != nil {
  206. err = errors.New("NewRequest Err:" + err.Error())
  207. return
  208. }
  209. return
  210. }
  211. // deleteSHCalendar 删除上海方的路演
  212. func deleteSHCalendar(userPhone string, calendarId int) (err error) {
  213. finalUrl := utils.CRM_OPEN_API_URL + "/v1/Calendar/delete"
  214. form := url.Values{
  215. "user_phone": {userPhone},
  216. "calendar_id": {fmt.Sprintf("%v", calendarId)},
  217. }
  218. //发送创建请求
  219. _, err, _ = postCurl(finalUrl, form, 0)
  220. if err != nil {
  221. err = errors.New("NewRequest Err:" + err.Error())
  222. return
  223. }
  224. return
  225. }
  226. // ShResponse 上海数据返回结构体
  227. type ShResponse struct {
  228. Code int `json:"code" description:"1:正常返回;4001:token失效"`
  229. Msg string `json:"msg" description:"文字描述"`
  230. Time int `json:"time" description:"时间戳"`
  231. Data interface{} `json:"data" description:"业务数据"`
  232. }
  233. // getCurl get请求上海接口
  234. func getCurl(urlStr string, params url.Values, num int) (body []byte, err error) {
  235. if err != nil {
  236. go alarm_msg.SendAlarmMsg("get请求上海接口失败,ERR:"+err.Error(), 3)
  237. //go utils.SendEmail(utils.APPNAME+"get请求上海接口失败:"+time.Now().Format("2006-01-02 15:04:05"), "get请求上海接口失败:"+err.Error(), utils.EmailSendToUsers)
  238. }
  239. token, err := GetAccessToken(false)
  240. if err != nil {
  241. return
  242. }
  243. params.Add("access_token", token)
  244. getUrl := urlStr + "?" + params.Encode()
  245. body, err = http.Get(getUrl)
  246. if err != nil {
  247. utils.FileLog.Info(fmt.Sprint("get Err:", err.Error(), ";url:", getUrl, ";params:", params.Encode(), ";response:", string(body)))
  248. err = errors.New("NewRequest Err:" + err.Error())
  249. return
  250. }
  251. var response ShResponse
  252. err = json.Unmarshal(body, &response)
  253. if err != nil {
  254. utils.FileLog.Info(fmt.Sprint("get Err:", err.Error(), ";url:", getUrl, ";params:", params.Encode(), ";response:", string(body)))
  255. err = errors.New("Unmarshal Err:" + err.Error())
  256. return
  257. }
  258. //如果是token失效,同时只是第一次请求(没有尝试强制刷新token,那么重新请求)
  259. if response.Code == 4001 && num <= 0 {
  260. utils.FileLog.Info(fmt.Sprint("get data err", ";url:", getUrl, ";params:", params.Encode(), ";response:", string(body)))
  261. //token失效
  262. _, tmpErr := refreshAccessToken()
  263. if tmpErr != nil {
  264. err = tmpErr
  265. }
  266. num++
  267. params.Del("access_token")
  268. return getCurl(urlStr, params, num)
  269. } else if response.Code != 1 {
  270. utils.FileLog.Info(fmt.Sprint("get data err", ";url:", getUrl, ";params:", params.Encode(), ";response:", string(body)))
  271. err = errors.New(response.Msg)
  272. return
  273. }
  274. itemApiLog := new(roadshow.RsCalendarApiLog)
  275. itemApiLog.CreateTime = time.Now()
  276. itemApiLog.Remark = "get请求上海接口"
  277. itemApiLog.Url = getUrl
  278. itemApiLog.Body = utils.ReplaceSpaceAndWrap(fmt.Sprint(params))
  279. itemApiLog.Result = string(body)
  280. go roadshow.AddRsCalendarApiLog(itemApiLog)
  281. return
  282. }
  283. // postCurl post请求上海接口
  284. func postCurl(urlStr string, form url.Values, num int) (body []byte, err error, errMsg string) {
  285. logMsg := ``
  286. defer func() {
  287. if err != nil {
  288. if logMsg != `` {
  289. errMsg = logMsg
  290. go alarm_msg.SendAlarmMsg("post请求上海接口失败,ERR:"+err.Error()+";errMsg:"+errMsg, 3)
  291. //go utils.SendEmail(utils.APPNAME+"post请求上海接口失败:"+time.Now().Format("2006-01-02 15:04:05"), "post请求上海接口失败:"+errMsg, utils.EmailSendToUsers)
  292. }
  293. }
  294. }()
  295. token, err := GetAccessToken(false)
  296. if err != nil {
  297. return
  298. }
  299. finalUrl := urlStr + "?access_token=" + token
  300. //发送创建请求
  301. resp, err := netHttp.PostForm(finalUrl, form)
  302. if err != nil {
  303. err = errors.New("NewRequest Err:" + err.Error())
  304. return
  305. }
  306. defer resp.Body.Close()
  307. //解析resp并且存入关联表
  308. body, err = ioutil.ReadAll(resp.Body)
  309. if err != nil {
  310. logMsg = fmt.Sprint("post err; request:", form.Encode(), "; errMsg:", err.Error())
  311. utils.FileLog.Info(logMsg)
  312. return
  313. }
  314. logMsg = fmt.Sprint("post request:", form.Encode(), "; response:", string(body))
  315. utils.FileLog.Info(logMsg)
  316. var response ShResponse
  317. err = json.Unmarshal(body, &response)
  318. if err != nil {
  319. utils.FileLog.Info("post Err:", err.Error(), ";url:", finalUrl, ";params:", form.Encode(), ";response:", string(body))
  320. err = errors.New("Unmarshal Err:" + err.Error())
  321. return
  322. }
  323. utils.FileLog.Info(fmt.Sprint("post request url:", finalUrl, ";params:", form.Encode(), ";response:", string(body)))
  324. itemApiLog := new(roadshow.RsCalendarApiLog)
  325. itemApiLog.CreateTime = time.Now()
  326. itemApiLog.Remark = "post请求上海接口"
  327. itemApiLog.Url = urlStr
  328. itemApiLog.Body = utils.ReplaceSpaceAndWrap(fmt.Sprint(form))
  329. itemApiLog.Result = string(body)
  330. go roadshow.AddRsCalendarApiLog(itemApiLog)
  331. //如果是token失效,同时只是第一次请求(没有尝试强制刷新token,那么重新请求)
  332. if response.Code == 4001 && num <= 0 {
  333. //token失效
  334. _, tmpErr := refreshAccessToken()
  335. if tmpErr != nil {
  336. err = tmpErr
  337. }
  338. num++
  339. return postCurl(urlStr, form, num)
  340. } else if response.Code != 1 {
  341. utils.FileLog.Info(fmt.Sprint("post data err", ";url:", finalUrl, ";params:", form.Encode(), ";response:", string(body)))
  342. err = errors.New(response.Msg)
  343. return
  344. }
  345. return
  346. }
  347. // GetAccessToken 获取accessToken
  348. func GetAccessToken(isRefresh bool) (token string, err error) {
  349. defer func() {
  350. if err != nil {
  351. go alarm_msg.SendAlarmMsg("获取上海的token失败,ERR:"+err.Error(), 3)
  352. //go utils.SendEmail(utils.APPNAME+"获取上海的token失败:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
  353. }
  354. }()
  355. token, redisErr := utils.Rc.RedisString("SH_ACCESS_TOKEN")
  356. //如果从redis中accessToken 获取失败或者token为空了,再或者需要强制刷新了,那么重新获取accessToken
  357. if redisErr != nil || token == `` || isRefresh {
  358. return refreshAccessToken()
  359. }
  360. return
  361. }
  362. // CalendarToSH 创建活动时同步上海的前置函数
  363. func CalendarToSH(rsCalendar roadshow.RsCalendar, researcher roadshow.RsCalendarResearcher) {
  364. if researcher.Status != 2 { //研究员的路演申请,需要研究员接受后,再同步到老CRM
  365. return
  366. }
  367. var err error
  368. errMsg := ``
  369. defer func() {
  370. if err != nil {
  371. errMsg = err.Error() + ";" + errMsg
  372. go alarm_msg.SendAlarmMsg("新建上海研究员日历失败,ERR:"+err.Error()+";errMsg:"+errMsg, 3)
  373. //go utils.SendEmail(utils.APPNAME+"新建上海研究员日历失败:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
  374. }
  375. }()
  376. //redis获取创建者及研究员信息
  377. userInfo, err := getAdminInfoById(rsCalendar.SysUserId)
  378. if err != nil {
  379. utils.FileLog.Info("getAdminInfoById err: " + err.Error())
  380. return
  381. }
  382. researcherInfo, err := getAdminInfoById(researcher.ResearcherId)
  383. if err != nil {
  384. utils.FileLog.Info("getAdminInfoById err: " + err.Error())
  385. return
  386. }
  387. sTime, _ := time.ParseInLocation(utils.FormatDateTime, researcher.StartDate+" "+researcher.StartTime, time.Now().Location())
  388. startTime := sTime.Format("2006-01-02 15:04")
  389. eTime, _ := time.ParseInLocation(utils.FormatDateTime, researcher.EndDate+" "+researcher.EndTime, time.Now().Location())
  390. endTime := eTime.Format("2006-01-02 15:04")
  391. // 创建上海路演日程
  392. err, errMsg = creatSHCalendar(userInfo.Mobile, researcherInfo.Mobile, rsCalendar.CompanyName+rsCalendar.Title, startTime, endTime, 1, researcher.RsCalendarResearcherId)
  393. if err != nil {
  394. utils.FileLog.Info("CreatSHCalendar err: " + err.Error())
  395. fmt.Println(err)
  396. return
  397. }
  398. // 更新路演与研究员关系表的同步字段
  399. whereParams := make(map[string]interface{})
  400. updateParams := make(map[string]interface{})
  401. whereParams["rs_calendar_researcher_id"] = researcher.RsCalendarResearcherId
  402. updateParams["is_synced"] = 1
  403. updateParams["modify_time"] = time.Now()
  404. err = roadshow.UpdateCalendarResearcher(whereParams, updateParams)
  405. if err != nil {
  406. utils.FileLog.Info("UpdateCalendarResearcher err: " + err.Error())
  407. fmt.Println("UpdateCalendarResearcher err: " + err.Error())
  408. return
  409. }
  410. // 更新路演的同步字段
  411. calWhereParams := make(map[string]interface{})
  412. calWhereParams["rs_calendar_id"] = rsCalendar.RsCalendarId
  413. err = roadshow.UpdateRsCalendar(calWhereParams, updateParams)
  414. if err != nil {
  415. utils.FileLog.Info("UpdateRsCalendar err: " + err.Error())
  416. fmt.Println("UpdateRsCalendar err: " + err.Error())
  417. return
  418. }
  419. }
  420. // MatterToSH 创建事项时同步上海的前置函数
  421. func MatterToSH(matter roadshow.RsMatters) (err error) {
  422. errMsg := ``
  423. defer func() {
  424. if err != nil {
  425. errMsg = err.Error() + ";" + errMsg
  426. go alarm_msg.SendAlarmMsg("新建上海研究员事项失败,ERR:"+err.Error()+";errMsg:"+errMsg, 3)
  427. //go utils.SendEmail(utils.APPNAME+"新建上海研究员事项失败:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
  428. }
  429. }()
  430. //获取研究员信息
  431. researcherInfo, err := getAdminInfoById(matter.SysUserId)
  432. if err != nil {
  433. //utils.FileLog.Info("getAdminInfoById err: " + err.Error())
  434. return
  435. }
  436. sTime, _ := time.ParseInLocation(utils.FormatDateTime, matter.StartDate+" "+matter.StartTime, time.Now().Location())
  437. startTime := sTime.Format("2006-01-02 15:04")
  438. eTime, _ := time.ParseInLocation(utils.FormatDateTime, matter.EndDate+" "+matter.EndTime, time.Now().Location())
  439. endTime := eTime.Format("2006-01-02 15:04")
  440. err, errMsg = creatSHCalendar(researcherInfo.Mobile, researcherInfo.Mobile, matter.MatterContent, startTime, endTime, 2, matter.RsMattersId)
  441. if err != nil {
  442. return
  443. }
  444. // 修改事项的同步字段
  445. updateParams := make(map[string]interface{})
  446. whereParams := make(map[string]interface{})
  447. updateParams["is_synced"] = 1
  448. whereParams["rs_matters_id"] = matter.RsMattersId
  449. err = roadshow.UpdateRsMatters(whereParams, updateParams)
  450. if err != nil {
  451. err = fmt.Errorf("UpdateRsMatters err: " + err.Error())
  452. }
  453. return
  454. }
  455. // UpdateSHCalendar 更新上海日历活动
  456. func UpdateSHCalendar(rsCalendarId, rsCalendarResearcherId int, activityType, roadshowType, activityCategory, roadshowPlatform, province, city string, calendarResearcherList []*roadshow.CalendarResearcher) (err error) {
  457. errMsg := ``
  458. defer func() {
  459. if errMsg != `` {
  460. if err != nil {
  461. errMsg = err.Error() + ";" + errMsg
  462. }
  463. go alarm_msg.SendAlarmMsg("更新上海日历活动失败,errMsg:"+errMsg, 3)
  464. //go utils.SendEmail(utils.APPNAME+"更新上海日历活动失败:"+time.Now().Format("2006-01-02 15:04:05"), errMsg, utils.EmailSendToUsers)
  465. }
  466. }()
  467. //查询关联表获取信息
  468. var condition string
  469. var pars []interface{}
  470. condition = ` AND self_calendar_id = ? and calendar_type = 1 `
  471. pars = append(pars, rsCalendarResearcherId)
  472. relationItem, err := roadshow.GetRelationByPars(condition, pars)
  473. if err != nil {
  474. err = errors.New("GetRelationByPars err:" + err.Error())
  475. fmt.Println(err)
  476. return
  477. }
  478. //需要同时修改创建人及研究员的日历(目前听说只有1个,先按一个处理吧)
  479. for _, researcher := range calendarResearcherList {
  480. sTime, _ := time.ParseInLocation(utils.FormatDateTime, researcher.StartDate+" "+researcher.StartTime, time.Now().Location())
  481. startTime := sTime.Format("2006-01-02 15:04")
  482. eTime, _ := time.ParseInLocation(utils.FormatDateTime, researcher.EndDate+" "+researcher.EndTime, time.Now().Location())
  483. endTime := eTime.Format("2006-01-02 15:04")
  484. title := getTitle(activityType, roadshowType, activityCategory, roadshowPlatform, province, city)
  485. //发送更新请求
  486. _, err = updateSHCalendar(fmt.Sprintf("%v", relationItem.ThirdCalendarId), relationItem.ResearcherMobile, title, startTime, endTime)
  487. if err != nil {
  488. errMsg += fmt.Sprint("更新研究员日历信息:", relationItem.ThirdCalendarId, ";err:"+err.Error(), ";")
  489. }
  490. //// 更新路演活动的信息
  491. //calWhereParams := make(map[string]interface{})
  492. //calWhereParams["rs_calendar_id"] = rsCalendarId
  493. //err = roadshow.UpdateRsCalendarRelation(calWhereParams, updateParams)
  494. //if err != nil {
  495. // errMsg += fmt.Sprint("UpdateRsCalendar:", relationItem.ThirdCalendarId, ";err:"+err.Error(), ";")
  496. // return
  497. //}
  498. }
  499. // 更新路演活动中 研究员的信息
  500. whereParams := make(map[string]interface{})
  501. updateParams := make(map[string]interface{})
  502. whereParams["rs_calendar_researcher_id"] = rsCalendarResearcherId
  503. updateParams["is_synced"] = 1
  504. updateParams["modify_time"] = time.Now()
  505. err = roadshow.UpdateCalendarResearcher(whereParams, updateParams)
  506. if err != nil {
  507. errMsg += fmt.Sprint("UpdateCalendarResearcher:", relationItem.ThirdCalendarId, ";err:"+err.Error(), ";")
  508. return
  509. }
  510. // 更新路演活动的信息
  511. calWhereParams := make(map[string]interface{})
  512. calWhereParams["rs_calendar_id"] = rsCalendarId
  513. err = roadshow.UpdateRsCalendar(calWhereParams, updateParams)
  514. if err != nil {
  515. errMsg += fmt.Sprint("UpdateRsCalendar:", relationItem.ThirdCalendarId, ";err:"+err.Error(), ";")
  516. return
  517. }
  518. return
  519. }
  520. // UpdateSHCalendarByMatter 更新上海日历活动
  521. func UpdateSHCalendarByMatter(req roadshow.UpdateMattersReq) (err error) {
  522. defer func() {
  523. if err != nil {
  524. go alarm_msg.SendAlarmMsg("更新上海日历活动失败,Err:"+err.Error(), 3)
  525. //go utils.SendEmail(utils.APPNAME+"更新上海日历活动失败:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
  526. }
  527. }()
  528. if err != nil {
  529. return
  530. }
  531. //需要同时修改创建人及研究员的日历
  532. //查询关联表获取信息
  533. var condition string
  534. var pars []interface{}
  535. condition = ` AND self_calendar_id = ? and calendar_type = 2 `
  536. pars = append(pars, req.RsMattersId)
  537. relationItem, err := roadshow.GetRelationByPars(condition, pars)
  538. if err != nil {
  539. err = errors.New("GetRelationByPars err:" + err.Error())
  540. fmt.Println(err)
  541. return
  542. }
  543. sTime, _ := time.ParseInLocation(utils.FormatDateTime, req.StartDate+" "+req.StartTime, time.Now().Location())
  544. startTime := sTime.Format("2006-01-02 15:04")
  545. eTime, _ := time.ParseInLocation(utils.FormatDateTime, req.EndDate+" "+req.EndTime, time.Now().Location())
  546. endTime := eTime.Format("2006-01-02 15:04")
  547. //发送更新请求
  548. _, err = updateSHCalendar(fmt.Sprintf("%v", relationItem.ThirdCalendarId), relationItem.ResearcherMobile, relationItem.CustomerName+req.MatterContent, startTime, endTime)
  549. if err == nil {
  550. updateParams := make(map[string]interface{})
  551. whereParams := make(map[string]interface{})
  552. updateParams["is_synced"] = 1
  553. whereParams["rs_matters_id"] = req.RsMattersId
  554. err = roadshow.UpdateRsMatters(whereParams, updateParams)
  555. if err != nil {
  556. utils.FileLog.Info("UpdateRsMatters err: " + err.Error())
  557. fmt.Println("UpdateRsMatters err: " + err.Error())
  558. return
  559. }
  560. }
  561. return
  562. }
  563. // DeleteSHCalendar 删除上海日历活动
  564. func DeleteSHCalendar(rsCalendarResearcherId int) (err error) {
  565. defer func() {
  566. if err != nil {
  567. go alarm_msg.SendAlarmMsg(fmt.Sprint("删除上海日历活动失败,关系id:", rsCalendarResearcherId, ";err:", err.Error()), 3)
  568. //go utils.SendEmail(utils.APPNAME+"删除上海日历活动失败:"+time.Now().Format("2006-01-02 15:04:05"), fmt.Sprint("关系id:", rsCalendarResearcherId, ";err:", err.Error()), utils.EmailSendToUsers)
  569. }
  570. }()
  571. var condition string
  572. var pars []interface{}
  573. condition = ` AND self_calendar_id = ? and calendar_type = 1 `
  574. pars = append(pars, rsCalendarResearcherId)
  575. relationItem, err := roadshow.GetRelationByPars(condition, pars)
  576. if err != nil {
  577. logInfo := fmt.Sprint("删除上海日历活动失败;rsCalendarResearcherId:,", rsCalendarResearcherId, " ;GetRelationByPars err"+err.Error())
  578. err = errors.New(logInfo)
  579. utils.FileLog.Info(logInfo)
  580. if err.Error() == utils.ErrNoRow() {
  581. err = nil
  582. return
  583. }
  584. return
  585. }
  586. //删除上海方的路演
  587. err = deleteSHCalendar(relationItem.UserPhone, relationItem.ThirdCalendarId)
  588. if err != nil {
  589. return
  590. }
  591. //删除活动表及事项表
  592. err = roadshow.DeleteRsCalendarRelation(relationItem.RelationId)
  593. if err != nil {
  594. err = errors.New("DeleteRsCalendarRelation err:" + err.Error())
  595. return
  596. }
  597. return
  598. }
  599. // DeleteSHMatter 删除上海日历事项
  600. func DeleteSHMatter(matterId int) (err error) {
  601. defer func() {
  602. if err != nil {
  603. go alarm_msg.SendAlarmMsg("删除上海研究员日历失败;ERR:"+err.Error(), 3)
  604. //go utils.SendEmail(utils.APPNAME+"删除上海日历事项失败:", "删除上海研究员日历失败 "+err.Error(), utils.EmailSendToUsers)
  605. }
  606. }()
  607. var condition string
  608. var pars []interface{}
  609. condition = ` AND self_calendar_id = ? and calendar_type = 2 `
  610. pars = append(pars, matterId)
  611. relationItem, err := roadshow.GetRelationByPars(condition, pars)
  612. if err != nil {
  613. err = errors.New(fmt.Sprint("删除上海日历事项失败;matterId:,", matterId, " ;GetRelationByPars err"+err.Error()))
  614. utils.FileLog.Info(err.Error())
  615. if err.Error() == utils.ErrNoRow() {
  616. err = nil
  617. return
  618. }
  619. return
  620. }
  621. //删除上海方的路演
  622. err = deleteSHCalendar(relationItem.UserPhone, relationItem.ThirdCalendarId)
  623. //删除活动表及事项表
  624. err = roadshow.DeleteRsCalendarRelation(relationItem.RelationId)
  625. if err != nil {
  626. err = errors.New("DeleteRsCalendarRelation err:" + err.Error())
  627. return
  628. }
  629. return
  630. }
  631. // SyncCalendarFromShanghai 上海路演数据同步到自系统
  632. func SyncCalendarFromShanghai(userPhone, startDate, endDate string) (err error) {
  633. errMsgList := make([]string, 0)
  634. defer func() {
  635. if len(errMsgList) > 0 {
  636. //fmt.Println("err:", errMsg)
  637. if err != nil {
  638. errMsgList = append(errMsgList, fmt.Sprint("ERR:"+err.Error(), ";"))
  639. }
  640. go alarm_msg.SendAlarmMsg("上海路演数据同步到自系统失败;errMsg:"+strings.Join(errMsgList, "\n"), 3)
  641. //go utils.SendEmail(utils.APPNAME+"上海路演数据同步到自系统失败:"+time.Now().Format("2006-01-02 15:04:05"), errMsg, utils.EmailSendToUsers)
  642. }
  643. }()
  644. //fmt.Println(userPhone, startDate, endDate)
  645. endDateTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  646. if err != nil {
  647. errMsgList = append(errMsgList, `结束日期转time类型失败,err:`+err.Error())
  648. return
  649. }
  650. //往后延迟一天,避免截止到当天
  651. endDate = endDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  652. currDay := time.Now().Format(utils.FormatDate)
  653. nowDateTime, _ := time.ParseInLocation(utils.FormatDate, currDay, time.Local)
  654. //如果传入的结束日期早于当前日期,那么就不去查询了
  655. if endDateTime.Before(nowDateTime) {
  656. return
  657. }
  658. //获取当前研究员信息
  659. userPhoneInfo, err := getAdminInfo(userPhone)
  660. if err != nil {
  661. errMsgList = append(errMsgList, fmt.Sprint("获取手机号信息失败:手机号:", userPhone, ";err:"+err.Error(), ";"))
  662. return
  663. }
  664. //根据研究员和开始/结束日期获取上海的活动路演
  665. rsCalendarResearcherList, err := roadshow.GetRsCalendarResearcherInfoIByResearcherIdAndDate(userPhoneInfo.AdminId, currDay, endDate)
  666. if err != nil {
  667. errMsgList = append(errMsgList, fmt.Sprint("获取研究员日程信息失败:手机号:", userPhone, ";err:"+err.Error(), ";"))
  668. return
  669. }
  670. //待删除的活动路演
  671. deleteRsCalendarResearcherMap := make(map[int][]*roadshow.RsCalendarResearcherRelationInfo)
  672. for _, v := range rsCalendarResearcherList {
  673. tmpList, ok := deleteRsCalendarResearcherMap[v.ThirdCalendarId]
  674. if !ok {
  675. tmpList = make([]*roadshow.RsCalendarResearcherRelationInfo, 0)
  676. }
  677. tmpList = append(tmpList, v)
  678. deleteRsCalendarResearcherMap[v.ThirdCalendarId] = tmpList
  679. }
  680. //以当前日期作为起始日期去同步
  681. list, err := getCalendarFrom(userPhone, currDay, endDate)
  682. if err != nil {
  683. errMsgList = append(errMsgList, fmt.Sprint("获取第三方路演日历数据失败,", "userPhone:", userPhone, ";currDay:", currDay, ";endDate:", endDate, ";err:"+err.Error()))
  684. return
  685. }
  686. thirdIdList := make([]int, 0)
  687. if len(list) > 0 {
  688. for _, v := range list {
  689. thirdIdList = append(thirdIdList, v.ID)
  690. //移除还存在的活动,保留不存在的活动
  691. delete(deleteRsCalendarResearcherMap, v.ID)
  692. }
  693. rsCalendarRelationList, tmpErr := roadshow.GetRsCalendarRelationListByThirdIds(thirdIdList)
  694. if tmpErr != nil {
  695. err = tmpErr
  696. errMsgList = append(errMsgList, "获取关联列表失败,err:"+tmpErr.Error())
  697. return
  698. }
  699. rsCalendarRelationMap := make(map[int]*roadshow.RsCalendarRelation)
  700. for _, rsCalendarRelation := range rsCalendarRelationList {
  701. rsCalendarRelationMap[rsCalendarRelation.ThirdCalendarId] = rsCalendarRelation
  702. }
  703. for _, v := range list {
  704. //展示优先级:1、customer_name 2、project_name 3、title 需求池953
  705. if v.CustomerName != "" {
  706. v.Title = v.CustomerName
  707. } else if v.ProjectName != "" {
  708. v.Title = v.ProjectName
  709. }
  710. if rsCalendarRelation, ok := rsCalendarRelationMap[v.ID]; ok {
  711. //存在的话,那么就去查找对应的信息
  712. if rsCalendarRelation.CalendarType == 1 {
  713. //路演
  714. rsCalendarResearcherInfo, tmpErr := roadshow.GetRsCalendarResearcherById(rsCalendarRelation.SelfCalendarId)
  715. if tmpErr != nil {
  716. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "获取路演研究员信息失败;err:"+tmpErr.Error(), ";"))
  717. continue
  718. }
  719. rsCalendarInfo, tmpErr := roadshow.GetRsCalendarById(rsCalendarResearcherInfo.RsCalendarId)
  720. if tmpErr != nil {
  721. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "获取路演信息失败;err:"+tmpErr.Error(), ";"))
  722. continue
  723. }
  724. if rsCalendarInfo.Source == 0 { //自系统创建的路演活动,不需要依靠上海方来修改
  725. continue
  726. }
  727. //是否去同步变更数据库
  728. isUpdateSync := false
  729. //是否变更
  730. isUpdate := false
  731. if v.StartTime != rsCalendarRelation.StartTime {
  732. isUpdate = true
  733. }
  734. if v.EndTime != rsCalendarRelation.EndTime {
  735. isUpdate = true
  736. }
  737. if v.Title != rsCalendarRelation.Title {
  738. isUpdate = true
  739. }
  740. if rsCalendarResearcherInfo.Status == 4 { // 被删除了,现在是需要更新
  741. isUpdate = true
  742. }
  743. researcherList := make([]system.AdminItem, 0)
  744. researcherMap := make(map[int]system.AdminItem)
  745. addResearcherList := make([]system.AdminItem, 0) // 需要新增的研究员
  746. delResearcherIdList := make([]int, 0) //需要删除的路演活动与研究员的关系id
  747. updateResearcherList := make([]*roadshow.RsCalendarResearcher, 0) //待更新的路演活动中研究员信息
  748. if v.ResearcherMobile != rsCalendarRelation.ResearcherMobile {
  749. //研究员变更了,需要去改表
  750. isUpdateSync = true
  751. researcherMobileList := strings.Split(v.ResearcherMobile, ",")
  752. if len(researcherMobileList) > 0 {
  753. for _, mobile := range researcherMobileList {
  754. researcherInfo, tmpErr := getAdminInfo(mobile)
  755. if tmpErr != nil {
  756. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "获取研究员失败,研究员手机号:", mobile, ";err:"+tmpErr.Error(), ";"))
  757. continue
  758. }
  759. if researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_RESEARCHR ||
  760. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RESEARCHR ||
  761. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_RESEARCHR ||
  762. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT ||
  763. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT ||
  764. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER ||
  765. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  766. researcherList = append(researcherList, researcherInfo)
  767. researcherMap[researcherInfo.AdminId] = researcherInfo
  768. }
  769. }
  770. }
  771. //没有研究员
  772. if len(researcherList) <= 0 {
  773. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, ";对方研究员信息失败;"))
  774. continue
  775. }
  776. rsCalendarResearcherList, tmpErr := roadshow.GetRsCalendarResearcherListByRsCalendarId(rsCalendarInfo.RsCalendarId)
  777. if tmpErr != nil {
  778. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "获取路演活动中的研究员列表失败,路演活动ID:", rsCalendarInfo.RsCalendarId, ";err:"+tmpErr.Error(), ";"))
  779. continue
  780. }
  781. //现有活动中的研究员
  782. rsCalendarResearcherMap := make(map[int]*roadshow.RsCalendarResearcher)
  783. for _, rsCalendarResearcher := range rsCalendarResearcherList {
  784. if _, ok := researcherMap[rsCalendarResearcher.ResearcherId]; ok {
  785. if isUpdate {
  786. updateResearcherList = append(updateResearcherList, rsCalendarResearcher)
  787. }
  788. } else {
  789. delResearcherIdList = append(delResearcherIdList, rsCalendarResearcher.RsCalendarResearcherId)
  790. }
  791. rsCalendarResearcherMap[rsCalendarResearcher.ResearcherId] = rsCalendarResearcher
  792. }
  793. //校验是否需要新增研究员
  794. for adminId, researcherInfo := range researcherMap {
  795. //如果
  796. if _, ok := rsCalendarResearcherMap[adminId]; !ok {
  797. addResearcherList = append(addResearcherList, researcherInfo)
  798. }
  799. }
  800. } else if isUpdate { //如果有字段更新,那么需要将所有的研究员信息更新
  801. isUpdateSync = true
  802. rsCalendarResearcherList, tmpErr := roadshow.GetRsCalendarResearcherListByRsCalendarId(rsCalendarInfo.RsCalendarId)
  803. if tmpErr != nil {
  804. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "获取路演活动中的研究员列表失败,路演活动ID:", rsCalendarInfo.RsCalendarId, ";err:"+tmpErr.Error(), ";"))
  805. continue
  806. }
  807. for _, rsCalendarResearcher := range rsCalendarResearcherList {
  808. updateResearcherList = append(updateResearcherList, rsCalendarResearcher)
  809. }
  810. }
  811. if isUpdateSync {
  812. tmpErr = roadshow.UpdateSyncRsCalendarRelation(v, rsCalendarInfo, rsCalendarRelation, updateResearcherList, delResearcherIdList, addResearcherList)
  813. if tmpErr != nil {
  814. err = tmpErr
  815. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "修改关联关系失败;err:"+tmpErr.Error(), ";"))
  816. return
  817. }
  818. }
  819. } else {
  820. //事项
  821. //事项都是由自系统创建的,不需要依靠上海方来修改
  822. }
  823. } else {
  824. //数据不存在
  825. createUser, tmpErr := getAdminInfo(v.UserPhone)
  826. if tmpErr != nil {
  827. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "获取创建人失败,创建人手机号:", v.UserPhone, ";err:"+tmpErr.Error(), ";"))
  828. continue
  829. }
  830. //研究员列表
  831. researcherList := make([]system.AdminItem, 0)
  832. researcherMobileList := strings.Split(v.ResearcherMobile, ",")
  833. if len(researcherMobileList) > 0 {
  834. for _, mobile := range researcherMobileList {
  835. researcherInfo, tmpErr := getAdminInfo(mobile)
  836. if tmpErr != nil {
  837. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "获取研究员失败,研究员手机号:", mobile, ";err:"+tmpErr.Error(), ";"))
  838. continue
  839. }
  840. if researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_RESEARCHR ||
  841. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RESEARCHR ||
  842. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_RESEARCHR ||
  843. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT ||
  844. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT ||
  845. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER ||
  846. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN ||
  847. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  848. researcherList = append(researcherList, researcherInfo)
  849. }
  850. }
  851. }
  852. //没有研究员
  853. if len(researcherList) <= 0 {
  854. continue
  855. }
  856. //数据入库
  857. tmpErr = roadshow.SyncRsCalendarRelation(v, createUser, researcherList)
  858. if tmpErr != nil {
  859. err = tmpErr
  860. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "绑定关联关系失败;err:"+tmpErr.Error(), ";"))
  861. continue
  862. }
  863. //fmt.Println("createUser:", createUser)
  864. }
  865. }
  866. }
  867. var itemsApiLog []*roadshow.RsCalendarApiLog
  868. //上海那边已经删除了路演,这边也要同步删除
  869. for _, deleteRsCalendarResearcherList := range deleteRsCalendarResearcherMap {
  870. for _, deleteRsCalendarResearcher := range deleteRsCalendarResearcherList {
  871. // 更新路演与研究员关系表的状态字段
  872. whereParams := make(map[string]interface{})
  873. updateParams := make(map[string]interface{})
  874. whereParams["rs_calendar_researcher_id"] = deleteRsCalendarResearcher.RsCalendarResearcherId
  875. updateParams["status"] = 4
  876. updateParams["modify_time"] = time.Now()
  877. tmpErr := roadshow.UpdateCalendarResearcher(whereParams, updateParams)
  878. if tmpErr != nil {
  879. err = tmpErr
  880. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", deleteRsCalendarResearcher.ThirdCalendarId, "删除关联关系失败;err:"+tmpErr.Error(), ";"))
  881. continue
  882. }
  883. itemApiLog := new(roadshow.RsCalendarApiLog)
  884. itemApiLog.CreateTime = time.Now()
  885. itemApiLog.Remark = "对比上海接口之后手动删除"
  886. itemApiLog.Url = fmt.Sprint("userPhone", userPhone, "currDay", currDay, "endDate", endDate)
  887. itemApiLog.Body = ""
  888. itemApiLog.Result = ""
  889. itemApiLog.RsCalendarResearcherId = deleteRsCalendarResearcher.RsCalendarResearcherId
  890. itemsApiLog = append(itemsApiLog, itemApiLog)
  891. // 更新路演的同步字段
  892. //calWhereParams := make(map[string]interface{})
  893. //calUpdateParams := make(map[string]interface{})
  894. //calWhereParams["rs_calendar_id"] = deleteRsCalendarResearcher.RsCalendarId
  895. //updateParams["is_synced"] = 1
  896. //err = roadshow.UpdateRsCalendar(calWhereParams, calUpdateParams)
  897. }
  898. }
  899. if len(itemsApiLog) > 0 {
  900. go roadshow.AddRsCalendarApiLogList(itemsApiLog)
  901. }
  902. return
  903. }
  904. // CreateOaUser 新增系统用户
  905. func CreateOaUser(mobile, username string, groupId int) (err error) {
  906. defer func() {
  907. if err != nil {
  908. errMsg := fmt.Sprint("手机号:", mobile, ";username:", username, ";分组id:", groupId, ";err:", err.Error())
  909. go alarm_msg.SendAlarmMsg("系统用户同步到上海失败;ERR:"+err.Error()+";errMsg:"+errMsg, 3)
  910. //go utils.SendEmail(utils.APPNAME+"系统用户同步到上海失败:"+time.Now().Format("2006-01-02 15:04:05"), errMsg, utils.EmailSendToUsers)
  911. }
  912. }()
  913. finalUrl := utils.CRM_OPEN_API_URL + "/v1/OaUser/create"
  914. //发送创建请求
  915. form := url.Values{}
  916. form.Add("mobile", mobile)
  917. form.Add("username", username)
  918. form.Add("group_id", fmt.Sprint(groupId))
  919. //发送创建请求
  920. _, err, _ = postCurl(finalUrl, form, 0)
  921. if err != nil {
  922. err = errors.New("NewRequest Err:" + err.Error())
  923. return
  924. }
  925. return
  926. }
  927. // getAdminInfo 通过手机号获取用户信息
  928. func getAdminInfo(mobile string) (adminInfo system.AdminItem, err error) {
  929. adminMap := make(map[string]system.AdminItem)
  930. redisAdminData, redisErr := utils.Rc.RedisString(utils.CACHE_KEY_ADMIN)
  931. if redisErr != nil {
  932. list, err := system.GetSysuserList("", []interface{}{}, 0, 1000)
  933. // GetSysuserList
  934. for _, tmpAdminInfo := range list {
  935. adminMap[tmpAdminInfo.Mobile] = *tmpAdminInfo
  936. }
  937. //入缓存
  938. redisJsonData, err := json.Marshal(adminMap)
  939. if err == nil {
  940. _ = utils.Rc.Put(utils.CACHE_KEY_ADMIN, string(redisJsonData), time.Minute*30)
  941. }
  942. } else {
  943. err := json.Unmarshal([]byte(redisAdminData), &adminMap)
  944. if err != nil {
  945. fmt.Println("用户数据,json转换失败:", err)
  946. }
  947. }
  948. adminInfo, ok := adminMap[mobile]
  949. if !ok {
  950. err = fmt.Errorf("找不到该手机号用户,mobile:" + mobile)
  951. }
  952. return
  953. }
  954. // getAdminInfoById 通过用户id获取用户信息
  955. func getAdminInfoById(sysId int) (adminInfo system.AdminItem, err error) {
  956. adminMap := make(map[int]system.AdminItem)
  957. redisAdminData, redisErr := utils.Rc.RedisString(utils.CACHE_KEY_ADMIN_ID)
  958. if redisErr != nil {
  959. list, err := system.GetSysuserList("", []interface{}{}, 0, 1000)
  960. // GetSysuserList
  961. for _, tmpAdminInfo := range list {
  962. adminMap[tmpAdminInfo.AdminId] = *tmpAdminInfo
  963. }
  964. //入缓存
  965. redisJsonData, err := json.Marshal(adminMap)
  966. if err == nil {
  967. _ = utils.Rc.Put(utils.CACHE_KEY_ADMIN_ID, string(redisJsonData), time.Minute*30)
  968. }
  969. } else {
  970. err := json.Unmarshal([]byte(redisAdminData), &adminMap)
  971. if err != nil {
  972. fmt.Println("用户数据,json转换失败:", err)
  973. }
  974. }
  975. adminInfo, ok := adminMap[sysId]
  976. if !ok {
  977. err = fmt.Errorf("找不到该用户id,sysId:" + fmt.Sprint(sysId))
  978. }
  979. return
  980. }
  981. func getTitle(activityType, roadshowType, activityCategory, roadshowPlatform, province, city string) string {
  982. var title string
  983. switch activityType {
  984. case "内部会议":
  985. title = "内部会议"
  986. case "公开会议", "路演":
  987. if roadshowType == "线上" {
  988. title = roadshowType + activityType + roadshowPlatform
  989. } else {
  990. title = roadshowType + activityType + province + city
  991. }
  992. case "报告电话会":
  993. title = activityCategory + "电话会"
  994. }
  995. return title
  996. }
  997. // GetCalendarFrom 获取上海方的路演活动列表
  998. func GetCalendarFrom(userPhone, startDate, endDate string) (list []roadshow.UserCalendar, err error) {
  999. list, err = getCalendarFrom(userPhone, startDate, endDate)
  1000. return
  1001. }