send_company_user.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/tealeg/xlsx"
  8. "hongze/hongze_cygx/models"
  9. "hongze/hongze_cygx/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. mappermissionDataNoChekcStatus := make(map[int][]*models.PermissionData)
  214. if len(companyList) > 0 {
  215. comapnyIdArr := make([]int, 0)
  216. for _, v := range companyList {
  217. comapnyIdArr = append(comapnyIdArr, v.Cid)
  218. }
  219. var conditionComapny string
  220. var pars []interface{}
  221. conditionComapny += ` AND cp.company_id IN (` + utils.GetOrmInReplace(len(comapnyIdArr)) + ` ) `
  222. pars = append(pars, comapnyIdArr)
  223. permissionDataList, e := models.GetSendCompanyPermissionDataList(conditionComapny, pars)
  224. if e != nil {
  225. err = errors.New("GetSendCompanyPermissionDataList, Err: " + e.Error())
  226. return
  227. }
  228. var IsTryOut int
  229. for _, v := range permissionDataList {
  230. if v.Status == "试用" {
  231. IsTryOut = 1
  232. } else {
  233. IsTryOut = 0
  234. }
  235. if v.PermissionName == utils.YAN_XUAN_KOU_DIAN_BAO_NAME {
  236. v.PermissionName = utils.MAI_FANG_YAN_XUAN_NAME
  237. }
  238. if v.PermissionName == utils.CHART_PERMISSION_NAME_MF_YANXUAN {
  239. v.PermissionName = utils.MAI_FANG_YAN_XUAN_NAME
  240. }
  241. if v.PermissionName == utils.GU_SHOU_NAME {
  242. v.PermissionName = "固定收益"
  243. }
  244. mappermissionData[v.CompanyId] = append(mappermissionData[v.CompanyId], &models.PermissionData{
  245. PermissionName: v.PermissionName,
  246. StartTime: strconv.Itoa(int(v.StartDate.Unix())),
  247. EndTime: strconv.Itoa(int(v.EndDate.Unix())),
  248. IsTryOut: IsTryOut,
  249. })
  250. }
  251. permissionDataListNoChekcStatus, e := models.GetSendCompanyPermissionDataListNoChekcStatus(conditionComapny, pars)
  252. if e != nil {
  253. err = errors.New("GetSendCompanyPermissionDataListNoChekcStatus, Err: " + e.Error())
  254. return
  255. }
  256. //var IsTryOut int
  257. for _, v := range permissionDataListNoChekcStatus {
  258. if v.PermissionName == utils.YAN_XUAN_KOU_DIAN_BAO_NAME {
  259. v.PermissionName = utils.MAI_FANG_YAN_XUAN_NAME
  260. }
  261. if v.PermissionName == utils.GU_SHOU_NAME {
  262. v.PermissionName = "固定收益"
  263. }
  264. mappermissionDataNoChekcStatus[v.CompanyId] = append(mappermissionDataNoChekcStatus[v.CompanyId], &models.PermissionData{
  265. PermissionName: v.PermissionName,
  266. StartTime: strconv.Itoa(int(v.StartDate.Unix())),
  267. EndTime: strconv.Itoa(int(v.EndDate.Unix())),
  268. IsTryOut: 1,
  269. })
  270. }
  271. }
  272. fmt.Println("权益客户", len(companyList))
  273. var companyItems []*models.CompanyJson
  274. if len(companyList) > 0 {
  275. //url = "https://crm.hzinsights.com/openapi/v1/Customer/batchSyncFiccCustomer?access_token=" + accessToken
  276. url = utils.ShangHaiCrmApiLink + "openapi/v1/Customer/batchSyncFiccCustomer?access_token=" + accessToken
  277. for k, _ := range companyList {
  278. if companyList[k].Status == "永续" {
  279. companyList[k].Status = "试用"
  280. }
  281. companyList[k].Fe = "权益"
  282. companyList[k].EndDate = strconv.Itoa(int(companyList[k].EndDateTime.Unix()))
  283. //一次同步一百条
  284. if len(mappermissionData[companyList[k].Cid]) > 0 {
  285. companyList[k].PermissionData = mappermissionData[companyList[k].Cid]
  286. } else if len(mappermissionDataNoChekcStatus[companyList[k].Cid]) > 0 {
  287. companyList[k].PermissionData = mappermissionDataNoChekcStatus[companyList[k].Cid]
  288. }
  289. companyItems = append(companyItems, companyList[k])
  290. if (k+1)%100 == 0 {
  291. data, err := json.Marshal(companyItems)
  292. if err != nil {
  293. msg = "companyItems Err:" + err.Error()
  294. return err
  295. }
  296. jsonstr := string(data)
  297. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  298. SendComapnyToShanghai(url, jsonstr, "权益")
  299. companyItems = make([]*models.CompanyJson, 0)
  300. }
  301. }
  302. //如果剩下还有则同步剩下的部分
  303. if len(companyItems) > 0 {
  304. data, err := json.Marshal(companyItems)
  305. if err != nil {
  306. msg = "companyItems Err:" + err.Error()
  307. return err
  308. }
  309. jsonstr := string(data)
  310. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  311. SendComapnyToShanghai(url, jsonstr, "权益")
  312. utils.FileLog.Info("发送模版消息失败,msg:%s", jsonstr)
  313. companyItems = make([]*models.CompanyJson, 0)
  314. }
  315. }
  316. //同步FICC客户
  317. companyFiccList, err := models.GetSendCompanyFiccList(condition)
  318. if err != nil {
  319. msg = "GetSendCompanyFiccList Err:" + err.Error()
  320. return err
  321. }
  322. mappermissionDataFicc := make(map[int][]*models.PermissionData)
  323. mappermissionDataFiccNoChekcStatus := make(map[int][]*models.PermissionData)
  324. if len(companyFiccList) > 0 {
  325. comapnyIdArr := make([]int, 0)
  326. for _, v := range companyFiccList {
  327. comapnyIdArr = append(comapnyIdArr, v.Cid)
  328. }
  329. var conditionComapny string
  330. var pars []interface{}
  331. conditionComapny = ` AND cp.company_id IN (` + utils.GetOrmInReplace(len(comapnyIdArr)) + ` ) `
  332. pars = append(pars, comapnyIdArr)
  333. permissionDataList, e := models.GetSendCompanyPermissionDataList(conditionComapny, pars)
  334. if e != nil {
  335. err = errors.New("GetSendCompanyPermissionDataList, Err: " + e.Error())
  336. return
  337. }
  338. var IsTryOut int
  339. for _, v := range permissionDataList {
  340. if v.Status == "试用" {
  341. IsTryOut = 1
  342. } else {
  343. IsTryOut = 0
  344. }
  345. if v.PermissionName == utils.YAN_XUAN_KOU_DIAN_BAO_NAME {
  346. v.PermissionName = utils.MAI_FANG_YAN_XUAN_NAME
  347. }
  348. if v.PermissionName == utils.GU_SHOU_NAME {
  349. v.PermissionName = "固定收益"
  350. }
  351. mappermissionDataFicc[v.CompanyId] = append(mappermissionDataFicc[v.CompanyId], &models.PermissionData{
  352. PermissionName: v.PermissionName,
  353. StartTime: strconv.Itoa(int(v.StartDate.Unix())),
  354. EndTime: strconv.Itoa(int(v.EndDate.Unix())),
  355. IsTryOut: IsTryOut,
  356. })
  357. }
  358. permissionDataListNoChekcStatus, e := models.GetSendCompanyPermissionDataListNoChekcStatus(conditionComapny, pars)
  359. if e != nil {
  360. err = errors.New("GetSendCompanyPermissionDataListNoChekcStatus, Err: " + e.Error())
  361. return
  362. }
  363. //var IsTryOut int
  364. for _, v := range permissionDataListNoChekcStatus {
  365. if v.PermissionName == utils.YAN_XUAN_KOU_DIAN_BAO_NAME {
  366. v.PermissionName = utils.MAI_FANG_YAN_XUAN_NAME
  367. }
  368. if v.PermissionName == utils.GU_SHOU_NAME {
  369. v.PermissionName = "固定收益"
  370. }
  371. mappermissionDataFiccNoChekcStatus[v.CompanyId] = append(mappermissionDataFiccNoChekcStatus[v.CompanyId], &models.PermissionData{
  372. PermissionName: v.PermissionName,
  373. StartTime: strconv.Itoa(int(v.StartDate.Unix())),
  374. EndTime: strconv.Itoa(int(v.EndDate.Unix())),
  375. IsTryOut: 1,
  376. })
  377. }
  378. }
  379. fmt.Println("FICC客户", len(companyFiccList))
  380. var companyFiccItems []*models.CompanyJson
  381. if len(companyFiccList) > 0 {
  382. //url = "https://crm.hzinsights.com/openapi/v1/Customer/batchSyncFiccCustomer?access_token=" + accessToken
  383. url = utils.ShangHaiCrmApiLink + "openapi/v1/Customer/batchSyncFiccCustomer?access_token=" + accessToken
  384. for k, _ := range companyFiccList {
  385. companyFiccList[k].Fe = "FICC"
  386. companyFiccList[k].EndDate = strconv.Itoa(int(companyFiccList[k].EndDateTime.Unix()))
  387. if len(mappermissionDataFicc[companyFiccList[k].Cid]) > 0 {
  388. companyFiccList[k].PermissionData = mappermissionDataFicc[companyFiccList[k].Cid]
  389. } else if len(mappermissionDataFiccNoChekcStatus[companyFiccList[k].Cid]) > 0 {
  390. companyFiccList[k].PermissionData = mappermissionDataFiccNoChekcStatus[companyFiccList[k].Cid]
  391. }
  392. //一次同步一百条
  393. companyFiccItems = append(companyFiccItems, companyFiccList[k])
  394. if (k+1)%100 == 0 {
  395. data, err := json.Marshal(companyFiccItems)
  396. if err != nil {
  397. msg = "companyFiccItems Err:" + err.Error()
  398. return err
  399. }
  400. jsonstr := string(data)
  401. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  402. SendComapnyToShanghai(url, jsonstr, "FICC")
  403. companyFiccItems = make([]*models.CompanyJson, 0)
  404. }
  405. }
  406. //如果剩下还有则同步剩下的部分
  407. if len(companyFiccItems) > 0 {
  408. data, err := json.Marshal(companyFiccItems)
  409. if err != nil {
  410. msg = "companyFiccItems Err:" + err.Error()
  411. return err
  412. }
  413. jsonstr := string(data)
  414. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  415. SendComapnyToShanghai(url, jsonstr, "FICC")
  416. }
  417. }
  418. userList, err := models.GetSendUserList(condition)
  419. if err != nil {
  420. msg = "GetSendUserList Err:" + err.Error()
  421. return err
  422. }
  423. fmt.Println("用户", len(userList))
  424. var userItems []*models.UserJson
  425. if len(userList) > 0 {
  426. //url = "https://crm.hzinsights.com/openapi/v1/CustomerContact/batchSyncFiccCustomerContact?access_token=" + accessToken
  427. url = utils.ShangHaiCrmApiLink + "openapi/v1/CustomerContact/batchSyncFiccCustomerContact?access_token=" + accessToken
  428. for k, v := range userList {
  429. if v.Sex == "1" {
  430. userList[k].Sex = "男"
  431. } else {
  432. userList[k].Sex = "女"
  433. }
  434. if v.IsMaker == "1" {
  435. userList[k].IsMaker = "是"
  436. } else {
  437. userList[k].IsMaker = "否"
  438. }
  439. userList[k].CountryCode = "+" + v.CountryCode
  440. userList[k].OutboundCountryCode = "+" + v.OutboundCountryCode
  441. if userList[k].Mobile != "" && userList[k].OutboundMobile != "" && userList[k].Mobile == userList[k].OutboundMobile {
  442. userList[k].OutboundMobile = ""
  443. userList[k].OutboundCountryCode = ""
  444. }
  445. //一次同步一百条
  446. userItems = append(userItems, userList[k])
  447. if (k+1)%100 == 0 {
  448. data, err := json.Marshal(userItems)
  449. if err != nil {
  450. msg = "userItems Err:" + err.Error()
  451. return err
  452. }
  453. fmt.Println(k)
  454. jsonstr := string(data)
  455. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  456. SendComapnyToShanghai(url, jsonstr, "user")
  457. userItems = make([]*models.UserJson, 0)
  458. }
  459. }
  460. //如果剩下还有则同步剩下的部分
  461. if len(userItems) > 0 {
  462. data, err := json.Marshal(userItems)
  463. if err != nil {
  464. msg = "userItems Err:" + err.Error()
  465. return err
  466. }
  467. jsonstr := string(data)
  468. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  469. SendComapnyToShanghai(url, jsonstr, "user")
  470. companyItems = make([]*models.CompanyJson, 0)
  471. }
  472. }
  473. return
  474. }
  475. func DotongbuShangHaiSendEmail(cont context.Context) (err error) {
  476. var msg string
  477. defer func() {
  478. if err != nil {
  479. go utils.SendAlarmMsg("发送邮件:同步用户到策略平台信息失败", 2)
  480. go utils.SendEmail("发送邮件:同步用户到策略平台信息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToExpert)
  481. }
  482. }()
  483. list, err := models.GetCygxShanghaiCompanyLog()
  484. if err != nil {
  485. msg = "GetCygxShanghaiCompanyLog Err:" + err.Error()
  486. return err
  487. }
  488. mapErrMsg := make(map[string]string)
  489. for _, v := range list {
  490. data := new(GetShangHaiTokenResultApiComPany)
  491. err := json.Unmarshal([]byte(v.Result), &data)
  492. if err != nil {
  493. msg = "GetShangHaiTokenResultApiComPany Err:" + err.Error()
  494. return err
  495. }
  496. if len(data.Data.FailInfo) > 0 {
  497. for _, vCompany := range data.Data.FailInfo {
  498. mapErrMsg[vCompany.ErrVal] = vCompany.ErrMsg
  499. }
  500. }
  501. }
  502. //创建excel
  503. dir, err := os.Executable()
  504. exPath := filepath.Dir(dir)
  505. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandDigit(5) + ".xlsx"
  506. xlsxFile := xlsx.NewFile()
  507. if err != nil {
  508. return
  509. }
  510. style := xlsx.NewStyle()
  511. alignment := xlsx.Alignment{
  512. Horizontal: "center",
  513. Vertical: "center",
  514. WrapText: true,
  515. }
  516. style.Alignment = alignment
  517. style.ApplyAlignment = true
  518. sheet, err := xlsxFile.AddSheet("上海策略品台同步名单")
  519. if err != nil {
  520. return
  521. }
  522. //设置宽度
  523. _ = sheet.SetColWidth(0, 1, 30)
  524. _ = sheet.SetColWidth(2, 2, 10)
  525. _ = sheet.SetColWidth(3, 3, 30)
  526. //标头
  527. rowTitle := sheet.AddRow()
  528. cellA := rowTitle.AddCell()
  529. cellA.Value = "客户名称"
  530. cellB := rowTitle.AddCell()
  531. cellB.Value = "社会信用码"
  532. cellC := rowTitle.AddCell()
  533. cellC.Value = "客户类型"
  534. cellD := rowTitle.AddCell()
  535. cellD.Value = "错误消息"
  536. for _, v := range list {
  537. data := new(models.CompanyUnJsonList)
  538. err := json.Unmarshal([]byte(v.Body), &data)
  539. if err != nil {
  540. msg = "CompanyUnJsonList Err:" + err.Error()
  541. return err
  542. }
  543. if len(data.SyncData) > 0 {
  544. for _, item := range data.SyncData {
  545. row := sheet.AddRow()
  546. cellA := row.AddCell()
  547. cellA.Value = item.CompanyName
  548. cellB := row.AddCell()
  549. cellB.Value = item.CreditCode
  550. cellC := row.AddCell()
  551. cellC.Value = item.Fe
  552. if mapErrMsg[item.CreditCode] != "" {
  553. cellD := row.AddCell()
  554. cellD.Value = mapErrMsg[item.CreditCode]
  555. }
  556. }
  557. }
  558. }
  559. title := time.Now().Format(utils.FormatDate) + "上海策略品台同步名单"
  560. content := time.Now().Format(utils.FormatDate) + "上海策略品台同步名单"
  561. fileName := downLoadnFilePath
  562. err = xlsxFile.Save(downLoadnFilePath)
  563. if err != nil {
  564. return
  565. }
  566. if len(list) > 0 {
  567. utils.SendEmailByHongze(title, content, "cxzhang@hzinsights.com;tshen@hzinsights.com", fileName, title+".xlsx")
  568. }
  569. os.Remove(downLoadnFilePath)
  570. go models.DeleteCygxShanghaiCompanyLog()
  571. return
  572. }
  573. // DeleteUserDotongbuShangHaApi 删除用户进行同步
  574. func DeleteUserDotongbuShangHaiApi() (err error) {
  575. var msg string
  576. defer func() {
  577. if err != nil {
  578. go utils.SendAlarmMsg("同步策略平台获取数据信息失败 DeleteUserDotongbuShangHaApi"+err.Error()+msg, 2)
  579. }
  580. }()
  581. updateTime := time.Now().Add(-time.Minute * 22).Format("2006-01-02 15:04:05")
  582. listDeleteUser, err := models.GetWxUserOpLogDelete(updateTime)
  583. if err != nil && err.Error() != utils.ErrNoRow() {
  584. return err
  585. }
  586. fmt.Println("删除用户", len(listDeleteUser))
  587. if len(listDeleteUser) > 0 {
  588. var deleteCompanyIds string
  589. for k, v := range listDeleteUser {
  590. deleteCompanyIds += strconv.Itoa(v.CompanyId) + ","
  591. var resultUser OriginalUserInfoResp
  592. if err := json.Unmarshal([]byte(v.OriginalUserInfo), &resultUser); err == nil {
  593. listDeleteUser[k].RealName = resultUser.RealName
  594. } else {
  595. return err
  596. }
  597. }
  598. } else {
  599. return err
  600. }
  601. var url string
  602. accessToken, err := GetShangHaiCrmApiToken()
  603. if err != nil {
  604. return
  605. }
  606. var userItems []*models.ShangHaiCrmUserDeleteResp
  607. if len(listDeleteUser) > 0 {
  608. url = utils.ShangHaiCrmApiLink + "openapi/v1/CustomerContact/batchDisableCustomerContact?access_token=" + accessToken
  609. for k, v := range listDeleteUser {
  610. userItem := new(models.ShangHaiCrmUserDeleteResp)
  611. userItem.Name = v.RealName
  612. userItem.Mobile = v.Mobile
  613. userItem.Social = v.CreditCode
  614. //一次同步一百条
  615. userItems = append(userItems, userItem)
  616. if (k+1)%100 == 0 {
  617. data, err := json.Marshal(userItems)
  618. if err != nil {
  619. msg = "companyItems Err:" + err.Error()
  620. return err
  621. }
  622. jsonstr := string(data)
  623. jsonstr = "{\"data\": " + jsonstr + " }"
  624. SendComapnyToShanghai(url, jsonstr, "权益")
  625. userItems = make([]*models.ShangHaiCrmUserDeleteResp, 0)
  626. }
  627. }
  628. //如果剩下还有则同步剩下的部分
  629. if len(userItems) > 0 {
  630. data, err := json.Marshal(userItems)
  631. if err != nil {
  632. msg = "companyItems Err:" + err.Error()
  633. return err
  634. }
  635. jsonstr := string(data)
  636. jsonstr = "{\"data\": " + jsonstr + " }"
  637. SendComapnyToShanghai(url, jsonstr, "权益")
  638. userItems = make([]*models.ShangHaiCrmUserDeleteResp, 0)
  639. }
  640. }
  641. return
  642. }
  643. // 获取用户的Token
  644. func GetShangHaiCrmApiToken() (token string, err error) {
  645. var msg string
  646. defer func() {
  647. if err != nil {
  648. go utils.SendAlarmMsg("同步策略平台获取数据信息失败 GetShangHaiCrmApiToken"+err.Error()+msg, 2)
  649. }
  650. }()
  651. cacheKey := utils.SHANG_HAI_CRM_API_TOKEN
  652. ttlTime := utils.Rc.GetRedisTTL(cacheKey)
  653. if ttlTime > 0 {
  654. token, _ = utils.Rc.RedisString(cacheKey)
  655. }
  656. if token == "" {
  657. //获取token
  658. url := utils.ShangHaiCrmApiLink + "openapi/v1/auth/getAccessToken?app_key=26945134&app_secret=b99cb2bdec70d20156000f664ec5ac30"
  659. method := "GET"
  660. client := &http.Client{}
  661. req, errReq := http.NewRequest(method, url, nil)
  662. if errReq != nil {
  663. err = errReq
  664. return
  665. }
  666. res, errReq := client.Do(req)
  667. if errReq != nil {
  668. err = errReq
  669. return
  670. }
  671. defer res.Body.Close()
  672. body, errReq := ioutil.ReadAll(res.Body)
  673. if errReq != nil {
  674. err = errReq
  675. return
  676. }
  677. var result GetShangHaiTokenResultApi
  678. errReq = json.Unmarshal(body, &result)
  679. if errReq != nil {
  680. err = errReq
  681. return
  682. }
  683. token = result.Data.AccessToken
  684. utils.Rc.Put(cacheKey, token, time.Second*7000)
  685. }
  686. return
  687. }
  688. // 处理被禁用的用户,将禁用用户的状态转冻结
  689. func CompanySuspend() (err error) {
  690. var msg string
  691. defer func() {
  692. if err != nil {
  693. fmt.Println(err)
  694. go utils.SendAlarmMsg("同步策略平台处理被禁用的用户,将禁用用户的状态转冻结失败", 2)
  695. go utils.SendEmail("同步策略平台处理被禁用的用户,将禁用用户的状态转冻结失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToExpert)
  696. }
  697. }()
  698. var url string
  699. accessToken, err := GetShangHaiCrmApiToken()
  700. if err != nil {
  701. return
  702. }
  703. updateTime := time.Now().Add(-time.Minute * 22).Format("2006-01-02 15:04:05")
  704. listSuspendCompany, err := models.GetWxUserOpLogSuspend(updateTime)
  705. if err != nil && err.Error() != utils.ErrNoRow() {
  706. return err
  707. }
  708. fmt.Println("禁用的用户", len(listSuspendCompany))
  709. var suspendCompanyIds string
  710. if len(listSuspendCompany) > 0 {
  711. for _, v := range listSuspendCompany {
  712. suspendCompanyIds += strconv.Itoa(v.CompanyId) + ","
  713. }
  714. } else {
  715. return err
  716. }
  717. fmt.Println(suspendCompanyIds)
  718. //同步权益客户
  719. if suspendCompanyIds == "" {
  720. return err
  721. }
  722. suspendCompanyIds = strings.TrimRight(suspendCompanyIds, ",")
  723. condition := ` AND c.company_id IN (` + suspendCompanyIds + ")"
  724. companyList, err := models.GetSendCompanyList(condition)
  725. if err != nil {
  726. msg = "GetSendCompanyList Err:" + err.Error()
  727. fmt.Println(err)
  728. return err
  729. }
  730. fmt.Println("权益客户", len(companyList))
  731. var companyItems []*models.CompanyJson
  732. if len(companyList) > 0 {
  733. //url = "https://crm.hzinsights.com/openapi/v1/Customer/batchSyncFiccCustomer?access_token=" + accessToken
  734. url = utils.ShangHaiCrmApiLink + "openapi/v1/Customer/batchSyncFiccCustomer?access_token=" + accessToken
  735. for k, _ := range companyList {
  736. companyList[k].Fe = "权益"
  737. companyList[k].Status = "暂停"
  738. companyList[k].EndDate = strconv.Itoa(int(companyList[k].EndDateTime.Unix()))
  739. //一次同步一百条
  740. companyItems = append(companyItems, companyList[k])
  741. if (k+1)%100 == 0 {
  742. data, err := json.Marshal(companyItems)
  743. if err != nil {
  744. msg = "companyItems Err:" + err.Error()
  745. return err
  746. }
  747. jsonstr := string(data)
  748. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  749. SendComapnyToShanghai(url, jsonstr, "权益")
  750. companyItems = make([]*models.CompanyJson, 0)
  751. }
  752. }
  753. //如果剩下还有则同步剩下的部分
  754. if len(companyItems) > 0 {
  755. data, err := json.Marshal(companyItems)
  756. if err != nil {
  757. msg = "companyItems Err:" + err.Error()
  758. return err
  759. }
  760. jsonstr := string(data)
  761. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  762. SendComapnyToShanghai(url, jsonstr, "权益")
  763. companyItems = make([]*models.CompanyJson, 0)
  764. }
  765. }
  766. //同步FICC客户
  767. companyFiccList, err := models.GetSendCompanyFiccList(condition)
  768. if err != nil {
  769. msg = "GetSendCompanyFiccList Err:" + err.Error()
  770. return err
  771. }
  772. fmt.Println("FICC客户", len(companyFiccList))
  773. var companyFiccItems []*models.CompanyJson
  774. if len(companyFiccList) > 0 {
  775. url = utils.ShangHaiCrmApiLink + "openapi/v1/Customer/batchSyncFiccCustomer?access_token=" + accessToken
  776. for k, _ := range companyFiccList {
  777. companyFiccList[k].Fe = "FICC"
  778. companyList[k].Status = "暂停"
  779. companyFiccList[k].EndDate = strconv.Itoa(int(companyFiccList[k].EndDateTime.Unix()))
  780. //一次同步一百条
  781. companyFiccItems = append(companyFiccItems, companyFiccList[k])
  782. if (k+1)%100 == 0 {
  783. data, err := json.Marshal(companyFiccItems)
  784. if err != nil {
  785. msg = "companyFiccItems Err:" + err.Error()
  786. return err
  787. }
  788. jsonstr := string(data)
  789. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  790. SendComapnyToShanghai(url, jsonstr, "FICC")
  791. companyFiccItems = make([]*models.CompanyJson, 0)
  792. }
  793. }
  794. //如果剩下还有则同步剩下的部分
  795. if len(companyFiccItems) > 0 {
  796. data, err := json.Marshal(companyFiccItems)
  797. if err != nil {
  798. msg = "companyFiccItems Err:" + err.Error()
  799. return err
  800. }
  801. jsonstr := string(data)
  802. jsonstr = "{\"sync_data\": " + jsonstr + " }"
  803. SendComapnyToShanghai(url, jsonstr, "FICC")
  804. }
  805. }
  806. return
  807. }