article.go 11 KB

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