base_from_clarksons.go 7.2 KB

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