banner.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. "time"
  15. )
  16. // banner
  17. type BannerCoAntroller struct {
  18. controllers.BaseAuthController
  19. }
  20. // @Title 图片选择列表
  21. // @Description 图片选择列表接口
  22. // @Param PageSize query int true "每页数据条数"
  23. // @Param CurrentIndex query int true "当前页页码,从1开始"
  24. // @Success 200 {object} cygx.CygxBannerImgListResp
  25. // @router /banner/img/list [get]
  26. func (this *BannerCoAntroller) ImgList() {
  27. br := new(models.BaseResponse).Init()
  28. defer func() {
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. AdminUser := this.SysUser
  33. if AdminUser == nil {
  34. br.Msg = "请登录"
  35. br.ErrMsg = "请登录,SysUser Is Empty"
  36. return
  37. }
  38. resp := new(cygx.CygxBannerImgListResp)
  39. pageSize, _ := this.GetInt("PageSize")
  40. currentIndex, _ := this.GetInt("CurrentIndex")
  41. var startSize int
  42. if pageSize <= 0 {
  43. pageSize = utils.PageSize20
  44. }
  45. if currentIndex <= 0 {
  46. currentIndex = 1
  47. }
  48. startSize = utils.StartIndex(currentIndex, pageSize)
  49. var condition string
  50. var pars []interface{}
  51. total, err := cygx.GetCygxBannerImgCount(condition, pars)
  52. if err != nil {
  53. br.Msg = "获取失败"
  54. br.ErrMsg = "获取失败,Err:" + err.Error()
  55. return
  56. }
  57. condition += " ORDER BY sort DESC "
  58. list, err := cygx.GetCygxBannerImgList(condition, pars, startSize, pageSize)
  59. if err != nil {
  60. br.Msg = "获取失败"
  61. br.ErrMsg = "获取失败,Err:" + err.Error()
  62. return
  63. }
  64. page := paging.GetPaging(currentIndex, pageSize, total)
  65. resp.List = list
  66. resp.Paging = page
  67. br.Ret = 200
  68. br.Success = true
  69. br.Msg = "获取成功"
  70. br.Data = resp
  71. }
  72. // @Title 列表
  73. // @Description 列表接口
  74. // @Param PageSize query int true "每页数据条数"
  75. // @Param CurrentIndex query int true "当前页页码,从1开始"
  76. // @Param Status query int true "发布状态 -1全部,1;已发布,0:未发布 默认-1"
  77. // @Param ListType query int true "ABC哪一列,默认A"
  78. // @Success 200 {object} cygx.CygxBannerImgListResp
  79. // @router /banner/list [get]
  80. func (this *BannerCoAntroller) List() {
  81. br := new(models.BaseResponse).Init()
  82. defer func() {
  83. this.Data["json"] = br
  84. this.ServeJSON()
  85. }()
  86. AdminUser := this.SysUser
  87. if AdminUser == nil {
  88. br.Msg = "请登录"
  89. br.ErrMsg = "请登录,SysUser Is Empty"
  90. return
  91. }
  92. resp := new(cygx.CygxBannerListResp)
  93. pageSize, _ := this.GetInt("PageSize")
  94. currentIndex, _ := this.GetInt("CurrentIndex")
  95. status, _ := this.GetInt("Status", -1)
  96. listType := this.GetString("ListType", "A")
  97. var startSize int
  98. if pageSize <= 0 {
  99. pageSize = utils.PageSize20
  100. }
  101. if currentIndex <= 0 {
  102. currentIndex = 1
  103. }
  104. startSize = utils.StartIndex(currentIndex, pageSize)
  105. var condition string
  106. var pars []interface{}
  107. if status == 0 || status == 1 {
  108. condition += ` AND art.status = ? `
  109. pars = append(pars, status)
  110. }
  111. condition += ` AND art.list_type = ? `
  112. pars = append(pars, listType)
  113. total, err := cygx.GetCygxBannerCount(condition, pars)
  114. if err != nil {
  115. br.Msg = "获取失败"
  116. br.ErrMsg = "获取失败,Err:" + err.Error()
  117. return
  118. }
  119. condition += " ORDER BY art.sort ASC , art.modify_time DESC "
  120. list, err := cygx.GetCygxBannerList(condition, pars, startSize, pageSize)
  121. if err != nil {
  122. br.Msg = "获取失败"
  123. br.ErrMsg = "获取失败,Err:" + err.Error()
  124. return
  125. }
  126. page := paging.GetPaging(currentIndex, pageSize, total)
  127. resp.List = list
  128. resp.Paging = page
  129. br.Ret = 200
  130. br.Success = true
  131. br.Msg = "获取成功"
  132. br.Data = resp
  133. }
  134. // @Title 添加修改banner
  135. // @Description 添加修改banner接口
  136. // @Param request body cygx.AddTacticsTimeLineReq true "type json string"
  137. // @Success 200 {object} "保存成功"
  138. // @router /banner/PreserveAndPublish [post]
  139. func (this *BannerCoAntroller) PreserveAndPublish() {
  140. br := new(models.BaseResponse).Init()
  141. defer func() {
  142. this.Data["json"] = br
  143. this.ServeJSON()
  144. }()
  145. AdminUser := this.SysUser
  146. if AdminUser == nil {
  147. br.Msg = "请登录"
  148. br.ErrMsg = "请登录,SysUser Is Empty"
  149. br.Ret = 408
  150. return
  151. }
  152. var req cygx.CygxBannerReq
  153. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  154. if err != nil {
  155. br.Msg = "参数解析异常!"
  156. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  157. return
  158. }
  159. bannerId := req.BannerId
  160. //var condition string
  161. //var pars []interface{}
  162. //
  163. //condition += ` AND art.status = 1 `
  164. //condition += ` AND art.list_type = ? AND art.sort = ?`
  165. //pars = append(pars, req.ListType, req.Sort)
  166. //if bannerId > 0 {
  167. // condition += ` AND art.banner_id != ? `
  168. // pars = append(pars, bannerId)
  169. //}
  170. //
  171. //total, err := cygx.GetCygxBannerCount(condition, pars)
  172. //if err != nil {
  173. // br.Msg = "获取失败"
  174. // br.ErrMsg = "获取失败,Err:" + err.Error()
  175. // return
  176. //}
  177. //if total > 0 {
  178. // br.Msg = "序号与已发布的已有序号不可重复!"
  179. // return
  180. //}
  181. item := new(cygx.CygxBanner)
  182. item.ImgId = req.ImgId
  183. item.ListType = req.ListType
  184. item.BannerType = req.BannerType
  185. item.Title = req.Title
  186. item.Subtitle = req.Subtitle
  187. item.Link = req.Link
  188. item.AdminId = AdminUser.AdminId
  189. item.Status = 1
  190. item.CreateTime = time.Now()
  191. item.ModifyTime = time.Now()
  192. //新的默认排序排到最后
  193. detailByListTypeMax, err := cygx.GetCygxBannerDetailByListTypeMaxSort(req.ListType)
  194. if err != nil && err.Error() != utils.ErrNoRow() {
  195. br.Msg = "保存失败"
  196. br.ErrMsg = " 获取某一列,最大的排序值失败,Err:" + err.Error()
  197. return
  198. }
  199. if detailByListTypeMax != nil {
  200. item.Sort = detailByListTypeMax.Sort + 10
  201. } else {
  202. item.Sort = 10
  203. }
  204. if bannerId == 0 {
  205. //新增
  206. err = cygx.AddCygxBanner(item)
  207. } else {
  208. //更新
  209. detail, err := cygx.GetCygxBannerDetail(bannerId)
  210. if err != nil {
  211. br.Msg = "详情不存在"
  212. br.ErrMsg = "获取失败,Err:" + err.Error()
  213. return
  214. }
  215. if detail.Status == 1 {
  216. item.Sort = detail.Sort
  217. }
  218. item.BannerId = bannerId
  219. err = cygx.UpdateCygxBanner(item)
  220. }
  221. if err != nil {
  222. br.Msg = "保存失败"
  223. br.ErrMsg = "保存失败,Err:" + err.Error()
  224. return
  225. }
  226. br.Ret = 200
  227. br.Success = true
  228. br.IsAddLog = true
  229. br.Msg = "操作成功"
  230. }
  231. // @Title 详情
  232. // @Description 获取详情接口
  233. // @Param BannerId query int true "BannerId"
  234. // @Success Ret=200 {object} cygx.ActivitySpecialDetail
  235. // @router /banner/detail [get]
  236. func (this *BannerCoAntroller) Detail() {
  237. br := new(models.BaseResponse).Init()
  238. defer func() {
  239. this.Data["json"] = br
  240. this.ServeJSON()
  241. }()
  242. AdminUser := this.SysUser
  243. if AdminUser == nil {
  244. br.Msg = "请登录"
  245. br.ErrMsg = "请登录,用户信息为空"
  246. br.Ret = 408
  247. return
  248. }
  249. resp := new(cygx.GetCygxBannerImgRespDetailResp)
  250. bannerId, _ := this.GetInt("BannerId")
  251. if bannerId < 1 {
  252. br.Msg = "请输入详情ID"
  253. return
  254. }
  255. detail, err := cygx.GetCygxBannerDetail(bannerId)
  256. if err != nil {
  257. br.Msg = "详情不存在"
  258. br.ErrMsg = "获取失败,Err:" + err.Error()
  259. return
  260. }
  261. detail.BannerTypeName = cygxService.GetCygxBannerTypeMap()[detail.BannerType]
  262. BannerImgDetail, err := cygx.GetCygxBannerImgDetail(detail.ImgId)
  263. if err != nil {
  264. br.Msg = "详情不存在"
  265. br.ErrMsg = "获取失败,Err:" + err.Error()
  266. return
  267. }
  268. if BannerImgDetail == nil {
  269. br.Msg = "详情不存在"
  270. br.ErrMsg = "BannerImgDetail,为空:"
  271. return
  272. }
  273. Uv, err := cygx.GetCygxBannerHistoryCountUv(bannerId)
  274. if err != nil {
  275. br.Msg = "获取失败"
  276. br.ErrMsg = "获取Uv失败,Err:" + err.Error()
  277. return
  278. }
  279. Pv, err := cygx.GetCygxBannerHistoryCountPv(bannerId)
  280. if err != nil {
  281. br.Msg = "获取失败"
  282. br.ErrMsg = "获取Pv失败,Err:" + err.Error()
  283. return
  284. }
  285. detail.Uv = Uv
  286. detail.Pv = Pv
  287. detail.IndexImg = BannerImgDetail.IndexImg
  288. resp.Detail = detail
  289. br.Ret = 200
  290. br.Success = true
  291. br.Msg = "获取成功"
  292. br.Data = resp
  293. }
  294. // @Title 发布/取消发布
  295. // @Description 发布/取消发布报告
  296. // @Param request body cygx.CygxBannerIdReq true "type json string"
  297. // @Success 200 Ret=200 发布成功
  298. // @router /banner/publishAndcancel [post]
  299. func (this *BannerCoAntroller) PublishReport() {
  300. br := new(models.BaseResponse).Init()
  301. defer func() {
  302. this.Data["json"] = br
  303. this.ServeJSON()
  304. }()
  305. var req cygx.CygxBannerIdReq
  306. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  307. if err != nil {
  308. br.Msg = "参数解析异常!"
  309. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  310. return
  311. }
  312. bannerId := req.BannerId
  313. if bannerId == 0 {
  314. br.Msg = "参数错误"
  315. br.ErrMsg = "参数错误,id不可为空"
  316. return
  317. }
  318. detail, err := cygx.GetCygxBannerDetail(bannerId)
  319. if err != nil {
  320. br.Msg = "详情不存在"
  321. br.ErrMsg = "获取失败,Err:" + err.Error()
  322. return
  323. }
  324. var status int
  325. if detail.Status == 0 {
  326. status = 1
  327. } else {
  328. status = 0
  329. }
  330. err = cygx.EditCygxBannerStatus(status, bannerId)
  331. if err != nil {
  332. br.Msg = "操作失败"
  333. br.ErrMsg = "获取失败,Err:" + err.Error()
  334. return
  335. }
  336. br.Ret = 200
  337. br.Success = true
  338. br.Msg = "操作成功"
  339. }
  340. // @Title 下载PV
  341. // @Description 下载PV接口
  342. // @Param BannerId query int true "BannerId"
  343. // @router /banner/PvExport [get]
  344. func (this *BannerCoAntroller) 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. bannerId, _ := this.GetInt("BannerId")
  358. if bannerId < 1 {
  359. br.Msg = "请输入详情ID"
  360. return
  361. }
  362. var condition string
  363. var pars []interface{}
  364. condition = ` AND banner_id = ? `
  365. pars = append(pars, bannerId)
  366. var respList []*cygx.CygxBannerHistory
  367. //respList := new(cygx.CygxTacticsTimeLineHistory)
  368. list, err := cygx.GetCygxBannerHistoryList(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. cellF := rowTitle.AddCell()
  437. cellF.Value = "来源"
  438. for _, item := range respList {
  439. row := sheet.AddRow()
  440. cellA := row.AddCell()
  441. cellA.Value = item.RealName
  442. cellB := row.AddCell()
  443. cellB.Value = item.Mobile
  444. cellC := row.AddCell()
  445. cellC.Value = item.CompanyName
  446. cellD := row.AddCell()
  447. cellD.Value = item.SellerName
  448. cellE := row.AddCell()
  449. cellE.Value = item.CreateTime.Format(utils.FormatDateTime)
  450. cellF := row.AddCell()
  451. if item.RegisterPlatform == 1 {
  452. cellF.Value = "小程序"
  453. } else {
  454. cellF.Value = "网页版"
  455. }
  456. }
  457. err = xlsxFile.Save(downLoadnFilePath)
  458. if err != nil {
  459. br.Msg = "保存文件失败"
  460. br.ErrMsg = "保存文件失败"
  461. return
  462. }
  463. downloadFileName := time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  464. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  465. defer func() {
  466. os.Remove(downLoadnFilePath)
  467. }()
  468. br.Ret = 200
  469. br.Success = true
  470. br.Msg = "获取成功"
  471. }
  472. // @Title 移动顺序
  473. // @Description 移动顺序
  474. // @Param request body cygx.CygxBannerIdReq true "type json string"
  475. // @Success 200 Ret=200 发布成功
  476. // @router /banner/move [post]
  477. func (this *BannerCoAntroller) Move() {
  478. br := new(models.BaseResponse).Init()
  479. defer func() {
  480. this.Data["json"] = br
  481. this.ServeJSON()
  482. }()
  483. var req cygx.CygxBannerMoveReq
  484. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  485. if err != nil {
  486. br.Msg = "参数解析异常!"
  487. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  488. return
  489. }
  490. bannerId := req.BannerId
  491. previousBannerId := req.PreviousBannerId
  492. nextBannerId := req.NextBannerId
  493. listType := req.ListType
  494. if bannerId == 0 {
  495. br.Msg = "参数错误"
  496. br.ErrMsg = "参数错误,id不可为空"
  497. return
  498. }
  499. if listType != "A" && listType != "B" && listType != "C" {
  500. br.Msg = "参数错误" + listType
  501. br.ErrMsg = "移动列数参数错误" + listType
  502. return
  503. }
  504. detailmove, err := cygx.GetCygxBannerDetail(bannerId)
  505. if err != nil {
  506. br.Msg = "详情不存在"
  507. br.ErrMsg = "获取失败,Err:" + err.Error()
  508. return
  509. }
  510. if detailmove.Status == 0 {
  511. br.Msg = "未发布的无法移动"
  512. return
  513. }
  514. //如果是单列就直接移过来
  515. if previousBannerId+nextBannerId == 0 {
  516. err = cygx.UpdateCygxBannerlistType(1, bannerId, listType)
  517. if err != nil {
  518. br.Msg = "移动失败"
  519. br.ErrMsg = "获取失败,EditCygxBannerMove Err:" + err.Error()
  520. return
  521. }
  522. br.Ret = 200
  523. br.Success = true
  524. br.Msg = "操作成功"
  525. return
  526. }
  527. var sort int
  528. if previousBannerId > 0 {
  529. detail, err := cygx.GetCygxBannerDetail(previousBannerId)
  530. if err != nil {
  531. br.Msg = "详情不存在"
  532. br.ErrMsg = "获取失败,Err:" + err.Error()
  533. return
  534. }
  535. listType = detail.ListType
  536. sort = detail.Sort + 5
  537. } else {
  538. detail, err := cygx.GetCygxBannerDetail(nextBannerId)
  539. if err != nil {
  540. br.Msg = "详情不存在"
  541. br.ErrMsg = "获取失败,Err:" + err.Error()
  542. return
  543. }
  544. listType = detail.ListType
  545. sort = detail.Sort - 5
  546. }
  547. err = cygx.UpdateCygxBannerlistType(sort, bannerId, listType)
  548. //err = cygx.EditCygxBannerMove(sort, bannerId, listType)
  549. if err != nil {
  550. br.Msg = "移动失败"
  551. br.ErrMsg = "获取失败,EditCygxBannerMove Err:" + err.Error()
  552. return
  553. }
  554. go cygxService.UpdateBannerSort(listType)
  555. br.Ret = 200
  556. br.Success = true
  557. br.Msg = "操作成功"
  558. }