base_from_yongyi.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "eta/eta_data_analysis/models"
  6. "eta/eta_data_analysis/services/base_from_yongyi_v2"
  7. "eta/eta_data_analysis/utils"
  8. "fmt"
  9. "io/fs"
  10. "os"
  11. "path"
  12. "path/filepath"
  13. "strings"
  14. "sync"
  15. "time"
  16. "github.com/xuri/excelize/v2"
  17. )
  18. /*
  19. 日度-商品猪出栏价
  20. 日度-商品猪标肥价差
  21. 日度-商品猪全国均价和宰量
  22. 日度-屠宰企业屠宰量
  23. 周度-商品猪出栏价
  24. 周度-体重
  25. 周度-屠宰厂宰前活猪重
  26. 周度-各体重段价差
  27. 周度-50公斤二元母猪价格
  28. 周度-规模场15公斤仔猪出栏价
  29. 周度-宰后结算价
  30. 周度-冻品库存
  31. 周度-冻品库存多样本
  32. 周度-猪肉价(前三等级白条均价)
  33. 月度出栏完成率
  34. 月度计划出栏量
  35. 月度-能繁母猪存栏(2020年2月新增)
  36. 月度-小猪存栏(2020年5月新增)
  37. 月度-中猪存栏(2020年5月新增)
  38. 月度-大猪存栏(2020年5月新增)
  39. 月度-商品猪出栏量
  40. */
  41. func HandleYongyiExcelDaily(filePath string) (err error) {
  42. //filePath := fmt.Sprintf("%s/%s_day.xlsx", utils.YongyiFilePath, time.Now().Format(utils.FormatDate))
  43. /*xlFile, err := xlsx.OpenFile(filePath)
  44. if err != nil {
  45. err = fmt.Errorf("打开文件失败, Err: %s", err)
  46. utils.FileLog.Info(fmt.Sprintf("打开文件:%s 失败, Err: %s", filePath, err))
  47. return
  48. }
  49. terminalCode := utils.TerminalCode
  50. for _, sheet := range xlFile.Sheets {
  51. var indexList []*models.YongyiExcelIndex
  52. switch sheet.Name {
  53. case "出栏价":
  54. indexList, err = base_from_yongyi.HandleYongyiExcelDaily1(sheet)
  55. case "标肥价差":
  56. indexList, err = base_from_yongyi.HandleYongyiExcelDaily2(sheet)
  57. case "价格+宰量":
  58. indexList, err = base_from_yongyi.HandleYongyiExcelDaily3(sheet)
  59. case "屠宰企业日度屠宰量":
  60. indexList, err = base_from_yongyi.HandleYongyiExcelDaily4(sheet)
  61. default:
  62. continue
  63. }
  64. if err != nil {
  65. utils.FileLog.Info(fmt.Sprintf("解析sheet:%s 失败, Err: %s", sheet.Name, err))
  66. continue
  67. }
  68. if len(indexList) > 0 {
  69. params := make(map[string]interface{})
  70. params["List"] = indexList
  71. params["TerminalCode"] = terminalCode
  72. result, e := PostEdbLib(params, utils.LIB_ROUTE_YONGYI_HANDLE)
  73. if e != nil {
  74. b, _ := json.Marshal(params)
  75. utils.FileLog.Info(fmt.Sprintf("sheet :%s PostEdbLib err: %s, params: %s", sheet.Name, e.Error(), string(b)))
  76. return
  77. }
  78. resp := new(models.BaseEdbLibResponse)
  79. if e := json.Unmarshal(result, &resp); e != nil {
  80. utils.FileLog.Info(fmt.Sprintf("sheet :%s json.Unmarshal err: %s", sheet.Name, e))
  81. continue
  82. }
  83. if resp.Ret != 200 {
  84. utils.FileLog.Info(fmt.Sprintf("sheet :%s Msg: %s, ErrMsg: %s", sheet.Name, resp.Msg, resp.ErrMsg))
  85. continue
  86. }
  87. }
  88. }*/
  89. return
  90. }
  91. func HandleYongyiExcelWeekly(filePath string) (err error) {
  92. // filePath := fmt.Sprintf("%s/%s_week.xlsx", utils.YongyiFilePath, time.Now().Format(utils.FormatDate))
  93. /*xlFile, err := xlsx.OpenFile(filePath)
  94. if err != nil {
  95. err = fmt.Errorf("打开文件失败, Err: %s", err)
  96. utils.FileLog.Info(fmt.Sprintf("打开文件:%s 失败, Err: %s", filePath, err))
  97. return
  98. }
  99. terminalCode := utils.TerminalCode
  100. for _, sheet := range xlFile.Sheets {
  101. var indexList []*models.YongyiExcelIndex
  102. switch sheet.Name {
  103. case "周度-商品猪出栏价":
  104. indexList, err = base_from_yongyi.HandleYongyiExcelWeekly1(sheet)
  105. case "周度-体重":
  106. indexList, err = base_from_yongyi.HandleYongyiExcelWeekly2(sheet)
  107. case "周度-屠宰厂宰前活猪重":
  108. indexList, err = base_from_yongyi.HandleYongyiExcelWeekly3(sheet)
  109. case "周度-各体重段价差":
  110. indexList, err = base_from_yongyi.HandleYongyiExcelWeekly4(sheet)
  111. case "周度-50公斤二元母猪价格", "周度-规模场15公斤仔猪出栏价", "周度-宰后结算价", "周度-冻品库存", "周度-猪肉价(前三等级白条均价)":
  112. indexList, err = base_from_yongyi.HandleYongyiExcelWeekly5(sheet)
  113. case "周度-冻品库存多样本", "月度出栏完成率":
  114. indexList, err = base_from_yongyi.HandleYongyiExcelWeekly6(sheet)
  115. case "月度计划出栏量":
  116. indexList, err = base_from_yongyi.HandleYongyiExcelWeekly7(sheet)
  117. case "月度-能繁母猪存栏(2020年2月新增)":
  118. indexList, err = base_from_yongyi.HandleYongyiExcelWeekly8(sheet)
  119. case "月度-小猪存栏(2020年5月新增)", "月度-中猪存栏(2020年5月新增)", "月度-大猪存栏(2020年5月新增)":
  120. indexList, err = base_from_yongyi.HandleYongyiExcelWeekly9(sheet)
  121. case "月度-商品猪出栏量":
  122. indexList, err = base_from_yongyi.HandleYongyiExcelWeekly10(sheet)
  123. default:
  124. continue
  125. }
  126. if err != nil {
  127. utils.FileLog.Info(fmt.Sprintf("解析sheet:%s 失败, Err: %s", sheet.Name, err))
  128. continue
  129. }
  130. if len(indexList) > 0 {
  131. params := make(map[string]interface{})
  132. params["List"] = indexList
  133. params["TerminalCode"] = terminalCode
  134. result, e := PostEdbLib(params, utils.LIB_ROUTE_YONGYI_HANDLE)
  135. if e != nil {
  136. b, _ := json.Marshal(params)
  137. utils.FileLog.Info(fmt.Sprintf("sheet :%s PostEdbLib err: %s, params: %s", sheet.Name, e.Error(), string(b)))
  138. return
  139. }
  140. resp := new(models.BaseEdbLibResponse)
  141. if e := json.Unmarshal(result, &resp); e != nil {
  142. utils.FileLog.Info(fmt.Sprintf("sheet :%s json.Unmarshal err: %s", sheet.Name, e))
  143. continue
  144. }
  145. if resp.Ret != 200 {
  146. utils.FileLog.Info(fmt.Sprintf("sheet :%s Msg: %s, ErrMsg: %s", sheet.Name, resp.Msg, resp.ErrMsg))
  147. continue
  148. }
  149. }
  150. }*/
  151. return
  152. }
  153. var (
  154. YongyiDownloadUrl = fmt.Sprintf("%syongyi/download", utils.YongyiDownloadHost)
  155. )
  156. // YongyiDownloadDaily
  157. // @Description: 调用python服务去涌益咨询官网下载日度excel文件
  158. // @datetime 2023-12-19 09:39:05
  159. func YongyiDownloadDaily(cont context.Context) (err error) {
  160. // 2023年12月19日涌益咨询日度数据
  161. now := time.Now()
  162. weekday := int(now.Weekday())
  163. if weekday == 0 {
  164. weekday = 7
  165. }
  166. // 计算与周一的天数差
  167. // 如果当天是周五,差值为0;周六为1,以此类推,直到下个周四为6
  168. offset := (weekday - 5) % 7 // 加7是为了处理weekday为0(周日)的情况
  169. if offset == 1 || offset == 2 {
  170. // 获取周一的日期
  171. now = now.AddDate(0, 0, -offset)
  172. }
  173. filePath := fmt.Sprintf("%s%s", now.Format("2006年1月2日"), "涌益咨询日度数据.xlsx")
  174. filePath = filepath.Join(utils.YongyiReadFilePath, filePath)
  175. fmt.Println("YongyiDownloadDaily: " + filePath)
  176. _, e := os.Stat(filePath)
  177. if e == nil { //文件或者目录存在
  178. return
  179. }
  180. url := fmt.Sprintf("%s?dayFlag=1&weekFlag=0", YongyiDownloadUrl)
  181. fmt.Println("YongyiDownload URL:" + url)
  182. body, err := HttpGet(url)
  183. if err != nil {
  184. utils.FileLog.Info("YongyiDownload Err:" + err.Error())
  185. return
  186. }
  187. utils.FileLog.Debug("YongyiDownload Result:" + string(body))
  188. return
  189. }
  190. // YongyiDownloadWeekyly
  191. // @Description: 调用python服务去涌益咨询官网下载周度excel文件
  192. // @datetime 2023-12-19 09:39:05
  193. func YongyiDownloadWeekyly(cont context.Context) (err error) {
  194. // 2023.11.10-2023.11.16涌益咨询 周度数据.xlsx
  195. // 获取本周的时间范围, 即当前时间
  196. now := time.Now()
  197. weekday := int(now.Weekday())
  198. if weekday == 0 {
  199. weekday = 7
  200. }
  201. // 计算与周一的天数差
  202. // 如果当天是周五,差值为0;周六为1,以此类推,直到下个周四为6
  203. offset := (weekday - 5) % 7 // 加7是为了处理weekday为0(周日)的情况
  204. // 获取周一的日期
  205. startDateT := now.AddDate(0, 0, -offset)
  206. startDate := startDateT.Format("2006.1.2")
  207. fmt.Printf("startDate: %s\n", startDate)
  208. endDate := startDateT.AddDate(0, 0, 6).Format("2006.1.2")
  209. fmt.Printf("endDate: %s\n", endDate)
  210. dataFileExsit := false
  211. chartFileExist := false
  212. fileName := fmt.Sprintf("%s-%s%s", startDate, endDate, "涌益咨询 周度数据.xlsx")
  213. filePath := filepath.Join(utils.YongyiReadFilePath, fileName)
  214. fmt.Println("YongyiDownloadWeekyly: " + filePath)
  215. // 从已处理的表格中查询是否已存在,如果已存在,也无需下载
  216. _, e := os.Stat(filePath)
  217. if e == nil { //文件或者目录存在
  218. dataFileExsit = true
  219. } else if os.IsNotExist(e) { //文件或者目录不存在
  220. filePath = filepath.Join(utils.YongyiFilePath, fileName)
  221. fmt.Println("YongyiDownloadWeekyly: " + filePath)
  222. // 从已处理的表格中查询是否已存在,如果已存在,也无需下载
  223. _, e = os.Stat(filePath)
  224. if e == nil { //文件或者目录存在
  225. dataFileExsit = true
  226. }
  227. }
  228. filePath = fmt.Sprintf("%s-%s%s", startDate, endDate, "周度图表版图.xlsx")
  229. filePath = filepath.Join(utils.YongyiReadFilePath, filePath)
  230. fmt.Println("YongyiDownloadWeekyly: " + filePath)
  231. _, e = os.Stat(filePath)
  232. if e == nil { //文件或者目录存在
  233. chartFileExist = true
  234. } else if os.IsNotExist(e) { //文件或者目录不存在
  235. filePath = filepath.Join(utils.YongyiFilePath, fileName)
  236. fmt.Println("YongyiDownloadWeekyly: " + filePath)
  237. // 从已处理的表格中查询是否已存在,如果已存在,也无需下载
  238. _, e = os.Stat(filePath)
  239. if e == nil { //文件或者目录存在
  240. chartFileExist = true
  241. }
  242. }
  243. if dataFileExsit && chartFileExist {
  244. return
  245. }
  246. url := fmt.Sprintf("%s?dayFlag=0&weekFlag=1", YongyiDownloadUrl)
  247. fmt.Println("YongyiDownload URL:" + url)
  248. body, err := HttpGet(url)
  249. if err != nil {
  250. utils.FileLog.Info("YongyiDownload Err:" + err.Error())
  251. return
  252. }
  253. utils.FileLog.Debug("YongyiDownload Result:" + string(body))
  254. return
  255. }
  256. var HandlezYongyiFilePathMutex sync.Mutex
  257. // 监听涌溢咨询文件夹是否有新增的excel文件
  258. func ReadWatchYongyiFile(cont context.Context) (err error) {
  259. fmt.Println("ReadWatchYongyiFile start")
  260. defer func() {
  261. if err != nil {
  262. fmt.Println("ReadWatchYongyiFile Err:" + err.Error())
  263. }
  264. }()
  265. if utils.YongyiFilePath == "" {
  266. utils.FileLog.Info("文件目录未配置")
  267. return
  268. }
  269. // 上一个监听未结束时不执行
  270. HandlezYongyiFilePathMutex.Lock()
  271. defer HandlezYongyiFilePathMutex.Unlock()
  272. err = filepath.Walk(utils.YongyiFilePath, func(path string, info fs.FileInfo, err error) error {
  273. if err != nil {
  274. err = fmt.Errorf("ReadWatchYongyiFile walk err: %s", err.Error())
  275. return err
  276. }
  277. if info.IsDir() {
  278. return nil
  279. }
  280. if path == "" {
  281. return nil
  282. }
  283. moveFlag := false
  284. //fmt.Println("path", path)
  285. if strings.Contains(path, "日度数据") {
  286. err = HandleYongyiExcelDailyV2(path)
  287. if err == nil {
  288. moveFlag = true
  289. }
  290. // todo 移动文件到新目录下
  291. } else if strings.Contains(path, "周度图表版图") {
  292. err = HandleYongyiExcelWeeklyChart(path)
  293. if err == nil {
  294. moveFlag = true
  295. }
  296. } else if strings.Contains(path, "周度数据") {
  297. err = HandleYongyiExcelWeeklyV2(path)
  298. if err == nil {
  299. moveFlag = true
  300. }
  301. }
  302. if err != nil {
  303. err = fmt.Errorf("file name: %s, handlerYongyi err: %s", path, err.Error())
  304. return err
  305. }
  306. if moveFlag {
  307. // 移动文件至已处理目录
  308. err = utils.CreateDirIfNotExists(utils.YongyiReadFilePath)
  309. if err != nil {
  310. err = fmt.Errorf("CreateDirIfNotExists err: %s", err.Error())
  311. fmt.Printf("ReadWatchYongyiFile CreateDirIfNotExists err: %s\n", err.Error())
  312. return err
  313. }
  314. destPath := filepath.Join(utils.YongyiReadFilePath, info.Name())
  315. err = os.Rename(path, destPath)
  316. if err != nil {
  317. err = fmt.Errorf("file move err: %s", err.Error())
  318. fmt.Printf("ReadWatchYongyiFile file move err: %s\n", err.Error())
  319. return err
  320. }
  321. }
  322. return nil
  323. })
  324. return
  325. }
  326. var YongyiDailyHandlers = map[string]func(f *excelize.File, sheetName string) ([]*models.YongyiExcelIndex, error){
  327. "出栏价": base_from_yongyi_v2.HandleYongyiExcelDaily1,
  328. "标肥价差": base_from_yongyi_v2.HandleYongyiExcelDaily2,
  329. "价格+宰量": base_from_yongyi_v2.HandleYongyiExcelDaily3,
  330. "屠宰企业日度屠宰量": base_from_yongyi_v2.HandleYongyiExcelDaily4,
  331. }
  332. var YongyiWeeklyHandlers = map[string]func(f *excelize.File, sheetName string) ([]*models.YongyiExcelIndex, error){
  333. "周度-商品猪出栏价": base_from_yongyi_v2.HandleYongyiExcelWeekly1,
  334. "周度-体重": base_from_yongyi_v2.HandleYongyiExcelWeekly2,
  335. "周度-屠宰厂宰前活猪重": base_from_yongyi_v2.HandleYongyiExcelWeekly3,
  336. "周度-各体重段价差": base_from_yongyi_v2.HandleYongyiExcelWeekly4,
  337. "周度-50公斤二元母猪价格": base_from_yongyi_v2.HandleYongyiExcelWeekly5,
  338. "周度-规模场15公斤仔猪出栏价": base_from_yongyi_v2.HandleYongyiExcelWeekly5,
  339. "周度-宰后结算价": base_from_yongyi_v2.HandleYongyiExcelWeekly5,
  340. "周度-冻品库存": base_from_yongyi_v2.HandleYongyiExcelWeekly5,
  341. "周度-猪肉价(前三等级白条均价)": base_from_yongyi_v2.HandleYongyiExcelWeekly5,
  342. "周度-冻品库存多样本": base_from_yongyi_v2.HandleYongyiExcelWeekly6,
  343. "月度出栏完成率": base_from_yongyi_v2.HandleYongyiExcelWeekly6,
  344. "月度计划出栏量": base_from_yongyi_v2.HandleYongyiExcelWeekly7,
  345. "月度-能繁母猪存栏(2020年2月新增)": base_from_yongyi_v2.HandleYongyiExcelWeekly8,
  346. "月度-小猪存栏(2020年5月新增)": base_from_yongyi_v2.HandleYongyiExcelWeekly9,
  347. "月度-中猪存栏(2020年5月新增)": base_from_yongyi_v2.HandleYongyiExcelWeekly9,
  348. "月度-大猪存栏(2020年5月新增)": base_from_yongyi_v2.HandleYongyiExcelWeekly9,
  349. "月度-商品猪出栏量": base_from_yongyi_v2.HandleYongyiExcelWeekly10,
  350. "历史猪价": base_from_yongyi_v2.HandleYongyiExcelWeekly12,
  351. "二育成本": base_from_yongyi_v2.HandleYongyiExcelWeekly13,
  352. "二育销量": base_from_yongyi_v2.HandleYongyiExcelWeekly14,
  353. "育肥栏舍利用率": base_from_yongyi_v2.HandleYongyiExcelWeekly15,
  354. "周度-养殖利润最新": base_from_yongyi_v2.HandleYongyiExcelWeekly16,
  355. "周度-当期、预期成本": base_from_yongyi_v2.HandleYongyiExcelWeekly17,
  356. "育肥全价料出厂价": base_from_yongyi_v2.HandleYongyiExcelWeekly18,
  357. "周度-成本计算附件": base_from_yongyi_v2.HandleYongyiExcelWeekly19,
  358. "周度-毛白价差": base_from_yongyi_v2.HandleYongyiExcelWeekly20,
  359. "仔猪、母猪": base_from_yongyi_v2.HandleYongyiExcelWeekly21,
  360. "周度-河南屠宰白条成本": base_from_yongyi_v2.HandleYongyiExcelWeekly22,
  361. "周度-淘汰母猪价格": base_from_yongyi_v2.HandleYongyiExcelWeekly23, //todo 少两个指标,云南和贵州,数据都不符合规范
  362. "鲜销率": base_from_yongyi_v2.HandleYongyiExcelWeekly24, //todo 等待解析(正月初六-二月初四)2022.2.6-3.6
  363. "周度-猪肉产品价格": base_from_yongyi_v2.HandleYongyiExcelWeekly25,
  364. "周度-屠宰企业日度屠宰量": base_from_yongyi_v2.HandleYongyiExcelWeekly26,
  365. "周度-屠宰新2022.10.28": base_from_yongyi_v2.HandleYongyiExcelWeekly27,
  366. "月度-淘汰母猪屠宰厂宰杀量": base_from_yongyi_v2.HandleYongyiExcelWeekly28,
  367. "月度-猪料销量": base_from_yongyi_v2.HandleYongyiExcelWeekly29,
  368. "月度-屠宰企业开工率": base_from_yongyi_v2.HandleYongyiExcelWeekly30,
  369. "月度-屠宰厂公母比例": base_from_yongyi_v2.HandleYongyiExcelWeekly31,
  370. "月度-生产指标(2021.5.7新增)": base_from_yongyi_v2.HandleYongyiExcelWeekly32,
  371. "月度-生产指标2": base_from_yongyi_v2.HandleYongyiExcelWeekly33,
  372. "月度-二元三元能繁比例": base_from_yongyi_v2.HandleYongyiExcelWeekly34, //todo 2019年1-6月
  373. "月度-能繁母猪存栏量": base_from_yongyi_v2.HandleYongyiExcelWeekly35,
  374. "月度-原种场二元后备母猪销量及出栏日龄": base_from_yongyi_v2.HandleYongyiExcelWeekly11,
  375. "周度-养殖利润": base_from_yongyi_v2.HandleYongyiExcelWeekly37,
  376. "华东冻品价格": base_from_yongyi_v2.HandleYongyiExcelWeekly38,
  377. "运费": base_from_yongyi_v2.HandleYongyiExcelWeekly39,
  378. "国产冻品2-4号肉价格": base_from_yongyi_v2.HandleYongyiExcelWeekly40,
  379. "月度-小猪(50公斤以下)存栏": base_from_yongyi_v2.HandleYongyiExcelWeekly41,
  380. "月度-中猪(50-80公斤)存栏": base_from_yongyi_v2.HandleYongyiExcelWeekly41,
  381. "月度-大猪(80公斤以上)月度存栏": base_from_yongyi_v2.HandleYongyiExcelWeekly41,
  382. "月度猪肉供应占比": base_from_yongyi_v2.HandleYongyiExcelWeekly42,
  383. "历史出栏体重": base_from_yongyi_v2.HandleYongyiExcelWeekly43,
  384. "重要部位冻品进口": base_from_yongyi_v2.HandleYongyiExcelWeekly44,
  385. "各存栏规模": base_from_yongyi_v2.HandleYongyiExcelWeekly45,
  386. "进口肉": base_from_yongyi_v2.HandleYongyiExcelWeekly46,
  387. "猪料原料占比": base_from_yongyi_v2.HandleYongyiExcelWeekly47,
  388. "MSY": base_from_yongyi_v2.HandleYongyiExcelWeekly48,
  389. }
  390. var YongyiWeeklyChartHandlers = map[string]func(f *excelize.File, sheetName string) ([]*models.YongyiExcelIndex, error){
  391. "涌益样本测算": base_from_yongyi_v2.HandleYongyiExcelWeekly36,
  392. }
  393. func HandleYongyiExcelDailyV2(filePath string) (err error) {
  394. //filePath = fmt.Sprintf("/Users/xiexiaoyuan/Downloads/2024年2月2日涌益咨询日度数据.xlsx")
  395. defer func() {
  396. if err != nil {
  397. fmt.Printf("HandleYongyiExcelDailyV2 ErrMsg: %s\n", err.Error())
  398. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelDailyV2 ErrMsg: %s", err.Error()))
  399. }
  400. }()
  401. _ = path.Base(filePath)
  402. f, e := excelize.OpenFile(filePath)
  403. if e != nil {
  404. err = fmt.Errorf("open file err: %s", e.Error())
  405. return
  406. }
  407. defer func() {
  408. if e = f.Close(); e != nil {
  409. err = fmt.Errorf("f close err: %s", e.Error())
  410. }
  411. }()
  412. terminalCode := utils.TerminalCode
  413. for _, sheetName := range f.GetSheetMap() {
  414. // 检查是否有对应的处理函数
  415. handler, ok := YongyiDailyHandlers[sheetName]
  416. if !ok {
  417. continue // 如果没有,则跳过当前sheet
  418. }
  419. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelDailyV2 %s start", sheetName))
  420. // 调用处理函数,并处理可能发生的错误
  421. indexList, e := handler(f, sheetName)
  422. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelDailyV2 %s end", sheetName))
  423. if e != nil {
  424. utils.FileLog.Info(fmt.Sprintf("解析sheet:%s 失败, Err: %s", sheetName, err))
  425. continue
  426. }
  427. if len(indexList) > 0 {
  428. params := make(map[string]interface{})
  429. params["List"] = indexList
  430. params["TerminalCode"] = terminalCode
  431. result, e := PostEdbLib(params, utils.LIB_ROUTE_YONGYI_HANDLE)
  432. if e != nil {
  433. b, _ := json.Marshal(params)
  434. utils.FileLog.Info(fmt.Sprintf("sheet :%s PostEdbLib err: %s, params: %s", sheetName, e.Error(), string(b)))
  435. continue
  436. }
  437. resp := new(models.BaseEdbLibResponse)
  438. if e := json.Unmarshal(result, &resp); e != nil {
  439. utils.FileLog.Info(fmt.Sprintf("sheet :%s json.Unmarshal err: %s", sheetName, e))
  440. continue
  441. }
  442. if resp.Ret != 200 {
  443. utils.FileLog.Info(fmt.Sprintf("sheet :%s Msg: %s, ErrMsg: %s", sheetName, resp.Msg, resp.ErrMsg))
  444. continue
  445. }
  446. }
  447. }
  448. return
  449. }
  450. func HandleYongyiExcelWeeklyV2(filePath string) (err error) {
  451. //filePath = fmt.Sprintf("/Users/xiexiaoyuan/Downloads/涌益生猪项目数据库-3/2024.1.26-2024.2.1涌益咨询周度数据.xlsx")
  452. defer func() {
  453. if err != nil {
  454. fmt.Printf("HandleYongyiExcelWeeklyV2 ErrMsg: %s\n", err.Error())
  455. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeeklyV2 ErrMsg: %s", err.Error()))
  456. }
  457. }()
  458. _ = path.Base(filePath)
  459. f, e := excelize.OpenFile(filePath)
  460. if e != nil {
  461. err = fmt.Errorf("open file err: %s", e.Error())
  462. return
  463. }
  464. defer func() {
  465. if e = f.Close(); e != nil {
  466. err = fmt.Errorf("f close err: %s", e.Error())
  467. }
  468. }()
  469. terminalCode := utils.TerminalCode
  470. for _, sheetName := range f.GetSheetMap() {
  471. // 检查是否有对应的处理函数
  472. handler, ok := YongyiWeeklyHandlers[sheetName]
  473. if !ok {
  474. continue // 如果没有,则跳过当前sheet
  475. }
  476. // 调用处理函数,并处理可能发生的错误
  477. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeeklyV2 %s start", sheetName))
  478. indexList, e := handler(f, sheetName)
  479. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeeklyV2 %s end", sheetName))
  480. if e != nil {
  481. utils.FileLog.Info(fmt.Sprintf("解析sheet:%s 失败, Err: %s", sheetName, err))
  482. continue
  483. }
  484. if len(indexList) > 0 {
  485. params := make(map[string]interface{})
  486. params["List"] = indexList
  487. params["TerminalCode"] = terminalCode
  488. result, e := PostEdbLib(params, utils.LIB_ROUTE_YONGYI_HANDLE)
  489. if e != nil {
  490. b, _ := json.Marshal(params)
  491. utils.FileLog.Info(fmt.Sprintf("sheet :%s PostEdbLib err: %s, params: %s", sheetName, e.Error(), string(b)))
  492. continue
  493. }
  494. resp := new(models.BaseEdbLibResponse)
  495. if e := json.Unmarshal(result, &resp); e != nil {
  496. utils.FileLog.Info(fmt.Sprintf("sheet :%s json.Unmarshal err: %s", sheetName, e))
  497. continue
  498. }
  499. if resp.Ret != 200 {
  500. utils.FileLog.Info(fmt.Sprintf("sheet :%s Msg: %s, ErrMsg: %s", sheetName, resp.Msg, resp.ErrMsg))
  501. continue
  502. }
  503. }
  504. }
  505. return
  506. }
  507. func HandleYongyiExcelWeeklyChart(filePath string) (err error) {
  508. //filePath = fmt.Sprintf("/Users/xiexiaoyuan/Downloads/涌益生猪项目数据库-3/2024.1.26-2024.2.1周度图表版图.xlsx")
  509. defer func() {
  510. if err != nil {
  511. fmt.Printf("HandleYongyiExcelWeeklyChart ErrMsg: %s\n", err.Error())
  512. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeeklyChart ErrMsg: %s", err.Error()))
  513. }
  514. }()
  515. _ = path.Base(filePath)
  516. f, e := excelize.OpenFile(filePath)
  517. if e != nil {
  518. err = fmt.Errorf("open file err: %s", e.Error())
  519. return
  520. }
  521. defer func() {
  522. if e = f.Close(); e != nil {
  523. err = fmt.Errorf("f close err: %s", e.Error())
  524. }
  525. }()
  526. terminalCode := utils.TerminalCode
  527. for _, sheetName := range f.GetSheetMap() {
  528. // 检查是否有对应的处理函数
  529. handler, ok := YongyiWeeklyChartHandlers[sheetName]
  530. if !ok {
  531. continue // 如果没有,则跳过当前sheet
  532. }
  533. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeeklyChart %s start", sheetName))
  534. // 调用处理函数,并处理可能发生的错误
  535. indexList, e := handler(f, sheetName)
  536. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeeklyChart %s end", sheetName))
  537. if e != nil {
  538. utils.FileLog.Info(fmt.Sprintf("解析sheet:%s 失败, Err: %s", sheetName, err))
  539. continue
  540. }
  541. if len(indexList) > 0 {
  542. params := make(map[string]interface{})
  543. params["List"] = indexList
  544. params["TerminalCode"] = terminalCode
  545. result, e := PostEdbLib(params, utils.LIB_ROUTE_YONGYI_HANDLE)
  546. if e != nil {
  547. b, _ := json.Marshal(params)
  548. utils.FileLog.Info(fmt.Sprintf("sheet :%s PostEdbLib err: %s, params: %s", sheetName, e.Error(), string(b)))
  549. continue
  550. }
  551. resp := new(models.BaseEdbLibResponse)
  552. if e := json.Unmarshal(result, &resp); e != nil {
  553. utils.FileLog.Info(fmt.Sprintf("sheet :%s json.Unmarshal err: %s", sheetName, e))
  554. continue
  555. }
  556. if resp.Ret != 200 {
  557. utils.FileLog.Info(fmt.Sprintf("sheet :%s Msg: %s, ErrMsg: %s", sheetName, resp.Msg, resp.ErrMsg))
  558. continue
  559. }
  560. }
  561. }
  562. return
  563. }