daily.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package base_from_yongyi
  2. import (
  3. "eta/eta_data_analysis/models"
  4. "eta/eta_data_analysis/utils"
  5. "fmt"
  6. "github.com/tealeg/xlsx"
  7. "strings"
  8. "time"
  9. )
  10. // HandleYongyiExcelDaily1 日度-商品猪出栏价
  11. func HandleYongyiExcelDaily1(sheet *xlsx.Sheet) (indexList []*models.YongyiExcelIndex, err error) {
  12. classifyName := "日度-商品猪出栏价"
  13. frequency := "日度"
  14. unit := "元/公斤"
  15. namePrefix := "商品猪出栏价"
  16. namePrefixPingin := "yyzxspzclj"
  17. //sheet := dailyPriceSheet
  18. // 遍历行读取
  19. indexList = make([]*models.YongyiExcelIndex, 0)
  20. dateMap := make(map[int]string)
  21. nameMap := make(map[int]string)
  22. maxRow := sheet.MaxRow
  23. fmt.Println("最大行")
  24. fmt.Println(maxRow)
  25. // 指标名称
  26. indexMap := make(map[string]*models.YongyiExcelIndex)
  27. for i := 0; i < maxRow; i++ {
  28. fmt.Printf("当前第%d行 \n", i)
  29. if i == 0 { // 首行,表示时间
  30. row := sheet.Row(i)
  31. cells := row.Cells
  32. for k, cell := range cells {
  33. text := cell.String()
  34. if k > 1 && text != "" {
  35. if cell.IsTime() {
  36. dateText, _ := cell.GetTime(false)
  37. text = dateText.Format(utils.FormatDate)
  38. }
  39. // 检查单元格是否为合并单元格
  40. if cell.HMerge > 0 {
  41. for j := 1; j <= cell.HMerge; j++ {
  42. dateMap[k+j] = text
  43. }
  44. }
  45. fmt.Printf("合并单元格开始列:%d \n", k)
  46. dateMap[k] = text
  47. }
  48. }
  49. } else if i == 1 { //表示表头
  50. // 处理 index指标表
  51. row := sheet.Row(i)
  52. cells := row.Cells
  53. for k, cell := range cells {
  54. text := cell.String()
  55. nameMap[k] = text
  56. }
  57. } else { //数据列
  58. row := sheet.Row(i)
  59. cells := row.Cells
  60. province := ""
  61. for k, cell := range cells {
  62. fmt.Printf("当前第%d列 \n", k)
  63. text := cell.String()
  64. if k == 0 {
  65. province = text
  66. continue
  67. } else if k == 1 {
  68. continue
  69. }
  70. date, ok1 := dateMap[k]
  71. if !ok1 {
  72. err = fmt.Errorf("找不到对应的日期,第%d行,第%d列", i, k)
  73. return
  74. }
  75. name, ok2 := nameMap[k]
  76. if !ok2 {
  77. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
  78. return
  79. }
  80. fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, province)
  81. // 处理指标名称
  82. fullIndexName := fmt.Sprintf("%s/%s/%s", namePrefix, province, name)
  83. if name != "规模场" && name != "小散户" && name != "均价" {
  84. // 只处理以上三个类型,其余过滤
  85. continue
  86. }
  87. if fullIndexName == "商品猪出栏价/全国/均价" {
  88. continue
  89. }
  90. indexItem, okIndex := indexMap[fullIndexName]
  91. provincePingyin := utils.GetFullPingYin(province)
  92. namePingin := utils.GetFirstPingYin(name)
  93. fullIndexNamePingyin := fmt.Sprintf("%s%s%s", namePrefixPingin, provincePingyin, namePingin)
  94. if !okIndex {
  95. // 新增指标
  96. indexItem = new(models.YongyiExcelIndex)
  97. indexItem.IndexName = fullIndexName
  98. indexItem.ClassifyName = classifyName
  99. indexItem.IndexCode = fullIndexNamePingyin
  100. indexItem.Frequency = frequency
  101. // todo 排序
  102. indexItem.Sort = i + k
  103. indexItem.Unit = unit
  104. indexItem.ExcelDataMap = make(map[string]string)
  105. }
  106. fmt.Printf("indexItem%s", indexItem.IndexCode)
  107. indexItem.ExcelDataMap[date] = text
  108. indexMap[fullIndexName] = indexItem
  109. continue
  110. }
  111. }
  112. }
  113. for _, v := range indexMap {
  114. indexList = append(indexList, v)
  115. }
  116. return
  117. }
  118. // HandleYongyiExcelDaily2 日度-商品猪标肥价差
  119. func HandleYongyiExcelDaily2(sheet *xlsx.Sheet) (indexList []*models.YongyiExcelIndex, err error) {
  120. classifyName := "日度-商品猪标肥价差"
  121. frequency := "日度"
  122. unit := "元/公斤"
  123. pingYinPrefix := "yyzx"
  124. // 遍历行读取
  125. indexList = make([]*models.YongyiExcelIndex, 0)
  126. dateMap := make(map[int]string)
  127. nameMap := make(map[int]string)
  128. maxRow := sheet.MaxRow
  129. fmt.Println("最大行")
  130. fmt.Println(maxRow)
  131. // 指标名称
  132. indexMap := make(map[string]*models.YongyiExcelIndex)
  133. for i := 0; i < maxRow; i++ {
  134. fmt.Printf("当前第%d行 \n", i)
  135. if i == 0 { // 首行,表示时间
  136. row := sheet.Row(i)
  137. cells := row.Cells
  138. for k, cell := range cells {
  139. text := cell.String()
  140. if text != "" {
  141. if cell.IsTime() {
  142. dateText, _ := cell.GetTime(false)
  143. text = dateText.Format(utils.FormatDate)
  144. }
  145. // 检查单元格是否为合并单元格
  146. if cell.HMerge > 0 {
  147. for j := 1; j <= cell.HMerge; j++ {
  148. dateMap[k+j] = text
  149. }
  150. }
  151. fmt.Printf("合并单元格开始列:%d \n", k)
  152. dateMap[k] = text
  153. }
  154. }
  155. } else if i == 1 { //表示表头
  156. // 处理 index指标表
  157. row := sheet.Row(i)
  158. cells := row.Cells
  159. for k, cell := range cells {
  160. text := cell.String()
  161. nameMap[k] = text
  162. }
  163. } else { //数据列
  164. row := sheet.Row(i)
  165. cells := row.Cells
  166. province := ""
  167. for k, cell := range cells {
  168. fmt.Printf("当前第%d列 \n", k)
  169. text := cell.String()
  170. if k == 0 {
  171. province = text
  172. continue
  173. }
  174. date, ok1 := dateMap[k]
  175. if !ok1 {
  176. err = fmt.Errorf("找不到对应的日期,第%d行,第%d列", i, k)
  177. return
  178. }
  179. name, ok2 := nameMap[k]
  180. if !ok2 {
  181. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
  182. return
  183. }
  184. fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, province)
  185. // 处理指标名称
  186. // 过滤特殊的指标 放到对应的指标名称的下方
  187. if name != "市场标重猪价" && name != "150公斤左右较标猪" && name != "175公斤左右较标猪" {
  188. // 只处理以上三个类型,其余过滤
  189. continue
  190. }
  191. var namePrefixPingin string
  192. if name == "150公斤左右较标猪" {
  193. name = "150公斤较标猪价差"
  194. namePrefixPingin = "150gjjbzjc"
  195. } else if name == "175公斤左右较标猪" {
  196. name = "175公斤较标猪价差"
  197. namePrefixPingin = "175gjjbzjc"
  198. } else if name == "市场标重猪价" {
  199. namePrefixPingin = "scbzzj"
  200. }
  201. fullIndexName := fmt.Sprintf("%s/%s", name, province)
  202. indexItem, okIndex := indexMap[fullIndexName]
  203. provincePingyin := utils.GetFullPingYin(province)
  204. fullIndexNamePingyin := fmt.Sprintf("%s%s%s", pingYinPrefix, namePrefixPingin, provincePingyin)
  205. if !okIndex {
  206. // 新增指标
  207. indexItem = new(models.YongyiExcelIndex)
  208. indexItem.IndexName = fullIndexName
  209. indexItem.ClassifyName = classifyName
  210. indexItem.IndexCode = fullIndexNamePingyin
  211. indexItem.Frequency = frequency
  212. // todo 排序
  213. indexItem.Sort = i + k
  214. indexItem.Unit = unit
  215. indexItem.ExcelDataMap = make(map[string]string)
  216. }
  217. fmt.Printf("indexItem%s", indexItem.IndexCode)
  218. indexItem.ExcelDataMap[date] = text
  219. indexMap[fullIndexName] = indexItem
  220. continue
  221. }
  222. }
  223. }
  224. for _, v := range indexMap {
  225. indexList = append(indexList, v)
  226. }
  227. return
  228. }
  229. // HandleYongyiExcelDaily3 日度-商品猪全国均价和宰量
  230. func HandleYongyiExcelDaily3(sheet *xlsx.Sheet) (indexList []*models.YongyiExcelIndex, err error) {
  231. classifyName := "日度-商品猪全国均价和宰量"
  232. frequency := "日度"
  233. unit := ""
  234. // 遍历行读取
  235. indexList = make([]*models.YongyiExcelIndex, 0)
  236. nameMap := make(map[int]string)
  237. maxRow := sheet.MaxRow
  238. fmt.Println("最大行")
  239. fmt.Println(maxRow)
  240. // 指标名称
  241. indexMap := make(map[string]*models.YongyiExcelIndex)
  242. for i := 0; i < maxRow; i++ {
  243. fmt.Printf("当前第%d行 \n", i)
  244. if i == 0 { // 首行,名称
  245. row := sheet.Row(i)
  246. cells := row.Cells
  247. for k, cell := range cells {
  248. text := cell.String()
  249. nameMap[k] = text
  250. }
  251. } else { //数据列
  252. row := sheet.Row(i)
  253. cells := row.Cells
  254. date := ""
  255. for k, cell := range cells {
  256. fmt.Printf("当前第%d列 \n", k)
  257. text := cell.String()
  258. if k == 0 {
  259. if cell.IsTime() {
  260. dateText, _ := cell.GetTime(false)
  261. text = dateText.Format(utils.FormatDate)
  262. }
  263. date = text
  264. continue
  265. }
  266. name, ok2 := nameMap[k]
  267. if !ok2 {
  268. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
  269. return
  270. }
  271. if name != "全国均价" && name != "日屠宰量" {
  272. // 只处理以上三个类型,其余过滤
  273. continue
  274. }
  275. var fullIndexNamePingyin string
  276. if name == "全国均价" {
  277. name = "商品猪出拦价/全国/均价"
  278. fullIndexNamePingyin = "yyzxspzcljquanguojj"
  279. unit = "元/公斤"
  280. } else if name == "日屠宰量" {
  281. name = "商品猪日屠宰量/全国"
  282. fullIndexNamePingyin = "yyzxspzrtzlquanguo"
  283. unit = "头/日"
  284. }
  285. fullIndexName := name
  286. indexItem, okIndex := indexMap[fullIndexName]
  287. if !okIndex {
  288. // 新增指标
  289. indexItem = new(models.YongyiExcelIndex)
  290. indexItem.IndexName = fullIndexName
  291. indexItem.ClassifyName = classifyName
  292. indexItem.IndexCode = fullIndexNamePingyin
  293. indexItem.Frequency = frequency
  294. indexItem.Sort = k
  295. indexItem.Unit = unit
  296. indexItem.ExcelDataMap = make(map[string]string)
  297. }
  298. fmt.Printf("indexItem%s", indexItem.IndexCode)
  299. indexItem.ExcelDataMap[date] = text
  300. indexMap[fullIndexName] = indexItem
  301. }
  302. }
  303. }
  304. for _, v := range indexMap {
  305. indexList = append(indexList, v)
  306. }
  307. return
  308. }
  309. // HandleYongyiExcelDaily4 日度-屠宰企业屠宰量
  310. func HandleYongyiExcelDaily4(sheet *xlsx.Sheet) (indexList []*models.YongyiExcelIndex, err error) {
  311. classifyName := "日度-屠宰企业屠宰量"
  312. frequency := "日度"
  313. unit := "头/日"
  314. namePrefix := "商品猪日屠宰量"
  315. pingYinPrefix := "yyzx"
  316. namePrefixPingin := "spzrtzl"
  317. // 遍历行读取
  318. indexList = make([]*models.YongyiExcelIndex, 0)
  319. dateMap := make(map[int]string)
  320. maxRow := sheet.MaxRow
  321. fmt.Println("最大行")
  322. fmt.Println(maxRow)
  323. // 指标名称
  324. indexMap := make(map[string]*models.YongyiExcelIndex)
  325. for i := 0; i < maxRow; i++ {
  326. fmt.Printf("当前第%d行 \n", i)
  327. if i == 0 { // 首行,表示时间
  328. row := sheet.Row(i)
  329. cells := row.Cells
  330. for k, cell := range cells {
  331. text := cell.String()
  332. if text != "" && text != "省份" {
  333. if cell.IsTime() {
  334. dateText, _ := cell.GetTime(false)
  335. text = dateText.Format(utils.FormatDate)
  336. } else {
  337. if strings.Contains(text, "(") {
  338. tmp := strings.Split(text, "(")
  339. text = tmp[0]
  340. }
  341. timeTmp, tErr := time.ParseInLocation(utils.FormatDate1, text, time.Local)
  342. if tErr != nil {
  343. err = fmt.Errorf("时间格式转换失败,Err:%s", tErr)
  344. return
  345. }
  346. text = timeTmp.Format(utils.FormatDate)
  347. }
  348. dateMap[k] = text
  349. }
  350. }
  351. } else { //数据列
  352. row := sheet.Row(i)
  353. cells := row.Cells
  354. province := ""
  355. for k, cell := range cells {
  356. fmt.Printf("当前第%d列 \n", k)
  357. text := cell.String()
  358. if k == 0 {
  359. province = text
  360. continue
  361. }
  362. date, ok1 := dateMap[k]
  363. if !ok1 {
  364. err = fmt.Errorf("找不到对应的日期,第%d行,第%d列", i, k)
  365. return
  366. }
  367. fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, province)
  368. // 处理指标名称
  369. // 过滤特殊的指标 放到对应的指标名称的下方
  370. fullIndexName := fmt.Sprintf("%s/%s", namePrefix, province)
  371. provincePingyin := utils.GetFullPingYin(province)
  372. fullIndexNamePingyin := fmt.Sprintf("%s%s%s", pingYinPrefix, namePrefixPingin, provincePingyin)
  373. indexItem, okIndex := indexMap[fullIndexName]
  374. if !okIndex {
  375. // 新增指标
  376. indexItem = new(models.YongyiExcelIndex)
  377. indexItem.IndexName = fullIndexName
  378. indexItem.ClassifyName = classifyName
  379. indexItem.IndexCode = fullIndexNamePingyin
  380. indexItem.Frequency = frequency
  381. indexItem.Sort = i
  382. indexItem.Unit = unit
  383. indexItem.ExcelDataMap = make(map[string]string)
  384. }
  385. fmt.Printf("indexItem%s", indexItem.IndexCode)
  386. indexItem.ExcelDataMap[date] = text
  387. indexMap[fullIndexName] = indexItem
  388. }
  389. }
  390. }
  391. for _, v := range indexMap {
  392. indexList = append(indexList, v)
  393. }
  394. return
  395. }