report.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package roadshow
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_task/models"
  6. "hongze/hongze_task/models/roadshow"
  7. "hongze/hongze_task/utils"
  8. "strconv"
  9. "time"
  10. "context"
  11. )
  12. func AddReportRecord(cont context.Context) (err error) {
  13. defer func() {
  14. if err != nil {
  15. fmt.Println("AddReportRecord Err:" + err.Error())
  16. }
  17. }()
  18. adminAll, err := models.GetAdminList()
  19. if err != nil {
  20. return
  21. }
  22. adminMap := make(map[int]*models.AdminView)
  23. for _, v := range adminAll {
  24. adminMap[v.AdminId] = v
  25. }
  26. companyList, err := roadshow.RsCompanyList()
  27. if err != nil {
  28. return
  29. }
  30. companyMap := make(map[int]*roadshow.CompanySearchView)
  31. for _, v := range companyList {
  32. companyMap[v.CompanyId] = v
  33. }
  34. endDate := time.Now().AddDate(-1, 0, 0).Format(utils.FormatDate)
  35. existList, err := roadshow.GetExistRsReportRecord(endDate)
  36. if err != nil {
  37. return
  38. }
  39. existMap := make(map[string]*roadshow.RsReportRecord)
  40. for _, v := range existList {
  41. key := strconv.Itoa(v.RsCalendarId) + "_" + strconv.Itoa(v.RsCalendarResearcherId)
  42. existMap[key] = v
  43. }
  44. list, err := roadshow.GetRsCalendarDetail(endDate)
  45. if err != nil {
  46. return
  47. }
  48. for _, v := range list {
  49. company := new(roadshow.CompanySearchView)
  50. if v.CompanyId > 0 {
  51. company, _ = companyMap[v.CompanyId]
  52. }
  53. researcher, ok := adminMap[v.ResearcherId]
  54. if !ok {
  55. err = errors.New("研究员信息不存在:" + strconv.Itoa(v.ResearcherId))
  56. //continue
  57. }
  58. seller, ok := adminMap[v.SysUserId]
  59. if !ok {
  60. err = errors.New("销售信息不存在:" + strconv.Itoa(v.SysUserId))
  61. //return
  62. //continue
  63. }
  64. key := strconv.Itoa(v.RsCalendarId) + "_" + strconv.Itoa(v.RsCalendarResearcherId)
  65. if _, ok := existMap[key]; ok { //修改
  66. utils.FileLog.Info("update:" + strconv.Itoa(v.RsCalendarId))
  67. reportWhereParams := make(map[string]interface{})
  68. reportWhereParams["rs_calendar_id"] = v.RsCalendarId
  69. reportWhereParams["rs_calendar_researcher_id"] = v.RsCalendarResearcherId
  70. reportUpdateParams := make(map[string]interface{})
  71. if company != nil {
  72. reportUpdateParams["company_id"] = company.CompanyId
  73. reportUpdateParams["company_name"] = company.CompanyName
  74. reportUpdateParams["credit_code"] = company.CreditCode
  75. reportUpdateParams["company_status"] = company.Status
  76. }
  77. reportUpdateParams["activity_type"] = v.ActivityType
  78. if researcher != nil {
  79. reportUpdateParams["researcher_id"] = researcher.AdminId
  80. reportUpdateParams["researcher_name"] = researcher.RealName
  81. reportUpdateParams["researcher_group_id"] = researcher.GroupId
  82. reportUpdateParams["researcher_group_name"] = researcher.GroupName
  83. } else {
  84. reportUpdateParams["researcher_id"] = v.ResearcherId
  85. reportUpdateParams["researcher_name"] = v.ResearcherName
  86. reportUpdateParams["researcher_group_id"] = 0
  87. reportUpdateParams["researcher_group_name"] = ""
  88. }
  89. if seller != nil {
  90. reportUpdateParams["seller_id"] = seller.AdminId
  91. reportUpdateParams["seller_name"] = seller.RealName
  92. reportUpdateParams["seller_group_id"] = seller.GroupId
  93. reportUpdateParams["seller_group_name"] = seller.GroupName
  94. } else {
  95. reportUpdateParams["seller_id"] = v.SysUserId
  96. reportUpdateParams["seller_name"] = v.SysUserRealName
  97. reportUpdateParams["seller_group_id"] = 0
  98. reportUpdateParams["seller_group_name"] = ""
  99. }
  100. reportUpdateParams["start_date"] = v.StartDate
  101. reportUpdateParams["end_date"] = v.EndDate
  102. reportUpdateParams["start_time"] = v.StartTime
  103. reportUpdateParams["end_time"] = v.EndTime
  104. reportUpdateParams["start_week"] = v.StartWeek
  105. reportUpdateParams["end_week"] = v.EndWeek
  106. reportUpdateParams["rs_calendar_researcher_status"] = v.Status
  107. reportUpdateParams["activity_type"] = v.ActivityType
  108. reportUpdateParams["roadshow_type"] = v.RoadshowType
  109. reportUpdateParams["modify_time"] = time.Now()
  110. err = roadshow.UpdateRsReportRecord(reportWhereParams, reportUpdateParams)
  111. } else { //新增
  112. item := new(roadshow.RsReportRecord)
  113. if company != nil {
  114. item.CompanyId = company.CompanyId
  115. item.CompanyName = company.CompanyName
  116. item.CreditCode = company.CreditCode
  117. item.CompanyStatus = company.Status
  118. }
  119. item.ActivityType = v.ActivityType
  120. item.ResearcherId = v.ResearcherId
  121. item.ResearcherName = v.ResearcherName
  122. if researcher != nil {
  123. item.ResearcherGroupId = researcher.GroupId
  124. item.ResearcherGroupName = researcher.GroupName
  125. } else {
  126. item.ResearcherGroupId = 0
  127. item.ResearcherGroupName = ""
  128. }
  129. if seller != nil {
  130. item.SellerId = seller.AdminId
  131. item.SellerName = seller.RealName
  132. item.SellerGroupId = seller.GroupId
  133. item.SellerGroupName = seller.GroupName
  134. } else {
  135. item.SellerId = v.SysUserId
  136. item.SellerName = v.SysUserRealName
  137. item.SellerGroupId = 0
  138. item.SellerGroupName = ""
  139. }
  140. utils.FileLog.Info("add:" + strconv.Itoa(v.RsCalendarId))
  141. item.RsCalendarId = v.RsCalendarId
  142. item.RsCalendarResearcherId = v.RsCalendarResearcherId
  143. item.StartDate = v.StartDate
  144. item.EndDate = v.EndDate
  145. item.StartTime = v.StartTime
  146. item.EndTime = v.EndTime
  147. item.StartWeek = v.StartWeek
  148. item.EndWeek = v.EndWeek
  149. item.CreateTime = time.Now()
  150. item.ModifyTime = time.Now()
  151. item.RsCalendarResearcherStatus = v.Status
  152. item.RoadshowType = v.RoadshowType
  153. _, err = roadshow.AddRsReportRecord(item)
  154. if err != nil {
  155. return
  156. }
  157. }
  158. }
  159. return err
  160. }