sci_hq_data.go 41 KB

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