task.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package services
  2. import (
  3. "fmt"
  4. "hongze/hz_crm_api/models/company"
  5. "hongze/hz_crm_api/models/eta_business"
  6. roadshowService "hongze/hz_crm_api/services/roadshow"
  7. "hongze/hz_crm_api/utils"
  8. "time"
  9. )
  10. func Task() {
  11. fmt.Println("task start")
  12. go AutoInsertLogToDB()
  13. go AutoInsertAdminOperateRecordToDB()
  14. go roadshowService.InsertSyncCalendarFromShanghai() // 上海路演数据同步到自系统 从Redis获取
  15. //go FixEtaBusinessCodeEncrypt()
  16. //InitContractUnusual()
  17. fmt.Println("task end")
  18. }
  19. // FixEtaBusinessCodeEncrypt 修复商家编码
  20. func FixEtaBusinessCodeEncrypt() {
  21. ob := new(eta_business.EtaBusiness)
  22. list, e := ob.GetItemsByCondition("", make([]interface{}, 0), []string{}, "")
  23. if e != nil {
  24. fmt.Println(e.Error())
  25. return
  26. }
  27. for _, v := range list {
  28. v.CodeEncrypt = utils.MD5(fmt.Sprintf("%s%s", v.BusinessCode, utils.BusinessCodeSalt))
  29. if e = v.Update([]string{"CodeEncrypt"}); e != nil {
  30. fmt.Println(e.Error())
  31. return
  32. }
  33. }
  34. }
  35. // InitContractUnusual 补全续约统计异常的客户
  36. func InitContractUnusual() (err error) {
  37. defer func() {
  38. if err != nil {
  39. fmt.Println("initYearContractUnusual 补全未续约合同记录,Err:" + err.Error())
  40. utils.FileLog.Info("initYearContractUnusual 补全未续约合同记录,Err:" + err.Error())
  41. }
  42. }()
  43. addList := make([]*company.CompanyRenewalRecord, 0)
  44. // 整理时间表
  45. startDate, _ := time.ParseInLocation(utils.FormatDate, "2023-01-01", time.Local)
  46. endDate, _ := time.ParseInLocation(utils.FormatDate, "2023-12-07", time.Local)
  47. for startDate.Before(endDate) || startDate == endDate {
  48. tmpC := ` AND end_date = ? AND status = 1 AND contract_type IN ( "新签合同", "续约合同" ) AND product_id = 1`
  49. var tmpP []interface{}
  50. before61 := startDate.AddDate(0, 0, -61)
  51. tmpP = append(tmpP, before61)
  52. contractList, e := company.GetCompanyContractList(tmpC, tmpP)
  53. if e != nil {
  54. err = fmt.Errorf("查询合同列表失败, ERR:%s", e)
  55. return
  56. }
  57. if len(contractList) > 0 {
  58. contractMap := make(map[int]*company.CompanyContractResp)
  59. for _, v := range contractList {
  60. //查询这个合同是否存在续约合同,起始日大于今日,或者截止日
  61. contractMap[v.CompanyId] = v
  62. }
  63. ficcNormalMap := make(map[int]struct{})
  64. tmpC = ` AND start_date >= ? AND start_date <= ? AND status = 1 AND contract_type="续约合同" AND product_id = 1`
  65. tmpP = make([]interface{}, 0)
  66. tmpP = append(tmpP, before61, startDate)
  67. contractNormalList, e := company.GetCompanyContractList(tmpC, tmpP)
  68. if e != nil {
  69. err = fmt.Errorf("查询合同列表失败, ERR:%s", e)
  70. return
  71. }
  72. for _, v := range contractNormalList {
  73. ficcNormalMap[v.CompanyId] = struct{}{}
  74. }
  75. for _, v := range contractList {
  76. sellers, tErr := company.GetCompanyProductByCompanyIdAndProductId(v.CompanyId, v.ProductId)
  77. if tErr != nil {
  78. err = tErr
  79. return
  80. }
  81. //查询这个合同是否存在续约合同,起始日大于今日,或者截止日
  82. if _, ok := ficcNormalMap[v.CompanyId]; !ok {
  83. tmp := &company.CompanyRenewalRecord{
  84. CompanyId: v.CompanyId,
  85. ProductId: v.ProductId,
  86. Source: 1,
  87. SellerId: sellers.SellerId,
  88. SellerName: sellers.SellerName,
  89. CreateTime: startDate,
  90. ModifyTime: startDate,
  91. }
  92. addList = append(addList, tmp)
  93. }
  94. }
  95. }
  96. startDate = startDate.AddDate(0, 0, 1)
  97. }
  98. if len(addList) > 0 {
  99. item := new(company.CompanyRenewalRecord)
  100. err = item.InsertMulti(addList)
  101. }
  102. return
  103. }