cygx_yanxuan_special_company.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "hongze/hongze_clpt/models"
  8. "hongze/hongze_clpt/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. _, e = models.AddCygxYanxuanSpecialRecord(item) // 添加历史记录
  112. if e != nil {
  113. err = errors.New("AddCygxYanxuanSpecialRecord, Err: " + e.Error())
  114. return
  115. }
  116. //如果不是弘则研究的人员阅读的,就修改Pv、Uv数量
  117. if user.CompanyId != utils.HZ_COMPANY_ID {
  118. //专栏Pv数量进行加一
  119. e = models.UpdateYanxuanSpecialPv(specialId)
  120. if e != nil {
  121. err = errors.New("UpdateYanxuanSpecialPv, Err: " + e.Error())
  122. return
  123. }
  124. //专栏作者Pv数量进行加一
  125. e = models.UpdateCygxYanxuanSpecialAuthorPv(detail.UserId)
  126. if e != nil {
  127. err = errors.New("UpdateCygxYanxuanSpecialAuthorPv, Err: " + e.Error())
  128. return
  129. }
  130. //如果没有阅读过,那么就给专栏文章的UV、作者的UV进行加一
  131. if totalRecord == 0 {
  132. e = models.UpdateYanxuanSpecialUv(specialId)
  133. if e != nil {
  134. err = errors.New("UpdateYanxuanSpecialUv, Err: " + e.Error())
  135. return
  136. }
  137. //专栏作者Uv数量进行加一
  138. e = models.UpdateCygxYanxuanSpecialAuthorUv(detail.UserId)
  139. if e != nil {
  140. err = errors.New("UpdateCygxYanxuanSpecialAuthorUv, Err: " + e.Error())
  141. return
  142. }
  143. }
  144. } else {
  145. //专栏Pv数量进行加一
  146. e = models.UpdateYanxuanSpecialHzPv(specialId)
  147. if e != nil {
  148. err = errors.New("UpdateYanxuanSpecialHzPv, Err: " + e.Error())
  149. return
  150. }
  151. //如果没有阅读过,那么就给专栏文章的UV、作者的UV进行加一
  152. if totalRecord == 0 {
  153. e = models.UpdateYanxuanSpecialHzUv(specialId)
  154. if e != nil {
  155. err = errors.New("UpdateYanxuanSpecialHzUv, Err: " + e.Error())
  156. return
  157. }
  158. }
  159. }
  160. }
  161. itemLog := new(models.CygxYanxuanSpecialRecordLog)
  162. itemLog.UserId = user.UserId
  163. itemLog.Mobile = user.Mobile
  164. itemLog.Email = user.Email
  165. itemLog.CompanyId = user.CompanyId
  166. itemLog.CompanyName = user.CompanyName
  167. itemLog.RealName = user.RealName
  168. itemLog.SellerName = sellerName
  169. itemLog.CreateTime = time.Now().Add(-time.Duration(stopTime) * time.Second) // 往前推迟的时间就是他的阅读时间
  170. itemLog.ModifyTime = time.Now()
  171. itemLog.RegisterPlatform = utils.REGISTER_PLATFORM
  172. itemLog.YanxuanSpecialId = specialId
  173. itemLog.StopTime = stopTime
  174. itemLog.PermissionCode = permissionCode
  175. _, e = models.AddCygxYanxuanSpecialRecordLog(itemLog) // 添加历史记录
  176. if e != nil {
  177. err = errors.New("AddCygxYanxuanSpecialRecordLog, Err: " + e.Error())
  178. return
  179. }
  180. return
  181. }
  182. // GetYanxuanSpecialRecordByYanxuanSpecialId 获取研选专栏阅读 pv map
  183. func GetYanxuanSpecialRecordByYanxuanSpecialId(articleIds []int) (mapResp map[int]int) {
  184. var err error
  185. defer func() {
  186. if err != nil {
  187. fmt.Println(err)
  188. go utils.SendAlarmMsg("获取研选专栏阅读,信息失败,GetYanxuanSpecialRecordByYanxuanSpecialId Err:"+err.Error(), 3)
  189. }
  190. }()
  191. lenIds := len(articleIds)
  192. if lenIds == 0 {
  193. return
  194. }
  195. var condition string
  196. var pars []interface{}
  197. //condition = ` AND yanxuan_special_id IN (` + utils.GetOrmInReplace(lenIds) + `) `
  198. //pars = append(pars, articleIds)
  199. //listPv, e := models.GetCygxYanxuanSpecialRecordListPv(condition, pars)
  200. //if e != nil {
  201. // err = errors.New("GetCygxArticleHistoryRecordNewpvListPvCy, Err: " + e.Error())
  202. // return
  203. //}
  204. condition += ` AND id IN (` + utils.GetOrmInReplace(lenIds) + `) `
  205. pars = append(pars, articleIds)
  206. listPv, e := models.GetYanxuanSpecialListBycondition(condition, pars, 0, lenIds)
  207. if e != nil && e.Error() != utils.ErrNoRow() {
  208. err = errors.New("GetYanxuanSpecialListBycondition, Err: " + e.Error())
  209. return
  210. }
  211. mapResp = make(map[int]int, 0)
  212. for _, v := range listPv {
  213. mapResp[v.Id] = v.Pv + v.HzPv
  214. }
  215. return
  216. }