variety_edb.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. package supply_analysis
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/data_manage/request"
  7. "eta/eta_api/models/data_manage/response"
  8. "eta/eta_api/models/data_manage/supply_analysis"
  9. "eta/eta_api/services/data"
  10. supply_analysisServ "eta/eta_api/services/data/supply_analysis"
  11. "eta/eta_api/utils"
  12. "fmt"
  13. "github.com/tealeg/xlsx"
  14. "os"
  15. "path/filepath"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. // EdbList
  21. // @Title 指标列表
  22. // @Description 指标列表
  23. // @Param VarietyId query int true "品种id"
  24. // @Success 200 {object} response.VarietyEdbListResp
  25. // @router /variety/edb/list [get]
  26. func (this *VarietyController) EdbList() {
  27. br := new(models.BaseResponse).Init()
  28. defer func() {
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. varietyId, _ := this.GetInt("VarietyId")
  33. if varietyId <= 0 {
  34. br.Msg = "请选择品种"
  35. br.IsSendEmail = false
  36. return
  37. }
  38. // 获取品种详情
  39. varietyInfo, err := supply_analysis.GetVarietyById(varietyId)
  40. if err != nil {
  41. br.Msg = "查询品种失败"
  42. br.ErrMsg = "查询品种失败;ERR:" + err.Error()
  43. return
  44. }
  45. if varietyInfo == nil {
  46. br.Msg = "不存在该品种或该品种已被删除"
  47. br.ErrMsg = "不存在该品种或该品种已被删除"
  48. br.IsSendEmail = false
  49. return
  50. }
  51. var hasPermission bool //是否有操作权限
  52. // 是否有装置的操作权限
  53. hasPermission, err = supply_analysisServ.HasVarietyPlantPermission(this.SysUser, varietyId)
  54. if err != nil && err.Error() != utils.ErrNoRow() {
  55. br.Msg = "获取失败"
  56. br.ErrMsg = "查询是否有装置的操作权限失败;ERR:" + err.Error()
  57. return
  58. }
  59. var list []*supply_analysis.VarietyEdbInfoItem
  60. list, err = supply_analysis.GetAllVarietyEdbInfoByVarietyId(varietyId)
  61. if err != nil && err.Error() != utils.ErrNoRow() {
  62. br.Msg = "获取失败"
  63. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  64. return
  65. }
  66. // ETA指标库的相关信息
  67. edbInfoAndClassifyMap := make(map[int]*data_manage.EdbAndClassify)
  68. {
  69. edbInfoIdList := make([]int, 0)
  70. for _, v := range list {
  71. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  72. }
  73. edbInfoAndClassifyList, err := data_manage.GetEdbInfoAndClassifyListByEdbIdList(edbInfoIdList)
  74. if err != nil {
  75. br.Msg = "获取失败"
  76. br.ErrMsg = "获取ETA指标的信息失败,Err:" + err.Error()
  77. return
  78. }
  79. for _, v := range edbInfoAndClassifyList {
  80. edbInfoAndClassifyMap[v.EdbInfoId] = v
  81. }
  82. }
  83. for k, v := range list {
  84. button := supply_analysis.VarietyEdbInfoButton{
  85. Copy: true,
  86. Show: true,
  87. }
  88. if tmpItem, ok := edbInfoAndClassifyMap[v.EdbInfoId]; ok {
  89. v.ClassifyName = tmpItem.FirstClassifyName + "/" + tmpItem.SecondClassifyName
  90. v.ClassifyId = tmpItem.SecondClassifyId
  91. v.EdbCode = tmpItem.EdbCode
  92. v.UniqueCode = tmpItem.UniqueCode
  93. } else {
  94. button.Show = false
  95. v.EdbCode = ``
  96. v.EdbInfoId = 0
  97. if hasPermission {
  98. button.Add = true
  99. }
  100. }
  101. v.Button = button
  102. list[k] = v
  103. }
  104. resp := response.VarietyEdbListResp{
  105. List: list,
  106. }
  107. br.Ret = 200
  108. br.Success = true
  109. br.Msg = "获取成功"
  110. br.Data = resp
  111. }
  112. // AddToEdb
  113. // @Title 添加到指标库
  114. // @Description 添加到指标库
  115. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  116. // @Success 200 {object} response.VarietyEdbListResp
  117. // @router /variety/edb/add [post]
  118. func (this *VarietyController) AddToEdb() {
  119. br := new(models.BaseResponse).Init()
  120. defer func() {
  121. this.Data["json"] = br
  122. this.ServeJSON()
  123. }()
  124. sysUser := this.SysUser
  125. if sysUser == nil {
  126. br.Msg = "请登录"
  127. br.ErrMsg = "请登录,SysUser Is Empty"
  128. br.Ret = 408
  129. return
  130. }
  131. var req request.AddEdbInfoReq
  132. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  133. if err != nil {
  134. br.Msg = "参数解析异常!"
  135. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  136. return
  137. }
  138. req.EdbName = strings.Trim(req.EdbName, " ")
  139. if req.VarietyEdbId <= 0 {
  140. br.Msg = "请选择指标"
  141. br.IsSendEmail = false
  142. return
  143. }
  144. cacheKey := "VARIETY_ADD_EDB:" + strconv.Itoa(req.VarietyEdbId)
  145. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  146. br.Msg = "系统处理中,请稍后重试!"
  147. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  148. return
  149. }
  150. defer func() {
  151. utils.Rc.Delete(cacheKey)
  152. }()
  153. if req.EdbName == "" {
  154. br.Msg = "指标名称不能为空"
  155. return
  156. }
  157. if req.Frequency == "" {
  158. br.Msg = "频率不能为空"
  159. return
  160. }
  161. if req.Unit == "" {
  162. br.Msg = "单位不能为空"
  163. return
  164. }
  165. if req.ClassifyId <= 0 {
  166. br.Msg = "请选择分类"
  167. return
  168. }
  169. varietyEdbInfo, err := supply_analysis.GetVarietyEdbInfoByVarietyEdbId(req.VarietyEdbId)
  170. if err != nil {
  171. if err.Error() == utils.ErrNoRow() {
  172. br.Msg = "找不到该指标"
  173. br.IsSendEmail = false
  174. } else {
  175. br.Msg = "添加失败"
  176. br.ErrMsg = "添加失败,ERR:" + err.Error()
  177. }
  178. return
  179. }
  180. var hasPermission bool //是否有操作权限
  181. // 是否有装置的操作权限
  182. hasPermission, err = supply_analysisServ.HasVarietyPlantPermission(this.SysUser, varietyEdbInfo.VarietyId)
  183. if err != nil && err.Error() != utils.ErrNoRow() {
  184. br.Msg = "获取失败"
  185. br.ErrMsg = "查询是否有装置的操作权限失败;ERR:" + err.Error()
  186. return
  187. }
  188. if !hasPermission {
  189. br.Msg = "无操作权限"
  190. br.ErrMsg = "无操作权限"
  191. return
  192. }
  193. if varietyEdbInfo.EdbInfoId > 0 {
  194. tmpEdbInfo, err := data_manage.GetEdbInfoById(varietyEdbInfo.EdbInfoId)
  195. if err != nil && err.Error() != utils.ErrNoRow() {
  196. br.Msg = "添加失败"
  197. br.ErrMsg = "查询ETA指标失败,ERR:" + err.Error()
  198. return
  199. }
  200. if tmpEdbInfo != nil {
  201. br.Msg = "该指标已加入ETA指标库中"
  202. br.ErrMsg = "该指标已加入ETA指标库中"
  203. return
  204. }
  205. }
  206. // 指标入库
  207. randStr := utils.GetRandDigit(4)
  208. prefixCode := "HZ_V"
  209. suffixCode := time.Now().Format("060102") + randStr
  210. edbCode := fmt.Sprint(prefixCode, varietyEdbInfo.Source, suffixCode)
  211. edbInfo, err, errMsg, isSendEmail := data.EdbInfoAdd(utils.DATA_SOURCE_STOCK_PLANT, req.ClassifyId, edbCode, req.EdbName, req.Frequency, req.Unit, varietyEdbInfo.StartDate.Format(utils.FormatDate), varietyEdbInfo.EndDate.Format(utils.FormatDate), sysUser.AdminId, sysUser.RealName)
  212. if err != nil {
  213. br.Msg = "保存失败"
  214. if errMsg != `` {
  215. br.Msg = errMsg
  216. }
  217. br.ErrMsg = err.Error()
  218. br.IsSendEmail = isSendEmail
  219. return
  220. }
  221. // 修改指标信息
  222. {
  223. varietyEdbInfo.EdbInfoId = edbInfo.EdbInfoId
  224. varietyEdbInfo.EdbCode = edbInfo.EdbCode
  225. varietyEdbInfo.EdbName = edbInfo.EdbName
  226. varietyEdbInfo.ModifyTime = time.Now()
  227. err = varietyEdbInfo.Update([]string{"EdbInfoId", "EdbCode", "EdbName", "ModifyTime"})
  228. if err != nil {
  229. br.Msg = "保存失败"
  230. br.ErrMsg = "修改指标信息失败,err:" + err.Error()
  231. return
  232. }
  233. }
  234. // 更新ETA指标库信息
  235. {
  236. data.RefreshEdbData(edbInfo.EdbInfoId, edbInfo.Source, edbInfo.EdbCode, edbInfo.StartDate)
  237. }
  238. //新增操作日志
  239. {
  240. edbLog := new(data_manage.EdbInfoLog)
  241. edbLog.EdbInfoId = edbInfo.EdbInfoId
  242. edbLog.SourceName = edbInfo.SourceName
  243. edbLog.Source = edbInfo.Source
  244. edbLog.EdbCode = edbInfo.EdbCode
  245. edbLog.EdbName = edbInfo.EdbName
  246. edbLog.ClassifyId = edbInfo.ClassifyId
  247. edbLog.SysUserId = sysUser.AdminId
  248. edbLog.SysUserRealName = sysUser.RealName
  249. edbLog.CreateTime = time.Now()
  250. edbLog.Content = string(this.Ctx.Input.RequestBody)
  251. edbLog.Status = "新增指标"
  252. edbLog.Method = this.Ctx.Input.URI()
  253. go data_manage.AddEdbInfoLog(edbLog)
  254. }
  255. //resp := new(data_manage.AddEdbInfoResp)
  256. //resp.EdbInfoId = edbInfo.EdbInfoId
  257. //resp.UniqueCode = edbInfo.UniqueCode
  258. br.Ret = 200
  259. br.Success = true
  260. br.Msg = "添加成功"
  261. //br.Data = resp
  262. br.IsAddLog = true
  263. }
  264. // EdbDataExport
  265. // @Title 导出品种的指标数据
  266. // @Description 商品价格图表分类列表接口
  267. // @Param VarietyId query int true "品种id"
  268. // @Success 200 {object} response.VarietyEdbListResp
  269. // @router /variety/data/export [get]
  270. func (this *VarietyController) EdbDataExport() {
  271. br := new(models.BaseResponse).Init()
  272. defer func() {
  273. this.Data["json"] = br
  274. this.ServeJSON()
  275. }()
  276. sysUser := this.SysUser
  277. if sysUser == nil {
  278. br.Msg = "请重新登录"
  279. return
  280. }
  281. varietyId, _ := this.GetInt("VarietyId")
  282. if varietyId <= 0 {
  283. br.Msg = "请选择品种"
  284. br.IsSendEmail = false
  285. return
  286. }
  287. // 获取品种详情
  288. varietyInfo, err := supply_analysis.GetVarietyById(varietyId)
  289. if err != nil {
  290. br.Msg = "查询品种失败"
  291. br.ErrMsg = "查询品种失败;ERR:" + err.Error()
  292. return
  293. }
  294. if varietyInfo == nil {
  295. br.Msg = "不存在该品种或该品种已被删除"
  296. br.ErrMsg = "不存在该品种或该品种已被删除"
  297. br.IsSendEmail = false
  298. return
  299. }
  300. var list []*supply_analysis.VarietyEdbInfoItem
  301. list, err = supply_analysis.GetAllVarietyEdbInfoByVarietyId(varietyId)
  302. if err != nil && err.Error() != utils.ErrNoRow() {
  303. br.Msg = "获取失败"
  304. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  305. return
  306. }
  307. dataMap := make(map[int]map[time.Time]*supply_analysis.BaseFromStockPlantData)
  308. idList := make([]int, 0)
  309. for _, v := range list {
  310. idList = append(idList, v.VarietyEdbId)
  311. dataList, err := supply_analysis.GetDataVarietyEdbInfoByVarietyId(v.VarietyEdbId)
  312. if err != nil {
  313. br.Msg = "获取数据失败"
  314. br.ErrMsg = "获取数据失败,标识:" + strconv.Itoa(v.VarietyEdbId) + ";err:" + err.Error()
  315. return
  316. }
  317. for _, item := range dataList {
  318. if dataMap[item.VarietyEdbId] == nil {
  319. dataMap[item.VarietyEdbId] = make(map[time.Time]*supply_analysis.BaseFromStockPlantData)
  320. }
  321. dataMap[item.VarietyEdbId][item.DataTime] = item
  322. }
  323. }
  324. dir, _ := os.Executable()
  325. exPath := filepath.Dir(dir)
  326. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  327. xlsxFile := xlsx.NewFile()
  328. if err != nil {
  329. br.Msg = "生成文件失败"
  330. br.ErrMsg = "生成文件失败"
  331. return
  332. }
  333. sheet, err := xlsxFile.AddSheet("存量装置")
  334. rowSecName := sheet.AddRow()
  335. celSecName := rowSecName.AddCell()
  336. celSecName.SetValue("指标名称")
  337. rowFrequency := sheet.AddRow()
  338. celFrequency := rowFrequency.AddCell()
  339. celFrequency.SetValue("频率")
  340. rowUnit := sheet.AddRow()
  341. celUnit := rowUnit.AddCell()
  342. celUnit.SetValue("单位")
  343. rowUpdateTime := sheet.AddRow()
  344. celUpdateTime := rowUpdateTime.AddCell()
  345. celUpdateTime.SetValue("更新时间")
  346. // 日度产量
  347. {
  348. cellSenName := rowSecName.AddCell()
  349. cellSenName.SetValue("日度减产量")
  350. celFrequency := rowFrequency.AddCell()
  351. celFrequency.SetValue("日度")
  352. celUnit := rowUnit.AddCell()
  353. celUnit.SetValue("万吨")
  354. celUpdateTime := rowUpdateTime.AddCell()
  355. celUpdateTime.SetValue(time.Now().Format(utils.FormatDateTime))
  356. }
  357. for _, v := range list {
  358. cellSenName := rowSecName.AddCell()
  359. cellSenName.SetValue(v.EdbName)
  360. celFrequency := rowFrequency.AddCell()
  361. celFrequency.SetValue(v.Frequency)
  362. celUnit := rowUnit.AddCell()
  363. celUnit.SetValue("万吨")
  364. celUpdateTime := rowUpdateTime.AddCell()
  365. celUpdateTime.SetValue(v.ModifyTime)
  366. }
  367. // 获取日期数据
  368. //dateList, err := supply_analysis.GetVarietyEdbDataList(idList)
  369. //if err != nil {
  370. // br.Msg = "获取数据失败"
  371. // br.ErrMsg = "获取数据失败,Err:" + err.Error()
  372. // return
  373. //}
  374. //
  375. //for _, n := range dateList {
  376. // rowData := sheet.AddRow()
  377. // celDate := rowData.AddCell()
  378. // celDate.SetValue(n.DataTime.Format(utils.FormatDate))
  379. //
  380. // for _, varietyEdbId := range idList {
  381. // celData := rowData.AddCell()
  382. // if dataMap[varietyEdbId][n.DataTime] != nil {
  383. // celData.SetFloat(dataMap[varietyEdbId][n.DataTime].Value)
  384. // }
  385. // }
  386. //}
  387. // 日度数据
  388. dayDataMap, startDate, lastDate, err := supply_analysisServ.CalculateByDay(varietyInfo)
  389. if err != nil {
  390. br.Msg = "获取数据失败"
  391. br.ErrMsg = "获取日度数据失败,Err:" + err.Error()
  392. return
  393. }
  394. // 最后日期的月份最后一天
  395. lastMonthDay := time.Date(lastDate.Year(), lastDate.Month(), 1, lastDate.Hour(), lastDate.Minute(), lastDate.Second(), lastDate.Nanosecond(), time.Local).AddDate(0, 1, -1)
  396. for tmpDate := lastMonthDay; !tmpDate.Before(startDate); tmpDate = tmpDate.AddDate(0, 0, -1) {
  397. rowData := sheet.AddRow()
  398. dateCelDate := rowData.AddCell()
  399. dateCelDate.SetValue(tmpDate.Format(utils.FormatDate))
  400. // 日度数据
  401. {
  402. dayCelDate := rowData.AddCell()
  403. if tmpVal, ok := dayDataMap[tmpDate]; ok {
  404. dayCelDate.SetFloat(tmpVal)
  405. }
  406. }
  407. // 计算出来的数据
  408. for _, varietyEdbId := range idList {
  409. celData := rowData.AddCell()
  410. if dataMap[varietyEdbId][tmpDate] != nil {
  411. celData.SetFloat(dataMap[varietyEdbId][tmpDate].Value)
  412. }
  413. }
  414. }
  415. err = xlsxFile.Save(downLoadnFilePath)
  416. if err != nil {
  417. //有指标无数据时先导出一遍空表
  418. sheet, err := xlsxFile.AddSheet("无数据")
  419. if err != nil {
  420. br.Msg = "新增Sheet失败"
  421. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  422. return
  423. }
  424. rowSecName := sheet.AddRow()
  425. celSecName := rowSecName.AddCell()
  426. celSecName.SetValue("")
  427. err = xlsxFile.Save(downLoadnFilePath)
  428. if err != nil {
  429. br.Msg = "保存文件失败"
  430. br.ErrMsg = "保存文件失败"
  431. return
  432. }
  433. }
  434. fileName := varietyInfo.VarietyName + time.Now().Format("06.01.02") + `.xlsx` //文件名称
  435. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  436. defer func() {
  437. os.Remove(downLoadnFilePath)
  438. }()
  439. br.Ret = 200
  440. br.Success = true
  441. br.Msg = "success"
  442. }
  443. // EdbDataList
  444. // @Title 指标数据列表接口
  445. // @Description 指标数据列表接口
  446. // @Param VarietyEdbId query int true "指标id"
  447. // @Success 200 {object} response.VarietyEdbListResp
  448. // @router /variety/edb/data/list [get]
  449. func (this *VarietyController) EdbDataList() {
  450. br := new(models.BaseResponse).Init()
  451. defer func() {
  452. this.Data["json"] = br
  453. this.ServeJSON()
  454. }()
  455. varietyEdbId, _ := this.GetInt("VarietyEdbId")
  456. if varietyEdbId <= 0 {
  457. br.Msg = "请选择指标"
  458. br.IsSendEmail = false
  459. return
  460. }
  461. // 根据品种指标id获取所有的数据
  462. list, err := supply_analysis.GetDataVarietyEdbInfoByVarietyId(varietyEdbId)
  463. if err != nil {
  464. br.Msg = "查询品种失败"
  465. br.ErrMsg = "查询品种失败;ERR:" + err.Error()
  466. return
  467. }
  468. resp := make([]response.VarietyEdbDataListResp, 0)
  469. for _, v := range list {
  470. resp = append(resp, response.VarietyEdbDataListResp{
  471. DataTime: v.DataTime.Format(utils.FormatDate),
  472. Value: v.Value,
  473. })
  474. }
  475. br.Ret = 200
  476. br.Success = true
  477. br.Msg = "获取成功"
  478. br.Data = resp
  479. }
  480. // EdbInfoDataTb
  481. // @Title 获取指标数据同比值
  482. // @Description 获取指标数据
  483. // @Param EdbInfoId query int true "指标ID"
  484. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今,10:全部"
  485. // @Param StartDate query string true "自定义开始日期"
  486. // @Param EndDate query string true "自定义结束日期"
  487. // @Success 200 {object} data_manage.EdbInfoDataResp
  488. // @router /variety/edb/data/tb [get]
  489. func (this *VarietyController) EdbInfoDataTb() {
  490. br := new(models.BaseResponse).Init()
  491. defer func() {
  492. this.Data["json"] = br
  493. this.ServeJSON()
  494. }()
  495. sysUser := this.SysUser
  496. if sysUser == nil {
  497. br.Msg = "请登录"
  498. br.ErrMsg = "请登录,SysUser Is Empty"
  499. br.Ret = 408
  500. return
  501. }
  502. varietyEdbId, _ := this.GetInt("VarietyEdbId")
  503. if varietyEdbId <= 0 {
  504. br.Msg = "请选择指标"
  505. br.IsSendEmail = false
  506. return
  507. }
  508. dateType, _ := this.GetInt("DateType")
  509. if dateType <= 0 {
  510. dateType = 3
  511. }
  512. startDate := this.GetString("StartDate")
  513. endDate := this.GetString("EndDate")
  514. startYear, _ := this.GetInt("StartYear")
  515. varietyEdbInfo, err := supply_analysis.GetVarietyEdbInfoByVarietyEdbId(varietyEdbId)
  516. if err != nil {
  517. br.Msg = "获取失败"
  518. br.Msg = "获取失败,Err:" + err.Error()
  519. return
  520. }
  521. edbInfo := &data_manage.EdbInfo{
  522. EdbInfoId: varietyEdbId,
  523. EdbInfoType: 0,
  524. SourceName: "",
  525. Source: 0,
  526. EdbCode: varietyEdbInfo.EdbCode,
  527. EdbName: varietyEdbInfo.EdbName,
  528. EdbNameEn: "",
  529. EdbNameSource: "",
  530. Frequency: varietyEdbInfo.Frequency,
  531. Unit: ``,
  532. UnitEn: "",
  533. StartDate: varietyEdbInfo.StartDate.Format(utils.FormatDate),
  534. EndDate: varietyEdbInfo.EndDate.Format(utils.FormatDate),
  535. ClassifyId: 0,
  536. SysUserId: 0,
  537. SysUserRealName: "",
  538. UniqueCode: "",
  539. CreateTime: varietyEdbInfo.CreateTime,
  540. ModifyTime: varietyEdbInfo.ModifyTime,
  541. MinValue: 0,
  542. MaxValue: 0,
  543. CalculateFormula: "",
  544. EdbType: 1,
  545. Sort: 0,
  546. LatestDate: varietyEdbInfo.EndDate.Format(utils.FormatDate),
  547. LatestValue: varietyEdbInfo.LatestValue,
  548. MoveType: 0,
  549. MoveFrequency: "",
  550. NoUpdate: 0,
  551. ServerUrl: "",
  552. ChartImage: "",
  553. Calendar: "",
  554. }
  555. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, varietyEdbInfo.EndDate.Year())
  556. var startDateTime time.Time
  557. if startDate != `` {
  558. startDateTime, err = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  559. if err != nil {
  560. br.Msg = "开始日期异常"
  561. br.Msg = "开始日期异常,Err:" + err.Error()
  562. return
  563. }
  564. //同比值的计算方式,是需要与去年同期对比,所以如果用户需要某个时间段的数据的话,获取基础数据时,需要往前面推(1年+兼容的日期频度(目前是35天))
  565. startDate = startDateTime.AddDate(-1, 0, -35).Format(utils.FormatDate)
  566. }
  567. // 根据品种指标id获取所有的数据
  568. tmpDataList, _, _, err := supply_analysisServ.GetCustomEdbData(varietyEdbId, startDate, endDate)
  569. if err != nil {
  570. br.Msg = "获取失败"
  571. br.Msg = "获取失败,Err:" + err.Error()
  572. return
  573. }
  574. // 计算同比值
  575. dataList, minValue, maxValue, err := data.GetEdbDataTbz(edbInfo, tmpDataList, startDateTime)
  576. if err != nil {
  577. br.Msg = "获取失败"
  578. br.Msg = "计算同比值失败,Err:" + err.Error()
  579. return
  580. }
  581. edbInfo.MaxValue = maxValue
  582. edbInfo.MinValue = minValue
  583. resp := new(data_manage.EdbInfoDataResp)
  584. resp.EdbInfo = edbInfo
  585. resp.DataList = dataList
  586. br.Ret = 200
  587. br.Success = true
  588. br.Msg = "获取成功"
  589. br.Data = resp
  590. }
  591. // EdbInfoDataSeasonal
  592. // @Title 获取指标数据(季节性)
  593. // @Description 获取指标数据
  594. // @Param EdbInfoId query int true "指标ID"
  595. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今,10:全部"
  596. // @Param StartDate query string true "自定义开始日期"
  597. // @Param EndDate query string true "自定义结束日期"
  598. // @Param Calendar query string true "公历/农历"
  599. // @Success 200 {object} data_manage.EdbInfoDataResp
  600. // @router /variety/edb/data/seasonal [get]
  601. func (this *VarietyController) EdbInfoDataSeasonal() {
  602. br := new(models.BaseResponse).Init()
  603. defer func() {
  604. this.Data["json"] = br
  605. this.ServeJSON()
  606. }()
  607. sysUser := this.SysUser
  608. if sysUser == nil {
  609. br.Msg = "请登录"
  610. br.ErrMsg = "请登录,SysUser Is Empty"
  611. br.Ret = 408
  612. return
  613. }
  614. varietyEdbId, _ := this.GetInt("VarietyEdbId")
  615. if varietyEdbId <= 0 {
  616. br.Msg = "请选择指标"
  617. br.IsSendEmail = false
  618. return
  619. }
  620. calendar := this.GetString("Calendar")
  621. if calendar == "" {
  622. calendar = "公历"
  623. }
  624. dateType, _ := this.GetInt("DateType")
  625. startDate := this.GetString("StartDate")
  626. endDate := this.GetString("EndDate")
  627. startYear, _ := this.GetInt("StartYear")
  628. if dateType == 0 {
  629. dateType = utils.DateTypeNYears
  630. }
  631. // 指标详情
  632. varietyEdbInfo, err := supply_analysis.GetVarietyEdbInfoByVarietyEdbId(varietyEdbId)
  633. if err != nil {
  634. br.Msg = "获取失败"
  635. br.Msg = "获取失败,Err:" + err.Error()
  636. return
  637. }
  638. edbInfo := &data_manage.EdbInfo{
  639. EdbInfoId: varietyEdbId,
  640. EdbInfoType: 0,
  641. SourceName: "",
  642. Source: 0,
  643. EdbCode: varietyEdbInfo.EdbCode,
  644. EdbName: varietyEdbInfo.EdbName,
  645. EdbNameEn: "",
  646. EdbNameSource: "",
  647. Frequency: varietyEdbInfo.Frequency,
  648. Unit: ``,
  649. UnitEn: "",
  650. StartDate: varietyEdbInfo.StartDate.Format(utils.FormatDate),
  651. EndDate: varietyEdbInfo.EndDate.Format(utils.FormatDate),
  652. ClassifyId: 0,
  653. SysUserId: 0,
  654. SysUserRealName: "",
  655. UniqueCode: "",
  656. CreateTime: varietyEdbInfo.CreateTime,
  657. ModifyTime: varietyEdbInfo.ModifyTime,
  658. MinValue: 0,
  659. MaxValue: 0,
  660. CalculateFormula: "",
  661. EdbType: 1,
  662. Sort: 0,
  663. LatestDate: varietyEdbInfo.EndDate.Format(utils.FormatDate),
  664. LatestValue: varietyEdbInfo.LatestValue,
  665. MoveType: 0,
  666. MoveFrequency: "",
  667. NoUpdate: 0,
  668. ServerUrl: "",
  669. ChartImage: "",
  670. Calendar: "",
  671. }
  672. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, varietyEdbInfo.EndDate.Year())
  673. dataList, minVal, maxVal, err := supply_analysisServ.GetChartEdbSeasonalData(varietyEdbId, calendar, startDate, endDate, edbInfo.LatestDate)
  674. if err != nil {
  675. br.Msg = "获取失败"
  676. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  677. return
  678. }
  679. edbInfo.MinValue = minVal
  680. edbInfo.MaxValue = maxVal
  681. resp := new(data_manage.EdbInfoDataSeasonalResp)
  682. resp.EdbInfo = edbInfo
  683. resp.DataList = dataList
  684. br.Ret = 200
  685. br.Success = true
  686. br.Msg = "获取成功"
  687. br.Data = resp
  688. }
  689. // EdbDataListV2
  690. // @Title 指标数据列表接口
  691. // @Description 指标数据列表接口
  692. // @Param VarietyEdbId query int true "指标id"
  693. // @Success 200 {object} response.VarietyEdbListResp
  694. // @router /variety/edb/data/list/v2 [get]
  695. func (this *VarietyController) EdbDataListV2() {
  696. br := new(models.BaseResponse).Init()
  697. defer func() {
  698. this.Data["json"] = br
  699. this.ServeJSON()
  700. }()
  701. sysUser := this.SysUser
  702. if sysUser == nil {
  703. br.Msg = "请登录"
  704. br.ErrMsg = "请登录,SysUser Is Empty"
  705. br.Ret = 408
  706. return
  707. }
  708. varietyEdbId, _ := this.GetInt("VarietyEdbId")
  709. if varietyEdbId <= 0 {
  710. br.Msg = "请选择指标"
  711. br.IsSendEmail = false
  712. return
  713. }
  714. dateType, _ := this.GetInt("DateType")
  715. if dateType <= 0 {
  716. dateType = 3
  717. }
  718. edbInfoType, _ := this.GetInt("EdbInfoType")
  719. if edbInfoType < 0 {
  720. edbInfoType = 1
  721. }
  722. startDate := this.GetString("StartDate")
  723. endDate := this.GetString("EndDate")
  724. startYear, _ := this.GetInt("StartYear")
  725. varietyEdbInfo, err := supply_analysis.GetVarietyEdbInfoByVarietyEdbId(varietyEdbId)
  726. if err != nil {
  727. br.Msg = "获取失败"
  728. br.Msg = "获取失败,Err:" + err.Error()
  729. return
  730. }
  731. edbInfo := &data_manage.EdbInfo{
  732. EdbInfoId: varietyEdbId,
  733. EdbInfoType: 0,
  734. SourceName: "",
  735. Source: 0,
  736. EdbCode: varietyEdbInfo.EdbCode,
  737. EdbName: varietyEdbInfo.EdbName,
  738. EdbNameEn: "",
  739. EdbNameSource: "",
  740. Frequency: varietyEdbInfo.Frequency,
  741. Unit: ``,
  742. UnitEn: "",
  743. StartDate: varietyEdbInfo.StartDate.Format(utils.FormatDate),
  744. EndDate: varietyEdbInfo.EndDate.Format(utils.FormatDate),
  745. ClassifyId: 0,
  746. SysUserId: 0,
  747. SysUserRealName: "",
  748. UniqueCode: "",
  749. CreateTime: varietyEdbInfo.CreateTime,
  750. ModifyTime: varietyEdbInfo.ModifyTime,
  751. MinValue: 0,
  752. MaxValue: 0,
  753. CalculateFormula: "",
  754. EdbType: 1,
  755. Sort: 0,
  756. LatestDate: varietyEdbInfo.EndDate.Format(utils.FormatDate),
  757. LatestValue: varietyEdbInfo.LatestValue,
  758. MoveType: 0,
  759. MoveFrequency: "",
  760. NoUpdate: 0,
  761. ServerUrl: "",
  762. ChartImage: "",
  763. Calendar: "",
  764. }
  765. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, varietyEdbInfo.EndDate.Year())
  766. var startDateTime time.Time
  767. if startDate != `` {
  768. startDateTime, err = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  769. if err != nil {
  770. br.Msg = "开始日期异常"
  771. br.Msg = "开始日期异常,Err:" + err.Error()
  772. return
  773. }
  774. //同比值的计算方式,是需要与去年同期对比,所以如果用户需要某个时间段的数据的话,获取基础数据时,需要往前面推(1年+兼容的日期频度(目前是35天))
  775. startDate = startDateTime.AddDate(-1, 0, -35).Format(utils.FormatDate)
  776. }
  777. // 根据品种指标id获取所有的数据
  778. dataList, minVal, maxVal, err := supply_analysisServ.GetCustomEdbData(varietyEdbId, startDate, endDate)
  779. if err != nil {
  780. br.Msg = "获取失败"
  781. br.Msg = "获取失败,Err:" + err.Error()
  782. return
  783. }
  784. edbInfo.MinValue = minVal
  785. edbInfo.MaxValue = maxVal
  786. resp := new(data_manage.EdbInfoDataResp)
  787. resp.EdbInfo = edbInfo
  788. resp.DataList = dataList
  789. br.Ret = 200
  790. br.Success = true
  791. br.Msg = "获取成功"
  792. br.Data = resp
  793. }