my_chart.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. package my_chart
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "hongze/hongze_yb/controller/response"
  6. "hongze/hongze_yb/models/request"
  7. responseModel "hongze/hongze_yb/models/response"
  8. chartInfoModel "hongze/hongze_yb/models/tables/chart_info"
  9. "hongze/hongze_yb/models/tables/company_product"
  10. "hongze/hongze_yb/models/tables/yb_config"
  11. "hongze/hongze_yb/models/tables/yb_my_chart"
  12. "hongze/hongze_yb/models/tables/yb_my_chart_classify"
  13. "hongze/hongze_yb/services"
  14. userService "hongze/hongze_yb/services/user"
  15. "hongze/hongze_yb/utils"
  16. "strconv"
  17. "time"
  18. )
  19. // MyChartController 用户-我的图表
  20. type MyChartController struct{}
  21. // List 我的图表列表
  22. func (this *MyChartController) List(c *gin.Context) {
  23. userInfo := userService.GetInfoByClaims(c)
  24. userId := int(userInfo.UserID)
  25. var req request.MyChartCollectListReq
  26. if c.Bind(&req) != nil {
  27. response.Fail("参数有误", c)
  28. return
  29. }
  30. cond := `user_id = ?`
  31. pars := make([]interface{}, 0)
  32. pars = append(pars, userId)
  33. if req.Keyword != "" {
  34. kw := fmt.Sprint("%", req.Keyword, "%")
  35. cond += ` AND chart_name LIKE ?`
  36. pars = append(pars, kw)
  37. }
  38. if req.ClassifyId > 0 {
  39. cond += ` AND my_chart_classify_id = ?`
  40. pars = append(pars, req.ClassifyId)
  41. }
  42. // classifyId = -1的时候查询默认分类
  43. if req.ClassifyId == -1 {
  44. cond += ` AND my_chart_classify_id = 0`
  45. }
  46. pageIndex := services.GetCurrPageByClaims(c)
  47. pageSize := services.GetPageSizeByClaims(c)
  48. ob := new(yb_my_chart.YbMyChart)
  49. chartTotal, e := ob.Count(cond, pars)
  50. if e != nil {
  51. response.FailMsg("获取失败", "获取用户图表列表总数失败, Err:"+e.Error(), c)
  52. return
  53. }
  54. total := int(chartTotal)
  55. list, e := ob.PageList(cond, pars, pageIndex, pageSize)
  56. if e != nil {
  57. response.FailMsg("获取失败", "获取用户图表列表失败, Err:"+e.Error(), c)
  58. return
  59. }
  60. respList := make([]*responseModel.MyChartItem, 0)
  61. for i := range list {
  62. respList = append(respList, &responseModel.MyChartItem{
  63. MyChartID: list[i].MyChartID,
  64. MyChartClassifyID: list[i].MyChartClassifyID,
  65. ChartInfoID: list[i].ChartInfoID,
  66. ChartName: list[i].ChartName,
  67. UniqueCode: list[i].UniqueCode,
  68. ChartImage: list[i].ChartImage,
  69. UserID: list[i].UserID,
  70. ReportID: list[i].ReportID,
  71. ReportChapterID: list[i].ReportChapterID,
  72. ChartInfoSource: list[i].Source,
  73. CreateTime: utils.TimeTransferString(utils.FormatDateTime, list[i].CreateTime),
  74. })
  75. }
  76. response.OkData("获取成功", &responseModel.MyChartListResp{
  77. List: respList,
  78. Paging: responseModel.GetPaging(pageIndex, pageSize, total),
  79. }, c)
  80. }
  81. // Collect 收藏图表
  82. func (this *MyChartController) Collect(c *gin.Context) {
  83. var req request.MyChartCollectReq
  84. if c.ShouldBind(&req) != nil {
  85. response.Fail("参数有误", c)
  86. return
  87. }
  88. if req.Authorization == "" {
  89. response.Fail("用户身份有误", c)
  90. return
  91. }
  92. if req.UniqueCode == "" {
  93. response.Fail("请选择图表", c)
  94. return
  95. }
  96. if req.ReportId <= 0 {
  97. response.Fail("请选择报告", c)
  98. return
  99. }
  100. // 获取用户信息
  101. userInfo, e := userService.GetUserInfoByToken(req.Authorization)
  102. if e != nil {
  103. response.FailMsg("操作失败", "Token获取有效用户失败, Err: "+e.Error(), c)
  104. return
  105. }
  106. if userInfo.UserID <= 0 {
  107. response.FailMsg("操作失败", "Token获取有效用户失败", c)
  108. return
  109. }
  110. userId := int(userInfo.UserID)
  111. // 获取图表信息
  112. chartInfo, e := chartInfoModel.GetChartInfoViewByUniqueCode(req.UniqueCode)
  113. if e != nil {
  114. response.FailMsg("操作失败", "UniqueCode获取图表信息失败, Err:"+e.Error(), c)
  115. return
  116. }
  117. if chartInfo.ChartInfoId <= 0 {
  118. response.FailMsg("操作失败", "图表信息有误", c)
  119. return
  120. }
  121. // 图表收藏上限
  122. ob := new(yb_my_chart.YbMyChart)
  123. countCond := `user_id = ?`
  124. countPars := make([]interface{}, 0)
  125. countPars = append(countPars, userId)
  126. count, e := ob.Count(countCond, countPars)
  127. if e != nil {
  128. response.FailMsg("操作失败", "获取用户图表收藏数量失败, Err: "+e.Error(), c)
  129. return
  130. }
  131. // 获取图表收藏上限配置
  132. configCond := `config_code = ?`
  133. configPars := make([]interface{}, 0)
  134. configPars = append(configPars, yb_config.UserChartCollectMax)
  135. confOB := new(yb_config.YbConfig)
  136. conf, e := confOB.Fetch(configCond, configPars)
  137. if e != nil {
  138. response.FailMsg("操作失败", "获取图表收藏上限配置失败, Err: "+e.Error(), c)
  139. return
  140. }
  141. max, e := strconv.Atoi(conf.ConfigValue)
  142. if e != nil {
  143. response.FailMsg("操作失败", "图表收藏上限配置有误, Err: "+e.Error(), c)
  144. return
  145. }
  146. if int(count) >= max {
  147. response.Fail(fmt.Sprintf("图表最多可收藏%d张", max), c)
  148. return
  149. }
  150. // 是否已收藏
  151. cond := `user_id = ? AND chart_info_id = ?`
  152. pars := make([]interface{}, 0)
  153. pars = append(pars, userId, chartInfo.ChartInfoId)
  154. exists, e := ob.FetchByCondition(cond, pars)
  155. if e != nil && e != utils.ErrNoRow {
  156. response.FailMsg("操作失败", "获取用户图表失败, Err: "+e.Error(), c)
  157. return
  158. }
  159. nowTime := time.Now().Local()
  160. collectId := 0
  161. if exists != nil && exists.MyChartID > 0 {
  162. // 更新收藏信息
  163. exists.ChartName = chartInfo.ChartName
  164. exists.ChartImage = chartInfo.ChartImage
  165. exists.ReportID = req.ReportId
  166. exists.ReportChapterID = req.ReportChapterId
  167. exists.ModifyTime = nowTime
  168. exists.Source = chartInfo.Source
  169. updateCols := []string{"ChartName", "ChartImage", "ReportID", "ReportChapterID", "ModifyTime"}
  170. if e = exists.Update(updateCols); e != nil {
  171. response.FailMsg("操作失败", "更新收藏失败, Err: "+e.Error(), c)
  172. return
  173. }
  174. collectId = exists.MyChartID
  175. } else {
  176. // 查询客户信息
  177. companyName := "潜在客户"
  178. companyStatus := "潜在"
  179. sellerId := 0
  180. if userInfo.CompanyID > 0 {
  181. //查询ficc产品信息和ficc销售信息
  182. companyInfo, e := company_product.GetByCompany2ProductId(userInfo.CompanyID, 1)
  183. if e != nil && e != utils.ErrNoRow {
  184. response.FailMsg("获取客户信息失败", "获取客户信息失败, Err: "+e.Error(), c)
  185. return
  186. }
  187. if companyInfo != nil && companyInfo.CompanyID > 0 {
  188. companyStatus = companyInfo.Status
  189. companyName = companyInfo.CompanyName
  190. sellerId = companyInfo.SellerID
  191. }
  192. }
  193. // 新增收藏
  194. ob.ChartInfoID = chartInfo.ChartInfoId
  195. ob.ChartName = chartInfo.ChartName
  196. ob.UniqueCode = chartInfo.UniqueCode
  197. ob.ChartImage = chartInfo.ChartImage
  198. ob.UserID = userId
  199. ob.CompanyID = int(userInfo.CompanyID)
  200. ob.CompanyStatus = companyStatus
  201. ob.CompanyName = companyName
  202. ob.RealName = userInfo.RealName
  203. ob.SellerID = sellerId
  204. ob.ReportID = req.ReportId
  205. ob.ReportChapterID = req.ReportChapterId
  206. ob.CreateTime = nowTime
  207. ob.ModifyTime = nowTime
  208. ob.Source = chartInfo.Source
  209. if e = ob.Create(); e != nil {
  210. response.FailMsg("操作失败", "新增收藏失败, Err: "+e.Error(), c)
  211. return
  212. }
  213. collectId = ob.MyChartID
  214. }
  215. response.OkData("操作成功", collectId, c)
  216. }
  217. // CollectCancel 取消收藏
  218. func (this *MyChartController) CollectCancel(c *gin.Context) {
  219. var req request.MyChartCollectCancelReq
  220. if c.ShouldBind(&req) != nil {
  221. response.Fail("参数有误", c)
  222. return
  223. }
  224. if req.Authorization == "" {
  225. response.Fail("用户身份有误", c)
  226. return
  227. }
  228. if req.UniqueCode == "" {
  229. response.Fail("请选择图表", c)
  230. return
  231. }
  232. // 获取用户信息
  233. userInfo, e := userService.GetUserInfoByToken(req.Authorization)
  234. if e != nil {
  235. response.FailMsg("操作失败", "Token获取有效用户失败, Err: "+e.Error(), c)
  236. return
  237. }
  238. if userInfo.UserID <= 0 {
  239. response.FailMsg("操作失败", "Token获取有效用户失败", c)
  240. return
  241. }
  242. userId := int(userInfo.UserID)
  243. // 获取图表信息
  244. //chartInfo, e := chartInfoModel.GetChartInfoViewByUniqueCode(req.UniqueCode)
  245. //if e != nil {
  246. // response.FailMsg("操作失败", "UniqueCode获取图表信息失败, Err:"+e.Error(), c)
  247. // return
  248. //}
  249. //if chartInfo.ChartInfoId <= 0 {
  250. // response.FailMsg("操作失败", "图表信息有误", c)
  251. // return
  252. //}
  253. cond := `user_id = ? AND unique_code = ?`
  254. pars := make([]interface{}, 0)
  255. pars = append(pars, userId, req.UniqueCode)
  256. ob := new(yb_my_chart.YbMyChart)
  257. // 此处可能出现同一个报告两个同样的图表均有"取消收藏"按钮,点击第一个取消之后再点击第二个会报错,此时进行一个友好的提示"已取消收藏"
  258. item, e := ob.FetchByCondition(cond, pars)
  259. if e != nil {
  260. response.FailMsg("已取消收藏", "获取用户图表失败, Err: "+e.Error(), c)
  261. return
  262. }
  263. if e = item.Delete(); e != nil {
  264. response.FailMsg("已取消收藏", "取消用户图表收藏失败, Err: "+e.Error(), c)
  265. return
  266. }
  267. response.Ok("操作成功", c)
  268. }
  269. // RelateClassify 加入分类
  270. func (this *MyChartController) RelateClassify(c *gin.Context) {
  271. userInfo := userService.GetInfoByClaims(c)
  272. userId := int(userInfo.UserID)
  273. if userId <= 0 {
  274. response.Fail("请登录后操作", c)
  275. return
  276. }
  277. var req request.MyChartRelateClassifyReq
  278. if c.ShouldBind(&req) != nil {
  279. response.Fail("参数有误", c)
  280. return
  281. }
  282. if req.MyChartId <= 0 {
  283. response.Fail("请选择图表", c)
  284. return
  285. }
  286. if req.ClassifyId <= 0 {
  287. response.Fail("请选择分类", c)
  288. return
  289. }
  290. ob := new(yb_my_chart.YbMyChart)
  291. item, e := ob.Fetch(req.MyChartId)
  292. if e != nil {
  293. response.FailMsg("图表信息有误", "获取用户图表失败, Err: "+e.Error(), c)
  294. return
  295. }
  296. if item.UserID != userId {
  297. response.Fail("无权操作", c)
  298. return
  299. }
  300. item.MyChartClassifyID = req.ClassifyId
  301. item.ModifyTime = time.Now().Local()
  302. if e = item.Update([]string{"MyChartClassifyID", "ModifyTime"}); e != nil {
  303. response.FailMsg("操作失败", "更新用户图表关联分类失败, Err: "+e.Error(), c)
  304. return
  305. }
  306. response.Ok("操作成功", c)
  307. }
  308. // IsCollect 是否已收藏
  309. func (this *MyChartController) IsCollect(c *gin.Context) {
  310. var req request.MyChartIsCollectReq
  311. if c.Bind(&req) != nil {
  312. response.Fail("参数有误", c)
  313. return
  314. }
  315. if req.Authorization == "" {
  316. response.Fail("用户身份有误", c)
  317. return
  318. }
  319. if req.UniqueCode == "" {
  320. response.Fail("请选择图表", c)
  321. return
  322. }
  323. // 获取用户信息
  324. userInfo, e := userService.GetUserInfoByToken(req.Authorization)
  325. if e != nil {
  326. response.FailMsg("获取失败", "Token获取有效用户失败, Err: "+e.Error(), c)
  327. return
  328. }
  329. if userInfo.UserID <= 0 {
  330. response.FailMsg("获取失败", "Token获取有效用户失败", c)
  331. return
  332. }
  333. userId := int(userInfo.UserID)
  334. // 获取图表信息
  335. //chartInfo, e := chartInfoModel.GetChartInfoViewByUniqueCode(req.UniqueCode)
  336. //if e != nil {
  337. // response.FailMsg("操作失败", "UniqueCode获取图表信息失败, Err:"+e.Error(), c)
  338. // return
  339. //}
  340. //if chartInfo.ChartInfoId <= 0 {
  341. // response.FailMsg("操作失败", "图表信息有误", c)
  342. // return
  343. //}
  344. // 是否已收藏
  345. ob := new(yb_my_chart.YbMyChart)
  346. cond := `user_id = ? AND unique_code = ?`
  347. pars := make([]interface{}, 0)
  348. pars = append(pars, userId, req.UniqueCode)
  349. exists, e := ob.FetchByCondition(cond, pars)
  350. if e != nil && e != utils.ErrNoRow {
  351. response.FailMsg("获取失败", "获取用户图表失败, Err: "+e.Error(), c)
  352. return
  353. }
  354. isCollect := false
  355. if exists != nil && exists.MyChartID > 0 {
  356. isCollect = true
  357. }
  358. response.OkData("获取成功", isCollect, c)
  359. }
  360. // Locate 图表在列表中的定位
  361. func (this *MyChartController) Locate(c *gin.Context) {
  362. userInfo := userService.GetInfoByClaims(c)
  363. userId := int(userInfo.UserID)
  364. var req request.MyChartCollectLocateReq
  365. if c.Bind(&req) != nil {
  366. response.Fail("参数有误", c)
  367. return
  368. }
  369. cond := `user_id = ?`
  370. pars := make([]interface{}, 0)
  371. pars = append(pars, userId)
  372. ob := new(yb_my_chart.YbMyChart)
  373. list, e := ob.List(cond, pars)
  374. if e != nil {
  375. response.FailMsg("获取失败", "获取用户图表列表失败, Err:"+e.Error(), c)
  376. return
  377. }
  378. charts := make([]*responseModel.MyChartLocateItem, 0)
  379. if len(list) == 0 {
  380. response.OkData("获取成功", charts, c)
  381. return
  382. }
  383. // 如果是分类下的图表, 则优先分类排序, 其次分类图表排序
  384. if req.ClassifyId > 0 || req.ClassifyId == -1 {
  385. classifyCond := `user_id = ?`
  386. classifyPars := make([]interface{}, 0)
  387. classifyPars = append(classifyPars, userId)
  388. classifyOB := new(yb_my_chart_classify.YbMyChartClassify)
  389. classies, e := classifyOB.List(classifyCond, classifyPars)
  390. if e != nil {
  391. response.FailMsg("获取失败", "获取用户图表分类失败, Err: "+e.Error(), c)
  392. return
  393. }
  394. // 未分类的图表排在前面
  395. for _, ch := range list {
  396. if ch.MyChartClassifyID > 0 {
  397. continue
  398. }
  399. v := new(responseModel.MyChartLocateItem)
  400. v.MyChartID = ch.MyChartID
  401. v.MyChartClassifyID = -1 // 未分类ID
  402. v.ChartInfoID = ch.ChartInfoID
  403. v.ChartName = ch.ChartName
  404. v.UniqueCode = ch.UniqueCode
  405. v.ChartInfoSource = ch.Source
  406. charts = append(charts, v)
  407. }
  408. // 已归类的图表
  409. for _, cl := range classies {
  410. for _, ch := range list {
  411. if ch.MyChartClassifyID != cl.MyChartClassifyID {
  412. continue
  413. }
  414. v := new(responseModel.MyChartLocateItem)
  415. v.MyChartID = ch.MyChartID
  416. v.MyChartClassifyID = ch.MyChartClassifyID
  417. v.ChartInfoID = ch.ChartInfoID
  418. v.ChartName = ch.ChartName
  419. v.UniqueCode = ch.UniqueCode
  420. v.ChartInfoSource = ch.Source
  421. charts = append(charts, v)
  422. }
  423. }
  424. total := len(charts)
  425. if total > 0 {
  426. last := total - 1
  427. for k, v := range charts {
  428. // 首图-首图无上一张
  429. if k == 0 {
  430. if (k + 1) <= last {
  431. v.NextChartInfoID = charts[k+1].ChartInfoID
  432. }
  433. continue
  434. }
  435. // 尾图-尾图无下一张
  436. if k == last {
  437. if (k - 1) >= 0 {
  438. v.PrevChartInfoID = charts[k-1].ChartInfoID
  439. }
  440. continue
  441. }
  442. // 中间
  443. if (k - 1) >= 0 {
  444. v.PrevChartInfoID = charts[k-1].ChartInfoID
  445. }
  446. if (k + 1) <= last {
  447. v.NextChartInfoID = charts[k+1].ChartInfoID
  448. }
  449. }
  450. }
  451. response.OkData("获取成功", charts, c)
  452. return
  453. }
  454. // 非分类列表
  455. if req.ClassifyId == 0 {
  456. for _, ch := range list {
  457. v := new(responseModel.MyChartLocateItem)
  458. v.MyChartID = ch.MyChartID
  459. v.MyChartClassifyID = ch.MyChartClassifyID
  460. v.ChartInfoID = ch.ChartInfoID
  461. v.ChartName = ch.ChartName
  462. v.UniqueCode = ch.UniqueCode
  463. v.ChartInfoSource = ch.Source
  464. charts = append(charts, v)
  465. }
  466. total := len(charts)
  467. if total > 0 {
  468. last := total - 1
  469. for k, v := range charts {
  470. // 首图-首图的上一张为尾图
  471. if k == 0 {
  472. if last > 0 {
  473. v.PrevChartInfoID = charts[last].ChartInfoID
  474. }
  475. if (k + 1) <= last {
  476. v.NextChartInfoID = charts[k+1].ChartInfoID
  477. }
  478. continue
  479. }
  480. // 尾图-尾图的下一张为首图
  481. if k == last {
  482. if (k - 1) >= 0 {
  483. v.PrevChartInfoID = charts[k-1].ChartInfoID
  484. }
  485. v.NextChartInfoID = charts[0].ChartInfoID
  486. continue
  487. }
  488. // 中间
  489. if (k - 1) >= 0 {
  490. v.PrevChartInfoID = charts[k-1].ChartInfoID
  491. }
  492. if (k + 1) <= last {
  493. v.NextChartInfoID = charts[k+1].ChartInfoID
  494. }
  495. }
  496. }
  497. }
  498. response.OkData("获取成功", charts, c)
  499. }