variety_edb.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. package supply_analysis
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/tealeg/xlsx"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/data_manage"
  8. "hongze/hz_crm_api/models/data_manage/request"
  9. "hongze/hz_crm_api/models/data_manage/response"
  10. "hongze/hz_crm_api/models/data_manage/supply_analysis"
  11. "hongze/hz_crm_api/services/data"
  12. supply_analysisServ "hongze/hz_crm_api/services/data/supply_analysis"
  13. "hongze/hz_crm_api/utils"
  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. startDate, endDate = utils.GetDateByDateType(dateType, startDate, endDate)
  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. var startDateTime time.Time
  556. if startDate != `` {
  557. startDateTime, err = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  558. if err != nil {
  559. br.Msg = "开始日期异常"
  560. br.Msg = "开始日期异常,Err:" + err.Error()
  561. return
  562. }
  563. //同比值的计算方式,是需要与去年同期对比,所以如果用户需要某个时间段的数据的话,获取基础数据时,需要往前面推(1年+兼容的日期频度(目前是35天))
  564. startDate = startDateTime.AddDate(-1, 0, -35).Format(utils.FormatDate)
  565. }
  566. // 根据品种指标id获取所有的数据
  567. tmpDataList, _, _, err := supply_analysisServ.GetCustomEdbData(varietyEdbId, startDate, endDate)
  568. if err != nil {
  569. br.Msg = "获取失败"
  570. br.Msg = "获取失败,Err:" + err.Error()
  571. return
  572. }
  573. // 计算同比值
  574. dataList, minValue, maxValue, err := data.GetEdbDataTbz(edbInfo, tmpDataList, startDateTime)
  575. if err != nil {
  576. br.Msg = "获取失败"
  577. br.Msg = "计算同比值失败,Err:" + err.Error()
  578. return
  579. }
  580. edbInfo.MaxValue = maxValue
  581. edbInfo.MinValue = minValue
  582. resp := new(data_manage.EdbInfoDataResp)
  583. resp.EdbInfo = edbInfo
  584. resp.DataList = dataList
  585. br.Ret = 200
  586. br.Success = true
  587. br.Msg = "获取成功"
  588. br.Data = resp
  589. }
  590. // EdbInfoDataSeasonal
  591. // @Title 获取指标数据(季节性)
  592. // @Description 获取指标数据
  593. // @Param EdbInfoId query int true "指标ID"
  594. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今,10:全部"
  595. // @Param StartDate query string true "自定义开始日期"
  596. // @Param EndDate query string true "自定义结束日期"
  597. // @Param Calendar query string true "公历/农历"
  598. // @Success 200 {object} data_manage.EdbInfoDataResp
  599. // @router /variety/edb/data/seasonal [get]
  600. func (this *VarietyController) EdbInfoDataSeasonal() {
  601. br := new(models.BaseResponse).Init()
  602. defer func() {
  603. this.Data["json"] = br
  604. this.ServeJSON()
  605. }()
  606. sysUser := this.SysUser
  607. if sysUser == nil {
  608. br.Msg = "请登录"
  609. br.ErrMsg = "请登录,SysUser Is Empty"
  610. br.Ret = 408
  611. return
  612. }
  613. varietyEdbId, _ := this.GetInt("VarietyEdbId")
  614. if varietyEdbId <= 0 {
  615. br.Msg = "请选择指标"
  616. br.IsSendEmail = false
  617. return
  618. }
  619. calendar := this.GetString("Calendar")
  620. if calendar == "" {
  621. calendar = "公历"
  622. }
  623. startDate := this.GetString("StartDate")
  624. endDate := this.GetString("EndDate")
  625. if startDate != `` {
  626. startDate = startDate + "-01"
  627. } else {
  628. fivePre := time.Now().AddDate(-4, 0, 0).Year()
  629. startDate = strconv.Itoa(fivePre) + "-01-01"
  630. }
  631. if endDate != `` {
  632. seasonEndDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, endDate+"-01", time.Local)
  633. if tmpErr != nil {
  634. br.Msg = "获取失败"
  635. br.ErrMsg = "获取图表,指标信息失败,Err:" + tmpErr.Error()
  636. return
  637. }
  638. endDate = seasonEndDateTime.AddDate(0, 1, -1).Format(utils.FormatDate)
  639. }
  640. // 指标详情
  641. varietyEdbInfo, err := supply_analysis.GetVarietyEdbInfoByVarietyEdbId(varietyEdbId)
  642. if err != nil {
  643. br.Msg = "获取失败"
  644. br.Msg = "获取失败,Err:" + err.Error()
  645. return
  646. }
  647. edbInfo := &data_manage.EdbInfo{
  648. EdbInfoId: varietyEdbId,
  649. EdbInfoType: 0,
  650. SourceName: "",
  651. Source: 0,
  652. EdbCode: varietyEdbInfo.EdbCode,
  653. EdbName: varietyEdbInfo.EdbName,
  654. EdbNameEn: "",
  655. EdbNameSource: "",
  656. Frequency: varietyEdbInfo.Frequency,
  657. Unit: ``,
  658. UnitEn: "",
  659. StartDate: varietyEdbInfo.StartDate.Format(utils.FormatDate),
  660. EndDate: varietyEdbInfo.EndDate.Format(utils.FormatDate),
  661. ClassifyId: 0,
  662. SysUserId: 0,
  663. SysUserRealName: "",
  664. UniqueCode: "",
  665. CreateTime: varietyEdbInfo.CreateTime,
  666. ModifyTime: varietyEdbInfo.ModifyTime,
  667. MinValue: 0,
  668. MaxValue: 0,
  669. CalculateFormula: "",
  670. EdbType: 1,
  671. Sort: 0,
  672. LatestDate: varietyEdbInfo.EndDate.Format(utils.FormatDate),
  673. LatestValue: varietyEdbInfo.LatestValue,
  674. MoveType: 0,
  675. MoveFrequency: "",
  676. NoUpdate: 0,
  677. ServerUrl: "",
  678. ChartImage: "",
  679. Calendar: "",
  680. }
  681. dataList, minVal, maxVal, err := supply_analysisServ.GetChartEdbSeasonalData(varietyEdbId, calendar, startDate, endDate, edbInfo.LatestDate)
  682. if err != nil {
  683. br.Msg = "获取失败"
  684. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  685. return
  686. }
  687. edbInfo.MinValue = minVal
  688. edbInfo.MaxValue = maxVal
  689. resp := new(data_manage.EdbInfoDataSeasonalResp)
  690. resp.EdbInfo = edbInfo
  691. resp.DataList = dataList
  692. br.Ret = 200
  693. br.Success = true
  694. br.Msg = "获取成功"
  695. br.Data = resp
  696. }
  697. // EdbDataListV2
  698. // @Title 指标数据列表接口
  699. // @Description 指标数据列表接口
  700. // @Param VarietyEdbId query int true "指标id"
  701. // @Success 200 {object} response.VarietyEdbListResp
  702. // @router /variety/edb/data/list/v2 [get]
  703. func (this *VarietyController) EdbDataListV2() {
  704. br := new(models.BaseResponse).Init()
  705. defer func() {
  706. this.Data["json"] = br
  707. this.ServeJSON()
  708. }()
  709. sysUser := this.SysUser
  710. if sysUser == nil {
  711. br.Msg = "请登录"
  712. br.ErrMsg = "请登录,SysUser Is Empty"
  713. br.Ret = 408
  714. return
  715. }
  716. varietyEdbId, _ := this.GetInt("VarietyEdbId")
  717. if varietyEdbId <= 0 {
  718. br.Msg = "请选择指标"
  719. br.IsSendEmail = false
  720. return
  721. }
  722. dateType, _ := this.GetInt("DateType")
  723. if dateType <= 0 {
  724. dateType = 3
  725. }
  726. edbInfoType, _ := this.GetInt("EdbInfoType")
  727. if edbInfoType < 0 {
  728. edbInfoType = 1
  729. }
  730. startDate := this.GetString("StartDate")
  731. endDate := this.GetString("EndDate")
  732. startDate, endDate = utils.GetDateByDateType(dateType, startDate, endDate)
  733. varietyEdbInfo, err := supply_analysis.GetVarietyEdbInfoByVarietyEdbId(varietyEdbId)
  734. if err != nil {
  735. br.Msg = "获取失败"
  736. br.Msg = "获取失败,Err:" + err.Error()
  737. return
  738. }
  739. edbInfo := &data_manage.EdbInfo{
  740. EdbInfoId: varietyEdbId,
  741. EdbInfoType: 0,
  742. SourceName: "",
  743. Source: 0,
  744. EdbCode: varietyEdbInfo.EdbCode,
  745. EdbName: varietyEdbInfo.EdbName,
  746. EdbNameEn: "",
  747. EdbNameSource: "",
  748. Frequency: varietyEdbInfo.Frequency,
  749. Unit: ``,
  750. UnitEn: "",
  751. StartDate: varietyEdbInfo.StartDate.Format(utils.FormatDate),
  752. EndDate: varietyEdbInfo.EndDate.Format(utils.FormatDate),
  753. ClassifyId: 0,
  754. SysUserId: 0,
  755. SysUserRealName: "",
  756. UniqueCode: "",
  757. CreateTime: varietyEdbInfo.CreateTime,
  758. ModifyTime: varietyEdbInfo.ModifyTime,
  759. MinValue: 0,
  760. MaxValue: 0,
  761. CalculateFormula: "",
  762. EdbType: 1,
  763. Sort: 0,
  764. LatestDate: varietyEdbInfo.EndDate.Format(utils.FormatDate),
  765. LatestValue: varietyEdbInfo.LatestValue,
  766. MoveType: 0,
  767. MoveFrequency: "",
  768. NoUpdate: 0,
  769. ServerUrl: "",
  770. ChartImage: "",
  771. Calendar: "",
  772. }
  773. var startDateTime time.Time
  774. if startDate != `` {
  775. startDateTime, err = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  776. if err != nil {
  777. br.Msg = "开始日期异常"
  778. br.Msg = "开始日期异常,Err:" + err.Error()
  779. return
  780. }
  781. //同比值的计算方式,是需要与去年同期对比,所以如果用户需要某个时间段的数据的话,获取基础数据时,需要往前面推(1年+兼容的日期频度(目前是35天))
  782. startDate = startDateTime.AddDate(-1, 0, -35).Format(utils.FormatDate)
  783. }
  784. // 根据品种指标id获取所有的数据
  785. dataList, minVal, maxVal, err := supply_analysisServ.GetCustomEdbData(varietyEdbId, startDate, endDate)
  786. if err != nil {
  787. br.Msg = "获取失败"
  788. br.Msg = "获取失败,Err:" + err.Error()
  789. return
  790. }
  791. edbInfo.MinValue = minVal
  792. edbInfo.MaxValue = maxVal
  793. resp := new(data_manage.EdbInfoDataResp)
  794. resp.EdbInfo = edbInfo
  795. resp.DataList = dataList
  796. br.Ret = 200
  797. br.Success = true
  798. br.Msg = "获取成功"
  799. br.Data = resp
  800. }