daily.go 13 KB

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