commodity_coal_firm.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. "os"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. func FileCoalFirm() {
  15. var err error
  16. defer func() {
  17. if err != nil {
  18. utils.FileLog.Info("RefreshDataFromCoalFirm Err:", err)
  19. fmt.Println("RefreshDataFromCoalFirm Err:" + err.Error())
  20. msg := "失败提醒" + "RefreshDataFromCoalFirm ErrMsg:" + err.Error()
  21. go alarm_msg.SendAlarmMsg(msg, 3)
  22. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromCoalFirm ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  23. }
  24. }()
  25. syear := time.Now().Year()
  26. nyear := strconv.Itoa(syear)
  27. var smonth string
  28. dateMonth := int(time.Now().Month())
  29. smonth = strconv.Itoa(dateMonth)
  30. path := "/home/code/python/coal_mail/emailFile/全国分企业煤炭产量旬度数据(%s年%s月).xlsx"
  31. path = fmt.Sprintf(path, nyear, smonth)
  32. path = "/home/code/python/coal_mail/emailFile/全国分企业煤炭产量旬度数据(2022年10月上旬).xlsx"
  33. exist, err := PathExists(path)
  34. if err != nil {
  35. fmt.Println(err)
  36. return
  37. }
  38. fmt.Println(path)
  39. if !exist {
  40. path = "/home/code/python/coal_mail/emailFile/全国分企业煤炭产量旬度数据(%s年%s月中旬).xlsx"
  41. path = fmt.Sprintf(path, nyear, smonth)
  42. exist, err = PathExists(path)
  43. if err != nil {
  44. fmt.Println(err)
  45. return
  46. }
  47. }
  48. fmt.Println(path)
  49. if !exist {
  50. path = "/home/code/python/coal_mail/emailFile/全国分企业煤炭产量旬度数据(%s年%s月上旬).xlsx"
  51. path = fmt.Sprintf(path, nyear, smonth)
  52. exist, err = PathExists(path)
  53. if err != nil {
  54. fmt.Println(err)
  55. return
  56. }
  57. }
  58. fmt.Println(path)
  59. if !exist {
  60. //本月三个文件都没有,去取上一个月的
  61. dateMonth = dateMonth - 1
  62. smonth = strconv.Itoa(dateMonth)
  63. path = "/home/code/python/coal_mail/emailFile/全国分企业煤炭产量旬度数据(%s年%s月).xlsx"
  64. path = fmt.Sprintf(path, nyear, smonth)
  65. exist, err = PathExists(path)
  66. if err != nil {
  67. fmt.Println(err)
  68. return
  69. }
  70. }
  71. fmt.Println(path)
  72. if !exist {
  73. //本月三个文件都没有,去取上一个月的中旬
  74. smonth = strconv.Itoa(dateMonth)
  75. path = "/home/code/python/coal_mail/emailFile/全国分企业煤炭产量旬度数据(%s年%s月中旬).xlsx"
  76. path = fmt.Sprintf(path, nyear, smonth)
  77. exist, err = PathExists(path)
  78. if err != nil {
  79. fmt.Println(err)
  80. return
  81. }
  82. }
  83. fmt.Println(path)
  84. if !exist {
  85. //本月三个文件都没有,去取上一个月的上旬
  86. smonth = strconv.Itoa(dateMonth)
  87. path = "/home/code/python/coal_mail/emailFile/全国分企业煤炭产量旬度数据(%s年%s月上旬).xlsx"
  88. path = fmt.Sprintf(path, nyear, smonth)
  89. exist, err = PathExists(path)
  90. if err != nil {
  91. fmt.Println(err)
  92. return
  93. }
  94. }
  95. fmt.Println(path)
  96. var xlFile *xlsx.File
  97. if exist {
  98. xlFile, err = xlsx.OpenFile(path)
  99. if err != nil {
  100. fmt.Println("OpenFile err:", err)
  101. return
  102. }
  103. } else {
  104. return
  105. }
  106. var year, month, day string
  107. var yearMap = make(map[int]string)
  108. var monthMap = make(map[int]string)
  109. var dateMap = make(map[int]string)
  110. var mappingItems []*models.BaseFromCoalmineMapping
  111. var indexItems []*models.BaseFromCoalmineFirmIndex
  112. var codeMap = make(map[string]string)
  113. var indexMap = make(map[string]string)
  114. var groupName string
  115. codeList, err := models.GetBaseFromCoalmineMapping()
  116. if err != nil && err.Error() != utils.ErrNoRow() {
  117. utils.FileLog.Info("获取煤炭指标失败:", err)
  118. return
  119. }
  120. if len(codeList) > 0 {
  121. for _, v := range codeList {
  122. codeMap[v.IndexName] = v.IndexCode
  123. }
  124. }
  125. indexCompanyList, err := models.GetBaseFromCoalmineFirmIndex()
  126. if err != nil && err.Error() != utils.ErrNoRow() {
  127. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  128. return
  129. }
  130. if len(indexCompanyList) > 0 {
  131. for _, v := range indexCompanyList {
  132. indexMap[v.IndexName+v.DataTime] = v.DealValue
  133. }
  134. }
  135. for _, sheet := range xlFile.Sheets {
  136. //遍历行读取
  137. maxRow := sheet.MaxRow
  138. for i := 0; i < maxRow; i++ {
  139. //获取年份
  140. //if i == 2 {
  141. // row := sheet.Row(i)
  142. // cells := row.Cells
  143. // for k, cell := range cells {
  144. // text := cell.String()
  145. // if k > 0 && text != "" {
  146. // year = text
  147. // fmt.Println("year:",year)
  148. // fmt.Println("k:",k)
  149. // yearMap[k] = year
  150. // }
  151. // }
  152. //}
  153. //获取月份和年度
  154. if i == 3 {
  155. row := sheet.Row(i)
  156. cells := row.Cells
  157. for k, cell := range cells {
  158. text := cell.String()
  159. if k > 0 && text != "" {
  160. month = text
  161. month = utils.ConvertToFormatDay(month)
  162. time, _ := time.Parse(utils.FormatDate, month)
  163. yearInt := time.Year()
  164. monthInt := int(time.Month())
  165. year = strconv.Itoa(yearInt)
  166. yearMap[k] = year
  167. month = strconv.Itoa(monthInt)
  168. monthMap[k] = month
  169. }
  170. }
  171. }
  172. //获取旬度
  173. if i == 4 {
  174. row := sheet.Row(i)
  175. cells := row.Cells
  176. for k, cell := range cells {
  177. text := cell.String()
  178. if k > 0 && text != "" {
  179. if yearMap[k] != "" {
  180. year = yearMap[k]
  181. }
  182. if monthMap[k] != "" {
  183. month = monthMap[k]
  184. }
  185. day = text
  186. dateMap[k] = year + "年" + month + "月" + day
  187. }
  188. }
  189. }
  190. //获取企业或地区名及信息
  191. if i > 4 {
  192. row := sheet.Row(i)
  193. cells := row.Cells
  194. var companyName string
  195. for k, cell := range cells {
  196. if k == 0 {
  197. companyName = cell.String()
  198. //省名
  199. if !strings.Contains(companyName, " ") {
  200. groupName = companyName
  201. }
  202. if groupName == "全国合计" {
  203. groupName = "全国"
  204. }
  205. //若有下面四个名称要拼上省名不然会重复
  206. if companyName == " 地方合计" || companyName == " 地方国有" ||
  207. companyName == " 地方乡镇" || companyName == " 国有重点" {
  208. companyName = groupName + companyName
  209. }
  210. companyName = strings.Replace(companyName, " ", "", -1)
  211. var item models.BaseFromCoalmineMapping
  212. //生成code
  213. exists := ContainsSpecialName(companyName)
  214. var code string
  215. if exists {
  216. abbr := trimProvinceName(companyName)
  217. //取处理后公司名首字母缩写
  218. a := pinyin.NewArgs()
  219. rows := pinyin.Pinyin(companyName[9:], a)
  220. for i := 0; i < len(rows); i++ {
  221. if len(rows[i]) != 0 {
  222. str := rows[i][0]
  223. pi := str[0:1]
  224. code += pi
  225. }
  226. }
  227. item.IndexCode = abbr[:2] + code + "firm"
  228. } else {
  229. a := pinyin.NewArgs()
  230. rows := pinyin.Pinyin(companyName, a)
  231. for i := 0; i < len(rows); i++ {
  232. if len(rows[i]) != 0 {
  233. str := rows[i][0]
  234. pi := str[0:1]
  235. code += pi
  236. }
  237. }
  238. item.IndexCode = code + "firm"
  239. }
  240. item.IndexName = companyName
  241. item.CreateTime = time.Now()
  242. mappingItems = append(mappingItems, &item)
  243. } else {
  244. dealValue := cell.String()
  245. item := models.BaseFromCoalmineFirmIndex{
  246. IndexName: companyName,
  247. IndexCode: codeMap[companyName],
  248. DataTime: dateMap[k],
  249. DealValue: dealValue,
  250. GroupName: groupName,
  251. Source: "全国分企业",
  252. Unit: "万吨",
  253. Frequency: "旬度",
  254. CreateTime: time.Now(),
  255. ModifyTime: time.Now(),
  256. }
  257. indexItems = append(indexItems, &item)
  258. }
  259. }
  260. }
  261. }
  262. }
  263. //添加数据到数据库
  264. for _, v := range mappingItems {
  265. if codeMap[v.IndexName] == "" {
  266. codeMap[v.IndexName] = v.IndexCode
  267. newId, err := models.AddBaseFromCoalmineMapping(v)
  268. if err != nil {
  269. for i := 0; i < 10; i++ {
  270. v.IndexCode = v.IndexCode + strconv.Itoa(i)
  271. codeMap[v.IndexName] = v.IndexCode
  272. newId, err := models.AddBaseFromCoalmineMapping(v)
  273. if err != nil {
  274. fmt.Println("再次添加公司指标名称错误", err)
  275. continue
  276. } else {
  277. fmt.Println("新增公司成功", newId)
  278. break
  279. }
  280. }
  281. } else {
  282. fmt.Println("新增公司成功", newId)
  283. }
  284. }
  285. }
  286. fmt.Println("指标操作完成")
  287. //给indexItem中的code赋值并插入index表
  288. for _, v := range indexItems {
  289. v.IndexCode = codeMap[v.IndexName]
  290. if indexMap[v.IndexName+v.DataTime] == "" && v.DealValue != "" {
  291. newId, err := models.AddBaseFromCoalFirmIndex(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.UpdateBaseFromCoalFirmIndex(v)
  300. if err != nil {
  301. fmt.Println("修改数据错误错误", err)
  302. return
  303. }
  304. }
  305. }
  306. }
  307. return
  308. }
  309. func trimProvinceName(name string) string {
  310. name = strings.Replace(name, "陕西省", "sn", -1)
  311. name = strings.Replace(name, "陕西", "sn", -1)
  312. name = strings.Replace(name, "海南市", "hi", -1)
  313. name = strings.Replace(name, "海南", "hi", -1)
  314. name = strings.Replace(name, "河南省", "ha", -1)
  315. name = strings.Replace(name, "河南", "ha", -1)
  316. name = strings.Replace(name, "河北省", "he", -1)
  317. name = strings.Replace(name, "河北", "he", -1)
  318. name = strings.Replace(name, "澳门", "mo", -1)
  319. name = strings.Replace(name, "内蒙古自治区", "nm", -1)
  320. name = strings.Replace(name, "内蒙古", "nm", -1)
  321. name = strings.Replace(name, "黑龙江", "hl", -1)
  322. name = strings.Replace(name, "(", "", -1)
  323. name = strings.Replace(name, ")", "", -1)
  324. name = strings.Replace(name, "+", "", -1)
  325. return name
  326. }
  327. func PathExists(path string) (bool, error) {
  328. _, err := os.Stat(path)
  329. if err == nil { //文件或者目录存在
  330. return true, nil
  331. }
  332. if os.IsNotExist(err) {
  333. return false, nil
  334. }
  335. return false, err
  336. }