sci_hq_data.go 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage"
  7. "eta/eta_api/models/data_manage/request"
  8. "eta/eta_api/models/data_manage/response"
  9. "eta/eta_api/models/system"
  10. "eta/eta_api/services/data"
  11. "eta/eta_api/services/data_stat"
  12. etaTrialService "eta/eta_api/services/eta_trial"
  13. "eta/eta_api/utils"
  14. "fmt"
  15. "os"
  16. "path/filepath"
  17. "strconv"
  18. "strings"
  19. "time"
  20. "github.com/rdlucklib/rdluck_tools/paging"
  21. "github.com/tealeg/xlsx"
  22. )
  23. type SciHqDataController struct {
  24. controllers.BaseAuthController
  25. }
  26. // @Title 卓创红期数据分类
  27. // @Description 卓创红期数据分类接口
  28. // @Success 200 {object} data_manage.SciClassify
  29. // @router /sci_hq/classify [get]
  30. func (this *SciHqDataController) Classify() {
  31. br := new(models.BaseResponse).Init()
  32. defer func() {
  33. this.Data["json"] = br
  34. this.ServeJSON()
  35. }()
  36. classifyList, err := data_manage.GetSciHqClassifyAll()
  37. if err != nil {
  38. br.Msg = "查询失败"
  39. br.ErrMsg = "查询失败, Err:" + err.Error()
  40. return
  41. }
  42. initClassify := &data_manage.BaseFromSciHqClassifyItem{
  43. ClassifyId: 0,
  44. ClassifyName: "未分类",
  45. ClassifyNameEn: "Unclassified",
  46. UniqueCode: "0",
  47. ParentId: 0,
  48. Level: 1,
  49. Sort: 0,
  50. Children: nil,
  51. }
  52. finalList := make([]*data_manage.BaseFromSciHqClassifyItem, 0)
  53. classifyTree := getSciHqClassifyTree(classifyList, 0)
  54. finalList = append(finalList, initClassify)
  55. finalList = append(finalList, classifyTree...)
  56. br.Msg = "查询成功"
  57. br.Data = finalList
  58. br.Success = true
  59. br.Ret = 200
  60. }
  61. // AddSciClassify
  62. // @Title 新增分类
  63. // @Description 新增分类接口
  64. // @Param request body data_manage.AddBaseFromSciClassifyReq true "type json string"
  65. // @Success 200 Ret=200 保存成功
  66. // @router /sci_hq/classify/add [post]
  67. func (this *SciHqDataController) AddClassify() {
  68. br := new(models.BaseResponse).Init()
  69. defer func() {
  70. this.Data["json"] = br
  71. this.ServeJSON()
  72. }()
  73. var req request.AddBaseFromSciHqClassifyReq
  74. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  75. if err != nil {
  76. br.Msg = "参数解析异常!"
  77. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  78. return
  79. }
  80. if req.ClassifyName == "" {
  81. br.Msg = "请输入分类名称"
  82. return
  83. }
  84. if req.ParentId < 0 {
  85. br.Msg = "操作异常"
  86. return
  87. }
  88. ok, msg, err := data.AddSciHqClassify(req.ClassifyName, this.Lang, req.ParentId)
  89. if err != nil {
  90. br.Msg = "添加失败"
  91. br.ErrMsg = "添加失败,Err:" + err.Error()
  92. return
  93. }
  94. if !ok {
  95. if msg != "" {
  96. br.Msg = msg
  97. return
  98. }
  99. br.Msg = "添加失败"
  100. return
  101. }
  102. br.Msg = "添加成功"
  103. br.Success = true
  104. br.Ret = 200
  105. }
  106. // DelClassify
  107. // @Title 新增分类
  108. // @Description 新增分类接口
  109. // @Param request body data_manage.AddBaseFromSciClassifyReq true "type json string"
  110. // @Success 200 Ret=200 保存成功
  111. // @router /sci_hq/classify/del [post]
  112. func (this *SciHqDataController) DelClassify() {
  113. br := new(models.BaseResponse).Init()
  114. defer func() {
  115. this.Data["json"] = br
  116. this.ServeJSON()
  117. }()
  118. var req request.DelBaseFromSciHqClassifyReq
  119. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  120. if err != nil {
  121. br.Msg = "参数解析异常!"
  122. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  123. return
  124. }
  125. if req.ClassifyId < 0 {
  126. br.Msg = "参数错误"
  127. br.IsSendEmail = false
  128. return
  129. }
  130. err = data.DelSciHqClassify(req.ClassifyId)
  131. if err != nil {
  132. br.Msg = "删除失败"
  133. br.ErrMsg = "删除失败,Err:" + err.Error()
  134. return
  135. }
  136. br.Ret = 200
  137. br.Msg = "删除成功"
  138. br.Success = true
  139. br.IsAddLog = true
  140. }
  141. // EditClassify
  142. // @Title 修改分类
  143. // @Description 修改分类接口
  144. // @Param request body data_manage.EditBaseFromMysteelChemicalClassifyReq true "type json string"
  145. // @Success 200 Ret=200 修改成功
  146. // @router /sci_hq/classify/edit [post]
  147. func (this *SciHqDataController) EditClassify() {
  148. br := new(models.BaseResponse).Init()
  149. defer func() {
  150. this.Data["json"] = br
  151. this.ServeJSON()
  152. }()
  153. var req request.EditBaseFromSciHqClassifyReq
  154. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  155. if err != nil {
  156. br.Msg = "参数解析异常!"
  157. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  158. return
  159. }
  160. if req.ClassifyId <= 0 {
  161. br.Msg = "参数错误"
  162. return
  163. }
  164. if req.ClassifyName == "" {
  165. br.Msg = "请输入分类名称"
  166. return
  167. }
  168. classify, err := data_manage.GetSciHqClassifyById(req.ClassifyId)
  169. if err != nil {
  170. if utils.IsErrNoRow(err) {
  171. br.Msg = "分类不存在"
  172. return
  173. }
  174. br.Msg = "编辑失败"
  175. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  176. return
  177. }
  178. switch this.Lang {
  179. case utils.EnLangVersion:
  180. if classify.ClassifyNameEn != req.ClassifyName {
  181. count, err := data_manage.GetSciHqClassifyCountByName(req.ClassifyName, this.Lang)
  182. if err != nil {
  183. br.Msg = "编辑失败"
  184. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  185. return
  186. }
  187. if count > 0 {
  188. br.Msg = "分类名称已存在"
  189. return
  190. }
  191. classify.ClassifyNameEn = req.ClassifyName
  192. classify.ModifyTime = time.Now()
  193. err = classify.Update([]string{"classify_name_en", "modify_time"})
  194. if err != nil {
  195. br.Msg = "编辑失败"
  196. br.ErrMsg = "编辑失败,Err:" + err.Error()
  197. return
  198. }
  199. }
  200. default:
  201. if classify.ClassifyName != req.ClassifyName {
  202. count, err := data_manage.GetSciHqClassifyCountByName(req.ClassifyName, this.Lang)
  203. if err != nil {
  204. br.Msg = "编辑失败"
  205. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  206. return
  207. }
  208. if count > 0 {
  209. br.Msg = "分类名称已存在"
  210. return
  211. }
  212. classify.ClassifyName = req.ClassifyName
  213. classify.ModifyTime = time.Now()
  214. err = classify.Update([]string{"classify_name", "modify_time"})
  215. if err != nil {
  216. br.Msg = "编辑失败"
  217. br.ErrMsg = "编辑失败,Err:" + err.Error()
  218. return
  219. }
  220. }
  221. }
  222. br.Msg = "编辑成功"
  223. br.Success = true
  224. br.Ret = 200
  225. br.IsAddLog = true
  226. }
  227. // MoveClassify
  228. // @Title 分类移动接口
  229. // @Description 分类移动接口
  230. // @Success 200 {object} data_manage.MoveBaseFromMysteelChemicalClassifyReq
  231. // @router /sci_hq/classify/move [post]
  232. func (this *SciHqDataController) MoveClassify() {
  233. br := new(models.BaseResponse).Init()
  234. defer func() {
  235. this.Data["json"] = br
  236. this.ServeJSON()
  237. }()
  238. var req request.MoveBaseFromSciHqClassifyReq
  239. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  240. br.Msg = "参数解析异常!"
  241. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  242. return
  243. }
  244. if req.ClassifyId <= 0 {
  245. br.Msg = "参数错误"
  246. br.ErrMsg = "分类id小于等于0"
  247. br.IsSendEmail = false
  248. return
  249. }
  250. err, errMsg := data.MoveSciHqClassify(req.ClassifyId, req.ParentClassifyId, req.PrevClassifyId, req.NextClassifyId)
  251. if errMsg != `` {
  252. br.Msg = errMsg
  253. br.ErrMsg = errMsg
  254. if err != nil {
  255. br.ErrMsg = err.Error()
  256. } else {
  257. br.IsSendEmail = false
  258. }
  259. return
  260. }
  261. br.Ret = 200
  262. br.Success = true
  263. br.IsAddLog = true
  264. br.Msg = "移动成功"
  265. }
  266. // IndexList
  267. // @Title 卓创红期指标列表
  268. // @Description 卓创红期数据指标列表接口
  269. // @Param ClassifyId query int true "分类id"
  270. // @Success 200 {object} data_manage.BaseFromMysteelChemicalIndexResp
  271. // @router /sci_hq/index/list [get]
  272. func (this *SciHqDataController) IndexList() {
  273. br := new(models.BaseResponse).Init()
  274. defer func() {
  275. this.Data["json"] = br
  276. this.ServeJSON()
  277. }()
  278. classifyId, _ := this.GetInt("ClassifyId", 0)
  279. indexList, err := data_manage.GetSciHqIndexBaseInfoByClassifyId(classifyId)
  280. if err != nil && !utils.IsErrNoRow(err) {
  281. br.Msg = "获取失败"
  282. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  283. return
  284. }
  285. br.Ret = 200
  286. br.Success = true
  287. br.Msg = "获取成功"
  288. br.Data = indexList
  289. }
  290. // BatchIndexList
  291. // @Title 卓创红期批量操作指标列表
  292. // @Description 卓创红期批量操作指标列表
  293. // @Param request body request.SciHqDataBatchListReq true "type json string"
  294. // @Success 200 {object} data_manage.BaseFromMysteelChemicalIndexResp
  295. // @router /sci_hq/index/batch_list [post]
  296. func (this *SciHqDataController) BatchIndexList() {
  297. br := new(models.BaseResponse).Init()
  298. defer func() {
  299. this.Data["json"] = br
  300. this.ServeJSON()
  301. }()
  302. var req request.SciHqDataBatchListReq
  303. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  304. br.Msg = "参数解析异常!"
  305. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  306. return
  307. }
  308. var condition string
  309. var pars []interface{}
  310. if req.KeyWord != "" {
  311. condition += `AND (index_code=? OR index_name=?)`
  312. pars = utils.GetLikeKeywordPars(pars, req.KeyWord, 2)
  313. }
  314. if req.IsSelectAll {
  315. if len(req.SelectedId) > 0 {
  316. condition += ` AND base_from_sci_hq_index_id NOT IN (?)`
  317. pars = append(pars, req.SelectedId)
  318. }
  319. } else {
  320. if len(req.SelectedId) > 0 {
  321. condition += ` AND base_from_sci_hq_index_id IN (?)`
  322. pars = append(pars, req.SelectedId)
  323. }
  324. }
  325. if req.ClassifyId < 0 {
  326. br.Msg = "分类选择错误"
  327. return
  328. }
  329. if req.ClassifyId > 0 {
  330. childClassify, err := data_manage.GetChildSciHqClassifyListById(req.ClassifyId)
  331. if err != nil {
  332. br.Msg = "下载失败"
  333. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  334. return
  335. }
  336. var childIds []int
  337. for _, v := range childClassify {
  338. childIds = append(childIds, v.ClassifyId)
  339. }
  340. if len(childIds) > 0 {
  341. childIds = append(childIds, req.ClassifyId)
  342. condition += `AND classify_id IN (?)`
  343. pars = append(pars, childIds)
  344. } else {
  345. condition += ` AND classify_id=?`
  346. pars = append(pars, req.ClassifyId)
  347. }
  348. } else {
  349. condition += ` AND classify_id=?`
  350. pars = append(pars, req.ClassifyId)
  351. }
  352. count, err := data_manage.GetSciHqIndexCountByCondition(condition, pars)
  353. if err != nil {
  354. br.Msg = "获取失败"
  355. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  356. return
  357. }
  358. MaxLen := 30
  359. if count > MaxLen {
  360. br.Msg = "批量操作数量不能超过" + strconv.Itoa(MaxLen)
  361. return
  362. }
  363. indexList, err := data_manage.GetSciHqIndexByCondition(condition, pars)
  364. if err != nil {
  365. br.Msg = "获取失败"
  366. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  367. return
  368. }
  369. br.Ret = 200
  370. br.Success = true
  371. br.Msg = "获取成功"
  372. br.Data = indexList
  373. }
  374. // IndexPageList
  375. // @Title 卓创红期指标列表
  376. // @Description 卓创红期数据指标列表接口
  377. // @Param PageSize query int true "每页数据条数"
  378. // @Param CurrentIndex query int true "当前页页码,从1开始"
  379. // @Param ClassifyId query int true "分类id"
  380. // @Param IsListAll query int true "是否展示全部"
  381. // @Success 200 {object} data_manage.BaseFromMysteelChemicalIndexResp
  382. // @router /sci_hq/index/page/list [get]
  383. func (this *SciHqDataController) IndexPageList() {
  384. br := new(models.BaseResponse).Init()
  385. defer func() {
  386. this.Data["json"] = br
  387. this.ServeJSON()
  388. }()
  389. classifyId, _ := this.GetInt("ClassifyId", 0)
  390. pageSize, _ := this.GetInt("PageSize")
  391. currrentIndex, _ := this.GetInt("CurrentIndex")
  392. isListAll, _ := this.GetBool("IsListAll")
  393. var startSize int
  394. if pageSize <= 0 {
  395. pageSize = utils.PageSize20
  396. }
  397. if currrentIndex <= 0 {
  398. currrentIndex = 1
  399. }
  400. startSize = utils.StartIndex(currrentIndex, pageSize)
  401. var total int
  402. var indexList []*data_manage.BaseFromSciHqIndexView
  403. if isListAll {
  404. tmpTotal, err := data_manage.GetSciHqIndexCount()
  405. if err != nil {
  406. br.Msg = "获取失败"
  407. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  408. return
  409. }
  410. total = tmpTotal
  411. tmpIndexList, err := data_manage.GetSciHqIndexByPage(startSize, pageSize)
  412. if err != nil && !utils.IsErrNoRow(err) {
  413. br.Msg = "获取失败"
  414. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  415. return
  416. }
  417. indexList = tmpIndexList
  418. } else {
  419. var classifyIds []int
  420. if classifyId > 0 {
  421. tmpClassifyIds, err := data_manage.GetSciHqChildClassifyIdsById(classifyId)
  422. if err != nil {
  423. br.Msg = "获取失败"
  424. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  425. return
  426. }
  427. if len(tmpClassifyIds) > 0 {
  428. classifyIds = append(classifyIds, tmpClassifyIds...)
  429. }
  430. }
  431. classifyIds = append(classifyIds, classifyId)
  432. tmpTotal, err := data_manage.GetSciHqIndexCountByClassifyId(classifyIds)
  433. if err != nil {
  434. br.Msg = "获取失败"
  435. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  436. return
  437. }
  438. total = tmpTotal
  439. tmpIndexList, err := data_manage.GetSciHqIndexByClassifyId(classifyIds, startSize, pageSize)
  440. if err != nil && !utils.IsErrNoRow(err) {
  441. br.Msg = "获取失败"
  442. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  443. return
  444. }
  445. indexList = tmpIndexList
  446. }
  447. page := paging.GetPaging(currrentIndex, pageSize, total)
  448. resp := new(response.SciHqIndexPageListResp)
  449. resp.List = indexList
  450. resp.Paging = page
  451. br.Ret = 200
  452. br.Success = true
  453. br.Msg = "获取成功"
  454. br.Data = resp
  455. }
  456. // SearchList
  457. // @Title 卓创红期模糊搜索
  458. // @Description 卓创红期模糊搜索
  459. // @Param Keyword query string true "关键字搜索"
  460. // @Success 200 {object} models.BaseResponse
  461. // @router /sci_hq/search_list [get]
  462. func (this *SciHqDataController) SearchList() {
  463. br := new(models.BaseResponse).Init()
  464. defer func() {
  465. this.Data["json"] = br
  466. this.ServeJSON()
  467. }()
  468. //关键字
  469. keyword := this.GetString("Keyword")
  470. var condition string
  471. var pars []interface{}
  472. if keyword != "" {
  473. condition += ` AND (index_code LIKE ? OR index_name LIKE ?)`
  474. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  475. } else {
  476. br.Msg = "请输入指标ID/指标名称"
  477. return
  478. }
  479. list, err := data_manage.GetSciHqIndexBaseInfoByCondition(condition, pars)
  480. if err != nil {
  481. br.Msg = "获取失败"
  482. br.ErrMsg = "获取失败,Err:" + err.Error()
  483. return
  484. }
  485. br.Ret = 200
  486. br.Success = true
  487. br.Msg = "获取成功"
  488. br.Data = list
  489. }
  490. // SingleData
  491. // @Title 获取卓创红期据
  492. // @Description 获取卓创红期单条数据接口
  493. // @Param BaseFromSciHqIndexId query string true "指标唯一编码"
  494. // @Success 200 {object} models.BaseResponse
  495. // @router /sci_hq/single_data [get]
  496. func (this *SciHqDataController) SingleData() {
  497. br := new(models.BaseResponse).Init()
  498. defer func() {
  499. this.Data["json"] = br
  500. this.ServeJSON()
  501. }()
  502. baseFromSciHqIndexId, _ := this.GetInt("BaseFromSciHqIndexId")
  503. indexInfo, err := data_manage.GetSciHqIndexByIndexId(baseFromSciHqIndexId)
  504. if err != nil {
  505. br.Msg = "获取指标信息失败"
  506. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  507. return
  508. }
  509. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_SCI_HQ, indexInfo.IndexCode)
  510. if err != nil && !utils.IsErrNoRow(err) {
  511. br.Msg = "获取指标数据失败"
  512. br.ErrMsg = "获取指标库数据失败,Err:" + err.Error()
  513. return
  514. }
  515. var edbInfoId int
  516. if edbInfo != nil {
  517. edbInfoId = edbInfo.EdbInfoId
  518. }
  519. dataList, err := data_manage.GetSciHqDataByIndexId(baseFromSciHqIndexId)
  520. if err != nil {
  521. br.Msg = "获取数据失败"
  522. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  523. return
  524. }
  525. var ret response.SciHqSingleDataResp
  526. ret.ClassifyId = indexInfo.ClassifyId
  527. ret.BaseFromSciHqIndexId = indexInfo.BaseFromSciHqIndexId
  528. ret.IndexCode = indexInfo.IndexCode
  529. ret.EdbInfoId = edbInfoId
  530. ret.IndexName = indexInfo.IndexName
  531. ret.Frequency = indexInfo.Frequency
  532. ret.CreateTime = indexInfo.CreateTime.Format(utils.FormatDateTime)
  533. ret.ModifyTime = indexInfo.ModifyTime.Format(utils.FormatDateTime)
  534. ret.Unit = indexInfo.Unit
  535. ret.Data = dataList
  536. br.Ret = 200
  537. br.Success = true
  538. br.Msg = "获取成功"
  539. br.Data = ret
  540. }
  541. // MoveSciHqData
  542. // @Title 卓创红期指标移动接口
  543. // @Description 卓创红期指标移动接口
  544. // @Success 200 {object} request.MoveBaseFromSciHqReq
  545. // @router /sci_hq/move [post]
  546. func (this *SciHqDataController) MoveSciHqData() {
  547. br := new(models.BaseResponse).Init()
  548. defer func() {
  549. this.Data["json"] = br
  550. this.ServeJSON()
  551. }()
  552. sysUser := this.SysUser
  553. if sysUser == nil {
  554. br.Msg = "请登录"
  555. br.ErrMsg = "请登录,SysUser Is Empty"
  556. br.Ret = 408
  557. return
  558. }
  559. var req request.MoveBaseFromSciHqReq
  560. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  561. if err != nil {
  562. br.Msg = "参数解析异常!"
  563. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  564. return
  565. }
  566. if req.BaseFromSciHqIndexId <= 0 {
  567. br.Msg = "参数错误"
  568. br.ErrMsg = "指标id小于等于0"
  569. br.IsSendEmail = false
  570. return
  571. }
  572. if req.ClassifyId < 0 {
  573. br.Msg = "请选择分类"
  574. br.ErrMsg = "请选择分类"
  575. br.IsSendEmail = false
  576. return
  577. }
  578. err, errMsg := data.MoveSciHqData(req.BaseFromSciHqIndexId, req.ClassifyId, req.PrevBaseFromSciHqIndexId, req.NextBaseFromSciHqIndexId)
  579. if errMsg != `` {
  580. br.Msg = errMsg
  581. br.ErrMsg = errMsg
  582. if err != nil {
  583. br.ErrMsg = err.Error()
  584. } else {
  585. br.IsSendEmail = false
  586. }
  587. return
  588. }
  589. br.Ret = 200
  590. br.Success = true
  591. br.IsAddLog = true
  592. br.Msg = "移动成功"
  593. }
  594. // ResetSciHqIndex
  595. // @Title 指标数据清除分类
  596. // @Description 指标数据清除分类
  597. // @Param request body data_manage.DelBaseFromSciReq true "type json string"
  598. // @Success 200 Ret=200 操作成功
  599. // @router /sci_hq/reset [post]
  600. func (this *SciHqDataController) ResetSciHqIndex() {
  601. br := new(models.BaseResponse).Init()
  602. defer func() {
  603. this.Data["json"] = br
  604. this.ServeJSON()
  605. }()
  606. var req request.ResetBaseFromSciHqReq
  607. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  608. if err != nil {
  609. br.Msg = "参数解析异常!"
  610. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  611. return
  612. }
  613. if req.BaseFromSciHqIndexId < 0 {
  614. br.Msg = "参数错误"
  615. br.IsSendEmail = false
  616. return
  617. }
  618. err = data.ResetSciHqIndex(req.BaseFromSciHqIndexId)
  619. if err != nil {
  620. br.Msg = "移动失败"
  621. br.ErrMsg = "移动失败,Err:" + err.Error()
  622. return
  623. }
  624. br.Ret = 200
  625. br.Msg = "操作成功"
  626. br.Success = true
  627. br.IsAddLog = true
  628. }
  629. // AddEdbInfo
  630. // @Title 新增指标接口
  631. // @Description 新增指标接口
  632. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  633. // @Success Ret=200 保存成功
  634. // @router /sci_hq/edb_info/add [post]
  635. func (this *SciHqDataController) AddEdbInfo() {
  636. br := new(models.BaseResponse).Init()
  637. defer func() {
  638. this.Data["json"] = br
  639. this.ServeJSON()
  640. }()
  641. sysUser := this.SysUser
  642. if sysUser == nil {
  643. br.Msg = "请登录"
  644. br.ErrMsg = "请登录,SysUser Is Empty"
  645. br.Ret = 408
  646. return
  647. }
  648. deleteCache := true
  649. cacheKey := "CACHE_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  650. defer func() {
  651. if deleteCache {
  652. utils.Rc.Delete(cacheKey)
  653. }
  654. }()
  655. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  656. deleteCache = false
  657. br.Msg = "系统处理中,请稍后重试!"
  658. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  659. return
  660. }
  661. var req data_manage.AddEdbInfoReq
  662. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  663. if err != nil {
  664. br.Msg = "参数解析异常!"
  665. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  666. return
  667. }
  668. req.EdbName = strings.Trim(req.EdbName, " ")
  669. req.EdbCode = strings.Trim(req.EdbCode, " ")
  670. if req.EdbCode == "" {
  671. br.Msg = "指标ID不能为空"
  672. return
  673. }
  674. if req.EdbName == "" {
  675. br.Msg = "指标名称不能为空"
  676. return
  677. }
  678. if req.Frequency == "" {
  679. br.Msg = "频率不能为空"
  680. return
  681. }
  682. if req.Unit == "" {
  683. br.Msg = "单位不能为空"
  684. return
  685. }
  686. if req.ClassifyId <= 0 {
  687. br.Msg = "请选择分类"
  688. return
  689. }
  690. _, err = data_manage.GetSciHqIndexByIndexCode(req.EdbCode)
  691. if err != nil {
  692. if utils.IsErrNoRow(err) {
  693. br.Msg = "指标不存在"
  694. return
  695. }
  696. br.Msg = "获取失败"
  697. br.ErrMsg = "获取失败,Err:" + err.Error()
  698. return
  699. }
  700. source := utils.DATA_SOURCE_SCI_HQ
  701. // 指标入库
  702. edbInfo, err, errMsg, isSendEmail := data.EdbInfoAdd(source, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, req.StartDate, req.EndDate, sysUser.AdminId, sysUser.RealName, this.Lang)
  703. if err != nil {
  704. br.Msg = "保存失败"
  705. if errMsg != `` {
  706. br.Msg = errMsg
  707. }
  708. br.ErrMsg = err.Error()
  709. br.IsSendEmail = isSendEmail
  710. return
  711. }
  712. // 刷新指标数据
  713. refreshRes, e := data.RefreshEdbData(edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo.EdbCode, "")
  714. if e != nil {
  715. br.Msg = "操作失败"
  716. br.ErrMsg = fmt.Sprintf("刷新指标数据失败, %v", e)
  717. return
  718. }
  719. if refreshRes != nil && refreshRes.Ret != 200 {
  720. br.Msg = "操作失败"
  721. br.ErrMsg = fmt.Sprintf("刷新指标数据失败, Ret: %d, Msg: %s, ErrMsg: %s", refreshRes.Ret, refreshRes.Msg, refreshRes.ErrMsg)
  722. return
  723. }
  724. // 试用平台更新用户累计新增指标数
  725. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  726. if e != nil {
  727. br.Msg = "操作失败"
  728. br.ErrMsg = "获取系统用户数据失败,Err:" + e.Error()
  729. return
  730. }
  731. if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
  732. go func() {
  733. var r etaTrialService.EtaTrialUserReq
  734. r.Mobile = adminItem.Mobile
  735. _, _ = etaTrialService.UpdateUserIndexNum(r)
  736. }()
  737. }
  738. //新增操作日志
  739. {
  740. // 添加钢联指标更新日志
  741. if edbInfo.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  742. go data_stat.AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", sysUser, 2)
  743. }
  744. edbLog := new(data_manage.EdbInfoLog)
  745. edbLog.EdbInfoId = edbInfo.EdbInfoId
  746. edbLog.SourceName = edbInfo.SourceName
  747. edbLog.Source = edbInfo.Source
  748. edbLog.EdbCode = edbInfo.EdbCode
  749. edbLog.EdbName = edbInfo.EdbName
  750. edbLog.ClassifyId = edbInfo.ClassifyId
  751. edbLog.SysUserId = sysUser.AdminId
  752. edbLog.SysUserRealName = sysUser.RealName
  753. edbLog.CreateTime = time.Now()
  754. edbLog.Content = string(this.Ctx.Input.RequestBody)
  755. edbLog.Status = "新增指标"
  756. edbLog.Method = this.Ctx.Input.URI()
  757. go data_manage.AddEdbInfoLog(edbLog)
  758. }
  759. // 更新es
  760. go data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  761. resp := new(data_manage.AddEdbInfoResp)
  762. resp.EdbInfoId = edbInfo.EdbInfoId
  763. resp.UniqueCode = edbInfo.UniqueCode
  764. br.Ret = 200
  765. br.Success = true
  766. br.Msg = "保存成功"
  767. br.Data = resp
  768. br.IsAddLog = true
  769. }
  770. // AddCheck
  771. // @Title 新增校验
  772. // @Description 新增校验
  773. // @Param request body request.BusinessDataBatchAddCheckReq true "type json string"
  774. // @Success 200 string "操作成功"
  775. // @router /sci_hq/edb_info/add_check [post]
  776. func (this *SciHqDataController) AddCheck() {
  777. br := new(models.BaseResponse).Init()
  778. defer func() {
  779. if br.ErrMsg == "" {
  780. br.IsSendEmail = false
  781. }
  782. this.Data["json"] = br
  783. this.ServeJSON()
  784. }()
  785. sysUser := this.SysUser
  786. if sysUser == nil {
  787. br.Msg = "请登录"
  788. br.ErrMsg = "请登录,SysUser Is Empty"
  789. br.Ret = 408
  790. return
  791. }
  792. var req *request.SciHqDataBatchAddCheckReq
  793. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  794. br.Msg = "参数解析异常!"
  795. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  796. return
  797. }
  798. codeMax := 30
  799. codeLen := len(req.IndexCodes)
  800. if len(req.IndexCodes) == 0 {
  801. br.Msg = "请选择指标"
  802. return
  803. }
  804. if codeLen > codeMax {
  805. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  806. return
  807. }
  808. // 获取指标库已有指标
  809. existsEdb, e := data_manage.GetEdbCodesBySource(utils.DATA_SOURCE_SCI_HQ)
  810. if e != nil {
  811. br.Msg = "获取失败"
  812. br.ErrMsg = "获取自有数据已添加的指标失败, Err: " + e.Error()
  813. return
  814. }
  815. existMap := make(map[string]*data_manage.EdbInfo)
  816. for _, v := range existsEdb {
  817. existMap[v.EdbCode] = v
  818. }
  819. // 查询选中的指标
  820. cond := fmt.Sprintf(` AND index_code IN (%s)`, utils.GetOrmInReplace(codeLen))
  821. pars := make([]interface{}, 0)
  822. pars = append(pars, req.IndexCodes)
  823. list, err := data_manage.GetSciHqIndexAndEdbInfoByCondition(cond, pars)
  824. if err != nil {
  825. br.Msg = "获取失败"
  826. br.ErrMsg = "获取卓创红期原始指标列表失败, Err: " + err.Error()
  827. return
  828. }
  829. resp := make([]*data_manage.BaseFromSciHqIndexView, 0)
  830. for _, v := range list {
  831. if v.EdbInfoId > 0 {
  832. v.EdbExist = 1
  833. }
  834. resp = append(resp, v)
  835. }
  836. br.Data = resp
  837. br.Msg = "校验成功"
  838. br.Ret = 200
  839. br.Success = true
  840. }
  841. // NameCheck
  842. // @Title 重名校验
  843. // @Description 批量新增
  844. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  845. // @Success 200 string "操作成功"
  846. // @router /sci_hq/edb_info/name_check [post]
  847. func (c *SciHqDataController) NameCheck() {
  848. br := new(models.BaseResponse).Init()
  849. defer func() {
  850. if br.ErrMsg == "" {
  851. br.IsSendEmail = false
  852. }
  853. c.Data["json"] = br
  854. c.ServeJSON()
  855. }()
  856. sysUser := c.SysUser
  857. if sysUser == nil {
  858. br.Msg = "请登录"
  859. br.ErrMsg = "请登录,SysUser Is Empty"
  860. br.Ret = 408
  861. return
  862. }
  863. var req []*data_manage.NameCheckEdbInfoReq
  864. if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
  865. br.Msg = "参数解析异常!"
  866. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  867. return
  868. }
  869. if len(req) == 0 {
  870. br.Msg = "请选择指标"
  871. return
  872. }
  873. codeMax := 30
  874. codeLen := len(req)
  875. if codeLen > codeMax {
  876. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  877. return
  878. }
  879. type NameCheckResult struct {
  880. EdbCode string
  881. EdbName string
  882. Exist bool
  883. }
  884. indexNames := make([]string, 0)
  885. resp := make([]*NameCheckResult, 0)
  886. for _, v := range req {
  887. v.EdbCode = strings.TrimSpace(v.EdbCode)
  888. if v.EdbCode == "" {
  889. br.Msg = "指标ID不可为空"
  890. return
  891. }
  892. v.EdbName = strings.TrimSpace(v.EdbName)
  893. if v.EdbName == "" {
  894. br.Msg = "请输入指标名称"
  895. return
  896. }
  897. indexNames = append(indexNames, v.EdbName)
  898. resp = append(resp, &NameCheckResult{
  899. EdbCode: v.EdbCode,
  900. EdbName: v.EdbName,
  901. })
  902. dataItems, err := data_manage.GetEdbDataAllByEdbCode(v.EdbCode, utils.DATA_SOURCE_SCI_HQ, 0, utils.EDB_DATA_LIMIT)
  903. if err != nil && !utils.IsErrNoRow(err) {
  904. br.Msg = "获取失败"
  905. br.ErrMsg = "获取卓创红期已存在信息失败,Err:" + err.Error()
  906. return
  907. }
  908. if len(dataItems) <= 0 {
  909. respItem, err := data.AddEdbData(utils.DATA_SOURCE_SCI_HQ, v.EdbCode, v.Frequency)
  910. if err != nil {
  911. br.Msg = "获取失败"
  912. br.ErrMsg = "获取失败,Err:" + err.Error()
  913. return
  914. }
  915. if respItem.Ret != 200 {
  916. br.Msg = "未搜索到该指标"
  917. br.ErrMsg = respItem.ErrMsg + ";EdbCode:" + v.EdbCode
  918. return
  919. }
  920. }
  921. }
  922. // 重名校验
  923. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  924. if e != nil {
  925. br.Msg = "操作失败"
  926. br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
  927. return
  928. }
  929. nameExists := make(map[string]bool)
  930. for _, v := range edbList {
  931. nameExists[v.EdbName] = true
  932. }
  933. if len(nameExists) > 0 {
  934. for _, v := range resp {
  935. v.Exist = nameExists[v.EdbName]
  936. }
  937. }
  938. br.Data = resp
  939. br.Msg = "校验成功"
  940. br.Ret = 200
  941. br.Success = true
  942. }
  943. // BatchAdd
  944. // @Title 批量新增
  945. // @Description 批量新增
  946. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  947. // @Success 200 string "操作成功"
  948. // @router /sci_hq/edb_info/batch_add [post]
  949. func (this *SciHqDataController) BatchAdd() {
  950. br := new(models.BaseResponse).Init()
  951. defer func() {
  952. if br.ErrMsg == "" {
  953. br.IsSendEmail = false
  954. }
  955. this.Data["json"] = br
  956. this.ServeJSON()
  957. }()
  958. sysUser := this.SysUser
  959. if sysUser == nil {
  960. br.Msg = "请登录"
  961. br.ErrMsg = "请登录,SysUser Is Empty"
  962. br.Ret = 408
  963. return
  964. }
  965. deleteCache := true
  966. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_SCI_HQ_" + strconv.Itoa(sysUser.AdminId)
  967. defer func() {
  968. if deleteCache {
  969. _ = utils.Rc.Delete(cacheKey)
  970. }
  971. }()
  972. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  973. deleteCache = false
  974. br.Msg = "系统处理中,请稍后重试!"
  975. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  976. return
  977. }
  978. var req []*data_manage.AddEdbInfoReq
  979. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  980. br.Msg = "参数解析异常!"
  981. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  982. return
  983. }
  984. if len(req) == 0 {
  985. br.Msg = "请选择指标"
  986. return
  987. }
  988. if len(req) > 30 {
  989. br.Msg = "批量添加指标数量不得超过30个"
  990. return
  991. }
  992. for _, v := range req {
  993. v.EdbCode = strings.TrimSpace(v.EdbCode)
  994. if v.EdbCode == "" {
  995. br.Msg = "指标ID不可为空"
  996. return
  997. }
  998. v.EdbName = strings.TrimSpace(v.EdbName)
  999. if v.EdbName == "" {
  1000. br.Msg = "请输入指标名称"
  1001. return
  1002. }
  1003. v.Frequency = strings.TrimSpace(v.Frequency)
  1004. if v.Frequency == "" {
  1005. br.Msg = "请选择频度"
  1006. return
  1007. }
  1008. v.Unit = strings.TrimSpace(v.Unit)
  1009. if v.Unit == "" {
  1010. br.Msg = "请输入单位"
  1011. return
  1012. }
  1013. if v.ClassifyId <= 0 {
  1014. br.Msg = "请选择分类"
  1015. return
  1016. }
  1017. }
  1018. // 限定同一时间最多批量新增30个指标
  1019. for _, v := range req {
  1020. var r data.SciHqIndexSource2EdbReq
  1021. r.EdbCode = v.EdbCode
  1022. r.EdbName = v.EdbName
  1023. r.Frequency = v.Frequency
  1024. r.Unit = v.Unit
  1025. r.ClassifyId = v.ClassifyId
  1026. r.AdminId = sysUser.AdminId
  1027. r.AdminRealName = sysUser.RealName
  1028. edbInfo, errMsg, skip, e := data.SciHqIndexSource2Edb(r, this.Lang)
  1029. if e != nil {
  1030. br.Msg = "操作失败"
  1031. if errMsg != "" {
  1032. br.Msg = errMsg
  1033. }
  1034. br.ErrMsg = e.Error()
  1035. return
  1036. }
  1037. if skip {
  1038. continue
  1039. }
  1040. // 刷新指标数据
  1041. refreshRes, e := data.RefreshEdbData(edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo.EdbCode, "")
  1042. if e != nil {
  1043. br.Msg = "操作失败"
  1044. br.ErrMsg = fmt.Sprintf("刷新指标数据失败, %v", e)
  1045. return
  1046. }
  1047. if refreshRes != nil && refreshRes.Ret != 200 {
  1048. br.Msg = "操作失败"
  1049. br.ErrMsg = fmt.Sprintf("刷新指标数据失败, Ret: %d, Msg: %s, ErrMsg: %s", refreshRes.Ret, refreshRes.Msg, refreshRes.ErrMsg)
  1050. return
  1051. }
  1052. // 试用平台更新用户累计新增指标数
  1053. if utils.BusinessCode == utils.BusinessCodeSandbox {
  1054. go func() {
  1055. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  1056. if e != nil {
  1057. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  1058. utils.FileLog.Info(tips)
  1059. return
  1060. }
  1061. if adminItem.DepartmentName != "ETA试用客户" {
  1062. return
  1063. }
  1064. var ur etaTrialService.EtaTrialUserReq
  1065. ur.Mobile = adminItem.Mobile
  1066. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  1067. }()
  1068. }
  1069. // 新增操作日志
  1070. {
  1071. edbLog := new(data_manage.EdbInfoLog)
  1072. edbLog.EdbInfoId = edbInfo.EdbInfoId
  1073. edbLog.SourceName = edbInfo.SourceName
  1074. edbLog.Source = edbInfo.Source
  1075. edbLog.EdbCode = edbInfo.EdbCode
  1076. edbLog.EdbName = edbInfo.EdbName
  1077. edbLog.ClassifyId = edbInfo.ClassifyId
  1078. edbLog.SysUserId = sysUser.AdminId
  1079. edbLog.SysUserRealName = sysUser.RealName
  1080. edbLog.CreateTime = time.Now()
  1081. edbLog.Content = string(this.Ctx.Input.RequestBody)
  1082. edbLog.Status = "新增指标"
  1083. edbLog.Method = this.Ctx.Input.URI()
  1084. go data_manage.AddEdbInfoLog(edbLog)
  1085. }
  1086. }
  1087. br.Msg = "操作成功"
  1088. br.Ret = 200
  1089. br.Success = true
  1090. br.IsAddLog = true
  1091. }
  1092. // BatchDelete
  1093. // @Title 批量删除
  1094. // @Description 批量删除
  1095. // @Param request body []*request.DelBaseFromSciHqReq true "type json string"
  1096. // @Success 200 string "操作成功"
  1097. // @router /sci_hq/edb_info/batch_delete [post]
  1098. func (this *SciHqDataController) BatchDelete() {
  1099. br := new(models.BaseResponse).Init()
  1100. defer func() {
  1101. if br.ErrMsg == "" {
  1102. br.IsSendEmail = false
  1103. }
  1104. this.Data["json"] = br
  1105. this.ServeJSON()
  1106. }()
  1107. var req []*request.DelBaseFromSciHqReq
  1108. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1109. br.Msg = "参数解析异常!"
  1110. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  1111. return
  1112. }
  1113. if len(req) == 0 {
  1114. br.Msg = "请选择指标"
  1115. return
  1116. }
  1117. if len(req) > 30 {
  1118. br.Msg = "批量添加指标数量不得超过30个"
  1119. return
  1120. }
  1121. var deleteIds []int
  1122. for _, v := range req {
  1123. if v.BaseFromSciHqIndexId <= 0 {
  1124. continue
  1125. }
  1126. deleteIds = append(deleteIds, v.BaseFromSciHqIndexId)
  1127. }
  1128. existList, err := data.BatchDelSciHqData(deleteIds)
  1129. if err != nil {
  1130. br.Msg = "删除失败"
  1131. br.ErrMsg = "删除失败,Err:" + err.Error()
  1132. return
  1133. }
  1134. br.Data = existList
  1135. br.Msg = "操作成功"
  1136. br.Ret = 200
  1137. br.Success = true
  1138. br.IsAddLog = true
  1139. }
  1140. // BatchEdit
  1141. // @Title 批量编辑
  1142. // @Description 批量编辑
  1143. // @Param request body []*request.DelBaseFromSciHqReq true "type json string"
  1144. // @Success 200 string "操作成功"
  1145. // @router /sci_hq/edb_info/batch_edit [post]
  1146. func (this *SciHqDataController) BatchEdit() {
  1147. br := new(models.BaseResponse).Init()
  1148. defer func() {
  1149. if br.ErrMsg == "" {
  1150. br.IsSendEmail = false
  1151. }
  1152. this.Data["json"] = br
  1153. this.ServeJSON()
  1154. }()
  1155. var req []*request.EditBaseFromSciHqReq
  1156. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1157. br.Msg = "参数解析异常!"
  1158. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  1159. return
  1160. }
  1161. if len(req) == 0 {
  1162. br.Msg = "请选择指标"
  1163. return
  1164. }
  1165. indexIds := make([]int, 0)
  1166. classifyIds := make([]int, 0)
  1167. indexIdToclassifyId := make(map[int]int)
  1168. for _, v := range req {
  1169. if v.BaseFromSciHqIndexId <= 0 {
  1170. continue
  1171. }
  1172. if v.ClassifyId <= 0 {
  1173. continue
  1174. }
  1175. indexIds = append(indexIds, v.BaseFromSciHqIndexId)
  1176. classifyIds = append(classifyIds, v.ClassifyId)
  1177. indexIdToclassifyId[v.BaseFromSciHqIndexId] = v.ClassifyId
  1178. }
  1179. if len(indexIds) == 0 {
  1180. br.Msg = "请选择指标或指定正确的分类"
  1181. return
  1182. }
  1183. indexList, err := data_manage.GetSciHqIndexListByIndexIds(indexIds)
  1184. if err != nil {
  1185. br.Msg = "编辑失败"
  1186. br.ErrMsg = "获取指标失败,Err:" + err.Error()
  1187. return
  1188. }
  1189. classifySort, err := data_manage.GetSciHqClassifyMaxSortByClassifyIds(classifyIds)
  1190. if err != nil {
  1191. br.Msg = "编辑失败"
  1192. br.ErrMsg = "获取分类最大排序失败,Err:" + err.Error()
  1193. return
  1194. }
  1195. classifySortMap := make(map[int]int)
  1196. for _, v := range classifySort {
  1197. classifySortMap[v.ClassifyId] = v.MaxSort
  1198. }
  1199. classifyList, err := data_manage.GetSciHqClassifyListByIds(classifyIds)
  1200. if err != nil {
  1201. br.Msg = "编辑失败"
  1202. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  1203. return
  1204. }
  1205. existClassifyList := make(map[int]struct{})
  1206. for _, v := range classifyList {
  1207. existClassifyList[v.ClassifyId] = struct{}{}
  1208. }
  1209. //编辑指标
  1210. updateIndexList := make([]*data_manage.BaseFromSciHqIndex, 0)
  1211. for _, v := range indexList {
  1212. tmpClassifyId := indexIdToclassifyId[v.BaseFromSciHqIndexId]
  1213. if _, ok := existClassifyList[tmpClassifyId]; ok {
  1214. v.ClassifyId = tmpClassifyId
  1215. v.Sort = classifySortMap[tmpClassifyId] + 1
  1216. classifySortMap[tmpClassifyId] += 1
  1217. updateIndexList = append(updateIndexList, v)
  1218. }
  1219. }
  1220. err = data_manage.BatchModifySciHqIndexClassify(updateIndexList)
  1221. if err != nil {
  1222. br.Msg = "编辑失败"
  1223. br.ErrMsg = "编辑指标失败,Err:" + err.Error()
  1224. return
  1225. }
  1226. br.Msg = "操作成功"
  1227. br.Ret = 200
  1228. br.Success = true
  1229. br.IsAddLog = true
  1230. }
  1231. // EditSciHq
  1232. // @Title 编辑卓创红期指标
  1233. // @Description 编辑卓创红期指标接口
  1234. // @Param request body data_manage.AddEdbClassifyReq true "type json string"
  1235. // @Success 200 Ret=200 保存成功
  1236. // @router /sci_hq/edit [post]
  1237. func (this *SciHqDataController) EditSciHq() {
  1238. br := new(models.BaseResponse).Init()
  1239. defer func() {
  1240. this.Data["json"] = br
  1241. this.ServeJSON()
  1242. }()
  1243. var req request.EditBaseFromSciHqReq
  1244. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1245. if err != nil {
  1246. br.Msg = "参数解析异常!"
  1247. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1248. return
  1249. }
  1250. if req.BaseFromSciHqIndexId <= 0 {
  1251. br.Msg = "请选择指标"
  1252. br.IsSendEmail = false
  1253. return
  1254. }
  1255. if req.ClassifyId <= 0 {
  1256. br.Msg = "请选择分类"
  1257. br.IsSendEmail = false
  1258. return
  1259. }
  1260. //编辑指标
  1261. sciIndexInfo, errMsg, err := data.EditSciHqIndex(req.BaseFromSciHqIndexId, req.ClassifyId)
  1262. if errMsg != `` {
  1263. br.Msg = errMsg
  1264. return
  1265. }
  1266. if err != nil {
  1267. br.Msg = "编辑失败"
  1268. br.ErrMsg = "编辑指标失败,Err:" + err.Error()
  1269. return
  1270. }
  1271. resp := response.EditSciHqIndexInfoResp{
  1272. BaseFromSciHqIndexId: sciIndexInfo.BaseFromSciHqIndexId,
  1273. IndexCode: sciIndexInfo.IndexCode,
  1274. }
  1275. br.Data = resp
  1276. br.Ret = 200
  1277. br.Msg = "保存成功"
  1278. br.Success = true
  1279. br.IsAddLog = true
  1280. }
  1281. // DeleteSciHqData
  1282. // @Title 删除指标
  1283. // @Description 删除指标接口
  1284. // @Param request body data_manage.DelBaseFromSciReq true "type json string"
  1285. // @Success 200 Ret=200 删除成功
  1286. // @router /sci_hq/del [post]
  1287. func (this *SciHqDataController) DeleteSciHqData() {
  1288. br := new(models.BaseResponse).Init()
  1289. defer func() {
  1290. this.Data["json"] = br
  1291. this.ServeJSON()
  1292. }()
  1293. var req request.DelBaseFromSciHqReq
  1294. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1295. if err != nil {
  1296. br.Msg = "参数解析异常!"
  1297. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1298. return
  1299. }
  1300. if req.BaseFromSciHqIndexId < 0 {
  1301. br.Msg = "参数错误"
  1302. br.IsSendEmail = false
  1303. return
  1304. }
  1305. err, errMsg := data.DelSciHqData(req.BaseFromSciHqIndexId)
  1306. if errMsg != `` {
  1307. br.Msg = errMsg
  1308. br.ErrMsg = errMsg
  1309. if err != nil {
  1310. br.ErrMsg = errMsg + ";Err:" + err.Error()
  1311. } else {
  1312. br.IsSendEmail = false
  1313. }
  1314. return
  1315. }
  1316. br.Ret = 200
  1317. br.Msg = "删除成功"
  1318. br.Success = true
  1319. br.IsAddLog = true
  1320. }
  1321. // ExportSciHqList
  1322. // @Title 导出Sci数据
  1323. // @Description 导出Sci数据
  1324. // @Param ClassifyId query int true "关键字搜索"
  1325. // @Param IndexCode query string true "指标编码"
  1326. // @Success 200 导出成功
  1327. // @router /export/sciHqList [get]
  1328. func (this *SciHqDataController) ExportSciHqList() {
  1329. br := new(models.BaseResponse).Init()
  1330. defer func() {
  1331. this.Data["json"] = br
  1332. this.ServeJSON()
  1333. }()
  1334. sysUser := this.SysUser
  1335. if sysUser == nil {
  1336. br.Msg = "请重新登录"
  1337. return
  1338. }
  1339. classifyId, _ := this.GetInt("ClassifyId")
  1340. indexCode := this.GetString("IndexCode")
  1341. if classifyId < 0 {
  1342. br.Msg = "请选择分类"
  1343. return
  1344. }
  1345. secNameList := make([]*data_manage.BaseFromSciHqIndex, 0)
  1346. dir, _ := os.Executable()
  1347. exPath := filepath.Dir(dir)
  1348. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1349. xlsxFile := xlsx.NewFile()
  1350. var condition string
  1351. var pars []interface{}
  1352. if classifyId > 0 {
  1353. childClassify, err := data_manage.GetChildSciHqClassifyListById(classifyId)
  1354. if err != nil {
  1355. br.Msg = "下载失败"
  1356. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  1357. return
  1358. }
  1359. var childIds []int
  1360. for _, v := range childClassify {
  1361. childIds = append(childIds, v.ClassifyId)
  1362. }
  1363. if len(childIds) > 0 {
  1364. condition += `AND classify_id IN (?)`
  1365. pars = append(pars, childIds)
  1366. } else {
  1367. condition += ` AND classify_id=?`
  1368. pars = append(pars, classifyId)
  1369. }
  1370. } else {
  1371. condition += ` AND classify_id=?`
  1372. pars = append(pars, classifyId)
  1373. }
  1374. if indexCode != "" {
  1375. condition += ` AND index_code=? `
  1376. pars = append(pars, indexCode)
  1377. }
  1378. frequencies, err := data_manage.GetSciHqFrequencyByCondition(condition, pars)
  1379. if err != nil {
  1380. fmt.Println("GetSciFrequency err:", err.Error())
  1381. utils.FileLog.Info("GetSciFrequency err:" + err.Error())
  1382. return
  1383. }
  1384. for _, frequency := range frequencies {
  1385. //获取指标
  1386. secNameList, err = data_manage.GetSciHqIndexByConditionAndFrequency(condition, *frequency, pars)
  1387. if err != nil {
  1388. fmt.Println("获取数据失败,Err:" + err.Error())
  1389. return
  1390. }
  1391. if len(secNameList) <= 0 {
  1392. fmt.Println("secNameList长度为0")
  1393. return
  1394. }
  1395. sheetNew, err := xlsxFile.AddSheet(*frequency)
  1396. if err != nil {
  1397. fmt.Println("新增Sheet失败", err.Error())
  1398. return
  1399. }
  1400. secNameRow := sheetNew.AddRow()
  1401. frequencyRow := sheetNew.AddRow()
  1402. unitRow := sheetNew.AddRow()
  1403. lastModifyDateRow := sheetNew.AddRow()
  1404. var indexIdList []int
  1405. for _, sv := range secNameList {
  1406. indexIdList = append(indexIdList, sv.BaseFromSciHqIndexId)
  1407. }
  1408. dataTimeList, err := data_manage.GetSciHqDataDataTimeByIndexId(indexIdList)
  1409. if err != nil {
  1410. fmt.Println("获取数据时间失败", err.Error())
  1411. return
  1412. }
  1413. // 添加excel左侧指标日期
  1414. setRowIndex := 4
  1415. for rk, dv := range dataTimeList {
  1416. rowIndex := setRowIndex + rk
  1417. row := sheetNew.Row(rowIndex)
  1418. displayDate, _ := time.Parse(utils.FormatDate, dv)
  1419. displayDateCell := row.AddCell()
  1420. style := new(xlsx.Style)
  1421. style.ApplyAlignment = true
  1422. style.Alignment.WrapText = true
  1423. displayDateCell.SetStyle(style)
  1424. displayDateCell.SetDate(displayDate)
  1425. }
  1426. for k, sv := range secNameList {
  1427. //获取数据
  1428. dataList, err := data_manage.GetSciHqIndexDataByCode(sv.IndexCode)
  1429. if err != nil {
  1430. br.Msg = "获取数据失败"
  1431. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1432. return
  1433. }
  1434. if k == 0 {
  1435. secNameRow.AddCell().SetValue("指标名称")
  1436. frequencyRow.AddCell().SetValue("频率")
  1437. unitRow.AddCell().SetValue("单位")
  1438. lastModifyDateRow.AddCell().SetValue("更新时间")
  1439. min := k * 3
  1440. sheetNew.SetColWidth(min, min, 15)
  1441. }
  1442. if len(dataList) == 0 {
  1443. continue
  1444. }
  1445. secNameRow.AddCell().SetValue(sv.IndexName)
  1446. frequencyRow.AddCell().SetValue(sv.Frequency)
  1447. unitRow.AddCell().SetValue(sv.Unit)
  1448. lastModifyDateRow.AddCell().SetValue(sv.ModifyTime.Format(utils.FormatDate))
  1449. dataInfoMap := make(map[string]*data_manage.BaseFromSciHqData)
  1450. for _, v := range dataList {
  1451. dataInfoMap[v.DataTime] = v
  1452. }
  1453. for rk, dtv := range dataTimeList {
  1454. rowIndex := setRowIndex + rk
  1455. row := sheetNew.Row(rowIndex)
  1456. displayDateCell := row.AddCell()
  1457. tmpData, ok := dataInfoMap[dtv]
  1458. if ok {
  1459. displayDateCell.SetValue(tmpData.Value)
  1460. }
  1461. }
  1462. }
  1463. }
  1464. err = xlsxFile.Save(downLoadnFilePath)
  1465. if err != nil {
  1466. //有指标无数据时先导出一遍空表
  1467. sheet, err := xlsxFile.AddSheet("无数据")
  1468. if err != nil {
  1469. br.Msg = "新增Sheet失败"
  1470. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1471. return
  1472. }
  1473. rowSecName := sheet.AddRow()
  1474. celSecName := rowSecName.AddCell()
  1475. celSecName.SetValue("")
  1476. err = xlsxFile.Save(downLoadnFilePath)
  1477. if err != nil {
  1478. br.Msg = "保存文件失败"
  1479. br.ErrMsg = "保存文件失败"
  1480. return
  1481. }
  1482. }
  1483. fileName := `卓创红期`
  1484. if indexCode != "" && len(secNameList) == 1 {
  1485. fileName = secNameList[0].IndexName
  1486. }
  1487. fileName += time.Now().Format("2006.01.02") + `.xlsx` //文件名称
  1488. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  1489. defer func() {
  1490. os.Remove(downLoadnFilePath)
  1491. }()
  1492. br.Ret = 200
  1493. br.Success = true
  1494. br.Msg = "success"
  1495. }
  1496. // getSciHqClassifyTree 返回卓创红旗的树形结构
  1497. func getSciHqClassifyTree(items []*data_manage.BaseFromSciHqClassifyItem, parentId int) []*data_manage.BaseFromSciHqClassifyItem {
  1498. res := make([]*data_manage.BaseFromSciHqClassifyItem, 0)
  1499. for _, item := range items {
  1500. if item.ParentId == parentId {
  1501. t := new(data_manage.BaseFromSciHqClassifyItem)
  1502. t.ClassifyId = item.ClassifyId
  1503. t.ClassifyName = item.ClassifyName
  1504. t.ParentId = item.ParentId
  1505. t.Level = item.Level
  1506. t.Sort = item.Sort
  1507. t.ModifyTime = item.ModifyTime
  1508. t.CreateTime = item.CreateTime
  1509. t.ClassifyNameEn = item.ClassifyNameEn
  1510. if item.UniqueCode == "" {
  1511. t.UniqueCode = strconv.Itoa(item.ClassifyId)
  1512. }
  1513. t.Children = getSciHqClassifyTree(items, item.ClassifyId)
  1514. res = append(res, t)
  1515. }
  1516. }
  1517. return res
  1518. }