base.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. func getClassifySortMap() map[string]int {
  13. classifyMap := map[string]int{
  14. "日度-商品猪出栏价": 1,
  15. "日度-商品猪标肥价差": 2,
  16. "日度-商品猪全国均价和宰量": 3,
  17. "日度-屠宰企业屠宰量": 4,
  18. "历史猪价": 5,
  19. "周度-商品猪出栏价": 6,
  20. "周度-体重": 7,
  21. "周度-屠宰厂宰前活猪重": 8,
  22. "周度-各体重段价差": 9,
  23. "二育成本": 10,
  24. "二育销量": 11,
  25. "育肥栏舍利用率": 12,
  26. "周度-养殖利润最新": 13,
  27. "周度-当期、预期成本": 14,
  28. "周度-育肥全价料出厂价": 15,
  29. "周度-成本计算附件": 16,
  30. "周度-毛白价差": 17,
  31. "周度-50公斤二元母猪价格": 18,
  32. "周度-规模场15公斤仔猪出栏价": 19,
  33. "仔猪、母猪": 20,
  34. "周度-河南屠宰白条成本": 21,
  35. "周度-淘汰母猪价格": 22,
  36. "周度-宰后结算价": 23,
  37. "周度-冻品库存": 24,
  38. "周度-冻品库存多样本": 25,
  39. "周度-鲜销率": 26,
  40. "周度-猪肉价(前三等级白条均价)": 27,
  41. "周度-猪肉产品价格": 28,
  42. "周度-屠宰企业日度屠宰量": 29,
  43. "周度-屠宰新2022.10.28": 30,
  44. "月度出栏完成率": 31,
  45. "月度计划出栏量": 32,
  46. "月度-淘汰母猪屠宰厂宰杀量": 33,
  47. "月度-猪料销量": 34,
  48. "月度-屠宰企业开工率": 35,
  49. "月度-屠宰厂公母比例": 36,
  50. "月度-生产指标(2021.5.7新增)": 37,
  51. "月度-生产指标2": 38,
  52. "月度-二元三元能繁比例": 39,
  53. "月度-能繁母猪存栏(2020年2月新增)": 40,
  54. "月度-小猪存栏(2020年5月新增)": 41,
  55. "月度-中猪存栏(2020年5月新增)": 42,
  56. "月度-大猪存栏(2020年5月新增)": 43,
  57. "月度-商品猪出栏量": 44,
  58. "月度-能繁母猪存栏量": 45,
  59. "月度-原种场二元后备母猪销量及出栏日龄": 46,
  60. "涌益样本测算": 47,
  61. "周度-养殖利润": 48,
  62. "周度-华东冻品价格": 49,
  63. "运费": 50,
  64. "国产冻品2-4号肉价格": 51,
  65. "小猪(50公斤以下)存栏": 52,
  66. "中猪(50-80公斤)存栏": 53,
  67. "大猪(80公斤以上)月度存栏": 54,
  68. "月度猪肉供应占比": 55,
  69. "历史出栏体重": 56,
  70. "重要部位冻品进口": 57,
  71. "各存栏规模": 58,
  72. "进口肉": 59,
  73. "猪料原料占比": 60,
  74. "MSY": 61,
  75. }
  76. return classifyMap
  77. }
  78. var YongyiWeeklySheetMap5 map[string]*models.YongyiSheet
  79. var YongyiWeeklySheetMap6 map[string]*models.YongyiSheet
  80. var YongyiWeeklySheetMap9 map[string]*models.YongyiSheet
  81. var YongyiBaseInfoMap map[string]*models.YongyiSheet
  82. func init() {
  83. YongyiWeeklySheetMap5 = map[string]*models.YongyiSheet{
  84. "周度-50公斤二元母猪价格": {"50公斤二元母猪价格", "周度-50公斤二元母猪价格", "周度", "元/头"},
  85. "周度-规模场15公斤仔猪出栏价": {"规模场15公斤仔猪出栏价", "周度-规模场15公斤仔猪出栏价", "周度", "元/头"},
  86. "周度-宰后结算价": {"宰后结算价", "周度-宰后结算价", "周度", "元/公斤"},
  87. "周度-冻品库存": {"冻品库存", "周度-冻品库存", "周度", "%"},
  88. "周度-猪肉价(前三等级白条均价)": {"前三等级白条均价", "周度-猪肉价(前三等级白条均价)", "周度", "元/公斤"},
  89. }
  90. YongyiWeeklySheetMap6 = map[string]*models.YongyiSheet{
  91. "周度-冻品库存多样本": {"冻品库存多样本", "周度-冻品库存多样本", "周度", "%"},
  92. "月度出栏完成率": {"出栏完成率", "月度出栏完成率", "旬度", "%"},
  93. }
  94. YongyiWeeklySheetMap9 = map[string]*models.YongyiSheet{
  95. "月度-小猪存栏(2020年5月新增)": {"50kg以下小猪存栏量", "月度-小猪存栏(2020年5月新增)", "月度", "头"},
  96. "月度-中猪存栏(2020年5月新增)": {"中猪存栏量", "月度-中猪存栏(2020年5月新增)", "月度", "头"},
  97. "月度-大猪存栏(2020年5月新增)": {"大猪存栏量", "月度-大猪存栏(2020年5月新增)", "月度", "头"},
  98. }
  99. YongyiBaseInfoMap = map[string]*models.YongyiSheet{
  100. "出栏价": {"商品猪出栏价", "日度-商品猪出栏价", "日度", "元/公斤"},
  101. "标肥价差": {"", "日度-商品猪标肥价差", "日度", "元/公斤"},
  102. "价格+宰量": {"", "日度-商品猪全国均价和宰量", "日度", "元/公斤"},
  103. "屠宰企业日度屠宰量": {"商品猪日屠宰量", "日度-屠宰企业屠宰量", "日度", "头"},
  104. "周度-商品猪出栏价": {"商品猪出栏价", "周度-商品猪出栏价", "周度", "元/公斤"},
  105. "周度-体重": {"商品猪出栏体重", "周度-体重", "周度", "公斤"},
  106. "周度-屠宰厂宰前活猪重": {"商品猪宰前活猪重", "周度-屠宰厂宰前活猪重", "周度", "公斤"},
  107. "周度-各体重段价差": {"", "周度-各体重段价差", "周度", "元/斤"},
  108. "周度-50公斤二元母猪价格": {"50公斤二元母猪价格", "周度-50公斤二元母猪价格", "周度", "元/头"},
  109. "周度-规模场15公斤仔猪出栏价": {"规模场15公斤仔猪出栏价", "周度-规模场15公斤仔猪出栏价", "周度", "元/头"},
  110. "周度-宰后结算价": {"宰后结算价", "周度-宰后结算价", "周度", "元/公斤"},
  111. "周度-冻品库存": {"冻品库存", "周度-冻品库存", "周度", "%"},
  112. "周度-冻品库存多样本": {"冻品库存多样本", "周度-冻品库存多样本", "周度", "%"},
  113. "周度-猪肉价(前三等级白条均价)": {"前三等级白条均价", "周度-猪肉价(前三等级白条均价)", "周度", "元/公斤"},
  114. "月度出栏完成率": {"出栏完成率", "月度出栏完成率", "旬度", "%"},
  115. "月度计划出栏量": {"月度计划出栏量环比", "月度计划出栏量", "月度", "%"},
  116. "月度-能繁母猪存栏(2020年2月新增)": {"能繁母猪存栏量", "月度-能繁母猪存栏(2020年2月新增)", "月度", "头"},
  117. "月度-小猪存栏(2020年5月新增)": {"50kg以下小猪存栏量", "月度-小猪存栏(2020年5月新增)", "月度", "头"},
  118. "月度-中猪存栏(2020年5月新增)": {"中猪存栏量", "月度-中猪存栏(2020年5月新增)", "月度", "头"},
  119. "月度-大猪存栏(2020年5月新增)": {"大猪存栏量", "月度-大猪存栏(2020年5月新增)", "月度", "头"},
  120. "月度-商品猪出栏量": {"商品猪出栏量", "月度-商品猪出栏量", "月度", "头"},
  121. "历史猪价": {"历史猪价", "历史猪价", "月度", "元/公斤"},
  122. "二育成本": {"", "二育成本", "周度", "kg"}, //单位有四种kg、无, 元/kg, 元
  123. "二育销量": {"二育销量", "二育销量", "周度", "%"},
  124. "育肥栏舍利用率": {"育肥栏舍利用率", "育肥栏舍利用率", "旬度", "%"},
  125. "周度-养殖利润最新": {"出栏肥猪利润", "周度-养殖利润最新", "周度", "元/头"},
  126. "周度-当期、预期成本": {"", "周度-当期、预期成本", "周度", "元/kg"},
  127. "育肥全价料出厂价": {"育肥全价料出厂均价参考", "周度-育肥全价料出厂价", "周度", "元/吨"},
  128. "周度-成本计算附件": {"", "周度-成本计算附件", "周度", "元/头"}, //多个单位:元/头,%,kg,元
  129. "周度-毛白价差": {"毛白价差", "周度-毛白价差", "周度", "元/公斤"},
  130. "仔猪、母猪": {"", "仔猪、母猪", "周度", "元/头"}, //多个前缀,仔猪价,二元母猪价
  131. "周度-河南屠宰白条成本": {"河南屠宰白条成本", "周度-河南屠宰白条成本", "周度", "元/kg"}, //多个单位:kg,元/kg,元/头
  132. "周度-淘汰母猪价格": {"淘汰母猪价格", "周度-淘汰母猪价格", "周度", "元/斤"},
  133. "鲜销率": {"鲜销率", "周度-鲜销率", "周度", "%"},
  134. "周度-猪肉产品价格": {"猪肉产品价格", "周度-猪肉产品价格", "周度", "元/公斤"},
  135. "周度-屠宰企业日度屠宰量": {"屠宰企业日度屠宰量", "周度-屠宰企业日度屠宰量", "周度", "头"},
  136. "周度-屠宰新2022.10.28": {"不同规模屠宰厂宰杀量", "周度-屠宰新2022.10.28", "周度", "头"},
  137. "月度-淘汰母猪屠宰厂宰杀量": {"淘汰母猪屠宰厂宰杀量", "月度-淘汰母猪屠宰厂宰杀量", "月度", "头"},
  138. "月度-猪料销量": {"饲料销量(环比)", "月度-猪料销量", "月度", "头"}, //todo 单位有问题
  139. "月度-屠宰企业开工率": {"", "月度-屠宰企业开工率", "月度", "%"},
  140. "月度-屠宰厂公母比例": {"屠宰厂", "月度-屠宰厂公母比例", "月度", "头"}, //多个单位:头,%
  141. "月度-生产指标(2021.5.7新增)": {"生产指标", "月度-生产指标(2021.5.7新增)", "月度", "头"}, //多个单位:头,%
  142. "月度-生产指标2": {"生产指标", "月度-生产指标2", "月度", "头"}, //多个单位:头,%
  143. "月度-二元三元能繁比例": {"能繁母猪存栏量", "月度-二元三元能繁比例", "月度", "%"}, //多个单位:头,%
  144. "月度-能繁母猪存栏量": {"月度能繁母猪存栏量", "月度-能繁母猪存栏量", "月度", "头"},
  145. "月度-原种场二元后备母猪销量及出栏日龄": {"原种场二元后备母猪销量及出栏日龄", "月度-原种场二元后备母猪销量及出栏日龄", "月度", "头"},
  146. "涌益样本测算": {"涌益样本测算", "涌益样本测算", "月度", "窝"}, //多个单位:头,%,窝,公斤,元/公斤
  147. "周度-养殖利润": {"外购育肥", "周度-养殖利润", "周度", "元/头"},
  148. "华东冻品价格": {"华东冻品价格", "周度-华东冻品价格", "周度", "元/公斤"},
  149. "运费": {"9.6米拉猪车主流运费", "运费", "周度", "元/公里"},
  150. "国产冻品2-4号肉价格": {"", "国产冻品2-4号肉价格", "月度", "元/吨"},
  151. "月度-小猪(50公斤以下)存栏": {"小猪(50公斤以下)月度存栏", "小猪(50公斤以下)存栏", "月度", "头"},
  152. "月度-中猪(50-80公斤)存栏": {"中猪(50-80公斤)月度存栏", "中猪(50-80公斤)存栏", "月度", "头"},
  153. "月度-大猪(80公斤以上)月度存栏": {"大猪(80公斤以上)月度存栏", "大猪(80公斤以上)月度存栏", "月度", "头"},
  154. "月度猪肉供应占比": {"月度猪肉供应占比", "月度猪肉供应占比", "月度", "%"},
  155. "历史出栏体重": {"商品猪历史出栏体重", "历史出栏体重", "周度", "公斤"},
  156. "重要部位冻品进口": {"重要部位冻品进口", "重要部位冻品进口", "月度", "-"},
  157. "各存栏规模": {"半年度各规模存栏", "各存栏规模", "半年度", "%"},
  158. "进口肉": {"", "进口肉", "月度", ""},
  159. "猪料原料占比": {"猪料育肥全价料中各原料占比", "猪料原料占比", "月度", "%"},
  160. "MSY": {"MSY", "MSY", "年度", "%"},
  161. }
  162. }
  163. func GetBaseInfo(sheetName string) (classifyName string, classifySort int, frequency string, unit string, namePrefix string, namePrefixPingin string) {
  164. // 获取指标分类
  165. info, ok := YongyiBaseInfoMap[sheetName]
  166. if !ok {
  167. return
  168. }
  169. classifyName = info.ClassifyName
  170. classifyMap := getClassifySortMap()
  171. classifySort, _ = classifyMap[classifyName]
  172. frequency = info.Frequency
  173. unit = info.Unit
  174. namePrefix = info.NamePrefix
  175. namePrefixPingin = "yyzx" + utils.GetFirstPingYin(namePrefix)
  176. return
  177. }
  178. func GetMergeCells(f *excelize.File, sheet string) (mergeCellMap map[int]map[int]string, err error) {
  179. mergedCells, err := f.GetMergeCells(sheet)
  180. if err != nil {
  181. fmt.Println(err)
  182. return
  183. }
  184. mergeCellMap = make(map[int]map[int]string)
  185. // 遍历所有合并单元格范围
  186. for _, cellRange := range mergedCells {
  187. fmt.Println("Merged Cell Range:", cellRange)
  188. cellVal := cellRange.GetCellValue()
  189. // 解析合并单元格范围,例如 "A1:B2"
  190. startCell, endCell := cellRange.GetStartAxis(), cellRange.GetEndAxis()
  191. if err != nil {
  192. fmt.Println(err)
  193. continue
  194. }
  195. // 解析起始单元格的下标
  196. startCol, startRow, err := excelize.CellNameToCoordinates(startCell)
  197. if err != nil {
  198. fmt.Println(err)
  199. continue
  200. }
  201. // 解析结束单元格的下标
  202. endCol, endRow, err := excelize.CellNameToCoordinates(endCell)
  203. if err != nil {
  204. fmt.Println(err)
  205. continue
  206. }
  207. // 打印合并单元格的起始和结束下标
  208. fmt.Printf("Start: Row %d, Col %d\n", startRow, startCol)
  209. fmt.Printf("End: Row %d, Col %d\n", endRow, endCol)
  210. //把合并的单元格整理成单个单元格,用于计算
  211. for i := startRow; i <= endRow; i++ {
  212. cellMap, ok := mergeCellMap[i-1]
  213. if !ok {
  214. cellMap = make(map[int]string)
  215. }
  216. for j := startCol; j <= endCol; j++ {
  217. cellMap[j-1] = strings.TrimSpace(cellVal)
  218. }
  219. mergeCellMap[i-1] = cellMap
  220. }
  221. }
  222. return
  223. }
  224. // GetWeekly13IndexName 二育成本指标名称处理
  225. func GetWeekly13IndexName(name, level string) string {
  226. switch {
  227. case strings.Contains(name, "二育主流采购体重段"):
  228. return fmt.Sprintf("二育主流%s采购体重段", level)
  229. case strings.Contains(name, "计划出栏体重"):
  230. return fmt.Sprintf("计划%s出栏体重", level)
  231. case strings.Contains(name, "料肉比"):
  232. return fmt.Sprintf("%s料肉比", level)
  233. case strings.Contains(name, "采购成本"):
  234. return fmt.Sprintf("%s采购成本", level)
  235. case strings.Contains(name, "运费+损耗"):
  236. return fmt.Sprintf("%s运费+损耗", level)
  237. case strings.Contains(name, "增重饲料成本"):
  238. return fmt.Sprintf("%s增重饲料成本", level)
  239. case strings.Contains(name, "二育成本"):
  240. return fmt.Sprintf("%s二育成本", level)
  241. }
  242. return name
  243. }
  244. // GetWeekly13IndexUnit 二育成本指标名称处理
  245. func GetWeekly13IndexUnit(name string) string {
  246. switch {
  247. case strings.Contains(name, "二育主流采购体重段") || strings.Contains(name, "计划出栏体重"):
  248. return "kg"
  249. case strings.Contains(name, "料肉比"):
  250. return "无"
  251. case strings.Contains(name, "采购成本") || strings.Contains(name, "运费+损耗") || strings.Contains(name, "二育成本"):
  252. return "元/kg"
  253. case strings.Contains(name, "增重饲料成本"):
  254. return "元"
  255. }
  256. return name
  257. }
  258. func getSundayOfWeek(year int, week int) string {
  259. t := time.Date(year, 1, 1, 0, 0, 0, 0, time.Local)
  260. weekday := int(t.Weekday())
  261. daysToFirstSunday := (7 - weekday) % 7
  262. firstSunday := t.AddDate(0, 0, daysToFirstSunday)
  263. daysToDesiredSunday := (week - 1) * 7
  264. return firstSunday.AddDate(0, 0, daysToDesiredSunday).Format(utils.FormatDate)
  265. }
  266. func isValueValid(value string) bool {
  267. if value == "" {
  268. return false
  269. }
  270. re := regexp.MustCompile(`^-?(\d+(\.\d+)?|\.\d+)(%)?$`)
  271. return re.MatchString(value)
  272. }
  273. func excelDateToTimeDateStr(excelDate int) string {
  274. baseDate := time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC)
  275. return baseDate.AddDate(0, 0, excelDate).Format(utils.FormatDate)
  276. }
  277. func getMergeCellValMap(f *excelize.File, sheetName string) (mergeIndexDataMap map[string]string, e error) {
  278. mergeIndexDataMap = make(map[string]string)
  279. merges, e := f.GetMergeCells(sheetName)
  280. if e != nil {
  281. return
  282. }
  283. for _, merge := range merges {
  284. xmin, ymin, e := excelize.CellNameToCoordinates(merge.GetStartAxis())
  285. if e != nil {
  286. continue
  287. }
  288. xmax, ymax, e := excelize.CellNameToCoordinates(merge.GetEndAxis())
  289. if e != nil {
  290. continue
  291. }
  292. for i := xmin; i <= xmax; i++ {
  293. for j := ymin; j <= ymax; j++ {
  294. mergeIndexDataMap[fmt.Sprintf("%d:%d", i, j)] = merge.GetCellValue()
  295. }
  296. }
  297. }
  298. return
  299. }
  300. func getCellValue(cell string) (val string, ok bool) {
  301. valueArr := strings.Split(cell, "-")
  302. switch len(valueArr) {
  303. case 1:
  304. if !isValueValid(valueArr[0]) {
  305. return
  306. }
  307. return valueArr[0], true
  308. case 2:
  309. val1, err := strconv.ParseFloat(valueArr[0], 64)
  310. if err != nil {
  311. return
  312. }
  313. val2, err := strconv.ParseFloat(valueArr[1], 64)
  314. if err != nil {
  315. return
  316. }
  317. mean := (val1 + val2) / 2
  318. return fmt.Sprintf("%.2f", mean), true
  319. }
  320. return "", false
  321. }
  322. func LastDayOfMonth(year int, month time.Month) string {
  323. firstDay := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
  324. nextMonth := firstDay.AddDate(0, 1, 0)
  325. return nextMonth.Add(-time.Nanosecond * 1).Format(utils.FormatDate)
  326. }