base_from_mtjh.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. // 更新指标最新日期
  235. {
  236. go func() {
  237. obj := new(models.BaseFromMtjhMapping)
  238. for _, indexCode := range codeMap {
  239. var dateItem *models.EdbInfoMaxAndMinInfo
  240. dateItem, err = obj.GetMaxAndMinDateByIndexCode(indexCode)
  241. if err != nil {
  242. err = fmt.Errorf("查询指标最新日期失败 Err:%s", err)
  243. return
  244. }
  245. obj.ModifyIndexMaxAndMinDate(indexCode, dateItem)
  246. }
  247. }()
  248. }
  249. fmt.Println("数据操作完成")
  250. br.Ret = 200
  251. br.Success = true
  252. br.Msg = "获取成功"
  253. return
  254. }
  255. // @Title 新增煤炭网指标接口
  256. // @Description 新增煤炭网指标接口
  257. // @Success 200 {object} models.AddEdbInfoReq
  258. // @router /add [post]
  259. func (this *MtjhDataController) Add() {
  260. br := new(models.BaseResponse).Init()
  261. var cacheKey string
  262. defer func() {
  263. utils.Rc.Delete(cacheKey)
  264. this.Data["json"] = br
  265. this.ServeJSON()
  266. }()
  267. source := utils.DATA_SOURCE_MTJH
  268. var req models.AddEdbInfoReq
  269. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  270. if err != nil {
  271. br.Msg = "参数解析异常!"
  272. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  273. return
  274. }
  275. if req.EdbCode == "" {
  276. br.Msg = "请输入指标编码!"
  277. br.ErrMsg = "请输入指标编码,指标编码为空"
  278. return
  279. }
  280. cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(source) + "_" + req.EdbCode
  281. if !utils.Rc.IsExist(cacheKey) {
  282. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  283. err = models.AddEdbDataFromMtjh(req.EdbCode)
  284. if err != nil {
  285. br.Msg = "获取指标信息失败!"
  286. br.ErrMsg = "获取指标信息失败 AddEdbDataFromMtjh,Err:" + err.Error()
  287. return
  288. }
  289. br.Ret = 200
  290. br.Success = true
  291. br.Msg = "获取成功"
  292. } else {
  293. br.Ret = 501
  294. br.Success = true
  295. br.Msg = "系统处理中,请稍后重试"
  296. }
  297. }
  298. // @Title 刷新中国煤炭市场网指标接口
  299. // @Description 刷新中国煤炭市场网指标接口
  300. // @Success 200 {object} models.RefreshEdbInfoReq
  301. // @router /refresh [post]
  302. func (this *MtjhDataController) Refresh() {
  303. br := new(models.BaseResponse).Init()
  304. var cacheKey string
  305. defer func() {
  306. utils.Rc.Delete(cacheKey)
  307. this.Data["json"] = br
  308. this.ServeJSON()
  309. }()
  310. source := utils.DATA_SOURCE_MTJH
  311. var req models.RefreshEdbInfoReq
  312. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  313. if err != nil {
  314. br.Msg = "参数解析异常!"
  315. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  316. return
  317. }
  318. if req.EdbCode == "" {
  319. br.Msg = "请输入指标编码!"
  320. br.ErrMsg = "请输入指标编码,指标编码为空"
  321. return
  322. }
  323. if req.EdbInfoId <= 0 {
  324. br.Msg = "请输入指标ID!"
  325. br.ErrMsg = "请输入指标ID"
  326. return
  327. }
  328. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  329. if utils.Rc.IsExist(cacheKey) {
  330. br.Ret = 501
  331. br.Success = true
  332. br.Msg = "系统处理中,请稍后重试"
  333. return
  334. }
  335. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  336. // 获取指标详情
  337. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  338. if err != nil {
  339. br.Msg = "指标不存在!"
  340. br.ErrMsg = "指标不存在"
  341. return
  342. }
  343. err = models.RefreshEdbDataFromMtjh(req.EdbInfoId, req.EdbCode, req.StartDate)
  344. if err != nil && err.Error() != utils.ErrNoRow() {
  345. br.Msg = "刷新指标信息失败!"
  346. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromCoal,Err:" + err.Error()
  347. return
  348. }
  349. // 更新指标最大最小值
  350. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  351. if err != nil {
  352. br.Msg = errMsg
  353. br.ErrMsg = err.Error()
  354. return
  355. }
  356. // 更新ES
  357. go logic.UpdateEs(edbInfo.EdbInfoId)
  358. br.Ret = 200
  359. br.Success = true
  360. br.Msg = "获取成功"
  361. }