commodity_coal_firm.go 9.4 KB

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