base_from_clarksons.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/logic"
  5. "eta/eta_index_lib/models"
  6. "eta/eta_index_lib/utils"
  7. "fmt"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type ClarksonsDataController struct {
  13. BaseAuthController
  14. }
  15. // @Title 克拉克森
  16. // @Description 刷新克拉克森数据接口
  17. // @Param request body models.CoalMineDataReq true "type json string"
  18. // @Success 200 {object} models.EdbClassify
  19. // @router /data [post]
  20. func (this *ClarksonsDataController) ClarksonsData() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. this.Data["json"] = br
  24. this.ServeJSON()
  25. }()
  26. var req models.CoalMineDataReq
  27. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  28. if err != nil {
  29. br.Msg = "参数解析异常!"
  30. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  31. return
  32. }
  33. var dateMap = make(map[int]string)
  34. var indexMap = make(map[string]*models.BaseFromClarksonsIndex)
  35. //var indexMap = make(map[string]string)
  36. var itemsIndex []*models.BaseFromClarksonsData
  37. codeList, err := models.GetBaseFromClarksonsIndex()
  38. if err != nil && err.Error() != utils.ErrNoRow() {
  39. utils.FileLog.Info("获取克拉克森指标失败:", err)
  40. return
  41. }
  42. if len(codeList) > 0 {
  43. for _, v := range codeList {
  44. indexMap[v.IndexCode] = v
  45. }
  46. }
  47. for _, sheet := range req.SheetData {
  48. //遍历行读取
  49. maxRow := sheet.MaxRow
  50. var indexId int64
  51. index := new(models.BaseFromClarksonsIndex)
  52. indexCode := ""
  53. for i := 3; i < maxRow; i++ {
  54. //获取样本情况
  55. row := sheet.Rows[i]
  56. cells := row.Cells
  57. // indexCode
  58. if i == 3 {
  59. indexCode = cells[1].Value
  60. continue
  61. }
  62. if indexItem, ok := indexMap[indexCode]; !ok {
  63. // indexName
  64. if i == 4 {
  65. index.IndexName = cells[1].Value
  66. continue
  67. }
  68. // unit
  69. if i == 5 {
  70. index.Unit = cells[1].Value
  71. continue
  72. }
  73. index.IndexCode = indexCode
  74. index.CreateTime = time.Now()
  75. index.ModifyTime = time.Now()
  76. indexId, err = models.AddBaseFromClarksonsIndex(index)
  77. if err != nil {
  78. utils.FileLog.Info("添加指标名称错误", err.Error())
  79. return
  80. }
  81. index.BaseFromClarksonsIndexId = int(indexId)
  82. indexMap[indexCode] = index
  83. } else {
  84. index = indexItem
  85. }
  86. // 数据及日期
  87. if i > 5 {
  88. var date string
  89. if len(cells) == 0 {
  90. break
  91. }
  92. for k, cell := range cells {
  93. text := cell.Value
  94. text = strings.Replace(text, "\\", "", -1)
  95. if text != "" {
  96. // 时间
  97. if k == 0 {
  98. if text != "" {
  99. dataTime, err := utils.ParseDateTime(text)
  100. if err != nil {
  101. utils.FileLog.Info("解析时间字符串出错:", err)
  102. fmt.Println("解析时间字符串出错:", err)
  103. return
  104. }
  105. date = dataTime.Format(utils.FormatDate)
  106. }
  107. dateMap[k] = date
  108. }
  109. // 数据
  110. if k == 1 {
  111. item := new(models.BaseFromClarksonsData)
  112. item.IndexCode = indexCode
  113. item.Value = text
  114. item.DataTime = date
  115. item.BaseFromClarksonsIndexId = index.BaseFromClarksonsIndexId
  116. item.ModifyTime = time.Now()
  117. item.CreateTime = time.Now()
  118. itemsIndex = append(itemsIndex, item)
  119. continue
  120. }
  121. }
  122. }
  123. }
  124. }
  125. if len(itemsIndex) > 2 {
  126. time1, _ := time.Parse(utils.FormatDate, itemsIndex[0].DataTime)
  127. time2, _ := time.Parse(utils.FormatDate, itemsIndex[1].DataTime)
  128. diff := time2.Sub(time1).Hours() / 24
  129. if diff < 5 {
  130. index.Frequency = "日度"
  131. } else if diff > 5 && diff < 10 {
  132. index.Frequency = "周度"
  133. } else if diff > 20 && diff < 50 {
  134. index.Frequency = "月度"
  135. } else if diff > 50 && diff < 100 {
  136. index.Frequency = "季度"
  137. } else if diff > 100 {
  138. index.Frequency = "年度"
  139. }
  140. }
  141. err = models.UpdateBaseFromClarksonsIndex(index)
  142. if err != nil {
  143. fmt.Println("修改指标频度错误", err)
  144. return
  145. } else {
  146. fmt.Println("修改指标频度成功 ", index.IndexCode, index.Frequency)
  147. }
  148. }
  149. count, err := models.AddBaseFromClarksonsDataMuti(itemsIndex)
  150. if err != nil {
  151. fmt.Println("添加数据错误", err.Error())
  152. } else {
  153. fmt.Println("添加数据成功", count)
  154. }
  155. fmt.Println("数据操作完成")
  156. br.Ret = 200
  157. br.Success = true
  158. br.Msg = "获取成功"
  159. return
  160. }
  161. // @Title 新增克拉克森指标接口
  162. // @Description 新增克拉克森指标接口
  163. // @Success 200 {object} models.AddEdbInfoReq
  164. // @router /add [post]
  165. func (this *ClarksonsDataController) Add() {
  166. br := new(models.BaseResponse).Init()
  167. var cacheKey string
  168. defer func() {
  169. utils.Rc.Delete(cacheKey)
  170. this.Data["json"] = br
  171. this.ServeJSON()
  172. }()
  173. source := utils.DATA_SOURCE_CLARKSONS
  174. var req models.AddEdbInfoReq
  175. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  176. if err != nil {
  177. br.Msg = "参数解析异常!"
  178. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  179. return
  180. }
  181. if req.EdbCode == "" {
  182. br.Msg = "请输入指标编码!"
  183. br.ErrMsg = "请输入指标编码,指标编码为空"
  184. return
  185. }
  186. cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(source) + "_" + req.EdbCode
  187. if !utils.Rc.IsExist(cacheKey) {
  188. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  189. err = models.AddEdbDataFromClarksons(req.EdbCode)
  190. if err != nil {
  191. br.Msg = "获取指标信息失败!"
  192. br.ErrMsg = "获取指标信息失败 AddEdbDataFromClarksons,Err:" + err.Error()
  193. return
  194. }
  195. br.Ret = 200
  196. br.Success = true
  197. br.Msg = "获取成功"
  198. } else {
  199. br.Ret = 501
  200. br.Success = true
  201. br.Msg = "系统处理中,请稍后重试"
  202. }
  203. }
  204. // @Title 刷新克拉克森指标接口
  205. // @Description 刷新克拉克森指标接口
  206. // @Success 200 {object} models.RefreshEdbInfoReq
  207. // @router /refresh [post]
  208. func (this *ClarksonsDataController) Refresh() {
  209. br := new(models.BaseResponse).Init()
  210. var cacheKey string
  211. defer func() {
  212. utils.Rc.Delete(cacheKey)
  213. this.Data["json"] = br
  214. this.ServeJSON()
  215. }()
  216. source := utils.DATA_SOURCE_CLARKSONS
  217. var req models.RefreshEdbInfoReq
  218. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  219. if err != nil {
  220. br.Msg = "参数解析异常!"
  221. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  222. return
  223. }
  224. if req.EdbCode == "" {
  225. br.Msg = "请输入指标编码!"
  226. br.ErrMsg = "请输入指标编码,指标编码为空"
  227. return
  228. }
  229. if req.EdbInfoId <= 0 {
  230. br.Msg = "请输入指标ID!"
  231. br.ErrMsg = "请输入指标ID"
  232. return
  233. }
  234. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  235. if utils.Rc.IsExist(cacheKey) {
  236. br.Ret = 501
  237. br.Success = true
  238. br.Msg = "系统处理中,请稍后重试"
  239. return
  240. }
  241. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  242. // 获取指标详情
  243. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  244. if err != nil {
  245. br.Msg = "指标不存在!"
  246. br.ErrMsg = "指标不存在"
  247. return
  248. }
  249. err = models.RefreshEdbDataFromClarksons(req.EdbInfoId, req.EdbCode, req.StartDate)
  250. if err != nil && err.Error() != utils.ErrNoRow() {
  251. br.Msg = "刷新指标信息失败!"
  252. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromCoal,Err:" + err.Error()
  253. return
  254. }
  255. // 更新指标最大最小值
  256. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  257. if err != nil {
  258. br.Msg = errMsg
  259. br.ErrMsg = err.Error()
  260. return
  261. }
  262. // 更新ES
  263. go logic.UpdateEs(edbInfo.EdbInfoId)
  264. br.Ret = 200
  265. br.Success = true
  266. br.Msg = "获取成功"
  267. }