eta_forum_hub.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. package eta_forum
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models/data_manage"
  5. "eta/eta_api/models/data_manage/chart_theme"
  6. "eta/eta_api/models/system"
  7. "eta/eta_api/services/alarm_msg"
  8. "eta/eta_api/utils"
  9. "fmt"
  10. "strconv"
  11. "time"
  12. )
  13. type UploadChartToForumReq struct {
  14. ChartInfoId int `description:"图表ID"`
  15. Description string `description:"逻辑简述"`
  16. }
  17. type UpdateChartToForumReq struct {
  18. ChartInfoId int `description:"图表ID"`
  19. }
  20. type ChartSaveLibReq struct {
  21. ChartInfo *data_manage.ChartInfo
  22. Description string `description:"逻辑简述"`
  23. EdbInfoList []*data_manage.EdbInfo
  24. ChartThemeList []*chart_theme.ChartTheme
  25. EdbInfoDataList []*AddEdbDataReq
  26. ChartEdbMapping []*data_manage.ChartEdbMapping
  27. EdbInfoCalculateMapping []*data_manage.EdbInfoCalculateMapping
  28. ChartSeries []*data_manage.ChartSeries
  29. ChartSeriesEdbMapping []*data_manage.ChartSeriesEdbMapping
  30. CreatorInfo *system.Admin //创建者的账号信息
  31. UploaderInfo *system.Admin //上传者的账号信息
  32. }
  33. type ChartSaveLibResp struct {
  34. Ret int
  35. Msg string
  36. ErrMsg string
  37. ErrCode string
  38. Data *data_manage.AddChartInfoResp
  39. Success bool `description:"true 执行成功,false 执行失败"`
  40. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  41. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  42. }
  43. type AddEdbDataReq struct {
  44. EdbCode string
  45. EdbType int
  46. DataList []*data_manage.EdbDataBase
  47. }
  48. type DeleteChartReq struct {
  49. ChartInfoId int `description:"图表id"`
  50. }
  51. // UploadChart 上传图表接口
  52. func UploadChart(chartInfoId int, description string, uploaderInfo *system.Admin) (err error, errMsg string) {
  53. defer func() {
  54. if err != nil {
  55. go alarm_msg.SendAlarmMsg(fmt.Sprintf("上传图表至社区失败:Err:%v,ErrMsg:%s", err, errMsg), 3)
  56. }
  57. }()
  58. // 查询图表信息
  59. chartInfo, err := data_manage.GetChartInfoById(chartInfoId)
  60. if err != nil {
  61. if utils.IsErrNoRow(err) {
  62. errMsg = "图表不存在"
  63. err = fmt.Errorf(errMsg)
  64. return
  65. }
  66. errMsg = "获取图表信息失败"
  67. err = fmt.Errorf("获取图表信息失败,Err:" + err.Error())
  68. return
  69. }
  70. if chartInfo.ForumChartInfoId > 0 {
  71. errMsg = "该图表已上传至社区,无法再次上传"
  72. err = fmt.Errorf(errMsg)
  73. return
  74. }
  75. //查询图表主题
  76. //查询图表指标
  77. //获取原图表关联的指标信息列表
  78. chartMappingList, err := data_manage.GetChartMappingList(chartInfoId)
  79. if err != nil {
  80. errMsg = "获取图表关联的指标信息失败"
  81. err = fmt.Errorf("获取图表关联的指标信息失败,Err:" + err.Error())
  82. return
  83. }
  84. edbIds := make([]int, 0)
  85. for _, v := range chartMappingList {
  86. edbIds = append(edbIds, v.EdbInfoId)
  87. }
  88. chartSeriesList := make([]*data_manage.ChartSeries, 0)
  89. chartSeriesEdbList := make([]*data_manage.ChartSeriesEdbMapping, 0)
  90. if chartInfo.ChartType == utils.CHART_TYPE_SECTION_COMBINE {
  91. chartSeriesList, err = data_manage.GetChartSeriesByChartInfoId(chartInfoId)
  92. if err != nil {
  93. errMsg = "获取图表关联的系列信息失败"
  94. err = fmt.Errorf("获取图表关联的系列信息失败,Err:" + err.Error())
  95. return
  96. }
  97. chartSeriesEdbList, err = data_manage.GetChartSeriesEdbByChartInfoId(chartInfoId)
  98. if err != nil {
  99. errMsg = "获取图表关联的系列指标信息失败"
  100. err = fmt.Errorf("获取图表关联的系列指标信息失败,Err:" + err.Error())
  101. return
  102. }
  103. }
  104. var (
  105. edbInfoList []*data_manage.EdbInfo
  106. edbMappingList []*data_manage.EdbInfoCalculateMapping
  107. edbInfoDataList []*AddEdbDataReq
  108. )
  109. //查询指标详情
  110. edbInfoList, edbMappingList, edbInfoDataList, err = GetEdbListByEdbInfoId(edbIds)
  111. if err != nil {
  112. errMsg = "获取指标详情失败"
  113. err = fmt.Errorf("获取指标详情失败,Err:" + err.Error())
  114. return
  115. }
  116. req := new(ChartSaveLibReq)
  117. req.ChartInfo = chartInfo
  118. req.ChartEdbMapping = chartMappingList
  119. req.EdbInfoList = edbInfoList
  120. req.EdbInfoDataList = edbInfoDataList
  121. req.EdbInfoCalculateMapping = edbMappingList
  122. req.Description = description
  123. req.ChartSeries = chartSeriesList
  124. req.ChartSeriesEdbMapping = chartSeriesEdbList
  125. // 查询创建者信息
  126. creatorInfo, _ := system.GetSysAdminById(chartInfo.SysUserId)
  127. if creatorInfo != nil {
  128. req.CreatorInfo = creatorInfo
  129. }
  130. req.UploaderInfo = uploaderInfo
  131. // 添加计算指标
  132. reqJson, err := json.Marshal(req)
  133. if err != nil {
  134. errMsg = "参数解析异常"
  135. err = fmt.Errorf("参数解析异常,Err:" + err.Error())
  136. return
  137. }
  138. respItem, err := ChartSaveLib(string(reqJson))
  139. if err != nil {
  140. errMsg = "上传失败"
  141. err = fmt.Errorf("上传失败,Err:" + err.Error())
  142. return
  143. }
  144. if respItem.Ret != 200 {
  145. errMsg = "上传失败"
  146. err = fmt.Errorf(respItem.ErrMsg)
  147. return
  148. }
  149. if respItem.Data != nil && respItem.Data.ChartInfoId != 0 {
  150. // 更新社区返回的图表ID
  151. err = data_manage.SetForumChartInfoId(chartInfoId, respItem.Data.ChartInfoId, utils.ChartClassifyResourceStatusUp)
  152. if err != nil {
  153. errMsg = "更新图表ID失败"
  154. err = fmt.Errorf("更新图表ID失败,Err:" + err.Error())
  155. return
  156. }
  157. }
  158. return
  159. }
  160. // UpdateChart 更新社区里的图表接口
  161. func UpdateChart(chartInfoId int) (err error, errMsg string) {
  162. defer func() {
  163. if err != nil {
  164. go alarm_msg.SendAlarmMsg(fmt.Sprintf("同步图表信息至社区失败:Err:%v,ErrMsg:%s", err, errMsg), 3)
  165. }
  166. }()
  167. // 查询图表信息
  168. chartInfo, err := data_manage.GetChartInfoById(chartInfoId)
  169. if err != nil {
  170. if utils.IsErrNoRow(err) {
  171. errMsg = "图表不存在"
  172. err = fmt.Errorf(errMsg)
  173. return
  174. }
  175. errMsg = "获取图表信息失败"
  176. err = fmt.Errorf("获取图表信息失败,Err:" + err.Error())
  177. return
  178. }
  179. if chartInfo.ForumChartInfoId <= 0 {
  180. errMsg = "该图表未上传至社区"
  181. err = fmt.Errorf(errMsg)
  182. return
  183. }
  184. //查询图表主题
  185. //查询图表指标
  186. //获取原图表关联的指标信息列表
  187. chartMappingList, err := data_manage.GetChartMappingList(chartInfoId)
  188. if err != nil {
  189. errMsg = "获取图表关联的指标信息失败"
  190. err = fmt.Errorf("获取图表关联的指标信息失败,Err:" + err.Error())
  191. return
  192. }
  193. edbIds := make([]int, 0)
  194. for _, v := range chartMappingList {
  195. edbIds = append(edbIds, v.EdbInfoId)
  196. }
  197. chartSeriesList := make([]*data_manage.ChartSeries, 0)
  198. chartSeriesEdbList := make([]*data_manage.ChartSeriesEdbMapping, 0)
  199. if chartInfo.ChartType == utils.CHART_TYPE_SECTION_COMBINE {
  200. chartSeriesList, err = data_manage.GetChartSeriesByChartInfoId(chartInfoId)
  201. if err != nil {
  202. errMsg = "获取图表关联的系列信息失败"
  203. err = fmt.Errorf("获取图表关联的系列信息失败,Err:" + err.Error())
  204. return
  205. }
  206. chartSeriesEdbList, err = data_manage.GetChartSeriesEdbByChartInfoId(chartInfoId)
  207. if err != nil {
  208. errMsg = "获取图表关联的系列指标信息失败"
  209. err = fmt.Errorf("获取图表关联的系列指标信息失败,Err:" + err.Error())
  210. return
  211. }
  212. }
  213. var (
  214. edbInfoList []*data_manage.EdbInfo
  215. edbMappingList []*data_manage.EdbInfoCalculateMapping
  216. edbInfoDataList []*AddEdbDataReq
  217. )
  218. //查询指标详情
  219. edbInfoList, edbMappingList, edbInfoDataList, err = GetEdbListByEdbInfoId(edbIds)
  220. if err != nil {
  221. errMsg = "获取指标详情失败"
  222. err = fmt.Errorf("获取指标详情失败,Err:" + err.Error())
  223. return
  224. }
  225. req := new(ChartSaveLibReq)
  226. req.ChartInfo = chartInfo
  227. req.ChartInfo.ChartInfoId = chartInfo.ForumChartInfoId
  228. req.ChartEdbMapping = chartMappingList
  229. req.EdbInfoList = edbInfoList
  230. req.EdbInfoDataList = edbInfoDataList
  231. req.EdbInfoCalculateMapping = edbMappingList
  232. req.ChartSeries = chartSeriesList
  233. req.ChartSeriesEdbMapping = chartSeriesEdbList
  234. // 查询创建者信息
  235. creatorInfo, _ := system.GetSysAdminById(chartInfo.SysUserId)
  236. if creatorInfo != nil {
  237. req.CreatorInfo = creatorInfo
  238. }
  239. // 添加计算指标
  240. reqJson, err := json.Marshal(req)
  241. if err != nil {
  242. errMsg = "参数解析异常"
  243. err = fmt.Errorf("参数解析异常,Err:" + err.Error())
  244. return
  245. }
  246. respItem, err := ChartUpdateLib(string(reqJson))
  247. if err != nil {
  248. errMsg = "更新失败"
  249. err = fmt.Errorf("上传失败,Err:" + err.Error())
  250. return
  251. }
  252. if respItem.Ret != 200 {
  253. errMsg = "更新失败"
  254. err = fmt.Errorf(respItem.ErrMsg)
  255. return
  256. }
  257. return
  258. }
  259. func GetEdbListByEdbInfoId(edbInfoIds []int) (edbInfoList []*data_manage.EdbInfo, edbMappingList []*data_manage.EdbInfoCalculateMapping, edbInfoDataList []*AddEdbDataReq, err error) {
  260. //查询指标信息
  261. //查询指标映射
  262. //查询所有指标数据
  263. //查询这个指标相关的mapping信息放到数组里,
  264. //将得到的指标ID信息放到数组里
  265. hasFindMap := make(map[int]struct{})
  266. edbInfoIdMap := make(map[int]struct{})
  267. edbMappingList = make([]*data_manage.EdbInfoCalculateMapping, 0)
  268. edbMappingMap := make(map[int]struct{})
  269. for _, edbInfoId := range edbInfoIds {
  270. edbMappingList, err = traceEdbInfoByEdbInfoId(edbInfoId, hasFindMap, edbInfoIdMap, edbMappingList, edbMappingMap)
  271. if err != nil {
  272. err = fmt.Errorf(" traceEdbInfoByEdbInfoId err: %s", err.Error())
  273. return
  274. }
  275. }
  276. // 指标信息map
  277. edbInfoIdList := make([]int, 0)
  278. for k, _ := range edbInfoIdMap {
  279. edbInfoIdList = append(edbInfoIdList, k)
  280. }
  281. edbInfoList, err = data_manage.GetEdbInfoByIdList(edbInfoIdList)
  282. if err != nil {
  283. err = fmt.Errorf(" GetEdbInfoByIdList err: %s", err.Error())
  284. return
  285. }
  286. for _, v := range edbInfoList {
  287. var dataList []*data_manage.EdbDataBase
  288. if v.Source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  289. dataList, err = data_manage.GetEdbDataBaseMongoByEdbInfoId(v.EdbInfoId, v.Source, v.SubSource)
  290. } else if v.Source == utils.DATA_SOURCE_THS && v.SubSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
  291. dataList, err = data_manage.GetThsHfEdbDataBaseMongoByEdbInfoId(v.EdbInfoId, v.Source, v.SubSource)
  292. } else {
  293. dataList, err = data_manage.GetEdbDataBaseByEdbInfoId(v.EdbInfoId, v.Source, v.SubSource)
  294. }
  295. if err != nil {
  296. err = fmt.Errorf("查询指标数据失败 Err: %s", err.Error())
  297. return
  298. }
  299. tmp := new(AddEdbDataReq)
  300. tmp.EdbCode = v.EdbCode
  301. tmp.EdbType = v.EdbType
  302. tmp.DataList = dataList
  303. edbInfoDataList = append(edbInfoDataList, tmp)
  304. }
  305. return
  306. }
  307. // traceEdbInfoByEdbInfoId 指标追溯
  308. func traceEdbInfoByEdbInfoId(edbInfoId int, hasFindMap map[int]struct{}, edbInfoIdMap map[int]struct{}, edbMappingList []*data_manage.EdbInfoCalculateMapping, edbMappingMap map[int]struct{}) (newEdbMappingList []*data_manage.EdbInfoCalculateMapping, err error) {
  309. newEdbMappingList = edbMappingList
  310. _, ok := hasFindMap[edbInfoId]
  311. if ok {
  312. return
  313. }
  314. if _, ok1 := edbInfoIdMap[edbInfoId]; !ok1 {
  315. edbInfoIdMap[edbInfoId] = struct{}{}
  316. }
  317. edbInfoMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
  318. if e != nil {
  319. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", e.Error())
  320. return
  321. }
  322. if len(edbInfoMappingList) > 0 {
  323. fromEdbInfoIdList := make([]int, 0)
  324. for _, v := range edbInfoMappingList {
  325. fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
  326. if _, ok1 := edbInfoIdMap[v.FromEdbInfoId]; !ok1 {
  327. edbInfoIdMap[v.FromEdbInfoId] = struct{}{}
  328. }
  329. if _, ok2 := edbMappingMap[v.EdbInfoCalculateMappingId]; !ok2 {
  330. edbMappingMap[v.EdbInfoCalculateMappingId] = struct{}{}
  331. tmp := &data_manage.EdbInfoCalculateMapping{
  332. EdbInfoCalculateMappingId: v.EdbInfoCalculateMappingId,
  333. EdbInfoId: v.EdbInfoId,
  334. Source: v.Source,
  335. SourceName: v.SourceName,
  336. EdbCode: v.EdbCode,
  337. FromEdbInfoId: v.FromEdbInfoId,
  338. FromEdbCode: v.FromEdbCode,
  339. FromEdbName: v.FromEdbName,
  340. FromSource: v.FromSource,
  341. FromSourceName: v.FromSourceName,
  342. FromTag: v.FromTag,
  343. Sort: v.Sort,
  344. CreateTime: v.CreateTime,
  345. ModifyTime: v.ModifyTime,
  346. }
  347. newEdbMappingList = append(newEdbMappingList, tmp)
  348. }
  349. if edbInfoId != v.FromEdbInfoId && (v.FromEdbType == 2 || v.FromEdbInfoType == 1) {
  350. // 查过了就不查了
  351. if _, ok2 := hasFindMap[v.FromEdbInfoId]; !ok2 {
  352. newEdbMappingList, e = traceEdbInfoByEdbInfoId(v.FromEdbInfoId, hasFindMap, edbInfoIdMap, newEdbMappingList, edbMappingMap)
  353. if e != nil {
  354. err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", e.Error())
  355. return
  356. }
  357. }
  358. }
  359. hasFindMap[v.FromEdbInfoId] = struct{}{}
  360. }
  361. }
  362. hasFindMap[edbInfoId] = struct{}{}
  363. return
  364. }
  365. // DeleteChart 上传图表接口
  366. func DeleteChart(chartInfoId int) (err error, errMsg string) {
  367. // 查询图表信息
  368. chartInfo, err := data_manage.GetChartInfoById(chartInfoId)
  369. if err != nil {
  370. if utils.IsErrNoRow(err) {
  371. errMsg = "图表不存在"
  372. err = fmt.Errorf(errMsg)
  373. return
  374. }
  375. errMsg = "获取图表信息失败"
  376. err = fmt.Errorf("获取图表信息失败,Err:" + err.Error())
  377. return
  378. }
  379. if chartInfo.ForumChartInfoId <= 0 {
  380. errMsg = "该图表未上传至社区,无需撤回"
  381. err = fmt.Errorf(errMsg)
  382. return
  383. }
  384. req := new(DeleteChartReq)
  385. req.ChartInfoId = chartInfo.ForumChartInfoId
  386. // 添加计算指标
  387. reqJson, err := json.Marshal(req)
  388. if err != nil {
  389. errMsg = "参数解析异常"
  390. err = fmt.Errorf("参数解析异常,Err:" + err.Error())
  391. return
  392. }
  393. respItem, err := ChartDeleteLib(string(reqJson))
  394. if err != nil {
  395. errMsg = "撤回失败"
  396. err = fmt.Errorf("撤回失败,Err:" + err.Error())
  397. return
  398. }
  399. if respItem.Ret != 200 {
  400. errMsg = "撤回失败"
  401. err = fmt.Errorf(respItem.ErrMsg)
  402. return
  403. }
  404. // 更新社区返回的图表ID
  405. err = data_manage.SetForumChartInfoId(chartInfoId, 0, utils.ChartClassifyResourceStatusDown)
  406. if err != nil {
  407. errMsg = "撤回失败"
  408. err = fmt.Errorf("更新图表ID失败,Err:" + err.Error())
  409. return
  410. }
  411. return
  412. }
  413. type AdminSaveReq struct {
  414. *system.Admin
  415. BusinessCode string `description:"商家编码"`
  416. }
  417. // 上传管理员信息
  418. func AdminSave(adminId int) (err error) {
  419. if utils.BusinessCode == "" || (utils.BusinessCode != utils.BusinessCodeRelease && utils.BusinessCode != utils.BusinessCodeDebug && utils.BusinessCode != utils.BusinessCodeSandbox) {
  420. return
  421. }
  422. //查询用户信息
  423. adminInfo, err := system.GetSysAdminById(adminId)
  424. if err != nil {
  425. if utils.IsErrNoRow(err) {
  426. err = fmt.Errorf("用户不存在")
  427. return
  428. }
  429. err = fmt.Errorf("获取用户信息失败,Err:" + err.Error())
  430. return
  431. }
  432. var req AdminSaveReq
  433. req.Admin = adminInfo
  434. req.BusinessCode = utils.BusinessCode
  435. reqJson, err := json.Marshal(req)
  436. if err != nil {
  437. err = fmt.Errorf("参数解析异常,Err:" + err.Error())
  438. return
  439. }
  440. respItem, err := AdminSaveLib(string(reqJson))
  441. if err != nil {
  442. err = fmt.Errorf("上传管理员信息失败,Err:" + err.Error())
  443. return
  444. }
  445. if respItem.Ret != 200 {
  446. err = fmt.Errorf("上传管理员信息失败,Err:%v,errMsg:%v", respItem.Msg, respItem.ErrMsg)
  447. return
  448. }
  449. return
  450. }
  451. type AdminDeleteReq struct {
  452. AdminName string
  453. BusinessCode string `description:"商家编码"`
  454. }
  455. // 上传管理员信息
  456. func AdminDelete(adminName string) (err error) {
  457. if utils.BusinessCode == "" || (utils.BusinessCode != utils.BusinessCodeRelease && utils.BusinessCode != utils.BusinessCodeDebug && utils.BusinessCode != utils.BusinessCodeSandbox) {
  458. return
  459. }
  460. var req AdminDeleteReq
  461. req.AdminName = adminName
  462. req.BusinessCode = utils.BusinessCode
  463. reqJson, err := json.Marshal(req)
  464. if err != nil {
  465. err = fmt.Errorf("参数解析异常,Err:" + err.Error())
  466. return
  467. }
  468. respItem, err := AdminDeleteLib(string(reqJson))
  469. if err != nil {
  470. err = fmt.Errorf("删除管理员信息失败,Err:" + err.Error())
  471. return
  472. }
  473. if respItem.Ret != 200 {
  474. err = fmt.Errorf("删除管理员信息失败,Err:%v,errMsg:%v", respItem.Msg, respItem.ErrMsg)
  475. return
  476. }
  477. return
  478. }
  479. type ChartThemeReq struct {
  480. *chart_theme.ChartTheme
  481. BusinessCode string `description:"商家编码"`
  482. }
  483. // 上传主题信息
  484. func ChartThemeSave(theme *chart_theme.ChartTheme) (err error) {
  485. if utils.BusinessCode == "" || (utils.BusinessCode != utils.BusinessCodeRelease && utils.BusinessCode != utils.BusinessCodeDebug && utils.BusinessCode != utils.BusinessCodeSandbox) {
  486. return
  487. }
  488. var req ChartThemeReq
  489. req.ChartTheme = theme
  490. req.BusinessCode = utils.BusinessCode
  491. reqJson, err := json.Marshal(req)
  492. if err != nil {
  493. err = fmt.Errorf("参数解析异常,Err:" + err.Error())
  494. return
  495. }
  496. respItem, err := ChartThemeSaveLib(string(reqJson))
  497. if err != nil {
  498. err = fmt.Errorf("上传图表主题信息失败,Err:" + err.Error())
  499. return
  500. }
  501. if respItem.Ret != 200 {
  502. err = fmt.Errorf("上传图表主题信息失败,Err:%v,errMsg:%v", respItem.Msg, respItem.ErrMsg)
  503. return
  504. }
  505. return
  506. }
  507. // ChartThemeTypeSave 上传主题类型信息
  508. func ChartThemeTypeSave(theme *chart_theme.ChartThemeType) (err error) {
  509. if utils.BusinessCode == "" || (utils.BusinessCode != utils.BusinessCodeRelease && utils.BusinessCode != utils.BusinessCodeDebug && utils.BusinessCode != utils.BusinessCodeSandbox) {
  510. return
  511. }
  512. //req.BusinessCode = utils.BusinessCode
  513. reqJson, err := json.Marshal(theme)
  514. if err != nil {
  515. err = fmt.Errorf("参数解析异常,Err:" + err.Error())
  516. return
  517. }
  518. respItem, err := ChartThemeTypeSaveLib(string(reqJson))
  519. if err != nil {
  520. err = fmt.Errorf("上传图表主题信息失败,Err:" + err.Error())
  521. return
  522. }
  523. if respItem.Ret != 200 {
  524. err = fmt.Errorf("上传图表主题信息失败,Err:%v,errMsg:%v", respItem.Msg, respItem.ErrMsg)
  525. return
  526. }
  527. return
  528. }
  529. // ChartBatchUpdate 批量更新图表信息
  530. func ChartBatchUpdate(chartClassifyId int) (err error) {
  531. if utils.BusinessCode == "" || (utils.BusinessCode != utils.BusinessCodeRelease && utils.BusinessCode != utils.BusinessCodeDebug && utils.BusinessCode != utils.BusinessCodeSandbox) {
  532. return
  533. }
  534. var tmpErr []error
  535. deleteCache := true
  536. cacheKey := "eta_forum_task:EtaForumChartUpdateByClassifyId:" + strconv.Itoa(chartClassifyId)
  537. defer func() {
  538. if deleteCache {
  539. _ = utils.Rc.Delete(cacheKey)
  540. }
  541. stack := ""
  542. if err != nil {
  543. stack = fmt.Sprintln(stack + err.Error())
  544. }
  545. if len(tmpErr) > 0 {
  546. for _, v := range tmpErr {
  547. stack = fmt.Sprintln(stack + v.Error())
  548. }
  549. }
  550. if stack != "" {
  551. utils.FileLog.Error("批量更新资源库图表信息失败"+"<br/>"+stack)
  552. go alarm_msg.SendAlarmMsg("批量更新资源库图表信息失败"+"<br/>"+stack, 3)
  553. }
  554. }()
  555. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  556. deleteCache = false
  557. err = fmt.Errorf("系统处理中,请稍后重试!")
  558. return
  559. }
  560. // 获取当前分类以及子分类下的所有图表ID
  561. chartClassifyInfo, err := data_manage.GetChartClassifyById(chartClassifyId)
  562. if err != nil {
  563. return
  564. }
  565. chartClassifyInfoList, err := data_manage.GetChartClassifyByLevelPath(chartClassifyInfo.LevelPath)
  566. if err != nil {
  567. return
  568. }
  569. chartClassifyIdList := make([]string, 0)
  570. for _, v := range chartClassifyInfoList {
  571. chartClassifyIdList = append(chartClassifyIdList, strconv.Itoa(v.ChartClassifyId))
  572. }
  573. // 设置缓存判断处理中则不更新
  574. condition := " and forum_chart_info_id > 0 and source=1 and chart_classify_id in ?"
  575. // 查询需要更新的图表信息总数
  576. total, err := data_manage.GetChartInfoCountByCondition(condition, []interface{}{chartClassifyIdList})
  577. if err != nil {
  578. return
  579. }
  580. if total == 0 {
  581. return
  582. }
  583. // 更新图表数据
  584. offset := 0
  585. pageSize := 100
  586. success := 0
  587. // 循环更新100个图表数据
  588. for i := 0; offset < total; i++ {
  589. // 查询需要更新的图表信息
  590. chartInfos, e := data_manage.GetChartInfoListByCondition(condition, []interface{}{chartClassifyIdList}, offset, pageSize)
  591. if e != nil {
  592. err = fmt.Errorf("查询需要更新的图表信息失败: %v", e)
  593. return
  594. }
  595. // 循环更新图表数据
  596. for _, chartInfo := range chartInfos {
  597. // 更新图表数据
  598. er, msg := UpdateChart(chartInfo.ChartInfoId)
  599. if er != nil {
  600. er = fmt.Errorf("图表ID %d, 更新图表数据失败: %s, %v", chartInfo.ChartInfoId, msg, er)
  601. tmpErr = append(tmpErr, er)
  602. continue
  603. }
  604. success += 1
  605. }
  606. offset = (i + 1) * pageSize
  607. }
  608. fmt.Println("更新图表数据完成, 更新图表数据总数:", success)
  609. return
  610. }
  611. // 批量上传图表分类信息
  612. func ChartInfoSaveBatch(chartClassifyInfo *data_manage.ChartClassify, sysUser *system.Admin) (err error) {
  613. if utils.BusinessCode == "" || (utils.BusinessCode != utils.BusinessCodeRelease && utils.BusinessCode != utils.BusinessCodeDebug && utils.BusinessCode != utils.BusinessCodeSandbox) {
  614. return
  615. }
  616. var tmpErr []error
  617. deleteCache := true
  618. cacheKey := "eta_forum_task:ChartInfoSaveBatch:" + strconv.Itoa(chartClassifyInfo.ChartClassifyId)
  619. defer func() {
  620. if deleteCache {
  621. _ = utils.Rc.Delete(cacheKey)
  622. }
  623. stack := ""
  624. if err != nil {
  625. stack = fmt.Sprintln(stack + err.Error())
  626. }
  627. if len(tmpErr) > 0 {
  628. for _, v := range tmpErr {
  629. stack = fmt.Sprintln(stack + v.Error())
  630. }
  631. }
  632. if stack != "" {
  633. utils.FileLog.Error("批量上传资源库图表信息失败"+"<br/>"+stack)
  634. go alarm_msg.SendAlarmMsg("批量上传资源库图表信息失败"+"<br/>"+stack, 3)
  635. }
  636. }()
  637. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  638. deleteCache = false
  639. err = fmt.Errorf("系统处理中,请稍后重试!")
  640. return
  641. }
  642. classifyCondition := " AND source=? and is_selected=1 and resource_status=1 and level_path like '" + chartClassifyInfo.LevelPath + "%'"
  643. var classifyPars []interface{}
  644. classifyPars = append(classifyPars, chartClassifyInfo.Source)
  645. classifyList, err := data_manage.GetChartClassifyListByCondition(classifyCondition, classifyPars)
  646. if err != nil {
  647. return
  648. }
  649. chartClassifyIdList := make([]string, 0)
  650. for _, v := range classifyList {
  651. chartClassifyIdList = append(chartClassifyIdList, strconv.Itoa(v.ChartClassifyId))
  652. }
  653. // 批量上传图表信息
  654. condition := " and source=1 and chart_classify_id in ?"
  655. // 查询需要更新的图表信息总数
  656. total, err := data_manage.GetChartInfoCountByCondition(condition, []interface{}{chartClassifyIdList})
  657. if err != nil {
  658. return
  659. }
  660. if total == 0 {
  661. return
  662. }
  663. // 批量上传图表信息
  664. offset := 0
  665. pageSize := 100
  666. success := 0
  667. // 循环更新100个图表数据
  668. for i := 0; offset < total; i++ {
  669. // 查询需要更新的图表信息
  670. chartInfos, e := data_manage.GetChartInfoListByCondition(condition, []interface{}{chartClassifyIdList}, offset, pageSize)
  671. if e != nil {
  672. err = fmt.Errorf("查询需要更新的图表信息失败: %v", e)
  673. return
  674. }
  675. // 循环更新图表数据
  676. for _, chartInfo := range chartInfos {
  677. var er error
  678. var msg string
  679. if chartInfo.ForumChartInfoId == 0 {
  680. // 更新图表数据
  681. er, msg = UploadChart(chartInfo.ChartInfoId, "", sysUser)
  682. }else if chartInfo.ForumChartInfoId > 0 {
  683. // 更新图表数据
  684. er, msg = UpdateChart(chartInfo.ChartInfoId)
  685. }
  686. if er != nil {
  687. er = fmt.Errorf("图表ID %d, 更新图表数据失败: %s, %v", chartInfo.ChartInfoId, msg, er)
  688. tmpErr = append(tmpErr, er)
  689. continue
  690. }
  691. success += 1
  692. }
  693. offset = (i + 1) * pageSize
  694. }
  695. fmt.Println("更新图表数据完成, 更新图表数据总数:", success)
  696. return
  697. }
  698. // 批量删除资源库图表信息
  699. func ChartInfoDeleteBatch(chartClassifyInfo *data_manage.ChartClassify, sysUser *system.Admin) (err error) {
  700. if utils.BusinessCode == "" || (utils.BusinessCode != utils.BusinessCodeRelease && utils.BusinessCode != utils.BusinessCodeDebug && utils.BusinessCode != utils.BusinessCodeSandbox) {
  701. return
  702. }
  703. var tmpErr []error
  704. deleteCache := true
  705. cacheKey := "eta_forum_task:ChartInfoDeleteBatch:" + strconv.Itoa(chartClassifyInfo.ChartClassifyId)
  706. defer func() {
  707. if deleteCache {
  708. _ = utils.Rc.Delete(cacheKey)
  709. }
  710. stack := ""
  711. if err != nil {
  712. stack = fmt.Sprintln(stack + err.Error())
  713. }
  714. if len(tmpErr) > 0 {
  715. for _, v := range tmpErr {
  716. stack = fmt.Sprintln(stack + v.Error())
  717. }
  718. }
  719. if stack != "" {
  720. utils.FileLog.Error("批量删除资源库图表信息失败"+"<br/>"+stack)
  721. go alarm_msg.SendAlarmMsg("批量删除资源库图表信息失败"+"<br/>"+stack, 3)
  722. }
  723. }()
  724. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  725. deleteCache = false
  726. err = fmt.Errorf("系统处理中,请稍后重试!")
  727. return
  728. }
  729. // 查询所有子分类
  730. chartClassifyInfoList, err := data_manage.GetChartClassifyByLevelPath(chartClassifyInfo.LevelPath)
  731. if err != nil {
  732. return
  733. }
  734. chartClassifyIdList := make([]string, 0)
  735. for _, v := range chartClassifyInfoList {
  736. chartClassifyIdList = append(chartClassifyIdList, strconv.Itoa(v.ChartClassifyId))
  737. }
  738. // 批量删除图表信息
  739. condition := "source=? and chart_classify_id in ?"
  740. total, err := data_manage.GetChartInfoCountByCondition(condition, []interface{}{chartClassifyInfo.Source, chartClassifyIdList})
  741. if err != nil {
  742. return
  743. }
  744. if total == 0 {
  745. return
  746. }
  747. offset := 0
  748. pageSize := 100
  749. success := 0
  750. // 循环更新100个图表数据
  751. for i := 0; offset < total; i++ {
  752. // 查询需要更新的图表信息
  753. chartInfos, e := data_manage.GetChartInfoListByCondition(condition, []interface{}{chartClassifyInfo.Source, chartClassifyIdList}, offset, pageSize)
  754. if e != nil {
  755. err = fmt.Errorf("查询需要更新的图表信息失败: %v", e)
  756. return
  757. }
  758. // 循环更新图表数据
  759. for _, chartInfo := range chartInfos {
  760. // 删除图表数据
  761. er, msg := DeleteChart(chartInfo.ChartInfoId)
  762. if er != nil {
  763. er = fmt.Errorf("图表ID %d, 删除图表数据失败: %s, %v", chartInfo.ChartInfoId, msg, er)
  764. tmpErr = append(tmpErr, er)
  765. continue
  766. }
  767. success += 1
  768. }
  769. offset = (i + 1) * pageSize
  770. }
  771. fmt.Println("删除图表数据完成, 删除图表数据总数:", success)
  772. return
  773. }
  774. // 修改分类状态,一键上架至资源库
  775. func SetChartClassifyResourceStatusUp(chartClassifyInfo *data_manage.ChartClassify, sysUser *system.Admin) (errMsg string, err error) {
  776. if chartClassifyInfo.ResourceStatus == utils.ChartClassifyResourceStatusUp {
  777. errMsg = "分类状态已为已上架, 无需重复操作"
  778. err = fmt.Errorf("%s", errMsg)
  779. return
  780. }
  781. // 判断是否是精选资源
  782. if chartClassifyInfo.IsSelected != utils.ChartClassifyIsSelected {
  783. errMsg = "分类状态已为已上架, 无需重复操作"
  784. err = fmt.Errorf("%s", errMsg)
  785. return
  786. }
  787. // 查询分类下的所有图表信息
  788. err = data_manage.UpdateChartClassifyResourceStatus(chartClassifyInfo.Source, utils.ChartClassifyResourceStatusUp, chartClassifyInfo.LevelPath)
  789. if err != nil {
  790. errMsg = "更新分类状态失败"
  791. err = fmt.Errorf("更新分类状态失败,Err:%w", err)
  792. return
  793. }
  794. go ChartInfoSaveBatch(chartClassifyInfo, sysUser)
  795. return
  796. }
  797. // 修改分类,一键从资源库下架
  798. func SetChartClassifyResourceStatusDown(chartClassifyInfo *data_manage.ChartClassify, sysUser *system.Admin) (errMsg string, err error) {
  799. if chartClassifyInfo.ResourceStatus == utils.ChartClassifyResourceStatusDown {
  800. errMsg = "分类状态已为已下架, 无需重复操作"
  801. err = fmt.Errorf("%s", errMsg)
  802. return
  803. }
  804. // 查询分类下的所有图表信息
  805. err = data_manage.UpdateChartClassifyResourceStatus(chartClassifyInfo.Source, utils.ChartClassifyResourceStatusDown, chartClassifyInfo.LevelPath)
  806. if err != nil {
  807. errMsg = "更新分类状态失败"
  808. err = fmt.Errorf("更新分类状态失败,Err:%w", err)
  809. return
  810. }
  811. go ChartInfoDeleteBatch(chartClassifyInfo, sysUser)
  812. return
  813. }