commodity_coal_firm.go 9.5 KB

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