cygx_yanxuan_special_company.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "hongze/hongze_cygx/models"
  8. "hongze/hongze_cygx/utils"
  9. "io/ioutil"
  10. "net/http"
  11. "time"
  12. )
  13. func GetStocksFromVmp(cont context.Context) (err error) {
  14. defer func() {
  15. if err != nil {
  16. fmt.Println("err:", err)
  17. go utils.SendAlarmMsg("更新上市公司表失败"+err.Error(), 2)
  18. }
  19. }()
  20. err = models.DelYanxuanSpecialCompany()
  21. if err != nil {
  22. fmt.Println("获取上市公司信息失败 Err:%s", err.Error())
  23. return
  24. }
  25. getUrl := "https://vmp.hzinsights.com/v2api/articles/stock"
  26. result, err := http.Get(getUrl)
  27. if err != nil {
  28. return
  29. }
  30. defer result.Body.Close()
  31. body, err := ioutil.ReadAll(result.Body)
  32. if err != nil {
  33. fmt.Println("err:" + err.Error())
  34. return
  35. }
  36. fmt.Println(body)
  37. var resp models.VmpStocks
  38. err = json.Unmarshal(body, &resp)
  39. if err != nil {
  40. fmt.Println("获取上市公司信息失败 Err:%s", err.Error())
  41. return
  42. }
  43. items := make([]*models.CygxYanxuanSpecialCompany, 0)
  44. for i, _ := range resp.Data {
  45. items = append(items, &resp.Data[i])
  46. if len(items) > 5000 {
  47. err = models.AddCygxYanxuanSpecialCompanyMulti(items)
  48. if err != nil {
  49. fmt.Println("AddCygxYanxuanSpecialCompanyMulti Err:%s", err.Error())
  50. return
  51. }
  52. items = []*models.CygxYanxuanSpecialCompany{}
  53. }
  54. }
  55. err = models.AddCygxYanxuanSpecialCompanyMulti(items)
  56. if err != nil {
  57. fmt.Println("AddCygxYanxuanSpecialCompanyMulti Err:%s", err.Error())
  58. return
  59. }
  60. return
  61. }
  62. // 记录用户阅读时长
  63. func AddSpecialRecord(user *models.WxUserItem, specialId, stopTime int) (err error) {
  64. if user.UserId == 0 {
  65. return
  66. }
  67. defer func() {
  68. if err != nil {
  69. go utils.SendAlarmMsg(fmt.Sprint("记录用户阅读时长 失败 AddSpecialRecord Err:"+err.Error(), "userId:", user.UserId, "specialId:", specialId), 2)
  70. }
  71. }()
  72. //校验研选专栏权限,以及无权限的时候的对应状态码
  73. havePower, e := GetYanxuanSpecialDetailUserPower(user)
  74. if e != nil {
  75. err = errors.New("GetYanxuanSpecialDetailUserPower, Err: " + e.Error())
  76. return
  77. }
  78. var permissionCode int
  79. if havePower {
  80. permissionCode = 1
  81. }
  82. var sellerName string
  83. //获取销售信息
  84. sellerName, _, _ = GetSellerName(user)
  85. if stopTime >= 3 {
  86. //判断一个用户是否阅读过 某一篇研选专栏
  87. totalRecord, e := models.GetCygxYanxuanSpecialRecordCountByUser(user.UserId, specialId)
  88. if e != nil {
  89. err = errors.New("GetCygxYanxuanSpecialRecordCountByUser, Err: " + e.Error())
  90. return
  91. }
  92. detail, e := models.GetYanxuanSpecialBySpecialId(specialId)
  93. if e != nil {
  94. err = errors.New("GetYanxuanSpecialBySpecialId, Err: " + e.Error())
  95. return
  96. }
  97. item := new(models.CygxYanxuanSpecialRecord)
  98. item.UserId = user.UserId
  99. item.Mobile = user.Mobile
  100. item.Email = user.Email
  101. item.CompanyId = user.CompanyId
  102. item.CompanyName = user.CompanyName
  103. item.RealName = user.RealName
  104. item.SellerName = sellerName
  105. item.CreateTime = time.Now().Add(-time.Duration(stopTime) * time.Second) // 往前推迟的时间就是他的阅读时间
  106. item.ModifyTime = time.Now()
  107. item.RegisterPlatform = utils.REGISTER_PLATFORM
  108. item.YanxuanSpecialId = specialId
  109. item.StopTime = stopTime
  110. item.PermissionCode = permissionCode
  111. //go CygxRaiServeBillRedisAdd(detail.Title, utils.CYGX_OBJ_YANXUANSPECIAL, user.UserId, user.CompanyId, specialId, utils.REGISTER_PLATFORM, item.CreateTime) // 权益服务统计添加到Redis队列中
  112. _, e = models.AddCygxYanxuanSpecialRecord(item) // 添加历史记录
  113. if e != nil {
  114. err = errors.New("AddCygxYanxuanSpecialRecord, Err: " + e.Error())
  115. return
  116. }
  117. //如果不是弘则研究的人员阅读的,就修改Pv、Uv数量
  118. if user.CompanyId != utils.HZ_COMPANY_ID {
  119. //专栏Pv数量进行加一
  120. e = models.UpdateYanxuanSpecialPv(specialId)
  121. if e != nil {
  122. err = errors.New("UpdateYanxuanSpecialPv, Err: " + e.Error())
  123. return
  124. }
  125. //专栏作者Pv数量进行加一
  126. e = models.UpdateCygxYanxuanSpecialAuthorPv(detail.UserId)
  127. if e != nil {
  128. err = errors.New("UpdateCygxYanxuanSpecialAuthorPv, Err: " + e.Error())
  129. return
  130. }
  131. //如果没有阅读过,那么就给专栏文章的UV、作者的UV进行加一
  132. if totalRecord == 0 {
  133. e = models.UpdateYanxuanSpecialUv(specialId)
  134. if e != nil {
  135. err = errors.New("UpdateYanxuanSpecialUv, Err: " + e.Error())
  136. return
  137. }
  138. //专栏作者Uv数量进行加一
  139. e = models.UpdateCygxYanxuanSpecialAuthorUv(detail.UserId)
  140. if e != nil {
  141. err = errors.New("UpdateCygxYanxuanSpecialAuthorUv, Err: " + e.Error())
  142. return
  143. }
  144. }
  145. } else {
  146. //专栏Pv数量进行加一
  147. e = models.UpdateYanxuanSpecialHzPv(specialId)
  148. if e != nil {
  149. err = errors.New("UpdateYanxuanSpecialHzPv, Err: " + e.Error())
  150. return
  151. }
  152. //如果没有阅读过,那么就给专栏文章的UV、作者的UV进行加一
  153. if totalRecord == 0 {
  154. e = models.UpdateYanxuanSpecialHzUv(specialId)
  155. if e != nil {
  156. err = errors.New("UpdateYanxuanSpecialHzUv, Err: " + e.Error())
  157. return
  158. }
  159. }
  160. }
  161. }
  162. itemLog := new(models.CygxYanxuanSpecialRecordLog)
  163. itemLog.UserId = user.UserId
  164. itemLog.Mobile = user.Mobile
  165. itemLog.Email = user.Email
  166. itemLog.CompanyId = user.CompanyId
  167. itemLog.CompanyName = user.CompanyName
  168. itemLog.RealName = user.RealName
  169. itemLog.SellerName = sellerName
  170. itemLog.CreateTime = time.Now().Add(-time.Duration(stopTime) * time.Second) // 往前推迟的时间就是他的阅读时间
  171. itemLog.ModifyTime = time.Now()
  172. itemLog.RegisterPlatform = utils.REGISTER_PLATFORM
  173. itemLog.YanxuanSpecialId = specialId
  174. itemLog.StopTime = stopTime
  175. itemLog.PermissionCode = permissionCode
  176. _, e = models.AddCygxYanxuanSpecialRecordLog(itemLog) // 添加历史记录
  177. if e != nil {
  178. err = errors.New("AddCygxYanxuanSpecialRecordLog, Err: " + e.Error())
  179. return
  180. }
  181. return
  182. }
  183. // GetYanxuanSpecialRecordByYanxuanSpecialId 获取研选专栏阅读 pv map
  184. func GetYanxuanSpecialRecordByYanxuanSpecialId(articleIds []int) (mapResp map[int]int) {
  185. var err error
  186. defer func() {
  187. if err != nil {
  188. fmt.Println(err)
  189. go utils.SendAlarmMsg("获取研选专栏阅读,信息失败,GetYanxuanSpecialRecordByYanxuanSpecialId Err:"+err.Error(), 3)
  190. }
  191. }()
  192. lenIds := len(articleIds)
  193. if lenIds == 0 {
  194. return
  195. }
  196. var condition string
  197. var pars []interface{}
  198. //condition = ` AND yanxuan_special_id IN (` + utils.GetOrmInReplace(lenIds) + `) `
  199. //pars = append(pars, articleIds)
  200. //listPv, e := models.GetCygxYanxuanSpecialRecordListPv(condition, pars)
  201. //if e != nil {
  202. // err = errors.New("GetCygxArticleHistoryRecordNewpvListPvCy, Err: " + e.Error())
  203. // return
  204. //}
  205. condition += ` AND id IN (` + utils.GetOrmInReplace(lenIds) + `) `
  206. pars = append(pars, articleIds)
  207. listPv, e := models.GetYanxuanSpecialListBycondition(condition, pars, 0, lenIds)
  208. if e != nil && e.Error() != utils.ErrNoRow() {
  209. err = errors.New("GetYanxuanSpecialListBycondition, Err: " + e.Error())
  210. return
  211. }
  212. mapResp = make(map[int]int, 0)
  213. for _, v := range listPv {
  214. mapResp[v.Id] = v.Pv + v.HzPv
  215. }
  216. return
  217. }