sci_hq_data.go 42 KB

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