eta_forum_hub.go 31 KB

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