apply_record.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package yb
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/advisory"
  8. "hongze/hz_crm_api/models/yb"
  9. "hongze/hz_crm_api/utils"
  10. "strings"
  11. "sync"
  12. "time"
  13. )
  14. // MarkApplyRecord 标记申请记录
  15. func MarkApplyRecord(applyRecordId, adminId, userId int) (err error) {
  16. o := orm.NewOrm()
  17. to, err := o.Begin()
  18. if err != nil {
  19. return
  20. }
  21. defer func() {
  22. if err != nil {
  23. _ = to.Rollback()
  24. } else {
  25. _ = to.Commit()
  26. }
  27. }()
  28. if applyRecordId > 0 {
  29. applyRecord, tErr := yb.GetApplyRecordById(applyRecordId)
  30. if tErr != nil {
  31. if tErr.Error() == utils.ErrNoRow() {
  32. err = errors.New(fmt.Sprint("申请记录不存在:", err))
  33. } else {
  34. err = tErr
  35. }
  36. return
  37. }
  38. if applyRecord.OpStatus != 0 {
  39. err = errors.New(fmt.Sprint("申请记录处理状态有误:", err))
  40. return
  41. }
  42. if userId > 0 && userId != applyRecord.UserId {
  43. err = errors.New(fmt.Sprint("申请记录ID与用户ID不匹配:", err))
  44. return
  45. }
  46. userId = applyRecord.UserId
  47. // 1.标记申请记录
  48. applyRecord.OpStatus = 1
  49. applyRecord.DealTime = time.Now()
  50. applyRecord.SysUserId = adminId
  51. updateCols := make([]string, 0)
  52. updateCols = append(updateCols, "OpStatus", "DealTime", "SysUserId")
  53. err = applyRecord.Update(updateCols)
  54. if err != nil {
  55. err = errors.New(fmt.Sprint("申请记录标记失败", err))
  56. return
  57. }
  58. }
  59. // 2.标记wx用户表
  60. wxUser, err := models.GetWxUserByUserId(userId)
  61. if err != nil {
  62. if err.Error() == utils.ErrNoRow() {
  63. err = nil // 用户可能在潜在用户列表直接被删除,那么直接不标记
  64. return
  65. }
  66. return
  67. }
  68. // 未处理过则进行标记
  69. if wxUser.IsDeal == 0 {
  70. wxUser.IsDeal = 1
  71. wxUser.LastUpdatedTime = time.Now()
  72. userUpdateCols := make([]string, 0)
  73. userUpdateCols = append(userUpdateCols, "IsDeal", "LastUpdatedTime")
  74. err = wxUser.Update(userUpdateCols)
  75. if err != nil {
  76. err = errors.New(fmt.Sprint("对应用户信息标记失败", err))
  77. return
  78. }
  79. }
  80. return
  81. }
  82. func GetUserViewTotalByMobiles(mobilesSlice []string) (mobileTotalMap map[string]int, lastTimeMap map[string]time.Time) {
  83. //根据手机号查询
  84. //手机号
  85. mobileTotalMap = make(map[string]int)
  86. lastTimeMap = make(map[string]time.Time)
  87. if len(mobilesSlice) > 0 {
  88. mobileStr := strings.Join(mobilesSlice, "','")
  89. mobileStr = "'" + mobileStr + "'"
  90. w := sync.WaitGroup{}
  91. //用户浏览数据
  92. var userViewMobileTotalList []*models.UserViewMobileTotalSlice
  93. w.Add(1)
  94. go func() {
  95. defer w.Done()
  96. userViewMobileTotalList, _ = models.GetCountUserViewHistoryByMobiles(mobileStr)
  97. }()
  98. //每日点评手机号数据
  99. var userReportViewMobileTotalList []*models.ReportViewMobileRecord
  100. w.Add(1)
  101. go func() {
  102. defer w.Done()
  103. userReportViewMobileTotalList, _ = models.GetReportViewMaxTimeByMobiles(mobileStr)
  104. }()
  105. //研报手机号数据
  106. var chartArticleViewMobileTotalList []*advisory.UserViewMobileTotalSlice
  107. w.Add(1)
  108. go func() {
  109. defer w.Done()
  110. chartArticleViewMobileTotalList, _ = advisory.GetCountUserViewHistoryByMobiles(mobileStr)
  111. }()
  112. var articleViewMobileTotalList []*models.UserViewMobileTotalSlice
  113. w.Add(1)
  114. go func() {
  115. defer w.Done()
  116. articleViewMobileTotalList, _ = models.GetCountCygxArticleHistoryRecordByMobiles(mobileStr)
  117. }()
  118. w.Wait()
  119. for _, userViewMobile := range userViewMobileTotalList {
  120. lastTimeMap[userViewMobile.Mobile] = userViewMobile.CreatedTime
  121. mobileTotalMap[userViewMobile.Mobile] = userViewMobile.Total
  122. }
  123. //每日点评手机号数据
  124. for _, item := range userReportViewMobileTotalList {
  125. if t, ok := lastTimeMap[item.Mobile]; ok {
  126. if t.Before(item.LastViewTime) {
  127. lastTimeMap[item.Mobile] = item.LastViewTime
  128. }
  129. } else {
  130. lastTimeMap[item.Mobile] = item.LastViewTime
  131. }
  132. mobileTotalMap[item.Mobile] += item.ViewTotal
  133. }
  134. for _, item := range chartArticleViewMobileTotalList {
  135. if t, ok := lastTimeMap[item.Mobile]; ok {
  136. if t.Before(item.CreatedTime) {
  137. lastTimeMap[item.Mobile] = item.CreatedTime
  138. }
  139. } else {
  140. lastTimeMap[item.Mobile] = item.CreatedTime
  141. }
  142. mobileTotalMap[item.Mobile] += item.Total
  143. }
  144. for _, item := range articleViewMobileTotalList {
  145. if t, ok := lastTimeMap[item.Mobile]; ok {
  146. if t.Before(item.CreatedTime) {
  147. lastTimeMap[item.Mobile] = item.CreatedTime
  148. }
  149. } else {
  150. lastTimeMap[item.Mobile] = item.CreatedTime
  151. }
  152. mobileTotalMap[item.Mobile] += item.Total
  153. }
  154. }
  155. return
  156. }
  157. func GetUserViewTotalByEmails(emailsSlice []string) (emailTotalMap map[string]int, lastTimeMap map[string]time.Time) {
  158. //根据邮箱查询
  159. lastTimeMap = make(map[string]time.Time)
  160. emailTotalMap = make(map[string]int)
  161. if len(emailsSlice) > 0 {
  162. emailStr := strings.Join(emailsSlice, "','")
  163. emailStr = "'" + emailStr + "'"
  164. w := sync.WaitGroup{}
  165. //用户浏览数据
  166. var userViewEmailTotalList []*models.UserViewEmailTotalSlice
  167. w.Add(1)
  168. go func() {
  169. defer w.Done()
  170. userViewEmailTotalList, _ = models.GetCountUserViewHistoryByEmails(emailStr)
  171. }()
  172. //每日点评手机号数据
  173. var userReportViewEmailTotalList []*models.ReportViewEmailRecord
  174. w.Add(1)
  175. go func() {
  176. defer w.Done()
  177. userReportViewEmailTotalList, _ = models.GetReportViewMaxTimeByEmails(emailStr)
  178. }()
  179. // advisory_user_chart_article_record
  180. var chartArticleTotalList []*advisory.UserViewEmailTotalSlice
  181. w.Add(1)
  182. go func() {
  183. defer w.Done()
  184. chartArticleTotalList, _ = advisory.GetCountUserViewHistoryByEmails(emailStr)
  185. }()
  186. // cygx_article_history_record_newpv
  187. var articleViewTotalList []*models.UserViewEmailTotalSlice
  188. w.Add(1)
  189. go func() {
  190. defer w.Done()
  191. articleViewTotalList, _ = models.GetCountCygxArticleHistoryRecordByEmails(emailStr)
  192. }()
  193. w.Wait()
  194. for _, userViewEmail := range userViewEmailTotalList {
  195. lastTimeMap[userViewEmail.Email] = userViewEmail.CreatedTime
  196. emailTotalMap[userViewEmail.Email] = userViewEmail.Total
  197. }
  198. for _, item := range userReportViewEmailTotalList {
  199. if t, ok := lastTimeMap[item.Email]; ok {
  200. if t.Before(item.LastViewTime) {
  201. lastTimeMap[item.Email] = item.LastViewTime
  202. }
  203. } else {
  204. lastTimeMap[item.Email] = item.LastViewTime
  205. }
  206. emailTotalMap[item.Email] += item.ViewTotal
  207. }
  208. for _, item := range chartArticleTotalList {
  209. if t, ok := lastTimeMap[item.Email]; ok {
  210. if t.Before(item.CreatedTime) {
  211. lastTimeMap[item.Email] = item.CreatedTime
  212. }
  213. } else {
  214. lastTimeMap[item.Email] = item.CreatedTime
  215. }
  216. emailTotalMap[item.Email] += item.Total
  217. }
  218. for _, item := range articleViewTotalList {
  219. if t, ok := lastTimeMap[item.Email]; ok {
  220. if t.Before(item.CreatedTime) {
  221. lastTimeMap[item.Email] = item.CreatedTime
  222. }
  223. } else {
  224. lastTimeMap[item.Email] = item.CreatedTime
  225. }
  226. emailTotalMap[item.Email] += item.Total
  227. }
  228. }
  229. return
  230. }
  231. // DeleteApplyUser 删除用户的申请记录
  232. func DeleteApplyUser(userId int64) (err error) {
  233. err = yb.DelApplyRecordByUserId(userId)
  234. if err != nil {
  235. err = errors.New(fmt.Sprint("删除申请记录失败", err))
  236. return
  237. }
  238. return
  239. }
  240. // MarkGroupApplyRecord 标记申请记录和分组
  241. func MarkGroupApplyRecord(applyRecordId, adminId, userId int, groupName string) (err error) {
  242. o := orm.NewOrm()
  243. to, err := o.Begin()
  244. if err != nil {
  245. return
  246. }
  247. defer func() {
  248. if err != nil {
  249. _ = to.Rollback()
  250. } else {
  251. _ = to.Commit()
  252. }
  253. }()
  254. if applyRecordId > 0 {
  255. applyRecord, tErr := yb.GetApplyRecordById(applyRecordId)
  256. if tErr != nil {
  257. if tErr.Error() == utils.ErrNoRow() {
  258. err = errors.New(fmt.Sprint("申请记录不存在:", err))
  259. } else {
  260. err = tErr
  261. }
  262. return
  263. }
  264. //if applyRecord.OpStatus != 0 {
  265. // err = errors.New(fmt.Sprint("申请记录处理状态有误:", err))
  266. // return
  267. //}
  268. if userId > 0 && userId != applyRecord.UserId {
  269. err = errors.New(fmt.Sprint("申请记录ID与用户ID不匹配:", err))
  270. return
  271. }
  272. userId = applyRecord.UserId
  273. // 1.标记申请记录
  274. applyRecord.OpStatus = 1
  275. applyRecord.DealTime = time.Now()
  276. applyRecord.SysUserId = adminId
  277. applyRecord.MarkGroup = groupName
  278. updateCols := make([]string, 0)
  279. updateCols = append(updateCols, "OpStatus", "DealTime", "SysUserId","MarkGroup")
  280. err = applyRecord.Update(updateCols)
  281. if err != nil {
  282. err = errors.New(fmt.Sprint("申请记录标记失败", err))
  283. return
  284. }
  285. }
  286. // 2.标记wx用户表
  287. wxUser, err := models.GetWxUserByUserId(userId)
  288. if err != nil {
  289. if err.Error() == utils.ErrNoRow() {
  290. err = nil // 用户可能在潜在用户列表直接被删除,那么直接不标记
  291. return
  292. }
  293. return
  294. }
  295. // 未处理过则进行标记
  296. if wxUser.IsDeal == 0 || wxUser.MarkGroup == "" {
  297. wxUser.IsDeal = 1
  298. wxUser.MarkGroup = groupName
  299. wxUser.LastUpdatedTime = time.Now()
  300. userUpdateCols := make([]string, 0)
  301. userUpdateCols = append(userUpdateCols, "IsDeal", "MarkGroup", "LastUpdatedTime")
  302. err = wxUser.Update(userUpdateCols)
  303. if err != nil {
  304. err = errors.New(fmt.Sprint("对应用户信息标记失败", err))
  305. return
  306. }
  307. }
  308. return
  309. }