article.go 17 KB

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