commodity_coal_coastal.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package services
  2. import (
  3. "eta/eta_crawler/models"
  4. "eta/eta_crawler/services/alarm_msg"
  5. "eta/eta_crawler/utils"
  6. "fmt"
  7. "github.com/mozillazg/go-pinyin"
  8. "github.com/tealeg/xlsx"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. func FileCoalCoastal() {
  14. var err error
  15. defer func() {
  16. if err != nil {
  17. fmt.Println("RefreshDataFromCoalCoastal Err:" + err.Error())
  18. msg := "失败提醒" + "RefreshDataFromCoalCoastal ErrMsg:" + err.Error()
  19. go alarm_msg.SendAlarmMsg(msg, 3)
  20. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromCoalCoastal ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  21. }
  22. }()
  23. //path := "static/file/沿海八省动力煤终端用户供耗存数据更新03.07.xlsx"
  24. //var month, day string
  25. //path := "/home/code/python/coal_mail/emailFile/沿海八省动力煤终端用户供耗存数据更新%s%s.xlsx"
  26. path := "/home/code/python/coal_mail/emailFile/沿海八省动力煤终端用户供耗存数据更新.xlsx"
  27. //for i := 0; i < 7; i++ {
  28. // dateDay := time.Now().AddDate(0, 0, -i).Day()
  29. // if dateDay < 10 {
  30. // day = strconv.Itoa(dateDay)
  31. // day = "0" + day
  32. // } else {
  33. // day = strconv.Itoa(dateDay)
  34. // }
  35. // dateMonth := int(time.Now().AddDate(0, 0, -i).Month())
  36. // if dateMonth < 10 {
  37. // month = strconv.Itoa(dateMonth)
  38. // month = "0" + month
  39. // }else {
  40. // month = strconv.Itoa(dateMonth)
  41. // }
  42. // npath := fmt.Sprintf(path, month, day)
  43. // fmt.Println(npath)
  44. // exist, err := PathExists(npath)
  45. // if err != nil {
  46. // fmt.Println(err)
  47. // return
  48. // }
  49. // if exist {
  50. // path = npath
  51. // break
  52. // }
  53. //}
  54. var xlFile *xlsx.File
  55. exist, err := PathExists(path)
  56. if err != nil {
  57. fmt.Println(err)
  58. return
  59. }
  60. if exist {
  61. xlFile, err = xlsx.OpenFile(path)
  62. if err != nil {
  63. fmt.Println("OpenFile err:", err)
  64. return
  65. }
  66. } else {
  67. return
  68. }
  69. var mappingItems []*models.BaseFromCoalmineMapping
  70. var indexItems []*models.BaseFromCoalmineCoastalIndex
  71. var codeMap = make(map[string]string)
  72. var indexMap = make(map[string]string)
  73. var nameMap = make(map[int]string)
  74. var groupMap = make(map[int]string)
  75. var dataTime string
  76. var sheetName, unit string
  77. codeList, err := models.GetBaseFromCoalmineMapping()
  78. if err != nil && err.Error() != utils.ErrNoRow() {
  79. utils.FileLog.Info("获取煤炭指标失败:", err)
  80. return
  81. }
  82. if len(codeList) > 0 {
  83. for _, v := range codeList {
  84. codeMap[v.IndexName] = v.IndexCode
  85. }
  86. }
  87. indexCompanyList, err := models.GetBaseFromCoalmineCoastalIndex()
  88. if err != nil && err.Error() != utils.ErrNoRow() {
  89. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  90. return
  91. }
  92. if len(indexCompanyList) > 0 {
  93. for _, v := range indexCompanyList {
  94. indexMap[v.IndexName+v.DataTime] = v.DealValue
  95. }
  96. }
  97. for _, sheet := range xlFile.Sheets {
  98. sheetName = sheet.Name
  99. //遍历行读取
  100. maxCol := sheet.MaxCol
  101. for i := 0; i < maxCol; i++ {
  102. if i == 0 {
  103. row := sheet.Row(i)
  104. cells := row.Cells
  105. for k, cell := range cells {
  106. if k > 0 && k < 9 {
  107. text := cell.String()
  108. groupMap[k] = text
  109. var item models.BaseFromCoalmineMapping
  110. //合计命名
  111. if k == 9 {
  112. item.IndexName = sheetName
  113. }
  114. item.IndexName = text + sheetName
  115. nameMap[k] = item.IndexName
  116. //code
  117. exists := ContainsSpecialName(text)
  118. var code string
  119. if exists {
  120. abbr := trimProvinceName(text)
  121. //取处理后公司名首字母缩写
  122. a := pinyin.NewArgs()
  123. rows := pinyin.Pinyin(sheetName, a)
  124. for i := 0; i < len(rows); i++ {
  125. if len(rows[i]) != 0 {
  126. str := rows[i][0]
  127. pi := str[0:1]
  128. code += pi
  129. }
  130. }
  131. item.IndexCode = abbr + code + "coastal"
  132. } else {
  133. a := pinyin.NewArgs()
  134. rows := pinyin.Pinyin(text+sheetName, a)
  135. for i := 0; i < len(rows); i++ {
  136. if len(rows[i]) != 0 {
  137. str := rows[i][0]
  138. pi := str[0:1]
  139. code += pi
  140. }
  141. }
  142. item.IndexCode = code + "coastal"
  143. }
  144. item.CreateTime = time.Now()
  145. mappingItems = append(mappingItems, &item)
  146. }
  147. if k == 10 {
  148. unit = strings.Replace(cell.String(), "单位:", "", -1)
  149. }
  150. }
  151. }
  152. if i > 0 {
  153. row := sheet.Row(i)
  154. cells := row.Cells
  155. for k, cell := range cells {
  156. if k < 9 {
  157. var item models.BaseFromCoalmineCoastalIndex
  158. if k == 0 {
  159. text := cell.String()
  160. dataTime = ExcelDateToDate(text).Format(utils.FormatDate)
  161. }
  162. if k > 0 {
  163. text := cell.String()
  164. item.IndexName = nameMap[k]
  165. item.IndexCode = codeMap[nameMap[k]]
  166. item.DealValue = text
  167. item.GroupName = groupMap[k]
  168. item.Source = "沿海八省"
  169. item.DataTime = dataTime
  170. item.Frequency = "日度"
  171. item.ModifyTime = time.Now()
  172. item.CreateTime = time.Now()
  173. }
  174. item.Unit = unit
  175. indexItems = append(indexItems, &item)
  176. }
  177. }
  178. }
  179. }
  180. }
  181. //添加数据到数据库
  182. for _, v := range mappingItems {
  183. if codeMap[v.IndexName] == "" {
  184. codeMap[v.IndexName] = v.IndexCode
  185. newId, err := models.AddBaseFromCoalmineMapping(v)
  186. if err != nil {
  187. for i := 0; i < 10; i++ {
  188. v.IndexCode = v.IndexCode + strconv.Itoa(i)
  189. codeMap[v.IndexName] = v.IndexCode
  190. newId, err := models.AddBaseFromCoalmineMapping(v)
  191. if err != nil {
  192. fmt.Println("再次添加公司指标名称错误", err)
  193. continue
  194. } else {
  195. fmt.Println("新增公司成功", newId)
  196. break
  197. }
  198. }
  199. } else {
  200. fmt.Println("新增公司成功", newId)
  201. }
  202. }
  203. }
  204. fmt.Println("指标操作完成")
  205. //给indexItem中的code赋值并插入index表
  206. for _, v := range indexItems {
  207. v.IndexCode = codeMap[v.IndexName]
  208. if indexMap[v.IndexName+v.DataTime] == "" && v.DealValue != "" {
  209. newId, err := models.AddBaseFromCoalCoastalIndex(v)
  210. if err != nil {
  211. fmt.Println("添加数据错误", err)
  212. } else {
  213. fmt.Println("新增成功", newId)
  214. }
  215. } else {
  216. if indexMap[v.IndexName+v.DataTime] != v.DealValue && v.DealValue != "" {
  217. err = models.UpdateBaseFromCoalCoastalIndex(v)
  218. if err != nil {
  219. fmt.Println("修改数据错误错误", err)
  220. return
  221. }
  222. }
  223. }
  224. }
  225. return
  226. }
  227. func ttrimProvinceName(name string) string {
  228. name = strings.Replace(name, "陕西省", "sn", -1)
  229. name = strings.Replace(name, "陕西", "sn", -1)
  230. name = strings.Replace(name, "海南市", "hi", -1)
  231. name = strings.Replace(name, "海南", "hi", -1)
  232. name = strings.Replace(name, "河南省", "ha", -1)
  233. name = strings.Replace(name, "河南", "ha", -1)
  234. name = strings.Replace(name, "河北省", "he", -1)
  235. name = strings.Replace(name, "河北", "he", -1)
  236. name = strings.Replace(name, "澳门", "mo", -1)
  237. name = strings.Replace(name, "内蒙古自治区", "nm", -1)
  238. name = strings.Replace(name, "内蒙古", "nm", -1)
  239. name = strings.Replace(name, "黑龙江", "hl", -1)
  240. name = strings.Replace(name, "(", "", -1)
  241. name = strings.Replace(name, ")", "", -1)
  242. name = strings.Replace(name, "+", "", -1)
  243. return name
  244. }
  245. func ExcelDateToDate(excelDate string) time.Time {
  246. excelTime := time.Date(1899, time.December, 30, 0, 0, 0, 0, time.UTC)
  247. var days, _ = strconv.Atoi(excelDate)
  248. return excelTime.Add(time.Second * time.Duration(days*86400))
  249. }