clarksons_data.go 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  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.ParentClassifyId, 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. // 更新es
  952. go data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  953. }
  954. br.Msg = "操作成功"
  955. br.Ret = 200
  956. br.Success = true
  957. br.IsAddLog = true
  958. }
  959. // BatchDelete
  960. // @Title 批量删除
  961. // @Description 批量删除
  962. // @Param request body []*request.DelBaseFromClarksonsReq true "type json string"
  963. // @Success 200 string "操作成功"
  964. // @router /clarksons/edb_info/batch_delete [post]
  965. func (this *ClarksonsDataController) BatchDelete() {
  966. br := new(models.BaseResponse).Init()
  967. defer func() {
  968. if br.ErrMsg == "" {
  969. br.IsSendEmail = false
  970. }
  971. this.Data["json"] = br
  972. this.ServeJSON()
  973. }()
  974. var req []*request.DelBaseFromClarksonsReq
  975. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  976. br.Msg = "参数解析异常!"
  977. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  978. return
  979. }
  980. if len(req) == 0 {
  981. br.Msg = "请选择指标"
  982. return
  983. }
  984. if len(req) > 30 {
  985. br.Msg = "批量添加指标数量不得超过30个"
  986. return
  987. }
  988. var deleteIds []int
  989. for _, v := range req {
  990. if v.BaseFromClarksonsIndexId <= 0 {
  991. continue
  992. }
  993. deleteIds = append(deleteIds, v.BaseFromClarksonsIndexId)
  994. }
  995. existList, err := data.BatchDelClarksonsData(deleteIds)
  996. if err != nil {
  997. br.Msg = "删除失败"
  998. br.ErrMsg = "删除失败,Err:" + err.Error()
  999. return
  1000. }
  1001. br.Data = existList
  1002. br.Msg = "操作成功"
  1003. br.Ret = 200
  1004. br.Success = true
  1005. br.IsAddLog = true
  1006. }
  1007. // BatchEdit
  1008. // @Title 批量编辑
  1009. // @Description 批量编辑
  1010. // @Param request body []*request.DelBaseFromClarksonsReq true "type json string"
  1011. // @Success 200 string "操作成功"
  1012. // @router /clarksons/edb_info/batch_edit [post]
  1013. func (this *ClarksonsDataController) BatchEdit() {
  1014. br := new(models.BaseResponse).Init()
  1015. defer func() {
  1016. if br.ErrMsg == "" {
  1017. br.IsSendEmail = false
  1018. }
  1019. this.Data["json"] = br
  1020. this.ServeJSON()
  1021. }()
  1022. var req []*request.EditBaseFromClarksonsReq
  1023. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1024. br.Msg = "参数解析异常!"
  1025. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  1026. return
  1027. }
  1028. if len(req) == 0 {
  1029. br.Msg = "请选择指标"
  1030. return
  1031. }
  1032. indexIds := make([]int, 0)
  1033. classifyIds := make([]int, 0)
  1034. indexIdToclassifyId := make(map[int]int)
  1035. for _, v := range req {
  1036. if v.BaseFromClarksonsIndexId <= 0 {
  1037. continue
  1038. }
  1039. if v.BaseFromClassifyId <= 0 {
  1040. continue
  1041. }
  1042. indexIds = append(indexIds, v.BaseFromClarksonsIndexId)
  1043. classifyIds = append(classifyIds, v.BaseFromClassifyId)
  1044. indexIdToclassifyId[v.BaseFromClarksonsIndexId] = v.BaseFromClassifyId
  1045. }
  1046. if len(indexIds) == 0 {
  1047. br.Msg = "请选择指标或指定正确的分类"
  1048. return
  1049. }
  1050. indexList, err := data_manage.GetClarksonsIndexListByIndexIds(indexIds)
  1051. if err != nil {
  1052. br.Msg = "编辑失败"
  1053. br.ErrMsg = "获取指标失败,Err:" + err.Error()
  1054. return
  1055. }
  1056. classifySort, err := data_manage.GetClarksonsClassifyMaxSortByClassifyIds(classifyIds)
  1057. if err != nil {
  1058. br.Msg = "编辑失败"
  1059. br.ErrMsg = "获取分类最大排序失败,Err:" + err.Error()
  1060. return
  1061. }
  1062. classifySortMap := make(map[int]int)
  1063. for _, v := range classifySort {
  1064. classifySortMap[v.BaseFromClassifyId] = v.MaxSort
  1065. }
  1066. classifyList, err := data_manage.GetClarksonsClassifyListByIds(classifyIds)
  1067. if err != nil {
  1068. br.Msg = "编辑失败"
  1069. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  1070. return
  1071. }
  1072. existClassifyList := make(map[int]struct{})
  1073. for _, v := range classifyList {
  1074. existClassifyList[v.BaseFromClassifyId] = struct{}{}
  1075. }
  1076. //编辑指标
  1077. updateIndexList := make([]*data_manage.BaseFromClarksonsIndex, 0)
  1078. for _, v := range indexList {
  1079. tmpClassifyId := indexIdToclassifyId[v.BaseFromClarksonsIndexId]
  1080. if _, ok := existClassifyList[tmpClassifyId]; ok {
  1081. v.ClassifyId = tmpClassifyId
  1082. v.Sort = classifySortMap[tmpClassifyId] + 1
  1083. classifySortMap[tmpClassifyId] += 1
  1084. updateIndexList = append(updateIndexList, v)
  1085. }
  1086. }
  1087. err = data_manage.BatchModifyClarksonsIndexClassify(updateIndexList)
  1088. if err != nil {
  1089. br.Msg = "编辑失败"
  1090. br.ErrMsg = "编辑指标失败,Err:" + err.Error()
  1091. return
  1092. }
  1093. br.Msg = "操作成功"
  1094. br.Ret = 200
  1095. br.Success = true
  1096. br.IsAddLog = true
  1097. }
  1098. // EditClarksons
  1099. // @Title 编辑克拉克森指标
  1100. // @Description 编辑克拉克森指标接口
  1101. // @Param request body data_manage.AddEdbClassifyReq true "type json string"
  1102. // @Success 200 Ret=200 保存成功
  1103. // @router /clarksons/edit [post]
  1104. func (this *ClarksonsDataController) EditClarksons() {
  1105. br := new(models.BaseResponse).Init()
  1106. defer func() {
  1107. this.Data["json"] = br
  1108. this.ServeJSON()
  1109. }()
  1110. var req request.EditBaseFromClarksonsReq
  1111. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1112. if err != nil {
  1113. br.Msg = "参数解析异常!"
  1114. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1115. return
  1116. }
  1117. if req.BaseFromClarksonsIndexId <= 0 {
  1118. br.Msg = "请选择指标"
  1119. br.IsSendEmail = false
  1120. return
  1121. }
  1122. if req.BaseFromClassifyId < 0 {
  1123. br.Msg = "请选择分类"
  1124. br.IsSendEmail = false
  1125. return
  1126. }
  1127. //编辑指标
  1128. sciIndexInfo, errMsg, err := data.EditClarksonsIndex(req.BaseFromClarksonsIndexId, req.BaseFromClassifyId)
  1129. if errMsg != `` {
  1130. br.Msg = errMsg
  1131. return
  1132. }
  1133. if err != nil {
  1134. br.Msg = "编辑失败"
  1135. br.ErrMsg = "编辑指标失败,Err:" + err.Error()
  1136. return
  1137. }
  1138. resp := response.EditClarksonsIndexInfoResp{
  1139. BaseFromClarksonsIndexId: sciIndexInfo.BaseFromClarksonsIndexId,
  1140. IndexCode: sciIndexInfo.IndexCode,
  1141. }
  1142. br.Data = resp
  1143. br.Ret = 200
  1144. br.Msg = "保存成功"
  1145. br.Success = true
  1146. br.IsAddLog = true
  1147. }
  1148. // DeleteClarksonsData
  1149. // @Title 删除指标
  1150. // @Description 删除指标接口
  1151. // @Param request body data_manage.DelBaseFromSciReq true "type json string"
  1152. // @Success 200 Ret=200 删除成功
  1153. // @router /clarksons/del [post]
  1154. func (this *ClarksonsDataController) DeleteClarksonsData() {
  1155. br := new(models.BaseResponse).Init()
  1156. defer func() {
  1157. this.Data["json"] = br
  1158. this.ServeJSON()
  1159. }()
  1160. var req request.DelBaseFromClarksonsReq
  1161. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1162. if err != nil {
  1163. br.Msg = "参数解析异常!"
  1164. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1165. return
  1166. }
  1167. if req.BaseFromClarksonsIndexId < 0 {
  1168. br.Msg = "参数错误"
  1169. br.IsSendEmail = false
  1170. return
  1171. }
  1172. err, errMsg := data.DelClarksonsData(req.BaseFromClarksonsIndexId)
  1173. if errMsg != `` {
  1174. br.Msg = errMsg
  1175. br.ErrMsg = errMsg
  1176. if err != nil {
  1177. br.ErrMsg = errMsg + ";Err:" + err.Error()
  1178. } else {
  1179. br.IsSendEmail = false
  1180. }
  1181. return
  1182. }
  1183. br.Ret = 200
  1184. br.Msg = "删除成功"
  1185. br.Success = true
  1186. br.IsAddLog = true
  1187. }
  1188. // ExportClarksonsList
  1189. // @Title 导出Sci数据
  1190. // @Description 导出Sci数据
  1191. // @Param BaseFromClassifyId query int true "关键字搜索"
  1192. // @Param IndexCode query string true "指标编码"
  1193. // @Success 200 导出成功
  1194. // @router /export/clarksonsList [get]
  1195. func (this *ClarksonsDataController) ExportClarksonsList() {
  1196. br := new(models.BaseResponse).Init()
  1197. defer func() {
  1198. this.Data["json"] = br
  1199. this.ServeJSON()
  1200. }()
  1201. sysUser := this.SysUser
  1202. if sysUser == nil {
  1203. br.Msg = "请重新登录"
  1204. return
  1205. }
  1206. classifyId, _ := this.GetInt("BaseFromClassifyId")
  1207. indexCode := this.GetString("IndexCode")
  1208. if classifyId < 0 {
  1209. br.Msg = "请选择分类"
  1210. return
  1211. }
  1212. secNameList := make([]*data_manage.BaseFromClarksonsIndex, 0)
  1213. dir, _ := os.Executable()
  1214. exPath := filepath.Dir(dir)
  1215. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1216. xlsxFile := xlsx.NewFile()
  1217. var condition string
  1218. var pars []interface{}
  1219. if classifyId > 0 {
  1220. childClassify, err := data_manage.GetChildClarksonsClassifyListById(classifyId)
  1221. if err != nil {
  1222. br.Msg = "下载失败"
  1223. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  1224. return
  1225. }
  1226. if len(childClassify) > 0 {
  1227. condition += `AND classify_id IN (` + utils.GetOrmInReplace(len(childClassify)) + `)`
  1228. for _, child := range childClassify {
  1229. pars = append(pars, child.BaseFromClassifyId)
  1230. }
  1231. } else {
  1232. condition += ` AND classify_id=?`
  1233. pars = append(pars, classifyId)
  1234. }
  1235. } else {
  1236. condition += ` AND classify_id=?`
  1237. pars = append(pars, classifyId)
  1238. }
  1239. if indexCode != "" {
  1240. condition += ` AND index_code=? `
  1241. pars = append(pars, indexCode)
  1242. }
  1243. frequencies, err := data_manage.GetClarksonsFrequencyByCondition(condition, pars)
  1244. if err != nil {
  1245. fmt.Println("GetSciFrequency err:", err.Error())
  1246. utils.FileLog.Info("GetSciFrequency err:" + err.Error())
  1247. return
  1248. }
  1249. for _, frequency := range frequencies {
  1250. //获取指标
  1251. secNameList, err = data_manage.GetClarksonsIndexByConditionAndFrequency(condition, *frequency, pars)
  1252. if err != nil {
  1253. fmt.Println("获取数据失败,Err:" + err.Error())
  1254. return
  1255. }
  1256. if len(secNameList) <= 0 {
  1257. fmt.Println("secNameList长度为0")
  1258. return
  1259. }
  1260. sheetNew, err := xlsxFile.AddSheet(*frequency)
  1261. if err != nil {
  1262. fmt.Println("新增Sheet失败", err.Error())
  1263. return
  1264. }
  1265. secNameRow := sheetNew.AddRow()
  1266. frequencyRow := sheetNew.AddRow()
  1267. unitRow := sheetNew.AddRow()
  1268. lastModifyDateRow := sheetNew.AddRow()
  1269. var indexIdList []int
  1270. for _, sv := range secNameList {
  1271. indexIdList = append(indexIdList, sv.BaseFromClarksonsIndexId)
  1272. }
  1273. dataTimeList, err := data_manage.GetClarksonsDataDataTimeByIndexId(indexIdList)
  1274. if err != nil {
  1275. fmt.Println("获取数据时间失败", err.Error())
  1276. return
  1277. }
  1278. // 添加excel左侧指标日期
  1279. setRowIndex := 4
  1280. for rk, dv := range dataTimeList {
  1281. rowIndex := setRowIndex + rk
  1282. row := sheetNew.Row(rowIndex)
  1283. displayDate, _ := time.Parse(utils.FormatDate, dv)
  1284. displayDateCell := row.AddCell()
  1285. style := new(xlsx.Style)
  1286. style.ApplyAlignment = true
  1287. style.Alignment.WrapText = true
  1288. displayDateCell.SetStyle(style)
  1289. displayDateCell.SetDate(displayDate)
  1290. }
  1291. for k, sv := range secNameList {
  1292. //获取数据
  1293. dataList, err := data_manage.GetClarksonsIndexDataByCode(sv.IndexCode)
  1294. if err != nil {
  1295. br.Msg = "获取数据失败"
  1296. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1297. return
  1298. }
  1299. if k == 0 {
  1300. secNameRow.AddCell().SetValue("指标名称")
  1301. frequencyRow.AddCell().SetValue("频率")
  1302. unitRow.AddCell().SetValue("单位")
  1303. lastModifyDateRow.AddCell().SetValue("更新时间")
  1304. min := k * 3
  1305. sheetNew.SetColWidth(min, min, 15)
  1306. }
  1307. if len(dataList) == 0 {
  1308. continue
  1309. }
  1310. secNameRow.AddCell().SetValue(sv.IndexName)
  1311. frequencyRow.AddCell().SetValue(sv.Frequency)
  1312. unitRow.AddCell().SetValue(sv.Unit)
  1313. lastModifyDateRow.AddCell().SetValue(sv.ModifyTime.Format(utils.FormatDate))
  1314. dataInfoMap := make(map[string]*data_manage.BaseFromClarksonsData)
  1315. for _, v := range dataList {
  1316. dataInfoMap[v.DataTime] = v
  1317. }
  1318. for rk, dtv := range dataTimeList {
  1319. rowIndex := setRowIndex + rk
  1320. row := sheetNew.Row(rowIndex)
  1321. displayDateCell := row.AddCell()
  1322. tmpData, ok := dataInfoMap[dtv]
  1323. if ok {
  1324. displayDateCell.SetValue(tmpData.Value)
  1325. }
  1326. }
  1327. }
  1328. }
  1329. err = xlsxFile.Save(downLoadnFilePath)
  1330. if err != nil {
  1331. //有指标无数据时先导出一遍空表
  1332. sheet, err := xlsxFile.AddSheet("无数据")
  1333. if err != nil {
  1334. br.Msg = "新增Sheet失败"
  1335. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1336. return
  1337. }
  1338. rowSecName := sheet.AddRow()
  1339. celSecName := rowSecName.AddCell()
  1340. celSecName.SetValue("")
  1341. err = xlsxFile.Save(downLoadnFilePath)
  1342. if err != nil {
  1343. br.Msg = "保存文件失败"
  1344. br.ErrMsg = "保存文件失败"
  1345. return
  1346. }
  1347. }
  1348. fileName := `克拉克森`
  1349. if indexCode != "" && len(secNameList) == 1 {
  1350. fileName = secNameList[0].IndexName
  1351. }
  1352. fileName += time.Now().Format("2006.01.02") + `.xlsx` //文件名称
  1353. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  1354. defer func() {
  1355. os.Remove(downLoadnFilePath)
  1356. }()
  1357. br.Ret = 200
  1358. br.Success = true
  1359. br.Msg = "success"
  1360. }
  1361. // getClarksonsClassifyTree 返回卓创红旗的树形结构
  1362. func getClarksonsClassifyTree(items []*data_manage.BaseFromClarksonsClassifyItem, parentId int) []*data_manage.BaseFromClarksonsClassifyItem {
  1363. res := make([]*data_manage.BaseFromClarksonsClassifyItem, 0)
  1364. for _, item := range items {
  1365. if item.ParentId == parentId {
  1366. t := new(data_manage.BaseFromClarksonsClassifyItem)
  1367. t.BaseFromClassifyId = item.BaseFromClassifyId
  1368. t.ClassifyName = item.ClassifyName
  1369. t.ParentId = item.ParentId
  1370. t.Level = item.Level
  1371. t.Sort = item.Sort
  1372. t.ModifyTime = item.ModifyTime
  1373. t.CreateTime = item.CreateTime
  1374. t.ClassifyNameEn = item.ClassifyNameEn
  1375. if item.UniqueCode == "" {
  1376. t.UniqueCode = strconv.Itoa(item.BaseFromClassifyId)
  1377. }
  1378. t.Children = getClarksonsClassifyTree(items, item.BaseFromClassifyId)
  1379. res = append(res, t)
  1380. }
  1381. }
  1382. return res
  1383. }
  1384. // @Title 获取克拉克森数据
  1385. // @Description 获取克拉克森数据接口
  1386. // @Param PageSize query int true "每页数据条数"
  1387. // @Param CurrentIndex query int true "当前页页码,从1开始"
  1388. // @Param BaseFromSci99ClassifyId query int true "分类id"
  1389. // @Param KeyWord query string true "关键词"
  1390. // @Success 200 {object} data_source.BaseFromSci99IndexView
  1391. // @router /clarksons/index/data [get]
  1392. func (this *ClarksonsDataController) ClarksonsData() {
  1393. br := new(models.BaseResponse).Init()
  1394. defer func() {
  1395. this.Data["json"] = br
  1396. this.ServeJSON()
  1397. }()
  1398. sysUser := this.SysUser
  1399. if sysUser == nil {
  1400. br.Msg = "请登录"
  1401. br.ErrMsg = "请登录,SysUser Is Empty"
  1402. br.Ret = 408
  1403. return
  1404. }
  1405. pageSize, _ := this.GetInt("PageSize")
  1406. currentIndex, _ := this.GetInt("CurrentIndex")
  1407. var startSize int
  1408. if pageSize <= 0 {
  1409. pageSize = utils.PageSize20
  1410. }
  1411. if currentIndex <= 0 {
  1412. currentIndex = 1
  1413. }
  1414. startSize = utils.StartIndex(currentIndex, pageSize)
  1415. baseFromClarksonsClassifyId, _ := this.GetInt("BaseFromClarksonsClassifyId")
  1416. if baseFromClarksonsClassifyId < 0 {
  1417. br.Msg = "请选择分类"
  1418. br.ErrMsg = "请选择分类"
  1419. return
  1420. }
  1421. keyword := this.GetString("KeyWord")
  1422. //获取指标
  1423. var condition string
  1424. var pars []interface{}
  1425. if baseFromClarksonsClassifyId > 0 {
  1426. condition += ` AND classify_id=? `
  1427. pars = append(pars, baseFromClarksonsClassifyId)
  1428. }
  1429. if keyword != "" {
  1430. condition += ` AND (index_code =? OR index_name LIKE ?) `
  1431. pars = append(pars, keyword)
  1432. pars = append(pars, "%"+keyword+"%")
  1433. }
  1434. sci99List, err := data_manage.GetClarksonsIndex(condition, pars)
  1435. if err != nil {
  1436. br.Msg = "获取数据失败"
  1437. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1438. return
  1439. }
  1440. resultList := make([]*data_manage.BaseFromClarksonsIndexList, 0)
  1441. for _, v := range sci99List {
  1442. product := new(data_manage.BaseFromClarksonsIndexList)
  1443. product.BaseFromClarksonsIndexId = v.BaseFromClarksonsIndexId
  1444. product.ClassifyId = v.ClassifyId
  1445. product.IndexCode = v.IndexCode
  1446. product.IndexName = v.IndexName
  1447. product.Frequency = v.Frequency
  1448. product.Unit = v.Unit
  1449. product.ModifyTime = v.ModifyTime
  1450. modifyTime, err := data_manage.GetClarksonsIndexLatestDate(v.IndexCode)
  1451. if err != nil && err.Error() != utils.ErrNoRow() {
  1452. br.Msg = "获取更新时间失败"
  1453. br.ErrMsg = "获取更新时间失败,Err:" + err.Error()
  1454. return
  1455. }
  1456. product.ModifyTime = modifyTime
  1457. total, err := data_manage.GetClarksonsIndexDataCount(v.IndexCode)
  1458. page := paging.GetPaging(currentIndex, pageSize, total)
  1459. dataList, err := data_manage.GetClarksonsIndexData(v.IndexCode, startSize, pageSize)
  1460. if err != nil {
  1461. br.Msg = "获取数据失败"
  1462. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  1463. return
  1464. }
  1465. if dataList == nil {
  1466. dataList = make([]*data_manage.BaseFromClarksonsData, 0)
  1467. }
  1468. product.DataList = dataList
  1469. product.Paging = page
  1470. resultList = append(resultList, product)
  1471. }
  1472. br.Ret = 200
  1473. br.Success = true
  1474. br.Msg = "获取成功"
  1475. br.Data = resultList
  1476. }
  1477. // IndexPageList
  1478. // @Title 克拉克森指标列表
  1479. // @Description 克拉克森数据指标列表接口
  1480. // @Param PageSize query int true "每页数据条数"
  1481. // @Param CurrentIndex query int true "当前页页码,从1开始"
  1482. // @Param ClassifyId query int true "分类id"
  1483. // @Param IsListAll query int true "是否展示全部"
  1484. // @Success 200 {object} data_manage.BaseFromMysteelChemicalIndexResp
  1485. // @router /clarksons/index/page/list [get]
  1486. func (this *ClarksonsDataController) IndexPageList() {
  1487. br := new(models.BaseResponse).Init()
  1488. defer func() {
  1489. this.Data["json"] = br
  1490. this.ServeJSON()
  1491. }()
  1492. classifyId, _ := this.GetInt("ClassifyId", 0)
  1493. pageSize, _ := this.GetInt("PageSize")
  1494. currrentIndex, _ := this.GetInt("CurrentIndex")
  1495. isListAll, _ := this.GetBool("IsListAll")
  1496. var startSize int
  1497. if pageSize <= 0 {
  1498. pageSize = utils.PageSize20
  1499. }
  1500. if currrentIndex <= 0 {
  1501. currrentIndex = 1
  1502. }
  1503. startSize = utils.StartIndex(currrentIndex, pageSize)
  1504. var total int
  1505. var indexList []*data_manage.BaseFromClarksonsIndexView
  1506. if isListAll {
  1507. tmpTotal, err := data_manage.GetClarksonsIndexCount()
  1508. if err != nil {
  1509. br.Msg = "获取失败"
  1510. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1511. return
  1512. }
  1513. total = tmpTotal
  1514. tmpIndexList, err := data_manage.GetClarksonsIndexByPage(startSize, pageSize)
  1515. if err != nil && err.Error() != utils.ErrNoRow() {
  1516. br.Msg = "获取失败"
  1517. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1518. return
  1519. }
  1520. indexList = tmpIndexList
  1521. } else {
  1522. var classifyIds []int
  1523. if classifyId > 0 {
  1524. tmpClassifyIds, err := data_manage.GetClarksonsChildClassifyIdsById(classifyId)
  1525. if err != nil {
  1526. br.Msg = "获取失败"
  1527. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  1528. return
  1529. }
  1530. if len(tmpClassifyIds) > 0 {
  1531. classifyIds = append(classifyIds, tmpClassifyIds...)
  1532. }
  1533. }
  1534. classifyIds = append(classifyIds, classifyId)
  1535. tmpTotal, err := data_manage.GetClarksonsIndexCountByClassifyId(classifyIds)
  1536. if err != nil {
  1537. br.Msg = "获取失败"
  1538. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1539. return
  1540. }
  1541. total = tmpTotal
  1542. tmpIndexList, err := data_manage.GetClarksonsIndexByClassifyId(classifyIds, startSize, pageSize)
  1543. if err != nil && err.Error() != utils.ErrNoRow() {
  1544. br.Msg = "获取失败"
  1545. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1546. return
  1547. }
  1548. indexList = tmpIndexList
  1549. }
  1550. page := paging.GetPaging(currrentIndex, pageSize, total)
  1551. resp := new(response.ClarksonsIndexPageListResp)
  1552. resp.List = indexList
  1553. resp.Paging = page
  1554. br.Ret = 200
  1555. br.Success = true
  1556. br.Msg = "获取成功"
  1557. br.Data = resp
  1558. }