base_from_clarksons.go 7.7 KB

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