article.go 10 KB

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