week40.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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. // HandleYongyiExcelWeekly31 月度-屠宰厂公母比例
  13. func HandleYongyiExcelWeekly31(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  14. defer func() {
  15. if err != nil {
  16. //fmt.Printf("HandleYongyiExcelWeekly31 月度-屠宰厂公母比例 ErrMsg: %s\n", err.Error())
  17. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly31 月度-屠宰厂公母比例 ErrMsg: %s", err.Error()))
  18. }
  19. }()
  20. rows, e := f.GetRows(sheetName)
  21. if e != nil {
  22. err = fmt.Errorf("f GetRows err: %s", e.Error())
  23. return
  24. }
  25. // 获取指标分类
  26. classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName)
  27. // 遍历行读取
  28. indexList = make([]*models.YongyiExcelIndex, 0)
  29. sort := 0
  30. nameMap := make(map[int]string)
  31. // 指标名称
  32. indexMap := make(map[string]*models.YongyiExcelIndex)
  33. for i, row := range rows {
  34. //fmt.Printf("当前第%d行 \n", i)
  35. if i == 0 {
  36. for k, text := range row {
  37. if text != "" {
  38. text = strings.TrimSpace(text)
  39. nameMap[k] = text
  40. }
  41. }
  42. } else { //数据列
  43. date := ""
  44. for k, text := range row {
  45. //fmt.Printf("当前第%d列 \n", k)
  46. if k == 0 {
  47. text = strings.TrimSpace(text)
  48. var dateT time.Time
  49. dateT, e = time.ParseInLocation("2006年1月", text, time.Local)
  50. if e != nil {
  51. text = strings.TrimSpace(text)
  52. dateSlice := strings.Split(text, "-")
  53. if len(dateSlice) <= 1 {
  54. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, text))
  55. continue
  56. }
  57. dateT, e = time.ParseInLocation("2006年1月2日", dateSlice[0], time.Local)
  58. if e != nil {
  59. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e))
  60. continue
  61. }
  62. date = dateT.Format("2006年1月") + dateSlice[1]
  63. dateT, e = time.ParseInLocation("2006年1月2日", date, time.Local)
  64. if e != nil {
  65. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e))
  66. continue
  67. }
  68. date = dateT.Format(utils.FormatDate)
  69. } else {
  70. // 查询当月的最后一天
  71. monthDate := dateT.Format(utils.FormatYearMonthDate)
  72. firstDayStr := monthDate + "-01"
  73. tmpT, _ := time.ParseInLocation(utils.FormatDate, firstDayStr, time.Local)
  74. date = tmpT.AddDate(0, 1, -1).Format(utils.FormatDate)
  75. }
  76. continue
  77. } else {
  78. // 判断出不是字符的,则过滤
  79. if text == "" {
  80. continue
  81. }
  82. if strings.Contains(text, "%") {
  83. text = strings.Replace(text, "%", "", 1)
  84. }
  85. _, e := strconv.ParseFloat(text, 64)
  86. if e != nil {
  87. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e))
  88. continue
  89. }
  90. }
  91. name, ok2 := nameMap[k]
  92. if !ok2 {
  93. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
  94. return
  95. }
  96. //fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, name)
  97. unit = "头"
  98. if strings.Contains(name, "占比") {
  99. unit = "%"
  100. }
  101. fullIndexName := fmt.Sprintf("%s%s", namePrefix, name)
  102. namePingin := utils.GetFirstPingYin(name)
  103. fullIndexNamePingyin := namePrefixPingin + namePingin
  104. indexItem, okIndex := indexMap[fullIndexName]
  105. if !okIndex {
  106. // 新增指标
  107. indexItem = new(models.YongyiExcelIndex)
  108. indexItem.IndexName = fullIndexName
  109. indexItem.ClassifyName = classifyName
  110. indexItem.ClassifySort = classifySort
  111. indexItem.IndexCode = fullIndexNamePingyin
  112. indexItem.Frequency = frequency
  113. indexItem.Sort = sort
  114. indexItem.Unit = unit
  115. indexItem.ExcelDataMap = make(map[string]string)
  116. sort++
  117. }
  118. //fmt.Printf("IndexCode: %s", indexItem.IndexCode)
  119. indexItem.ExcelDataMap[date] = text
  120. indexMap[fullIndexName] = indexItem
  121. continue
  122. }
  123. }
  124. }
  125. for _, v := range indexMap {
  126. indexList = append(indexList, v)
  127. }
  128. return
  129. }
  130. // HandleYongyiExcelWeekly32 月度-生产指标(2021.5.7新增)
  131. func HandleYongyiExcelWeekly32(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  132. defer func() {
  133. if err != nil {
  134. //fmt.Printf("HandleYongyiExcelWeekly32 月度-生产指标(2021.5.7新增) ErrMsg: %s\n", err.Error())
  135. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly32 月度-生产指标(2021.5.7新增) ErrMsg: %s", err.Error()))
  136. }
  137. }()
  138. rows, e := f.GetRows(sheetName)
  139. if e != nil {
  140. err = fmt.Errorf("f GetRows err: %s", e.Error())
  141. return
  142. }
  143. // 获取指标分类
  144. classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName)
  145. // 遍历行读取
  146. indexList = make([]*models.YongyiExcelIndex, 0)
  147. sort := 0
  148. nameMap := make(map[int]string)
  149. provinceMap := make(map[int]string)
  150. mergeCellMap, err := GetMergeCells(f, sheetName)
  151. if err != nil {
  152. err = fmt.Errorf("获取合并单元格失败, sheetName: %s", sheetName)
  153. return
  154. }
  155. // 指标名称
  156. indexMap := make(map[string]*models.YongyiExcelIndex)
  157. for i, row := range rows {
  158. //fmt.Printf("当前第%d行 \n", i)
  159. currentMergeCells, mergeOk := mergeCellMap[i]
  160. if i == 0 {
  161. for k, text := range row {
  162. if text != "" {
  163. if mergeOk {
  164. for j, v := range currentMergeCells {
  165. provinceMap[j] = v
  166. }
  167. }
  168. provinceMap[k] = text
  169. }
  170. }
  171. } else if i == 1 {
  172. for k, text := range row {
  173. if text != "" {
  174. nameMap[k] = text
  175. }
  176. }
  177. } else { //数据列
  178. date := ""
  179. for k, text := range row {
  180. //fmt.Printf("当前第%d列 \n", k)
  181. if k == 0 {
  182. text = strings.TrimSpace(text)
  183. var dateT time.Time
  184. dateT, e = time.ParseInLocation("2006年1月", text, time.Local)
  185. if e != nil {
  186. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e))
  187. continue
  188. }
  189. // 查询当月的最后一天
  190. monthDate := dateT.Format(utils.FormatYearMonthDate)
  191. firstDayStr := monthDate + "-01"
  192. tmpT, _ := time.ParseInLocation(utils.FormatDate, firstDayStr, time.Local)
  193. date = tmpT.AddDate(0, 1, -1).Format(utils.FormatDate)
  194. fmt.Println(date)
  195. continue
  196. } else {
  197. // 判断出不是字符的,则过滤
  198. if text == "" {
  199. continue
  200. }
  201. if strings.Contains(text, "%") {
  202. text = strings.Replace(text, "%", "", 1)
  203. }
  204. _, e := strconv.ParseFloat(text, 64)
  205. if e != nil {
  206. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e))
  207. continue
  208. }
  209. }
  210. province, ok1 := provinceMap[k]
  211. if !ok1 {
  212. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
  213. return
  214. }
  215. name, ok2 := nameMap[k]
  216. if !ok2 {
  217. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
  218. return
  219. }
  220. if strings.Contains(name, "环比") {
  221. continue
  222. }
  223. //fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, name)
  224. unit = "头"
  225. if strings.Contains(name, "率") {
  226. unit = "%"
  227. }
  228. fullIndexName := fmt.Sprintf("%s/%s/%s", namePrefix, province, name)
  229. provincePingyin := utils.GetFullPingYin(province)
  230. namePingin := utils.GetFirstPingYin(name)
  231. fullIndexNamePingyin := namePrefixPingin + provincePingyin + namePingin
  232. if province == "五省合计数量" || province == "五省平均水平" {
  233. provincePingyin = utils.GetFirstPingYin(province)
  234. fullIndexNamePingyin = namePrefixPingin + provincePingyin + namePingin
  235. }
  236. indexItem, okIndex := indexMap[fullIndexName]
  237. if !okIndex {
  238. // 新增指标
  239. indexItem = new(models.YongyiExcelIndex)
  240. indexItem.IndexName = fullIndexName
  241. indexItem.ClassifyName = classifyName
  242. indexItem.ClassifySort = classifySort
  243. indexItem.IndexCode = fullIndexNamePingyin
  244. indexItem.Frequency = frequency
  245. indexItem.Sort = sort
  246. indexItem.Unit = unit
  247. indexItem.ExcelDataMap = make(map[string]string)
  248. sort++
  249. }
  250. //fmt.Printf("IndexCode: %s", indexItem.IndexCode)
  251. indexItem.ExcelDataMap[date] = text
  252. indexMap[fullIndexName] = indexItem
  253. continue
  254. }
  255. }
  256. }
  257. for _, v := range indexMap {
  258. indexList = append(indexList, v)
  259. }
  260. return
  261. }
  262. // HandleYongyiExcelWeekly33 月度-生产指标2
  263. func HandleYongyiExcelWeekly33(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  264. defer func() {
  265. if err != nil {
  266. //fmt.Printf("HandleYongyiExcelWeekly33 月度-生产指标2ErrMsg: %s\n", err.Error())
  267. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly33 月度-生产指标2 ErrMsg: %s", err.Error()))
  268. }
  269. }()
  270. rows, e := f.GetRows(sheetName)
  271. if e != nil {
  272. err = fmt.Errorf("f GetRows err: %s", e.Error())
  273. return
  274. }
  275. // 获取指标分类
  276. classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName)
  277. // 遍历行读取
  278. indexList = make([]*models.YongyiExcelIndex, 0)
  279. sort := 0
  280. nameMap := make(map[int]string)
  281. // 指标名称
  282. indexMap := make(map[string]*models.YongyiExcelIndex)
  283. for i, row := range rows {
  284. //fmt.Printf("当前第%d行 \n", i)
  285. if i == 0 {
  286. continue
  287. } else if i == 1 {
  288. for k, text := range row {
  289. if text != "" {
  290. text = strings.TrimSpace(text)
  291. nameMap[k] = text
  292. }
  293. }
  294. } else { //数据列
  295. date := ""
  296. for k, text := range row {
  297. //fmt.Printf("当前第%d列 \n", k)
  298. if k == 0 {
  299. text = strings.TrimSpace(text)
  300. var dateT time.Time
  301. dateT, e = time.ParseInLocation("2006年1月", text, time.Local)
  302. if e != nil {
  303. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e))
  304. continue
  305. }
  306. // 查询当月的最后一天
  307. monthDate := dateT.Format(utils.FormatYearMonthDate)
  308. firstDayStr := monthDate + "-01"
  309. tmpT, _ := time.ParseInLocation(utils.FormatDate, firstDayStr, time.Local)
  310. date = tmpT.AddDate(0, 1, -1).Format(utils.FormatDate)
  311. fmt.Println(date)
  312. continue
  313. } else {
  314. // 判断出不是字符的,则过滤
  315. if text == "" {
  316. continue
  317. }
  318. if strings.Contains(text, "%") {
  319. text = strings.Replace(text, "%", "", 1)
  320. }
  321. _, e := strconv.ParseFloat(text, 64)
  322. if e != nil {
  323. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e))
  324. continue
  325. }
  326. }
  327. name, ok2 := nameMap[k]
  328. if !ok2 {
  329. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
  330. return
  331. }
  332. //fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, name)
  333. fullIndexName := fmt.Sprintf("%s/%s", namePrefix, name)
  334. namePingin := utils.GetFirstPingYin(name)
  335. fullIndexNamePingyin := namePrefixPingin + namePingin
  336. unit = "头"
  337. if strings.Contains(name, "率") {
  338. unit = "%"
  339. }
  340. indexItem, okIndex := indexMap[fullIndexName]
  341. if !okIndex {
  342. // 新增指标
  343. indexItem = new(models.YongyiExcelIndex)
  344. indexItem.IndexName = fullIndexName
  345. indexItem.ClassifyName = classifyName
  346. indexItem.ClassifySort = classifySort
  347. indexItem.IndexCode = fullIndexNamePingyin
  348. indexItem.Frequency = frequency
  349. indexItem.Sort = sort
  350. indexItem.Unit = unit
  351. indexItem.ExcelDataMap = make(map[string]string)
  352. sort++
  353. }
  354. //fmt.Printf("IndexCode: %s", indexItem.IndexCode)
  355. indexItem.ExcelDataMap[date] = text
  356. indexMap[fullIndexName] = indexItem
  357. continue
  358. }
  359. }
  360. }
  361. for _, v := range indexMap {
  362. indexList = append(indexList, v)
  363. }
  364. return
  365. }
  366. // HandleYongyiExcelWeekly34 月度-二元三元能繁比例
  367. func HandleYongyiExcelWeekly34(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  368. defer func() {
  369. if err != nil {
  370. //fmt.Printf("HandleYongyiExcelWeekly34 月度-二元三元能繁比例ErrMsg: %s\n", err.Error())
  371. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly34 月度-二元三元能繁比例 ErrMsg: %s", err.Error()))
  372. }
  373. }()
  374. rows, e := f.GetRows(sheetName)
  375. if e != nil {
  376. err = fmt.Errorf("f GetRows err: %s", e.Error())
  377. return
  378. }
  379. // 获取指标分类
  380. classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName)
  381. // 遍历行读取
  382. indexList = make([]*models.YongyiExcelIndex, 0)
  383. sort := 0
  384. year := ""
  385. nameMap := make(map[int]string)
  386. // 指标名称
  387. indexMap := make(map[string]*models.YongyiExcelIndex)
  388. for i, row := range rows {
  389. //fmt.Printf("当前第%d行 \n", i)
  390. if i == 0 {
  391. for k, text := range row {
  392. if k > 3 && text != "" {
  393. text = strings.TrimSpace(text)
  394. nameMap[k] = text
  395. }
  396. }
  397. } else { //数据列
  398. date := ""
  399. for k, text := range row {
  400. //fmt.Printf("当前第%d列 \n", k)
  401. if k <= 3 || k > 8 {
  402. continue
  403. } else if k == 4 {
  404. text = strings.TrimSpace(text)
  405. var dateT time.Time
  406. //判断是否是数字,如果是数字,另外单独处理
  407. if dateFloat, e := strconv.ParseFloat(text, 64); e == nil {
  408. dateT, e = excelize.ExcelDateToTime(dateFloat, false)
  409. if e != nil {
  410. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e))
  411. continue
  412. }
  413. } else {
  414. dateSlice := strings.Split(text, "年")
  415. if len(dateSlice) >= 2 {
  416. year = dateSlice[0]
  417. } else {
  418. text = fmt.Sprintf("%s年%s", year, text)
  419. }
  420. fmt.Println(text)
  421. dateT, e = time.ParseInLocation("2006年1月", text, time.Local)
  422. if e != nil {
  423. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e))
  424. continue
  425. }
  426. }
  427. // 查询当月的最后一天
  428. monthDate := dateT.Format(utils.FormatYearMonthDate)
  429. firstDayStr := monthDate + "-01"
  430. tmpT, _ := time.ParseInLocation(utils.FormatDate, firstDayStr, time.Local)
  431. date = tmpT.AddDate(0, 1, -1).Format(utils.FormatDate)
  432. fmt.Println(date)
  433. continue
  434. } else {
  435. // 判断出不是字符的,则过滤
  436. if text == "" {
  437. continue
  438. }
  439. if strings.Contains(text, "%") {
  440. text = strings.Replace(text, "%", "", 1)
  441. }
  442. _, e := strconv.ParseFloat(text, 64)
  443. if e != nil {
  444. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e))
  445. continue
  446. }
  447. }
  448. if date == "" {
  449. continue
  450. }
  451. name, ok2 := nameMap[k]
  452. if !ok2 {
  453. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
  454. return
  455. }
  456. //fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, name)
  457. name += "比例"
  458. fullIndexName := fmt.Sprintf("%s/%s", namePrefix, name)
  459. namePingin := utils.GetFirstPingYin(name)
  460. fullIndexNamePingyin := namePrefixPingin + namePingin
  461. indexItem, okIndex := indexMap[fullIndexName]
  462. if !okIndex {
  463. // 新增指标
  464. indexItem = new(models.YongyiExcelIndex)
  465. indexItem.IndexName = fullIndexName
  466. indexItem.ClassifyName = classifyName
  467. indexItem.ClassifySort = classifySort
  468. indexItem.IndexCode = fullIndexNamePingyin
  469. indexItem.Frequency = frequency
  470. indexItem.Sort = sort
  471. indexItem.Unit = unit
  472. indexItem.ExcelDataMap = make(map[string]string)
  473. sort++
  474. }
  475. //fmt.Printf("IndexCode: %s", indexItem.IndexCode)
  476. indexItem.ExcelDataMap[date] = text
  477. indexMap[fullIndexName] = indexItem
  478. continue
  479. }
  480. }
  481. }
  482. for _, v := range indexMap {
  483. indexList = append(indexList, v)
  484. }
  485. return
  486. }
  487. // HandleYongyiExcelWeekly35 月度-能繁母猪存栏量
  488. func HandleYongyiExcelWeekly35(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  489. defer func() {
  490. if err != nil {
  491. //fmt.Printf("HandleYongyiExcelWeekly35 月度-能繁母猪存栏量 ErrMsg: %s\n", err.Error())
  492. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly35 月度-能繁母猪存栏量 ErrMsg: %s", err.Error()))
  493. }
  494. }()
  495. rows, e := f.GetRows(sheetName)
  496. if e != nil {
  497. err = fmt.Errorf("f GetRows err: %s", e.Error())
  498. return
  499. }
  500. // 获取指标分类
  501. classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName)
  502. // 遍历行读取
  503. indexList = make([]*models.YongyiExcelIndex, 0)
  504. sort := 0
  505. nameMap := make(map[int]string)
  506. // 指标名称
  507. indexMap := make(map[string]*models.YongyiExcelIndex)
  508. for i, row := range rows {
  509. //fmt.Printf("当前第%d行 \n", i)
  510. if i == 0 {
  511. continue
  512. } else if i == 1 {
  513. for k, text := range row {
  514. if text != "" {
  515. nameMap[k] = text
  516. }
  517. }
  518. } else { //数据列
  519. date := ""
  520. for k, text := range row {
  521. //fmt.Printf("当前第%d列 \n", k)
  522. if k == 0 {
  523. text = strings.TrimSpace(text)
  524. var dateT time.Time
  525. dateT, e = time.ParseInLocation("2006年1月", text, time.Local)
  526. if e != nil {
  527. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e))
  528. continue
  529. }
  530. // 查询当月的最后一天
  531. monthDate := dateT.Format(utils.FormatYearMonthDate)
  532. firstDayStr := monthDate + "-01"
  533. tmpT, _ := time.ParseInLocation(utils.FormatDate, firstDayStr, time.Local)
  534. date = tmpT.AddDate(0, 1, -1).Format(utils.FormatDate)
  535. fmt.Println(date)
  536. continue
  537. } else {
  538. // 判断出不是字符的,则过滤
  539. if text == "" {
  540. continue
  541. }
  542. if strings.Contains(text, "%") {
  543. text = strings.Replace(text, "%", "", 1)
  544. }
  545. _, e := strconv.ParseFloat(text, 64)
  546. if e != nil {
  547. //utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e))
  548. continue
  549. }
  550. }
  551. name, ok2 := nameMap[k]
  552. if !ok2 {
  553. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
  554. return
  555. }
  556. if strings.Contains(name, "环比") || strings.Contains(name, "同比") || strings.Contains(name, "日期") {
  557. continue
  558. }
  559. //fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, name)
  560. fullIndexName := fmt.Sprintf("%s/%s", namePrefix, name)
  561. namePingin := utils.GetFirstPingYin(name)
  562. fullIndexNamePingyin := namePrefixPingin + namePingin
  563. unit = "头"
  564. if name == "较非瘟前" {
  565. area, ok3 := nameMap[k-3]
  566. if !ok3 {
  567. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k-3)
  568. return
  569. }
  570. fullIndexName = fmt.Sprintf("%s/%s%s", namePrefix, area, name)
  571. namePingin = utils.GetFirstPingYin(area + name)
  572. fullIndexNamePingyin = namePrefixPingin + namePingin
  573. unit = "%"
  574. }
  575. indexItem, okIndex := indexMap[fullIndexName]
  576. if !okIndex {
  577. // 新增指标
  578. indexItem = new(models.YongyiExcelIndex)
  579. indexItem.IndexName = fullIndexName
  580. indexItem.ClassifyName = classifyName
  581. indexItem.ClassifySort = classifySort
  582. indexItem.IndexCode = fullIndexNamePingyin
  583. indexItem.Frequency = frequency
  584. indexItem.Sort = sort
  585. indexItem.Unit = unit
  586. indexItem.ExcelDataMap = make(map[string]string)
  587. sort++
  588. }
  589. //fmt.Printf("IndexCode: %s", indexItem.IndexCode)
  590. indexItem.ExcelDataMap[date] = text
  591. indexMap[fullIndexName] = indexItem
  592. continue
  593. }
  594. }
  595. }
  596. for _, v := range indexMap {
  597. indexList = append(indexList, v)
  598. }
  599. return
  600. }
  601. // HandleYongyiExcelWeekly37 周度-养殖利润
  602. func HandleYongyiExcelWeekly37(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  603. defer func() {
  604. if err != nil {
  605. //fmt.Printf("HandleYongyiExcelWeekly35 月度-能繁母猪存栏量 ErrMsg: %s\n", err.Error())
  606. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly37 周度-养殖利润 ErrMsg: %s", err.Error()))
  607. }
  608. }()
  609. col, e := f.GetCols(sheetName)
  610. if e != nil {
  611. err = fmt.Errorf("f GetCols err: %s", e.Error())
  612. return
  613. }
  614. yearReg := regexp.MustCompile(`^\d{4}年$`)
  615. // 获取指标信息
  616. classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName)
  617. // GetBaseInfo(sheetName)
  618. indexList = make([]*models.YongyiExcelIndex, 0)
  619. indexInfoMap := make(map[string]*models.YongyiExcelIndex)
  620. var indexName string
  621. var weekIndexMap = make(map[int]int)
  622. var sort = 0
  623. for i, c := range col {
  624. if len(c) < 2 {
  625. continue
  626. }
  627. if i == 0 {
  628. // 记录周度索引
  629. for j, v := range c {
  630. vInt, e := strconv.Atoi(v)
  631. if e != nil {
  632. continue
  633. }
  634. weekIndexMap[j] = vInt
  635. }
  636. }
  637. if strings.TrimSpace(c[1]) != "" {
  638. indexName = strings.TrimSpace(c[1])
  639. }
  640. if indexName == "" {
  641. continue
  642. }
  643. if len(c) < 4 {
  644. continue
  645. }
  646. // 匹配 2021年 的格式
  647. if ok := yearReg.MatchString(c[2]); !ok {
  648. continue
  649. }
  650. yearStr := c[2][:4]
  651. year, e := strconv.Atoi(yearStr)
  652. if e != nil {
  653. continue
  654. }
  655. if indexInfo, ok := indexInfoMap[indexName]; !ok {
  656. tmpYongyiIndex := new(models.YongyiExcelIndex)
  657. tmpYongyiIndex.IndexName = namePrefix + "/" + indexName
  658. tmpYongyiIndex.ClassifyName = classifyName
  659. tmpYongyiIndex.ClassifySort = classifySort
  660. tmpYongyiIndex.IndexCode = namePrefixPingin + utils.GetFirstPingYin(indexName)
  661. tmpYongyiIndex.Frequency = frequency
  662. tmpYongyiIndex.Unit = unit
  663. tmpYongyiIndex.Sort = sort
  664. tmpYongyiIndex.ExcelDataMap = make(map[string]string)
  665. for j, v := range c[3:] {
  666. dataTime := getSundayOfWeek(year, weekIndexMap[j+3])
  667. if isValueValid(v) {
  668. tmpYongyiIndex.ExcelDataMap[dataTime] = v
  669. }
  670. }
  671. indexInfoMap[indexName] = tmpYongyiIndex
  672. indexList = append(indexList, tmpYongyiIndex)
  673. sort++
  674. } else {
  675. for j, v := range c[3:] {
  676. dataTime := getSundayOfWeek(year, weekIndexMap[j+3])
  677. if isValueValid(v) {
  678. indexInfo.ExcelDataMap[dataTime] = v
  679. }
  680. }
  681. }
  682. }
  683. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly37 周度-养殖利润 指标列表:%v", indexList))
  684. return
  685. }
  686. // HandleYongyiExcelWeekly38 华东冻品价格
  687. func HandleYongyiExcelWeekly38(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  688. defer func() {
  689. if err != nil {
  690. //fmt.Printf("HandleYongyiExcelWeekly35 月度-能繁母猪存栏量 ErrMsg: %s\n", err.Error())
  691. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly37 周度-养殖利润 ErrMsg: %s", err.Error()))
  692. }
  693. }()
  694. rows, e := f.GetRows(sheetName)
  695. if e != nil {
  696. err = fmt.Errorf("f GetRows err: %s", e.Error())
  697. return
  698. }
  699. classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName)
  700. if len(rows) < 2 {
  701. err = fmt.Errorf("sheet %s 行数小于2", sheetName)
  702. return
  703. }
  704. mergeCellMap, e := getMergeCellValMap(f, sheetName)
  705. if e != nil {
  706. err = fmt.Errorf("getMergeCellValMap err: %s", e.Error())
  707. return
  708. }
  709. sort := 0
  710. var indexNamePart1, indexNamePart2 string
  711. indexNamePart3 := make(map[int]string)
  712. indexInfoMap := make(map[string]*models.YongyiExcelIndex)
  713. dateTimeIndexMap := make(map[int]string)
  714. maxLen := 0
  715. for _, row := range rows {
  716. if len(row) > maxLen {
  717. maxLen = len(row)
  718. }
  719. }
  720. for k, v := range mergeCellMap {
  721. axis := strings.Split(k, ":")
  722. if len(axis) != 2 {
  723. continue
  724. }
  725. x, y := axis[0], axis[1]
  726. xInt, e := strconv.Atoi(x)
  727. if e != nil {
  728. continue
  729. }
  730. yInt, e := strconv.Atoi(y)
  731. if e != nil {
  732. continue
  733. }
  734. tmpList := make([]string, maxLen)
  735. copy(tmpList, rows[yInt-1])
  736. tmpList[xInt-1] = v
  737. rows[yInt-1] = tmpList
  738. }
  739. for i, row := range rows {
  740. if i == 0 {
  741. continue
  742. }
  743. // 日期列
  744. if i == 1 {
  745. for j, cell := range row {
  746. if cell == "" {
  747. continue
  748. }
  749. dateInt, err := strconv.Atoi(cell)
  750. if err != nil {
  751. continue
  752. }
  753. dateTimeIndexMap[j] = excelDateToTimeDateStr(dateInt)
  754. }
  755. }
  756. // 指标名称列3
  757. if i == 2 {
  758. for j, cell := range row[2:] {
  759. if cell == "" {
  760. continue
  761. }
  762. indexNamePart3[j] = cell
  763. }
  764. }
  765. if i > 2 {
  766. for j, cell := range row {
  767. if j == 0 && cell != "" {
  768. indexNamePart1 = row[0]
  769. }
  770. if j == 1 && cell != "" {
  771. indexNamePart2 = row[1]
  772. }
  773. if indexNamePart1 == "" || indexNamePart2 == "" {
  774. continue
  775. }
  776. namePart3, ok := indexNamePart3[j-2]
  777. if !ok {
  778. continue
  779. }
  780. indexName := fmt.Sprintf("%s/%s/%s", indexNamePart1, indexNamePart2, namePart3)
  781. if indexInfo, ok := indexInfoMap[indexName]; !ok {
  782. tmpYongyiIndex := new(models.YongyiExcelIndex)
  783. tmpYongyiIndex.IndexName = namePrefix + "/" + indexName
  784. tmpYongyiIndex.ClassifyName = classifyName
  785. tmpYongyiIndex.ClassifySort = classifySort
  786. tmpIndexName := strings.ReplaceAll(indexName, "/", "")
  787. tmpYongyiIndex.IndexCode = namePrefixPingin + utils.GetFirstPingYin(tmpIndexName)
  788. tmpYongyiIndex.Frequency = frequency
  789. tmpYongyiIndex.Unit = unit
  790. tmpYongyiIndex.Sort = sort
  791. tmpYongyiIndex.ExcelDataMap = make(map[string]string)
  792. val, ok := getCellValue(cell)
  793. if !ok {
  794. continue
  795. }
  796. tmpYongyiIndex.ExcelDataMap[dateTimeIndexMap[j]] = val
  797. indexInfoMap[indexName] = tmpYongyiIndex
  798. indexList = append(indexList, tmpYongyiIndex)
  799. sort++
  800. } else {
  801. val, ok := getCellValue(cell)
  802. if !ok {
  803. continue
  804. }
  805. indexInfo.ExcelDataMap[dateTimeIndexMap[j]] = val
  806. }
  807. }
  808. }
  809. }
  810. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly38 华东冻品价格 更新指标数量:%d", len(indexList)))
  811. return
  812. }
  813. // HandleYongyiExcelWeekly39 运费
  814. func HandleYongyiExcelWeekly39(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  815. defer func() {
  816. if err != nil {
  817. //fmt.Printf("HandleYongyiExcelWeekly35 月度-能繁母猪存栏量 ErrMsg: %s\n", err.Error())
  818. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly39 月度-能繁母猪存栏量 ErrMsg: %s", err.Error()))
  819. }
  820. }()
  821. rows, e := f.GetRows(sheetName)
  822. if e != nil {
  823. err = fmt.Errorf("f GetRows err: %s", e.Error())
  824. return
  825. }
  826. classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName)
  827. if len(rows) < 2 {
  828. err = fmt.Errorf("sheet %s 行数小于2", sheetName)
  829. return
  830. }
  831. var dataTime string
  832. var sort int
  833. indexInfoMap := make(map[string]*models.YongyiExcelIndex)
  834. for i, row := range rows {
  835. if i <= 1 {
  836. continue
  837. }
  838. if len(row) <= 3 {
  839. continue
  840. }
  841. tmpDate, err := time.Parse("01-02-06", row[2])
  842. if err == nil {
  843. dataTime = tmpDate.Format(utils.FormatDate)
  844. }
  845. var fromSrc, toDst string
  846. if len([]rune(row[0])) == 2 && len([]rune(row[1])) == 2 {
  847. fromSrc = row[0]
  848. toDst = row[1]
  849. }
  850. if fromSrc == "" || toDst == "" {
  851. continue
  852. }
  853. indexName := fmt.Sprintf("%s/%s至%s", namePrefix, fromSrc, toDst)
  854. if indexInfo, ok := indexInfoMap[indexName]; !ok {
  855. tmpYongyiIndex := new(models.YongyiExcelIndex)
  856. tmpYongyiIndex.IndexName = indexName
  857. tmpYongyiIndex.ClassifyName = classifyName
  858. tmpYongyiIndex.ClassifySort = classifySort
  859. tmpIndexNameSuffix := fmt.Sprintf("%s至%s", fromSrc, toDst)
  860. tmpYongyiIndex.IndexCode = namePrefixPingin + utils.GetFirstPingYin(tmpIndexNameSuffix)
  861. tmpYongyiIndex.Frequency = frequency
  862. tmpYongyiIndex.Unit = unit
  863. tmpYongyiIndex.Sort = sort
  864. tmpYongyiIndex.ExcelDataMap = make(map[string]string)
  865. val, ok := getCellValue(row[2])
  866. if !ok {
  867. continue
  868. }
  869. tmpYongyiIndex.ExcelDataMap[dataTime] = val
  870. indexInfoMap[indexName] = tmpYongyiIndex
  871. indexList = append(indexList, tmpYongyiIndex)
  872. sort++
  873. } else {
  874. val, ok := getCellValue(row[2])
  875. if !ok {
  876. continue
  877. }
  878. indexInfo.ExcelDataMap[dataTime] = val
  879. }
  880. }
  881. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly39 运费 更新指标数量:%d", len(indexList)))
  882. return
  883. }
  884. // HandleYongyiExcelWeekly40 国产冻品2-4号肉价格
  885. func HandleYongyiExcelWeekly40(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  886. defer func() {
  887. if err != nil {
  888. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly40 国产冻品2-4号肉价格 ErrMsg: %s", err.Error()))
  889. }
  890. }()
  891. rows, err := f.GetRows(sheetName)
  892. if err != nil {
  893. err = fmt.Errorf("f GetRows err: %s", err.Error())
  894. return
  895. }
  896. className, classSort, frequency, unit, _, namePrefixPingin := GetBaseInfo(sheetName)
  897. var indexName string
  898. var sort int
  899. re := regexp.MustCompile(`[+-]?[0-9]*\.?[0-9]+`)
  900. indexList = make([]*models.YongyiExcelIndex, 0)
  901. monthIndexMap := make(map[int]int)
  902. indexInfoMap := make(map[string]*models.YongyiExcelIndex)
  903. for _, row := range rows {
  904. if len(row) == 0 {
  905. indexName = ""
  906. continue
  907. }
  908. if strings.Contains(row[0], "冻猪") {
  909. indexName = strings.TrimSpace(row[0])
  910. }
  911. if indexName == "" {
  912. continue
  913. }
  914. var year int
  915. for i, cell := range row {
  916. if i == 0 && strings.Contains(cell, "年销价") {
  917. if tmp := re.FindString(cell); tmp != "" {
  918. year, err = strconv.Atoi("20" + tmp)
  919. if err != nil {
  920. continue
  921. }
  922. }
  923. } else if strings.Contains(cell, "月") {
  924. if month := re.FindString(cell); month != "" {
  925. mo, err := strconv.Atoi(month)
  926. if err != nil {
  927. continue
  928. }
  929. monthIndexMap[i] = mo
  930. }
  931. }
  932. if re.MatchString(cell) {
  933. if indexInfo, ok := indexInfoMap[indexName]; !ok {
  934. if isValueValid(cell) {
  935. tmpYongyiIndex := new(models.YongyiExcelIndex)
  936. tmpYongyiIndex.IndexName = indexName
  937. tmpYongyiIndex.ClassifyName = className
  938. tmpYongyiIndex.ClassifySort = classSort
  939. tmpYongyiIndex.IndexCode = namePrefixPingin + utils.GetFirstPingYin(indexName)
  940. tmpYongyiIndex.Frequency = frequency
  941. tmpYongyiIndex.Unit = unit
  942. tmpYongyiIndex.Sort = sort
  943. tmpYongyiIndex.ExcelDataMap = make(map[string]string)
  944. date := LastDayOfMonth(year, time.Month(monthIndexMap[i]))
  945. tmpYongyiIndex.ExcelDataMap[date] = cell
  946. indexInfoMap[indexName] = tmpYongyiIndex
  947. indexList = append(indexList, tmpYongyiIndex)
  948. sort++
  949. }
  950. } else {
  951. if isValueValid(cell) {
  952. date := LastDayOfMonth(year, time.Month(monthIndexMap[i]))
  953. indexInfo.ExcelDataMap[date] = cell
  954. }
  955. }
  956. }
  957. }
  958. }
  959. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly40 国产冻品2-4号肉价格 更新指标数量:%d", len(indexList)))
  960. return
  961. }