calendar.go 39 KB

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