article.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "hongze/hongze_cygx/models"
  5. "hongze/hongze_cygx/services"
  6. "hongze/hongze_cygx/utils"
  7. "html"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type ArticleController struct {
  13. BaseAuthController
  14. }
  15. type ArticleCommonController struct {
  16. BaseCommonController
  17. }
  18. // @Title 获取报告详情
  19. // @Description 获取报告详情接口
  20. // @Param ArticleId query int true "报告ID"
  21. // @Success 200 {object} models.ArticleDetailResp
  22. // @router /detail [get]
  23. func (this *ArticleController) Detail() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. user := this.User
  30. if user == nil {
  31. br.Msg = "请登录"
  32. br.ErrMsg = "请登录,用户信息为空"
  33. br.Ret = 408
  34. return
  35. }
  36. uid := user.UserId
  37. articleId, err := this.GetInt("ArticleId")
  38. if articleId <= 0 {
  39. br.Msg = "文章不存在"
  40. br.ErrMsg = "文章不存在,文章ID错误"
  41. return
  42. }
  43. detail := new(models.ArticleDetail)
  44. hasPermission := 0
  45. hasFree := 0
  46. //判断是否已经申请过
  47. applyCount, err := models.GetApplyRecordCount(uid)
  48. if err != nil && err.Error() != utils.ErrNoRow() {
  49. br.Msg = "获取信息失败"
  50. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  51. return
  52. }
  53. //`description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,4:潜在客户,未提交过申请,5:潜在客户,已提交过申请"`
  54. if user.CompanyId > 1 {
  55. companyPermission, err := models.GetCompanyPermission(user.CompanyId)
  56. if err != nil {
  57. br.Msg = "获取信息失败"
  58. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  59. return
  60. }
  61. detail, err = models.GetArticleDetailById(articleId)
  62. if err != nil {
  63. br.Msg = "获取信息失败"
  64. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  65. return
  66. }
  67. detail.Body = html.UnescapeString(detail.Body)
  68. //detail.Abstract = html.UnescapeString(detail.Abstract)
  69. detail.Abstract, _ = services.GetReportContentTextSub(detail.Abstract)
  70. if companyPermission == "" {
  71. if applyCount > 0 {
  72. hasPermission = 5
  73. } else {
  74. hasPermission = 2
  75. }
  76. hasFree = 2
  77. goto Loop
  78. } else {
  79. hasFree = 1
  80. // 原有的权限校验 更改于 2021-05-18
  81. //articlePermission, err := models.GetArticlePermission(detail.SubCategoryName)
  82. //fmt.Println(articlePermission)
  83. //fmt.Println(detail.SubCategoryName)
  84. //if err != nil {
  85. // br.Msg = "获取信息失败"
  86. // br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  87. // return
  88. //}
  89. //if articlePermission == nil {
  90. // br.Msg = "获取信息失败"
  91. // br.ErrMsg = "报告权限不存在,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  92. // return
  93. //}
  94. //for _, p := range articlePermission {
  95. // if strings.Contains(companyPermission, p.PermissionName) {
  96. // hasPermission = 1
  97. // historyRecord := new(models.CygxArticleHistoryRecord)
  98. // historyRecord.UserId = uid
  99. // historyRecord.ArticleId = articleId
  100. // historyRecord.CreateTime = time.Now()
  101. // historyRecord.Mobile = user.Mobile
  102. // historyRecord.Email = user.Email
  103. // historyRecord.CompanyId = user.CompanyId
  104. // historyRecord.CompanyName = user.CompanyName
  105. // go models.AddCygxArticleHistoryRecord(historyRecord)
  106. // break
  107. // } else { //无该行业权限
  108. // hasPermission = 3
  109. // }
  110. //}
  111. var articlePermissionPermissionName string
  112. if detail.CategoryId > 0 {
  113. articlePermission, err := models.GetArticlePermission(detail.CategoryId)
  114. if err != nil {
  115. br.Msg = "获取信息失败"
  116. br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  117. return
  118. }
  119. if articlePermission == nil {
  120. br.Msg = "获取信息失败"
  121. br.ErrMsg = "报告权限不存在,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  122. return
  123. }
  124. articlePermissionPermissionName = articlePermission.PermissionName
  125. } else {
  126. articlePermissionPermissionName = detail.CategoryName
  127. }
  128. var detailNew *models.AddStopTimeNewRep
  129. detailNew, _ = models.GetNewArticleHistoryRecord(uid, articleId)
  130. if detailNew != nil {
  131. if detailNew.StopTime != 0 {
  132. if strings.Contains(companyPermission, articlePermissionPermissionName) {
  133. hasPermission = 1
  134. historyRecord := new(models.CygxArticleHistoryRecord)
  135. historyRecord.UserId = uid
  136. historyRecord.ArticleId = articleId
  137. historyRecord.CreateTime = time.Now()
  138. historyRecord.Mobile = user.Mobile
  139. historyRecord.Email = user.Email
  140. historyRecord.CompanyId = user.CompanyId
  141. historyRecord.CompanyName = user.CompanyName
  142. go models.AddCygxArticleHistoryRecord(historyRecord)
  143. } else { //无该行业权限
  144. hasPermission = 3
  145. }
  146. }
  147. }
  148. if hasPermission == 1 {
  149. key := "CYGX_ARTICLE_" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid)
  150. if !utils.Rc.IsExist(key) {
  151. //新增浏览记录
  152. record := new(models.CygxArticleViewRecord)
  153. record.UserId = uid
  154. record.ArticleId = articleId
  155. record.CreateTime = time.Now()
  156. record.Mobile = user.Mobile
  157. record.Email = user.Email
  158. record.CompanyId = user.CompanyId
  159. record.CompanyName = user.CompanyName
  160. go models.AddCygxArticleViewRecord(record)
  161. utils.Rc.Put(key, 1, 5*time.Second)
  162. models.ModifyReportLastViewTime(uid)
  163. }
  164. }
  165. }
  166. collectCount, err := models.GetArticleCollectCount(uid, articleId)
  167. if err != nil && err.Error() != utils.ErrNoRow() {
  168. br.Msg = "获取信息失败"
  169. br.ErrMsg = "判断是否已收藏失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  170. return
  171. }
  172. if collectCount > 0 {
  173. detail.IsCollect = true
  174. }
  175. interviewApplyItem, err := models.GetArticleInterviewApply(uid, articleId)
  176. if err != nil && err.Error() != utils.ErrNoRow() {
  177. br.Msg = "获取信息失败"
  178. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  179. return
  180. }
  181. if interviewApplyItem != nil && interviewApplyItem.InterviewApplyId > 0 {
  182. detail.IsInterviewApply = true
  183. detail.InterviewApplyStatus = interviewApplyItem.Status
  184. }
  185. //获取销售手机号
  186. sellerItem, err := models.GetSellerByCompanyId(user.CompanyId)
  187. if err != nil {
  188. br.Msg = "获取信息失败"
  189. br.ErrMsg = "获取销售数据失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  190. return
  191. }
  192. if sellerItem != nil {
  193. detail.SellerMobile = sellerItem.Mobile
  194. detail.SellerName = sellerItem.RealName
  195. }
  196. sellerList, err := models.GetSellerList(articleId)
  197. if err != nil {
  198. br.Msg = "获取信息失败"
  199. br.ErrMsg = "获取销售数据失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  200. return
  201. }
  202. detail.SellerList = sellerList
  203. } else { //潜在客户
  204. if applyCount > 0 {
  205. hasPermission = 5
  206. } else {
  207. hasPermission = 4
  208. }
  209. }
  210. Loop:
  211. if hasPermission != 1 {
  212. detail.Body = ""
  213. detail.BodyText = ""
  214. }
  215. resp := new(models.ArticleDetailResp)
  216. resp.HasPermission = hasPermission
  217. resp.HasFree = hasFree
  218. resp.Detail = detail
  219. br.Ret = 200
  220. br.Success = true
  221. br.Msg = "获取成功"
  222. br.Data = resp
  223. }
  224. // @Title 收藏
  225. // @Description 收藏
  226. // @Param request body models.ArticleCollectReq true "type json string"
  227. // @Success 200 {object} models.FontsCollectResp
  228. // @router /collect [post]
  229. func (this *ArticleController) ArticleCollect() {
  230. br := new(models.BaseResponse).Init()
  231. defer func() {
  232. this.Data["json"] = br
  233. this.ServeJSON()
  234. }()
  235. user := this.User
  236. if user == nil {
  237. br.Msg = "请重新登录"
  238. br.Ret = 408
  239. return
  240. }
  241. uid := user.UserId
  242. var req models.ArticleCollectReq
  243. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  244. if err != nil {
  245. br.Msg = "参数解析异常!"
  246. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  247. return
  248. }
  249. count, err := models.GetArticleCollectCount(uid, req.ArticleId)
  250. if err != nil {
  251. br.Msg = "获取数据失败!"
  252. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  253. return
  254. }
  255. resp := new(models.ArticleCollectResp)
  256. if count <= 0 {
  257. item := new(models.CygxArticleCollect)
  258. item.ArticleId = req.ArticleId
  259. item.UserId = uid
  260. item.CreateTime = time.Now()
  261. _, err = models.AddCygxArticleCollect(item)
  262. if err != nil {
  263. br.Msg = "收藏失败"
  264. br.ErrMsg = "收藏失败,Err:" + err.Error()
  265. return
  266. }
  267. br.Msg = "收藏成功"
  268. resp.Status = 1
  269. } else {
  270. err = models.RemoveArticleCollect(uid, req.ArticleId)
  271. if err != nil {
  272. br.Msg = "取消收藏失败"
  273. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  274. return
  275. }
  276. br.Msg = "已取消收藏"
  277. resp.Status = 2
  278. }
  279. collectTotal, err := models.GetArticleCollectUsersCount(req.ArticleId)
  280. if err != nil {
  281. br.Msg = "获取数据失败"
  282. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  283. return
  284. }
  285. resp.CollectCount = collectTotal
  286. br.Ret = 200
  287. br.Success = true
  288. br.Data = resp
  289. }
  290. // @Title 访谈申请
  291. // @Description 访谈申请
  292. // @Param request body models.ArticleInterviewApplyReq true "type json string"
  293. // @Success 200 {object} models.FontsCollectResp
  294. // @router /interview/apply [post]
  295. func (this *ArticleController) InterviewApply() {
  296. br := new(models.BaseResponse).Init()
  297. defer func() {
  298. this.Data["json"] = br
  299. this.ServeJSON()
  300. }()
  301. user := this.User
  302. if user == nil {
  303. br.Msg = "请重新登录"
  304. br.Ret = 408
  305. return
  306. }
  307. uid := user.UserId
  308. var req models.ArticleInterviewApplyReq
  309. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  310. if err != nil {
  311. br.Msg = "参数解析异常!"
  312. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  313. return
  314. }
  315. article, err := models.GetArticleDetailById(req.ArticleId)
  316. if err != nil {
  317. br.Msg = "获取纪要失败!"
  318. br.ErrMsg = "获取纪要失败,Err:" + err.Error()
  319. return
  320. }
  321. count, err := models.GetArticleInterviewApplyCount(uid, req.ArticleId)
  322. if err != nil {
  323. br.Msg = "获取数据失败!"
  324. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  325. return
  326. }
  327. resp := new(models.ArticleInterviewApplyResp)
  328. if count <= 0 {
  329. item := new(models.CygxInterviewApply)
  330. item.ArticleId = req.ArticleId
  331. item.UserId = uid
  332. item.CompanyId = user.CompanyId
  333. item.Status = "待邀请"
  334. item.Sort = 1
  335. item.ArticleTitle = article.Title
  336. item.CreateTime = time.Now()
  337. item.ModifyTime = time.Now()
  338. item.ArticleIdMd5 = article.ArticleIdMd5
  339. _, err = models.AddCygxInterviewApply(item)
  340. if err != nil {
  341. br.Msg = "申请失败"
  342. br.ErrMsg = "申请失败,Err:" + err.Error()
  343. return
  344. }
  345. br.Msg = "申请成功"
  346. resp.Status = 1
  347. //发送模板消息
  348. if user.CompanyId > 1 {
  349. mobile := user.Mobile
  350. if mobile == "" {
  351. mobile = user.Email
  352. }
  353. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  354. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  355. openIpItem, _ := models.GetUserRecordByUserId(sellerItem.UserId, 1)
  356. if openIpItem != nil && openIpItem.OpenId != "" {
  357. go services.SendInterviewApplyTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
  358. }
  359. }
  360. }
  361. } else {
  362. err = models.RemoveArticleInterviewApply(uid, req.ArticleId)
  363. if err != nil {
  364. br.Msg = "取消申请失败"
  365. br.ErrMsg = "取消申请失败,Err:" + err.Error()
  366. return
  367. }
  368. br.Msg = "已取消申请"
  369. resp.Status = 2
  370. if user.CompanyId > 1 {
  371. mobile := user.Mobile
  372. if mobile == "" {
  373. mobile = user.Email
  374. }
  375. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  376. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  377. openIpItem, _ := models.GetUserRecordByUserId(sellerItem.UserId, 1)
  378. if openIpItem != nil && openIpItem.OpenId != "" {
  379. go services.SendInterviewApplyCancelTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
  380. }
  381. }
  382. }
  383. }
  384. br.Ret = 200
  385. br.Success = true
  386. br.Data = resp
  387. }
  388. // @Title 获取报告详情
  389. // @Description 获取报告详情接口
  390. // @Param ArticleIdMd5 query int true "报告ID"
  391. // @Success 200 {object} models.ArticleDetailResp
  392. // @router /look/detail [get]
  393. func (this *ArticleCommonController) Detail() {
  394. br := new(models.BaseResponse).Init()
  395. defer func() {
  396. this.Data["json"] = br
  397. this.ServeJSON()
  398. }()
  399. articleIdMd5 := this.GetString("ArticleIdMd5")
  400. if articleIdMd5 == "" {
  401. br.Msg = "参数错误"
  402. br.ErrMsg = "参数错误"
  403. return
  404. }
  405. resp := new(models.ArticleDetailResp)
  406. detail, err := models.GetArticleDetailByIdMd5(articleIdMd5)
  407. if err != nil && err.Error() != utils.ErrNoRow() {
  408. br.Msg = "获取信息失败"
  409. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  410. return
  411. }
  412. if detail == nil {
  413. resp.HasPermission = 2
  414. } else {
  415. resp.HasPermission = 1
  416. }
  417. if detail != nil {
  418. detail.Body = html.UnescapeString(detail.Body)
  419. detail.Abstract = html.UnescapeString(detail.Abstract)
  420. }
  421. sellerList, err := models.GetSellerList(detail.ArticleId)
  422. if err != nil {
  423. br.Msg = "获取信息失败"
  424. br.ErrMsg = "获取销售数据失败,Err:" + err.Error() + ";articleId" + strconv.Itoa(detail.ArticleId)
  425. return
  426. }
  427. detail.SellerList = sellerList
  428. resp.Detail = detail
  429. br.Ret = 200
  430. br.Success = true
  431. br.Msg = "获取成功"
  432. br.Data = resp
  433. }
  434. // @Title 上传文章阅读时间
  435. // @Description 上传文章阅读时间接口
  436. // @Param request body models.AddStopTimeRep true "type json string"
  437. // @Success 200 {object} models.ArticleDetailResp
  438. // @router /addStopTime [post]
  439. func (this *ArticleController) AddStopTime() {
  440. br := new(models.BaseResponse).Init()
  441. defer func() {
  442. this.Data["json"] = br
  443. this.ServeJSON()
  444. }()
  445. user := this.User
  446. if user == nil {
  447. br.Msg = "请登录"
  448. br.ErrMsg = "请登录,用户信息为空"
  449. br.Ret = 408
  450. return
  451. }
  452. var req models.AddStopTimeRep
  453. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  454. if err != nil {
  455. br.Msg = "参数解析异常!"
  456. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  457. return
  458. }
  459. uid := user.UserId
  460. articleId := req.ArticleId
  461. stopTime := req.StopTime
  462. if articleId <= 0 {
  463. br.Msg = "参数错误"
  464. br.ErrMsg = "参数错误"
  465. return
  466. }
  467. if stopTime == 0 {
  468. stopTime = 1
  469. //br.Msg = "时间格式错误"
  470. //br.ErrMsg = "时间错误"
  471. //return
  472. }
  473. detail := new(models.ArticleDetail)
  474. hasPermission := 0
  475. hasFree := 0
  476. //判断是否已经申请过
  477. applyCount, err := models.GetApplyRecordCount(uid)
  478. if err != nil && err.Error() != utils.ErrNoRow() {
  479. br.Msg = "获取信息失败"
  480. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  481. return
  482. }
  483. //`description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,4:潜在客户,未提交过申请,5:潜在客户,已提交过申请"`
  484. if user.CompanyId > 1 {
  485. companyPermission, err := models.GetCompanyPermission(user.CompanyId)
  486. if err != nil {
  487. br.Msg = "获取信息失败"
  488. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  489. return
  490. }
  491. detail, err = models.GetArticleDetailById(articleId)
  492. if err != nil {
  493. br.Msg = "获取信息失败"
  494. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  495. return
  496. }
  497. if companyPermission == "" {
  498. if applyCount > 0 {
  499. hasPermission = 5
  500. } else {
  501. hasPermission = 2
  502. }
  503. hasFree = 2
  504. goto Loop
  505. } else {
  506. hasFree = 1
  507. var articlePermissionPermissionName string
  508. if detail.CategoryId > 0 {
  509. articlePermission, err := models.GetArticlePermission(detail.CategoryId)
  510. if err != nil {
  511. br.Msg = "获取信息失败"
  512. br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  513. return
  514. }
  515. if articlePermission == nil {
  516. br.Msg = "获取信息失败"
  517. br.ErrMsg = "报告权限不存在,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  518. return
  519. }
  520. articlePermissionPermissionName = articlePermission.PermissionName
  521. } else {
  522. articlePermissionPermissionName = detail.CategoryName
  523. }
  524. if strings.Contains(companyPermission, articlePermissionPermissionName) {
  525. detailNew, err := models.GetNewArticleHistoryRecord(uid, articleId)
  526. if err != nil {
  527. br.Msg = "获取信息失败"
  528. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  529. return
  530. }
  531. hasPermission = 1
  532. historyRecord := new(models.AddStopTimeNewRep)
  533. historyRecord.StopTime = detailNew.StopTime + stopTime
  534. historyRecord.Id = detailNew.Id
  535. go models.AddArticleStopTime(historyRecord)
  536. } else { //无该行业权限
  537. hasPermission = 3
  538. }
  539. }
  540. } else { //潜在客户
  541. if applyCount > 0 {
  542. hasPermission = 5
  543. } else {
  544. hasPermission = 4
  545. }
  546. }
  547. Loop:
  548. resp := new(models.ArticleDetailAddStopTimeRep)
  549. resp.HasPermission = hasPermission
  550. resp.HasFree = hasFree
  551. br.Ret = 200
  552. br.Success = true
  553. br.Msg = "操作成功"
  554. br.Data = resp
  555. }