base_from_mtjh.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. "github.com/mozillazg/go-pinyin"
  9. "strconv"
  10. "time"
  11. )
  12. type MtjhDataController 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 *MtjhDataController) Mtjh() {
  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 area string
  34. var port string
  35. var description string
  36. var dateMap = make(map[int]string)
  37. var varietyMap = make(map[int]string)
  38. var codeMap = make(map[string]string)
  39. var indexMap = make(map[string]string)
  40. var codeCompanyMap = make(map[string]string)
  41. var indexCompanyMap = make(map[string]string)
  42. var items []*models.BaseFromMtjhMapping
  43. var itemsIndex []*models.BaseFromMtjhIndex
  44. codeList, err := models.GetBaseFromMtjhMapping()
  45. if err != nil && err.Error() != utils.ErrNoRow() {
  46. utils.FileLog.Info("获取煤炭指标失败:", err)
  47. return
  48. }
  49. if len(codeList) > 0 {
  50. for _, v := range codeList {
  51. codeMap[v.IndexName] = v.IndexCode
  52. }
  53. }
  54. codeCompanyList, err := models.GetBaseFromMtjhMapping()
  55. if err != nil && err.Error() != utils.ErrNoRow() {
  56. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  57. return
  58. }
  59. if len(codeCompanyList) > 0 {
  60. for _, v := range codeCompanyList {
  61. codeCompanyMap[v.IndexName] = v.IndexCode
  62. }
  63. }
  64. indexList, err := models.GetBaseFromMtjhIndex()
  65. if err != nil && err.Error() != utils.ErrNoRow() {
  66. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  67. return
  68. }
  69. if len(indexList) > 0 {
  70. for _, v := range indexList {
  71. indexMap[v.IndexName+v.DataTime] = v.DealValue
  72. }
  73. }
  74. indexCompanyList, err := models.GetBaseFromCoalmineCompanyIndex()
  75. if err != nil && err.Error() != utils.ErrNoRow() {
  76. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  77. return
  78. }
  79. if len(indexCompanyList) > 0 {
  80. for _, v := range indexCompanyList {
  81. indexCompanyMap[v.IndexName+v.DataTime] = v.DealValue
  82. }
  83. }
  84. for _, sheet := range req.SheetData {
  85. //遍历行读取
  86. maxRow := sheet.MaxRow
  87. for i := 1; i < maxRow; i++ {
  88. //获取样本情况
  89. row := sheet.Rows[i]
  90. cells := row.Cells
  91. // 获取日期
  92. if i == 1 {
  93. var date string
  94. for k, cell := range cells {
  95. if k > 2 {
  96. text := cell.Value
  97. if text != "" {
  98. stamp, _ := time.ParseInLocation("01-02-06", text, time.Now().Location())
  99. date = stamp.Format(utils.FormatDate)
  100. }
  101. dateMap[k] = date
  102. }
  103. }
  104. }
  105. // 获取品种
  106. if i == 2 {
  107. for k, cell := range cells {
  108. if k > 2 {
  109. text := cell.Value
  110. if text != "" {
  111. varietyMap[k] = text
  112. }
  113. }
  114. }
  115. }
  116. //for i := 0 ;i < len(cells);i ++ {
  117. // fmt.Println("k:",i)
  118. // fmt.Println("dateMap:",dateMap[i])
  119. // fmt.Println("varietyMap:",varietyMap[i])
  120. //}
  121. if i > 2 {
  122. for k, cell := range cells {
  123. text := cell.Value
  124. if text != "" {
  125. if k == 1 {
  126. area = text
  127. }
  128. if k == 2 {
  129. port = text
  130. for j := 0; j < 3; j++ {
  131. item := new(models.BaseFromMtjhMapping)
  132. switch j {
  133. case 0:
  134. description = "动力煤"
  135. case 1:
  136. description = "焦煤"
  137. case 2:
  138. description = "合计"
  139. }
  140. itemName := area + port + description
  141. //取处理后公司名首字母缩写
  142. var strResult string
  143. a := pinyin.NewArgs()
  144. pys := [][]string{}
  145. for _, r := range itemName {
  146. py := pinyin.SinglePinyin(r, a)
  147. if len(py) > 0 {
  148. pys = append(pys, py)
  149. } else {
  150. if r != '-' && r != '(' && r != ')' {
  151. pys = append(pys, []string{string(r)})
  152. }
  153. }
  154. }
  155. for i := 0; i < len(pys); i++ {
  156. if len(pys[i]) != 0 {
  157. str := pys[i][0]
  158. pi := str[0:1]
  159. strResult += pi
  160. }
  161. }
  162. strResult = "mtjh" + strResult
  163. item.IndexCode = strResult
  164. item.Area = area
  165. item.Port = port
  166. item.Variety = description
  167. item.Unit = "万吨"
  168. item.Frequency = "周度"
  169. item.IndexName = itemName
  170. item.CreateTime = time.Now()
  171. items = append(items, item)
  172. }
  173. }
  174. if k > 2 {
  175. item := new(models.BaseFromMtjhIndex)
  176. item.IndexName = area + port + varietyMap[k]
  177. item.IndexCode = codeMap[item.IndexName]
  178. item.DealValue = text
  179. item.DataTime = dateMap[k]
  180. item.Area = area
  181. item.Port = port
  182. item.Variety = varietyMap[k]
  183. item.Unit = "万吨"
  184. item.Frequency = "周度"
  185. item.ModifyTime = time.Now()
  186. item.CreateTime = time.Now()
  187. itemsIndex = append(itemsIndex, item)
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. //添加数据到数据库
  195. mappingAddList := make([]*models.BaseFromMtjhMapping, 0)
  196. for _, v := range items {
  197. if codeMap[v.IndexName] == "" {
  198. codeMap[v.IndexName] = v.IndexCode
  199. mappingAddList = append(mappingAddList, v)
  200. }
  201. }
  202. if len(mappingAddList) > 0 {
  203. newId, err := models.AddBaseFromMtjhMappingMuti(mappingAddList)
  204. if err != nil {
  205. fmt.Println("添加指标名称错误", err.Error())
  206. } else {
  207. fmt.Println("添加指标名称成功", newId)
  208. }
  209. }
  210. fmt.Println("指标操作完成")
  211. indexAddList := make([]*models.BaseFromMtjhIndex, 0)
  212. for _, v := range itemsIndex {
  213. v.IndexCode = codeMap[v.IndexName]
  214. if indexMap[v.IndexName+v.DataTime] == "" && v.DealValue != "" {
  215. indexAddList = append(indexAddList, v)
  216. } else {
  217. if indexMap[v.IndexName+v.DataTime] != v.DealValue && v.DealValue != "" {
  218. err = models.UpdateBaseFromMtjhIndex(v)
  219. if err != nil {
  220. fmt.Println("修改数据错误错误", err)
  221. return
  222. }
  223. }
  224. }
  225. }
  226. if len(indexAddList) > 0 {
  227. newId, err := models.AddBaseFromMtjhIndexMuti(indexAddList)
  228. if err != nil {
  229. fmt.Println("添加指标名称错误", err.Error())
  230. } else {
  231. fmt.Println("添加指标名称成功", newId)
  232. }
  233. }
  234. fmt.Println("数据操作完成")
  235. br.Ret = 200
  236. br.Success = true
  237. br.Msg = "获取成功"
  238. return
  239. }
  240. // @Title 新增煤炭网指标接口
  241. // @Description 新增煤炭网指标接口
  242. // @Success 200 {object} models.AddEdbInfoReq
  243. // @router /add [post]
  244. func (this *MtjhDataController) Add() {
  245. br := new(models.BaseResponse).Init()
  246. var cacheKey string
  247. defer func() {
  248. utils.Rc.Delete(cacheKey)
  249. this.Data["json"] = br
  250. this.ServeJSON()
  251. }()
  252. source := utils.DATA_SOURCE_MTJH
  253. var req models.AddEdbInfoReq
  254. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  255. if err != nil {
  256. br.Msg = "参数解析异常!"
  257. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  258. return
  259. }
  260. if req.EdbCode == "" {
  261. br.Msg = "请输入指标编码!"
  262. br.ErrMsg = "请输入指标编码,指标编码为空"
  263. return
  264. }
  265. cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(source) + "_" + req.EdbCode
  266. if !utils.Rc.IsExist(cacheKey) {
  267. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  268. err = models.AddEdbDataFromMtjh(req.EdbCode)
  269. if err != nil {
  270. br.Msg = "获取指标信息失败!"
  271. br.ErrMsg = "获取指标信息失败 AddEdbDataFromMtjh,Err:" + err.Error()
  272. return
  273. }
  274. br.Ret = 200
  275. br.Success = true
  276. br.Msg = "获取成功"
  277. } else {
  278. br.Ret = 501
  279. br.Success = true
  280. br.Msg = "系统处理中,请稍后重试"
  281. }
  282. }
  283. // @Title 刷新中国煤炭市场网指标接口
  284. // @Description 刷新中国煤炭市场网指标接口
  285. // @Success 200 {object} models.RefreshEdbInfoReq
  286. // @router /refresh [post]
  287. func (this *MtjhDataController) Refresh() {
  288. br := new(models.BaseResponse).Init()
  289. var cacheKey string
  290. defer func() {
  291. utils.Rc.Delete(cacheKey)
  292. this.Data["json"] = br
  293. this.ServeJSON()
  294. }()
  295. source := utils.DATA_SOURCE_MTJH
  296. var req models.RefreshEdbInfoReq
  297. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  298. if err != nil {
  299. br.Msg = "参数解析异常!"
  300. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  301. return
  302. }
  303. if req.EdbCode == "" {
  304. br.Msg = "请输入指标编码!"
  305. br.ErrMsg = "请输入指标编码,指标编码为空"
  306. return
  307. }
  308. if req.EdbInfoId <= 0 {
  309. br.Msg = "请输入指标ID!"
  310. br.ErrMsg = "请输入指标ID"
  311. return
  312. }
  313. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  314. if utils.Rc.IsExist(cacheKey) {
  315. br.Ret = 501
  316. br.Success = true
  317. br.Msg = "系统处理中,请稍后重试"
  318. return
  319. }
  320. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  321. // 获取指标详情
  322. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  323. if err != nil {
  324. br.Msg = "指标不存在!"
  325. br.ErrMsg = "指标不存在"
  326. return
  327. }
  328. err = models.RefreshEdbDataFromMtjh(req.EdbInfoId, req.EdbCode, req.StartDate)
  329. if err != nil && err.Error() != utils.ErrNoRow() {
  330. br.Msg = "刷新指标信息失败!"
  331. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromCoal,Err:" + err.Error()
  332. return
  333. }
  334. // 更新指标最大最小值
  335. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  336. if err != nil {
  337. br.Msg = errMsg
  338. br.ErrMsg = err.Error()
  339. return
  340. }
  341. // 更新ES
  342. go logic.UpdateEs(edbInfo.EdbInfoId)
  343. br.Ret = 200
  344. br.Success = true
  345. br.Msg = "获取成功"
  346. }