commodity_coal_inland.go 6.5 KB

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