send_company_user.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/tealeg/xlsx"
  8. "hongze/hongze_mfyx/models"
  9. "hongze/hongze_mfyx/utils"
  10. "io/ioutil"
  11. "net/http"
  12. "os"
  13. "path/filepath"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. type GetShangHaiTokenResultApi struct {
  19. Code int `json:"code"`
  20. Msg string `json:"msg"`
  21. Time int `json:"time"`
  22. Data ShangHaiResultApidate `json:"data"`
  23. }
  24. type ShangHaiResultApidate struct {
  25. AccessToken string `json:"access_token"`
  26. ExpireIn int `json:"expire_in"`
  27. }
  28. type GetShangHaiTokenResultApiComPany struct {
  29. Code int `json:"code"`
  30. Msg string `json:"msg"`
  31. Time int `json:"time"`
  32. Data FailInfos `json:"data"`
  33. }
  34. type FailInfos struct {
  35. TotalCount int `json:"total_count"`
  36. FailInfo []ShangHaiResultApidateComPany `json:"fail_info"`
  37. }
  38. type ShangHaiResultApidateComPany struct {
  39. ErrVal string `json:"errVal"`
  40. ErrMsg string `json:"errMsg"`
  41. }
  42. type OriginalUserInfoResp struct {
  43. Mobile string `json:"Mobile"`
  44. RealName string `json:"RealName"`
  45. }
  46. // c
  47. func SendComapnyToShanghai(url, jsonCompany, postType string) (err error) {
  48. var msg string
  49. defer func() {
  50. if err != nil {
  51. go utils.SendAlarmMsg(msg, 1)
  52. go utils.SendEmail("同步策略平台获取数据信息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToExpert)
  53. }
  54. }()
  55. method := "POST"
  56. payload := strings.NewReader(jsonCompany)
  57. client := &http.Client{}
  58. req, err := http.NewRequest(method, url, payload)
  59. if err != nil {
  60. msg = "NewRequest Err:" + err.Error()
  61. return err
  62. }
  63. req.Header.Add("Content-Type", "application/json")
  64. res, err := client.Do(req)
  65. if err != nil {
  66. msg = "client.Do Err:" + err.Error()
  67. return err
  68. }
  69. defer res.Body.Close()
  70. body, err := ioutil.ReadAll(res.Body)
  71. if err != nil {
  72. msg = "ioutil.ReadAll Err:" + err.Error()
  73. return err
  74. }
  75. var result GetShangHaiTokenResultApiComPany
  76. err = json.Unmarshal(body, &result)
  77. if err != nil {
  78. msg = "json.Unmarshal Err:" + err.Error()
  79. return err
  80. }
  81. item := new(models.CygxShanghaiCompanyLog)
  82. item.CreateTime = time.Now()
  83. item.Url = url
  84. item.Body = jsonCompany
  85. item.Result = string(body)
  86. go models.AddCygxShanghaiCompanyLog(item)
  87. for _, v := range result.Data.FailInfo {
  88. if v.ErrMsg != "" {
  89. //companyCode += "'" + v.ErrVal + "',"
  90. item := new(models.CygxShanghaiErrLog)
  91. item.CreateTime = time.Now()
  92. item.ErrMsg = v.ErrMsg
  93. item.ErrVal = v.ErrVal
  94. item.ErrType = postType
  95. go models.AddCygxShanghaiErrLog(item)
  96. }
  97. }
  98. return
  99. }
  100. func DotongbuShangHai(cont context.Context) (err error) {
  101. defer func() {
  102. if err != nil {
  103. fmt.Println(err)
  104. go utils.SendAlarmMsg("把权限信息同步到上海策略品台失败,DotongbuShangHai:"+err.Error(), 2)
  105. }
  106. }()
  107. //先删除被禁用的用户
  108. DeleteUserDotongbuShangHaiApi()
  109. var condition string
  110. updateTime := time.Now().Add(-time.Minute * 22).Format("2006-01-02 15:04:05")
  111. listUpdateUser, e := models.GetWxUserOpLog(updateTime)
  112. if e != nil && e.Error() != utils.ErrNoRow() {
  113. err = errors.New("GetWxUserOpLog, Err: " + e.Error())
  114. return
  115. }
  116. var companyIds string
  117. if len(listUpdateUser) > 0 {
  118. for _, v := range listUpdateUser {
  119. companyIds += strconv.Itoa(v.CompanyId) + ","
  120. }
  121. }
  122. ////获取冻结跟流失的客户
  123. listFreezeAndLoss, e := models.GetCompanyFreezeAndLoss(updateTime)
  124. if e != nil && e.Error() != utils.ErrNoRow() {
  125. err = errors.New("GetCompanyFreezeAndLoss, Err: " + e.Error())
  126. return
  127. }
  128. if len(listFreezeAndLoss) > 0 {
  129. for _, v := range listFreezeAndLoss {
  130. companyIds += strconv.Itoa(v.CompanyId) + ","
  131. }
  132. }
  133. listDeleteUser, e := models.GetWxUserOpLogDelete(updateTime)
  134. if e != nil && e.Error() != utils.ErrNoRow() {
  135. err = errors.New("GetWxUserOpLogDelete, Err: " + e.Error())
  136. return
  137. }
  138. ////获取删除客户
  139. if len(listDeleteUser) > 0 {
  140. for _, v := range listDeleteUser {
  141. companyIds += strconv.Itoa(v.CompanyId) + ","
  142. }
  143. }
  144. //获取指定时间内被移动的客户
  145. listOperationRecord, e := models.GetCompanyOperationRecord(updateTime)
  146. if e != nil && e.Error() != utils.ErrNoRow() {
  147. err = errors.New("GetCompanyOperationRecord, Err: " + e.Error())
  148. return
  149. }
  150. if len(listOperationRecord) > 0 {
  151. for _, v := range listOperationRecord {
  152. companyIds += strconv.Itoa(v.CompanyId) + ","
  153. }
  154. }
  155. //获取正式转试用的客户ID
  156. var conditionPer string
  157. var parsPer []interface{}
  158. conditionPer += " AND product_id = 2 AND modify_time > ? GROUP BY company_id "
  159. parsPer = append(parsPer, updateTime)
  160. companyReportPermissionList, e := models.GetCompanyReportPermissionList(conditionPer, parsPer)
  161. if e != nil && e.Error() != utils.ErrNoRow() {
  162. err = errors.New("GetCompanyReportPermissionList, Err: " + e.Error())
  163. return
  164. }
  165. if len(companyReportPermissionList) > 0 {
  166. for _, v := range companyReportPermissionList {
  167. companyIds += strconv.Itoa(v.CompanyId) + ","
  168. }
  169. }
  170. condition = ` AND p.is_suspend = 0 AND a.modify_time > ` + "'" + updateTime + "'"
  171. companyIds = strings.TrimRight(companyIds, ",")
  172. fmt.Println("获取冻结跟流失的用户", len(listFreezeAndLoss))
  173. //return err
  174. if companyIds != "" {
  175. //这一部分先不推送,避免并发推送
  176. condition += ` AND c.company_id NOT IN (` + companyIds + ") "
  177. }
  178. //偶尔需要单独同步某一个公司的人员
  179. //condition = ` AND c.company_id = 13902 `
  180. //同30小时有变更的公司
  181. DotongbuShangHaApi(condition)
  182. //原有的公司如果新增了也同步
  183. companyIds = strings.TrimRight(companyIds, ",")
  184. if companyIds != "" {
  185. condition = ` AND p.is_suspend = 0 AND c.company_id IN (` + companyIds + ")"
  186. DotongbuShangHaApi(condition)
  187. }
  188. //处理被禁用的用户,将禁用用户的状态同步
  189. CompanySuspend()
  190. return
  191. }
  192. func DotongbuShangHaApi(condition string) (err error) {
  193. var msg string
  194. defer func() {
  195. if err != nil {
  196. fmt.Println(err)
  197. go utils.SendAlarmMsg("同步策略平台获取数据信息失败", 2)
  198. go utils.SendEmail("同步策略平台获取数据信息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToExpert)
  199. }
  200. }()
  201. var url string
  202. accessToken, err := GetShangHaiCrmApiToken()
  203. if err != nil {
  204. return
  205. }
  206. //同步权益客户
  207. companyList, err := models.GetSendCompanyList(condition)
  208. if err != nil {
  209. msg = "GetSendCompanyList Err:" + err.Error()
  210. return
  211. }
  212. mappermissionData := make(map[int][]*models.PermissionData)
  213. if len(companyList) > 0 {
  214. comapnyIdArr := make([]int, 0)
  215. for _, v := range companyList {
  216. comapnyIdArr = append(comapnyIdArr, v.Cid)
  217. }
  218. var conditionComapny string
  219. var pars []interface{}
  220. conditionComapny += ` AND cp.company_id IN (` + utils.GetOrmInReplace(len(comapnyIdArr)) + ` ) `
  221. pars = append(pars, comapnyIdArr)
  222. permissionDataList, e := models.GetSendCompanyPermissionDataList(conditionComapny, pars)
  223. if e != nil {
  224. err = errors.New("GetSendCompanyPermissionDataList, Err: " + e.Error())
  225. return
  226. }
  227. var IsTryOut int
  228. for _, v := range permissionDataList {
  229. if v.Status == "试用" {
  230. IsTryOut = 1
  231. } else {
  232. IsTryOut = 0
  233. }
  234. mappermissionData[v.CompanyId] = append(mappermissionData[v.CompanyId], &models.PermissionData{
  235. PermissionName: v.PermissionName,
  236. StartTime: strconv.Itoa(int(v.StartDate.Unix())),
  237. EndTime: strconv.Itoa(int(v.EndDate.Unix())),
  238. IsTryOut: IsTryOut,
  239. })
  240. }
  241. }
  242. fmt.Println("权益客户", len(companyList))
  243. var companyItems []*models.CompanyJson
  244. if len(companyList) > 0 {
  245. //url = "https://crm.hzinsights.com/openapi/v1/Customer/batchSyncFiccCustomer?access_token=" + accessToken
  246. url = utils.ShangHaiCrmApiLink + "openapi/v1/Customer/batchSyncFiccCustomer?access_token=" + accessToken
  247. for k, _ := range companyList {
  248. companyList[k].Fe = "权益"
  249. companyList[k].EndDate = strconv.Itoa(int(companyList[k].EndDateTime.Unix()))
  250. //一次同步一百条
  251. if len(mappermissionData[companyList[k].Cid]) > 0 {
  252. companyList[k].PermissionData = mappermissionData[companyList[k].Cid]
  253. }
  254. companyItems = append(companyItems, companyList[k])
  255. if (k+1)%100 == 0 {
  256. data, err := json.Marshal(companyItems)
  257. if err != nil {
  258. msg = "companyItems Err:" + err.Error()
  259. return err
  260. }
  261. jsonstr := string(data)
  262. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  263. SendComapnyToShanghai(url, jsonstr, "权益")
  264. companyItems = make([]*models.CompanyJson, 0)
  265. }
  266. }
  267. //如果剩下还有则同步剩下的部分
  268. if len(companyItems) > 0 {
  269. data, err := json.Marshal(companyItems)
  270. if err != nil {
  271. msg = "companyItems Err:" + err.Error()
  272. return err
  273. }
  274. jsonstr := string(data)
  275. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  276. SendComapnyToShanghai(url, jsonstr, "权益")
  277. utils.FileLog.Info("发送模版消息失败,msg:%s", jsonstr)
  278. companyItems = make([]*models.CompanyJson, 0)
  279. }
  280. }
  281. //同步FICC客户
  282. companyFiccList, err := models.GetSendCompanyFiccList(condition)
  283. if err != nil {
  284. msg = "GetSendCompanyFiccList Err:" + err.Error()
  285. return err
  286. }
  287. mappermissionDataFicc := make(map[int][]*models.PermissionData)
  288. if len(companyFiccList) > 0 {
  289. comapnyIdArr := make([]int, 0)
  290. for _, v := range companyFiccList {
  291. comapnyIdArr = append(comapnyIdArr, v.Cid)
  292. }
  293. var conditionComapny string
  294. var pars []interface{}
  295. conditionComapny = ` AND cp.company_id IN (` + utils.GetOrmInReplace(len(comapnyIdArr)) + ` ) `
  296. pars = append(pars, comapnyIdArr)
  297. permissionDataList, e := models.GetSendCompanyPermissionDataList(conditionComapny, pars)
  298. if e != nil {
  299. err = errors.New("GetSendCompanyPermissionDataList, Err: " + e.Error())
  300. return
  301. }
  302. var IsTryOut int
  303. for _, v := range permissionDataList {
  304. if v.Status == "试用" {
  305. IsTryOut = 1
  306. } else {
  307. IsTryOut = 0
  308. }
  309. mappermissionDataFicc[v.CompanyId] = append(mappermissionDataFicc[v.CompanyId], &models.PermissionData{
  310. PermissionName: v.PermissionName,
  311. StartTime: strconv.Itoa(int(v.StartDate.Unix())),
  312. EndTime: strconv.Itoa(int(v.EndDate.Unix())),
  313. IsTryOut: IsTryOut,
  314. })
  315. }
  316. }
  317. fmt.Println("FICC客户", len(companyFiccList))
  318. var companyFiccItems []*models.CompanyJson
  319. if len(companyFiccList) > 0 {
  320. //url = "https://crm.hzinsights.com/openapi/v1/Customer/batchSyncFiccCustomer?access_token=" + accessToken
  321. url = utils.ShangHaiCrmApiLink + "openapi/v1/Customer/batchSyncFiccCustomer?access_token=" + accessToken
  322. for k, _ := range companyFiccList {
  323. companyFiccList[k].Fe = "FICC"
  324. companyFiccList[k].EndDate = strconv.Itoa(int(companyFiccList[k].EndDateTime.Unix()))
  325. if len(mappermissionDataFicc[companyFiccList[k].Cid]) > 0 {
  326. companyFiccList[k].PermissionData = mappermissionDataFicc[companyFiccList[k].Cid]
  327. }
  328. //一次同步一百条
  329. companyFiccItems = append(companyFiccItems, companyFiccList[k])
  330. if (k+1)%100 == 0 {
  331. data, err := json.Marshal(companyFiccItems)
  332. if err != nil {
  333. msg = "companyFiccItems Err:" + err.Error()
  334. return err
  335. }
  336. jsonstr := string(data)
  337. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  338. SendComapnyToShanghai(url, jsonstr, "FICC")
  339. companyFiccItems = make([]*models.CompanyJson, 0)
  340. }
  341. }
  342. //如果剩下还有则同步剩下的部分
  343. if len(companyFiccItems) > 0 {
  344. data, err := json.Marshal(companyFiccItems)
  345. if err != nil {
  346. msg = "companyFiccItems Err:" + err.Error()
  347. return err
  348. }
  349. jsonstr := string(data)
  350. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  351. SendComapnyToShanghai(url, jsonstr, "FICC")
  352. }
  353. }
  354. userList, err := models.GetSendUserList(condition)
  355. if err != nil {
  356. msg = "GetSendUserList Err:" + err.Error()
  357. return err
  358. }
  359. fmt.Println("用户", len(userList))
  360. var userItems []*models.UserJson
  361. if len(userList) > 0 {
  362. //url = "https://crm.hzinsights.com/openapi/v1/CustomerContact/batchSyncFiccCustomerContact?access_token=" + accessToken
  363. url = utils.ShangHaiCrmApiLink + "openapi/v1/CustomerContact/batchSyncFiccCustomerContact?access_token=" + accessToken
  364. for k, v := range userList {
  365. if v.Sex == "1" {
  366. userList[k].Sex = "男"
  367. } else {
  368. userList[k].Sex = "女"
  369. }
  370. if v.IsMaker == "1" {
  371. userList[k].IsMaker = "是"
  372. } else {
  373. userList[k].IsMaker = "否"
  374. }
  375. userList[k].CountryCode = "+" + v.CountryCode
  376. userList[k].OutboundCountryCode = "+" + v.OutboundCountryCode
  377. if userList[k].Mobile != "" && userList[k].OutboundMobile != "" && userList[k].Mobile == userList[k].OutboundMobile {
  378. userList[k].OutboundMobile = ""
  379. userList[k].OutboundCountryCode = ""
  380. }
  381. //一次同步一百条
  382. userItems = append(userItems, userList[k])
  383. if (k+1)%100 == 0 {
  384. data, err := json.Marshal(userItems)
  385. if err != nil {
  386. msg = "userItems Err:" + err.Error()
  387. return err
  388. }
  389. fmt.Println(k)
  390. jsonstr := string(data)
  391. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  392. SendComapnyToShanghai(url, jsonstr, "user")
  393. userItems = make([]*models.UserJson, 0)
  394. }
  395. }
  396. //如果剩下还有则同步剩下的部分
  397. if len(userItems) > 0 {
  398. data, err := json.Marshal(userItems)
  399. if err != nil {
  400. msg = "userItems Err:" + err.Error()
  401. return err
  402. }
  403. jsonstr := string(data)
  404. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  405. SendComapnyToShanghai(url, jsonstr, "user")
  406. companyItems = make([]*models.CompanyJson, 0)
  407. }
  408. }
  409. return
  410. }
  411. func DotongbuShangHaiSendEmail(cont context.Context) (err error) {
  412. var msg string
  413. defer func() {
  414. if err != nil {
  415. go utils.SendAlarmMsg("发送邮件:同步用户到策略平台信息失败", 2)
  416. go utils.SendEmail("发送邮件:同步用户到策略平台信息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToExpert)
  417. }
  418. }()
  419. list, err := models.GetCygxShanghaiCompanyLog()
  420. if err != nil {
  421. msg = "GetCygxShanghaiCompanyLog Err:" + err.Error()
  422. return err
  423. }
  424. mapErrMsg := make(map[string]string)
  425. for _, v := range list {
  426. data := new(GetShangHaiTokenResultApiComPany)
  427. err := json.Unmarshal([]byte(v.Result), &data)
  428. if err != nil {
  429. msg = "GetShangHaiTokenResultApiComPany Err:" + err.Error()
  430. return err
  431. }
  432. if len(data.Data.FailInfo) > 0 {
  433. for _, vCompany := range data.Data.FailInfo {
  434. mapErrMsg[vCompany.ErrVal] = vCompany.ErrMsg
  435. }
  436. }
  437. }
  438. //创建excel
  439. dir, err := os.Executable()
  440. exPath := filepath.Dir(dir)
  441. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandDigit(5) + ".xlsx"
  442. xlsxFile := xlsx.NewFile()
  443. if err != nil {
  444. return
  445. }
  446. style := xlsx.NewStyle()
  447. alignment := xlsx.Alignment{
  448. Horizontal: "center",
  449. Vertical: "center",
  450. WrapText: true,
  451. }
  452. style.Alignment = alignment
  453. style.ApplyAlignment = true
  454. sheet, err := xlsxFile.AddSheet("上海策略品台同步名单")
  455. if err != nil {
  456. return
  457. }
  458. //设置宽度
  459. _ = sheet.SetColWidth(0, 1, 30)
  460. _ = sheet.SetColWidth(2, 2, 10)
  461. _ = sheet.SetColWidth(3, 3, 30)
  462. //标头
  463. rowTitle := sheet.AddRow()
  464. cellA := rowTitle.AddCell()
  465. cellA.Value = "客户名称"
  466. cellB := rowTitle.AddCell()
  467. cellB.Value = "社会信用码"
  468. cellC := rowTitle.AddCell()
  469. cellC.Value = "客户类型"
  470. cellD := rowTitle.AddCell()
  471. cellD.Value = "错误消息"
  472. for _, v := range list {
  473. data := new(models.CompanyUnJsonList)
  474. err := json.Unmarshal([]byte(v.Body), &data)
  475. if err != nil {
  476. msg = "CompanyUnJsonList Err:" + err.Error()
  477. return err
  478. }
  479. if len(data.SyncData) > 0 {
  480. for _, item := range data.SyncData {
  481. row := sheet.AddRow()
  482. cellA := row.AddCell()
  483. cellA.Value = item.CompanyName
  484. cellB := row.AddCell()
  485. cellB.Value = item.CreditCode
  486. cellC := row.AddCell()
  487. cellC.Value = item.Fe
  488. if mapErrMsg[item.CreditCode] != "" {
  489. cellD := row.AddCell()
  490. cellD.Value = mapErrMsg[item.CreditCode]
  491. }
  492. }
  493. }
  494. }
  495. title := time.Now().Format(utils.FormatDate) + "上海策略品台同步名单"
  496. content := time.Now().Format(utils.FormatDate) + "上海策略品台同步名单"
  497. fileName := downLoadnFilePath
  498. err = xlsxFile.Save(downLoadnFilePath)
  499. if err != nil {
  500. return
  501. }
  502. if len(list) > 0 {
  503. utils.SendEmailByHongze(title, content, "cxzhang@hzinsights.com;tshen@hzinsights.com", fileName, title+".xlsx")
  504. }
  505. os.Remove(downLoadnFilePath)
  506. go models.DeleteCygxShanghaiCompanyLog()
  507. return
  508. }
  509. // DeleteUserDotongbuShangHaApi 删除用户进行同步
  510. func DeleteUserDotongbuShangHaiApi() (err error) {
  511. var msg string
  512. defer func() {
  513. if err != nil {
  514. go utils.SendAlarmMsg("同步策略平台获取数据信息失败 DeleteUserDotongbuShangHaApi"+err.Error()+msg, 2)
  515. }
  516. }()
  517. updateTime := time.Now().Add(-time.Minute * 22).Format("2006-01-02 15:04:05")
  518. listDeleteUser, err := models.GetWxUserOpLogDelete(updateTime)
  519. if err != nil && err.Error() != utils.ErrNoRow() {
  520. return err
  521. }
  522. fmt.Println("删除用户", len(listDeleteUser))
  523. if len(listDeleteUser) > 0 {
  524. var deleteCompanyIds string
  525. for k, v := range listDeleteUser {
  526. deleteCompanyIds += strconv.Itoa(v.CompanyId) + ","
  527. var resultUser OriginalUserInfoResp
  528. if err := json.Unmarshal([]byte(v.OriginalUserInfo), &resultUser); err == nil {
  529. listDeleteUser[k].RealName = resultUser.RealName
  530. } else {
  531. return err
  532. }
  533. }
  534. } else {
  535. return err
  536. }
  537. var url string
  538. accessToken, err := GetShangHaiCrmApiToken()
  539. if err != nil {
  540. return
  541. }
  542. var userItems []*models.ShangHaiCrmUserDeleteResp
  543. if len(listDeleteUser) > 0 {
  544. url = utils.ShangHaiCrmApiLink + "openapi/v1/CustomerContact/batchDisableCustomerContact?access_token=" + accessToken
  545. for k, v := range listDeleteUser {
  546. userItem := new(models.ShangHaiCrmUserDeleteResp)
  547. userItem.Name = v.RealName
  548. userItem.Mobile = v.Mobile
  549. userItem.Social = v.CreditCode
  550. //一次同步一百条
  551. userItems = append(userItems, userItem)
  552. if (k+1)%100 == 0 {
  553. data, err := json.Marshal(userItems)
  554. if err != nil {
  555. msg = "companyItems Err:" + err.Error()
  556. return err
  557. }
  558. jsonstr := string(data)
  559. jsonstr = "{\"data\": " + jsonstr + " }"
  560. SendComapnyToShanghai(url, jsonstr, "权益")
  561. userItems = make([]*models.ShangHaiCrmUserDeleteResp, 0)
  562. }
  563. }
  564. //如果剩下还有则同步剩下的部分
  565. if len(userItems) > 0 {
  566. data, err := json.Marshal(userItems)
  567. if err != nil {
  568. msg = "companyItems Err:" + err.Error()
  569. return err
  570. }
  571. jsonstr := string(data)
  572. jsonstr = "{\"data\": " + jsonstr + " }"
  573. SendComapnyToShanghai(url, jsonstr, "权益")
  574. userItems = make([]*models.ShangHaiCrmUserDeleteResp, 0)
  575. }
  576. }
  577. return
  578. }
  579. // 获取用户的Token
  580. func GetShangHaiCrmApiToken() (token string, err error) {
  581. var msg string
  582. defer func() {
  583. if err != nil {
  584. go utils.SendAlarmMsg("同步策略平台获取数据信息失败 DeleteUserDotongbuShangHaApi"+err.Error()+msg, 2)
  585. }
  586. }()
  587. cacheKey := utils.SHANG_HAI_CRM_API_TOKEN
  588. ttlTime := utils.Rc.GetRedisTTL(cacheKey)
  589. if ttlTime > 0 {
  590. token, _ = utils.Rc.RedisString(cacheKey)
  591. }
  592. if token == "" {
  593. //获取token
  594. url := utils.ShangHaiCrmApiLink + "openapi/v1/auth/getAccessToken?app_key=26945134&app_secret=b99cb2bdec70d20156000f664ec5ac30"
  595. method := "GET"
  596. client := &http.Client{}
  597. req, errReq := http.NewRequest(method, url, nil)
  598. if errReq != nil {
  599. err = errReq
  600. return
  601. }
  602. res, errReq := client.Do(req)
  603. if errReq != nil {
  604. err = errReq
  605. return
  606. }
  607. defer res.Body.Close()
  608. body, errReq := ioutil.ReadAll(res.Body)
  609. if errReq != nil {
  610. err = errReq
  611. return
  612. }
  613. var result GetShangHaiTokenResultApi
  614. errReq = json.Unmarshal(body, &result)
  615. if errReq != nil {
  616. err = errReq
  617. return
  618. }
  619. token = result.Data.AccessToken
  620. utils.Rc.Put(cacheKey, token, time.Second*7000)
  621. }
  622. return
  623. }
  624. // 处理被禁用的用户,将禁用用户的状态转冻结
  625. func CompanySuspend() (err error) {
  626. var msg string
  627. defer func() {
  628. if err != nil {
  629. fmt.Println(err)
  630. go utils.SendAlarmMsg("同步策略平台处理被禁用的用户,将禁用用户的状态转冻结失败", 2)
  631. go utils.SendEmail("同步策略平台处理被禁用的用户,将禁用用户的状态转冻结失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToExpert)
  632. }
  633. }()
  634. var url string
  635. accessToken, err := GetShangHaiCrmApiToken()
  636. if err != nil {
  637. return
  638. }
  639. updateTime := time.Now().Add(-time.Minute * 22).Format("2006-01-02 15:04:05")
  640. listSuspendCompany, err := models.GetWxUserOpLogSuspend(updateTime)
  641. if err != nil && err.Error() != utils.ErrNoRow() {
  642. return err
  643. }
  644. fmt.Println("禁用的用户", len(listSuspendCompany))
  645. var suspendCompanyIds string
  646. if len(listSuspendCompany) > 0 {
  647. for _, v := range listSuspendCompany {
  648. suspendCompanyIds += strconv.Itoa(v.CompanyId) + ","
  649. }
  650. } else {
  651. return err
  652. }
  653. fmt.Println(suspendCompanyIds)
  654. //同步权益客户
  655. if suspendCompanyIds == "" {
  656. return err
  657. }
  658. suspendCompanyIds = strings.TrimRight(suspendCompanyIds, ",")
  659. condition := ` AND c.company_id IN (` + suspendCompanyIds + ")"
  660. companyList, err := models.GetSendCompanyList(condition)
  661. if err != nil {
  662. msg = "GetSendCompanyList Err:" + err.Error()
  663. fmt.Println(err)
  664. return err
  665. }
  666. fmt.Println("权益客户", len(companyList))
  667. var companyItems []*models.CompanyJson
  668. if len(companyList) > 0 {
  669. //url = "https://crm.hzinsights.com/openapi/v1/Customer/batchSyncFiccCustomer?access_token=" + accessToken
  670. url = utils.ShangHaiCrmApiLink + "openapi/v1/Customer/batchSyncFiccCustomer?access_token=" + accessToken
  671. for k, _ := range companyList {
  672. companyList[k].Fe = "权益"
  673. companyList[k].Status = "暂停"
  674. companyList[k].EndDate = strconv.Itoa(int(companyList[k].EndDateTime.Unix()))
  675. //一次同步一百条
  676. companyItems = append(companyItems, companyList[k])
  677. if (k+1)%100 == 0 {
  678. data, err := json.Marshal(companyItems)
  679. if err != nil {
  680. msg = "companyItems Err:" + err.Error()
  681. return err
  682. }
  683. jsonstr := string(data)
  684. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  685. SendComapnyToShanghai(url, jsonstr, "权益")
  686. companyItems = make([]*models.CompanyJson, 0)
  687. }
  688. }
  689. //如果剩下还有则同步剩下的部分
  690. if len(companyItems) > 0 {
  691. data, err := json.Marshal(companyItems)
  692. if err != nil {
  693. msg = "companyItems Err:" + err.Error()
  694. return err
  695. }
  696. jsonstr := string(data)
  697. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  698. SendComapnyToShanghai(url, jsonstr, "权益")
  699. companyItems = make([]*models.CompanyJson, 0)
  700. }
  701. }
  702. //同步FICC客户
  703. companyFiccList, err := models.GetSendCompanyFiccList(condition)
  704. if err != nil {
  705. msg = "GetSendCompanyFiccList Err:" + err.Error()
  706. return err
  707. }
  708. fmt.Println("FICC客户", len(companyFiccList))
  709. var companyFiccItems []*models.CompanyJson
  710. if len(companyFiccList) > 0 {
  711. url = utils.ShangHaiCrmApiLink + "openapi/v1/Customer/batchSyncFiccCustomer?access_token=" + accessToken
  712. for k, _ := range companyFiccList {
  713. companyFiccList[k].Fe = "FICC"
  714. companyList[k].Status = "暂停"
  715. companyFiccList[k].EndDate = strconv.Itoa(int(companyFiccList[k].EndDateTime.Unix()))
  716. //一次同步一百条
  717. companyFiccItems = append(companyFiccItems, companyFiccList[k])
  718. if (k+1)%100 == 0 {
  719. data, err := json.Marshal(companyFiccItems)
  720. if err != nil {
  721. msg = "companyFiccItems Err:" + err.Error()
  722. return err
  723. }
  724. jsonstr := string(data)
  725. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  726. SendComapnyToShanghai(url, jsonstr, "FICC")
  727. companyFiccItems = make([]*models.CompanyJson, 0)
  728. }
  729. }
  730. //如果剩下还有则同步剩下的部分
  731. if len(companyFiccItems) > 0 {
  732. data, err := json.Marshal(companyFiccItems)
  733. if err != nil {
  734. msg = "companyFiccItems Err:" + err.Error()
  735. return err
  736. }
  737. jsonstr := string(data)
  738. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  739. SendComapnyToShanghai(url, jsonstr, "FICC")
  740. }
  741. }
  742. return
  743. }