sci_hq_data.go 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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 err.Error() == utils.ErrNoRow() {
  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.ParentId, 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 && err.Error() != utils.ErrNoRow() {
  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 (` + utils.GetOrmInReplace(len(req.SelectedId)) + `)`
  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 (` + utils.GetOrmInReplace(len(req.SelectedId)) + `)`
  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. if len(childClassify) > 0 {
  337. condition += `AND classify_id IN (` + utils.GetOrmInReplace(len(childClassify)) + `)`
  338. for _, child := range childClassify {
  339. pars = append(pars, child.ClassifyId)
  340. }
  341. } else {
  342. condition += ` AND classify_id=?`
  343. pars = append(pars, req.ClassifyId)
  344. }
  345. } else {
  346. condition += ` AND classify_id=?`
  347. pars = append(pars, req.ClassifyId)
  348. }
  349. count, err := data_manage.GetSciHqIndexCountByCondition(condition, pars)
  350. if err != nil {
  351. br.Msg = "获取失败"
  352. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  353. return
  354. }
  355. MaxLen := 30
  356. if count > MaxLen {
  357. br.Msg = "批量操作数量不能超过" + strconv.Itoa(MaxLen)
  358. return
  359. }
  360. indexList, err := data_manage.GetSciHqIndexByCondition(condition, pars)
  361. if err != nil {
  362. br.Msg = "获取失败"
  363. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  364. return
  365. }
  366. br.Ret = 200
  367. br.Success = true
  368. br.Msg = "获取成功"
  369. br.Data = indexList
  370. }
  371. // IndexPageList
  372. // @Title 卓创红期指标列表
  373. // @Description 卓创红期数据指标列表接口
  374. // @Param PageSize query int true "每页数据条数"
  375. // @Param CurrentIndex query int true "当前页页码,从1开始"
  376. // @Param ClassifyId query int true "分类id"
  377. // @Param IsListAll query int true "是否展示全部"
  378. // @Success 200 {object} data_manage.BaseFromMysteelChemicalIndexResp
  379. // @router /sci_hq/index/page/list [get]
  380. func (this *SciHqDataController) IndexPageList() {
  381. br := new(models.BaseResponse).Init()
  382. defer func() {
  383. this.Data["json"] = br
  384. this.ServeJSON()
  385. }()
  386. classifyId, _ := this.GetInt("ClassifyId", 0)
  387. pageSize, _ := this.GetInt("PageSize")
  388. currrentIndex, _ := this.GetInt("CurrentIndex")
  389. isListAll, _ := this.GetBool("IsListAll")
  390. var startSize int
  391. if pageSize <= 0 {
  392. pageSize = utils.PageSize20
  393. }
  394. if currrentIndex <= 0 {
  395. currrentIndex = 1
  396. }
  397. startSize = utils.StartIndex(currrentIndex, pageSize)
  398. var total int
  399. var indexList []*data_manage.BaseFromSciHqIndexView
  400. if isListAll {
  401. tmpTotal, err := data_manage.GetSciHqIndexCount()
  402. if err != nil {
  403. br.Msg = "获取失败"
  404. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  405. return
  406. }
  407. total = tmpTotal
  408. tmpIndexList, err := data_manage.GetSciHqIndexByPage(startSize, pageSize)
  409. if err != nil && err.Error() != utils.ErrNoRow() {
  410. br.Msg = "获取失败"
  411. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  412. return
  413. }
  414. indexList = tmpIndexList
  415. } else {
  416. var classifyIds []int
  417. if classifyId > 0 {
  418. tmpClassifyIds, err := data_manage.GetSciHqChildClassifyIdsById(classifyId)
  419. if err != nil {
  420. br.Msg = "获取失败"
  421. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  422. return
  423. }
  424. if len(tmpClassifyIds) > 0 {
  425. classifyIds = append(classifyIds, tmpClassifyIds...)
  426. }
  427. }
  428. classifyIds = append(classifyIds, classifyId)
  429. tmpTotal, err := data_manage.GetSciHqIndexCountByClassifyId(classifyIds)
  430. if err != nil {
  431. br.Msg = "获取失败"
  432. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  433. return
  434. }
  435. total = tmpTotal
  436. tmpIndexList, err := data_manage.GetSciHqIndexByClassifyId(classifyIds, startSize, pageSize)
  437. if err != nil && err.Error() != utils.ErrNoRow() {
  438. br.Msg = "获取失败"
  439. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  440. return
  441. }
  442. indexList = tmpIndexList
  443. }
  444. page := paging.GetPaging(currrentIndex, pageSize, total)
  445. resp := new(response.SciHqIndexPageListResp)
  446. resp.List = indexList
  447. resp.Paging = page
  448. br.Ret = 200
  449. br.Success = true
  450. br.Msg = "获取成功"
  451. br.Data = resp
  452. }
  453. // SearchList
  454. // @Title 卓创红期模糊搜索
  455. // @Description 卓创红期模糊搜索
  456. // @Param Keyword query string true "关键字搜索"
  457. // @Success 200 {object} models.BaseResponse
  458. // @router /sci_hq/search_list [get]
  459. func (this *SciHqDataController) SearchList() {
  460. br := new(models.BaseResponse).Init()
  461. defer func() {
  462. this.Data["json"] = br
  463. this.ServeJSON()
  464. }()
  465. //关键字
  466. keyword := this.GetString("Keyword")
  467. var condition string
  468. var pars []interface{}
  469. if keyword != "" {
  470. condition += ` AND (index_code LIKE ? OR index_name LIKE ?)`
  471. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  472. } else {
  473. br.Msg = "请输入指标ID/指标名称"
  474. return
  475. }
  476. list, err := data_manage.GetSciHqIndexBaseInfoByCondition(condition, pars)
  477. if err != nil {
  478. br.Msg = "获取失败"
  479. br.ErrMsg = "获取失败,Err:" + err.Error()
  480. return
  481. }
  482. br.Ret = 200
  483. br.Success = true
  484. br.Msg = "获取成功"
  485. br.Data = list
  486. }
  487. // SingleData
  488. // @Title 获取卓创红期据
  489. // @Description 获取卓创红期单条数据接口
  490. // @Param BaseFromSciHqIndexId query string true "指标唯一编码"
  491. // @Success 200 {object} models.BaseResponse
  492. // @router /sci_hq/single_data [get]
  493. func (this *SciHqDataController) SingleData() {
  494. br := new(models.BaseResponse).Init()
  495. defer func() {
  496. this.Data["json"] = br
  497. this.ServeJSON()
  498. }()
  499. baseFromSciHqIndexId, _ := this.GetInt("BaseFromSciHqIndexId")
  500. indexInfo, err := data_manage.GetSciHqIndexByIndexId(baseFromSciHqIndexId)
  501. if err != nil {
  502. br.Msg = "获取指标信息失败"
  503. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  504. return
  505. }
  506. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_SCI_HQ, indexInfo.IndexCode)
  507. if err != nil && err.Error() != utils.ErrNoRow() {
  508. br.Msg = "获取指标数据失败"
  509. br.ErrMsg = "获取指标库数据失败,Err:" + err.Error()
  510. return
  511. }
  512. var edbInfoId int
  513. if edbInfo != nil {
  514. edbInfoId = edbInfo.EdbInfoId
  515. }
  516. dataList, err := data_manage.GetSciHqDataByIndexId(baseFromSciHqIndexId)
  517. if err != nil {
  518. br.Msg = "获取数据失败"
  519. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  520. return
  521. }
  522. var ret response.SciHqSingleDataResp
  523. ret.ClassifyId = indexInfo.ClassifyId
  524. ret.BaseFromSciHqIndexId = indexInfo.BaseFromSciHqIndexId
  525. ret.IndexCode = indexInfo.IndexCode
  526. ret.EdbInfoId = edbInfoId
  527. ret.IndexName = indexInfo.IndexName
  528. ret.Frequency = indexInfo.Frequency
  529. ret.CreateTime = indexInfo.CreateTime.Format(utils.FormatDateTime)
  530. ret.ModifyTime = indexInfo.ModifyTime.Format(utils.FormatDateTime)
  531. ret.Unit = indexInfo.Unit
  532. ret.Data = dataList
  533. br.Ret = 200
  534. br.Success = true
  535. br.Msg = "获取成功"
  536. br.Data = ret
  537. }
  538. // MoveSciHqData
  539. // @Title 卓创红期指标移动接口
  540. // @Description 卓创红期指标移动接口
  541. // @Success 200 {object} request.MoveBaseFromSciHqReq
  542. // @router /sci_hq/move [post]
  543. func (this *SciHqDataController) MoveSciHqData() {
  544. br := new(models.BaseResponse).Init()
  545. defer func() {
  546. this.Data["json"] = br
  547. this.ServeJSON()
  548. }()
  549. sysUser := this.SysUser
  550. if sysUser == nil {
  551. br.Msg = "请登录"
  552. br.ErrMsg = "请登录,SysUser Is Empty"
  553. br.Ret = 408
  554. return
  555. }
  556. var req request.MoveBaseFromSciHqReq
  557. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  558. if err != nil {
  559. br.Msg = "参数解析异常!"
  560. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  561. return
  562. }
  563. if req.BaseFromSciHqIndexId <= 0 {
  564. br.Msg = "参数错误"
  565. br.ErrMsg = "指标id小于等于0"
  566. br.IsSendEmail = false
  567. return
  568. }
  569. if req.ClassifyId < 0 {
  570. br.Msg = "请选择分类"
  571. br.ErrMsg = "请选择分类"
  572. br.IsSendEmail = false
  573. return
  574. }
  575. err, errMsg := data.MoveSciHqData(req.BaseFromSciHqIndexId, req.ClassifyId, req.PrevBaseFromSciHqIndexId, req.NextBaseFromSciHqIndexId)
  576. if errMsg != `` {
  577. br.Msg = errMsg
  578. br.ErrMsg = errMsg
  579. if err != nil {
  580. br.ErrMsg = err.Error()
  581. } else {
  582. br.IsSendEmail = false
  583. }
  584. return
  585. }
  586. br.Ret = 200
  587. br.Success = true
  588. br.IsAddLog = true
  589. br.Msg = "移动成功"
  590. }
  591. // ResetSciHqIndex
  592. // @Title 指标数据清除分类
  593. // @Description 指标数据清除分类
  594. // @Param request body data_manage.DelBaseFromSciReq true "type json string"
  595. // @Success 200 Ret=200 操作成功
  596. // @router /sci_hq/reset [post]
  597. func (this *SciHqDataController) ResetSciHqIndex() {
  598. br := new(models.BaseResponse).Init()
  599. defer func() {
  600. this.Data["json"] = br
  601. this.ServeJSON()
  602. }()
  603. var req request.ResetBaseFromSciHqReq
  604. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  605. if err != nil {
  606. br.Msg = "参数解析异常!"
  607. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  608. return
  609. }
  610. if req.BaseFromSciHqIndexId < 0 {
  611. br.Msg = "参数错误"
  612. br.IsSendEmail = false
  613. return
  614. }
  615. err = data.ResetSciHqIndex(req.BaseFromSciHqIndexId)
  616. if err != nil {
  617. br.Msg = "移动失败"
  618. br.ErrMsg = "移动失败,Err:" + err.Error()
  619. return
  620. }
  621. br.Ret = 200
  622. br.Msg = "操作成功"
  623. br.Success = true
  624. br.IsAddLog = true
  625. }
  626. // AddEdbInfo
  627. // @Title 新增指标接口
  628. // @Description 新增指标接口
  629. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  630. // @Success Ret=200 保存成功
  631. // @router /sci_hq/edb_info/add [post]
  632. func (this *SciHqDataController) AddEdbInfo() {
  633. br := new(models.BaseResponse).Init()
  634. defer func() {
  635. this.Data["json"] = br
  636. this.ServeJSON()
  637. }()
  638. sysUser := this.SysUser
  639. if sysUser == nil {
  640. br.Msg = "请登录"
  641. br.ErrMsg = "请登录,SysUser Is Empty"
  642. br.Ret = 408
  643. return
  644. }
  645. deleteCache := true
  646. cacheKey := "CACHE_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  647. defer func() {
  648. if deleteCache {
  649. utils.Rc.Delete(cacheKey)
  650. }
  651. }()
  652. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  653. deleteCache = false
  654. br.Msg = "系统处理中,请稍后重试!"
  655. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  656. return
  657. }
  658. var req data_manage.AddEdbInfoReq
  659. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  660. if err != nil {
  661. br.Msg = "参数解析异常!"
  662. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  663. return
  664. }
  665. req.EdbName = strings.Trim(req.EdbName, " ")
  666. req.EdbCode = strings.Trim(req.EdbCode, " ")
  667. if req.EdbCode == "" {
  668. br.Msg = "指标ID不能为空"
  669. return
  670. }
  671. if req.EdbName == "" {
  672. br.Msg = "指标名称不能为空"
  673. return
  674. }
  675. if req.Frequency == "" {
  676. br.Msg = "频率不能为空"
  677. return
  678. }
  679. if req.Unit == "" {
  680. br.Msg = "单位不能为空"
  681. return
  682. }
  683. if req.ClassifyId <= 0 {
  684. br.Msg = "请选择分类"
  685. return
  686. }
  687. _, err = data_manage.GetSciHqIndexByIndexCode(req.EdbCode)
  688. if err != nil {
  689. if err.Error() == utils.ErrNoRow() {
  690. br.Msg = "指标不存在"
  691. return
  692. }
  693. br.Msg = "获取失败"
  694. br.ErrMsg = "获取失败,Err:" + err.Error()
  695. return
  696. }
  697. source := utils.DATA_SOURCE_SCI_HQ
  698. // 指标入库
  699. 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)
  700. if err != nil {
  701. br.Msg = "保存失败"
  702. if errMsg != `` {
  703. br.Msg = errMsg
  704. }
  705. br.ErrMsg = err.Error()
  706. br.IsSendEmail = isSendEmail
  707. return
  708. }
  709. // 试用平台更新用户累计新增指标数
  710. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  711. if e != nil {
  712. br.Msg = "操作失败"
  713. br.ErrMsg = "获取系统用户数据失败,Err:" + e.Error()
  714. return
  715. }
  716. if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
  717. go func() {
  718. var r etaTrialService.EtaTrialUserReq
  719. r.Mobile = adminItem.Mobile
  720. _, _ = etaTrialService.UpdateUserIndexNum(r)
  721. }()
  722. }
  723. //新增操作日志
  724. {
  725. // 添加钢联指标更新日志
  726. if edbInfo.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  727. go data_stat.AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", sysUser, 2)
  728. }
  729. edbLog := new(data_manage.EdbInfoLog)
  730. edbLog.EdbInfoId = edbInfo.EdbInfoId
  731. edbLog.SourceName = edbInfo.SourceName
  732. edbLog.Source = edbInfo.Source
  733. edbLog.EdbCode = edbInfo.EdbCode
  734. edbLog.EdbName = edbInfo.EdbName
  735. edbLog.ClassifyId = edbInfo.ClassifyId
  736. edbLog.SysUserId = sysUser.AdminId
  737. edbLog.SysUserRealName = sysUser.RealName
  738. edbLog.CreateTime = time.Now()
  739. edbLog.Content = string(this.Ctx.Input.RequestBody)
  740. edbLog.Status = "新增指标"
  741. edbLog.Method = this.Ctx.Input.URI()
  742. go data_manage.AddEdbInfoLog(edbLog)
  743. }
  744. // 更新es
  745. go data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  746. resp := new(data_manage.AddEdbInfoResp)
  747. resp.EdbInfoId = edbInfo.EdbInfoId
  748. resp.UniqueCode = edbInfo.UniqueCode
  749. br.Ret = 200
  750. br.Success = true
  751. br.Msg = "保存成功"
  752. br.Data = resp
  753. br.IsAddLog = true
  754. }
  755. // AddCheck
  756. // @Title 新增校验
  757. // @Description 新增校验
  758. // @Param request body request.BusinessDataBatchAddCheckReq true "type json string"
  759. // @Success 200 string "操作成功"
  760. // @router /sci_hq/edb_info/add_check [post]
  761. func (this *SciHqDataController) AddCheck() {
  762. br := new(models.BaseResponse).Init()
  763. defer func() {
  764. if br.ErrMsg == "" {
  765. br.IsSendEmail = false
  766. }
  767. this.Data["json"] = br
  768. this.ServeJSON()
  769. }()
  770. sysUser := this.SysUser
  771. if sysUser == nil {
  772. br.Msg = "请登录"
  773. br.ErrMsg = "请登录,SysUser Is Empty"
  774. br.Ret = 408
  775. return
  776. }
  777. var req *request.SciHqDataBatchAddCheckReq
  778. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  779. br.Msg = "参数解析异常!"
  780. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  781. return
  782. }
  783. codeMax := 30
  784. codeLen := len(req.IndexCodes)
  785. if len(req.IndexCodes) == 0 {
  786. br.Msg = "请选择指标"
  787. return
  788. }
  789. if codeLen > codeMax {
  790. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  791. return
  792. }
  793. // 获取指标库已有指标
  794. existsEdb, e := data_manage.GetEdbCodesBySource(utils.DATA_SOURCE_SCI_HQ)
  795. if e != nil {
  796. br.Msg = "获取失败"
  797. br.ErrMsg = "获取自有数据已添加的指标失败, Err: " + e.Error()
  798. return
  799. }
  800. existMap := make(map[string]*data_manage.EdbInfo)
  801. for _, v := range existsEdb {
  802. existMap[v.EdbCode] = v
  803. }
  804. // 查询选中的指标
  805. cond := fmt.Sprintf(` AND index_code IN (%s)`, utils.GetOrmInReplace(codeLen))
  806. pars := make([]interface{}, 0)
  807. pars = append(pars, req.IndexCodes)
  808. list, err := data_manage.GetSciHqIndexAndEdbInfoByCondition(cond, pars)
  809. if err != nil {
  810. br.Msg = "获取失败"
  811. br.ErrMsg = "获取卓创红期原始指标列表失败, Err: " + err.Error()
  812. return
  813. }
  814. resp := make([]*data_manage.BaseFromSciHqIndexView, 0)
  815. for _, v := range list {
  816. if v.EdbInfoId > 0 {
  817. v.EdbExist = 1
  818. }
  819. resp = append(resp, v)
  820. }
  821. br.Data = resp
  822. br.Msg = "校验成功"
  823. br.Ret = 200
  824. br.Success = true
  825. }
  826. // NameCheck
  827. // @Title 重名校验
  828. // @Description 批量新增
  829. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  830. // @Success 200 string "操作成功"
  831. // @router /sci_hq/edb_info/name_check [post]
  832. func (c *SciHqDataController) NameCheck() {
  833. br := new(models.BaseResponse).Init()
  834. defer func() {
  835. if br.ErrMsg == "" {
  836. br.IsSendEmail = false
  837. }
  838. c.Data["json"] = br
  839. c.ServeJSON()
  840. }()
  841. sysUser := c.SysUser
  842. if sysUser == nil {
  843. br.Msg = "请登录"
  844. br.ErrMsg = "请登录,SysUser Is Empty"
  845. br.Ret = 408
  846. return
  847. }
  848. var req []*data_manage.NameCheckEdbInfoReq
  849. if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
  850. br.Msg = "参数解析异常!"
  851. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  852. return
  853. }
  854. if len(req) == 0 {
  855. br.Msg = "请选择指标"
  856. return
  857. }
  858. codeMax := 30
  859. codeLen := len(req)
  860. if codeLen > codeMax {
  861. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  862. return
  863. }
  864. type NameCheckResult struct {
  865. EdbCode string
  866. EdbName string
  867. Exist bool
  868. }
  869. indexNames := make([]string, 0)
  870. resp := make([]*NameCheckResult, 0)
  871. for _, v := range req {
  872. v.EdbCode = strings.TrimSpace(v.EdbCode)
  873. if v.EdbCode == "" {
  874. br.Msg = "指标ID不可为空"
  875. return
  876. }
  877. v.EdbName = strings.TrimSpace(v.EdbName)
  878. if v.EdbName == "" {
  879. br.Msg = "请输入指标名称"
  880. return
  881. }
  882. indexNames = append(indexNames, v.EdbName)
  883. resp = append(resp, &NameCheckResult{
  884. EdbCode: v.EdbCode,
  885. EdbName: v.EdbName,
  886. })
  887. dataItems, err := data_manage.GetEdbDataAllByEdbCode(v.EdbCode, utils.DATA_SOURCE_SCI_HQ, 0, utils.EDB_DATA_LIMIT)
  888. if err != nil && err.Error() != utils.ErrNoRow() {
  889. br.Msg = "获取失败"
  890. br.ErrMsg = "获取卓创红期已存在信息失败,Err:" + err.Error()
  891. return
  892. }
  893. if len(dataItems) <= 0 {
  894. respItem, err := data.AddEdbData(utils.DATA_SOURCE_SCI_HQ, v.EdbCode, v.Frequency)
  895. if err != nil {
  896. br.Msg = "获取失败"
  897. br.ErrMsg = "获取失败,Err:" + err.Error()
  898. return
  899. }
  900. if respItem.Ret != 200 {
  901. br.Msg = "未搜索到该指标"
  902. br.ErrMsg = respItem.ErrMsg + ";EdbCode:" + v.EdbCode
  903. return
  904. }
  905. }
  906. }
  907. // 重名校验
  908. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  909. if e != nil {
  910. br.Msg = "操作失败"
  911. br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
  912. return
  913. }
  914. nameExists := make(map[string]bool)
  915. for _, v := range edbList {
  916. nameExists[v.EdbName] = true
  917. }
  918. if len(nameExists) > 0 {
  919. for _, v := range resp {
  920. v.Exist = nameExists[v.EdbName]
  921. }
  922. }
  923. br.Data = resp
  924. br.Msg = "校验成功"
  925. br.Ret = 200
  926. br.Success = true
  927. }
  928. // BatchAdd
  929. // @Title 批量新增
  930. // @Description 批量新增
  931. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  932. // @Success 200 string "操作成功"
  933. // @router /sci_hq/edb_info/batch_add [post]
  934. func (this *SciHqDataController) BatchAdd() {
  935. br := new(models.BaseResponse).Init()
  936. defer func() {
  937. if br.ErrMsg == "" {
  938. br.IsSendEmail = false
  939. }
  940. this.Data["json"] = br
  941. this.ServeJSON()
  942. }()
  943. sysUser := this.SysUser
  944. if sysUser == nil {
  945. br.Msg = "请登录"
  946. br.ErrMsg = "请登录,SysUser Is Empty"
  947. br.Ret = 408
  948. return
  949. }
  950. deleteCache := true
  951. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_SCI_HQ_" + strconv.Itoa(sysUser.AdminId)
  952. defer func() {
  953. if deleteCache {
  954. _ = utils.Rc.Delete(cacheKey)
  955. }
  956. }()
  957. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  958. deleteCache = false
  959. br.Msg = "系统处理中,请稍后重试!"
  960. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  961. return
  962. }
  963. var req []*data_manage.AddEdbInfoReq
  964. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  965. br.Msg = "参数解析异常!"
  966. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  967. return
  968. }
  969. if len(req) == 0 {
  970. br.Msg = "请选择指标"
  971. return
  972. }
  973. if len(req) > 30 {
  974. br.Msg = "批量添加指标数量不得超过30个"
  975. return
  976. }
  977. for _, v := range req {
  978. v.EdbCode = strings.TrimSpace(v.EdbCode)
  979. if v.EdbCode == "" {
  980. br.Msg = "指标ID不可为空"
  981. return
  982. }
  983. v.EdbName = strings.TrimSpace(v.EdbName)
  984. if v.EdbName == "" {
  985. br.Msg = "请输入指标名称"
  986. return
  987. }
  988. v.Frequency = strings.TrimSpace(v.Frequency)
  989. if v.Frequency == "" {
  990. br.Msg = "请选择频度"
  991. return
  992. }
  993. v.Unit = strings.TrimSpace(v.Unit)
  994. if v.Unit == "" {
  995. br.Msg = "请输入单位"
  996. return
  997. }
  998. if v.ClassifyId <= 0 {
  999. br.Msg = "请选择分类"
  1000. return
  1001. }
  1002. }
  1003. // 限定同一时间最多批量新增30个指标
  1004. for _, v := range req {
  1005. var r data.SciHqIndexSource2EdbReq
  1006. r.EdbCode = v.EdbCode
  1007. r.EdbName = v.EdbName
  1008. r.Frequency = v.Frequency
  1009. r.Unit = v.Unit
  1010. r.ClassifyId = v.ClassifyId
  1011. r.AdminId = sysUser.AdminId
  1012. r.AdminRealName = sysUser.RealName
  1013. edbInfo, errMsg, skip, e := data.SciHqIndexSource2Edb(r, this.Lang)
  1014. if e != nil {
  1015. br.Msg = "操作失败"
  1016. if errMsg != "" {
  1017. br.Msg = errMsg
  1018. }
  1019. br.ErrMsg = e.Error()
  1020. return
  1021. }
  1022. if skip {
  1023. continue
  1024. }
  1025. // 试用平台更新用户累计新增指标数
  1026. if utils.BusinessCode == utils.BusinessCodeSandbox {
  1027. go func() {
  1028. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  1029. if e != nil {
  1030. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  1031. utils.FileLog.Info(tips)
  1032. return
  1033. }
  1034. if adminItem.DepartmentName != "ETA试用客户" {
  1035. return
  1036. }
  1037. var ur etaTrialService.EtaTrialUserReq
  1038. ur.Mobile = adminItem.Mobile
  1039. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  1040. }()
  1041. }
  1042. // 新增操作日志
  1043. {
  1044. edbLog := new(data_manage.EdbInfoLog)
  1045. edbLog.EdbInfoId = edbInfo.EdbInfoId
  1046. edbLog.SourceName = edbInfo.SourceName
  1047. edbLog.Source = edbInfo.Source
  1048. edbLog.EdbCode = edbInfo.EdbCode
  1049. edbLog.EdbName = edbInfo.EdbName
  1050. edbLog.ClassifyId = edbInfo.ClassifyId
  1051. edbLog.SysUserId = sysUser.AdminId
  1052. edbLog.SysUserRealName = sysUser.RealName
  1053. edbLog.CreateTime = time.Now()
  1054. edbLog.Content = string(this.Ctx.Input.RequestBody)
  1055. edbLog.Status = "新增指标"
  1056. edbLog.Method = this.Ctx.Input.URI()
  1057. go data_manage.AddEdbInfoLog(edbLog)
  1058. }
  1059. }
  1060. br.Msg = "操作成功"
  1061. br.Ret = 200
  1062. br.Success = true
  1063. br.IsAddLog = true
  1064. }
  1065. // BatchDelete
  1066. // @Title 批量删除
  1067. // @Description 批量删除
  1068. // @Param request body []*request.DelBaseFromSciHqReq true "type json string"
  1069. // @Success 200 string "操作成功"
  1070. // @router /sci_hq/edb_info/batch_delete [post]
  1071. func (this *SciHqDataController) BatchDelete() {
  1072. br := new(models.BaseResponse).Init()
  1073. defer func() {
  1074. if br.ErrMsg == "" {
  1075. br.IsSendEmail = false
  1076. }
  1077. this.Data["json"] = br
  1078. this.ServeJSON()
  1079. }()
  1080. var req []*request.DelBaseFromSciHqReq
  1081. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1082. br.Msg = "参数解析异常!"
  1083. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  1084. return
  1085. }
  1086. if len(req) == 0 {
  1087. br.Msg = "请选择指标"
  1088. return
  1089. }
  1090. if len(req) > 30 {
  1091. br.Msg = "批量添加指标数量不得超过30个"
  1092. return
  1093. }
  1094. var deleteIds []int
  1095. for _, v := range req {
  1096. if v.BaseFromSciHqIndexId <= 0 {
  1097. continue
  1098. }
  1099. deleteIds = append(deleteIds, v.BaseFromSciHqIndexId)
  1100. }
  1101. existList, err := data.BatchDelSciHqData(deleteIds)
  1102. if err != nil {
  1103. br.Msg = "删除失败"
  1104. br.ErrMsg = "删除失败,Err:" + err.Error()
  1105. return
  1106. }
  1107. br.Data = existList
  1108. br.Msg = "操作成功"
  1109. br.Ret = 200
  1110. br.Success = true
  1111. br.IsAddLog = true
  1112. }
  1113. // BatchEdit
  1114. // @Title 批量编辑
  1115. // @Description 批量编辑
  1116. // @Param request body []*request.DelBaseFromSciHqReq true "type json string"
  1117. // @Success 200 string "操作成功"
  1118. // @router /sci_hq/edb_info/batch_edit [post]
  1119. func (this *SciHqDataController) BatchEdit() {
  1120. br := new(models.BaseResponse).Init()
  1121. defer func() {
  1122. if br.ErrMsg == "" {
  1123. br.IsSendEmail = false
  1124. }
  1125. this.Data["json"] = br
  1126. this.ServeJSON()
  1127. }()
  1128. var req []*request.EditBaseFromSciHqReq
  1129. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1130. br.Msg = "参数解析异常!"
  1131. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  1132. return
  1133. }
  1134. if len(req) == 0 {
  1135. br.Msg = "请选择指标"
  1136. return
  1137. }
  1138. indexIds := make([]int, 0)
  1139. classifyIds := make([]int, 0)
  1140. indexIdToclassifyId := make(map[int]int)
  1141. for _, v := range req {
  1142. if v.BaseFromSciHqIndexId <= 0 {
  1143. continue
  1144. }
  1145. if v.ClassifyId <= 0 {
  1146. continue
  1147. }
  1148. indexIds = append(indexIds, v.BaseFromSciHqIndexId)
  1149. classifyIds = append(classifyIds, v.ClassifyId)
  1150. indexIdToclassifyId[v.BaseFromSciHqIndexId] = v.ClassifyId
  1151. }
  1152. if len(indexIds) == 0 {
  1153. br.Msg = "请选择指标或指定正确的分类"
  1154. return
  1155. }
  1156. indexList, err := data_manage.GetSciHqIndexListByIndexIds(indexIds)
  1157. if err != nil {
  1158. br.Msg = "编辑失败"
  1159. br.ErrMsg = "获取指标失败,Err:" + err.Error()
  1160. return
  1161. }
  1162. classifySort, err := data_manage.GetSciHqClassifyMaxSortByClassifyIds(classifyIds)
  1163. if err != nil {
  1164. br.Msg = "编辑失败"
  1165. br.ErrMsg = "获取分类最大排序失败,Err:" + err.Error()
  1166. return
  1167. }
  1168. classifySortMap := make(map[int]int)
  1169. for _, v := range classifySort {
  1170. classifySortMap[v.ClassifyId] = v.MaxSort
  1171. }
  1172. classifyList, err := data_manage.GetSciHqClassifyListByIds(classifyIds)
  1173. if err != nil {
  1174. br.Msg = "编辑失败"
  1175. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  1176. return
  1177. }
  1178. existClassifyList := make(map[int]struct{})
  1179. for _, v := range classifyList {
  1180. existClassifyList[v.ClassifyId] = struct{}{}
  1181. }
  1182. //编辑指标
  1183. updateIndexList := make([]*data_manage.BaseFromSciHqIndex, 0)
  1184. for _, v := range indexList {
  1185. tmpClassifyId := indexIdToclassifyId[v.BaseFromSciHqIndexId]
  1186. if _, ok := existClassifyList[tmpClassifyId]; ok {
  1187. v.ClassifyId = tmpClassifyId
  1188. v.Sort = classifySortMap[tmpClassifyId] + 1
  1189. classifySortMap[tmpClassifyId] += 1
  1190. updateIndexList = append(updateIndexList, v)
  1191. }
  1192. }
  1193. err = data_manage.BatchModifySciHqIndexClassify(updateIndexList)
  1194. if err != nil {
  1195. br.Msg = "编辑失败"
  1196. br.ErrMsg = "编辑指标失败,Err:" + err.Error()
  1197. return
  1198. }
  1199. br.Msg = "操作成功"
  1200. br.Ret = 200
  1201. br.Success = true
  1202. br.IsAddLog = true
  1203. }
  1204. // EditSciHq
  1205. // @Title 编辑卓创红期指标
  1206. // @Description 编辑卓创红期指标接口
  1207. // @Param request body data_manage.AddEdbClassifyReq true "type json string"
  1208. // @Success 200 Ret=200 保存成功
  1209. // @router /sci_hq/edit [post]
  1210. func (this *SciHqDataController) EditSciHq() {
  1211. br := new(models.BaseResponse).Init()
  1212. defer func() {
  1213. this.Data["json"] = br
  1214. this.ServeJSON()
  1215. }()
  1216. var req request.EditBaseFromSciHqReq
  1217. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1218. if err != nil {
  1219. br.Msg = "参数解析异常!"
  1220. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1221. return
  1222. }
  1223. if req.BaseFromSciHqIndexId <= 0 {
  1224. br.Msg = "请选择指标"
  1225. br.IsSendEmail = false
  1226. return
  1227. }
  1228. if req.ClassifyId <= 0 {
  1229. br.Msg = "请选择分类"
  1230. br.IsSendEmail = false
  1231. return
  1232. }
  1233. //编辑指标
  1234. sciIndexInfo, errMsg, err := data.EditSciHqIndex(req.BaseFromSciHqIndexId, req.ClassifyId)
  1235. if errMsg != `` {
  1236. br.Msg = errMsg
  1237. return
  1238. }
  1239. if err != nil {
  1240. br.Msg = "编辑失败"
  1241. br.ErrMsg = "编辑指标失败,Err:" + err.Error()
  1242. return
  1243. }
  1244. resp := response.EditSciHqIndexInfoResp{
  1245. BaseFromSciHqIndexId: sciIndexInfo.BaseFromSciHqIndexId,
  1246. IndexCode: sciIndexInfo.IndexCode,
  1247. }
  1248. br.Data = resp
  1249. br.Ret = 200
  1250. br.Msg = "保存成功"
  1251. br.Success = true
  1252. br.IsAddLog = true
  1253. }
  1254. // DeleteSciHqData
  1255. // @Title 删除指标
  1256. // @Description 删除指标接口
  1257. // @Param request body data_manage.DelBaseFromSciReq true "type json string"
  1258. // @Success 200 Ret=200 删除成功
  1259. // @router /sci_hq/del [post]
  1260. func (this *SciHqDataController) DeleteSciHqData() {
  1261. br := new(models.BaseResponse).Init()
  1262. defer func() {
  1263. this.Data["json"] = br
  1264. this.ServeJSON()
  1265. }()
  1266. var req request.DelBaseFromSciHqReq
  1267. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1268. if err != nil {
  1269. br.Msg = "参数解析异常!"
  1270. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1271. return
  1272. }
  1273. if req.BaseFromSciHqIndexId < 0 {
  1274. br.Msg = "参数错误"
  1275. br.IsSendEmail = false
  1276. return
  1277. }
  1278. err, errMsg := data.DelSciHqData(req.BaseFromSciHqIndexId)
  1279. if errMsg != `` {
  1280. br.Msg = errMsg
  1281. br.ErrMsg = errMsg
  1282. if err != nil {
  1283. br.ErrMsg = errMsg + ";Err:" + err.Error()
  1284. } else {
  1285. br.IsSendEmail = false
  1286. }
  1287. return
  1288. }
  1289. br.Ret = 200
  1290. br.Msg = "删除成功"
  1291. br.Success = true
  1292. br.IsAddLog = true
  1293. }
  1294. // ExportSciHqList
  1295. // @Title 导出Sci数据
  1296. // @Description 导出Sci数据
  1297. // @Param ClassifyId query int true "关键字搜索"
  1298. // @Param IndexCode query string true "指标编码"
  1299. // @Success 200 导出成功
  1300. // @router /export/sciHqList [get]
  1301. func (this *SciHqDataController) ExportSciHqList() {
  1302. br := new(models.BaseResponse).Init()
  1303. defer func() {
  1304. this.Data["json"] = br
  1305. this.ServeJSON()
  1306. }()
  1307. sysUser := this.SysUser
  1308. if sysUser == nil {
  1309. br.Msg = "请重新登录"
  1310. return
  1311. }
  1312. classifyId, _ := this.GetInt("ClassifyId")
  1313. indexCode := this.GetString("IndexCode")
  1314. if classifyId < 0 {
  1315. br.Msg = "请选择分类"
  1316. return
  1317. }
  1318. secNameList := make([]*data_manage.BaseFromSciHqIndex, 0)
  1319. dir, _ := os.Executable()
  1320. exPath := filepath.Dir(dir)
  1321. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1322. xlsxFile := xlsx.NewFile()
  1323. var condition string
  1324. var pars []interface{}
  1325. if classifyId > 0 {
  1326. childClassify, err := data_manage.GetChildSciHqClassifyListById(classifyId)
  1327. if err != nil {
  1328. br.Msg = "下载失败"
  1329. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  1330. return
  1331. }
  1332. if len(childClassify) > 0 {
  1333. condition += `AND classify_id IN (` + utils.GetOrmInReplace(len(childClassify)) + `)`
  1334. for _, child := range childClassify {
  1335. pars = append(pars, child.ClassifyId)
  1336. }
  1337. } else {
  1338. condition += ` AND classify_id=?`
  1339. pars = append(pars, classifyId)
  1340. }
  1341. } else {
  1342. condition += ` AND classify_id=?`
  1343. pars = append(pars, classifyId)
  1344. }
  1345. if indexCode != "" {
  1346. condition += ` AND index_code=? `
  1347. pars = append(pars, indexCode)
  1348. }
  1349. frequencies, err := data_manage.GetSciHqFrequencyByCondition(condition, pars)
  1350. if err != nil {
  1351. fmt.Println("GetSciFrequency err:", err.Error())
  1352. utils.FileLog.Info("GetSciFrequency err:" + err.Error())
  1353. return
  1354. }
  1355. for _, frequency := range frequencies {
  1356. //获取指标
  1357. secNameList, err = data_manage.GetSciHqIndexByConditionAndFrequency(condition, *frequency, pars)
  1358. if err != nil {
  1359. fmt.Println("获取数据失败,Err:" + err.Error())
  1360. return
  1361. }
  1362. if len(secNameList) <= 0 {
  1363. fmt.Println("secNameList长度为0")
  1364. return
  1365. }
  1366. sheetNew, err := xlsxFile.AddSheet(*frequency)
  1367. if err != nil {
  1368. fmt.Println("新增Sheet失败", err.Error())
  1369. return
  1370. }
  1371. secNameRow := sheetNew.AddRow()
  1372. frequencyRow := sheetNew.AddRow()
  1373. unitRow := sheetNew.AddRow()
  1374. lastModifyDateRow := sheetNew.AddRow()
  1375. var indexIdList []int
  1376. for _, sv := range secNameList {
  1377. indexIdList = append(indexIdList, sv.BaseFromSciHqIndexId)
  1378. }
  1379. dataTimeList, err := data_manage.GetSciHqDataDataTimeByIndexId(indexIdList)
  1380. if err != nil {
  1381. fmt.Println("获取数据时间失败", err.Error())
  1382. return
  1383. }
  1384. // 添加excel左侧指标日期
  1385. setRowIndex := 4
  1386. for rk, dv := range dataTimeList {
  1387. rowIndex := setRowIndex + rk
  1388. row := sheetNew.Row(rowIndex)
  1389. displayDate, _ := time.Parse(utils.FormatDate, dv)
  1390. displayDateCell := row.AddCell()
  1391. style := new(xlsx.Style)
  1392. style.ApplyAlignment = true
  1393. style.Alignment.WrapText = true
  1394. displayDateCell.SetStyle(style)
  1395. displayDateCell.SetDate(displayDate)
  1396. }
  1397. for k, sv := range secNameList {
  1398. //获取数据
  1399. dataList, err := data_manage.GetSciHqIndexDataByCode(sv.IndexCode)
  1400. if err != nil {
  1401. br.Msg = "获取数据失败"
  1402. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1403. return
  1404. }
  1405. if k == 0 {
  1406. secNameRow.AddCell().SetValue("指标名称")
  1407. frequencyRow.AddCell().SetValue("频率")
  1408. unitRow.AddCell().SetValue("单位")
  1409. lastModifyDateRow.AddCell().SetValue("更新时间")
  1410. min := k * 3
  1411. sheetNew.SetColWidth(min, min, 15)
  1412. }
  1413. if len(dataList) == 0 {
  1414. continue
  1415. }
  1416. secNameRow.AddCell().SetValue(sv.IndexName)
  1417. frequencyRow.AddCell().SetValue(sv.Frequency)
  1418. unitRow.AddCell().SetValue(sv.Unit)
  1419. lastModifyDateRow.AddCell().SetValue(sv.ModifyTime.Format(utils.FormatDate))
  1420. dataInfoMap := make(map[string]*data_manage.BaseFromSciHqData)
  1421. for _, v := range dataList {
  1422. dataInfoMap[v.DataTime] = v
  1423. }
  1424. for rk, dtv := range dataTimeList {
  1425. rowIndex := setRowIndex + rk
  1426. row := sheetNew.Row(rowIndex)
  1427. displayDateCell := row.AddCell()
  1428. tmpData, ok := dataInfoMap[dtv]
  1429. if ok {
  1430. displayDateCell.SetValue(tmpData.Value)
  1431. }
  1432. }
  1433. }
  1434. }
  1435. err = xlsxFile.Save(downLoadnFilePath)
  1436. if err != nil {
  1437. //有指标无数据时先导出一遍空表
  1438. sheet, err := xlsxFile.AddSheet("无数据")
  1439. if err != nil {
  1440. br.Msg = "新增Sheet失败"
  1441. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1442. return
  1443. }
  1444. rowSecName := sheet.AddRow()
  1445. celSecName := rowSecName.AddCell()
  1446. celSecName.SetValue("")
  1447. err = xlsxFile.Save(downLoadnFilePath)
  1448. if err != nil {
  1449. br.Msg = "保存文件失败"
  1450. br.ErrMsg = "保存文件失败"
  1451. return
  1452. }
  1453. }
  1454. fileName := `卓创红期`
  1455. if indexCode != "" && len(secNameList) == 1 {
  1456. fileName = secNameList[0].IndexName
  1457. }
  1458. fileName += time.Now().Format("2006.01.02") + `.xlsx` //文件名称
  1459. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  1460. defer func() {
  1461. os.Remove(downLoadnFilePath)
  1462. }()
  1463. br.Ret = 200
  1464. br.Success = true
  1465. br.Msg = "success"
  1466. }
  1467. // getSciHqClassifyTree 返回卓创红旗的树形结构
  1468. func getSciHqClassifyTree(items []*data_manage.BaseFromSciHqClassifyItem, parentId int) []*data_manage.BaseFromSciHqClassifyItem {
  1469. res := make([]*data_manage.BaseFromSciHqClassifyItem, 0)
  1470. for _, item := range items {
  1471. if item.ParentId == parentId {
  1472. t := new(data_manage.BaseFromSciHqClassifyItem)
  1473. t.ClassifyId = item.ClassifyId
  1474. t.ClassifyName = item.ClassifyName
  1475. t.ParentId = item.ParentId
  1476. t.Level = item.Level
  1477. t.Sort = item.Sort
  1478. t.ModifyTime = item.ModifyTime
  1479. t.CreateTime = item.CreateTime
  1480. t.ClassifyNameEn = item.ClassifyNameEn
  1481. if item.UniqueCode == "" {
  1482. t.UniqueCode = strconv.Itoa(item.ClassifyId)
  1483. }
  1484. t.Children = getSciHqClassifyTree(items, item.ClassifyId)
  1485. res = append(res, t)
  1486. }
  1487. }
  1488. return res
  1489. }