gushou_time_line.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. "hongze/hz_crm_api/models/system"
  10. cygxService "hongze/hz_crm_api/services/cygx"
  11. "hongze/hz_crm_api/utils"
  12. "os"
  13. "path/filepath"
  14. "strconv"
  15. "time"
  16. )
  17. // 固收时间线
  18. type GushouTimeLineController struct {
  19. controllers.BaseAuthController
  20. }
  21. // @Title 新增固收时间线
  22. // @Description 新增固收时间线接口
  23. // @Param request body cygx.AddGushouTimeLineReq true "type json string"
  24. // @Success 200 {object} "保存成功"
  25. // @router /gushouTimeLine/preserveAndPublish [post]
  26. func (this *GushouTimeLineController) PreserveAndPublish() {
  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.AddGushouTimeLineReq
  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. publishTime := utils.StrDateToDate(req.PublishTime) //时间字符串格式转时间格式
  47. timeLineId := req.TimeLineId
  48. link := req.Link
  49. articleId, chartId := cygxService.HandleTacticsTimeLinLink(link)
  50. item := new(cygx.CygxGushouTimeLine)
  51. item.PublishTime = publishTime
  52. item.CreateTime = time.Now()
  53. item.ModifyTime = time.Now()
  54. item.Content = req.Content
  55. item.Link = req.Link
  56. item.AdminId = sysUser.AdminId
  57. if articleId > 0 {
  58. item.ArticleId = articleId
  59. }
  60. if chartId > 0 {
  61. item.ChartId = chartId
  62. }
  63. if timeLineId == 0 {
  64. //新增
  65. err = cygx.AddCygxGushouTimeLine(item)
  66. } else {
  67. //更新
  68. item.TimeLineId = timeLineId
  69. err = cygx.UpdateCygxGushouTimeLine(item)
  70. }
  71. if err != nil {
  72. br.Msg = "保存失败"
  73. br.ErrMsg = "保存失败,Err:" + err.Error()
  74. return
  75. }
  76. br.Ret = 200
  77. br.Success = true
  78. br.IsAddLog = true
  79. br.Msg = "操作成功"
  80. }
  81. // @Title 固收时间线列表
  82. // @Description 固收时间线列表接口
  83. // @Param PageSize query int true "每页数据条数"
  84. // @Param CurrentIndex query int true "当前页页码,从1开始"
  85. // @Param Status query int false "发布状态 ,1未发布,1已发布,传2查询所有"
  86. // @Success Ret=200 {object} cygx.GetCygxGushouTimeLineResp
  87. // @router /gushouTimeLine/list [get]
  88. func (this *GushouTimeLineController) List() {
  89. br := new(models.BaseResponse).Init()
  90. defer func() {
  91. this.Data["json"] = br
  92. this.ServeJSON()
  93. }()
  94. sysUser := this.SysUser
  95. if sysUser == nil {
  96. br.Msg = "请登录"
  97. br.ErrMsg = "请登录,SysUser Is Empty"
  98. br.Ret = 408
  99. return
  100. }
  101. resp := new(cygx.GetCygxGushouTimeLineResp)
  102. pageSize, _ := this.GetInt("PageSize")
  103. currentIndex, _ := this.GetInt("CurrentIndex")
  104. status, _ := this.GetInt("Status")
  105. var startSize int
  106. if pageSize <= 0 {
  107. pageSize = utils.PageSize20
  108. }
  109. if currentIndex <= 0 {
  110. currentIndex = 1
  111. }
  112. startSize = utils.StartIndex(currentIndex, pageSize)
  113. var condition string
  114. var pars []interface{}
  115. if status == 0 || status == 1 {
  116. condition += ` AND art.status = ? `
  117. pars = append(pars, status)
  118. }
  119. total, err := cygx.GetCygxGushouTimeLineCount(condition, pars)
  120. if err != nil {
  121. br.Msg = "获取失败"
  122. br.ErrMsg = "获取失败,Err:" + err.Error()
  123. return
  124. }
  125. condition += " ORDER BY art.publish_time DESC , art.time_line_id DESC "
  126. list, err := cygx.GetCygxGushouTimeLineList(condition, pars, startSize, pageSize)
  127. if err != nil {
  128. br.Msg = "获取失败"
  129. br.ErrMsg = "获取失败,Err:" + err.Error()
  130. return
  131. }
  132. var TimeLineIds []int
  133. var ArticleIds []int
  134. var ChartIds []int
  135. for _, v := range list {
  136. v.PublishTime = utils.TimeRemoveHms(v.PublishTime)
  137. TimeLineIds = append(TimeLineIds, v.TimeLineId)
  138. if v.ArticleId > 0 {
  139. ArticleIds = append(ArticleIds, v.ArticleId)
  140. }
  141. if v.ChartId > 0 {
  142. ChartIds = append(ChartIds, v.ChartId)
  143. }
  144. }
  145. //获取关联的文章map
  146. articleTitleMap := make(map[int]string)
  147. if len(ArticleIds) > 0 {
  148. articleTitleMap = cygxService.GetArticleTitleMapByid(ArticleIds)
  149. }
  150. chartTitleMap := make(map[int]string)
  151. if len(ChartIds) > 0 {
  152. chartTitleMap = cygxService.GetChartTitleMapByid(ChartIds)
  153. }
  154. //获取pv/Uv map
  155. mapPv, mapUv := cygxService.GetCygxGushouTimeLineHistoryListMap(TimeLineIds)
  156. for _, v := range list {
  157. v.Pv = mapPv[v.TimeLineId]
  158. v.Uv = mapUv[v.TimeLineId]
  159. if v.ArticleId > 0 {
  160. v.Title = articleTitleMap[v.ArticleId]
  161. }
  162. if v.ChartId > 0 {
  163. v.Title = chartTitleMap[v.ChartId]
  164. }
  165. }
  166. detail, err := cygx.GetCygxConfigDetailByCode(utils.CYGX_GUSHOU_TIME_LINE_STATUS)
  167. if err != nil {
  168. br.Msg = "获取失败"
  169. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  170. return
  171. }
  172. resp.Status, _ = strconv.Atoi(detail.ConfigValue)
  173. page := paging.GetPaging(currentIndex, pageSize, total)
  174. resp.List = list
  175. resp.Paging = page
  176. br.Ret = 200
  177. br.Success = true
  178. br.Msg = "获取成功"
  179. br.Data = resp
  180. }
  181. // @Title 详情
  182. // @Description 获取详情接口
  183. // @Param TimeLineId query int true "时间线ID"
  184. // @Success Ret=200 {object} cygx.ActivitySpecialDetail
  185. // @router /gushouTimeLine/detail [get]
  186. func (this *GushouTimeLineController) Detail() {
  187. br := new(models.BaseResponse).Init()
  188. defer func() {
  189. this.Data["json"] = br
  190. this.ServeJSON()
  191. }()
  192. AdminUser := this.SysUser
  193. if AdminUser == nil {
  194. br.Msg = "请登录"
  195. br.ErrMsg = "请登录,用户信息为空"
  196. br.Ret = 408
  197. return
  198. }
  199. resp := new(cygx.GetCygxGushouTimeLineDetailResp)
  200. timeLineId, _ := this.GetInt("TimeLineId")
  201. if timeLineId < 1 {
  202. br.Msg = "请输入详情ID"
  203. return
  204. }
  205. detail, err := cygx.GetCygxGushouTimeLineDetail(timeLineId)
  206. if err != nil {
  207. br.Msg = "详情不存在"
  208. br.ErrMsg = "获取失败,Err:" + err.Error()
  209. return
  210. }
  211. detail.PublishTime = utils.TimeRemoveHms(detail.PublishTime)
  212. resp.Detail = detail
  213. br.Ret = 200
  214. br.Success = true
  215. br.Msg = "获取成功"
  216. br.Data = resp
  217. }
  218. // @Title 删除
  219. // @Description 获取详情接口
  220. // @Param request body cygx.GushouTimeLineTimeLineIdReq true "type json string"
  221. // @Success 200 {object} "操作成功"
  222. // @router /gushouTimeLine/delete [POST]
  223. func (this *GushouTimeLineController) Delete() {
  224. br := new(models.BaseResponse).Init()
  225. defer func() {
  226. this.Data["json"] = br
  227. this.ServeJSON()
  228. }()
  229. AdminUser := this.SysUser
  230. if AdminUser == nil {
  231. br.Msg = "请登录"
  232. br.ErrMsg = "请登录,用户信息为空"
  233. br.Ret = 408
  234. return
  235. }
  236. var req cygx.GushouTimeLineTimeLineIdReq
  237. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  238. if err != nil {
  239. br.Msg = "参数解析异常!"
  240. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  241. return
  242. }
  243. timeLineId := req.TimeLineId
  244. if timeLineId == 0 {
  245. br.Msg = "参数错误"
  246. br.ErrMsg = "参数错误,id不可为空"
  247. return
  248. }
  249. err = cygx.DeleteCygxGushouTimeLine(timeLineId)
  250. if err != nil {
  251. br.Msg = "详情不存在"
  252. br.ErrMsg = "获取失败,Err:" + err.Error()
  253. return
  254. }
  255. br.Ret = 200
  256. br.Success = true
  257. br.IsAddLog = true
  258. br.Msg = "删除成功"
  259. }
  260. // @Title 发布/取消发布报告
  261. // @Description 发布/取消发布报告接口
  262. // @Param request body cygx.GushouTimeLineTimeLineIdReq true "type json string"
  263. // @Success 200 Ret=200 发布成功
  264. // @router /gushouTimeLine/publishAndcancel [post]
  265. func (this *GushouTimeLineController) PublishReport() {
  266. br := new(models.BaseResponse).Init()
  267. defer func() {
  268. this.Data["json"] = br
  269. this.ServeJSON()
  270. }()
  271. var req cygx.GushouTimeLineTimeLineIdReq
  272. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  273. if err != nil {
  274. br.Msg = "参数解析异常!"
  275. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  276. return
  277. }
  278. timeLineId := req.TimeLineId
  279. if timeLineId == 0 {
  280. br.Msg = "参数错误"
  281. br.ErrMsg = "参数错误,id不可为空"
  282. return
  283. }
  284. detail, err := cygx.GetCygxGushouTimeLineDetail(timeLineId)
  285. if err != nil {
  286. br.Msg = "详情不存在"
  287. br.ErrMsg = "获取失败,Err:" + err.Error()
  288. return
  289. }
  290. var status int
  291. if detail.Status == 0 {
  292. status = 1
  293. } else {
  294. status = 0
  295. }
  296. err = cygx.EditCygxGushouTimeLineStatus(status, timeLineId)
  297. if err != nil {
  298. br.Msg = "操作失败"
  299. br.ErrMsg = "获取失败,Err:" + err.Error()
  300. return
  301. }
  302. br.Ret = 200
  303. br.Success = true
  304. br.IsAddLog = true
  305. br.Msg = "操作成功"
  306. }
  307. // @Title 一键发布/取消发布报告接口
  308. // @Description 一键发布/取消发布报告
  309. // @Success 200 Ret=200 取消发布成功
  310. // @router /gushouTimeLine/all/publishAndcancel [post]
  311. func (this *GushouTimeLineController) PublishCancleReport() {
  312. br := new(models.BaseResponse).Init()
  313. defer func() {
  314. this.Data["json"] = br
  315. this.ServeJSON()
  316. }()
  317. detail, err := cygx.GetCygxConfigDetailByCode(utils.CYGX_GUSHOU_TIME_LINE_STATUS)
  318. if err != nil {
  319. br.Msg = "获取失败"
  320. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  321. return
  322. }
  323. var status int
  324. if detail.ConfigValue == "0" {
  325. status = 1
  326. } else {
  327. status = 0
  328. }
  329. err = cygx.EditCygxGushouTimeLineStatusAll(status)
  330. if err != nil {
  331. br.Msg = "修改失败"
  332. br.ErrMsg = "修改失败,Err:" + err.Error()
  333. return
  334. }
  335. br.Ret = 200
  336. br.Success = true
  337. br.IsAddLog = true
  338. br.Msg = "操作成功"
  339. }
  340. // @Title 下载PV
  341. // @Description 下载PV接口
  342. // @Param TimeLineId query int true "时间线ID"
  343. // @router /gushouTimeLine/PvExport [get]
  344. func (this *GushouTimeLineController) PvExport() {
  345. br := new(models.BaseResponse).Init()
  346. defer func() {
  347. this.Data["json"] = br
  348. this.ServeJSON()
  349. }()
  350. AdminUser := this.SysUser
  351. if AdminUser == nil {
  352. br.Msg = "请登录"
  353. br.ErrMsg = "请登录,用户信息为空"
  354. br.Ret = 408
  355. return
  356. }
  357. timeLineId, _ := this.GetInt("TimeLineId")
  358. if timeLineId < 1 {
  359. br.Msg = "请输入详情ID"
  360. return
  361. }
  362. var condition string
  363. var pars []interface{}
  364. condition = ` AND time_line_id = ? `
  365. pars = append(pars, timeLineId)
  366. var respList []*cygx.CygxGushouTimeLineHistory
  367. //respList := new(cygx.CygxGushouTimeLineHistory)
  368. list, err := cygx.GetCygxGushouTimeLineHistoryList(condition, pars)
  369. if err != nil {
  370. br.Msg = "获取失败"
  371. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  372. return
  373. }
  374. //超级管理员和权益管理员、权益研究员可以下载所有客户,销售组长能下载本组客户,销售只能下载本人名下客户
  375. resp := new(cygx.CanDownload)
  376. adminInfo, errAdmin := system.GetSysUserById(AdminUser.AdminId)
  377. if errAdmin != nil {
  378. br.Msg = "获取失败"
  379. br.ErrMsg = "获取失败,Err:" + errAdmin.Error()
  380. return
  381. }
  382. if adminInfo.Role == "admin" || adminInfo.Role == "researcher" {
  383. resp.IsCanDownload = true
  384. }
  385. //销售查看自己客户,销售组长查看组员
  386. if resp.IsCanDownload == false {
  387. mapMobile, err := cygxService.GetAdminLookUserMobile(adminInfo)
  388. if err != nil {
  389. br.Msg = "获取失败"
  390. br.ErrMsg = "获取失败,销售对应权限,Err:" + err.Error()
  391. return
  392. }
  393. for _, v := range list {
  394. if _, ok := mapMobile[v.Mobile]; ok {
  395. respList = append(respList, v)
  396. }
  397. }
  398. } else {
  399. respList = list
  400. }
  401. //创建excel
  402. dir, err := os.Executable()
  403. exPath := filepath.Dir(dir)
  404. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  405. xlsxFile := xlsx.NewFile()
  406. if err != nil {
  407. br.Msg = "生成文件失败"
  408. br.ErrMsg = "生成文件失败"
  409. return
  410. }
  411. style := xlsx.NewStyle()
  412. alignment := xlsx.Alignment{
  413. Horizontal: "center",
  414. Vertical: "center",
  415. WrapText: true,
  416. }
  417. style.Alignment = alignment
  418. style.ApplyAlignment = true
  419. sheet, err := xlsxFile.AddSheet("阅读明细")
  420. if err != nil {
  421. br.Msg = "新增Sheet失败"
  422. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  423. return
  424. }
  425. rowTitle := sheet.AddRow()
  426. cellA := rowTitle.AddCell()
  427. cellA.Value = "姓名"
  428. cellB := rowTitle.AddCell()
  429. cellB.Value = "手机号"
  430. cellC := rowTitle.AddCell()
  431. cellC.Value = "公司名称"
  432. cellD := rowTitle.AddCell()
  433. cellD.Value = "所属权益销售"
  434. cellE := rowTitle.AddCell()
  435. cellE.Value = "点击时间"
  436. for _, item := range respList {
  437. row := sheet.AddRow()
  438. cellA := row.AddCell()
  439. cellA.Value = item.RealName
  440. cellB := row.AddCell()
  441. cellB.Value = item.Mobile
  442. cellC := row.AddCell()
  443. cellC.Value = item.CompanyName
  444. cellD := row.AddCell()
  445. cellD.Value = item.SellerName
  446. cellE := row.AddCell()
  447. cellE.Value = item.CreateTime.Format(utils.FormatDateTime)
  448. }
  449. err = xlsxFile.Save(downLoadnFilePath)
  450. if err != nil {
  451. br.Msg = "保存文件失败"
  452. br.ErrMsg = "保存文件失败"
  453. return
  454. }
  455. downloadFileName := time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  456. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  457. defer func() {
  458. os.Remove(downLoadnFilePath)
  459. }()
  460. br.Ret = 200
  461. br.Success = true
  462. br.Msg = "获取成功"
  463. }