commodity_coal_inland.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package services
  2. import (
  3. "fmt"
  4. "github.com/mozillazg/go-pinyin"
  5. "github.com/tealeg/xlsx"
  6. "hongze/hongze_data_crawler/models"
  7. "hongze/hongze_data_crawler/utils"
  8. "strconv"
  9. "time"
  10. )
  11. func FileCoalInland() (err error) {
  12. defer func() {
  13. if err != nil {
  14. fmt.Println("RefreshDataFromDalian Err:" + err.Error())
  15. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromDalian ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  16. }
  17. }()
  18. path := "/home/code/python/coal_mail/emailFile/内陆17省动力煤终端用户供耗存%s.xlsx"
  19. path = fmt.Sprintf(path,time.Now().Format(utils.FormatDateUnSpace))
  20. xlFile, err := xlsx.OpenFile(path)
  21. if err != nil {
  22. fmt.Println(err)
  23. return
  24. }
  25. var mappingItems []*models.BaseFromCoalmineMapping
  26. var indexItems []*models.BaseFromCoalmineInlandIndex
  27. var codeMap = make(map[string]string)
  28. var indexMap = make(map[string]string)
  29. var nameMap = make(map[int]string)
  30. var groupMap = make(map[int]string)
  31. var dataTime string
  32. var sheetName string
  33. codeList, err := models.GetBaseFromCoalmineMapping()
  34. if err != nil && err.Error() != utils.ErrNoRow() {
  35. utils.FileLog.Info("获取煤炭指标失败:", err)
  36. return err
  37. }
  38. if len(codeList) > 0 {
  39. for _, v := range codeList {
  40. codeMap[v.IndexName] = v.IndexCode
  41. }
  42. }
  43. indexCompanyList, err := models.GetBaseFromCoalmineInlandIndex()
  44. if err != nil && err.Error() != utils.ErrNoRow() {
  45. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  46. return err
  47. }
  48. if len(indexCompanyList) > 0 {
  49. for _, v := range indexCompanyList {
  50. indexMap[v.IndexName+v.DataTime] = v.DealValue
  51. }
  52. }
  53. for s, sheet := range xlFile.Sheets {
  54. sheetName = sheet.Name
  55. //遍历行读取
  56. maxCol := sheet.MaxCol
  57. for i := 0; i < maxCol; i++ {
  58. if i == 0 {
  59. row := sheet.Row(i)
  60. cells := row.Cells
  61. for k, cell := range cells {
  62. if k > 0 && k < 18 {
  63. text := cell.String()
  64. groupMap[k] = text
  65. var item models.BaseFromCoalmineMapping
  66. //合计命名
  67. if k == 9 {
  68. item.IndexName = sheetName
  69. }
  70. item.IndexName = text + sheetName
  71. nameMap[k] = item.IndexName
  72. //code
  73. exists := ContainsSpecialName(text)
  74. var code string
  75. if exists {
  76. abbr := trimProvinceName(text)
  77. //取处理后公司名首字母缩写
  78. a := pinyin.NewArgs()
  79. rows := pinyin.Pinyin(sheetName, a)
  80. for i := 0; i < len(rows); i++ {
  81. if len(rows[i]) != 0 {
  82. str := rows[i][0]
  83. pi := str[0:1]
  84. code += pi
  85. }
  86. }
  87. item.IndexCode = abbr + code + "inland"
  88. } else {
  89. a := pinyin.NewArgs()
  90. rows := pinyin.Pinyin(text+sheetName, a)
  91. for i := 0; i < len(rows); i++ {
  92. if len(rows[i]) != 0 {
  93. str := rows[i][0]
  94. pi := str[0:1]
  95. code += pi
  96. }
  97. }
  98. item.IndexCode = code + "inland"
  99. }
  100. item.CreateTime = time.Now()
  101. mappingItems = append(mappingItems, &item)
  102. }
  103. }
  104. }
  105. if i > 0 {
  106. row := sheet.Row(i)
  107. cells := row.Cells
  108. for k, cell := range cells {
  109. if k < 18 {
  110. var item models.BaseFromCoalmineInlandIndex
  111. if k == 0 {
  112. if s == 0 {
  113. text := cell.String()
  114. t, _ := time.ParseInLocation("01-02-06", text, time.Local)
  115. dataTime = t.Format(utils.FormatDate)
  116. } else {
  117. text := cell.String()
  118. dataTime = ExcelDateToDate(text).Format(utils.FormatDate)
  119. }
  120. }
  121. if k > 0 {
  122. text := cell.String()
  123. item.IndexName = nameMap[k]
  124. item.IndexCode = codeMap[nameMap[k]]
  125. item.DealValue = text
  126. item.GroupName = groupMap[k]
  127. item.Source = "内陆十七省"
  128. item.DataTime = dataTime
  129. item.Unit = "万吨"
  130. item.Frequency = "日度"
  131. item.ModifyTime = time.Now()
  132. item.CreateTime = time.Now()
  133. }
  134. indexItems = append(indexItems, &item)
  135. }
  136. }
  137. }
  138. }
  139. }
  140. //添加数据到数据库
  141. for _, v := range mappingItems {
  142. if codeMap[v.IndexName] == "" {
  143. codeMap[v.IndexName] = v.IndexCode
  144. newId, err := models.AddBaseFromCoalmineMapping(v)
  145. if err != nil {
  146. for i := 0; i<10; i++ {
  147. v.IndexCode = v.IndexCode + strconv.Itoa(i)
  148. codeMap[v.IndexName] = v.IndexCode
  149. newId, err := models.AddBaseFromCoalmineMapping(v)
  150. if err != nil {
  151. fmt.Println("再次添加公司指标名称错误", err)
  152. continue
  153. } else {
  154. fmt.Println("新增公司成功", newId)
  155. break
  156. }
  157. }
  158. } else {
  159. fmt.Println("新增公司成功", newId)
  160. }
  161. }
  162. }
  163. fmt.Println("指标操作完成")
  164. //给indexItem中的code赋值并插入index表
  165. for _, v := range indexItems {
  166. v.IndexCode = codeMap[v.IndexName]
  167. if indexMap[v.IndexName+v.DataTime] == "" && v.DealValue != "" {
  168. newId, err := models.AddBaseFromCoalInlandIndex(v)
  169. if err != nil {
  170. fmt.Println("添加数据错误", err)
  171. } else {
  172. fmt.Println("新增成功", newId)
  173. }
  174. } else {
  175. if indexMap[v.IndexName+v.DataTime] != v.DealValue && v.DealValue != "" {
  176. err = models.UpdateBaseFromCoalInlandIndex(v)
  177. if err != nil {
  178. fmt.Println("修改数据错误错误", err)
  179. return
  180. }
  181. }
  182. }
  183. }
  184. return
  185. }