article.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. 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. if companyPermission == "" {
  70. hasPermission = 2
  71. hasFree = 2
  72. goto Loop
  73. } else {
  74. hasFree = 1
  75. articlePermission, err := models.GetArticlePermission(detail.SubCategoryName)
  76. if err != nil {
  77. br.Msg = "获取信息失败"
  78. br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  79. return
  80. }
  81. if articlePermission == nil {
  82. br.Msg = "获取信息失败"
  83. br.ErrMsg = "报告权限不存在,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  84. return
  85. }
  86. if strings.Contains(companyPermission, articlePermission.PermissionName) {
  87. hasPermission = 1
  88. //bm, err := cache.NewCache("file", `{"CachePath":"./temp/cache","FileSuffix":".cache","DirectoryLevel":2,"EmbedExpiry":120}`)
  89. //if err != nil {
  90. // fmt.Println("false")
  91. //}
  92. //key := "ViewRecord:" + strconv.Itoa(uid) + "_" + strconv.Itoa(articleId)
  93. //if !bm.IsExist(key) {
  94. // //新增浏览记录
  95. // record := new(models.CygxArticleViewRecord)
  96. // record.UserId = uid
  97. // record.ArticleId = articleId
  98. // record.CreateTime = time.Now()
  99. // record.Mobile = user.Mobile
  100. // record.Email = user.Email
  101. // record.CompanyId = user.CompanyId
  102. // record.CompanyName = user.CompanyName
  103. // go models.AddCygxArticleViewRecord(record)
  104. // bm.Put(key, 1, 30*time.Second)
  105. //}
  106. historyRecord := new(models.CygxArticleHistoryRecord)
  107. historyRecord.UserId = uid
  108. historyRecord.ArticleId = articleId
  109. historyRecord.CreateTime = time.Now()
  110. historyRecord.Mobile = user.Mobile
  111. historyRecord.Email = user.Email
  112. historyRecord.CompanyId = user.CompanyId
  113. historyRecord.CompanyName = user.CompanyName
  114. go models.AddCygxArticleHistoryRecord(historyRecord)
  115. } else { //无该行业权限
  116. hasPermission = 3
  117. }
  118. }
  119. collectCount, err := models.GetArticleCollectCount(uid, articleId)
  120. if err != nil && err.Error() != utils.ErrNoRow() {
  121. br.Msg = "获取信息失败"
  122. br.ErrMsg = "判断是否已收藏失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  123. return
  124. }
  125. if collectCount > 0 {
  126. detail.IsCollect = true
  127. }
  128. interviewApplyItem, err := models.GetArticleInterviewApply(uid, articleId)
  129. if err != nil && err.Error() != utils.ErrNoRow() {
  130. br.Msg = "获取信息失败"
  131. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  132. return
  133. }
  134. if interviewApplyItem != nil && interviewApplyItem.InterviewApplyId > 0 {
  135. detail.IsInterviewApply = true
  136. detail.InterviewApplyStatus = interviewApplyItem.Status
  137. }
  138. //获取销售手机号
  139. sellerItem, err := models.GetSellerByCompanyId(user.CompanyId)
  140. if err != nil {
  141. br.Msg = "获取信息失败"
  142. br.ErrMsg = "获取销售数据失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  143. return
  144. }
  145. if sellerItem != nil {
  146. detail.SellerMobile = sellerItem.Mobile
  147. detail.SellerName = sellerItem.RealName
  148. }
  149. } else { //潜在客户
  150. if applyCount > 0 {
  151. hasPermission = 5
  152. } else {
  153. hasPermission = 4
  154. }
  155. }
  156. Loop:
  157. if hasPermission != 1 {
  158. detail.Body = ""
  159. detail.BodyText = ""
  160. }
  161. resp := new(models.ArticleDetailResp)
  162. resp.HasPermission = hasPermission
  163. resp.HasFree = hasFree
  164. resp.Detail = detail
  165. br.Ret = 200
  166. br.Success = true
  167. br.Msg = "获取成功"
  168. br.Data = resp
  169. }
  170. // @Title 收藏
  171. // @Description 收藏
  172. // @Param request body models.ArticleCollectReq true "type json string"
  173. // @Success 200 {object} models.FontsCollectResp
  174. // @router /collect [post]
  175. func (this *ArticleController) ArticleCollect() {
  176. br := new(models.BaseResponse).Init()
  177. defer func() {
  178. this.Data["json"] = br
  179. this.ServeJSON()
  180. }()
  181. user := this.User
  182. if user == nil {
  183. br.Msg = "请重新登录"
  184. br.Ret = 408
  185. return
  186. }
  187. uid := user.UserId
  188. var req models.ArticleCollectReq
  189. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  190. if err != nil {
  191. br.Msg = "参数解析异常!"
  192. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  193. return
  194. }
  195. count, err := models.GetArticleCollectCount(uid, req.ArticleId)
  196. if err != nil {
  197. br.Msg = "获取数据失败!"
  198. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  199. return
  200. }
  201. resp := new(models.ArticleCollectResp)
  202. if count <= 0 {
  203. item := new(models.CygxArticleCollect)
  204. item.ArticleId = req.ArticleId
  205. item.UserId = uid
  206. item.CreateTime = time.Now()
  207. _, err = models.AddCygxArticleCollect(item)
  208. if err != nil {
  209. br.Msg = "收藏失败"
  210. br.ErrMsg = "收藏失败,Err:" + err.Error()
  211. return
  212. }
  213. br.Msg = "收藏成功"
  214. resp.Status = 1
  215. } else {
  216. err = models.RemoveArticleCollect(uid, req.ArticleId)
  217. if err != nil {
  218. br.Msg = "取消收藏失败"
  219. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  220. return
  221. }
  222. br.Msg = "已取消收藏"
  223. resp.Status = 2
  224. }
  225. collectTotal, err := models.GetArticleCollectUsersCount(req.ArticleId)
  226. if err != nil {
  227. br.Msg = "获取数据失败"
  228. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  229. return
  230. }
  231. resp.CollectCount = collectTotal
  232. br.Ret = 200
  233. br.Success = true
  234. br.Data = resp
  235. }
  236. // @Title 访谈申请
  237. // @Description 访谈申请
  238. // @Param request body models.ArticleInterviewApplyReq true "type json string"
  239. // @Success 200 {object} models.FontsCollectResp
  240. // @router /interview/apply [post]
  241. func (this *ArticleController) InterviewApply() {
  242. br := new(models.BaseResponse).Init()
  243. defer func() {
  244. this.Data["json"] = br
  245. this.ServeJSON()
  246. }()
  247. user := this.User
  248. if user == nil {
  249. br.Msg = "请重新登录"
  250. br.Ret = 408
  251. return
  252. }
  253. uid := user.UserId
  254. var req models.ArticleInterviewApplyReq
  255. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  256. if err != nil {
  257. br.Msg = "参数解析异常!"
  258. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  259. return
  260. }
  261. article, err := models.GetArticleDetailById(req.ArticleId)
  262. if err != nil {
  263. br.Msg = "获取纪要失败!"
  264. br.ErrMsg = "获取纪要失败,Err:" + err.Error()
  265. return
  266. }
  267. count, err := models.GetArticleInterviewApplyCount(uid, req.ArticleId)
  268. if err != nil {
  269. br.Msg = "获取数据失败!"
  270. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  271. return
  272. }
  273. resp := new(models.ArticleInterviewApplyResp)
  274. if count <= 0 {
  275. item := new(models.CygxInterviewApply)
  276. item.ArticleId = req.ArticleId
  277. item.UserId = uid
  278. item.CompanyId = user.CompanyId
  279. item.Status = "待邀请"
  280. item.Sort = 1
  281. item.ArticleTitle = article.Title
  282. item.CreateTime = time.Now()
  283. item.ModifyTime = time.Now()
  284. item.ArticleIdMd5 = article.ArticleIdMd5
  285. _, err = models.AddCygxInterviewApply(item)
  286. if err != nil {
  287. br.Msg = "申请失败"
  288. br.ErrMsg = "申请失败,Err:" + err.Error()
  289. return
  290. }
  291. br.Msg = "申请成功"
  292. resp.Status = 1
  293. //发送模板消息
  294. if user.CompanyId > 1 {
  295. mobile := user.Mobile
  296. if mobile == "" {
  297. mobile = user.Email
  298. }
  299. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  300. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  301. openIpItem, _ := models.GetUserRecordByUserId(sellerItem.UserId, 1)
  302. if openIpItem != nil && openIpItem.OpenId != "" {
  303. go services.SendInterviewApplyTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
  304. }
  305. }
  306. }
  307. } else {
  308. err = models.RemoveArticleInterviewApply(uid, req.ArticleId)
  309. if err != nil {
  310. br.Msg = "取消申请失败"
  311. br.ErrMsg = "取消申请失败,Err:" + err.Error()
  312. return
  313. }
  314. br.Msg = "已取消申请"
  315. resp.Status = 2
  316. if user.CompanyId > 1 {
  317. mobile := user.Mobile
  318. if mobile == "" {
  319. mobile = user.Email
  320. }
  321. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  322. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  323. openIpItem, _ := models.GetUserRecordByUserId(sellerItem.UserId, 1)
  324. if openIpItem != nil && openIpItem.OpenId != "" {
  325. go services.SendInterviewApplyCancelTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
  326. }
  327. }
  328. }
  329. }
  330. br.Ret = 200
  331. br.Success = true
  332. br.Data = resp
  333. }
  334. // @Title 获取报告详情
  335. // @Description 获取报告详情接口
  336. // @Param ArticleIdMd5 query int true "报告ID"
  337. // @Success 200 {object} models.ArticleDetailResp
  338. // @router /look/detail [get]
  339. func (this *ArticleCommonController) Detail() {
  340. br := new(models.BaseResponse).Init()
  341. defer func() {
  342. this.Data["json"] = br
  343. this.ServeJSON()
  344. }()
  345. articleIdMd5 := this.GetString("ArticleIdMd5")
  346. if articleIdMd5 == "" {
  347. br.Msg = "参数错误"
  348. br.ErrMsg = "参数错误"
  349. return
  350. }
  351. resp := new(models.ArticleDetailResp)
  352. detail, err := models.GetArticleDetailByIdMd5(articleIdMd5)
  353. if err != nil && err.Error() != utils.ErrNoRow() {
  354. br.Msg = "获取信息失败"
  355. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  356. return
  357. }
  358. if detail == nil {
  359. resp.HasPermission = 2
  360. } else {
  361. resp.HasPermission = 1
  362. }
  363. detail.Body = html.UnescapeString(detail.Body)
  364. resp.Detail = detail
  365. br.Ret = 200
  366. br.Success = true
  367. br.Msg = "获取成功"
  368. br.Data = resp
  369. }