article.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_cygx/models"
  6. "hongze/hongze_cygx/utils"
  7. "html"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/astaxie/beego/cache"
  12. )
  13. type ArticleController struct {
  14. BaseAuthController
  15. }
  16. type ArticleCommonController struct {
  17. BaseCommonController
  18. }
  19. // @Title 获取报告详情
  20. // @Description 获取报告详情接口
  21. // @Param ArticleId query int true "报告ID"
  22. // @Success 200 {object} models.ArticleDetailResp
  23. // @router /detail [get]
  24. func (this *ArticleController) Detail() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. user := this.User
  31. if user == nil {
  32. br.Msg = "请登录"
  33. br.ErrMsg = "请登录,用户信息为空"
  34. br.Ret = 408
  35. return
  36. }
  37. uid := user.UserId
  38. articleId, err := this.GetInt("ArticleId")
  39. if articleId <= 0 {
  40. br.Msg = "参数错误"
  41. br.ErrMsg = "参数错误"
  42. return
  43. }
  44. detail := new(models.ArticleDetail)
  45. hasPermission := 0
  46. hasFree := 0
  47. //判断是否已经申请过
  48. applyCount, err := models.GetApplyRecordCount(uid)
  49. if err != nil && err.Error() != utils.ErrNoRow() {
  50. br.Msg = "获取信息失败"
  51. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  52. return
  53. }
  54. //`description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,4:潜在客户,未提交过申请,5:潜在客户,已提交过申请"`
  55. if user.CompanyId > 1 {
  56. companyPermission, err := models.GetCompanyPermission(user.CompanyId)
  57. if err != nil {
  58. br.Msg = "获取信息失败"
  59. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  60. return
  61. }
  62. detail, err = models.GetArticleDetailById(articleId)
  63. if err != nil {
  64. br.Msg = "获取信息失败"
  65. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  66. return
  67. }
  68. detail.Body = html.UnescapeString(detail.Body)
  69. detail.Abstract = html.UnescapeString(detail.Abstract)
  70. if companyPermission == "" {
  71. hasPermission = 2
  72. hasFree = 2
  73. goto Loop
  74. } else {
  75. hasFree = 1
  76. articlePermission, err := models.GetArticlePermission(detail.SubCategoryName)
  77. if err != nil {
  78. br.Msg = "获取信息失败"
  79. br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  80. return
  81. }
  82. if articlePermission == nil {
  83. br.Msg = "获取信息失败"
  84. br.ErrMsg = "报告权限不存在,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  85. return
  86. }
  87. if strings.Contains(companyPermission, articlePermission.PermissionName) {
  88. hasPermission = 1
  89. bm, err := cache.NewCache("file", `{"CachePath":"./temp/cache","FileSuffix":".cache","DirectoryLevel":2,"EmbedExpiry":120}`)
  90. if err != nil {
  91. fmt.Println("false")
  92. }
  93. key := "ViewRecord:" + strconv.Itoa(uid) + "_" + strconv.Itoa(articleId)
  94. if !bm.IsExist(key) {
  95. //新增浏览记录
  96. record := new(models.CygxArticleViewRecord)
  97. record.UserId = uid
  98. record.ArticleId = articleId
  99. record.CreateTime = time.Now()
  100. record.Mobile = user.Mobile
  101. record.Email = user.Email
  102. record.CompanyId = user.CompanyId
  103. record.CompanyName = user.CompanyName
  104. go models.AddCygxArticleViewRecord(record)
  105. bm.Put(key, 1, 30*time.Second)
  106. }
  107. historyRecord := new(models.CygxArticleHistoryRecord)
  108. historyRecord.UserId = uid
  109. historyRecord.ArticleId = articleId
  110. historyRecord.CreateTime = time.Now()
  111. historyRecord.Mobile = user.Mobile
  112. historyRecord.Email = user.Email
  113. historyRecord.CompanyId = user.CompanyId
  114. historyRecord.CompanyName = user.CompanyName
  115. go models.AddCygxArticleHistoryRecord(historyRecord)
  116. } else { //无该行业权限
  117. hasPermission = 3
  118. }
  119. }
  120. collectCount, err := models.GetArticleCollectCount(uid, articleId)
  121. if err != nil && err.Error() != utils.ErrNoRow() {
  122. br.Msg = "获取信息失败"
  123. br.ErrMsg = "判断是否已收藏失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  124. return
  125. }
  126. if collectCount > 0 {
  127. detail.IsCollect = true
  128. }
  129. interviewApplyItem, err := models.GetArticleInterviewApply(uid, articleId)
  130. if err != nil && err.Error() != utils.ErrNoRow() {
  131. br.Msg = "获取信息失败"
  132. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  133. return
  134. }
  135. if interviewApplyItem != nil && interviewApplyItem.InterviewApplyId > 0 {
  136. detail.IsInterviewApply = true
  137. detail.InterviewApplyStatus = interviewApplyItem.Status
  138. }
  139. //获取销售手机号
  140. sellerItem, err := models.GetSellerByCompanyId(user.CompanyId)
  141. if err != nil {
  142. br.Msg = "获取信息失败"
  143. br.ErrMsg = "获取销售数据失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  144. return
  145. }
  146. if sellerItem != nil {
  147. detail.SellerMobile = sellerItem.Mobile
  148. detail.SellerName = sellerItem.RealName
  149. }
  150. } else { //潜在客户
  151. if applyCount > 0 {
  152. hasPermission = 5
  153. } else {
  154. hasPermission = 4
  155. }
  156. }
  157. Loop:
  158. if hasPermission != 1 {
  159. detail.Body = ""
  160. detail.BodyText = ""
  161. }
  162. resp := new(models.ArticleDetailResp)
  163. resp.HasPermission = hasPermission
  164. resp.HasFree = hasFree
  165. resp.Detail = detail
  166. br.Ret = 200
  167. br.Success = true
  168. br.Msg = "获取成功"
  169. br.Data = resp
  170. }
  171. // @Title 收藏
  172. // @Description 收藏
  173. // @Param request body models.ArticleCollectReq true "type json string"
  174. // @Success 200 {object} models.FontsCollectResp
  175. // @router /collect [post]
  176. func (this *ArticleController) ArticleCollect() {
  177. br := new(models.BaseResponse).Init()
  178. defer func() {
  179. this.Data["json"] = br
  180. this.ServeJSON()
  181. }()
  182. user := this.User
  183. if user == nil {
  184. br.Msg = "请重新登录"
  185. br.Ret = 408
  186. return
  187. }
  188. uid := user.UserId
  189. var req models.ArticleCollectReq
  190. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  191. if err != nil {
  192. br.Msg = "参数解析异常!"
  193. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  194. return
  195. }
  196. count, err := models.GetArticleCollectCount(uid, req.ArticleId)
  197. if err != nil {
  198. br.Msg = "获取数据失败!"
  199. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  200. return
  201. }
  202. resp := new(models.ArticleCollectResp)
  203. if count <= 0 {
  204. item := new(models.CygxArticleCollect)
  205. item.ArticleId = req.ArticleId
  206. item.UserId = uid
  207. item.CreateTime = time.Now()
  208. _, err = models.AddCygxArticleCollect(item)
  209. if err != nil {
  210. br.Msg = "收藏失败"
  211. br.ErrMsg = "收藏失败,Err:" + err.Error()
  212. return
  213. }
  214. br.Msg = "收藏成功"
  215. resp.Status = 1
  216. } else {
  217. err = models.RemoveArticleCollect(uid, req.ArticleId)
  218. if err != nil {
  219. br.Msg = "取消收藏失败"
  220. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  221. return
  222. }
  223. br.Msg = "已取消收藏"
  224. resp.Status = 2
  225. }
  226. collectTotal, err := models.GetArticleCollectUsersCount(req.ArticleId)
  227. if err != nil {
  228. br.Msg = "获取数据失败"
  229. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  230. return
  231. }
  232. resp.CollectCount = collectTotal
  233. br.Ret = 200
  234. br.Success = true
  235. br.Data = resp
  236. }
  237. // @Title 访谈申请
  238. // @Description 访谈申请
  239. // @Param request body models.ArticleInterviewApplyReq true "type json string"
  240. // @Success 200 {object} models.FontsCollectResp
  241. // @router /interview/apply [post]
  242. func (this *ArticleController) InterviewApply() {
  243. br := new(models.BaseResponse).Init()
  244. defer func() {
  245. this.Data["json"] = br
  246. this.ServeJSON()
  247. }()
  248. user := this.User
  249. if user == nil {
  250. br.Msg = "请重新登录"
  251. br.Ret = 408
  252. return
  253. }
  254. uid := user.UserId
  255. var req models.ArticleInterviewApplyReq
  256. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  257. if err != nil {
  258. br.Msg = "参数解析异常!"
  259. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  260. return
  261. }
  262. article, err := models.GetArticleDetailById(req.ArticleId)
  263. if err != nil {
  264. br.Msg = "获取纪要失败!"
  265. br.ErrMsg = "获取纪要失败,Err:" + err.Error()
  266. return
  267. }
  268. count, err := models.GetArticleInterviewApplyCount(uid, req.ArticleId)
  269. if err != nil {
  270. br.Msg = "获取数据失败!"
  271. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  272. return
  273. }
  274. resp := new(models.ArticleInterviewApplyResp)
  275. if count <= 0 {
  276. item := new(models.CygxInterviewApply)
  277. item.ArticleId = req.ArticleId
  278. item.UserId = uid
  279. item.CompanyId = user.CompanyId
  280. item.Status = "待邀请"
  281. item.Sort = 1
  282. item.ArticleTitle = article.Title
  283. item.CreateTime = time.Now()
  284. item.ModifyTime = time.Now()
  285. item.ArticleIdMd5 = article.ArticleIdMd5
  286. _, err = models.AddCygxInterviewApply(item)
  287. if err != nil {
  288. br.Msg = "申请失败"
  289. br.ErrMsg = "申请失败,Err:" + err.Error()
  290. return
  291. }
  292. br.Msg = "申请成功"
  293. resp.Status = 1
  294. } else {
  295. err = models.RemoveArticleInterviewApply(uid, req.ArticleId)
  296. if err != nil {
  297. br.Msg = "取消申请失败"
  298. br.ErrMsg = "取消申请失败,Err:" + err.Error()
  299. return
  300. }
  301. br.Msg = "已取消申请"
  302. resp.Status = 2
  303. }
  304. br.Ret = 200
  305. br.Success = true
  306. br.Data = resp
  307. }
  308. // @Title 获取报告详情
  309. // @Description 获取报告详情接口
  310. // @Param ArticleIdMd5 query int true "报告ID"
  311. // @Success 200 {object} models.ArticleDetailResp
  312. // @router /look/detail [get]
  313. func (this *ArticleCommonController) Detail() {
  314. br := new(models.BaseResponse).Init()
  315. defer func() {
  316. this.Data["json"] = br
  317. this.ServeJSON()
  318. }()
  319. articleIdMd5 := this.GetString("ArticleIdMd5")
  320. if articleIdMd5 == "" {
  321. br.Msg = "参数错误"
  322. br.ErrMsg = "参数错误"
  323. return
  324. }
  325. resp := new(models.ArticleDetailResp)
  326. detail, err := models.GetArticleDetailByIdMd5(articleIdMd5)
  327. if err != nil && err.Error() != utils.ErrNoRow() {
  328. br.Msg = "获取信息失败"
  329. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  330. return
  331. }
  332. if detail == nil {
  333. resp.HasPermission = 2
  334. } else {
  335. resp.HasPermission = 1
  336. }
  337. resp.Detail = detail
  338. br.Ret = 200
  339. br.Success = true
  340. br.Msg = "获取成功"
  341. br.Data = resp
  342. }