mysteel_chemical_data.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/data_manage/request"
  7. "eta/eta_api/models/data_manage/response"
  8. "eta/eta_api/services/alarm_msg"
  9. "eta/eta_api/services/data"
  10. "eta/eta_api/utils"
  11. "fmt"
  12. "github.com/rdlucklib/rdluck_tools/paging"
  13. "github.com/tealeg/xlsx"
  14. "os"
  15. "path/filepath"
  16. "sort"
  17. "strings"
  18. "time"
  19. )
  20. // MysteelChemicalClassify
  21. // @Title 钢联化工数据分类
  22. // @Description 钢联化工分类接口
  23. // @Success 200 {object} data_manage.BaseFromMysteelChemicalClassifyItems
  24. // @router /mysteel_chemical/classify [get]
  25. func (this *EdbInfoController) MysteelChemicalClassify() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. this.Data["json"] = br
  29. this.ServeJSON()
  30. }()
  31. sysUser := this.SysUser
  32. if sysUser == nil {
  33. br.Msg = "请登录"
  34. br.ErrMsg = "请登录,SysUser Is Empty"
  35. br.Ret = 408
  36. return
  37. }
  38. rootList, err := data_manage.GetBaseFromMysteelChemicalClassifyByParentId(0)
  39. if err != nil && err.Error() != utils.ErrNoRow() {
  40. br.Msg = "获取失败"
  41. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  42. return
  43. }
  44. classifyAll, err := data_manage.GetAllBaseFromMysteelChemicalClassify()
  45. if err != nil && err.Error() != utils.ErrNoRow() {
  46. br.Msg = "获取失败"
  47. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  48. return
  49. }
  50. baseFromMysteelChemicalIndexMap := make(map[int][]*data_manage.BaseFromMysteelChemicalClassifyItems)
  51. allBaseFromMysteelChemicalIndex, err := data_manage.GetMysteelChemicalIndexAll()
  52. if err != nil && err.Error() != utils.ErrNoRow() {
  53. br.Msg = "获取失败"
  54. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  55. return
  56. }
  57. for _, v := range allBaseFromMysteelChemicalIndex {
  58. v.UniqueCode = fmt.Sprint(v.BaseFromMysteelChemicalClassifyId, "_", v.BaseFromMysteelChemicalIndexId)
  59. button := data.GetMysteelChemicalOpButton(sysUser, v.SysUserId)
  60. button.AddButton = false //不管有没有权限,指标都是没有添加按钮的
  61. v.Button = button
  62. baseFromMysteelChemicalIndexMap[v.BaseFromMysteelChemicalClassifyId] = append(baseFromMysteelChemicalIndexMap[v.BaseFromMysteelChemicalClassifyId], v)
  63. }
  64. rootChildMap := make(map[int][]*data_manage.BaseFromMysteelChemicalClassifyItems)
  65. for _, v := range classifyAll {
  66. v.UniqueCode = fmt.Sprint(v.BaseFromMysteelChemicalClassifyId)
  67. button := data.GetMysteelChemicalOpButton(sysUser, v.SysUserId)
  68. button.AddButton = false //不管有没有权限,二级分类都是没有添加按钮的
  69. v.Button = button
  70. rootChildMap[v.ParentId] = append(rootChildMap[v.ParentId], v)
  71. if existItems, ok := baseFromMysteelChemicalIndexMap[v.BaseFromMysteelChemicalClassifyId]; ok {
  72. v.Children = existItems
  73. } else {
  74. items := make([]*data_manage.BaseFromMysteelChemicalClassifyItems, 0)
  75. v.Children = items
  76. }
  77. }
  78. nodeAll := make([]*data_manage.BaseFromMysteelChemicalClassifyItems, 0)
  79. for k, v := range rootList {
  80. button := data.GetMysteelChemicalOpButton(sysUser, v.SysUserId)
  81. rootList[k].Button = button
  82. rootList[k].UniqueCode = fmt.Sprint(v.BaseFromMysteelChemicalClassifyId)
  83. if existItems, ok := rootChildMap[v.BaseFromMysteelChemicalClassifyId]; ok {
  84. v.Children = existItems
  85. } else {
  86. items := make([]*data_manage.BaseFromMysteelChemicalClassifyItems, 0)
  87. v.Children = items
  88. }
  89. nodeAll = append(nodeAll, v)
  90. }
  91. br.Ret = 200
  92. br.Success = true
  93. br.Msg = "获取成功"
  94. br.Data = nodeAll
  95. }
  96. // AddMysteelChemicalClassify
  97. // @Title 新增分类
  98. // @Description 新增分类接口
  99. // @Param request body data_manage.AddBaseFromMysteelChemicalClassifyReq true "type json string"
  100. // @Success 200 Ret=200 保存成功
  101. // @router /mysteel_chemical/classify/add [post]
  102. func (this *EdbClassifyController) AddMysteelChemicalClassify() {
  103. br := new(models.BaseResponse).Init()
  104. defer func() {
  105. this.Data["json"] = br
  106. this.ServeJSON()
  107. }()
  108. var req request.AddBaseFromMysteelChemicalClassifyReq
  109. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  110. if err != nil {
  111. br.Msg = "参数解析异常!"
  112. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  113. return
  114. }
  115. if req.ClassifyName == "" {
  116. br.Msg = "请输入分类名称"
  117. br.IsSendEmail = false
  118. return
  119. }
  120. req.ClassifyName = utils.TrimStr(req.ClassifyName)
  121. req.ClassifyName = strings.Replace(req.ClassifyName, "/t", "", -1)
  122. //添加指标
  123. mysteelChemicalClassifyInfo, err, errMsg := data.AddMysteelChemicalClassify(req.ClassifyName, req.ParentId, req.Level, this.SysUser.AdminId, this.SysUser.RealName)
  124. if errMsg != `` {
  125. br.Msg = errMsg
  126. br.ErrMsg = errMsg
  127. if err != nil {
  128. br.ErrMsg = errMsg + ";Err:" + err.Error()
  129. } else {
  130. br.IsSendEmail = false
  131. }
  132. return
  133. }
  134. // 如果是添加一级分类,那么默认加上 价格、供应、需求、库存 四个二级分类
  135. if mysteelChemicalClassifyInfo.ParentId == 0 {
  136. secondClassifyList := []string{"价格", "供应", "需求", "库存"}
  137. for _, v := range secondClassifyList {
  138. _, _, tmpErrMsg := data.AddMysteelChemicalClassify(v, mysteelChemicalClassifyInfo.BaseFromMysteelChemicalClassifyId, mysteelChemicalClassifyInfo.Level, this.SysUser.AdminId, this.SysUser.RealName)
  139. if tmpErrMsg != `` {
  140. go alarm_msg.SendAlarmMsg("钢联化工-添加一级分类时,默认添加二级分类失败,一级分类名称:"+req.ClassifyName+",二级分类名称:"+v+", Err:"+tmpErrMsg, 3)
  141. return
  142. }
  143. }
  144. }
  145. resp := response.AddMysteelChemicalClassifyResp{
  146. BaseFromMysteelChemicalClassifyId: mysteelChemicalClassifyInfo.BaseFromMysteelChemicalClassifyId,
  147. UniqueCode: fmt.Sprint(mysteelChemicalClassifyInfo.BaseFromMysteelChemicalClassifyId),
  148. }
  149. br.Data = resp
  150. br.Ret = 200
  151. br.Msg = "保存成功"
  152. br.Success = true
  153. br.IsAddLog = true
  154. }
  155. // EditMysteelChemicalClassify
  156. // @Title 修改分类
  157. // @Description 修改分类接口
  158. // @Param request body data_manage.EditBaseFromMysteelChemicalClassifyReq true "type json string"
  159. // @Success 200 Ret=200 修改成功
  160. // @router /mysteel_chemical/classify/edit [post]
  161. func (this *EdbClassifyController) EditMysteelChemicalClassify() {
  162. br := new(models.BaseResponse).Init()
  163. defer func() {
  164. this.Data["json"] = br
  165. this.ServeJSON()
  166. }()
  167. var req request.EditBaseFromMysteelChemicalClassifyReq
  168. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  169. if err != nil {
  170. br.Msg = "参数解析异常!"
  171. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  172. return
  173. }
  174. if req.ClassifyName == "" {
  175. br.Msg = "请输入分类名称"
  176. br.IsSendEmail = false
  177. return
  178. }
  179. if req.BaseFromMysteelChemicalClassifyId <= 0 {
  180. br.Msg = "参数错误"
  181. br.IsSendEmail = false
  182. return
  183. }
  184. err, errMsg := data.EditMysteelChemicalClassify(req.BaseFromMysteelChemicalClassifyId, req.ClassifyName, this.SysUser)
  185. if errMsg != `` {
  186. br.Msg = errMsg
  187. br.ErrMsg = errMsg
  188. if err != nil {
  189. br.ErrMsg = errMsg + ";Err:" + err.Error()
  190. } else {
  191. br.IsSendEmail = false
  192. }
  193. return
  194. }
  195. br.Ret = 200
  196. br.Msg = "保存成功"
  197. br.Success = true
  198. br.IsAddLog = true
  199. }
  200. // MoveMysteelChemicalClassify
  201. // @Title 分类移动接口
  202. // @Description 分类移动接口
  203. // @Success 200 {object} data_manage.MoveBaseFromMysteelChemicalClassifyReq
  204. // @router /mysteel_chemical/classify/move [post]
  205. func (this *EdbClassifyController) MoveMysteelChemicalClassify() {
  206. br := new(models.BaseResponse).Init()
  207. defer func() {
  208. this.Data["json"] = br
  209. this.ServeJSON()
  210. }()
  211. sysUser := this.SysUser
  212. if sysUser == nil {
  213. br.Msg = "请登录"
  214. br.ErrMsg = "请登录,SysUser Is Empty"
  215. br.Ret = 408
  216. return
  217. }
  218. var req request.MoveBaseFromMysteelChemicalClassifyReq
  219. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  220. if err != nil {
  221. br.Msg = "参数解析异常!"
  222. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  223. return
  224. }
  225. if req.BaseFromMysteelChemicalClassifyId <= 0 {
  226. br.Msg = "参数错误"
  227. br.ErrMsg = "分类id小于等于0"
  228. br.IsSendEmail = false
  229. return
  230. }
  231. err, errMsg := data.MoveMysteelChemicalClassify(req.BaseFromMysteelChemicalClassifyId, req.ParentBaseFromMysteelChemicalClassifyId, req.PrevBaseFromMysteelChemicalClassifyId, req.NextBaseFromMysteelChemicalClassifyId, this.SysUser)
  232. if errMsg != `` {
  233. br.Msg = errMsg
  234. br.ErrMsg = errMsg
  235. if err != nil {
  236. br.ErrMsg = err.Error()
  237. } else {
  238. br.IsSendEmail = false
  239. }
  240. return
  241. }
  242. br.Ret = 200
  243. br.Success = true
  244. br.IsAddLog = true
  245. br.Msg = "移动成功"
  246. }
  247. // DeleteMysteelChemicalClassify
  248. // @Title 删除分类
  249. // @Description 删除分类接口
  250. // @Param request body data_manage.DelBaseFromMysteelChemicalClassifyReq true "type json string"
  251. // @Success 200 Ret=200 删除成功
  252. // @router /mysteel_chemical/classify/del [post]
  253. func (this *EdbClassifyController) DeleteMysteelChemicalClassify() {
  254. br := new(models.BaseResponse).Init()
  255. defer func() {
  256. this.Data["json"] = br
  257. this.ServeJSON()
  258. }()
  259. var req request.DelBaseFromMysteelChemicalClassifyReq
  260. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  261. if err != nil {
  262. br.Msg = "参数解析异常!"
  263. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  264. return
  265. }
  266. if req.BaseFromMysteelChemicalClassifyId < 0 {
  267. br.Msg = "参数错误"
  268. br.IsSendEmail = false
  269. return
  270. }
  271. err, errMsg := data.DelMysteelChemicalClassify(req.BaseFromMysteelChemicalClassifyId, this.SysUser)
  272. if errMsg != `` {
  273. br.Msg = errMsg
  274. br.ErrMsg = errMsg
  275. if err != nil {
  276. br.ErrMsg = errMsg + ";Err:" + err.Error()
  277. } else {
  278. br.IsSendEmail = false
  279. }
  280. return
  281. }
  282. br.Ret = 200
  283. br.Msg = "删除成功"
  284. br.Success = true
  285. br.IsAddLog = true
  286. }
  287. // MysteelChemicalSearch
  288. // @Title 钢联化工指标查询
  289. // @Description 钢联化工指标查询
  290. // @Param BaseFromMysteelChemicalClassifyId query int true "分类id"
  291. // @Param Keyword query string true "名称关键词"
  292. // @Success 200 {object} data_manage.LzFrequency
  293. // @router /mysteel_chemical/search [get]
  294. func (this *EdbInfoController) MysteelChemicalSearch() {
  295. br := new(models.BaseResponse).Init()
  296. defer func() {
  297. this.Data["json"] = br
  298. this.ServeJSON()
  299. }()
  300. sysUser := this.SysUser
  301. if sysUser == nil {
  302. br.Msg = "请登录"
  303. br.ErrMsg = "请登录,SysUser Is Empty"
  304. br.Ret = 408
  305. return
  306. }
  307. classifyId, _ := this.GetInt("BaseFromMysteelChemicalClassifyId")
  308. var condition string
  309. var pars []interface{}
  310. if classifyId > 0 {
  311. condition += " AND base_from_mysteel_chemical_classify_id=? "
  312. pars = append(pars, classifyId)
  313. }
  314. keyword := this.GetString("Keyword")
  315. if keyword != `` {
  316. condition += " AND (index_name like ? OR index_code like ?) "
  317. pars = append(pars, "%"+keyword+"%", "%"+keyword+"%")
  318. }
  319. list, err := data_manage.GetMysteelChemicalIndex(condition, pars)
  320. if err != nil {
  321. br.Msg = "获取失败"
  322. br.ErrMsg = "获取失败,Err:" + err.Error()
  323. return
  324. }
  325. for _, v := range list {
  326. v.UniqueCode = fmt.Sprint(v.BaseFromMysteelChemicalClassifyId, "_", v.Id)
  327. }
  328. br.Ret = 200
  329. br.Success = true
  330. br.Msg = "获取成功"
  331. br.Data = list
  332. }
  333. // MysteelChemicalFrequency
  334. // @Title 钢联化工数据频度
  335. // @Description 钢联化工数据频度接口
  336. // @Param BaseFromMysteelChemicalClassifyId query int true "分类id"
  337. // @Param BaseFromMysteelChemicalIndexId query int true "钢联化工指标id"
  338. // @Param Keyword query string true "名称关键词"
  339. // @Success 200 {object} data_manage.LzFrequency
  340. // @router /mysteel_chemical/frequency [get]
  341. func (this *EdbInfoController) MysteelChemicalFrequency() {
  342. br := new(models.BaseResponse).Init()
  343. defer func() {
  344. this.Data["json"] = br
  345. this.ServeJSON()
  346. }()
  347. sysUser := this.SysUser
  348. if sysUser == nil {
  349. br.Msg = "请登录"
  350. br.ErrMsg = "请登录,SysUser Is Empty"
  351. br.Ret = 408
  352. return
  353. }
  354. var condition string
  355. var pars []interface{}
  356. var classifyId int
  357. var frequency string
  358. // 指定指标
  359. indexId, _ := this.GetInt("BaseFromMysteelChemicalIndexId")
  360. if indexId > 0 {
  361. condition += " AND base_from_mysteel_chemical_index_id = ? "
  362. pars = append(pars, indexId)
  363. } else {
  364. classifyId, _ = this.GetInt("BaseFromMysteelChemicalClassifyId")
  365. if classifyId <= 0 {
  366. br.Msg = "请选择分类"
  367. br.ErrMsg = "请选择分类"
  368. return
  369. }
  370. }
  371. if classifyId > 0 {
  372. condition += " AND base_from_mysteel_chemical_classify_id=? "
  373. pars = append(pars, classifyId)
  374. }
  375. if frequency != `` {
  376. condition += " AND frequency=? "
  377. pars = append(pars, frequency)
  378. }
  379. // 关键词搜索
  380. keyword := this.GetString("Keyword")
  381. if keyword != `` {
  382. condition += " AND (index_name like ? OR index_code like ?) "
  383. pars = append(pars, "%"+keyword+"%", "%"+keyword+"%")
  384. }
  385. edbFrequencyList := make([]string, 0)
  386. frequencyList, err := data_manage.GetMysteelChemicalFrequency(condition, pars)
  387. for _, unit := range frequencyList {
  388. edbFrequencyList = append(edbFrequencyList, unit.Frequency)
  389. }
  390. edbFrequencyList = sortEdbFrequency(edbFrequencyList)
  391. for i, v := range edbFrequencyList {
  392. frequencyList[i].Frequency = v
  393. }
  394. if err != nil {
  395. br.Msg = "获取频度失败"
  396. br.ErrMsg = "获取频度失败,Err:" + err.Error()
  397. return
  398. }
  399. br.Ret = 200
  400. br.Success = true
  401. br.Msg = "获取成功"
  402. br.Data = frequencyList
  403. }
  404. // MysteelChemicalData
  405. // @Title 获取钢联化工数据
  406. // @Description 获取钢联化工数据
  407. // @Param BaseFromMysteelChemicalClassifyId query int true "分类id"
  408. // @Param BaseFromMysteelChemicalIndexId query int true "钢联化工指标id"
  409. // @Param Frequency query string true "频度名称"
  410. // @Param Keyword query string true "名称关键词"
  411. // @Param PageSize query int true "每页数据条数"
  412. // @Param CurrentIndex query int true "当前页页码,从1开始"
  413. // @Success 200 {object} data_manage.LzFrequency
  414. // @router /mysteel_chemical/data [get]
  415. func (this *EdbInfoController) MysteelChemicalData() {
  416. br := new(models.BaseResponse).Init()
  417. defer func() {
  418. this.Data["json"] = br
  419. this.ServeJSON()
  420. }()
  421. sysUser := this.SysUser
  422. if sysUser == nil {
  423. br.Msg = "请登录"
  424. br.ErrMsg = "请登录,SysUser Is Empty"
  425. br.Ret = 408
  426. return
  427. }
  428. pageSize, _ := this.GetInt("PageSize")
  429. currentIndex, _ := this.GetInt("CurrentIndex")
  430. var startSize int
  431. if pageSize <= 0 {
  432. pageSize = utils.PageSize20
  433. }
  434. if currentIndex <= 0 {
  435. currentIndex = 1
  436. }
  437. startSize = utils.StartIndex(currentIndex, pageSize)
  438. var condition string
  439. var pars []interface{}
  440. var classifyId int
  441. var frequency string
  442. // 指定指标
  443. indexId, _ := this.GetInt("BaseFromMysteelChemicalIndexId")
  444. if indexId > 0 {
  445. condition += " AND base_from_mysteel_chemical_index_id = ? "
  446. pars = append(pars, indexId)
  447. } else {
  448. classifyId, _ = this.GetInt("BaseFromMysteelChemicalClassifyId")
  449. if classifyId <= 0 {
  450. br.Msg = "请选择分类"
  451. br.ErrMsg = "请选择分类"
  452. br.IsSendEmail = false
  453. return
  454. }
  455. frequency = this.GetString("Frequency")
  456. if frequency == "" {
  457. br.Msg = "请选择频度"
  458. br.ErrMsg = "请选择频度"
  459. br.IsSendEmail = false
  460. return
  461. }
  462. }
  463. if classifyId > 0 {
  464. condition += " AND base_from_mysteel_chemical_classify_id=? "
  465. pars = append(pars, classifyId)
  466. }
  467. if frequency != `` {
  468. condition += " AND frequency=? "
  469. pars = append(pars, frequency)
  470. }
  471. // 关键词搜索
  472. keyword := this.GetString("Keyword")
  473. if keyword != `` {
  474. condition += " AND (index_name like ? OR index_code like ?) "
  475. pars = append(pars, "%"+keyword+"%", "%"+keyword+"%")
  476. }
  477. //获取指标
  478. list, err := data_manage.GetMysteelChemicalIndex(condition, pars)
  479. if err != nil {
  480. br.Msg = "获取数据失败"
  481. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  482. return
  483. }
  484. for k, v := range list {
  485. total, err := data_manage.GetMysteelChemicalIndexDataCount(v.IndexCode)
  486. page := paging.GetPaging(currentIndex, pageSize, total)
  487. dataList, err := data_manage.GetMysteelChemicalIndexData(v.IndexCode, startSize, pageSize)
  488. if err != nil {
  489. br.Msg = "获取数据失败"
  490. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  491. return
  492. }
  493. if dataList == nil {
  494. dataList = make([]*data_manage.MysteelChemicalData, 0)
  495. }
  496. list[k].DataList = dataList
  497. list[k].Paging = page
  498. }
  499. br.Ret = 200
  500. br.Success = true
  501. br.Msg = "获取成功"
  502. br.Data = list
  503. }
  504. // MysteelChemicalExport
  505. // @Title 导出钢联化工数据
  506. // @Description 导出钢联化工数据
  507. // @Param BaseFromMysteelChemicalClassifyId query int true "分类"
  508. // @Param BaseFromMysteelChemicalIndexId query string true "钢联化工指标id"
  509. // @Param Keyword query string true "名称关键词"
  510. // @Success 200 导出成功
  511. // @router /mysteel_chemical/export/dataList [get]
  512. func (this *EdbClassifyController) MysteelChemicalExport() {
  513. br := new(models.BaseResponse).Init()
  514. defer func() {
  515. this.Data["json"] = br
  516. this.ServeJSON()
  517. }()
  518. sysUser := this.SysUser
  519. if sysUser == nil {
  520. br.Msg = "请重新登录"
  521. return
  522. }
  523. classifyId, _ := this.GetInt("BaseFromMysteelChemicalClassifyId") //分类
  524. frequency := this.GetString("Frequency")
  525. dir, _ := os.Executable()
  526. exPath := filepath.Dir(dir)
  527. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  528. xlsxFile := xlsx.NewFile()
  529. var condition string
  530. var pars []interface{}
  531. indexId, _ := this.GetInt("BaseFromMysteelChemicalIndexId")
  532. if indexId > 0 {
  533. condition += " AND base_from_mysteel_chemical_index_id = ? "
  534. pars = append(pars, indexId)
  535. }
  536. if classifyId > 0 {
  537. condition += " AND base_from_mysteel_chemical_classify_id=? "
  538. pars = append(pars, classifyId)
  539. }
  540. if frequency != `` {
  541. condition += " AND frequency=? "
  542. pars = append(pars, frequency)
  543. }
  544. // 关键词搜索
  545. keyword := this.GetString("Keyword")
  546. if keyword != `` {
  547. condition += " AND (index_name like ? OR index_code like ?) "
  548. pars = append(pars, "%"+keyword+"%", "%"+keyword+"%")
  549. }
  550. var frequencies []string
  551. frequencyList, err := data_manage.GetMysteelChemicalFrequency(condition, pars)
  552. for _, v := range frequencyList {
  553. frequencies = append(frequencies, v.Frequency)
  554. }
  555. fileName := `钢联化工数据`
  556. if frequencies == nil {
  557. sheet, err := xlsxFile.AddSheet("无数据")
  558. if err != nil {
  559. br.Msg = "新增Sheet失败"
  560. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  561. return
  562. }
  563. rowSecName := sheet.AddRow()
  564. celSecName := rowSecName.AddCell()
  565. celSecName.SetValue("")
  566. }
  567. for _, frequency := range frequencies {
  568. //获取指标
  569. var tmpCondition string
  570. var tmpPars []interface{}
  571. if indexId > 0 {
  572. tmpCondition += " AND base_from_mysteel_chemical_index_id = ? "
  573. tmpPars = append(pars, indexId)
  574. }
  575. if classifyId > 0 {
  576. tmpCondition += " AND base_from_mysteel_chemical_classify_id=? "
  577. tmpPars = append(pars, classifyId)
  578. }
  579. if frequency != `` {
  580. tmpCondition += " AND frequency=? "
  581. tmpPars = append(pars, frequency)
  582. }
  583. // 关键词搜索
  584. keyword := this.GetString("Keyword")
  585. if keyword != `` {
  586. tmpCondition += " AND (index_name like ? OR index_code like ?) "
  587. tmpPars = append(pars, "%"+keyword+"%", "%"+keyword+"%")
  588. }
  589. //获取指标
  590. secNameList, err := data_manage.GetMysteelChemicalIndex(tmpCondition, tmpPars)
  591. if err != nil {
  592. fmt.Println("获取数据失败,Err:" + err.Error())
  593. return
  594. }
  595. if len(secNameList) <= 0 {
  596. fmt.Println("secNameList长度为0")
  597. return
  598. }
  599. sheetNew, err := xlsxFile.AddSheet(frequency)
  600. if err != nil {
  601. fmt.Println("新增Sheet失败", err.Error())
  602. return
  603. }
  604. //sheetNew.SetColWidth()
  605. //获取指标数据
  606. windRow := sheetNew.AddRow()
  607. secNameRow := sheetNew.AddRow()
  608. indexCodeRow := sheetNew.AddRow()
  609. frequencyRow := sheetNew.AddRow()
  610. unitRow := sheetNew.AddRow()
  611. lastModifyDateRow := sheetNew.AddRow()
  612. //获取分类下指标最大数据量
  613. dataMax, err := data_manage.GetBaseFromMysteelChemicalDataMaxCount(classifyId)
  614. if err != nil {
  615. fmt.Println("获取指标最大数据量失败", err.Error())
  616. return
  617. }
  618. fmt.Println("dataMax:", dataMax)
  619. setRowIndex := 6
  620. for k, sv := range secNameList {
  621. //获取数据
  622. dataList, err := data_manage.GetMysteelChemicalIndexDataByCode(sv.IndexCode)
  623. if err != nil {
  624. br.Msg = "获取数据失败"
  625. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  626. return
  627. }
  628. if len(dataList) > 0 {
  629. windRow.AddCell().SetValue("钢联")
  630. secNameRow.AddCell().SetValue("指标名称")
  631. indexCodeRow.AddCell().SetValue("指标ID")
  632. frequencyRow.AddCell().SetValue("频率")
  633. unitRow.AddCell().SetValue("单位")
  634. lastModifyDateRow.AddCell().SetValue("更新时间")
  635. secNameRow.AddCell().SetValue(sv.IndexName)
  636. indexCodeRow.AddCell().SetValue(sv.IndexCode)
  637. frequencyRow.AddCell().SetValue(sv.FrequencyName)
  638. unitRow.AddCell().SetValue(sv.UnitName)
  639. lastModifyDateRow.AddCell().SetValue(sv.UpdateTime)
  640. windRow.AddCell()
  641. windRow.AddCell()
  642. secNameRow.AddCell()
  643. indexCodeRow.AddCell()
  644. frequencyRow.AddCell()
  645. unitRow.AddCell()
  646. lastModifyDateRow.AddCell()
  647. min := k * 3
  648. sheetNew.SetColWidth(min, min, 15)
  649. if len(dataList) <= 0 {
  650. for n := 0; n < dataMax; n++ {
  651. rowIndex := setRowIndex + n
  652. row := sheetNew.Row(rowIndex)
  653. row.AddCell()
  654. row.AddCell()
  655. row.AddCell()
  656. }
  657. } else {
  658. endRowIndex := 0
  659. for rk, dv := range dataList {
  660. rowIndex := setRowIndex + rk
  661. row := sheetNew.Row(rowIndex)
  662. displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
  663. displayDateCell := row.AddCell()
  664. style := new(xlsx.Style)
  665. style.ApplyAlignment = true
  666. style.Alignment.WrapText = true
  667. displayDateCell.SetStyle(style)
  668. displayDateCell.SetDate(displayDate)
  669. row.AddCell().SetValue(dv.InputValue)
  670. row.AddCell()
  671. endRowIndex = rowIndex
  672. }
  673. if len(dataList) < dataMax {
  674. dataLen := dataMax - len(dataList)
  675. for n := 0; n < dataLen; n++ {
  676. rowIndex := (endRowIndex + 1) + n
  677. row := sheetNew.Row(rowIndex)
  678. row.AddCell()
  679. row.AddCell()
  680. row.AddCell()
  681. }
  682. }
  683. }
  684. }
  685. }
  686. }
  687. err = xlsxFile.Save(downLoadnFilePath)
  688. if err != nil {
  689. //有指标无数据时先导出一遍空表
  690. sheet, err := xlsxFile.AddSheet("无数据")
  691. if err != nil {
  692. br.Msg = "新增Sheet失败"
  693. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  694. return
  695. }
  696. rowSecName := sheet.AddRow()
  697. celSecName := rowSecName.AddCell()
  698. celSecName.SetValue("")
  699. err = xlsxFile.Save(downLoadnFilePath)
  700. if err != nil {
  701. br.Msg = "保存文件失败"
  702. br.ErrMsg = "保存文件失败"
  703. return
  704. }
  705. }
  706. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  707. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  708. defer func() {
  709. os.Remove(downLoadnFilePath)
  710. }()
  711. br.Ret = 200
  712. br.Success = true
  713. br.Msg = "success"
  714. }
  715. // AddMysteelChemical
  716. // @Title 新增钢联化工指标
  717. // @Description 新增钢联化工指标接口
  718. // @Param request body data_manage.AddEdbClassifyReq true "type json string"
  719. // @Success 200 Ret=200 保存成功
  720. // @router /mysteel_chemical/add [post]
  721. func (this *EdbClassifyController) AddMysteelChemical() {
  722. br := new(models.BaseResponse).Init()
  723. defer func() {
  724. this.Data["json"] = br
  725. this.ServeJSON()
  726. }()
  727. var req request.AddBaseFromMysteelChemicalReq
  728. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  729. if err != nil {
  730. br.Msg = "参数解析异常!"
  731. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  732. return
  733. }
  734. if req.IndexCode == "" {
  735. br.Msg = "请输入指标ID"
  736. br.IsSendEmail = false
  737. return
  738. }
  739. if req.BaseFromMysteelChemicalClassifyId <= 0 {
  740. br.Msg = "请选择分类"
  741. br.IsSendEmail = false
  742. return
  743. }
  744. req.IndexCode = utils.TrimStr(req.IndexCode)
  745. req.IndexCode = strings.Replace(req.IndexCode, "\t", "", -1)
  746. // 因为excel获取数据是界面化的,所以同一时间只能存在一个指标添加
  747. key := `mysteel_chemical:add`
  748. res := utils.Rc.SetNX(key, 1, 30*time.Second)
  749. if !res {
  750. br.Msg = "当前已存在添加中的指标,请在30秒后添加"
  751. br.IsSendEmail = false
  752. return
  753. }
  754. //添加指标
  755. mysteelChemicalIndexInfo, err, errMsg := data.AddMysteelChemicalIndex(req.BaseFromMysteelChemicalClassifyId, strings.ToUpper(req.IndexCode), req.UpdateWeek, req.UpdateTime, this.SysUser.AdminId, this.SysUser.RealName)
  756. if errMsg != `` {
  757. br.Msg = errMsg
  758. br.ErrMsg = errMsg
  759. if err != nil {
  760. br.ErrMsg = errMsg + ";Err:" + err.Error()
  761. } else {
  762. br.IsSendEmail = false
  763. }
  764. return
  765. }
  766. resp := response.AddMysteelChemicalIndexInfoResp{
  767. BaseFromMysteelChemicalIndexId: mysteelChemicalIndexInfo.BaseFromMysteelChemicalIndexId,
  768. IndexCode: mysteelChemicalIndexInfo.IndexCode,
  769. UniqueCode: fmt.Sprint(mysteelChemicalIndexInfo.BaseFromMysteelChemicalClassifyId, "_", mysteelChemicalIndexInfo.BaseFromMysteelChemicalIndexId),
  770. }
  771. br.Data = resp
  772. br.Ret = 200
  773. br.Msg = "保存成功"
  774. br.Success = true
  775. br.IsAddLog = true
  776. }
  777. func sortEdbFrequency(frequencyList []string) (newFrequencyList []string) {
  778. var frequencyMap1 = map[string]int{
  779. "日度": 1,
  780. "周度": 2,
  781. "旬度": 3,
  782. "月度": 4,
  783. "季度": 5,
  784. "半年度": 6,
  785. "年度": 7,
  786. }
  787. var frequencyMap2 = map[int]string{
  788. 1: "日度",
  789. 2: "周度",
  790. 3: "旬度",
  791. 4: "月度",
  792. 5: "季度",
  793. 6: "半年度",
  794. 7: "年度",
  795. }
  796. idList := make([]int, 0)
  797. for _, frequency := range frequencyList {
  798. idList = append(idList, frequencyMap1[frequency])
  799. }
  800. sort.Ints(idList)
  801. for _, id := range idList {
  802. newFrequencyList = append(newFrequencyList, frequencyMap2[id])
  803. }
  804. return
  805. }
  806. // EditMysteelChemical
  807. // @Title 编辑钢联化工指标
  808. // @Description 新增钢联化工指标接口
  809. // @Param request body data_manage.AddEdbClassifyReq true "type json string"
  810. // @Success 200 Ret=200 保存成功
  811. // @router /mysteel_chemical/edit [post]
  812. func (this *EdbClassifyController) EditMysteelChemical() {
  813. br := new(models.BaseResponse).Init()
  814. defer func() {
  815. this.Data["json"] = br
  816. this.ServeJSON()
  817. }()
  818. var req request.AddBaseFromMysteelChemicalReq
  819. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  820. if err != nil {
  821. br.Msg = "参数解析异常!"
  822. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  823. return
  824. }
  825. if req.BaseFromMysteelChemicalIndexId <= 0 {
  826. br.Msg = "请选择指标"
  827. br.IsSendEmail = false
  828. return
  829. }
  830. if req.BaseFromMysteelChemicalClassifyId <= 0 {
  831. br.Msg = "请选择分类"
  832. br.IsSendEmail = false
  833. return
  834. }
  835. //编辑指标
  836. mysteelChemicalIndexInfo, err, errMsg := data.EditMysteelChemicalIndex(req.BaseFromMysteelChemicalIndexId, req.BaseFromMysteelChemicalClassifyId, req.UpdateWeek, req.UpdateTime, this.SysUser)
  837. if errMsg != `` {
  838. br.Msg = errMsg
  839. br.ErrMsg = errMsg
  840. if err != nil {
  841. br.ErrMsg = errMsg + ";Err:" + err.Error()
  842. } else {
  843. br.IsSendEmail = false
  844. }
  845. return
  846. }
  847. resp := response.AddMysteelChemicalIndexInfoResp{
  848. BaseFromMysteelChemicalIndexId: mysteelChemicalIndexInfo.BaseFromMysteelChemicalIndexId,
  849. IndexCode: mysteelChemicalIndexInfo.IndexCode,
  850. UniqueCode: fmt.Sprint(mysteelChemicalIndexInfo.BaseFromMysteelChemicalClassifyId, "_", mysteelChemicalIndexInfo.BaseFromMysteelChemicalIndexId),
  851. }
  852. br.Data = resp
  853. br.Ret = 200
  854. br.Msg = "保存成功"
  855. br.Success = true
  856. br.IsAddLog = true
  857. }
  858. // DeleteMysteelChemical
  859. // @Title 删除指标
  860. // @Description 删除指标接口
  861. // @Param request body data_manage.DelBaseFromMysteelChemicalReq true "type json string"
  862. // @Success 200 Ret=200 删除成功
  863. // @router /mysteel_chemical/del [post]
  864. func (this *EdbClassifyController) DeleteMysteelChemical() {
  865. br := new(models.BaseResponse).Init()
  866. defer func() {
  867. this.Data["json"] = br
  868. this.ServeJSON()
  869. }()
  870. var req request.DelBaseFromMysteelChemicalReq
  871. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  872. if err != nil {
  873. br.Msg = "参数解析异常!"
  874. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  875. return
  876. }
  877. if req.BaseFromMysteelChemicalIndexId < 0 {
  878. br.Msg = "参数错误"
  879. br.IsSendEmail = false
  880. return
  881. }
  882. err, errMsg := data.DelMysteelChemical(req.BaseFromMysteelChemicalIndexId, this.SysUser)
  883. if errMsg != `` {
  884. br.Msg = errMsg
  885. br.ErrMsg = errMsg
  886. if err != nil {
  887. br.ErrMsg = errMsg + ";Err:" + err.Error()
  888. } else {
  889. br.IsSendEmail = false
  890. }
  891. return
  892. }
  893. br.Ret = 200
  894. br.Msg = "删除成功"
  895. br.Success = true
  896. br.IsAddLog = true
  897. }
  898. // MoveMysteelChemical
  899. // @Title 指标移动接口
  900. // @Description 分类移动接口
  901. // @Success 200 {object} data_manage.MoveBaseFromMysteelChemicalReq
  902. // @router /mysteel_chemical/move [post]
  903. func (this *EdbClassifyController) MoveMysteelChemical() {
  904. br := new(models.BaseResponse).Init()
  905. defer func() {
  906. this.Data["json"] = br
  907. this.ServeJSON()
  908. }()
  909. sysUser := this.SysUser
  910. if sysUser == nil {
  911. br.Msg = "请登录"
  912. br.ErrMsg = "请登录,SysUser Is Empty"
  913. br.Ret = 408
  914. return
  915. }
  916. var req request.MoveBaseFromMysteelChemicalReq
  917. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  918. if err != nil {
  919. br.Msg = "参数解析异常!"
  920. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  921. return
  922. }
  923. if req.BaseFromMysteelChemicalIndexId <= 0 {
  924. br.Msg = "参数错误"
  925. br.ErrMsg = "指标id小于等于0"
  926. br.IsSendEmail = false
  927. return
  928. }
  929. if req.BaseFromMysteelChemicalClassifyId <= 0 {
  930. br.Msg = "请选择分类"
  931. br.ErrMsg = "请选择分类"
  932. br.IsSendEmail = false
  933. return
  934. }
  935. err, errMsg := data.MoveMysteelChemical(req.BaseFromMysteelChemicalIndexId, req.BaseFromMysteelChemicalClassifyId, req.PrevBaseFromMysteelChemicalIndexId, req.NextBaseFromMysteelChemicalIndexId, this.SysUser)
  936. if errMsg != `` {
  937. br.Msg = errMsg
  938. br.ErrMsg = errMsg
  939. if err != nil {
  940. br.ErrMsg = err.Error()
  941. } else {
  942. br.IsSendEmail = false
  943. }
  944. return
  945. }
  946. br.Ret = 200
  947. br.Success = true
  948. br.IsAddLog = true
  949. br.Msg = "移动成功"
  950. }
  951. // MysteelChemicalDetail
  952. // @Title 钢联化工指标详情
  953. // @Description 钢联化工指标详情接口
  954. // @Param BaseFromMysteelChemicalIndexId query string true "钢联化工指标id"
  955. // @Success 200 {object} data_manage.BaseFromMysteelChemicalClassifyItems
  956. // @router /mysteel_chemical/detail [get]
  957. func (this *EdbInfoController) MysteelChemicalDetail() {
  958. br := new(models.BaseResponse).Init()
  959. defer func() {
  960. this.Data["json"] = br
  961. this.ServeJSON()
  962. }()
  963. sysUser := this.SysUser
  964. if sysUser == nil {
  965. br.Msg = "请登录"
  966. br.ErrMsg = "请登录,SysUser Is Empty"
  967. br.Ret = 408
  968. return
  969. }
  970. indexId, _ := this.GetInt("BaseFromMysteelChemicalIndexId")
  971. if indexId <= 0 {
  972. br.Msg = "请选择指标"
  973. br.IsSendEmail = false
  974. return
  975. }
  976. item, err := data_manage.GetBaseFromMysteelChemicalIndexByIndexId(indexId)
  977. if err != nil {
  978. br.Msg = "获取失败"
  979. br.ErrMsg = "获取失败,Err:" + err.Error()
  980. }
  981. if item.UpdateTime2 != `` {
  982. item.UpdateTime = item.UpdateTime + "," + item.UpdateTime2
  983. }
  984. br.Ret = 200
  985. br.Success = true
  986. br.Msg = "获取成功"
  987. br.Data = item
  988. }
  989. // MysteelChemicalDetail
  990. // @Title 钢联化工指标详情
  991. // @Description 钢联化工指标详情接口
  992. // @Param BaseFromMysteelChemicalIndexId query string true "钢联化工指标id"
  993. // @Success 200 {object} data_manage.BaseFromMysteelChemicalClassifyItems
  994. // @router /mysteel_chemical/refresh [get]
  995. func (this *EdbInfoController) MysteelChemicalRefresh() {
  996. br := new(models.BaseResponse).Init()
  997. defer func() {
  998. this.Data["json"] = br
  999. this.ServeJSON()
  1000. }()
  1001. sysUser := this.SysUser
  1002. if sysUser == nil {
  1003. br.Msg = "请登录"
  1004. br.ErrMsg = "请登录,SysUser Is Empty"
  1005. br.Ret = 408
  1006. return
  1007. }
  1008. // 每次刷新都需要两分钟的等待时间
  1009. setNxKey := utils.CACHE_KEY_MYSTEEL_REFRESH
  1010. if !utils.Rc.SetNX(setNxKey, 1, 2*60*time.Second) {
  1011. br.Msg = "系统正在处理数据生成,请2分钟之后刷新"
  1012. br.IsSendEmail = false
  1013. return
  1014. }
  1015. indexId, _ := this.GetInt("BaseFromMysteelChemicalIndexId")
  1016. if indexId <= 0 {
  1017. br.Msg = "请选择指标"
  1018. br.IsSendEmail = false
  1019. return
  1020. }
  1021. item, err := data_manage.GetBaseFromMysteelChemicalIndexByIndexId(indexId)
  1022. if err != nil {
  1023. br.Msg = "获取失败"
  1024. br.ErrMsg = "获取失败,Err:" + err.Error()
  1025. }
  1026. if item.MergeFilePath == "" {
  1027. br.Msg = "系统正在处理数据生成,请5分钟之后刷新"
  1028. br.IsSendEmail = false
  1029. return
  1030. }
  1031. go data.CurlRefreshMysteelChemicalData(item.MergeFilePath)
  1032. br.Ret = 200
  1033. br.Success = true
  1034. br.Msg = "刷新成功"
  1035. br.Data = item
  1036. }