commodity_coal_mine.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 FileCoalJsm() {
  14. var err error
  15. defer func() {
  16. if err != nil {
  17. fmt.Println("RefreshDataFromCoalJsm Err:" + err.Error())
  18. msg := "失败提醒" + "RefreshDataFromCoalJsm ErrMsg:" + err.Error()
  19. go alarm_msg.SendAlarmMsg(msg, 3)
  20. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromCoalJsm ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  21. }
  22. }()
  23. //path := "static/file/442家晋陕蒙煤矿周度产量数据-20220328.xlsx"
  24. var exist bool
  25. path := "/home/code/python/coal_mail/emailFile/442家晋陕蒙煤矿周度产量数据-%s.xlsx"
  26. for i := 0; i < 10; i++ {
  27. npath := fmt.Sprintf(path, time.Now().AddDate(0, 0, -i).Format(utils.FormatDateUnSpace))
  28. fmt.Println(npath)
  29. exist, err = PathExists(npath)
  30. if err != nil {
  31. fmt.Println(err)
  32. return
  33. }
  34. if exist {
  35. path = npath
  36. break
  37. }
  38. }
  39. var xlFile *xlsx.File
  40. if exist {
  41. xlFile, err = xlsx.OpenFile(path)
  42. if err != nil {
  43. return
  44. }
  45. } else {
  46. return
  47. }
  48. var province string
  49. var description string
  50. var exchange string
  51. var city string
  52. var companyName string
  53. var group string
  54. var dateMap = make(map[int]string)
  55. var codeMap = make(map[string]string)
  56. var indexMap = make(map[string]string)
  57. var codeCompanyMap = make(map[string]string)
  58. var indexCompanyMap = make(map[string]string)
  59. var items []*models.BaseFromCoalmineMapping
  60. var itemsCompany []*models.BaseFromCoalmineMapping
  61. var itemsIndex []*models.BaseFromCoalmineJsmIndex
  62. var itemsCompanyIndex []*models.BaseFromCoalmineCompanyIndex
  63. codeList, err := models.GetBaseFromCoalmineMapping()
  64. if err != nil && err.Error() != utils.ErrNoRow() {
  65. utils.FileLog.Info("获取煤炭指标失败:", err)
  66. return
  67. }
  68. if len(codeList) > 0 {
  69. for _, v := range codeList {
  70. codeMap[v.IndexName] = v.IndexCode
  71. }
  72. }
  73. codeCompanyList, err := models.GetBaseFromCoalmineMapping()
  74. if err != nil && err.Error() != utils.ErrNoRow() {
  75. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  76. return
  77. }
  78. if len(codeCompanyList) > 0 {
  79. for _, v := range codeCompanyList {
  80. codeCompanyMap[v.IndexName] = v.IndexCode
  81. }
  82. }
  83. indexList, err := models.GetBaseFromCoalmineIndex()
  84. if err != nil && err.Error() != utils.ErrNoRow() {
  85. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  86. return
  87. }
  88. if len(indexList) > 0 {
  89. for _, v := range indexList {
  90. indexMap[v.IndexName+v.DataTime] = v.DealValue
  91. }
  92. }
  93. indexCompanyList, err := models.GetBaseFromCoalmineCompanyIndex()
  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. indexCompanyMap[v.IndexName+v.DataTime] = v.DealValue
  101. }
  102. }
  103. for _, sheet := range xlFile.Sheets {
  104. //遍历行读取
  105. maxRow := sheet.MaxRow
  106. for i := 0; i < maxRow; i++ {
  107. //获取样本情况
  108. if i > 3 && i < 16 {
  109. row := sheet.Row(i)
  110. cells := row.Cells
  111. Loop2:
  112. for k, cell := range cells {
  113. text := cell.String()
  114. if text != "" {
  115. if k == 1 {
  116. province = text
  117. }
  118. if k == 3 {
  119. description = text
  120. }
  121. if k == 4 {
  122. item := new(models.BaseFromCoalmineMapping)
  123. exchange = text
  124. exists := ContainsSpecialName(province)
  125. var strResult string
  126. if exists {
  127. abbr := TrimProvinceName(province)
  128. //取处理后公司名首字母缩写
  129. a := pinyin.NewArgs()
  130. rows := pinyin.Pinyin(exchange, a)
  131. for i := 0; i < len(rows); i++ {
  132. if len(rows[i]) != 0 {
  133. str := rows[i][0]
  134. pi := str[0:1]
  135. strResult += pi
  136. }
  137. }
  138. item.IndexCode = abbr + strResult + "jsm"
  139. } else {
  140. //取处理后公司名首字母缩写
  141. a := pinyin.NewArgs()
  142. rows := pinyin.Pinyin(province+exchange, a)
  143. for i := 0; i < len(rows); i++ {
  144. if len(rows[i]) != 0 {
  145. str := rows[i][0]
  146. pi := str[0:1]
  147. strResult += pi
  148. }
  149. }
  150. item.IndexCode = strResult + "jsm"
  151. }
  152. item.IndexName = province + exchange
  153. item.CreateTime = time.Now()
  154. items = append(items, item)
  155. break Loop2
  156. }
  157. }
  158. }
  159. }
  160. //获取日期
  161. if i == 3 {
  162. row := sheet.Row(i)
  163. cells := row.Cells
  164. for k, cell := range cells {
  165. text := cell.String()
  166. stamp, _ := time.ParseInLocation("01-02-06", text, time.Now().Location())
  167. if k > 4 {
  168. dateMap[k] = stamp.Format(utils.FormatDate)
  169. }
  170. }
  171. }
  172. //获取具体产量
  173. if i > 3 && i < 16 {
  174. row := sheet.Row(i)
  175. cells := row.Cells
  176. for k, cell := range cells {
  177. text := cell.String()
  178. if k > 4 {
  179. item := new(models.BaseFromCoalmineJsmIndex)
  180. item.IndexName = province + exchange
  181. item.IndexCode = codeMap[item.IndexName]
  182. item.Exchange = exchange
  183. item.DealValue = text
  184. item.DataTime = dateMap[k]
  185. item.Exchange = exchange
  186. item.Description = description
  187. item.Province = province
  188. item.Source = "三省周度"
  189. if exchange == "产量" {
  190. item.Unit = "万吨"
  191. } else {
  192. item.Unit = "百分比"
  193. }
  194. item.Frequency = "周度"
  195. item.ModifyTime = time.Now()
  196. item.CreateTime = time.Now()
  197. itemsIndex = append(itemsIndex, item)
  198. }
  199. }
  200. }
  201. //获取公司指标名称
  202. if i > 17 {
  203. row := sheet.Row(i)
  204. cells := row.Cells
  205. Loop3:
  206. for k, cell := range cells {
  207. text := cell.String()
  208. if text != "" {
  209. if k == 1 {
  210. province = text
  211. }
  212. if k == 2 {
  213. city = text
  214. }
  215. if k == 3 {
  216. companyName = text
  217. }
  218. if k == 4 {
  219. item := new(models.BaseFromCoalmineMapping)
  220. group = text
  221. trimName := TrimCompanyName(companyName)
  222. item.IndexName = trimName
  223. //取处理后公司名首字母缩写
  224. strResult := ""
  225. a := pinyin.NewArgs()
  226. rows := pinyin.Pinyin(trimName, a)
  227. for i := 0; i < len(rows); i++ {
  228. if len(rows[i]) != 0 {
  229. str := rows[i][0]
  230. pi := str[0:1]
  231. strResult += pi
  232. }
  233. }
  234. item.IndexCode = strResult + "company"
  235. item.CreateTime = time.Now()
  236. itemsCompany = append(itemsCompany, item)
  237. break Loop3
  238. }
  239. }
  240. }
  241. }
  242. //获取公司具体产量
  243. if i > 18 {
  244. row := sheet.Row(i)
  245. cells := row.Cells
  246. for k, cell := range cells {
  247. text := cell.String()
  248. if k > 4 {
  249. item := new(models.BaseFromCoalmineCompanyIndex)
  250. companyName = TrimCompanyName(companyName)
  251. item.IndexName = companyName
  252. item.IndexCode = codeCompanyMap[item.IndexName]
  253. item.DealValue = text
  254. item.DataTime = dateMap[k]
  255. item.Province = province
  256. item.City = city
  257. //处理无类别名时的情况
  258. if group == "无" {
  259. item.GroupName = companyName
  260. } else {
  261. item.GroupName = group
  262. }
  263. item.Source = "三省企业"
  264. item.Unit = "万吨"
  265. item.Frequency = "周度"
  266. item.ModifyTime = time.Now()
  267. item.CreateTime = time.Now()
  268. //fmt.Println(item)
  269. itemsCompanyIndex = append(itemsCompanyIndex, item)
  270. }
  271. }
  272. }
  273. }
  274. }
  275. //添加数据到数据库
  276. for _, v := range items {
  277. if codeMap[v.IndexName] == "" {
  278. codeMap[v.IndexName] = v.IndexCode
  279. newId, err := models.AddBaseFromCoalmineMapping(v)
  280. if err != nil {
  281. fmt.Println("添加指标名称错误")
  282. } else {
  283. fmt.Println("添加指标名称成功", newId)
  284. }
  285. }
  286. }
  287. fmt.Println("指标操作完成")
  288. for _, v := range itemsIndex {
  289. v.IndexCode = codeMap[v.IndexName]
  290. if indexMap[v.IndexName+v.DataTime] == "" && v.DealValue != "" {
  291. newId, err := models.AddBaseFromCoalmineIndex(v)
  292. if err != nil {
  293. fmt.Println("添加数据错误", err)
  294. } else {
  295. fmt.Println("新增成功", newId)
  296. }
  297. } else {
  298. if indexMap[v.IndexName+v.DataTime] != v.DealValue && v.DealValue != "" {
  299. err = models.UpdateBaseFromCoalmineIndex(v)
  300. if err != nil {
  301. fmt.Println("修改数据错误错误", err)
  302. return
  303. }
  304. }
  305. }
  306. }
  307. fmt.Println("数据操作完成")
  308. for _, v := range itemsCompany {
  309. if codeCompanyMap[v.IndexName] == "" {
  310. codeCompanyMap[v.IndexName] = v.IndexCode
  311. newId, err := models.AddBaseFromCoalmineMapping(v)
  312. if err != nil {
  313. for i := 0; i < 10; i++ {
  314. v.IndexCode = v.IndexCode + strconv.Itoa(i)
  315. codeCompanyMap[v.IndexName] = v.IndexCode
  316. newId, err := models.AddBaseFromCoalmineMapping(v)
  317. if err != nil {
  318. fmt.Println("再次添加公司指标名称错误", err)
  319. fmt.Println(v.IndexName, v.IndexCode)
  320. continue
  321. } else {
  322. fmt.Println("新增公司成功", newId)
  323. break
  324. }
  325. }
  326. } else {
  327. fmt.Println("新增公司成功", newId)
  328. }
  329. }
  330. }
  331. fmt.Println("公司指标操作完成")
  332. for _, v := range itemsCompanyIndex {
  333. v.IndexCode = codeCompanyMap[v.IndexName]
  334. if indexCompanyMap[v.IndexName+v.DataTime] == "" && v.DealValue != "" {
  335. newId, err := models.AddBaseFromCoalmineCompanyIndex(v)
  336. if err != nil {
  337. fmt.Println("添加公司数据错误", err)
  338. } else {
  339. fmt.Println("新增公司数据成功", newId)
  340. }
  341. } else {
  342. if indexCompanyMap[v.IndexName+v.DataTime] != v.DealValue && v.DealValue != "" {
  343. err = models.UpdateBaseFromCoalmineCompanyIndex(v)
  344. if err != nil {
  345. fmt.Println("修改数据错误错误", err)
  346. }
  347. }
  348. }
  349. }
  350. return
  351. }
  352. func TrimCompanyName(name string) string {
  353. name = strings.Replace(name, "有限", "", -1)
  354. name = strings.Replace(name, "股份", "", -1)
  355. name = strings.Replace(name, "责任", "", -1)
  356. name = strings.Replace(name, "公司", "", -1)
  357. name = strings.Replace(name, "(", "", -1)
  358. name = strings.Replace(name, ")", "", -1)
  359. return name
  360. }
  361. func ContainsSpecialName(name string) bool {
  362. return strings.Contains(name, "陕西") ||
  363. strings.Contains(name, "海南") ||
  364. strings.Contains(name, "河南") ||
  365. strings.Contains(name, "河北") ||
  366. strings.Contains(name, "澳门") ||
  367. strings.Contains(name, "内蒙古") ||
  368. strings.Contains(name, "黑龙江")
  369. }
  370. func TrimProvinceName(name string) string {
  371. name = strings.Replace(name, "陕西省", "sns", -1)
  372. name = strings.Replace(name, "陕西", "sn", -1)
  373. name = strings.Replace(name, "海南市", "hi", -1)
  374. name = strings.Replace(name, "海南", "hi", -1)
  375. name = strings.Replace(name, "河南省", "has", -1)
  376. name = strings.Replace(name, "河南", "ha", -1)
  377. name = strings.Replace(name, "河北省", "hes", -1)
  378. name = strings.Replace(name, "河北", "he", -1)
  379. name = strings.Replace(name, "澳门", "mo", -1)
  380. name = strings.Replace(name, "内蒙古自治区", "nm", -1)
  381. name = strings.Replace(name, "内蒙古", "nm", -1)
  382. name = strings.Replace(name, "黑龙江", "hl", -1)
  383. name = strings.Replace(name, "(", "", -1)
  384. name = strings.Replace(name, ")", "", -1)
  385. name = strings.Replace(name, "+", "", -1)
  386. return name
  387. }