daily.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. //sheet := dailyPriceSheet
  32. // 遍历行读取
  33. indexList = make([]*models.YongyiExcelIndex, 0)
  34. dateMap := make(map[int]string)
  35. nameMap := make(map[int]string)
  36. maxRow := sheet.MaxRow
  37. fmt.Println("最大行")
  38. fmt.Println(maxRow)
  39. // 指标名称
  40. indexMap := make(map[string]*models.YongyiExcelIndex)
  41. for i := 0; i < maxRow; i++ {
  42. fmt.Printf("当前第%d行 \n", i)
  43. if i == 0 { // 首行,表示时间
  44. row := sheet.Row(i)
  45. cells := row.Cells
  46. for k, cell := range cells {
  47. text := cell.String()
  48. if k > 1 && text != "" {
  49. if cell.IsTime() {
  50. dateText, _ := cell.GetTime(false)
  51. text = dateText.Format(utils.FormatDate)
  52. }
  53. // 检查单元格是否为合并单元格
  54. if cell.HMerge > 0 {
  55. for j := 1; j <= cell.HMerge; j++ {
  56. dateMap[k+j] = text
  57. }
  58. }
  59. fmt.Printf("合并单元格开始列:%d \n", k)
  60. dateMap[k] = text
  61. }
  62. }
  63. } else if i == 1 { //表示表头
  64. // 处理 index指标表
  65. row := sheet.Row(i)
  66. cells := row.Cells
  67. for k, cell := range cells {
  68. text := cell.String()
  69. nameMap[k] = text
  70. }
  71. } else { //数据列
  72. row := sheet.Row(i)
  73. cells := row.Cells
  74. province := ""
  75. for k, cell := range cells {
  76. fmt.Printf("当前第%d列 \n", k)
  77. text := cell.String()
  78. if k == 0 {
  79. province = text
  80. continue
  81. } else if k == 1 {
  82. continue
  83. }
  84. date, ok1 := dateMap[k]
  85. if !ok1 {
  86. err = fmt.Errorf("找不到对应的日期,第%d行,第%d列", i, k)
  87. return
  88. }
  89. name, ok2 := nameMap[k]
  90. if !ok2 {
  91. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
  92. return
  93. }
  94. fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, province)
  95. // 处理指标名称
  96. fullIndexName := fmt.Sprintf("%s/%s/%s", namePrefix, province, name)
  97. if name != "规模场" && name != "小散户" && name != "均价" {
  98. // 只处理以上三个类型,其余过滤
  99. continue
  100. }
  101. if fullIndexName == "商品猪出栏价/全国/均价" {
  102. continue
  103. }
  104. indexItem, okIndex := indexMap[fullIndexName]
  105. if !okIndex {
  106. // 新增指标
  107. indexItem = new(models.YongyiExcelIndex)
  108. indexItem.IndexName = fullIndexName
  109. indexItem.ClassifyName = classifyName
  110. // todo 处理indexCode
  111. indexItem.IndexCode = fullIndexName
  112. indexItem.Frequency = frequency
  113. indexItem.Unit = unit
  114. indexItem.ExcelDataMap = make(map[string]string)
  115. }
  116. fmt.Printf("indexItem%s", indexItem.IndexCode)
  117. indexItem.ExcelDataMap[date] = text
  118. indexMap[fullIndexName] = indexItem
  119. continue
  120. }
  121. }
  122. }
  123. for _, v := range indexMap {
  124. indexList = append(indexList, v)
  125. }
  126. // todo 整理请求入参,并加入,改成一个指标请求一次还是多个指标请求一次
  127. return
  128. }
  129. // HandleYongyiExcelDaily2 日度-商品猪标肥价差
  130. func HandleYongyiExcelDaily2(sheet *xlsx.Sheet) (indexList []*models.YongyiExcelIndex, err error) {
  131. classifyName := "日度-商品猪标肥价差"
  132. frequency := "日度"
  133. unit := "元/公斤"
  134. // 遍历行读取
  135. indexList = make([]*models.YongyiExcelIndex, 0)
  136. dateMap := make(map[int]string)
  137. nameMap := make(map[int]string)
  138. maxRow := sheet.MaxRow
  139. fmt.Println("最大行")
  140. fmt.Println(maxRow)
  141. // 指标名称
  142. indexMap := make(map[string]*models.YongyiExcelIndex)
  143. for i := 0; i < maxRow; i++ {
  144. fmt.Printf("当前第%d行 \n", i)
  145. if i == 0 { // 首行,表示时间
  146. row := sheet.Row(i)
  147. cells := row.Cells
  148. for k, cell := range cells {
  149. text := cell.String()
  150. if text != "" {
  151. if cell.IsTime() {
  152. dateText, _ := cell.GetTime(false)
  153. text = dateText.Format(utils.FormatDate)
  154. }
  155. // 检查单元格是否为合并单元格
  156. if cell.HMerge > 0 {
  157. for j := 1; j <= cell.HMerge; j++ {
  158. dateMap[k+j] = text
  159. }
  160. }
  161. fmt.Printf("合并单元格开始列:%d \n", k)
  162. dateMap[k] = text
  163. }
  164. }
  165. } else if i == 1 { //表示表头
  166. // 处理 index指标表
  167. row := sheet.Row(i)
  168. cells := row.Cells
  169. for k, cell := range cells {
  170. text := cell.String()
  171. nameMap[k] = text
  172. }
  173. } else { //数据列
  174. row := sheet.Row(i)
  175. cells := row.Cells
  176. province := ""
  177. for k, cell := range cells {
  178. fmt.Printf("当前第%d列 \n", k)
  179. text := cell.String()
  180. if k == 0 {
  181. province = text
  182. continue
  183. }
  184. date, ok1 := dateMap[k]
  185. if !ok1 {
  186. err = fmt.Errorf("找不到对应的日期,第%d行,第%d列", i, k)
  187. return
  188. }
  189. name, ok2 := nameMap[k]
  190. if !ok2 {
  191. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
  192. return
  193. }
  194. fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, province)
  195. // 处理指标名称
  196. // 过滤特殊的指标 放到对应的指标名称的下方
  197. if name != "市场标重猪价" && name != "150公斤左右较标猪" && name != "175公斤左右较标猪" {
  198. // 只处理以上三个类型,其余过滤
  199. continue
  200. }
  201. if name == "150公斤左右较标猪" {
  202. name = "150公斤较标猪价差"
  203. } else if name == "175公斤左右较标猪" {
  204. name = "175公斤较标猪价差"
  205. }
  206. fullIndexName := fmt.Sprintf("%s/%s", name, province)
  207. indexItem, okIndex := indexMap[fullIndexName]
  208. if !okIndex {
  209. // 新增指标
  210. indexItem = new(models.YongyiExcelIndex)
  211. indexItem.IndexName = fullIndexName
  212. indexItem.ClassifyName = classifyName
  213. // todo 处理indexCode
  214. indexItem.IndexCode = fullIndexName
  215. indexItem.Frequency = frequency
  216. indexItem.Unit = unit
  217. indexItem.ExcelDataMap = make(map[string]string)
  218. }
  219. fmt.Printf("indexItem%s", indexItem.IndexCode)
  220. indexItem.ExcelDataMap[date] = text
  221. indexMap[fullIndexName] = indexItem
  222. continue
  223. }
  224. }
  225. }
  226. for _, v := range indexMap {
  227. indexList = append(indexList, v)
  228. }
  229. // todo 整理请求入参,并加入,改成一个指标请求一次还是多个指标请求一次
  230. return
  231. }
  232. // HandleYongyiExcelDaily3 日度-商品猪全国均价和宰量
  233. func HandleYongyiExcelDaily3(sheet *xlsx.Sheet) (indexList []*models.YongyiExcelIndex, err error) {
  234. classifyName := "日度-商品猪全国均价和宰量"
  235. frequency := "日度"
  236. unit := ""
  237. // 遍历行读取
  238. indexList = make([]*models.YongyiExcelIndex, 0)
  239. nameMap := make(map[int]string)
  240. maxRow := sheet.MaxRow
  241. fmt.Println("最大行")
  242. fmt.Println(maxRow)
  243. // 指标名称
  244. indexMap := make(map[string]*models.YongyiExcelIndex)
  245. for i := 0; i < maxRow; i++ {
  246. fmt.Printf("当前第%d行 \n", i)
  247. if i == 0 { // 首行,名称
  248. row := sheet.Row(i)
  249. cells := row.Cells
  250. for k, cell := range cells {
  251. text := cell.String()
  252. nameMap[k] = text
  253. }
  254. } else { //数据列
  255. row := sheet.Row(i)
  256. cells := row.Cells
  257. date := ""
  258. for k, cell := range cells {
  259. fmt.Printf("当前第%d列 \n", k)
  260. text := cell.String()
  261. if k == 0 {
  262. if cell.IsTime() {
  263. dateText, _ := cell.GetTime(false)
  264. text = dateText.Format(utils.FormatDate)
  265. }
  266. date = text
  267. continue
  268. }
  269. name, ok2 := nameMap[k]
  270. if !ok2 {
  271. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
  272. return
  273. }
  274. if name != "全国均价" && name != "日屠宰量" {
  275. // 只处理以上三个类型,其余过滤
  276. continue
  277. }
  278. if name == "全国均价" {
  279. name = "商品猪出拦价/全国/均价"
  280. unit = "元/公斤"
  281. } else if name == "日屠宰量" {
  282. name = "商品猪日屠宰量/全国"
  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. // todo 处理indexCode
  293. indexItem.IndexCode = fullIndexName
  294. indexItem.Frequency = frequency
  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. // todo 整理请求入参,并加入,改成一个指标请求一次还是多个指标请求一次
  308. return
  309. }
  310. // HandleYongyiExcelDaily4 日度-屠宰企业屠宰量
  311. func HandleYongyiExcelDaily4(sheet *xlsx.Sheet) (indexList []*models.YongyiExcelIndex, err error) {
  312. classifyName := "日度-屠宰企业屠宰量"
  313. frequency := "日度"
  314. unit := "头/日"
  315. namePrefix := "商品猪日屠宰量"
  316. namePrefixPingin := utils.GetFirstPingYin(namePrefix)
  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", 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.Unit = unit
  382. indexItem.ExcelDataMap = make(map[string]string)
  383. }
  384. fmt.Printf("indexItem%s", indexItem.IndexCode)
  385. indexItem.ExcelDataMap[date] = text
  386. indexMap[fullIndexName] = indexItem
  387. }
  388. }
  389. }
  390. for _, v := range indexMap {
  391. indexList = append(indexList, v)
  392. }
  393. return
  394. }