calendar.go 39 KB

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