smm_data.go 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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/services/alarm_msg"
  8. "eta/eta_api/services/data"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "github.com/rdlucklib/rdluck_tools/paging"
  12. "github.com/tealeg/xlsx"
  13. "os"
  14. "path/filepath"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. // @Title 有色数据分类
  20. // @Description 有色数据分类接口
  21. // @Success 200 {object} data_manage.SmmClassify
  22. // @router /smm/classify [get]
  23. func (this *EdbInfoController) SmmClassify() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. sysUser := this.SysUser
  30. if sysUser == nil {
  31. br.Msg = "请登录"
  32. br.ErrMsg = "请登录,SysUser Is Empty"
  33. br.Ret = 408
  34. return
  35. }
  36. rootList, err := data_manage.GetBaseFromSmmClassifyByParentId(0)
  37. if err != nil && err.Error() != utils.ErrNoRow() {
  38. br.Msg = "获取失败"
  39. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  40. return
  41. }
  42. classifyAll, err := data_manage.GetAllBaseFromSmmClassify()
  43. if err != nil && err.Error() != utils.ErrNoRow() {
  44. br.Msg = "获取失败"
  45. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  46. return
  47. }
  48. /*baseFromSmmIndexMap := make(map[int][]*data_manage.BaseFromSmmClassifyItems)
  49. allBaseFromSmmIndex, err := data_manage.GetSmmIndexAll()
  50. if err != nil && err.Error() != utils.ErrNoRow() {
  51. br.Msg = "获取失败"
  52. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  53. return
  54. }
  55. for _, v := range allBaseFromSmmIndex {
  56. baseFromSmmIndexMap[v.ClassifyId] = append(baseFromSmmIndexMap[v.ClassifyId], v)
  57. }*/
  58. rootChildMap := make(map[int][]*data_manage.BaseFromSmmClassifyItems)
  59. for _, v := range classifyAll {
  60. v.UniqueCode = strconv.Itoa(v.ClassifyId)
  61. rootChildMap[v.ParentId] = append(rootChildMap[v.ParentId], v)
  62. /*if existItems, ok := baseFromSmmIndexMap[v.ClassifyId]; ok {
  63. v.Children = existItems
  64. } else {
  65. items := make([]*data_manage.BaseFromSmmClassifyItems, 0)
  66. v.Children = items
  67. }*/
  68. }
  69. var ret data_manage.BaseFromSmmClassifyResp
  70. nodeAll := make([]*data_manage.BaseFromSmmClassifyItems, 0)
  71. for _, v := range rootList {
  72. if existItems, ok := rootChildMap[v.ClassifyId]; ok {
  73. v.Children = existItems
  74. } else {
  75. items := make([]*data_manage.BaseFromSmmClassifyItems, 0)
  76. v.Children = items
  77. }
  78. nodeAll = append(nodeAll, v)
  79. }
  80. //查询未分类的指标列表
  81. initClassify := &data_manage.BaseFromSmmClassifyItems{
  82. ClassifyId: 0,
  83. BaseFromSmmIndexId: 0,
  84. ClassifyName: "未分类",
  85. ClassifyNameEn: "Unclassified",
  86. ParentId: 0,
  87. Level: 1,
  88. Sort: 0,
  89. Children: nil,
  90. UniqueCode: "0",
  91. }
  92. /*initIndexList, err := data_manage.GetBaseFromSmmIndexByClassifyId(initClassify.ClassifyId)
  93. if err != nil {
  94. br.Msg = "获取未分类指标失败"
  95. br.ErrMsg = "获取未分类指标失败,Err:" + err.Error()
  96. return
  97. }
  98. for _, v := range initIndexList {
  99. tmp := &data_manage.BaseFromSmmClassifyItems{
  100. ClassifyId: v.ClassifyId,
  101. BaseFromSmmIndexId: v.BaseFromSmmIndexId,
  102. BaseFromSmmIndexCode: v.IndexCode,
  103. ClassifyName: v.IndexName,
  104. ParentId: 0,
  105. Level: 0,
  106. Sort: 0,
  107. Children: nil,
  108. }
  109. initClassify.Children = append(initClassify.Children, tmp)
  110. }*/
  111. finalList := make([]*data_manage.BaseFromSmmClassifyItems, 0)
  112. finalList = append(finalList, initClassify)
  113. finalList = append(finalList, nodeAll...)
  114. ret.List = finalList
  115. br.Ret = 200
  116. br.Success = true
  117. br.Msg = "获取成功"
  118. br.Data = ret
  119. }
  120. // SmmIndexList
  121. // @Title 有色数据指标
  122. // @Description 有色数据指标列表接口
  123. // @Success 200 {object} data_manage.SmmClassify
  124. // @router /smm/index/list [get]
  125. func (this *EdbInfoController) SmmIndexList() {
  126. br := new(models.BaseResponse).Init()
  127. defer func() {
  128. this.Data["json"] = br
  129. this.ServeJSON()
  130. }()
  131. sysUser := this.SysUser
  132. if sysUser == nil {
  133. br.Msg = "请登录"
  134. br.ErrMsg = "请登录,SysUser Is Empty"
  135. br.Ret = 408
  136. return
  137. }
  138. classifyId, _ := this.GetInt("ClassifyId", 0)
  139. list := make([]*data_manage.BaseFromSmmClassifyItems, 0)
  140. indexList, err := data_manage.GetBaseFromSmmIndexByClassifyId(classifyId)
  141. if err != nil {
  142. br.Msg = "获取未分类指标失败"
  143. br.ErrMsg = "获取未分类指标失败,Err:" + err.Error()
  144. return
  145. }
  146. for _, v := range indexList {
  147. tmp := &data_manage.BaseFromSmmClassifyItems{
  148. ClassifyId: v.ClassifyId,
  149. BaseFromSmmIndexId: v.BaseFromSmmIndexId,
  150. BaseFromSmmIndexCode: v.IndexCode,
  151. ClassifyName: v.IndexName,
  152. ParentId: 0,
  153. Level: 0,
  154. Sort: 0,
  155. Children: nil,
  156. }
  157. list = append(list, tmp)
  158. }
  159. var ret data_manage.BaseFromSmmClassifyResp
  160. ret.List = list
  161. br.Ret = 200
  162. br.Success = true
  163. br.Msg = "获取成功"
  164. br.Data = ret
  165. }
  166. // SmmClassifyName
  167. // @Title 有色数据分类名称查询
  168. // @Description 有色数据分类名称查询接口
  169. // @Success 200 {object} data_manage.SmmClassify
  170. // @router /smm/classify/name [get]
  171. func (this *EdbInfoController) SmmClassifyName() {
  172. br := new(models.BaseResponse).Init()
  173. defer func() {
  174. this.Data["json"] = br
  175. this.ServeJSON()
  176. }()
  177. sysUser := this.SysUser
  178. if sysUser == nil {
  179. br.Msg = "请登录"
  180. br.ErrMsg = "请登录,SysUser Is Empty"
  181. br.Ret = 408
  182. return
  183. }
  184. rootList, err := data_manage.GetBaseFromSmmClassifyByParentId(0)
  185. if err != nil && err.Error() != utils.ErrNoRow() {
  186. br.Msg = "获取失败"
  187. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  188. return
  189. }
  190. classifyAll, err := data_manage.GetAllBaseFromSmmClassify()
  191. if err != nil && err.Error() != utils.ErrNoRow() {
  192. br.Msg = "获取失败"
  193. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  194. return
  195. }
  196. rootChildMap := make(map[int][]*data_manage.BaseFromSmmClassifyNameItems)
  197. for _, v := range classifyAll {
  198. tmp := &data_manage.BaseFromSmmClassifyNameItems{
  199. ClassifyId: v.ClassifyId,
  200. ClassifyName: v.ClassifyName,
  201. ParentId: v.ParentId,
  202. Children: nil,
  203. }
  204. rootChildMap[v.ParentId] = append(rootChildMap[v.ParentId], tmp)
  205. }
  206. var ret data_manage.BaseFromSmmClassifyNameResp
  207. nodeAll := make([]*data_manage.BaseFromSmmClassifyNameItems, 0)
  208. for _, v := range rootList {
  209. tmp := &data_manage.BaseFromSmmClassifyNameItems{
  210. ClassifyId: v.ClassifyId,
  211. ClassifyName: v.ClassifyName,
  212. ParentId: v.ParentId,
  213. Children: nil,
  214. }
  215. if existItems, ok := rootChildMap[v.ClassifyId]; ok {
  216. tmp.Children = existItems
  217. } else {
  218. items := make([]*data_manage.BaseFromSmmClassifyNameItems, 0)
  219. tmp.Children = items
  220. }
  221. nodeAll = append(nodeAll, tmp)
  222. }
  223. ret.List = nodeAll
  224. br.Ret = 200
  225. br.Success = true
  226. br.Msg = "获取成功"
  227. br.Data = ret
  228. }
  229. // AddSmmClassify
  230. // @Title 新增分类
  231. // @Description 新增分类接口
  232. // @Param request body data_manage.AddBaseFromSmmClassifyReq true "type json string"
  233. // @Success 200 Ret=200 保存成功
  234. // @router /smm/classify/add [post]
  235. func (this *EdbInfoController) AddSmmClassify() {
  236. br := new(models.BaseResponse).Init()
  237. defer func() {
  238. this.Data["json"] = br
  239. this.ServeJSON()
  240. }()
  241. var req request.AddBaseFromSmmClassifyReq
  242. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  243. if err != nil {
  244. br.Msg = "参数解析异常!"
  245. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  246. return
  247. }
  248. if req.ClassifyName == "" {
  249. br.Msg = "请输入分类名称"
  250. br.IsSendEmail = false
  251. return
  252. }
  253. req.ClassifyName = utils.TrimStr(req.ClassifyName)
  254. req.ClassifyName = strings.Replace(req.ClassifyName, "/t", "", -1)
  255. //添加指标
  256. smmClassifyInfo, err, errMsg := data.AddSmmClassify(req.ClassifyName, req.ParentId, this.SysUser.AdminId, this.SysUser.RealName, this.Lang)
  257. if errMsg != `` {
  258. br.Msg = errMsg
  259. br.ErrMsg = errMsg
  260. if err != nil {
  261. br.ErrMsg = errMsg + ";Err:" + err.Error()
  262. } else {
  263. br.IsSendEmail = false
  264. }
  265. return
  266. }
  267. // 如果是添加一级分类,那么默认加上 价格、供应、需求、库存 四个二级分类
  268. if smmClassifyInfo.ParentId == 0 {
  269. secondClassifyList := []string{"价格", "供应", "需求", "库存"}
  270. for _, v := range secondClassifyList {
  271. _, _, tmpErrMsg := data.AddSmmClassify(v, smmClassifyInfo.ClassifyId, this.SysUser.AdminId, this.SysUser.RealName, this.Lang)
  272. if tmpErrMsg != `` {
  273. go alarm_msg.SendAlarmMsg("SMM原始数据-添加一级分类时,默认添加二级分类失败,一级分类名称:"+req.ClassifyName+",二级分类名称:"+v+", Err:"+tmpErrMsg, 3)
  274. return
  275. }
  276. }
  277. }
  278. resp := data_manage.AddSmmClassifyResp{
  279. ClassifyId: smmClassifyInfo.ClassifyId,
  280. //UniqueCode: fmt.Sprint(SmmClassifyInfo.BaseFromSmmClassifyId),
  281. }
  282. br.Data = resp
  283. br.Ret = 200
  284. br.Msg = "保存成功"
  285. br.Success = true
  286. br.IsAddLog = true
  287. }
  288. // EditSmmClassify
  289. // @Title 修改分类
  290. // @Description 修改分类接口
  291. // @Param request body data_manage.EditBaseFromMysteelChemicalClassifyReq true "type json string"
  292. // @Success 200 Ret=200 修改成功
  293. // @router /smm/classify/edit [post]
  294. func (this *EdbInfoController) EditSmmClassify() {
  295. br := new(models.BaseResponse).Init()
  296. defer func() {
  297. this.Data["json"] = br
  298. this.ServeJSON()
  299. }()
  300. var req request.EditBaseFromSmmClassifyReq
  301. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  302. if err != nil {
  303. br.Msg = "参数解析异常!"
  304. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  305. return
  306. }
  307. if req.ClassifyName == "" {
  308. br.Msg = "请输入分类名称"
  309. br.IsSendEmail = false
  310. return
  311. }
  312. if req.ClassifyId <= 0 {
  313. br.Msg = "参数错误"
  314. br.IsSendEmail = false
  315. return
  316. }
  317. err, errMsg := data.EditSmmClassify(req.ClassifyId, req.ClassifyName, this.Lang, this.SysUser)
  318. if errMsg != `` {
  319. br.Msg = errMsg
  320. br.ErrMsg = errMsg
  321. if err != nil {
  322. br.ErrMsg = errMsg + ";Err:" + err.Error()
  323. } else {
  324. br.IsSendEmail = false
  325. }
  326. return
  327. }
  328. br.Ret = 200
  329. br.Msg = "保存成功"
  330. br.Success = true
  331. br.IsAddLog = true
  332. }
  333. // @Title 有色数据频度
  334. // @Description 有色数据频度接口
  335. // @Param ClassifyId query string true "分类Id"
  336. // @Success 200 {object} data_manage.LzFrequency
  337. // @router /smm/frequency [get]
  338. func (this *EdbInfoController) SmmFrequency() {
  339. br := new(models.BaseResponse).Init()
  340. defer func() {
  341. this.Data["json"] = br
  342. this.ServeJSON()
  343. }()
  344. sysUser := this.SysUser
  345. if sysUser == nil {
  346. br.Msg = "请登录"
  347. br.ErrMsg = "请登录,SysUser Is Empty"
  348. br.Ret = 408
  349. return
  350. }
  351. classifyId, _ := this.GetInt("ClassifyId")
  352. if classifyId < 0 {
  353. br.Msg = "请选择分类"
  354. br.ErrMsg = "请选择分类"
  355. return
  356. }
  357. frequencyList, err := data_manage.GetSmmFrequencyByClassifyId(classifyId)
  358. if err != nil {
  359. br.Msg = "获取频度失败"
  360. br.ErrMsg = "获取频度失败,Err:" + err.Error()
  361. return
  362. }
  363. br.Ret = 200
  364. br.Success = true
  365. br.Msg = "获取成功"
  366. br.Data = frequencyList
  367. }
  368. // MoveSmmClassify
  369. // @Title 分类移动接口
  370. // @Description 分类移动接口
  371. // @Success 200 {object} data_manage.MoveBaseFromMysteelChemicalClassifyReq
  372. // @router /smm/classify/move [post]
  373. func (this *EdbInfoController) MoveSmmClassify() {
  374. br := new(models.BaseResponse).Init()
  375. defer func() {
  376. this.Data["json"] = br
  377. this.ServeJSON()
  378. }()
  379. sysUser := this.SysUser
  380. if sysUser == nil {
  381. br.Msg = "请登录"
  382. br.ErrMsg = "请登录,SysUser Is Empty"
  383. br.Ret = 408
  384. return
  385. }
  386. var req request.MoveBaseFromSmmClassifyReq
  387. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  388. if err != nil {
  389. br.Msg = "参数解析异常!"
  390. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  391. return
  392. }
  393. if req.ClassifyId <= 0 {
  394. br.Msg = "参数错误"
  395. br.ErrMsg = "分类id小于等于0"
  396. br.IsSendEmail = false
  397. return
  398. }
  399. err, errMsg := data.MoveSmmClassify(req.ClassifyId, req.ParentId, req.PrevClassifyId, req.NextClassifyId)
  400. if errMsg != `` {
  401. br.Msg = errMsg
  402. br.ErrMsg = errMsg
  403. if err != nil {
  404. br.ErrMsg = err.Error()
  405. } else {
  406. br.IsSendEmail = false
  407. }
  408. return
  409. }
  410. br.Ret = 200
  411. br.Success = true
  412. br.IsAddLog = true
  413. br.Msg = "移动成功"
  414. }
  415. // DeleteSmmClassify
  416. // @Title 删除分类
  417. // @Description 删除分类接口
  418. // @Param request body data_manage.DelBaseFromSmmClassifyReq true "type json string"
  419. // @Success 200 Ret=200 删除成功
  420. // @router /smm/classify/del [post]
  421. func (this *EdbInfoController) DeleteSmmClassify() {
  422. br := new(models.BaseResponse).Init()
  423. defer func() {
  424. this.Data["json"] = br
  425. this.ServeJSON()
  426. }()
  427. var req request.DelBaseFromSmmClassifyReq
  428. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  429. if err != nil {
  430. br.Msg = "参数解析异常!"
  431. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  432. return
  433. }
  434. if req.ClassifyId < 0 {
  435. br.Msg = "参数错误"
  436. br.IsSendEmail = false
  437. return
  438. }
  439. err, errMsg := data.DelSmmClassify(req.ClassifyId, this.SysUser)
  440. if errMsg != `` {
  441. br.Msg = errMsg
  442. br.ErrMsg = errMsg
  443. if err != nil {
  444. br.ErrMsg = errMsg + ";Err:" + err.Error()
  445. } else {
  446. br.IsSendEmail = false
  447. }
  448. return
  449. }
  450. br.Ret = 200
  451. br.Msg = "删除成功"
  452. br.Success = true
  453. br.IsAddLog = true
  454. }
  455. // MoveSmmData
  456. // @Title SMM指标移动接口
  457. // @Description SMM指标移动接口
  458. // @Success 200 {object} data_manage.MoveBaseFromSmmReq
  459. // @router /smm/move [post]
  460. func (this *EdbInfoController) MoveSmmData() {
  461. br := new(models.BaseResponse).Init()
  462. defer func() {
  463. this.Data["json"] = br
  464. this.ServeJSON()
  465. }()
  466. sysUser := this.SysUser
  467. if sysUser == nil {
  468. br.Msg = "请登录"
  469. br.ErrMsg = "请登录,SysUser Is Empty"
  470. br.Ret = 408
  471. return
  472. }
  473. var req request.MoveBaseFromSmmReq
  474. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  475. if err != nil {
  476. br.Msg = "参数解析异常!"
  477. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  478. return
  479. }
  480. if req.BaseFromSmmIndexId <= 0 {
  481. br.Msg = "参数错误"
  482. br.ErrMsg = "指标id小于等于0"
  483. br.IsSendEmail = false
  484. return
  485. }
  486. if req.ClassifyId < 0 {
  487. br.Msg = "请选择分类"
  488. br.ErrMsg = "请选择分类"
  489. br.IsSendEmail = false
  490. return
  491. }
  492. err, errMsg := data.MoveSmmData2(req.BaseFromSmmIndexId, req.ClassifyId, req.PrevBaseFromSmmIndexId, req.NextBaseFromSmmIndexId)
  493. if errMsg != `` {
  494. br.Msg = errMsg
  495. br.ErrMsg = errMsg
  496. if err != nil {
  497. br.ErrMsg = err.Error()
  498. } else {
  499. br.IsSendEmail = false
  500. }
  501. return
  502. }
  503. br.Ret = 200
  504. br.Success = true
  505. br.IsAddLog = true
  506. br.Msg = "移动成功"
  507. }
  508. // EditSmm
  509. // @Title 编辑有色指标
  510. // @Description 编辑有色指标接口
  511. // @Param request body data_manage.AddEdbClassifyReq true "type json string"
  512. // @Success 200 Ret=200 保存成功
  513. // @router /smm/edit [post]
  514. func (this *EdbInfoController) EditSmm() {
  515. br := new(models.BaseResponse).Init()
  516. defer func() {
  517. this.Data["json"] = br
  518. this.ServeJSON()
  519. }()
  520. var req request.EditBaseFromSmmReq
  521. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  522. if err != nil {
  523. br.Msg = "参数解析异常!"
  524. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  525. return
  526. }
  527. if req.BaseFromSmmIndexId <= 0 {
  528. br.Msg = "请选择指标"
  529. br.IsSendEmail = false
  530. return
  531. }
  532. if req.ClassifyId <= 0 {
  533. br.Msg = "请选择分类"
  534. br.IsSendEmail = false
  535. return
  536. }
  537. //编辑指标
  538. smmIndexInfo, err, errMsg := data.EditSmmIndex(req.BaseFromSmmIndexId, req.ClassifyId, this.SysUser)
  539. if errMsg != `` {
  540. br.Msg = errMsg
  541. br.ErrMsg = errMsg
  542. if err != nil {
  543. br.ErrMsg = errMsg + ";Err:" + err.Error()
  544. } else {
  545. br.IsSendEmail = false
  546. }
  547. return
  548. }
  549. resp := data_manage.EditSmmIndexInfoResp{
  550. BaseFromSmmIndexId: smmIndexInfo.BaseFromSmmIndexId,
  551. IndexCode: smmIndexInfo.IndexCode,
  552. }
  553. br.Data = resp
  554. br.Ret = 200
  555. br.Msg = "保存成功"
  556. br.Success = true
  557. br.IsAddLog = true
  558. }
  559. // @Title 获取钢联数据
  560. // @Description 获取钢联数据接口
  561. // @Param PageSize query int true "每页数据条数"
  562. // @Param CurrentIndex query int true "当前页页码,从1开始"
  563. // @Param ClassifyId query string true "分类id"
  564. // @Param Frequency query string true "频度名称"
  565. // @Success 200 {object} data_manage.LzFrequency
  566. // @router /smm/data [get]
  567. func (this *EdbInfoController) SmmData() {
  568. br := new(models.BaseResponse).Init()
  569. defer func() {
  570. this.Data["json"] = br
  571. this.ServeJSON()
  572. }()
  573. sysUser := this.SysUser
  574. if sysUser == nil {
  575. br.Msg = "请登录"
  576. br.ErrMsg = "请登录,SysUser Is Empty"
  577. br.Ret = 408
  578. return
  579. }
  580. pageSize, _ := this.GetInt("PageSize")
  581. currentIndex, _ := this.GetInt("CurrentIndex")
  582. var startSize int
  583. if pageSize <= 0 {
  584. pageSize = utils.PageSize20
  585. }
  586. if currentIndex <= 0 {
  587. currentIndex = 1
  588. }
  589. startSize = utils.StartIndex(currentIndex, pageSize)
  590. classifyId, _ := this.GetInt("ClassifyId")
  591. if classifyId < 0 {
  592. br.Msg = "请选择分类"
  593. br.ErrMsg = "请选择分类"
  594. return
  595. }
  596. frequency := this.GetString("Frequency")
  597. if frequency == "" {
  598. br.Msg = "请选择频度"
  599. br.ErrMsg = "请选择频度"
  600. return
  601. }
  602. //获取指标
  603. var condition string
  604. var pars []interface{}
  605. if classifyId >= 0 {
  606. condition += ` AND classify_id=? `
  607. pars = append(pars, classifyId)
  608. }
  609. if frequency != "" {
  610. condition += ` AND frequency=? `
  611. pars = append(pars, frequency)
  612. }
  613. smmList, err := data_manage.GetSmmIndex(condition, pars)
  614. if err != nil {
  615. br.Msg = "获取数据失败"
  616. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  617. return
  618. }
  619. resultList := make([]*data_manage.SmmIndexList, 0)
  620. for _, v := range smmList {
  621. product := new(data_manage.SmmIndexList)
  622. product.BaseFromSmmIndexId = v.BaseFromSmmIndexId
  623. product.Unit = v.Unit
  624. product.IndexCode = v.IndexCode
  625. product.IndexName = v.IndexName
  626. product.Frequency = v.Frequency
  627. product.ModifyTime = v.ModifyTime
  628. total, err := data_manage.GetSmmIndexDataCount(v.IndexCode)
  629. page := paging.GetPaging(currentIndex, pageSize, total)
  630. dataList, err := data_manage.GetSmmIndexData(v.IndexCode, startSize, pageSize)
  631. if err != nil {
  632. br.Msg = "获取数据失败"
  633. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  634. return
  635. }
  636. if dataList == nil {
  637. dataList = make([]*data_manage.SmmIndexData, 0)
  638. }
  639. product.DataList = dataList
  640. product.Paging = page
  641. resultList = append(resultList, product)
  642. }
  643. br.Ret = 200
  644. br.Success = true
  645. br.Msg = "获取成功"
  646. br.Data = resultList
  647. }
  648. // SmmSearchList
  649. // @Title SMM模糊搜索
  650. // @Description SMM模糊搜索
  651. // @Param Keyword query string ture "关键字搜索"
  652. // @Success 200 {object} models.BaseResponse
  653. // @router /smm/search_list [get]
  654. func (this *EdbInfoController) SmmSearchList() {
  655. br := new(models.BaseResponse).Init()
  656. defer func() {
  657. this.Data["json"] = br
  658. this.ServeJSON()
  659. }()
  660. sysUser := this.SysUser
  661. if sysUser == nil {
  662. br.Msg = "请重新登录"
  663. return
  664. }
  665. //关键字
  666. keyword := this.GetString("Keyword")
  667. list, err := data_manage.GetSmmItemList(keyword)
  668. if err != nil {
  669. br.ErrMsg = "获取失败,Err:" + err.Error()
  670. br.Msg = "获取失败"
  671. return
  672. }
  673. classifyIds := make([]int, 0)
  674. for _, v := range list {
  675. if v.ClassifyId > 0 {
  676. classifyIds = append(classifyIds, v.ClassifyId)
  677. }
  678. }
  679. if len(classifyIds) > 0 {
  680. // 查询父级分类信息
  681. classifyList, e := data_manage.GetBaseFromSmmClassifyByIds(classifyIds)
  682. if e != nil {
  683. br.Msg = "获取目录信息失败"
  684. br.ErrMsg = "获取失获取目录信息失败,Err:" + err.Error()
  685. return
  686. }
  687. classifyListMap := make(map[int]int, 0)
  688. for _, v := range classifyList {
  689. classifyListMap[v.ClassifyId] = v.ParentId
  690. }
  691. for _, v := range list {
  692. if p, ok := classifyListMap[v.ClassifyId]; ok {
  693. v.ParentClassifyId = p
  694. }
  695. }
  696. }
  697. br.Ret = 200
  698. br.Success = true
  699. br.Msg = "获取成功"
  700. br.Data = list
  701. }
  702. // SmmSingleData
  703. // @Title 获取Smm数据
  704. // @Description 获取smm单条数据接口
  705. // @Param IndexCode query string true "指标唯一编码"
  706. // @Success 200 {object} models.BaseResponse
  707. // @router /smm/single_data [get]
  708. func (this *EdbInfoController) SmmSingleData() {
  709. br := new(models.BaseResponse).Init()
  710. defer func() {
  711. this.Data["json"] = br
  712. this.ServeJSON()
  713. }()
  714. sysUser := this.SysUser
  715. if sysUser == nil {
  716. br.Msg = "请登录"
  717. br.ErrMsg = "请登录,SysUser Is Empty"
  718. br.Ret = 408
  719. return
  720. }
  721. indexCode := this.GetString("IndexCode")
  722. indexInfo, err := data_manage.GetBaseFromSmmIndexByIndexCode(indexCode)
  723. if err != nil {
  724. br.Msg = "获取指标信息失败"
  725. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  726. return
  727. }
  728. dataList, err := data_manage.GetSmmIndexDataByCode(indexCode)
  729. if err != nil {
  730. br.Msg = "获取数据失败"
  731. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  732. return
  733. }
  734. var ret data_manage.SmmSingleDataResp
  735. ret.ClassifyId = indexInfo.ClassifyId
  736. ret.BaseFromSmmIndexId = indexInfo.BaseFromSmmIndexId
  737. ret.IndexCode = indexInfo.IndexCode
  738. ret.IndexName = indexInfo.IndexName
  739. ret.Frequency = indexInfo.Frequency
  740. ret.Interface = indexInfo.Interface
  741. ret.Name = indexInfo.Name
  742. ret.StartTime = indexInfo.StartTime
  743. ret.FinishTime = indexInfo.FinishTime
  744. ret.ApiStartTime = indexInfo.ApiStartTime
  745. ret.ApiUpdateTime = indexInfo.ApiUpdateTime
  746. ret.CreateTime = indexInfo.CreateTime.Format(utils.FormatDateTime)
  747. ret.ModifyTime = indexInfo.ModifyTime.Format(utils.FormatDateTime)
  748. ret.Unit = indexInfo.Unit
  749. ret.Data = dataList
  750. br.Ret = 200
  751. br.Success = true
  752. br.Msg = "获取成功"
  753. br.Data = ret
  754. }
  755. // DeleteSmmData
  756. // @Title 删除指标
  757. // @Description 删除指标接口
  758. // @Param request body data_manage.DelBaseFromSmmReq true "type json string"
  759. // @Success 200 Ret=200 删除成功
  760. // @router /smm/del [post]
  761. func (this *EdbInfoController) DeleteSmmData() {
  762. br := new(models.BaseResponse).Init()
  763. defer func() {
  764. this.Data["json"] = br
  765. this.ServeJSON()
  766. }()
  767. var req request.DelBaseFromSmmReq
  768. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  769. if err != nil {
  770. br.Msg = "参数解析异常!"
  771. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  772. return
  773. }
  774. if req.BaseFromSmmIndexId < 0 {
  775. br.Msg = "参数错误"
  776. br.IsSendEmail = false
  777. return
  778. }
  779. err, errMsg := data.DelSmmData(req.BaseFromSmmIndexId)
  780. if errMsg != `` {
  781. br.Msg = errMsg
  782. br.ErrMsg = errMsg
  783. if err != nil {
  784. br.ErrMsg = errMsg + ";Err:" + err.Error()
  785. } else {
  786. br.IsSendEmail = false
  787. }
  788. return
  789. }
  790. br.Ret = 200
  791. br.Msg = "删除成功"
  792. br.Success = true
  793. br.IsAddLog = true
  794. }
  795. // ResetSmmData
  796. // @Title 指标数据清除分类
  797. // @Description 指标数据清除分类
  798. // @Param request body data_manage.DelBaseFromSmmReq true "type json string"
  799. // @Success 200 Ret=200 操作成功
  800. // @router /smm/reset [post]
  801. func (this *EdbInfoController) ResetSmmData() {
  802. br := new(models.BaseResponse).Init()
  803. defer func() {
  804. this.Data["json"] = br
  805. this.ServeJSON()
  806. }()
  807. var req request.DelBaseFromSmmReq
  808. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  809. if err != nil {
  810. br.Msg = "参数解析异常!"
  811. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  812. return
  813. }
  814. if req.BaseFromSmmIndexId < 0 {
  815. br.Msg = "参数错误"
  816. br.IsSendEmail = false
  817. return
  818. }
  819. err, errMsg := data.ResetSmmData(req.BaseFromSmmIndexId)
  820. if errMsg != `` {
  821. br.Msg = errMsg
  822. br.ErrMsg = errMsg
  823. if err != nil {
  824. br.ErrMsg = errMsg + ";Err:" + err.Error()
  825. } else {
  826. br.IsSendEmail = false
  827. }
  828. return
  829. }
  830. br.Ret = 200
  831. br.Msg = "操作成功"
  832. br.Success = true
  833. br.IsAddLog = true
  834. }
  835. // ExportSmmList
  836. // @Title 导出Smm数据
  837. // @Description 导出Smm数据
  838. // @Param IndexName query string false "名称关键词"
  839. // @Param IndexCode query string false "指标唯一编码"
  840. // @Param ClassifyId query string true "分类"
  841. // @Param Frequency query string true "频度"
  842. // @Param UnitName query string false "单位"
  843. // @Param ModifyTime query string false "更新时间"
  844. // @Success 200 导出成功
  845. // @router /export/smmList [get]
  846. func (this *EdbInfoController) ExportSmmList() {
  847. br := new(models.BaseResponse).Init()
  848. defer func() {
  849. this.Data["json"] = br
  850. this.ServeJSON()
  851. }()
  852. sysUser := this.SysUser
  853. if sysUser == nil {
  854. br.Msg = "请重新登录"
  855. return
  856. }
  857. //typeName := this.GetString("TypeName") //分类
  858. //frequency := this.GetString("Frequency")
  859. indexCode := this.GetString("IndexCode") //指标唯一编码
  860. classifyId, _ := this.GetInt("ClassifyId")
  861. secNameList := make([]*models.EdbdataExportList, 0)
  862. dir, _ := os.Executable()
  863. exPath := filepath.Dir(dir)
  864. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  865. xlsxFile := xlsx.NewFile()
  866. if classifyId >= 0 && indexCode == "" {
  867. frequencies, err := data_manage.GetSmmFrequency(classifyId)
  868. if err != nil {
  869. fmt.Println("GetSmmFrequency err:", err.Error())
  870. utils.FileLog.Info("GetSmmFrequency err:" + err.Error())
  871. return
  872. }
  873. for _, frequency := range frequencies {
  874. //获取指标
  875. var pars []interface{}
  876. pars = append(pars, classifyId)
  877. pars = append(pars, frequency)
  878. condition := " AND classify_id=? AND frequency=? "
  879. secNameList, err := data_manage.GetSmmIndex(condition, pars)
  880. if err != nil {
  881. fmt.Println("获取数据失败,Err:" + err.Error())
  882. return
  883. }
  884. if len(secNameList) <= 0 {
  885. fmt.Println("secNameList长度为0")
  886. return
  887. }
  888. sheetNew, err := xlsxFile.AddSheet(*frequency)
  889. if err != nil {
  890. fmt.Println("新增Sheet失败", err.Error())
  891. return
  892. }
  893. //sheetNew.SetColWidth()
  894. //获取指标数据
  895. windRow := sheetNew.AddRow()
  896. secNameRow := sheetNew.AddRow()
  897. indexCodeRow := sheetNew.AddRow()
  898. frequencyRow := sheetNew.AddRow()
  899. unitRow := sheetNew.AddRow()
  900. lastModifyDateRow := sheetNew.AddRow()
  901. //获取分类下指标最大数据量
  902. dataMax, err := data_manage.GetSmmDataMaxCount(classifyId)
  903. if err != nil {
  904. fmt.Println("获取指标最大数据量失败", err.Error())
  905. return
  906. }
  907. fmt.Println("dataMax:", dataMax)
  908. setRowIndex := 6
  909. for k, sv := range secNameList {
  910. //获取数据
  911. dataList, err := data_manage.GetSmmIndexDataByCode(sv.IndexCode)
  912. if err != nil {
  913. br.Msg = "获取数据失败"
  914. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  915. return
  916. }
  917. if len(dataList) > 0 {
  918. windRow.AddCell().SetValue("Smm")
  919. secNameRow.AddCell().SetValue("指标名称")
  920. indexCodeRow.AddCell().SetValue("指标ID")
  921. frequencyRow.AddCell().SetValue("频率")
  922. unitRow.AddCell().SetValue("单位")
  923. lastModifyDateRow.AddCell().SetValue("更新时间")
  924. secNameRow.AddCell().SetValue(sv.IndexName)
  925. indexCodeRow.AddCell().SetValue(sv.IndexCode)
  926. frequencyRow.AddCell().SetValue(sv.Frequency)
  927. unitRow.AddCell().SetValue(sv.Unit)
  928. lastModifyDateRow.AddCell().SetValue(sv.ModifyTime)
  929. windRow.AddCell()
  930. windRow.AddCell()
  931. secNameRow.AddCell()
  932. indexCodeRow.AddCell()
  933. frequencyRow.AddCell()
  934. unitRow.AddCell()
  935. lastModifyDateRow.AddCell()
  936. min := k * 3
  937. sheetNew.SetColWidth(min, min, 15)
  938. if len(dataList) <= 0 {
  939. for n := 0; n < dataMax; n++ {
  940. rowIndex := setRowIndex + n
  941. row := sheetNew.Row(rowIndex)
  942. row.AddCell()
  943. row.AddCell()
  944. row.AddCell()
  945. }
  946. } else {
  947. endRowIndex := 0
  948. for rk, dv := range dataList {
  949. rowIndex := setRowIndex + rk
  950. row := sheetNew.Row(rowIndex)
  951. displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
  952. displayDateCell := row.AddCell()
  953. style := new(xlsx.Style)
  954. style.ApplyAlignment = true
  955. style.Alignment.WrapText = true
  956. displayDateCell.SetStyle(style)
  957. displayDateCell.SetDate(displayDate)
  958. row.AddCell().SetValue(dv.Value)
  959. row.AddCell()
  960. endRowIndex = rowIndex
  961. }
  962. if len(dataList) < dataMax {
  963. dataLen := dataMax - len(dataList)
  964. for n := 0; n < dataLen; n++ {
  965. rowIndex := (endRowIndex + 1) + n
  966. row := sheetNew.Row(rowIndex)
  967. row.AddCell()
  968. row.AddCell()
  969. row.AddCell()
  970. }
  971. }
  972. }
  973. }
  974. }
  975. }
  976. } else {
  977. frequencies, err := data_manage.GetSmmFrequencyByCode(indexCode)
  978. if err != nil {
  979. fmt.Println("GetSmmFrequencyByCode err:", err.Error())
  980. utils.FileLog.Info("GetSmmFrequencyByCode err:" + err.Error())
  981. return
  982. }
  983. for _, frequency := range frequencies {
  984. //获取数据
  985. dataList, err := data_manage.GetSmmIndexDataByCode(indexCode)
  986. if err != nil {
  987. br.Msg = "获取数据失败"
  988. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  989. return
  990. }
  991. if len(dataList) > 0 {
  992. name := this.GetString("IndexName")
  993. unit := this.GetString("UnitName")
  994. modifyTime := this.GetString("ModifyTime")
  995. sheet, err := xlsxFile.AddSheet(*frequency)
  996. if err != nil {
  997. br.Msg = "新增Sheet失败"
  998. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  999. return
  1000. }
  1001. //获取指标数据
  1002. windRow := sheet.AddRow()
  1003. windRow.AddCell().SetValue("Smm")
  1004. rowSecName := sheet.AddRow()
  1005. celSecName := rowSecName.AddCell()
  1006. celSecName.SetValue("指标名称")
  1007. cellSenName := rowSecName.AddCell()
  1008. cellSenName.SetValue(name)
  1009. indexCodeRow := sheet.AddRow()
  1010. indexCodeRow.AddCell().SetValue("指标ID")
  1011. indexCodeRow.AddCell().SetValue(indexCode)
  1012. rowFrequency := sheet.AddRow()
  1013. celFrequency := rowFrequency.AddCell()
  1014. celFrequency.SetValue("频率")
  1015. cellFrequency := rowFrequency.AddCell()
  1016. cellFrequency.SetValue(frequency)
  1017. rowUnit := sheet.AddRow()
  1018. celUnit := rowUnit.AddCell()
  1019. celUnit.SetValue("单位")
  1020. cellUnit := rowUnit.AddCell()
  1021. cellUnit.SetValue(unit)
  1022. rowModifyDate := sheet.AddRow()
  1023. rowModifyCell := rowModifyDate.AddCell()
  1024. rowModifyCell.SetValue("更新时间")
  1025. rowModifyCell = rowModifyDate.AddCell()
  1026. rowModifyCell.SetValue(modifyTime)
  1027. dataMax, err := data_manage.GetSmmIndexDataCount(indexCode)
  1028. if len(dataList) <= 0 {
  1029. for n := 0; n < dataMax; n++ {
  1030. rowIndex := 6 + n
  1031. row := sheet.Row(rowIndex)
  1032. row.AddCell()
  1033. row.AddCell()
  1034. row.AddCell()
  1035. }
  1036. } else {
  1037. endRowIndex := 0
  1038. for rk, dv := range dataList {
  1039. rowIndex := 6 + rk
  1040. row := sheet.Row(rowIndex)
  1041. displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
  1042. displayDateCell := row.AddCell()
  1043. style := new(xlsx.Style)
  1044. style.ApplyAlignment = true
  1045. style.Alignment.WrapText = true
  1046. displayDateCell.SetStyle(style)
  1047. displayDateCell.SetDate(displayDate)
  1048. row.AddCell().SetValue(dv.Value)
  1049. row.AddCell()
  1050. endRowIndex = rowIndex
  1051. }
  1052. if len(dataList) < dataMax {
  1053. dataLen := dataMax - len(dataList)
  1054. for n := 0; n < dataLen; n++ {
  1055. rowIndex := (endRowIndex + 1) + n
  1056. row := sheet.Row(rowIndex)
  1057. row.AddCell()
  1058. row.AddCell()
  1059. row.AddCell()
  1060. }
  1061. }
  1062. }
  1063. }
  1064. }
  1065. }
  1066. err := xlsxFile.Save(downLoadnFilePath)
  1067. if err != nil {
  1068. //有指标无数据时先导出一遍空表
  1069. sheet, err := xlsxFile.AddSheet("无数据")
  1070. if err != nil {
  1071. br.Msg = "新增Sheet失败"
  1072. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1073. return
  1074. }
  1075. rowSecName := sheet.AddRow()
  1076. celSecName := rowSecName.AddCell()
  1077. celSecName.SetValue("")
  1078. err = xlsxFile.Save(downLoadnFilePath)
  1079. if err != nil {
  1080. br.Msg = "保存文件失败"
  1081. br.ErrMsg = "保存文件失败"
  1082. return
  1083. }
  1084. }
  1085. fileName := `Smm数据`
  1086. if len(secNameList) > 0 {
  1087. fileName = secNameList[0].ClassifyName
  1088. }
  1089. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  1090. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  1091. defer func() {
  1092. os.Remove(downLoadnFilePath)
  1093. }()
  1094. br.Ret = 200
  1095. br.Success = true
  1096. br.Msg = "success"
  1097. }