commodity_coal_mine.go 10 KB

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