base_from_yongyi.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. "github.com/xuri/excelize/v2"
  10. "io/fs"
  11. "os"
  12. "path"
  13. "path/filepath"
  14. "strings"
  15. "sync"
  16. "time"
  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. fileName = fmt.Sprintf("%s-%s%s", startDate, endDate, "周度图表版图.xlsx")
  229. filePath = filepath.Join(utils.YongyiReadFilePath, fileName)
  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. }
  376. var YongyiWeeklyChartHandlers = map[string]func(f *excelize.File, sheetName string) ([]*models.YongyiExcelIndex, error){
  377. "涌益样本测算": base_from_yongyi_v2.HandleYongyiExcelWeekly36,
  378. }
  379. func HandleYongyiExcelDailyV2(filePath string) (err error) {
  380. //filePath = fmt.Sprintf("/Users/xiexiaoyuan/Downloads/2024年2月2日涌益咨询日度数据.xlsx")
  381. defer func() {
  382. if err != nil {
  383. fmt.Printf("HandleYongyiExcelDailyV2 ErrMsg: %s\n", err.Error())
  384. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelDailyV2 ErrMsg: %s", err.Error()))
  385. }
  386. }()
  387. _ = path.Base(filePath)
  388. f, e := excelize.OpenFile(filePath)
  389. if e != nil {
  390. err = fmt.Errorf("open file err: %s", e.Error())
  391. return
  392. }
  393. defer func() {
  394. if e = f.Close(); e != nil {
  395. err = fmt.Errorf("f close err: %s", e.Error())
  396. }
  397. }()
  398. terminalCode := utils.TerminalCode
  399. for _, sheetName := range f.GetSheetMap() {
  400. // 检查是否有对应的处理函数
  401. handler, ok := YongyiDailyHandlers[sheetName]
  402. if !ok {
  403. continue // 如果没有,则跳过当前sheet
  404. }
  405. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelDailyV2 %s start", sheetName))
  406. // 调用处理函数,并处理可能发生的错误
  407. indexList, e := handler(f, sheetName)
  408. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelDailyV2 %s end", sheetName))
  409. if e != nil {
  410. utils.FileLog.Info(fmt.Sprintf("解析sheet:%s 失败, Err: %s", sheetName, err))
  411. continue
  412. }
  413. if len(indexList) > 0 {
  414. params := make(map[string]interface{})
  415. params["List"] = indexList
  416. params["TerminalCode"] = terminalCode
  417. result, e := PostEdbLib(params, utils.LIB_ROUTE_YONGYI_HANDLE)
  418. if e != nil {
  419. b, _ := json.Marshal(params)
  420. utils.FileLog.Info(fmt.Sprintf("sheet :%s PostEdbLib err: %s, params: %s", sheetName, e.Error(), string(b)))
  421. continue
  422. }
  423. resp := new(models.BaseEdbLibResponse)
  424. if e := json.Unmarshal(result, &resp); e != nil {
  425. utils.FileLog.Info(fmt.Sprintf("sheet :%s json.Unmarshal err: %s", sheetName, e))
  426. continue
  427. }
  428. if resp.Ret != 200 {
  429. utils.FileLog.Info(fmt.Sprintf("sheet :%s Msg: %s, ErrMsg: %s", sheetName, resp.Msg, resp.ErrMsg))
  430. continue
  431. }
  432. }
  433. }
  434. return
  435. }
  436. func HandleYongyiExcelWeeklyV2(filePath string) (err error) {
  437. //filePath = fmt.Sprintf("/Users/xiexiaoyuan/Downloads/涌益生猪项目数据库-3/2024.1.26-2024.2.1涌益咨询周度数据.xlsx")
  438. defer func() {
  439. if err != nil {
  440. fmt.Printf("HandleYongyiExcelWeeklyV2 ErrMsg: %s\n", err.Error())
  441. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeeklyV2 ErrMsg: %s", err.Error()))
  442. }
  443. }()
  444. _ = path.Base(filePath)
  445. f, e := excelize.OpenFile(filePath)
  446. if e != nil {
  447. err = fmt.Errorf("open file err: %s", e.Error())
  448. return
  449. }
  450. defer func() {
  451. if e = f.Close(); e != nil {
  452. err = fmt.Errorf("f close err: %s", e.Error())
  453. }
  454. }()
  455. terminalCode := utils.TerminalCode
  456. for _, sheetName := range f.GetSheetMap() {
  457. // 检查是否有对应的处理函数
  458. handler, ok := YongyiWeeklyHandlers[sheetName]
  459. if !ok {
  460. continue // 如果没有,则跳过当前sheet
  461. }
  462. // 调用处理函数,并处理可能发生的错误
  463. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeeklyV2 %s start", sheetName))
  464. indexList, e := handler(f, sheetName)
  465. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeeklyV2 %s end", sheetName))
  466. if e != nil {
  467. utils.FileLog.Info(fmt.Sprintf("解析sheet:%s 失败, Err: %s", sheetName, err))
  468. continue
  469. }
  470. if len(indexList) > 0 {
  471. params := make(map[string]interface{})
  472. params["List"] = indexList
  473. params["TerminalCode"] = terminalCode
  474. result, e := PostEdbLib(params, utils.LIB_ROUTE_YONGYI_HANDLE)
  475. if e != nil {
  476. b, _ := json.Marshal(params)
  477. utils.FileLog.Info(fmt.Sprintf("sheet :%s PostEdbLib err: %s, params: %s", sheetName, e.Error(), string(b)))
  478. continue
  479. }
  480. resp := new(models.BaseEdbLibResponse)
  481. if e := json.Unmarshal(result, &resp); e != nil {
  482. utils.FileLog.Info(fmt.Sprintf("sheet :%s json.Unmarshal err: %s", sheetName, e))
  483. continue
  484. }
  485. if resp.Ret != 200 {
  486. utils.FileLog.Info(fmt.Sprintf("sheet :%s Msg: %s, ErrMsg: %s", sheetName, resp.Msg, resp.ErrMsg))
  487. continue
  488. }
  489. }
  490. }
  491. return
  492. }
  493. func HandleYongyiExcelWeeklyChart(filePath string) (err error) {
  494. //filePath = fmt.Sprintf("/Users/xiexiaoyuan/Downloads/涌益生猪项目数据库-3/2024.1.26-2024.2.1周度图表版图.xlsx")
  495. defer func() {
  496. if err != nil {
  497. fmt.Printf("HandleYongyiExcelWeeklyChart ErrMsg: %s\n", err.Error())
  498. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeeklyChart ErrMsg: %s", err.Error()))
  499. }
  500. }()
  501. _ = path.Base(filePath)
  502. f, e := excelize.OpenFile(filePath)
  503. if e != nil {
  504. err = fmt.Errorf("open file err: %s", e.Error())
  505. return
  506. }
  507. defer func() {
  508. if e = f.Close(); e != nil {
  509. err = fmt.Errorf("f close err: %s", e.Error())
  510. }
  511. }()
  512. terminalCode := utils.TerminalCode
  513. for _, sheetName := range f.GetSheetMap() {
  514. // 检查是否有对应的处理函数
  515. handler, ok := YongyiWeeklyChartHandlers[sheetName]
  516. if !ok {
  517. continue // 如果没有,则跳过当前sheet
  518. }
  519. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeeklyChart %s start", sheetName))
  520. // 调用处理函数,并处理可能发生的错误
  521. indexList, e := handler(f, sheetName)
  522. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeeklyChart %s end", sheetName))
  523. if e != nil {
  524. utils.FileLog.Info(fmt.Sprintf("解析sheet:%s 失败, Err: %s", sheetName, err))
  525. continue
  526. }
  527. if len(indexList) > 0 {
  528. params := make(map[string]interface{})
  529. params["List"] = indexList
  530. params["TerminalCode"] = terminalCode
  531. result, e := PostEdbLib(params, utils.LIB_ROUTE_YONGYI_HANDLE)
  532. if e != nil {
  533. b, _ := json.Marshal(params)
  534. utils.FileLog.Info(fmt.Sprintf("sheet :%s PostEdbLib err: %s, params: %s", sheetName, e.Error(), string(b)))
  535. continue
  536. }
  537. resp := new(models.BaseEdbLibResponse)
  538. if e := json.Unmarshal(result, &resp); e != nil {
  539. utils.FileLog.Info(fmt.Sprintf("sheet :%s json.Unmarshal err: %s", sheetName, e))
  540. continue
  541. }
  542. if resp.Ret != 200 {
  543. utils.FileLog.Info(fmt.Sprintf("sheet :%s Msg: %s, ErrMsg: %s", sheetName, resp.Msg, resp.ErrMsg))
  544. continue
  545. }
  546. }
  547. }
  548. return
  549. }