article.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. sellerList, err := models.GetSellerList(articleId)
  141. if err != nil {
  142. br.Msg = "获取信息失败"
  143. br.ErrMsg = "获取销售数据失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  144. return
  145. }
  146. detail.SellerList = sellerList
  147. } else { //潜在客户
  148. if applyCount > 0 {
  149. hasPermission = 5
  150. } else {
  151. hasPermission = 4
  152. }
  153. }
  154. Loop:
  155. if hasPermission != 1 {
  156. detail.Body = ""
  157. detail.BodyText = ""
  158. }
  159. resp := new(models.ArticleDetailResp)
  160. resp.HasPermission = hasPermission
  161. resp.HasFree = hasFree
  162. resp.Detail = detail
  163. br.Ret = 200
  164. br.Success = true
  165. br.Msg = "获取成功"
  166. br.Data = resp
  167. }
  168. // @Title 收藏
  169. // @Description 收藏
  170. // @Param request body models.ArticleCollectReq true "type json string"
  171. // @Success 200 {object} models.FontsCollectResp
  172. // @router /collect [post]
  173. func (this *ArticleController) ArticleCollect() {
  174. br := new(models.BaseResponse).Init()
  175. defer func() {
  176. this.Data["json"] = br
  177. this.ServeJSON()
  178. }()
  179. user := this.User
  180. if user == nil {
  181. br.Msg = "请重新登录"
  182. br.Ret = 408
  183. return
  184. }
  185. uid := user.UserId
  186. var req models.ArticleCollectReq
  187. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  188. if err != nil {
  189. br.Msg = "参数解析异常!"
  190. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  191. return
  192. }
  193. count, err := models.GetArticleCollectCount(uid, req.ArticleId)
  194. if err != nil {
  195. br.Msg = "获取数据失败!"
  196. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  197. return
  198. }
  199. resp := new(models.ArticleCollectResp)
  200. if count <= 0 {
  201. item := new(models.CygxArticleCollect)
  202. item.ArticleId = req.ArticleId
  203. item.UserId = uid
  204. item.CreateTime = time.Now()
  205. _, err = models.AddCygxArticleCollect(item)
  206. if err != nil {
  207. br.Msg = "收藏失败"
  208. br.ErrMsg = "收藏失败,Err:" + err.Error()
  209. return
  210. }
  211. br.Msg = "收藏成功"
  212. resp.Status = 1
  213. } else {
  214. err = models.RemoveArticleCollect(uid, req.ArticleId)
  215. if err != nil {
  216. br.Msg = "取消收藏失败"
  217. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  218. return
  219. }
  220. br.Msg = "已取消收藏"
  221. resp.Status = 2
  222. }
  223. collectTotal, err := models.GetArticleCollectUsersCount(req.ArticleId)
  224. if err != nil {
  225. br.Msg = "获取数据失败"
  226. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  227. return
  228. }
  229. resp.CollectCount = collectTotal
  230. br.Ret = 200
  231. br.Success = true
  232. br.Data = resp
  233. }
  234. // @Title 访谈申请
  235. // @Description 访谈申请
  236. // @Param request body models.ArticleInterviewApplyReq true "type json string"
  237. // @Success 200 {object} models.FontsCollectResp
  238. // @router /interview/apply [post]
  239. func (this *ArticleController) InterviewApply() {
  240. br := new(models.BaseResponse).Init()
  241. defer func() {
  242. this.Data["json"] = br
  243. this.ServeJSON()
  244. }()
  245. user := this.User
  246. if user == nil {
  247. br.Msg = "请重新登录"
  248. br.Ret = 408
  249. return
  250. }
  251. uid := user.UserId
  252. var req models.ArticleInterviewApplyReq
  253. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  254. if err != nil {
  255. br.Msg = "参数解析异常!"
  256. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  257. return
  258. }
  259. article, err := models.GetArticleDetailById(req.ArticleId)
  260. if err != nil {
  261. br.Msg = "获取纪要失败!"
  262. br.ErrMsg = "获取纪要失败,Err:" + err.Error()
  263. return
  264. }
  265. count, err := models.GetArticleInterviewApplyCount(uid, req.ArticleId)
  266. if err != nil {
  267. br.Msg = "获取数据失败!"
  268. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  269. return
  270. }
  271. resp := new(models.ArticleInterviewApplyResp)
  272. if count <= 0 {
  273. item := new(models.CygxInterviewApply)
  274. item.ArticleId = req.ArticleId
  275. item.UserId = uid
  276. item.CompanyId = user.CompanyId
  277. item.Status = "待邀请"
  278. item.Sort = 1
  279. item.ArticleTitle = article.Title
  280. item.CreateTime = time.Now()
  281. item.ModifyTime = time.Now()
  282. item.ArticleIdMd5 = article.ArticleIdMd5
  283. _, err = models.AddCygxInterviewApply(item)
  284. if err != nil {
  285. br.Msg = "申请失败"
  286. br.ErrMsg = "申请失败,Err:" + err.Error()
  287. return
  288. }
  289. br.Msg = "申请成功"
  290. resp.Status = 1
  291. //发送模板消息
  292. if user.CompanyId > 1 {
  293. mobile := user.Mobile
  294. if mobile == "" {
  295. mobile = user.Email
  296. }
  297. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  298. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  299. openIpItem, _ := models.GetUserRecordByUserId(sellerItem.UserId, 1)
  300. if openIpItem != nil && openIpItem.OpenId != "" {
  301. go services.SendInterviewApplyTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
  302. }
  303. }
  304. }
  305. } else {
  306. err = models.RemoveArticleInterviewApply(uid, req.ArticleId)
  307. if err != nil {
  308. br.Msg = "取消申请失败"
  309. br.ErrMsg = "取消申请失败,Err:" + err.Error()
  310. return
  311. }
  312. br.Msg = "已取消申请"
  313. resp.Status = 2
  314. if user.CompanyId > 1 {
  315. mobile := user.Mobile
  316. if mobile == "" {
  317. mobile = user.Email
  318. }
  319. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  320. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  321. openIpItem, _ := models.GetUserRecordByUserId(sellerItem.UserId, 1)
  322. if openIpItem != nil && openIpItem.OpenId != "" {
  323. go services.SendInterviewApplyCancelTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
  324. }
  325. }
  326. }
  327. }
  328. br.Ret = 200
  329. br.Success = true
  330. br.Data = resp
  331. }
  332. // @Title 获取报告详情
  333. // @Description 获取报告详情接口
  334. // @Param ArticleIdMd5 query int true "报告ID"
  335. // @Success 200 {object} models.ArticleDetailResp
  336. // @router /look/detail [get]
  337. func (this *ArticleCommonController) Detail() {
  338. br := new(models.BaseResponse).Init()
  339. defer func() {
  340. this.Data["json"] = br
  341. this.ServeJSON()
  342. }()
  343. articleIdMd5 := this.GetString("ArticleIdMd5")
  344. if articleIdMd5 == "" {
  345. br.Msg = "参数错误"
  346. br.ErrMsg = "参数错误"
  347. return
  348. }
  349. resp := new(models.ArticleDetailResp)
  350. detail, err := models.GetArticleDetailByIdMd5(articleIdMd5)
  351. if err != nil && err.Error() != utils.ErrNoRow() {
  352. br.Msg = "获取信息失败"
  353. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  354. return
  355. }
  356. if detail == nil {
  357. resp.HasPermission = 2
  358. } else {
  359. resp.HasPermission = 1
  360. }
  361. if detail != nil {
  362. detail.Body = html.UnescapeString(detail.Body)
  363. }
  364. resp.Detail = detail
  365. br.Ret = 200
  366. br.Success = true
  367. br.Msg = "获取成功"
  368. br.Data = resp
  369. }
  370. // @Title 获取报告同步
  371. // @Description 获取报告详情接口
  372. // @Param ArticleIdMd5 query int true "报告ID"
  373. // @Success 200 {object} models.ArticleDetailResp
  374. // @router /tongbu [get]
  375. func (this *ArticleCommonController) Tongbu() {
  376. br := new(models.BaseResponse).Init()
  377. defer func() {
  378. this.Data["json"] = br
  379. this.ServeJSON()
  380. }()
  381. fmt.Println("同步数据")
  382. //indexName := utils.IndexName
  383. //endDate := time.Now().AddDate(0, 0, -7).Format(utils.FormatDate)
  384. //list, err := models.GetTacticsList(endDate)
  385. list, err := models.GetTacticsListAll2()
  386. if err != nil {
  387. fmt.Println("GetTacticsList Err:", err.Error())
  388. return
  389. }
  390. fmt.Println("list len:", len(list))
  391. for k, v := range list {
  392. //是否属于纪要库
  393. countSummary, err := models.GetPermissionMappingById(v.CategoryId)
  394. if err != nil && err.Error() != utils.ErrNoRow() {
  395. br.Msg = "参数解析异常!"
  396. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  397. return
  398. }
  399. if countSummary > 0 {
  400. v.IsSummary = 1
  401. }
  402. //是否属于报告
  403. countReport, err := models.GetReportMappingById(v.CategoryId)
  404. if err != nil && err.Error() != utils.ErrNoRow() {
  405. br.Msg = "参数解析异常!"
  406. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  407. return
  408. }
  409. if countReport > 0 {
  410. v.IsReport = 1
  411. //是否属于策略 策略自动归类
  412. if v.CategoryId == 7 || v.CategoryId == 11 || v.CategoryId == 51 || v.CategoryId == 52 {
  413. v.IsClass = 1
  414. }
  415. if v.CategoryId == 64 || v.CategoryId == 87 || v.CategoryId == 80 {
  416. v.ReportType = 2 //是否属于行业报告
  417. } else {
  418. v.ReportType = 1 //是否属于产业报告
  419. }
  420. }
  421. v.Department = "弘则权益研究"
  422. fmt.Println(k, v.ArticleId)
  423. hh, _ := time.ParseDuration("8h")
  424. //pDate := publishDate.Add(hh)
  425. v.PublishDate = v.PublishDate.Add(hh)
  426. //判断是否已经存在
  427. if v.ArticleId < 0 {
  428. if err != nil {
  429. br.Msg = "参数解析异常!"
  430. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  431. return
  432. }
  433. }
  434. count, err := models.GetArticleCountById(v.ArticleId)
  435. if err != nil && err.Error() != utils.ErrNoRow() {
  436. br.Msg = "参数解析异常!"
  437. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  438. return
  439. }
  440. v.Body = strings.Replace(v.Body, "http://vmp.hzinsights.com", "https://vmp.hzinsights.com", -1)
  441. expertNumStr, expertContentStr, interviewDateStr := services.BodyAnalysis(v.Body)
  442. if count > 0 {
  443. fmt.Println(k, v.ArticleId, "edit")
  444. bodyText, _ := services.GetReportContentTextSub(v.Body)
  445. updateParams := make(map[string]interface{})
  446. updateParams["Title"] = v.Title
  447. updateParams["TitleEn"] = v.TitleEn
  448. updateParams["UpdateFrequency"] = v.UpdateFrequency
  449. updateParams["CreateDate"] = v.CreateDate
  450. updateParams["PublishDate"] = v.PublishDate
  451. updateParams["Body"] = html.EscapeString(v.Body)
  452. updateParams["BodyText"] = bodyText
  453. updateParams["Abstract"] = html.EscapeString(v.Abstract)
  454. updateParams["CategoryName"] = v.CategoryName
  455. updateParams["SubCategoryName"] = v.SubCategoryName
  456. updateParams["CategoryId"] = v.CategoryId
  457. updateParams["PublishStatus"] = v.PublishStatus
  458. updateParams["ExpertBackground"] = expertContentStr
  459. updateParams["ExpertNumber"] = expertNumStr
  460. updateParams["InterviewDate"] = interviewDateStr
  461. updateParams["IsClass"] = v.IsClass
  462. updateParams["IsSummary"] = v.IsSummary
  463. updateParams["IsReport"] = v.IsReport
  464. updateParams["ReportType"] = v.ReportType
  465. if v.Department != "弘则权益研究" {
  466. v.Department = "弘则权益研究"
  467. }
  468. updateParams["Department"] = v.Department
  469. whereParam := map[string]interface{}{"article_id": v.ArticleId}
  470. err = models.UpdateByExpr(models.CygxArticle{}, whereParam, updateParams)
  471. if err != nil {
  472. fmt.Println("UpdateByExpr Err:" + err.Error())
  473. }
  474. } else {
  475. fmt.Println(k, v.ArticleId, "add")
  476. item := new(models.CygxArticle)
  477. articleIdInt := v.ArticleId
  478. item.ArticleId = articleIdInt
  479. item.Title = v.Title
  480. item.TitleEn = v.TitleEn
  481. item.UpdateFrequency = v.UpdateFrequency
  482. item.CreateDate = v.CreateDate
  483. item.PublishDate = v.PublishDate.Format(utils.FormatDateTime)
  484. item.Body = html.EscapeString(v.Body)
  485. item.Abstract = html.EscapeString(v.Abstract)
  486. item.CategoryName = v.CategoryName
  487. item.SubCategoryName = v.SubCategoryName
  488. item.CategoryId = v.CategoryId
  489. item.PublishStatus = v.PublishStatus
  490. item.ExpertBackground = expertContentStr
  491. item.ExpertNumber = expertNumStr
  492. item.InterviewDate = interviewDateStr
  493. item.Department = v.Department
  494. item.ArticleIdMd5 = utils.MD5(strconv.Itoa(articleIdInt))
  495. item.IsClass = v.IsClass
  496. item.IsSummary = v.IsSummary
  497. item.IsReport = v.IsReport
  498. item.ReportType = v.ReportType
  499. _, err = models.AddCygxArticles(item)
  500. if err != nil {
  501. fmt.Println("AddCygxArticle Err:", err.Error())
  502. br.Msg = "参数解析异常!"
  503. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  504. return
  505. }
  506. }
  507. //content := html.UnescapeString(v.Body)
  508. //doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
  509. //if err != nil {
  510. // fmt.Println("create doc err:", err.Error())
  511. // br.Msg = "参数解析异常!"
  512. // br.ErrMsg = "参数解析失败,Err:" + err.Error()
  513. // return
  514. //}
  515. //doc.Find("a").Each(func(i int, a *goquery.Selection) {
  516. // a.Remove()
  517. //})
  518. //bodyText := doc.Text()
  519. //item := new(ElasticTestArticleDetail)
  520. //item.ArticleId = v.ArticleId
  521. //item.Title = v.Title
  522. //item.BodyText = bodyText
  523. //item.PublishDate = v.PublishDate.Format(utils.FormatDateTime)
  524. //EsAddOrEditData(indexName, strconv.Itoa(v.ArticleId), item)
  525. }
  526. br.Ret = 200
  527. br.Success = true
  528. br.Msg = "同步成功"
  529. }