calendar.go 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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. return
  275. }
  276. // postCurl post请求上海接口
  277. func postCurl(urlStr string, form url.Values, num int) (body []byte, err error, errMsg string) {
  278. logMsg := ``
  279. defer func() {
  280. if err != nil {
  281. if logMsg != `` {
  282. errMsg = logMsg
  283. go alarm_msg.SendAlarmMsg("post请求上海接口失败,ERR:"+err.Error()+";errMsg:"+errMsg, 3)
  284. //go utils.SendEmail(utils.APPNAME+"post请求上海接口失败:"+time.Now().Format("2006-01-02 15:04:05"), "post请求上海接口失败:"+errMsg, utils.EmailSendToUsers)
  285. }
  286. }
  287. }()
  288. token, err := GetAccessToken(false)
  289. if err != nil {
  290. return
  291. }
  292. finalUrl := urlStr + "?access_token=" + token
  293. //发送创建请求
  294. resp, err := netHttp.PostForm(finalUrl, form)
  295. if err != nil {
  296. err = errors.New("NewRequest Err:" + err.Error())
  297. return
  298. }
  299. defer resp.Body.Close()
  300. //解析resp并且存入关联表
  301. body, err = ioutil.ReadAll(resp.Body)
  302. if err != nil {
  303. logMsg = fmt.Sprint("post err; request:", form.Encode(), "; errMsg:", err.Error())
  304. utils.FileLog.Info(logMsg)
  305. return
  306. }
  307. logMsg = fmt.Sprint("post request:", form.Encode(), "; response:", string(body))
  308. utils.FileLog.Info(logMsg)
  309. var response ShResponse
  310. err = json.Unmarshal(body, &response)
  311. if err != nil {
  312. utils.FileLog.Info("post Err:", err.Error(), ";url:", finalUrl, ";params:", form.Encode(), ";response:", string(body))
  313. err = errors.New("Unmarshal Err:" + err.Error())
  314. return
  315. }
  316. utils.FileLog.Info(fmt.Sprint("post request url:", finalUrl, ";params:", form.Encode(), ";response:", string(body)))
  317. //如果是token失效,同时只是第一次请求(没有尝试强制刷新token,那么重新请求)
  318. if response.Code == 4001 && num <= 0 {
  319. //token失效
  320. _, tmpErr := refreshAccessToken()
  321. if tmpErr != nil {
  322. err = tmpErr
  323. }
  324. num++
  325. return postCurl(urlStr, form, num)
  326. } else if response.Code != 1 {
  327. utils.FileLog.Info(fmt.Sprint("post data err", ";url:", finalUrl, ";params:", form.Encode(), ";response:", string(body)))
  328. err = errors.New(response.Msg)
  329. return
  330. }
  331. return
  332. }
  333. // GetAccessToken 获取accessToken
  334. func GetAccessToken(isRefresh bool) (token string, err error) {
  335. defer func() {
  336. if err != nil {
  337. go alarm_msg.SendAlarmMsg("获取上海的token失败,ERR:"+err.Error(), 3)
  338. //go utils.SendEmail(utils.APPNAME+"获取上海的token失败:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
  339. }
  340. }()
  341. token, redisErr := utils.Rc.RedisString("SH_ACCESS_TOKEN")
  342. //如果从redis中accessToken 获取失败或者token为空了,再或者需要强制刷新了,那么重新获取accessToken
  343. if redisErr != nil || token == `` || isRefresh {
  344. return refreshAccessToken()
  345. }
  346. return
  347. }
  348. // CalendarToSH 创建活动时同步上海的前置函数
  349. func CalendarToSH(rsCalendar roadshow.RsCalendar, researcher roadshow.RsCalendarResearcher) {
  350. var err error
  351. errMsg := ``
  352. defer func() {
  353. if err != nil {
  354. errMsg = err.Error() + ";" + errMsg
  355. go alarm_msg.SendAlarmMsg("新建上海研究员日历失败,ERR:"+err.Error()+";errMsg:"+errMsg, 3)
  356. //go utils.SendEmail(utils.APPNAME+"新建上海研究员日历失败:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
  357. }
  358. }()
  359. //redis获取创建者及研究员信息
  360. userInfo, err := getAdminInfoById(rsCalendar.SysUserId)
  361. if err != nil {
  362. utils.FileLog.Info("getAdminInfoById err: " + err.Error())
  363. return
  364. }
  365. researcherInfo, err := getAdminInfoById(researcher.ResearcherId)
  366. if err != nil {
  367. utils.FileLog.Info("getAdminInfoById err: " + err.Error())
  368. return
  369. }
  370. sTime, _ := time.ParseInLocation(utils.FormatDateTime, researcher.StartDate+" "+researcher.StartTime, time.Now().Location())
  371. startTime := sTime.Format("2006-01-02 15:04")
  372. eTime, _ := time.ParseInLocation(utils.FormatDateTime, researcher.EndDate+" "+researcher.EndTime, time.Now().Location())
  373. endTime := eTime.Format("2006-01-02 15:04")
  374. // 创建上海路演日程
  375. err, errMsg = creatSHCalendar(userInfo.Mobile, researcherInfo.Mobile, rsCalendar.Title, startTime, endTime, 1, researcher.RsCalendarResearcherId)
  376. if err != nil {
  377. utils.FileLog.Info("CreatSHCalendar err: " + err.Error())
  378. fmt.Println(err)
  379. return
  380. }
  381. // 更新路演与研究员关系表的同步字段
  382. whereParams := make(map[string]interface{})
  383. updateParams := make(map[string]interface{})
  384. whereParams["rs_calendar_researcher_id"] = researcher.RsCalendarResearcherId
  385. updateParams["is_synced"] = 1
  386. updateParams["modify_time"] = time.Now()
  387. err = roadshow.UpdateCalendarResearcher(whereParams, updateParams)
  388. if err != nil {
  389. utils.FileLog.Info("UpdateCalendarResearcher err: " + err.Error())
  390. fmt.Println("UpdateCalendarResearcher err: " + err.Error())
  391. return
  392. }
  393. // 更新路演的同步字段
  394. calWhereParams := make(map[string]interface{})
  395. calWhereParams["rs_calendar_id"] = rsCalendar.RsCalendarId
  396. err = roadshow.UpdateRsCalendar(calWhereParams, updateParams)
  397. if err != nil {
  398. utils.FileLog.Info("UpdateRsCalendar err: " + err.Error())
  399. fmt.Println("UpdateRsCalendar err: " + err.Error())
  400. return
  401. }
  402. }
  403. // MatterToSH 创建事项时同步上海的前置函数
  404. func MatterToSH(matter roadshow.RsMatters) (err error) {
  405. errMsg := ``
  406. defer func() {
  407. if err != nil {
  408. errMsg = err.Error() + ";" + errMsg
  409. go alarm_msg.SendAlarmMsg("新建上海研究员事项失败,ERR:"+err.Error()+";errMsg:"+errMsg, 3)
  410. //go utils.SendEmail(utils.APPNAME+"新建上海研究员事项失败:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
  411. }
  412. }()
  413. //获取研究员信息
  414. researcherInfo, err := getAdminInfoById(matter.SysUserId)
  415. if err != nil {
  416. //utils.FileLog.Info("getAdminInfoById err: " + err.Error())
  417. return
  418. }
  419. sTime, _ := time.ParseInLocation(utils.FormatDateTime, matter.StartDate+" "+matter.StartTime, time.Now().Location())
  420. startTime := sTime.Format("2006-01-02 15:04")
  421. eTime, _ := time.ParseInLocation(utils.FormatDateTime, matter.EndDate+" "+matter.EndTime, time.Now().Location())
  422. endTime := eTime.Format("2006-01-02 15:04")
  423. err, errMsg = creatSHCalendar(researcherInfo.Mobile, researcherInfo.Mobile, matter.MatterContent, startTime, endTime, 2, matter.RsMattersId)
  424. if err != nil {
  425. return
  426. }
  427. // 修改事项的同步字段
  428. updateParams := make(map[string]interface{})
  429. whereParams := make(map[string]interface{})
  430. updateParams["is_synced"] = 1
  431. whereParams["rs_matters_id"] = matter.RsMattersId
  432. err = roadshow.UpdateRsMatters(whereParams, updateParams)
  433. if err != nil {
  434. err = fmt.Errorf("UpdateRsMatters err: " + err.Error())
  435. }
  436. return
  437. }
  438. // UpdateSHCalendar 更新上海日历活动
  439. func UpdateSHCalendar(rsCalendarId, rsCalendarResearcherId int, activityType, roadshowType, activityCategory, roadshowPlatform, province, city string, calendarResearcherList []*roadshow.CalendarResearcher) (err error) {
  440. errMsg := ``
  441. defer func() {
  442. if errMsg != `` {
  443. if err != nil {
  444. errMsg = err.Error() + ";" + errMsg
  445. }
  446. go alarm_msg.SendAlarmMsg("更新上海日历活动失败,errMsg:"+errMsg, 3)
  447. //go utils.SendEmail(utils.APPNAME+"更新上海日历活动失败:"+time.Now().Format("2006-01-02 15:04:05"), errMsg, utils.EmailSendToUsers)
  448. }
  449. }()
  450. //查询关联表获取信息
  451. var condition string
  452. var pars []interface{}
  453. condition = ` AND self_calendar_id = ? and calendar_type = 1 `
  454. pars = append(pars, rsCalendarResearcherId)
  455. relationItem, err := roadshow.GetRelationByPars(condition, pars)
  456. if err != nil {
  457. err = errors.New("GetRelationByPars err:" + err.Error())
  458. fmt.Println(err)
  459. return
  460. }
  461. //需要同时修改创建人及研究员的日历(目前听说只有1个,先按一个处理吧)
  462. for _, researcher := range calendarResearcherList {
  463. sTime, _ := time.ParseInLocation(utils.FormatDateTime, researcher.StartDate+" "+researcher.StartTime, time.Now().Location())
  464. startTime := sTime.Format("2006-01-02 15:04")
  465. eTime, _ := time.ParseInLocation(utils.FormatDateTime, researcher.EndDate+" "+researcher.EndTime, time.Now().Location())
  466. endTime := eTime.Format("2006-01-02 15:04")
  467. title := getTitle(activityType, roadshowType, activityCategory, roadshowPlatform, province, city)
  468. //发送更新请求
  469. _, err = updateSHCalendar(fmt.Sprintf("%v", relationItem.ThirdCalendarId), relationItem.ResearcherMobile, title, startTime, endTime)
  470. if err != nil {
  471. errMsg += fmt.Sprint("更新研究员日历信息:", relationItem.ThirdCalendarId, ";err:"+err.Error(), ";")
  472. }
  473. //// 更新路演活动的信息
  474. //calWhereParams := make(map[string]interface{})
  475. //calWhereParams["rs_calendar_id"] = rsCalendarId
  476. //err = roadshow.UpdateRsCalendarRelation(calWhereParams, updateParams)
  477. //if err != nil {
  478. // errMsg += fmt.Sprint("UpdateRsCalendar:", relationItem.ThirdCalendarId, ";err:"+err.Error(), ";")
  479. // return
  480. //}
  481. }
  482. // 更新路演活动中 研究员的信息
  483. whereParams := make(map[string]interface{})
  484. updateParams := make(map[string]interface{})
  485. whereParams["rs_calendar_researcher_id"] = rsCalendarResearcherId
  486. updateParams["is_synced"] = 1
  487. updateParams["modify_time"] = time.Now()
  488. err = roadshow.UpdateCalendarResearcher(whereParams, updateParams)
  489. if err != nil {
  490. errMsg += fmt.Sprint("UpdateCalendarResearcher:", relationItem.ThirdCalendarId, ";err:"+err.Error(), ";")
  491. return
  492. }
  493. // 更新路演活动的信息
  494. calWhereParams := make(map[string]interface{})
  495. calWhereParams["rs_calendar_id"] = rsCalendarId
  496. err = roadshow.UpdateRsCalendar(calWhereParams, updateParams)
  497. if err != nil {
  498. errMsg += fmt.Sprint("UpdateRsCalendar:", relationItem.ThirdCalendarId, ";err:"+err.Error(), ";")
  499. return
  500. }
  501. return
  502. }
  503. // UpdateSHCalendarByMatter 更新上海日历活动
  504. func UpdateSHCalendarByMatter(req roadshow.UpdateMattersReq) (err error) {
  505. defer func() {
  506. if err != nil {
  507. go alarm_msg.SendAlarmMsg("更新上海日历活动失败,Err:"+err.Error(), 3)
  508. //go utils.SendEmail(utils.APPNAME+"更新上海日历活动失败:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
  509. }
  510. }()
  511. if err != nil {
  512. return
  513. }
  514. //需要同时修改创建人及研究员的日历
  515. //查询关联表获取信息
  516. var condition string
  517. var pars []interface{}
  518. condition = ` AND self_calendar_id = ? and calendar_type = 2 `
  519. pars = append(pars, req.RsMattersId)
  520. relationItem, err := roadshow.GetRelationByPars(condition, pars)
  521. if err != nil {
  522. err = errors.New("GetRelationByPars err:" + err.Error())
  523. fmt.Println(err)
  524. }
  525. sTime, _ := time.ParseInLocation(utils.FormatDateTime, req.StartDate+" "+req.StartTime, time.Now().Location())
  526. startTime := sTime.Format("2006-01-02 15:04")
  527. eTime, _ := time.ParseInLocation(utils.FormatDateTime, req.EndDate+" "+req.EndTime, time.Now().Location())
  528. endTime := eTime.Format("2006-01-02 15:04")
  529. //发送更新请求
  530. _, err = updateSHCalendar(fmt.Sprintf("%v", relationItem.ThirdCalendarId), relationItem.ResearcherMobile, req.MatterContent, startTime, endTime)
  531. if err == nil {
  532. updateParams := make(map[string]interface{})
  533. whereParams := make(map[string]interface{})
  534. updateParams["is_synced"] = 1
  535. whereParams["rs_matters_id"] = req.RsMattersId
  536. err = roadshow.UpdateRsMatters(whereParams, updateParams)
  537. if err != nil {
  538. utils.FileLog.Info("UpdateRsMatters err: " + err.Error())
  539. fmt.Println("UpdateRsMatters err: " + err.Error())
  540. return
  541. }
  542. }
  543. return
  544. }
  545. // DeleteSHCalendar 删除上海日历活动
  546. func DeleteSHCalendar(rsCalendarResearcherId int) (err error) {
  547. defer func() {
  548. if err != nil {
  549. go alarm_msg.SendAlarmMsg(fmt.Sprint("删除上海日历活动失败,关系id:", rsCalendarResearcherId, ";err:", err.Error()), 3)
  550. //go utils.SendEmail(utils.APPNAME+"删除上海日历活动失败:"+time.Now().Format("2006-01-02 15:04:05"), fmt.Sprint("关系id:", rsCalendarResearcherId, ";err:", err.Error()), utils.EmailSendToUsers)
  551. }
  552. }()
  553. var condition string
  554. var pars []interface{}
  555. condition = ` AND self_calendar_id = ? and calendar_type = 1 `
  556. pars = append(pars, rsCalendarResearcherId)
  557. relationItem, err := roadshow.GetRelationByPars(condition, pars)
  558. if err != nil {
  559. logInfo := fmt.Sprint("删除上海日历活动失败;rsCalendarResearcherId:,", rsCalendarResearcherId, " ;GetRelationByPars err"+err.Error())
  560. err = errors.New(logInfo)
  561. utils.FileLog.Info(logInfo)
  562. if err.Error() == utils.ErrNoRow() {
  563. err = nil
  564. return
  565. }
  566. return
  567. }
  568. //删除上海方的路演
  569. err = deleteSHCalendar(relationItem.UserPhone, relationItem.ThirdCalendarId)
  570. if err != nil {
  571. return
  572. }
  573. //删除活动表及事项表
  574. err = roadshow.DeleteRsCalendarRelation(relationItem.RelationId)
  575. if err != nil {
  576. err = errors.New("DeleteRsCalendarRelation err:" + err.Error())
  577. return
  578. }
  579. return
  580. }
  581. // DeleteSHMatter 删除上海日历事项
  582. func DeleteSHMatter(matterId int) (err error) {
  583. defer func() {
  584. if err != nil {
  585. go alarm_msg.SendAlarmMsg("删除上海研究员日历失败;ERR:"+err.Error(), 3)
  586. //go utils.SendEmail(utils.APPNAME+"删除上海日历事项失败:", "删除上海研究员日历失败 "+err.Error(), utils.EmailSendToUsers)
  587. }
  588. }()
  589. var condition string
  590. var pars []interface{}
  591. condition = ` AND self_calendar_id = ? and calendar_type = 2 `
  592. pars = append(pars, matterId)
  593. relationItem, err := roadshow.GetRelationByPars(condition, pars)
  594. if err != nil {
  595. err = errors.New(fmt.Sprint("删除上海日历事项失败;matterId:,", matterId, " ;GetRelationByPars err"+err.Error()))
  596. utils.FileLog.Info(err.Error())
  597. if err.Error() == utils.ErrNoRow() {
  598. err = nil
  599. return
  600. }
  601. return
  602. }
  603. //删除上海方的路演
  604. err = deleteSHCalendar(relationItem.UserPhone, relationItem.ThirdCalendarId)
  605. //删除活动表及事项表
  606. err = roadshow.DeleteRsCalendarRelation(relationItem.RelationId)
  607. if err != nil {
  608. err = errors.New("DeleteRsCalendarRelation err:" + err.Error())
  609. return
  610. }
  611. return
  612. }
  613. // SyncCalendarFromShanghai 上海路演数据同步到自系统
  614. func SyncCalendarFromShanghai(userPhone, startDate, endDate string) (err error) {
  615. errMsgList := make([]string, 0)
  616. defer func() {
  617. if len(errMsgList) > 0 {
  618. //fmt.Println("err:", errMsg)
  619. if err != nil {
  620. errMsgList = append(errMsgList, fmt.Sprint("ERR:"+err.Error(), ";"))
  621. }
  622. go alarm_msg.SendAlarmMsg("上海路演数据同步到自系统失败;errMsg:"+strings.Join(errMsgList, "\n"), 3)
  623. //go utils.SendEmail(utils.APPNAME+"上海路演数据同步到自系统失败:"+time.Now().Format("2006-01-02 15:04:05"), errMsg, utils.EmailSendToUsers)
  624. }
  625. }()
  626. //fmt.Println(userPhone, startDate, endDate)
  627. endDateTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  628. if err != nil {
  629. errMsgList = append(errMsgList, `结束日期转time类型失败,err:`+err.Error())
  630. return
  631. }
  632. //往后延迟一天,避免截止到当天
  633. endDate = endDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  634. currDay := time.Now().Format(utils.FormatDate)
  635. nowDateTime, _ := time.ParseInLocation(utils.FormatDate, currDay, time.Local)
  636. //如果传入的结束日期早于当前日期,那么就不去查询了
  637. if endDateTime.Before(nowDateTime) {
  638. return
  639. }
  640. //获取当前研究员信息
  641. userPhoneInfo, err := getAdminInfo(userPhone)
  642. if err != nil {
  643. errMsgList = append(errMsgList, fmt.Sprint("获取手机号信息失败:手机号:", userPhone, ";err:"+err.Error(), ";"))
  644. return
  645. }
  646. //根据研究员和开始/结束日期获取上海的活动路演
  647. rsCalendarResearcherList, err := roadshow.GetRsCalendarResearcherInfoIByResearcherIdAndDate(userPhoneInfo.AdminId, currDay, endDate)
  648. if err != nil {
  649. errMsgList = append(errMsgList, fmt.Sprint("获取研究员日程信息失败:手机号:", userPhone, ";err:"+err.Error(), ";"))
  650. return
  651. }
  652. //待删除的活动路演
  653. deleteRsCalendarResearcherMap := make(map[int][]*roadshow.RsCalendarResearcherRelationInfo)
  654. for _, v := range rsCalendarResearcherList {
  655. tmpList, ok := deleteRsCalendarResearcherMap[v.ThirdCalendarId]
  656. if !ok {
  657. tmpList = make([]*roadshow.RsCalendarResearcherRelationInfo, 0)
  658. }
  659. tmpList = append(tmpList, v)
  660. deleteRsCalendarResearcherMap[v.ThirdCalendarId] = tmpList
  661. }
  662. //以当前日期作为起始日期去同步
  663. list, err := getCalendarFrom(userPhone, currDay, endDate)
  664. if err != nil {
  665. errMsgList = append(errMsgList, fmt.Sprint("获取第三方路演日历数据失败,", "userPhone:", userPhone, ";currDay:", currDay, ";endDate:", endDate, ";err:"+err.Error()))
  666. return
  667. }
  668. thirdIdList := make([]int, 0)
  669. if len(list) > 0 {
  670. for _, v := range list {
  671. thirdIdList = append(thirdIdList, v.ID)
  672. //移除还存在的活动,保留不存在的活动
  673. delete(deleteRsCalendarResearcherMap, v.ID)
  674. }
  675. rsCalendarRelationList, tmpErr := roadshow.GetRsCalendarRelationListByThirdIds(thirdIdList)
  676. if tmpErr != nil {
  677. err = tmpErr
  678. errMsgList = append(errMsgList, "获取关联列表失败,err:"+tmpErr.Error())
  679. return
  680. }
  681. rsCalendarRelationMap := make(map[int]*roadshow.RsCalendarRelation)
  682. for _, rsCalendarRelation := range rsCalendarRelationList {
  683. rsCalendarRelationMap[rsCalendarRelation.ThirdCalendarId] = rsCalendarRelation
  684. }
  685. for _, v := range list {
  686. //展示优先级:1、customer_name 2、project_name 3、title 需求池953
  687. if v.CustomerName != "" {
  688. v.Title = v.CustomerName
  689. } else if v.ProjectName != "" {
  690. v.Title = v.ProjectName
  691. }
  692. if rsCalendarRelation, ok := rsCalendarRelationMap[v.ID]; ok {
  693. //存在的话,那么就去查找对应的信息
  694. if rsCalendarRelation.CalendarType == 1 {
  695. //路演
  696. rsCalendarResearcherInfo, tmpErr := roadshow.GetRsCalendarResearcherById(rsCalendarRelation.SelfCalendarId)
  697. if tmpErr != nil {
  698. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "获取路演研究员信息失败;err:"+tmpErr.Error(), ";"))
  699. continue
  700. }
  701. rsCalendarInfo, tmpErr := roadshow.GetRsCalendarById(rsCalendarResearcherInfo.RsCalendarId)
  702. if tmpErr != nil {
  703. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "获取路演信息失败;err:"+tmpErr.Error(), ";"))
  704. continue
  705. }
  706. if rsCalendarInfo.Source == 0 { //自系统创建的路演活动,不需要依靠上海方来修改
  707. continue
  708. }
  709. //是否去同步变更数据库
  710. isUpdateSync := false
  711. //是否变更
  712. isUpdate := false
  713. if v.StartTime != rsCalendarRelation.StartTime {
  714. isUpdate = true
  715. }
  716. if v.EndTime != rsCalendarRelation.EndTime {
  717. isUpdate = true
  718. }
  719. if v.Title != rsCalendarRelation.Title {
  720. isUpdate = true
  721. }
  722. if rsCalendarResearcherInfo.Status == 4 { // 被删除了,现在是需要更新
  723. isUpdate = true
  724. }
  725. researcherList := make([]system.AdminItem, 0)
  726. researcherMap := make(map[int]system.AdminItem)
  727. addResearcherList := make([]system.AdminItem, 0) // 需要新增的研究员
  728. delResearcherIdList := make([]int, 0) //需要删除的路演活动与研究员的关系id
  729. updateResearcherList := make([]*roadshow.RsCalendarResearcher, 0) //待更新的路演活动中研究员信息
  730. if v.ResearcherMobile != rsCalendarRelation.ResearcherMobile {
  731. //研究员变更了,需要去改表
  732. isUpdateSync = true
  733. researcherMobileList := strings.Split(v.ResearcherMobile, ",")
  734. if len(researcherMobileList) > 0 {
  735. for _, mobile := range researcherMobileList {
  736. researcherInfo, tmpErr := getAdminInfo(mobile)
  737. if tmpErr != nil {
  738. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "获取研究员失败,研究员手机号:", mobile, ";err:"+tmpErr.Error(), ";"))
  739. continue
  740. }
  741. if researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_RESEARCHR ||
  742. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RESEARCHR ||
  743. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_RESEARCHR ||
  744. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT ||
  745. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT ||
  746. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER ||
  747. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  748. researcherList = append(researcherList, researcherInfo)
  749. researcherMap[researcherInfo.AdminId] = researcherInfo
  750. }
  751. }
  752. }
  753. //没有研究员
  754. if len(researcherList) <= 0 {
  755. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, ";对方研究员信息失败;"))
  756. continue
  757. }
  758. rsCalendarResearcherList, tmpErr := roadshow.GetRsCalendarResearcherListByRsCalendarId(rsCalendarInfo.RsCalendarId)
  759. if tmpErr != nil {
  760. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "获取路演活动中的研究员列表失败,路演活动ID:", rsCalendarInfo.RsCalendarId, ";err:"+tmpErr.Error(), ";"))
  761. continue
  762. }
  763. //现有活动中的研究员
  764. rsCalendarResearcherMap := make(map[int]*roadshow.RsCalendarResearcher)
  765. for _, rsCalendarResearcher := range rsCalendarResearcherList {
  766. if _, ok := researcherMap[rsCalendarResearcher.ResearcherId]; ok {
  767. if isUpdate {
  768. updateResearcherList = append(updateResearcherList, rsCalendarResearcher)
  769. }
  770. } else {
  771. delResearcherIdList = append(delResearcherIdList, rsCalendarResearcher.RsCalendarResearcherId)
  772. }
  773. rsCalendarResearcherMap[rsCalendarResearcher.ResearcherId] = rsCalendarResearcher
  774. }
  775. //校验是否需要新增研究员
  776. for adminId, researcherInfo := range researcherMap {
  777. //如果
  778. if _, ok := rsCalendarResearcherMap[adminId]; !ok {
  779. addResearcherList = append(addResearcherList, researcherInfo)
  780. }
  781. }
  782. } else if isUpdate { //如果有字段更新,那么需要将所有的研究员信息更新
  783. isUpdateSync = true
  784. rsCalendarResearcherList, tmpErr := roadshow.GetRsCalendarResearcherListByRsCalendarId(rsCalendarInfo.RsCalendarId)
  785. if tmpErr != nil {
  786. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "获取路演活动中的研究员列表失败,路演活动ID:", rsCalendarInfo.RsCalendarId, ";err:"+tmpErr.Error(), ";"))
  787. continue
  788. }
  789. for _, rsCalendarResearcher := range rsCalendarResearcherList {
  790. updateResearcherList = append(updateResearcherList, rsCalendarResearcher)
  791. }
  792. }
  793. if isUpdateSync {
  794. tmpErr = roadshow.UpdateSyncRsCalendarRelation(v, rsCalendarInfo, rsCalendarRelation, updateResearcherList, delResearcherIdList, addResearcherList)
  795. if tmpErr != nil {
  796. err = tmpErr
  797. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "修改关联关系失败;err:"+tmpErr.Error(), ";"))
  798. return
  799. }
  800. }
  801. } else {
  802. //事项
  803. //事项都是由自系统创建的,不需要依靠上海方来修改
  804. }
  805. } else {
  806. //数据不存在
  807. createUser, tmpErr := getAdminInfo(v.UserPhone)
  808. if tmpErr != nil {
  809. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "获取创建人失败,创建人手机号:", v.UserPhone, ";err:"+tmpErr.Error(), ";"))
  810. continue
  811. }
  812. //研究员列表
  813. researcherList := make([]system.AdminItem, 0)
  814. researcherMobileList := strings.Split(v.ResearcherMobile, ",")
  815. if len(researcherMobileList) > 0 {
  816. for _, mobile := range researcherMobileList {
  817. researcherInfo, tmpErr := getAdminInfo(mobile)
  818. if tmpErr != nil {
  819. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "获取研究员失败,研究员手机号:", mobile, ";err:"+tmpErr.Error(), ";"))
  820. continue
  821. }
  822. if researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_RESEARCHR ||
  823. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RESEARCHR ||
  824. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_RESEARCHR ||
  825. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT ||
  826. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT ||
  827. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER ||
  828. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN ||
  829. researcherInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  830. researcherList = append(researcherList, researcherInfo)
  831. }
  832. }
  833. }
  834. //没有研究员
  835. if len(researcherList) <= 0 {
  836. continue
  837. }
  838. //数据入库
  839. tmpErr = roadshow.SyncRsCalendarRelation(v, createUser, researcherList)
  840. if tmpErr != nil {
  841. err = tmpErr
  842. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", v.ID, "绑定关联关系失败;err:"+tmpErr.Error(), ";"))
  843. continue
  844. }
  845. //fmt.Println("createUser:", createUser)
  846. }
  847. }
  848. }
  849. //上海那边已经删除了路演,这边也要同步删除
  850. for _, deleteRsCalendarResearcherList := range deleteRsCalendarResearcherMap {
  851. for _, deleteRsCalendarResearcher := range deleteRsCalendarResearcherList {
  852. // 更新路演与研究员关系表的状态字段
  853. whereParams := make(map[string]interface{})
  854. updateParams := make(map[string]interface{})
  855. whereParams["rs_calendar_researcher_id"] = deleteRsCalendarResearcher.RsCalendarResearcherId
  856. updateParams["status"] = 4
  857. updateParams["modify_time"] = time.Now()
  858. tmpErr := roadshow.UpdateCalendarResearcher(whereParams, updateParams)
  859. if tmpErr != nil {
  860. err = tmpErr
  861. errMsgList = append(errMsgList, fmt.Sprint("第三方日历ID:", deleteRsCalendarResearcher.ThirdCalendarId, "删除关联关系失败;err:"+tmpErr.Error(), ";"))
  862. continue
  863. }
  864. // 更新路演的同步字段
  865. //calWhereParams := make(map[string]interface{})
  866. //calUpdateParams := make(map[string]interface{})
  867. //calWhereParams["rs_calendar_id"] = deleteRsCalendarResearcher.RsCalendarId
  868. //updateParams["is_synced"] = 1
  869. //err = roadshow.UpdateRsCalendar(calWhereParams, calUpdateParams)
  870. }
  871. }
  872. return
  873. }
  874. // CreateOaUser 新增系统用户
  875. func CreateOaUser(mobile, username string, groupId int) (err error) {
  876. defer func() {
  877. if err != nil {
  878. errMsg := fmt.Sprint("手机号:", mobile, ";username:", username, ";分组id:", groupId, ";err:", err.Error())
  879. go alarm_msg.SendAlarmMsg("系统用户同步到上海失败;ERR:"+err.Error()+";errMsg:"+errMsg, 3)
  880. //go utils.SendEmail(utils.APPNAME+"系统用户同步到上海失败:"+time.Now().Format("2006-01-02 15:04:05"), errMsg, utils.EmailSendToUsers)
  881. }
  882. }()
  883. finalUrl := utils.CRM_OPEN_API_URL + "/v1/OaUser/create"
  884. //发送创建请求
  885. form := url.Values{}
  886. form.Add("mobile", mobile)
  887. form.Add("username", username)
  888. form.Add("group_id", fmt.Sprint(groupId))
  889. //发送创建请求
  890. _, err, _ = postCurl(finalUrl, form, 0)
  891. if err != nil {
  892. err = errors.New("NewRequest Err:" + err.Error())
  893. return
  894. }
  895. return
  896. }
  897. // getAdminInfo 通过手机号获取用户信息
  898. func getAdminInfo(mobile string) (adminInfo system.AdminItem, err error) {
  899. adminMap := make(map[string]system.AdminItem)
  900. redisAdminData, redisErr := utils.Rc.RedisString(utils.CACHE_KEY_ADMIN)
  901. if redisErr != nil {
  902. list, err := system.GetSysuserList("", []interface{}{}, 0, 1000)
  903. // GetSysuserList
  904. for _, tmpAdminInfo := range list {
  905. adminMap[tmpAdminInfo.Mobile] = *tmpAdminInfo
  906. }
  907. //入缓存
  908. redisJsonData, err := json.Marshal(adminMap)
  909. if err == nil {
  910. _ = utils.Rc.Put(utils.CACHE_KEY_ADMIN, string(redisJsonData), time.Minute*30)
  911. }
  912. } else {
  913. err := json.Unmarshal([]byte(redisAdminData), &adminMap)
  914. if err != nil {
  915. fmt.Println("用户数据,json转换失败:", err)
  916. }
  917. }
  918. adminInfo, ok := adminMap[mobile]
  919. if !ok {
  920. err = fmt.Errorf("找不到该手机号用户,mobile:" + mobile)
  921. }
  922. return
  923. }
  924. // getAdminInfoById 通过用户id获取用户信息
  925. func getAdminInfoById(sysId int) (adminInfo system.AdminItem, err error) {
  926. adminMap := make(map[int]system.AdminItem)
  927. redisAdminData, redisErr := utils.Rc.RedisString(utils.CACHE_KEY_ADMIN_ID)
  928. if redisErr != nil {
  929. list, err := system.GetSysuserList("", []interface{}{}, 0, 1000)
  930. // GetSysuserList
  931. for _, tmpAdminInfo := range list {
  932. adminMap[tmpAdminInfo.AdminId] = *tmpAdminInfo
  933. }
  934. //入缓存
  935. redisJsonData, err := json.Marshal(adminMap)
  936. if err == nil {
  937. _ = utils.Rc.Put(utils.CACHE_KEY_ADMIN_ID, string(redisJsonData), time.Minute*30)
  938. }
  939. } else {
  940. err := json.Unmarshal([]byte(redisAdminData), &adminMap)
  941. if err != nil {
  942. fmt.Println("用户数据,json转换失败:", err)
  943. }
  944. }
  945. adminInfo, ok := adminMap[sysId]
  946. if !ok {
  947. err = fmt.Errorf("找不到该用户id,sysId:" + fmt.Sprint(sysId))
  948. }
  949. return
  950. }
  951. func getTitle(activityType, roadshowType, activityCategory, roadshowPlatform, province, city string) string {
  952. var title string
  953. switch activityType {
  954. case "内部会议":
  955. title = "内部会议"
  956. case "公开会议", "路演":
  957. if roadshowType == "线上" {
  958. title = roadshowType + activityType + roadshowPlatform
  959. } else {
  960. title = roadshowType + activityType + province + city
  961. }
  962. case "报告电话会":
  963. title = activityCategory + "电话会"
  964. }
  965. return title
  966. }
  967. // GetCalendarFrom 获取上海方的路演活动列表
  968. func GetCalendarFrom(userPhone, startDate, endDate string) (list []roadshow.UserCalendar, err error) {
  969. list, err = getCalendarFrom(userPhone, startDate, endDate)
  970. return
  971. }