smm_data.go 31 KB

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