clarksons_data.go 44 KB

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