article.go 9.8 KB

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