variety_edb.go 25 KB

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