yanxuan_special.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. package cygx
  2. import (
  3. "encoding/json"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "github.com/tealeg/xlsx"
  6. "hongze/hz_crm_api/controllers"
  7. "hongze/hz_crm_api/models"
  8. "hongze/hz_crm_api/models/cygx"
  9. cygxService "hongze/hz_crm_api/services/cygx"
  10. "hongze/hz_crm_api/services/elastic"
  11. "hongze/hz_crm_api/utils"
  12. "os"
  13. "path/filepath"
  14. "strconv"
  15. "time"
  16. )
  17. // YanxuanSpecialController 研选专栏
  18. type YanxuanSpecialController struct {
  19. controllers.BaseAuthController
  20. }
  21. // @Title 新增研选专栏作者
  22. // @Description 新增研选专栏作者
  23. // @Param request body help_doc.AddHelpDocReq true "type json string"
  24. // @Success 200 {object} models.AddEnglishReportResp
  25. // @router /yanxuan_special/author/add [post]
  26. func (this *YanxuanSpecialController) Add() {
  27. br := new(models.BaseResponse).Init()
  28. defer func() {
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. sysUser := this.SysUser
  33. if sysUser == nil {
  34. br.Msg = "请登录"
  35. br.ErrMsg = "请登录,SysUser Is Empty"
  36. br.Ret = 408
  37. return
  38. }
  39. var req cygx.AddCygxYanxuanSpecialAuthorReq
  40. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  41. if err != nil {
  42. br.Msg = "参数解析异常!"
  43. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  44. return
  45. }
  46. if req.UserId <= 0 {
  47. br.Msg = "请输入用户信息"
  48. return
  49. }
  50. if req.RealName == "" {
  51. br.Msg = "请输入真实姓名"
  52. return
  53. }
  54. if req.Mobile == "" {
  55. br.Msg = "请输入手机号"
  56. return
  57. }
  58. //获取关联公司的用户信息
  59. infoUser, err := cygx.GetUserAndCompanyNameList(req.UserId)
  60. if err != nil {
  61. br.Msg = "获取失败"
  62. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  63. return
  64. }
  65. rnd := utils.GetRandInt(1, 5)
  66. item := cygx.CygxYanxuanSpecialAuthor{
  67. UserId: req.UserId,
  68. RealName: req.RealName,
  69. Mobile: req.Mobile,
  70. CompanyName: infoUser.CompanyName,
  71. CompanyId: infoUser.CompanyId,
  72. CreateTime: time.Now(),
  73. ModifyTime: time.Now(),
  74. HeadImg: utils.CYGX_YANXUAN_SPECIAL_HEAD_IMG_URL + strconv.Itoa(rnd) + ".png",
  75. BgImg: utils.CYGX_YANXUAN_SPECIAL_BG_IMG_URL + strconv.Itoa(rnd) + "a.png",
  76. BgImgDown: utils.CYGX_YANXUAN_SPECIAL_BG_IMG_URL + strconv.Itoa(rnd) + "b.png",
  77. BgImgPc: utils.CYGX_YANXUAN_SPECIAL_BG_IMG_URL_PC + strconv.Itoa(rnd) + ".png",
  78. Status: 1,
  79. }
  80. _, err = cygx.AddCygxYanxuanSpecialAuthor(&item)
  81. if err != nil {
  82. br.Msg = "新增失败"
  83. br.ErrMsg = "新增失败,Err:" + err.Error()
  84. return
  85. }
  86. br.Ret = 200
  87. br.Success = true
  88. br.Msg = "新增成功"
  89. }
  90. // @Title 禁用/启用研选专栏作者
  91. // @Description 禁用/启用研选专栏作者
  92. // @Param request body help_doc.AddHelpDocReq true "type json string"
  93. // @Success 200 {object} models.AddEnglishReportResp
  94. // @router /yanxuan_special/author/enable [post]
  95. func (this *YanxuanSpecialController) AuthorEnable() {
  96. br := new(models.BaseResponse).Init()
  97. defer func() {
  98. this.Data["json"] = br
  99. this.ServeJSON()
  100. }()
  101. sysUser := this.SysUser
  102. if sysUser == nil {
  103. br.Msg = "请登录"
  104. br.ErrMsg = "请登录,SysUser Is Empty"
  105. br.Ret = 408
  106. return
  107. }
  108. var req cygx.EnableCygxYanxuanSpecialAuthorReq
  109. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  110. if err != nil {
  111. br.Msg = "参数解析异常!"
  112. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  113. return
  114. }
  115. if req.UserId <= 0 {
  116. br.Msg = "用户id错误"
  117. return
  118. }
  119. if req.Status <= 0 {
  120. br.Msg = "参数错误"
  121. return
  122. }
  123. if tmpErr := cygx.EnableYanxuanSpecialAuthor(req.UserId, req.Status); tmpErr != nil {
  124. br.Msg = "启用/禁用作者失败"
  125. br.ErrMsg = "启用/禁用作者失败, Err:" + tmpErr.Error()
  126. return
  127. }
  128. if req.Status == 1 {
  129. br.Msg = "启用成功"
  130. } else {
  131. br.Msg = "禁用成功"
  132. }
  133. br.Ret = 200
  134. br.Success = true
  135. }
  136. // @Title 作者列表
  137. // @Description 作者列表
  138. // @Param request body help_doc.AddHelpDocReq true "type json string"
  139. // @Param PageSize query int true "每页数据条数"
  140. // @Param CurrentIndex query int true "当前页页码,从1开始"
  141. // @Param Status query string true "状态: 1:启用 、 2:禁用 。传其他默认所有"
  142. // @Param KeyWord query string false "搜索关键词"
  143. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'CreatTime':开通时间 、 'articleNum':已发布文章 、 'pv':总Pv/Uv "
  144. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  145. // @Success 200 {object} models.AddEnglishReportResp
  146. // @router /yanxuan_special/author/list [get]
  147. func (this *YanxuanSpecialController) AuthorList() {
  148. br := new(models.BaseResponse).Init()
  149. defer func() {
  150. this.Data["json"] = br
  151. this.ServeJSON()
  152. }()
  153. sysUser := this.SysUser
  154. if sysUser == nil {
  155. br.Msg = "请登录"
  156. br.ErrMsg = "请登录,SysUser Is Empty"
  157. br.Ret = 408
  158. return
  159. }
  160. pageSize, _ := this.GetInt("PageSize")
  161. currentIndex, _ := this.GetInt("CurrentIndex")
  162. status, _ := this.GetInt("Status")
  163. keyWord := this.GetString("KeyWord")
  164. //排序参数
  165. sortParam := this.GetString("SortParam")
  166. sortType := this.GetString("SortType")
  167. var startSize int
  168. if pageSize <= 0 {
  169. pageSize = utils.PageSize20
  170. }
  171. if currentIndex <= 0 {
  172. currentIndex = 1
  173. }
  174. startSize = utils.StartIndex(currentIndex, pageSize)
  175. var condition string
  176. var pars []interface{}
  177. //作者状态处理
  178. if status == 1 || status == 2 {
  179. condition += ` AND art.status = ? `
  180. pars = append(pars, status)
  181. }
  182. //关键词搜索
  183. if keyWord != "" {
  184. keyWord = "%" + keyWord + "%"
  185. condition += ` AND art.special_name like ? `
  186. pars = append(pars, keyWord)
  187. }
  188. //排序字段以及排序方式处理
  189. var sortStr string
  190. if sortParam != "" && sortType != "" {
  191. if sortParam == "CreatTime" {
  192. if sortType == "asc" {
  193. sortStr = " ORDER BY art.create_time ASC "
  194. } else {
  195. sortStr = " ORDER BY art.create_time DESC "
  196. }
  197. }
  198. if sortParam == "articleNum" {
  199. if sortType == "asc" {
  200. sortStr = " ORDER BY art.article_num ASC "
  201. } else {
  202. sortStr = " ORDER BY art.article_num DESC "
  203. }
  204. }
  205. if sortParam == "Pv" {
  206. if sortType == "asc" {
  207. sortStr = " ORDER BY art.pv ASC "
  208. } else {
  209. sortStr = " ORDER BY art.pv DESC "
  210. }
  211. }
  212. } else {
  213. sortStr = " ORDER BY art.create_time DESC "
  214. }
  215. total, err := cygx.GetYanxuanSpecialAuthorCount(condition, pars)
  216. if err != nil {
  217. br.Msg = "获取失败"
  218. br.ErrMsg = "获取失败,Err:" + err.Error()
  219. return
  220. }
  221. list, tmpErr := cygx.GetYanxuanSpecialAuthorList(condition+sortStr, pars, startSize, pageSize)
  222. if tmpErr != nil {
  223. br.Msg = "获取失败"
  224. br.ErrMsg = "获取失败, Err:" + tmpErr.Error()
  225. return
  226. }
  227. resp := new(cygx.GetCygxYanxuanSpecialAuthorItemResp)
  228. page := paging.GetPaging(currentIndex, pageSize, total)
  229. resp.List = list
  230. resp.Paging = page
  231. br.Data = resp
  232. br.Ret = 200
  233. br.Success = true
  234. br.Msg = "获取成功"
  235. }
  236. // @Title 审核列表
  237. // @Description 审核列表
  238. // @Param PageSize query int true "每页数据条数"
  239. // @Param CurrentIndex query int true "当前页页码,从1开始"
  240. // @Param Status query string true "状态: 1:待审核 、 2:已发布 。默认待审核"
  241. // @Param Type query int true "文章类型,1:笔记、2:观点"
  242. // @Param KeyWord query string false "搜索关键词"
  243. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'CreatTime':开通时间 、 'articleNum':已发布文章 、 'pv':总Pv/Uv "
  244. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  245. // @Param StartDate query string false "开始时间 ,列如2021-03-06 "
  246. // @Param EndDate query string false "结束时间,列如2021-03-06 "
  247. // @Success 200 {object} models.AddEnglishReportResp
  248. // @router /yanxuan_special/list [get]
  249. func (this *YanxuanSpecialController) List() {
  250. br := new(models.BaseResponse).Init()
  251. defer func() {
  252. this.Data["json"] = br
  253. this.ServeJSON()
  254. }()
  255. sysUser := this.SysUser
  256. if sysUser == nil {
  257. br.Msg = "请登录"
  258. br.ErrMsg = "请登录,SysUser Is Empty"
  259. br.Ret = 408
  260. return
  261. }
  262. userId, _ := this.GetInt("UserId", 0)
  263. pageSize, _ := this.GetInt("PageSize")
  264. currentIndex, _ := this.GetInt("CurrentIndex")
  265. status, _ := this.GetInt("Status", 1)
  266. specialType, _ := this.GetInt("Type", 1)
  267. keyWord := this.GetString("KeyWord")
  268. //排序参数
  269. sortParam := this.GetString("SortParam")
  270. sortType := this.GetString("SortType")
  271. startDate := this.GetString("StartDate")
  272. endDate := this.GetString("EndDate")
  273. var startSize int
  274. if pageSize <= 0 {
  275. pageSize = utils.PageSize20
  276. }
  277. if currentIndex <= 0 {
  278. currentIndex = 1
  279. }
  280. startSize = utils.StartIndex(currentIndex, pageSize)
  281. var condition string
  282. var pars []interface{}
  283. if userId > 0 {
  284. condition += ` AND a.user_id = ? `
  285. pars = append(pars, userId)
  286. }
  287. if status == 1 {
  288. condition += ` AND a.status = 2 `
  289. }
  290. if status == 2 {
  291. condition += ` AND a.status = 3 `
  292. }
  293. if startDate != "" {
  294. condition += ` AND a.publish_time >= ` + "'" + startDate + " 00:00:00'"
  295. }
  296. if endDate != "" {
  297. condition += ` AND a.publish_time <= ` + "'" + endDate + " 23:59:59'"
  298. }
  299. //文章类型
  300. if specialType == 1 || specialType == 2 {
  301. condition += ` AND a.type = ? `
  302. pars = append(pars, specialType)
  303. }
  304. //关键词搜索
  305. if keyWord != "" {
  306. keyWord = "%" + keyWord + "%"
  307. condition += ` AND a.title like ? `
  308. pars = append(pars, keyWord)
  309. }
  310. //排序字段以及排序方式处理
  311. var sortStr string
  312. if sortParam != "" && sortType != "" {
  313. if sortParam == "Pv" {
  314. if sortType == "asc" {
  315. sortStr = " ORDER BY a.pv ASC "
  316. } else {
  317. sortStr = " ORDER BY a.pv DESC "
  318. }
  319. }
  320. } else {
  321. sortStr = " ORDER BY a.publish_time DESC "
  322. }
  323. total, err := cygx.GetGetYanxuanSpecialCount(condition, pars)
  324. if err != nil {
  325. br.Msg = "获取失败"
  326. br.ErrMsg = "获取失败,Err:" + err.Error()
  327. return
  328. }
  329. list, tmpErr := cygx.GetYanxuanSpecialList(condition+sortStr, pars, startSize, pageSize)
  330. if tmpErr != nil {
  331. br.Msg = "获取失败"
  332. br.ErrMsg = "获取失败, Err:" + tmpErr.Error()
  333. return
  334. }
  335. for _, v := range list {
  336. hasImg, err := utils.ArticleHasImgUrl(v.Content)
  337. if err != nil {
  338. return
  339. }
  340. if hasImg {
  341. v.ContentHasImg = 1
  342. }
  343. if v.DocUrl != "" {
  344. var docs []cygx.Doc
  345. err := json.Unmarshal([]byte(v.DocUrl), &docs)
  346. if err != nil {
  347. br.Msg = "参数解析异常!"
  348. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  349. return
  350. }
  351. v.Docs = docs
  352. }
  353. if v.CompanyTags != "" {
  354. v.Tags += v.CompanyTags
  355. }
  356. if v.IndustryTags != "" {
  357. if v.Tags != "" {
  358. v.Tags += ","
  359. }
  360. v.Tags += v.IndustryTags
  361. }
  362. }
  363. resp := new(cygx.GetCygxYanxuanSpeciaResplItemResp)
  364. page := paging.GetPaging(currentIndex, pageSize, total)
  365. resp.List = list
  366. resp.Paging = page
  367. br.Data = resp
  368. br.Ret = 200
  369. br.Success = true
  370. br.Msg = "获取成功"
  371. }
  372. // @Title 审批研选专栏
  373. // @Description 审批研选专栏
  374. // @Param request body help_doc.AddHelpDocReq true "type json string"
  375. // @Success 200 {object} models.AddEnglishReportResp
  376. // @router /yanxuan_special/enable [post]
  377. func (this *YanxuanSpecialController) Enable() {
  378. br := new(models.BaseResponse).Init()
  379. defer func() {
  380. this.Data["json"] = br
  381. this.ServeJSON()
  382. }()
  383. sysUser := this.SysUser
  384. if sysUser == nil {
  385. br.Msg = "请登录"
  386. br.ErrMsg = "请登录,SysUser Is Empty"
  387. br.Ret = 408
  388. return
  389. }
  390. var req cygx.EnableCygxYanxuanSpecialReq
  391. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  392. if err != nil {
  393. br.Msg = "参数解析异常!"
  394. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  395. return
  396. }
  397. if req.Id <= 0 {
  398. br.Msg = "文章id错误"
  399. return
  400. }
  401. if req.Status <= 0 {
  402. br.Msg = "参数错误"
  403. return
  404. }
  405. status := 0
  406. if req.Status == 1 {
  407. status = 3
  408. } else {
  409. status = 4
  410. }
  411. if tmpErr := cygx.EnableYanxuanSpecial(req.Id, status, req.Reason); tmpErr != nil {
  412. br.Msg = "审批失败"
  413. br.ErrMsg = "审批失败, Err:" + tmpErr.Error()
  414. return
  415. }
  416. if req.Status == 1 {
  417. go cygxService.SendWxMsgSpecialFollow(req.Id)
  418. }
  419. go cygxService.SendWxMsgSpecialAuthor(req.Id, req.Status) // 研选专栏审核完成时,给提交人发送模板消息
  420. go cygxService.UpdateYanxuanSpecialResourceData(req.Id) // 写入首页最新 cygx_resource_data 表
  421. go elastic.EsAddYanxuanSpecial(req.Id) // 写入es 综合搜索
  422. br.Msg = "审批成功"
  423. br.Ret = 200
  424. br.Success = true
  425. }
  426. // @Title 收藏详情
  427. // @Description 收藏详情
  428. // @Param SpecialId query int true "每页数据条数"
  429. // @Success 200 {object} models.AddEnglishReportResp
  430. // @router /yanxuan_special/special_collect/list [get]
  431. func (this *YanxuanSpecialController) SpecialCollectList() {
  432. br := new(models.BaseResponse).Init()
  433. defer func() {
  434. this.Data["json"] = br
  435. this.ServeJSON()
  436. }()
  437. sysUser := this.SysUser
  438. if sysUser == nil {
  439. br.Msg = "请登录"
  440. br.ErrMsg = "请登录,SysUser Is Empty"
  441. br.Ret = 408
  442. return
  443. }
  444. specialId, _ := this.GetInt("SpecialId")
  445. var condition string
  446. var pars []interface{}
  447. condition += " AND yanxuan_special_id = ? ORDER BY art.create_time DESC "
  448. pars = append(pars, specialId)
  449. list, err := cygx.GetCygxYanxuanSpecialCollectList(condition, pars, 0, 100000)
  450. if err != nil {
  451. br.Msg = "获取失败"
  452. br.ErrMsg = "获取失败,Err:" + err.Error()
  453. return
  454. }
  455. resp := new(cygx.GetCygxYanxuanSpecialCollectResp)
  456. resp.List = list
  457. br.Data = resp
  458. br.Ret = 200
  459. br.Success = true
  460. br.Msg = "获取成功"
  461. }
  462. // @Title 下载PV
  463. // @Description 下载PV接口
  464. // @Param SpecialId query int true "每页数据条数"
  465. // @router /yanxuan_special/list_pv [get]
  466. func (this *YanxuanSpecialController) ListPv() {
  467. br := new(models.BaseResponse).Init()
  468. defer func() {
  469. this.Data["json"] = br
  470. this.ServeJSON()
  471. }()
  472. AdminUser := this.SysUser
  473. if AdminUser == nil {
  474. br.Msg = "请登录"
  475. br.ErrMsg = "请登录,用户信息为空"
  476. br.Ret = 408
  477. return
  478. }
  479. specialId, _ := this.GetInt("SpecialId")
  480. if specialId < 1 {
  481. br.Msg = "请输入专栏ID"
  482. return
  483. }
  484. var condition string
  485. var pars []interface{}
  486. condition = ` AND yanxuan_special_id = ? ORDER BY create_time DESC `
  487. pars = append(pars, specialId)
  488. list, err := cygx.GetCygxYanxuanSpecialRecordList(condition, pars)
  489. if err != nil {
  490. br.Msg = "获取失败"
  491. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  492. return
  493. }
  494. //创建excel
  495. dir, err := os.Executable()
  496. exPath := filepath.Dir(dir)
  497. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  498. xlsxFile := xlsx.NewFile()
  499. if err != nil {
  500. br.Msg = "生成文件失败"
  501. br.ErrMsg = "生成文件失败"
  502. return
  503. }
  504. style := xlsx.NewStyle()
  505. alignment := xlsx.Alignment{
  506. Horizontal: "center",
  507. Vertical: "center",
  508. WrapText: true,
  509. }
  510. style.Alignment = alignment
  511. style.ApplyAlignment = true
  512. sheet, err := xlsxFile.AddSheet("阅读明细")
  513. if err != nil {
  514. br.Msg = "新增Sheet失败"
  515. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  516. return
  517. }
  518. rowTitle := sheet.AddRow()
  519. cellA := rowTitle.AddCell()
  520. cellA.Value = "姓名"
  521. cellB := rowTitle.AddCell()
  522. cellB.Value = "公司名称"
  523. cellC := rowTitle.AddCell()
  524. cellC.Value = "所属权益销售"
  525. cellD := rowTitle.AddCell()
  526. cellD.Value = "阅读时间"
  527. cellE := rowTitle.AddCell()
  528. cellE.Value = "阅读时长"
  529. for _, item := range list {
  530. row := sheet.AddRow()
  531. cellA := row.AddCell()
  532. cellA.Value = item.RealName
  533. cellB := row.AddCell()
  534. cellB.Value = item.CompanyName
  535. cellC := row.AddCell()
  536. cellC.Value = item.SellerName
  537. cellD := row.AddCell()
  538. cellD.Value = item.CreateTime
  539. cellE := row.AddCell()
  540. cellE.Value = strconv.Itoa(item.StopTime)
  541. }
  542. err = xlsxFile.Save(downLoadnFilePath)
  543. if err != nil {
  544. br.Msg = "保存文件失败"
  545. br.ErrMsg = "保存文件失败"
  546. return
  547. }
  548. downloadFileName := time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  549. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  550. defer func() {
  551. os.Remove(downLoadnFilePath)
  552. }()
  553. br.Ret = 200
  554. br.Success = true
  555. br.Msg = "导出成功"
  556. }