week50.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. package base_from_yongyi_v2
  2. import (
  3. "eta/eta_data_analysis/models"
  4. "eta/eta_data_analysis/utils"
  5. "fmt"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/xuri/excelize/v2"
  11. )
  12. // HandleYongyiExcelWeekly41 月度-小猪(50斤以下)存栏
  13. func HandleYongyiExcelWeekly41(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  14. defer func() {
  15. if err != nil {
  16. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly40 国产冻品2-4号肉价格 ErrMsg: %s", err.Error()))
  17. }
  18. }()
  19. cols, err := f.GetCols(sheetName)
  20. if err != nil {
  21. err = fmt.Errorf("f GetCols err: %s", err.Error())
  22. return
  23. }
  24. classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName)
  25. sort := 0
  26. var preIndexName string
  27. dateIndexList := make([]string, 0)
  28. indexInfoMap := make(map[string]*models.YongyiExcelIndex)
  29. indexNamePart2 := map[string]struct{}{"全国": {}, "华北": {}, "东北": {}, "华中": {}, "华东": {}, "华南": {}, "西南": {}}
  30. for i, col := range cols {
  31. if len(col) <= 3 {
  32. continue
  33. }
  34. if i == 0 && col[1] == "日期" {
  35. for _, cell := range col {
  36. tmpDateTime, err := time.Parse("2006年1月", cell)
  37. if err != nil {
  38. dateIndexList = append(dateIndexList, "")
  39. } else {
  40. dateTime := LastDayOfMonth(tmpDateTime.Year(), tmpDateTime.Month())
  41. dateIndexList = append(dateIndexList, dateTime)
  42. }
  43. }
  44. }
  45. if len(dateIndexList) == 0 {
  46. continue
  47. }
  48. if col[1] != "" && col[1] != "日期" {
  49. var indexName, indexNameSuffix string
  50. if _, ok := indexNamePart2[col[1]]; ok {
  51. preIndexName = col[1]
  52. indexName = fmt.Sprintf("%s/%s", namePrefix, col[1])
  53. indexNameSuffix = col[1]
  54. } else if col[1] == "环比" {
  55. indexName = fmt.Sprintf("%s(%s)/%s", namePrefix, col[1], preIndexName)
  56. indexNameSuffix = fmt.Sprintf("(%s)%s", col[1], preIndexName)
  57. } else {
  58. continue
  59. }
  60. for j, cell := range col {
  61. if !isValueValid(cell) {
  62. continue
  63. }
  64. if indexInfo, ok := indexInfoMap[indexName]; !ok {
  65. tmpYongyiIndex := new(models.YongyiExcelIndex)
  66. tmpYongyiIndex.ClassifyName = classifyName
  67. tmpYongyiIndex.ClassifySort = classifySort
  68. tmpYongyiIndex.Frequency = frequency
  69. tmpYongyiIndex.Unit = unit
  70. tmpYongyiIndex.IndexName = indexName
  71. tmpYongyiIndex.IndexCode = namePrefixPingin + utils.GetFirstPingYin(indexNameSuffix)
  72. tmpYongyiIndex.Sort = sort
  73. tmpYongyiIndex.ExcelDataMap = make(map[string]string)
  74. if string(cell[len(cell)-1]) == `%` {
  75. tmpYongyiIndex.Unit = `%`
  76. tmpYongyiIndex.ExcelDataMap[dateIndexList[j]] = cell[:len(cell)-1]
  77. } else {
  78. tmpYongyiIndex.ExcelDataMap[dateIndexList[j]] = cell
  79. }
  80. indexInfoMap[indexName] = tmpYongyiIndex
  81. indexList = append(indexList, tmpYongyiIndex)
  82. sort++
  83. } else {
  84. if string(cell[len(cell)-1]) == `%` {
  85. indexInfo.ExcelDataMap[dateIndexList[j]] = cell[:len(cell)-1]
  86. } else {
  87. indexInfo.ExcelDataMap[dateIndexList[j]] = cell
  88. }
  89. }
  90. }
  91. }
  92. }
  93. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly41 %s 更新指标数量:%d", sheetName, len(indexList)))
  94. return
  95. }
  96. // HandleYongyiExcelWeekly42 月度猪肉供应占比
  97. func HandleYongyiExcelWeekly42(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  98. defer func() {
  99. if err != nil {
  100. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly40 月度猪肉供应占比 ErrMsg: %s", err.Error()))
  101. }
  102. }()
  103. rows, err := f.GetRows(sheetName)
  104. if err != nil {
  105. err = fmt.Errorf("f GetRows err: %s", err.Error())
  106. return
  107. }
  108. classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName)
  109. re := regexp.MustCompile(`[+-]?[0-9]*\.?[0-9]+`)
  110. yearIndexMap := make(map[int]string)
  111. yongyiExcelIndex := new(models.YongyiExcelIndex)
  112. yongyiExcelIndex.ClassifyName = classifyName
  113. yongyiExcelIndex.ClassifySort = classifySort
  114. yongyiExcelIndex.Frequency = frequency
  115. yongyiExcelIndex.Unit = unit
  116. yongyiExcelIndex.IndexName = namePrefix
  117. yongyiExcelIndex.IndexCode = namePrefixPingin
  118. yongyiExcelIndex.Sort = 0
  119. yongyiExcelIndex.ExcelDataMap = make(map[string]string)
  120. indexList = append(indexList, yongyiExcelIndex)
  121. for i, row := range rows {
  122. if i == 0 {
  123. // 记录年份索引
  124. for j, cell := range row {
  125. if isValueValid(cell) {
  126. yearIndexMap[j] = cell
  127. }
  128. }
  129. } else {
  130. var month string
  131. for j, cell := range row {
  132. if j == 0 {
  133. if tmp := re.FindString(cell); tmp != "" {
  134. month = tmp
  135. continue
  136. }
  137. }
  138. if month == "" {
  139. break
  140. }
  141. tmpYear, err := strconv.Atoi(yearIndexMap[j])
  142. if err != nil {
  143. continue
  144. }
  145. tmpMonth, err := strconv.Atoi(month)
  146. if err != nil {
  147. continue
  148. }
  149. dataTime := LastDayOfMonth(tmpYear, time.Month(tmpMonth))
  150. if isValueValid(cell) {
  151. newCell := strings.ReplaceAll(cell, "%", "")
  152. yongyiExcelIndex.ExcelDataMap[dataTime] = newCell
  153. }
  154. }
  155. }
  156. }
  157. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly42 %s 更新指标数量:%d", sheetName, len(indexList)))
  158. return
  159. }
  160. // HandleYongyiExcelWeekly43 历史出栏体重
  161. func HandleYongyiExcelWeekly43(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  162. defer func() {
  163. if err != nil {
  164. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly43 %s ErrMsg: %s", sheetName, err.Error()))
  165. }
  166. }()
  167. rows, err := f.GetRows(sheetName)
  168. if err != nil {
  169. err = fmt.Errorf("f GetRows err: %s", err.Error())
  170. return
  171. }
  172. re := regexp.MustCompile(`\d+`)
  173. classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName)
  174. yongyiExcelIndex := new(models.YongyiExcelIndex)
  175. yongyiExcelIndex.ClassifyName = classifyName
  176. yongyiExcelIndex.ClassifySort = classifySort
  177. yongyiExcelIndex.Frequency = frequency
  178. yongyiExcelIndex.Unit = unit
  179. yongyiExcelIndex.IndexName = namePrefix
  180. yongyiExcelIndex.IndexCode = namePrefixPingin
  181. yongyiExcelIndex.Sort = 0
  182. yongyiExcelIndex.ExcelDataMap = make(map[string]string)
  183. indexList = append(indexList, yongyiExcelIndex)
  184. for i, row := range rows {
  185. if i == 0 {
  186. continue
  187. }
  188. if len(row) < 2 {
  189. continue
  190. }
  191. dateArr := re.FindAllString(row[0], -1)
  192. year, err := strconv.Atoi(dateArr[0])
  193. if err != nil {
  194. continue
  195. }
  196. week, err := strconv.Atoi(dateArr[1])
  197. if err != nil {
  198. continue
  199. }
  200. if !isValueValid(row[1]) {
  201. continue
  202. }
  203. date := getSundayOfWeek(year, week)
  204. yongyiExcelIndex.ExcelDataMap[date] = row[1]
  205. }
  206. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly43 %s 更新指标数量:%d", sheetName, len(indexList)))
  207. return
  208. }
  209. // HandleYongyiExcelWeekly44 重要部位冻品进口
  210. func HandleYongyiExcelWeekly44(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  211. defer func() {
  212. if err != nil {
  213. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly44 %s ErrMsg: %s", sheetName, err.Error()))
  214. }
  215. }()
  216. cols, err := f.GetCols(sheetName)
  217. if err != nil {
  218. err = fmt.Errorf("f GetCols err: %s", err.Error())
  219. return
  220. }
  221. classifyName, classifySort, frequency, _, namePrefix, namePrefixPingin := GetBaseInfo(sheetName)
  222. sort := 0
  223. re := regexp.MustCompile(`\d+`)
  224. dateIndexMap := make(map[int]string)
  225. for i, col := range cols {
  226. if i == 0 {
  227. for j, cell := range col {
  228. dateArr := re.FindAllString(cell, -1)
  229. if len(dateArr) != 2 {
  230. continue
  231. }
  232. year, err := strconv.Atoi(dateArr[0])
  233. if err != nil {
  234. continue
  235. }
  236. month, err := strconv.Atoi(dateArr[1])
  237. if err != nil {
  238. continue
  239. }
  240. dateIndexMap[j] = LastDayOfMonth(year, time.Month(month))
  241. }
  242. } else {
  243. if len(col) < 2 {
  244. continue
  245. }
  246. if col[1] == "" {
  247. continue
  248. }
  249. indexName := fmt.Sprintf("%s/%s", namePrefix, col[1])
  250. yongyiExcelIndex := new(models.YongyiExcelIndex)
  251. yongyiExcelIndex.ClassifyName = classifyName
  252. yongyiExcelIndex.ClassifySort = classifySort
  253. yongyiExcelIndex.Frequency = frequency
  254. yongyiExcelIndex.IndexName = indexName
  255. yongyiExcelIndex.IndexCode = namePrefixPingin + utils.GetFirstPingYin(col[1])
  256. yongyiExcelIndex.Sort = sort
  257. yongyiExcelIndex.ExcelDataMap = make(map[string]string)
  258. if col[1] == "柜数" {
  259. yongyiExcelIndex.Unit = "个"
  260. } else if col[1] == "合计" {
  261. yongyiExcelIndex.Unit = "kg"
  262. } else {
  263. continue
  264. }
  265. for j, cell := range col {
  266. if j == 0 {
  267. continue
  268. }
  269. cell = strings.ReplaceAll(cell, ",", "")
  270. if !isValueValid(cell) {
  271. continue
  272. }
  273. yongyiExcelIndex.ExcelDataMap[dateIndexMap[j]] = cell
  274. }
  275. indexList = append(indexList, yongyiExcelIndex)
  276. sort++
  277. }
  278. }
  279. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly44 %s 更新指标数量:%d", sheetName, len(indexList)))
  280. return
  281. }
  282. // HandleYongyiExcelWeekly45 各存栏规模
  283. func HandleYongyiExcelWeekly45(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  284. defer func() {
  285. if err != nil {
  286. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly45 %s ErrMsg: %s", sheetName, err.Error()))
  287. }
  288. }()
  289. rows, err := f.GetRows(sheetName)
  290. if err != nil {
  291. err = fmt.Errorf("f GetRows err: %s", err.Error())
  292. return
  293. }
  294. classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName)
  295. sort := 0
  296. re := regexp.MustCompile(`\d+`)
  297. indexNamePart3 := make([]string, 0)
  298. indexInfoMap := make(map[string]*models.YongyiExcelIndex)
  299. for i, row := range rows {
  300. if i == 0 {
  301. continue
  302. }
  303. if i == 1 {
  304. for j, cell := range row {
  305. if j >= 2 {
  306. indexNamePart3 = append(indexNamePart3, cell)
  307. } else {
  308. indexNamePart3 = append(indexNamePart3, "")
  309. }
  310. }
  311. }
  312. if len(row) == 0 {
  313. break
  314. }
  315. if i > 1 && len(row) > 2 {
  316. var date string
  317. for j, cell := range row {
  318. if j == 0 {
  319. dateArr := re.FindAllString(cell, -1)
  320. if len(dateArr) != 2 {
  321. break
  322. }
  323. year, err := strconv.Atoi(dateArr[0])
  324. if err != nil {
  325. break
  326. }
  327. month, err := strconv.Atoi(dateArr[1])
  328. if err != nil {
  329. break
  330. }
  331. date = LastDayOfMonth(year, time.Month(month))
  332. }
  333. if !isValueValid(cell) {
  334. continue
  335. }
  336. indexName := fmt.Sprintf("%s/%s/%s", namePrefix, row[1], indexNamePart3[j])
  337. if indexInfo, ok := indexInfoMap[indexName]; !ok {
  338. tmpYongyiIndex := new(models.YongyiExcelIndex)
  339. tmpYongyiIndex.ClassifyName = classifyName
  340. tmpYongyiIndex.ClassifySort = classifySort
  341. tmpYongyiIndex.Frequency = frequency
  342. tmpYongyiIndex.Unit = unit
  343. tmpYongyiIndex.IndexName = indexName
  344. tmpYongyiIndex.IndexCode = namePrefixPingin + utils.GetFirstPingYin(row[1]+indexNamePart3[j])
  345. tmpYongyiIndex.Sort = sort
  346. tmpYongyiIndex.ExcelDataMap = make(map[string]string)
  347. newCell := strings.ReplaceAll(cell, "%", "")
  348. tmpYongyiIndex.ExcelDataMap[date] = newCell
  349. indexInfoMap[indexName] = tmpYongyiIndex
  350. indexList = append(indexList, tmpYongyiIndex)
  351. sort++
  352. } else {
  353. newCell := strings.ReplaceAll(cell, "%", "")
  354. indexInfo.ExcelDataMap[date] = newCell
  355. }
  356. }
  357. }
  358. }
  359. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly45 %s 更新指标数量:%d", sheetName, len(indexList)))
  360. return
  361. }
  362. // HandleYongyiExcelWeekly46 进口肉
  363. func HandleYongyiExcelWeekly46(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  364. defer func() {
  365. if err != nil {
  366. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly46 %s ErrMsg: %s", sheetName, err.Error()))
  367. }
  368. }()
  369. cols, err := f.GetCols(sheetName)
  370. if err != nil {
  371. err = fmt.Errorf("f GetCols err: %s", err.Error())
  372. return
  373. }
  374. classifyName, classifySort, frequency, _, _, namePrefixPingin := GetBaseInfo(sheetName)
  375. sort := 0
  376. var indexName string
  377. re := regexp.MustCompile(`\d+`)
  378. indexInfoMap := make(map[string]*models.YongyiExcelIndex)
  379. for _, col := range cols {
  380. if indexName == "" {
  381. indexName = col[1]
  382. continue
  383. }
  384. if len(col) == 0 {
  385. indexName = ""
  386. continue
  387. }
  388. if indexName != "" && strings.Contains(indexName, "猪肉进口量") {
  389. indexName = "猪肉进口量"
  390. var year int
  391. if strings.Contains(col[2], "年") {
  392. year, err = strconv.Atoi(re.FindString(col[2]))
  393. if err != nil {
  394. continue
  395. }
  396. }
  397. for j, cell := range col {
  398. if !isValueValid(cell) {
  399. continue
  400. }
  401. monthArr := strings.Split(cols[0][j], "-")
  402. var month int
  403. switch len(monthArr[1]) {
  404. case 2:
  405. month, err = strconv.Atoi(re.FindString(monthArr[1]))
  406. if err != nil {
  407. continue
  408. }
  409. case 1:
  410. month, err = strconv.Atoi(re.FindString(monthArr[0]))
  411. if err != nil {
  412. continue
  413. }
  414. }
  415. date := LastDayOfMonth(year, time.Month(month))
  416. if date == "" {
  417. continue
  418. }
  419. if indexInfo, ok := indexInfoMap[indexName]; !ok {
  420. tmpYongyiIndex := new(models.YongyiExcelIndex)
  421. tmpYongyiIndex.ClassifyName = classifyName
  422. tmpYongyiIndex.ClassifySort = classifySort
  423. tmpYongyiIndex.Frequency = frequency
  424. tmpYongyiIndex.Unit = "万吨"
  425. tmpYongyiIndex.IndexName = indexName
  426. tmpYongyiIndex.IndexCode = namePrefixPingin + utils.GetFirstPingYin(indexName)
  427. tmpYongyiIndex.Sort = sort
  428. tmpYongyiIndex.ExcelDataMap = make(map[string]string)
  429. tmpYongyiIndex.ExcelDataMap[date] = cell
  430. indexInfoMap[indexName] = tmpYongyiIndex
  431. indexList = append(indexList, tmpYongyiIndex)
  432. sort++
  433. } else {
  434. indexInfo.ExcelDataMap[date] = cell
  435. }
  436. }
  437. } else if indexName != "" && strings.Contains(indexName, "猪肉及杂碎进口量") {
  438. // preName := "猪肉及杂碎进口量"
  439. }
  440. }
  441. return
  442. }