smm_data.go 29 KB

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