base_from_fenwei.go 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_data_analysis/models"
  5. "eta/eta_data_analysis/utils"
  6. "fmt"
  7. "github.com/shakinm/xlsReader/xls"
  8. "github.com/tealeg/xlsx"
  9. "github.com/xuri/excelize/v2"
  10. "path"
  11. "strings"
  12. "time"
  13. )
  14. const (
  15. FenweiIndexNamePrefix = "fwsj" // 指标名称前缀
  16. FenweiExcelNameA = "动力煤产销存-66家"
  17. FenweiExcelNameB = "动力煤产销存-100家"
  18. FenweiExcelNameC = "焦煤产销存-53家"
  19. FenweiNewExcelNameC = "炼焦煤产销存分省-53家"
  20. FenweiExcelNameD = "焦煤产销存-88家"
  21. FenweiNewExcelNameD = "炼焦煤产销存分省-88家"
  22. FenweiExcelNameE = "样本焦化厂焦煤"
  23. FenweiYangBenIndexNamePre = "样本焦企炼焦煤库存可用天数"
  24. FenweiYangBenIndexClassifyId = 5 // 样本焦企炼焦分类ID
  25. FenweiExcelNameF = "焦炭产销存-61家"
  26. FenweiNewExcelNameF = "炼焦煤产销存分省-61家"
  27. FenweiExcelNameG = "焦炭产销存-100家"
  28. FenweiNewExcelNameG = "炼焦炭产销存分省-100家"
  29. FenweiExcelNameH = "样本钢厂焦炭"
  30. FenweiYangBenSteelCoalIndexNamePre = "样本钢厂焦炭可用天数"
  31. FenweiYangBenSteelCoalIndexClassifyId = 35 // 样本焦企炼焦分类ID
  32. )
  33. var FenweiProvinces = []string{
  34. "内蒙古", "山西", "陕西", "三省合计", "临汾市", "临汾", "吕梁市", "吕梁", "长治市", "长治", "山西其他", "河北", "合计", "山东", "其他",
  35. }
  36. // FenweiProvinceClassifyMapping 指标名称省份->分类省份
  37. var FenweiProvinceClassifyMapping = map[string]string{
  38. "内蒙古": "内蒙古",
  39. "山西": "山西",
  40. "陕西": "陕西",
  41. "三省合计": "三省合计",
  42. "临汾市": "临汾市",
  43. "吕梁市": "吕梁市",
  44. "长治市": "长治市",
  45. "临汾": "临汾市",
  46. "吕梁": "吕梁市",
  47. "长治": "长治市",
  48. "山西其他": "山西其他",
  49. "河北": "河北",
  50. "山东": "山东",
  51. "合计": "合计",
  52. "其他": "其他",
  53. }
  54. // FenweiFirstClassifyMap excel表名->一级分类ID
  55. var FenweiFirstClassifyMap = map[string]int{
  56. FenweiExcelNameA: 1,
  57. FenweiExcelNameB: 2,
  58. FenweiExcelNameC: 3,
  59. FenweiExcelNameD: 4,
  60. FenweiExcelNameE: 5,
  61. FenweiExcelNameF: 33,
  62. FenweiExcelNameG: 34,
  63. FenweiExcelNameH: 35,
  64. }
  65. // FenweiNewDlm66DiffIndexNameReplaceMapping 动力煤66家-指标名称不同的进行替换
  66. var FenweiNewDlm66DiffIndexNameReplaceMapping = map[string]string{
  67. "价格平均上涨幅度(元/吨)": "煤矿价格平均上涨幅度",
  68. "库存库存量(万吨)": "煤矿库存",
  69. "盈亏情况盈利煤矿数量": "盈利煤矿数量",
  70. "盈亏情况平均利润(元/吨)": "煤矿平均利润",
  71. "盈亏情况亏损煤矿数量": "亏损煤矿数量",
  72. "盈亏情况平均亏损(元/吨)": "煤矿平均亏损",
  73. "周度产量产量(万吨)": "煤矿周度产量",
  74. "销售好": "销售好煤矿数量",
  75. "销售一般": "销售一般煤矿数量",
  76. "销售不好": "销售不好煤矿数量",
  77. "销售关闭": "关闭煤矿数量",
  78. "产能利用率产能利用率": "煤矿产能利用率",
  79. "开工率开工率": "煤矿算术平均开工率(含停产)",
  80. "价格平均下跌幅度": "煤矿价格平均下跌幅度",
  81. }
  82. // FenweiNewDlm66IgnoreIndexNameMapping 动力煤66家-需要忽略的指标名称
  83. var FenweiNewDlm66IgnoreIndexNameMapping = []string{
  84. "库存环比", "发运至港口平均利润", "周度产量环比", "开工率算数平均(不含停产矿)", "变化", "产能利用率变化", "开工率变化",
  85. }
  86. // FenweiNewDlm100DiffIndexNameReplaceMapping 动力煤100家-指标名称不同的进行替换
  87. var FenweiNewDlm100DiffIndexNameReplaceMapping = map[string]string{
  88. "价格平均上涨幅度(元/吨)": "煤矿价格平均上涨幅度",
  89. "价格平均下跌幅度": "煤矿价格平均下跌幅度",
  90. "库存库存量(万吨)": "煤矿库存",
  91. "库存环比": "煤矿库存环比",
  92. "盈亏情况盈利煤矿数量": "盈利煤矿数量",
  93. "盈亏情况平均利润(元/吨)": "煤矿平均利润",
  94. "盈亏情况亏损煤矿数量": "亏损煤矿数量",
  95. "盈亏情况平均亏损(元/吨)": "煤矿平均亏损",
  96. "发运至港口平均利润": "煤炭发运至港口平均利润",
  97. "周度产量产量(万吨)": "煤矿周度产量",
  98. "周度产量环比": "煤矿周度产量环比",
  99. "产能利用率产能利用率": "煤矿产能利用率",
  100. "产能利用率变化": "煤矿产能利用率环比",
  101. "开工率开工率": "煤矿平均开工率",
  102. "开工率变化": "煤矿平均开工率环比",
  103. "销售好": "销售好煤矿数量",
  104. "销售一般": "销售一般煤矿数量",
  105. "销售不好": "销售不好煤矿数量",
  106. "销售关闭": "关闭煤矿数量",
  107. }
  108. // FenweiNewDlm100IgnoreIndexNameMapping 动力煤100家-需要忽略的指标名称
  109. var FenweiNewDlm100IgnoreIndexNameMapping = make([]string, 0)
  110. // FenweiNewJm53DiffIndexNameReplaceMapping 焦煤53家-指标名称不同的进行替换
  111. var FenweiNewJm53DiffIndexNameReplaceMapping = map[string]string{
  112. "价格情况煤矿数量": "煤矿数量",
  113. "价格情况上涨煤矿数量": "上涨煤矿数量",
  114. "价格情况平均上涨(元/吨)": "煤矿平均上涨幅度",
  115. "价格情况下跌煤矿数量": "下跌煤矿数量",
  116. "价格情况平均下跌(元/吨)": "煤矿平均下跌幅度",
  117. "价格情况持平煤矿数量": "持平煤矿数量",
  118. "原煤库存情况库存总量(万吨)": "原煤库存",
  119. "精煤库存情况库存总量(万吨)": "精煤库存",
  120. "开工情况权重平均开工率": "煤矿加权开工率",
  121. "开工情况算数平均开工率": "煤矿算术平均开工率",
  122. "开工情况停产煤矿数量": "停产煤矿数量",
  123. "开工情况本周原煤产量(万吨)": "本周原煤产量",
  124. "销售良好": "销售好煤矿数量",
  125. "销售正常": "销售一般煤矿数量",
  126. "销售不好": "销售不好煤矿数量",
  127. "成本盈利煤矿数量": "盈利煤矿数量",
  128. "成本平均利润": "煤矿平均利润",
  129. "成本亏损煤矿数量": "亏损煤矿数量",
  130. "成本平均亏损": "煤矿平均亏损",
  131. }
  132. // FenweiNewJm53IgnoreIndexNameMapping 焦煤53家-需要忽略的指标名称
  133. var FenweiNewJm53IgnoreIndexNameMapping = []string{
  134. "原煤库存情况环比(万吨)", "精煤库存情况环比(万吨)", "开工情况开工率变化", "开工情况停产煤矿数量变化", "开工情况环比产量变化",
  135. }
  136. // FenweiNewJm88DiffIndexNameReplaceMapping 焦煤88家-指标名称不同的进行替换
  137. var FenweiNewJm88DiffIndexNameReplaceMapping = map[string]string{
  138. "价格情况煤企数量": "煤企数量",
  139. "价格情况上涨煤企数量": "上涨煤企数量",
  140. "价格情况平均上涨(元/吨)": "煤企平均上涨幅度",
  141. "价格情况下跌煤矿数量": "下跌煤企数量",
  142. "价格情况平均下跌(元/吨)": "煤企平均下跌幅度",
  143. "价格情况持平煤企数量": "持平煤企数量",
  144. "原煤库存情况库存总量(万吨)": "原煤库存",
  145. "原煤库存情况环比(万吨)": "原煤库存环比",
  146. "精煤库存情况库存总量(万吨)": "精煤库存",
  147. "精煤库存情况环比(万吨)": "精煤库存环比",
  148. "开工情况产能利用率": "煤企产能利用率",
  149. "开工情况产能利用率变化": "煤企产能利用率环比",
  150. "开工情况开工率": "煤企开工率",
  151. "开工情况开工率变化": "煤企开工率环比",
  152. "开工情况停产煤矿数量": "停产煤矿数量",
  153. "开工情况停产煤矿数量变化": "停产煤矿数量变化",
  154. "开工情况本周原煤产量(万吨)": "本周原煤产量",
  155. "开工情况环比产量变化": "本周原煤产量变化",
  156. "本周精煤产量(万吨)": "本周精煤产量",
  157. "环比产量变化": "本周精煤产量变化",
  158. "良好": "销售好煤企数量",
  159. "正常": "销售一般煤企数量",
  160. "不好": "销售不好煤企数量",
  161. "盈利煤企数量": "盈利煤企数量",
  162. "商品煤平均利润": "煤企平均利润",
  163. "亏损煤企数量": "亏损煤企数量",
  164. "商品煤平均亏损": "煤企平均亏损",
  165. }
  166. // FenweiNewJm88IgnoreIndexNameMapping 焦煤88家-需要忽略的指标名称
  167. var FenweiNewJm88IgnoreIndexNameMapping = make([]string, 0)
  168. // FenweiNewJt61DiffIndexNameReplaceMapping 焦炭61家-指标名称不同的进行替换
  169. var FenweiNewJt61DiffIndexNameReplaceMapping = map[string]string{
  170. "价格情况焦化厂数量": "焦化厂数量",
  171. "价格情况上涨煤矿数量": "上涨煤矿数量",
  172. "价格情况平均上涨(元/吨)": "价格平均上涨",
  173. "下跌焦化厂数量": "价格下跌焦化厂数量",
  174. "价格情况平均下跌(元/吨)": "价格平均下跌",
  175. "价格情况持平焦化厂数量": "价格持平焦化厂数量",
  176. "原煤库存情况库存总量(万吨)": "原煤库存",
  177. "焦炭库存情况库存总量(万吨)": "精煤库存",
  178. "焦炭库存情况库存销售比(天数)": "库存销售比(天数)",
  179. "开工情况平均开工率(算数)": "算数平均开工率",
  180. "开工情况开工率(加权)": "加权开工率",
  181. "开工情况本周焦炭产量(万吨)": "本周焦炭产量",
  182. "销售良好": "销售良好焦炭数量",
  183. "销售正常": "销售正常焦炭数量",
  184. "销售不好": "销售不好焦炭数量",
  185. "成本盈利企业数量": "盈利企业数量",
  186. "成本平均利润": "焦炭平均利润",
  187. "成本亏损企业数量": "亏损企业数量",
  188. }
  189. // FenweiNewJt61IgnoreIndexNameMapping 焦炭61家-需要忽略的指标名称
  190. var FenweiNewJt61IgnoreIndexNameMapping = []string{
  191. "焦炭库存情况环比(万吨)", "焦炭库存情况环比(天数)", "开工情况开工率变化", "开工情况开工率变化(加权)", "开工情况环比产量变化",
  192. }
  193. // FenweiNewJt100DiffIndexNameReplaceMapping 焦炭100家-指标名称不同的进行替换
  194. var FenweiNewJt100DiffIndexNameReplaceMapping = map[string]string{
  195. "价格情况焦化厂数量": "焦化厂数量",
  196. "价格情况上涨煤矿数量": "价格上涨煤矿数量",
  197. "价格情况平均上涨(元/吨)": "价格平均上涨",
  198. "下跌焦化厂数量": "价格下跌焦化厂数量",
  199. "价格情况平均下跌(元/吨)": "价格平均下跌",
  200. "价格情况持平焦化厂数量": "价格持平焦化厂数量",
  201. "原煤库存情况库存总量(万吨)": "原煤库存",
  202. "焦炭库存情况库存总量(万吨)": "精煤库存",
  203. "焦炭库存情况库存销售比(天数)": "库存销售比(天数)",
  204. "开工情况产能利用率": "产能利用率",
  205. "开工情况本周焦炭产量(万吨)": "本周焦炭产量",
  206. "销售良好": "销售良好焦炭数量",
  207. "销售正常": "销售正常焦炭数量",
  208. "销售不好": "销售不好焦炭数量",
  209. "成本盈利企业数量": "盈利企业数量",
  210. "成本平均利润": "焦炭平均利润",
  211. "成本亏损企业数量": "亏损企业数量",
  212. }
  213. // FenweiNewJt100IgnoreIndexNameMapping 焦炭100家-需要忽略的指标名称
  214. var FenweiNewJt100IgnoreIndexNameMapping = []string{
  215. "焦炭库存情况环比(万吨)", "焦炭库存情况环比(天数)", "开工情况开工率变化", "开工情况开工率变化(加权)", "开工情况环比产量变化",
  216. }
  217. var FenweiNewDiffIndexNameReplaceMapping = map[string]map[string]string{
  218. FenweiExcelNameA: FenweiNewDlm66DiffIndexNameReplaceMapping,
  219. FenweiExcelNameB: FenweiNewDlm100DiffIndexNameReplaceMapping,
  220. FenweiExcelNameC: FenweiNewJm53DiffIndexNameReplaceMapping,
  221. FenweiExcelNameD: FenweiNewJm88DiffIndexNameReplaceMapping,
  222. FenweiExcelNameE: FenweiNewJt61DiffIndexNameReplaceMapping,
  223. FenweiExcelNameF: FenweiNewJt100DiffIndexNameReplaceMapping,
  224. }
  225. var FenweiNewIgnoreIndexNameMapping = map[string][]string{
  226. FenweiExcelNameA: FenweiNewDlm66IgnoreIndexNameMapping,
  227. FenweiExcelNameB: FenweiNewDlm100IgnoreIndexNameMapping,
  228. FenweiExcelNameC: FenweiNewJm53IgnoreIndexNameMapping,
  229. FenweiExcelNameD: FenweiNewJm88IgnoreIndexNameMapping,
  230. FenweiExcelNameE: FenweiNewJt61IgnoreIndexNameMapping,
  231. FenweiExcelNameF: FenweiNewJt100IgnoreIndexNameMapping,
  232. }
  233. var FenweiNewIndexNameSuffixMapping = map[string]string{
  234. FenweiExcelNameA: "/动力煤/66家样本",
  235. FenweiExcelNameB: "/动力煤/100家样本",
  236. FenweiExcelNameC: "/焦煤/53家样本",
  237. FenweiExcelNameD: "/焦煤/88家样本",
  238. FenweiExcelNameE: "/焦炭/61家样本",
  239. FenweiExcelNameF: "/焦炭/100家样本",
  240. }
  241. var FenweiNewExcelProvinceMapping = map[string]string{
  242. "内蒙古": "内蒙古",
  243. "山西": "山西",
  244. "陕西": "陕西",
  245. "三省合计": "三省合计",
  246. "山西-临汾市": "临汾",
  247. "山西-吕梁市": "吕梁",
  248. "山西-长治市": "长治",
  249. "山西-其他": "山西其他",
  250. "山东": "山东",
  251. "其他": "其他",
  252. "合计": "合计",
  253. "河北": "河北",
  254. }
  255. // AnalysisFenweiHistoryExcel 解析汾渭历史Excel数据
  256. func AnalysisFenweiHistoryExcel(filePath string) (err error) {
  257. defer func() {
  258. if err != nil {
  259. fmt.Printf("AnalysisFenweiExcel ErrMsg: %s\n", err.Error())
  260. utils.FileLog.Info(fmt.Sprintf("AnalysisFenweiExcel ErrMsg: %s", err.Error()))
  261. }
  262. }()
  263. fileName := path.Base(filePath)
  264. var indexes []*models.FenweiExcelIndex
  265. var e error
  266. if strings.Contains(fileName, FenweiExcelNameA) {
  267. indexes, e = AnalysisFenweiHistoryExcelNormal(filePath, FenweiExcelNameA)
  268. } else if strings.Contains(fileName, FenweiExcelNameB) {
  269. indexes, e = AnalysisFenweiHistoryExcelNormal(filePath, FenweiExcelNameB)
  270. } else if strings.Contains(fileName, FenweiExcelNameC) {
  271. indexes, e = AnalysisFenweiHistoryExcelNormal(filePath, FenweiExcelNameC)
  272. } else if strings.Contains(fileName, FenweiExcelNameD) {
  273. indexes, e = AnalysisFenweiHistoryExcelNormal(filePath, FenweiExcelNameD)
  274. } else if strings.Contains(fileName, FenweiExcelNameE) {
  275. indexes, e = AnalysisFenweiExcelYangBen(filePath)
  276. } else if strings.Contains(fileName, FenweiExcelNameF) {
  277. indexes, e = AnalysisFenweiHistoryExcelNormal(filePath, FenweiExcelNameF)
  278. } else if strings.Contains(fileName, FenweiExcelNameG) {
  279. indexes, e = AnalysisFenweiHistoryExcelNormal(filePath, FenweiExcelNameG)
  280. } else if strings.Contains(fileName, FenweiExcelNameH) {
  281. indexes, e = AnalysisFenweiExcelYangBenSteelCoal(filePath)
  282. } else {
  283. utils.FileLog.Info(fmt.Sprintf("文件名有误, FileName: %s", fileName))
  284. return
  285. }
  286. if e != nil {
  287. utils.FileLog.Info(fmt.Sprintf("获取excel数据失败, FileName: %s, Err: %s", fileName, e))
  288. return
  289. }
  290. if len(indexes) == 0 {
  291. utils.FileLog.Info(fmt.Sprintf("fileName: %s, index empty", fileName))
  292. return
  293. }
  294. // 写入数据
  295. params := make(map[string]interface{})
  296. params["List"] = indexes
  297. params["TerminalCode"] = utils.TerminalCode
  298. result, e := PostEdbLib(params, utils.LIB_ROUTE_FENWEI_HANDLE)
  299. if e != nil {
  300. b, _ := json.Marshal(params)
  301. utils.FileLog.Info(fmt.Sprintf("fileName: %s, PostEdbLib err: %s, params: %s", fileName, e.Error(), string(b)))
  302. return
  303. }
  304. resp := new(models.BaseEdbLibResponse)
  305. if e := json.Unmarshal(result, &resp); e != nil {
  306. utils.FileLog.Info(fmt.Sprintf("fileName: %s, json.Unmarshal err: %s", fileName, e.Error()))
  307. return
  308. }
  309. if resp.Ret != 200 {
  310. utils.FileLog.Info(fmt.Sprintf("fileName: %s, Msg: %s, ErrMsg: %s", fileName, resp.Msg, resp.ErrMsg))
  311. return
  312. }
  313. return
  314. }
  315. // AnalysisFenweiHistoryExcelNormal 历史数据-动力煤产销存/焦煤产销存/焦炭产销存
  316. func AnalysisFenweiHistoryExcelNormal(filePath, excelName string) (indexes []*models.FenweiExcelIndex, err error) {
  317. defer func() {
  318. if err != nil {
  319. fmt.Printf("AnalysisFenweiHistoryExcelNormal ErrMsg: %s\n", err.Error())
  320. utils.FileLog.Info(fmt.Sprintf("AnalysisFenweiHistoryExcelNormal ErrMsg: %s", err.Error()))
  321. }
  322. }()
  323. f, e := excelize.OpenFile(filePath)
  324. if e != nil {
  325. err = fmt.Errorf("open file err: %s", e.Error())
  326. return
  327. }
  328. defer func() {
  329. if e = f.Close(); e != nil {
  330. err = fmt.Errorf("f close err: %s", e.Error())
  331. }
  332. }()
  333. sheetName := f.GetSheetName(0)
  334. rows, e := f.GetRows(sheetName)
  335. if e != nil {
  336. err = fmt.Errorf("f GetRows err: %s", e.Error())
  337. return
  338. }
  339. // 获取指标分类
  340. firstId := FenweiFirstClassifyMap[excelName]
  341. if firstId <= 0 {
  342. err = fmt.Errorf("excel名称有误, 分类不匹配, excelName: %s", excelName)
  343. return
  344. }
  345. classifies, e := GetFenweiClassify()
  346. if e != nil {
  347. err = fmt.Errorf("获取指标分类失败, Err: %s", e.Error())
  348. return
  349. }
  350. secondClassifyMap := make(map[string]int)
  351. for _, v := range classifies {
  352. if v.ClassifyId != firstId {
  353. continue
  354. }
  355. for _, c := range v.Child {
  356. secondClassifyMap[c.ClassifyName] = c.ClassifyId
  357. }
  358. }
  359. indexes = make([]*models.FenweiExcelIndex, 0)
  360. indexColIndex := make(map[int]*models.FenweiExcelIndex) // 列对应的指标(B列-?列)
  361. maxCol := 0
  362. sortArr := make([]string, 0) // 排序用的
  363. for i, row := range rows {
  364. // 首行为标题, 忽略
  365. if i == 0 {
  366. continue
  367. }
  368. // 第二行-指标名称
  369. if i == 1 {
  370. for k, cell := range row {
  371. if k == 0 {
  372. maxCol += 1
  373. continue
  374. }
  375. name := strings.TrimSpace(cell)
  376. name = strings.ReplaceAll(name, " ", "")
  377. if name == "" {
  378. break
  379. }
  380. maxCol += 1
  381. if indexColIndex[k] == nil {
  382. indexColIndex[k] = new(models.FenweiExcelIndex)
  383. indexColIndex[k].ExcelDataMap = make(map[string]string)
  384. indexColIndex[k].TerminalCode = utils.TerminalCode
  385. indexColIndex[k].IndexName = name
  386. }
  387. }
  388. continue
  389. }
  390. // 第三行-指标ID、指标分类
  391. if i == 2 {
  392. for k := 0; k < maxCol; k++ {
  393. if k == 0 {
  394. continue
  395. }
  396. if indexColIndex[k] == nil {
  397. utils.FileLog.Info(fmt.Sprintf("第%d列无指标ID", k))
  398. break
  399. }
  400. // 例:内蒙古煤矿数量/动力煤/66家样本->fwsjneimenggumksldlm66jyb
  401. name := strings.ReplaceAll(indexColIndex[k].IndexName, "/", "")
  402. var middle, suffix, province string
  403. for _, p := range FenweiProvinces {
  404. // 此处山西其他可能会被错误的给鉴别为山西, 所以优先鉴别一下山西其他
  405. if strings.Contains(name, "山西其他") {
  406. province = "山西其他"
  407. middle = utils.GetFullPingYin("山西其他")
  408. name = strings.ReplaceAll(name, p, "")
  409. break
  410. }
  411. if strings.Contains(name, p) {
  412. province = p
  413. middle = utils.GetFullPingYin(p)
  414. name = strings.ReplaceAll(name, p, "")
  415. break
  416. }
  417. }
  418. if middle == "" {
  419. utils.FileLog.Info(fmt.Sprintf("指标名称转换编码有误, IndexName: %s", indexColIndex[k].IndexName))
  420. continue
  421. }
  422. classifyProvince := FenweiProvinceClassifyMapping[province]
  423. classifyId := secondClassifyMap[classifyProvince]
  424. if classifyId <= 0 {
  425. utils.FileLog.Info(fmt.Sprintf("指标名称分类不匹配, IndexName: %s", indexColIndex[k].IndexName))
  426. continue
  427. }
  428. suffix = utils.GetFirstPingYin(name)
  429. indexColIndex[k].IndexCode = fmt.Sprint(FenweiIndexNamePrefix, middle, suffix)
  430. indexColIndex[k].ClassifyId = classifyId
  431. sortArr = append(sortArr, indexColIndex[k].IndexCode)
  432. }
  433. continue
  434. }
  435. // 第四行-频度
  436. if i == 3 {
  437. for k, cell := range row {
  438. if k == 0 {
  439. continue
  440. }
  441. if indexColIndex[k] == nil {
  442. utils.FileLog.Info(fmt.Sprintf("第%d列无指标名称", k))
  443. break
  444. }
  445. frequency := strings.TrimSpace(cell)
  446. indexColIndex[k].Frequency = frequency
  447. }
  448. continue
  449. }
  450. // 第五行-单位
  451. if i == 4 {
  452. for k, cell := range row {
  453. if k == 0 {
  454. continue
  455. }
  456. if indexColIndex[k] == nil {
  457. utils.FileLog.Info(fmt.Sprintf("第%d列无指标单位", k))
  458. break
  459. }
  460. unit := strings.TrimSpace(cell)
  461. indexColIndex[k].Unit = unit
  462. }
  463. continue
  464. }
  465. // 数据行
  466. if i > 4 {
  467. var date string
  468. for k, cell := range row {
  469. if k == 0 {
  470. // 日期
  471. date = strings.TrimSpace(cell)
  472. _, e := time.Parse(utils.FormatDate, date)
  473. if e != nil {
  474. // 如果第一列不是日期那么结束本行的读取
  475. break
  476. }
  477. continue
  478. }
  479. // 日期数据
  480. if date == "" {
  481. utils.FileLog.Info(fmt.Sprintf("第%d行无数据日期", i))
  482. break
  483. }
  484. if indexColIndex[k] == nil {
  485. utils.FileLog.Info(fmt.Sprintf("第%d列无指标名称", k))
  486. break
  487. }
  488. val := strings.TrimSpace(cell)
  489. if val == "" {
  490. // 单元格数据为空, 则当前日期无数据
  491. continue
  492. }
  493. indexColIndex[k].ExcelDataMap[date] = val
  494. }
  495. continue
  496. }
  497. }
  498. codeIndexes := make(map[string]*models.FenweiExcelIndex)
  499. for _, v := range indexColIndex {
  500. if v.IndexCode == "" {
  501. continue
  502. }
  503. codeIndexes[v.IndexCode] = v
  504. }
  505. for _, v := range sortArr {
  506. if codeIndexes[v] != nil {
  507. indexes = append(indexes, codeIndexes[v])
  508. }
  509. }
  510. return
  511. }
  512. // AnalysisFenweiExcelYangBen 样本焦化厂
  513. func AnalysisFenweiExcelYangBen(filePath string) (indexes []*models.FenweiExcelIndex, err error) {
  514. defer func() {
  515. if err != nil {
  516. fmt.Printf("AnalysisFenweiExcelYangBen ErrMsg: %s\n", err.Error())
  517. utils.FileLog.Info(fmt.Sprintf("AnalysisFenweiExcelYangBen ErrMsg: %s", err.Error()))
  518. }
  519. }()
  520. indexColIndex := make(map[int]*models.FenweiExcelIndex) // 列对应的指标(B列-E列)
  521. provinces := []string{"地区", "全国", "山西", "河北", "山东"}
  522. for k, v := range provinces {
  523. if k == 0 {
  524. continue
  525. }
  526. if indexColIndex[k] == nil {
  527. indexColIndex[k] = new(models.FenweiExcelIndex)
  528. indexColIndex[k].ExcelDataMap = make(map[string]string)
  529. indexColIndex[k].TerminalCode = utils.TerminalCode
  530. indexColIndex[k].ClassifyId = FenweiYangBenIndexClassifyId
  531. indexColIndex[k].Frequency = "周度"
  532. indexColIndex[k].Unit = "天"
  533. indexColIndex[k].IndexName = fmt.Sprintf("%s/%s", FenweiYangBenIndexNamePre, v)
  534. // 指标编码, 例:样本焦企炼焦煤库存可用天数/全国->fwsjybjqljmkckytsquanguo
  535. name := strings.ReplaceAll(indexColIndex[k].IndexName, "/", "")
  536. var middle, suffix string
  537. for _, p := range provinces {
  538. if strings.Contains(name, p) {
  539. suffix = utils.GetFullPingYin(p)
  540. name = strings.ReplaceAll(name, p, "")
  541. break
  542. }
  543. }
  544. if suffix == "" {
  545. utils.FileLog.Info(fmt.Sprintf("指标名称转换编码有误, IndexName: %s", indexColIndex[k].IndexName))
  546. continue
  547. }
  548. middle = utils.GetFirstPingYin(name)
  549. indexColIndex[k].IndexCode = fmt.Sprint(FenweiIndexNamePrefix, middle, suffix)
  550. }
  551. }
  552. maxCols := 5 // 每行只读取前5列即可
  553. startDateRow := 0 // 数据日期起始行(第一行), 后面依次加4行读取
  554. startDataRow := 2 // 数据值起始行(第三行), 后面依次加4行读取
  555. sumDateRow := 0 // 数据日期累加的行, 每加4次重置0
  556. sumDataRow := 0 // 数据值累加的行, 每加4次重置0
  557. currDate := "" // 最近的一次日期
  558. stop := false // 结束读取excel
  559. xlsFile, e := xls.OpenFile(filePath)
  560. if e != nil {
  561. err = fmt.Errorf("open xls file err: %s", e.Error())
  562. return
  563. }
  564. sheet, e := xlsFile.GetSheet(0)
  565. if e != nil {
  566. err = fmt.Errorf("GetSheet err: %s", e.Error())
  567. return
  568. }
  569. rows := sheet.GetRows()
  570. for i, row := range rows {
  571. sumDateRow += 1
  572. sumDataRow += 1
  573. // 第一个日期行/后续每4行取一次日期
  574. if i == startDateRow || sumDateRow == 4 {
  575. if stop {
  576. break
  577. }
  578. sumDateRow = 0
  579. for k, cell := range row.GetCols() {
  580. // 日期只取第一个单元格中的
  581. if k > 0 {
  582. break
  583. }
  584. strDates := strings.TrimSpace(cell.GetString())
  585. strDates = strings.ReplaceAll(strDates, "样本焦企炼焦煤库存", "")
  586. strDates = strings.ReplaceAll(strDates, "\x00", "")
  587. strDates = strings.ReplaceAll(strDates, "本焦企炼焦煤库存", "")
  588. strDateArr := strings.Split(strDates, "-")
  589. // 正常情况下, 长度不为2说明读取到了最后一行了
  590. if len(strDateArr) != 2 {
  591. utils.FileLog.Info(fmt.Sprintf("第%d行数据日期长度有误, 结束读取", i+1))
  592. stop = true
  593. break
  594. }
  595. t, e := time.Parse("2006.1.2", strDateArr[1])
  596. if e != nil {
  597. utils.FileLog.Info(fmt.Sprintf("第%d行数据日期有误, err: %s", i+1, e.Error()))
  598. break
  599. }
  600. currDate = t.Format(utils.FormatDate)
  601. }
  602. }
  603. // 第一个数据行/后续每4行取一次数据
  604. if i == startDataRow || sumDataRow == 4 {
  605. if currDate == "" {
  606. utils.FileLog.Info(fmt.Sprintf("第%d行数据对应日期有误", i+1))
  607. continue
  608. }
  609. sumDataRow = 0
  610. for k, cell := range row.GetCols() {
  611. if k >= maxCols {
  612. break
  613. }
  614. if k == 0 {
  615. continue
  616. }
  617. val := cell.GetFloat64()
  618. // 保留4位小数
  619. indexColIndex[k].ExcelDataMap[currDate] = fmt.Sprintf("%.4f", val)
  620. }
  621. // 重置日期
  622. currDate = ""
  623. }
  624. }
  625. indexes = make([]*models.FenweiExcelIndex, 0)
  626. for _, v := range indexColIndex {
  627. if v.IndexCode == "" {
  628. continue
  629. }
  630. indexes = append(indexes, v)
  631. }
  632. return
  633. }
  634. // GetFenweiClassify 获取汾渭数据分类
  635. func GetFenweiClassify() (classifies []models.BaseFromFenweiClassifyItem, err error) {
  636. params := make(map[string]interface{})
  637. result, e := PostEdbLib(params, utils.LIB_ROUTE_FENWEI_CLASSIFY)
  638. if e != nil {
  639. b, _ := json.Marshal(params)
  640. err = fmt.Errorf("GetFenweiClassify, PostEdbLib err: %s, params: %s", e.Error(), string(b))
  641. return
  642. }
  643. resp := new(models.EdbLibFenweiClassifyResponse)
  644. if e = json.Unmarshal(result, &resp); e != nil {
  645. err = fmt.Errorf(fmt.Sprintf("GetFenweiClassify, json.Unmarshal err: %s", e.Error()))
  646. return
  647. }
  648. if resp.Ret != 200 {
  649. err = fmt.Errorf(fmt.Sprintf("GetFenweiClassify, Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg))
  650. return
  651. }
  652. classifies = resp.Data
  653. return
  654. }
  655. // GetFenweiIndexList 获取汾渭指标列表
  656. func GetFenweiIndexList() (list []models.BaseFromFenweiIndex, err error) {
  657. params := make(map[string]interface{})
  658. result, e := PostEdbLib(params, utils.LIB_ROUTE_FENWEI_INDEX_LIST)
  659. if e != nil {
  660. b, _ := json.Marshal(params)
  661. err = fmt.Errorf("GetFenweiIndexList, PostEdbLib err: %s, params: %s", e.Error(), string(b))
  662. return
  663. }
  664. resp := new(models.EdbLibFenweiIndexListResponse)
  665. if e = json.Unmarshal(result, &resp); e != nil {
  666. err = fmt.Errorf(fmt.Sprintf("GetFenweiIndexList, json.Unmarshal err: %s", e.Error()))
  667. return
  668. }
  669. if resp.Ret != 200 {
  670. err = fmt.Errorf(fmt.Sprintf("GetFenweiIndexList, Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg))
  671. return
  672. }
  673. list = resp.Data
  674. return
  675. }
  676. // AnalysisFenweiNewExcel 解析汾渭增量Excel数据
  677. func AnalysisFenweiNewExcel(filePath string) (err error) {
  678. defer func() {
  679. if err != nil {
  680. fmt.Printf("AnalysisFenweiNewExcel ErrMsg: %s\n", err.Error())
  681. utils.FileLog.Info(fmt.Sprintf("AnalysisFenweiNewExcel ErrMsg: %s", err.Error()))
  682. }
  683. }()
  684. fileName := path.Base(filePath)
  685. var indexes []*models.FenweiExcelIndex
  686. var e error
  687. if strings.Contains(fileName, FenweiExcelNameA) {
  688. indexes, e = AnalysisFenweiNewExcelNormal(filePath, FenweiExcelNameA)
  689. } else if strings.Contains(fileName, FenweiExcelNameB) {
  690. indexes, e = AnalysisFenweiNewExcelNormal(filePath, FenweiExcelNameB)
  691. } else if strings.Contains(fileName, FenweiNewExcelNameC) {
  692. indexes, e = AnalysisFenweiNewExcelNormal(filePath, FenweiExcelNameC)
  693. } else if strings.Contains(fileName, FenweiNewExcelNameD) {
  694. indexes, e = AnalysisFenweiNewExcelNormal(filePath, FenweiExcelNameD)
  695. } else if strings.Contains(fileName, FenweiExcelNameE) {
  696. indexes, e = AnalysisFenweiExcelYangBen(filePath) // 样本焦化厂-增量数据和历史数据excel格式相同
  697. } else if strings.Contains(fileName, FenweiNewExcelNameF) {
  698. indexes, e = AnalysisFenweiNewExcelNormal(filePath, FenweiExcelNameF)
  699. } else if strings.Contains(fileName, FenweiNewExcelNameG) {
  700. indexes, e = AnalysisFenweiNewExcelNormal(filePath, FenweiExcelNameG)
  701. } else if strings.Contains(fileName, FenweiExcelNameH) {// 样本钢厂焦炭-增量数据和历史数据excel格式相同
  702. indexes, e = AnalysisFenweiExcelYangBenSteelCoal(filePath)
  703. } else {
  704. utils.FileLog.Info(fmt.Sprintf("文件名有误, FileName: %s", fileName))
  705. return
  706. }
  707. if e != nil {
  708. utils.FileLog.Info(fmt.Sprintf("获取excel数据失败, FileName: %s, Err: %s", fileName, e))
  709. return
  710. }
  711. if len(indexes) == 0 {
  712. utils.FileLog.Info(fmt.Sprintf("fileName: %s, index empty", fileName))
  713. return
  714. }
  715. // 写入数据
  716. params := make(map[string]interface{})
  717. params["List"] = indexes
  718. params["TerminalCode"] = utils.TerminalCode
  719. result, e := PostEdbLib(params, utils.LIB_ROUTE_FENWEI_HANDLE)
  720. if e != nil {
  721. b, _ := json.Marshal(params)
  722. utils.FileLog.Info(fmt.Sprintf("fileName: %s, PostEdbLib err: %s, params: %s", fileName, e.Error(), string(b)))
  723. return
  724. }
  725. resp := new(models.BaseEdbLibResponse)
  726. if e := json.Unmarshal(result, &resp); e != nil {
  727. utils.FileLog.Info(fmt.Sprintf("fileName: %s, json.Unmarshal err: %s", fileName, e.Error()))
  728. return
  729. }
  730. if resp.Ret != 200 {
  731. utils.FileLog.Info(fmt.Sprintf("fileName: %s, Msg: %s, ErrMsg: %s", fileName, resp.Msg, resp.ErrMsg))
  732. return
  733. }
  734. return
  735. }
  736. // AnalysisFenweiNewExcelNormal 增量数据-动力煤/焦煤/焦炭
  737. func AnalysisFenweiNewExcelNormal(filePath, excelName string) (indexes []*models.FenweiExcelIndex, err error) {
  738. defer func() {
  739. if err != nil {
  740. fmt.Printf("AnalysisFenweiNewExcelNormal ErrMsg: %s\n", err.Error())
  741. utils.FileLog.Info(fmt.Sprintf("AnalysisFenweiNewExcelNormal ErrMsg: %s", err.Error()))
  742. }
  743. }()
  744. xlsFile, e := xlsx.OpenFile(filePath)
  745. if e != nil {
  746. err = fmt.Errorf("OpenFile Err: %s", e.Error())
  747. return
  748. }
  749. // 获取存量指标, 增量数据只新增存量指标的数据
  750. originIndexes, e := GetFenweiIndexList()
  751. if e != nil {
  752. err = fmt.Errorf("GetFenweiIndexList err: %s", e.Error())
  753. return
  754. }
  755. indexMapping := make(map[string]models.BaseFromFenweiIndex)
  756. for _, v := range originIndexes {
  757. indexMapping[v.IndexCode] = v
  758. }
  759. diffNameReplace := FenweiNewDiffIndexNameReplaceMapping[excelName]
  760. ignoreIndexName := FenweiNewIgnoreIndexNameMapping[excelName]
  761. indexNameSuffix := FenweiNewIndexNameSuffixMapping[excelName]
  762. var dataRowMin, dataRowMax int // 数据起始行数
  763. if excelName == FenweiExcelNameA || excelName == FenweiExcelNameB {
  764. dataRowMin = 3
  765. dataRowMax = 6
  766. }
  767. if excelName == FenweiExcelNameC {
  768. dataRowMin = 3
  769. dataRowMax = 10
  770. }
  771. if excelName == FenweiExcelNameD {
  772. dataRowMin = 3
  773. dataRowMax = 13
  774. }
  775. var dataDate string
  776. secondRowNames := make(map[int]string) // 第二行-指标名称的上半部分
  777. thirdRowNames := make(map[int]string) // 第三行-指标名称的另一部分
  778. indexes = make([]*models.FenweiExcelIndex, 0)
  779. indexColIndex := make(map[string]*models.FenweiExcelIndex) // 行-列对应的指标
  780. rowProvince := make(map[int]string) // 行对应的省份名
  781. for _, sheet := range xlsFile.Sheet {
  782. maxRow := sheet.MaxRow
  783. for i := 0; i < maxRow; i++ {
  784. cells := sheet.Row(i).Cells
  785. // 首行为数据日期
  786. if i == 0 {
  787. for _, cell := range cells {
  788. str := strings.TrimSpace(cell.String())
  789. if str == "" {
  790. continue
  791. }
  792. dateArr := strings.Split(str, "-")
  793. if len(dateArr) != 2 {
  794. err = fmt.Errorf("数据日期有误, len(dateArr): %d", len(dateArr))
  795. return
  796. }
  797. t, e := time.ParseInLocation("2006.1.2", dateArr[1], time.Local)
  798. if e != nil {
  799. t, e = time.ParseInLocation("2006.01.02", dateArr[1], time.Local)
  800. if e != nil {
  801. err = fmt.Errorf("数据日期有误, date: %s", dateArr[1])
  802. return
  803. }
  804. }
  805. dataDate = t.Format(utils.FormatDate)
  806. break
  807. }
  808. }
  809. // 第二行-指标名称上半部分
  810. if i == 1 {
  811. for k, cell := range cells {
  812. text := strings.TrimSpace(cell.String())
  813. if k > 0 && text != "" {
  814. // 纵向合并
  815. if cell.VMerge > 0 {
  816. thirdRowNames[k] = text
  817. continue
  818. }
  819. secondRowNames[k] = text
  820. // 横向合并
  821. if cell.HMerge > 0 {
  822. for j := 1; j <= cell.HMerge; j++ {
  823. if secondRowNames[k+j] != "" {
  824. continue
  825. }
  826. secondRowNames[k+j] = text
  827. }
  828. }
  829. }
  830. }
  831. }
  832. // 第三行-指标名称下半部分, 指标名称, 指标编码在这一步生成
  833. if i == 2 {
  834. for k, cell := range cells {
  835. text := strings.TrimSpace(cell.String())
  836. if k > 0 && text != "" {
  837. thirdRowNames[k] = text
  838. }
  839. }
  840. }
  841. // 数据行
  842. if i >= dataRowMin && i <= dataRowMax {
  843. for k, cell := range cells {
  844. text := strings.TrimSpace(cell.String())
  845. if text == "" {
  846. continue
  847. }
  848. // 第一个单元格为地区
  849. if k == 0 {
  850. if text == "合计" && (excelName == FenweiExcelNameA || excelName == FenweiExcelNameB) {
  851. text = "三省合计"
  852. }
  853. rowProvince[i] = FenweiNewExcelProvinceMapping[text]
  854. continue
  855. }
  856. rowsName := strings.TrimSpace(fmt.Sprint(secondRowNames[k], thirdRowNames[k]))
  857. rowsName = strings.ReplaceAll(rowsName, "\n", "")
  858. namePrefix := diffNameReplace[rowsName]
  859. if namePrefix == "" {
  860. namePrefix = rowsName
  861. }
  862. // 忽略部分名称
  863. if utils.InArrayByStr(ignoreIndexName, namePrefix) {
  864. continue
  865. }
  866. indexName := fmt.Sprintf("%s%s%s", rowProvince[i], namePrefix, indexNameSuffix)
  867. //fmt.Printf("k: %d, %s\n", k, indexName)
  868. name := strings.ReplaceAll(indexName, "/", "")
  869. var middle, suffix string
  870. for _, p := range FenweiProvinces {
  871. // 此处山西其他可能会被错误的给鉴别为山西, 所以优先鉴别一下山西其他
  872. if strings.Contains(name, "山西其他") {
  873. middle = utils.GetFullPingYin("山西其他")
  874. name = strings.ReplaceAll(name, p, "")
  875. break
  876. }
  877. if strings.Contains(name, p) {
  878. middle = utils.GetFullPingYin(p)
  879. name = strings.ReplaceAll(name, p, "")
  880. break
  881. }
  882. }
  883. if middle == "" {
  884. utils.FileLog.Info(fmt.Sprintf("指标名称转换编码有误, IndexName: %s", indexName))
  885. continue
  886. }
  887. suffix = utils.GetFirstPingYin(name)
  888. indexCode := fmt.Sprint(FenweiIndexNamePrefix, middle, suffix)
  889. // 根据转换的编码, 匹配存量指标, 无则忽略
  890. originIndex := indexMapping[indexCode]
  891. if originIndex.FenweiIndexId <= 0 {
  892. utils.FileLog.Info(fmt.Sprintf("无对应存量指标, IndexName: %s, IndexCode: %s", indexName, indexCode))
  893. continue
  894. }
  895. indexKey := fmt.Sprintf("%d-%d", i, k)
  896. if indexColIndex[indexKey] == nil {
  897. indexColIndex[indexKey] = new(models.FenweiExcelIndex)
  898. indexColIndex[indexKey].TerminalCode = utils.TerminalCode
  899. indexColIndex[indexKey].ClassifyId = originIndex.ClassifyId
  900. indexColIndex[indexKey].Frequency = originIndex.Frequency
  901. indexColIndex[indexKey].Unit = originIndex.Unit
  902. indexColIndex[indexKey].IndexName = indexName
  903. indexColIndex[indexKey].IndexCode = indexCode
  904. indexColIndex[indexKey].ExcelDataMap = make(map[string]string)
  905. indexColIndex[indexKey].ExcelDataMap[dataDate] = text
  906. }
  907. }
  908. }
  909. }
  910. // 只读取第一个sheet
  911. break
  912. }
  913. for _, v := range indexColIndex {
  914. if v.IndexCode == "" {
  915. continue
  916. }
  917. indexes = append(indexes, v)
  918. }
  919. return
  920. }
  921. // AnalysisFenweiExcelYangBen 样本钢厂焦炭
  922. func AnalysisFenweiExcelYangBenSteelCoal(filePath string) (indexes []*models.FenweiExcelIndex, err error) {
  923. defer func() {
  924. if err != nil {
  925. fmt.Printf("AnalysisFenweiExcelYangBenSteel ErrMsg: %s\n", err.Error())
  926. utils.FileLog.Info(fmt.Sprintf("AnalysisFenweiExcelYangBenSteel ErrMsg: %s", err.Error()))
  927. }
  928. }()
  929. indexColIndex := make(map[int]*models.FenweiExcelIndex) // 列对应的指标(C列-D列)
  930. provinces := []string{"河北", "山东", "其他", "汇总"}
  931. for k, v := range provinces {
  932. //if k < 2 {
  933. // continue
  934. //}
  935. if indexColIndex[k] == nil {
  936. indexColIndex[k] = new(models.FenweiExcelIndex)
  937. indexColIndex[k].ExcelDataMap = make(map[string]string)
  938. indexColIndex[k].TerminalCode = utils.TerminalCode
  939. indexColIndex[k].ClassifyId = FenweiYangBenSteelCoalIndexClassifyId
  940. indexColIndex[k].Frequency = "周度"
  941. indexColIndex[k].Unit = "天"
  942. indexColIndex[k].IndexName = fmt.Sprintf("%s/%s", FenweiYangBenSteelCoalIndexNamePre, v)
  943. // 指标编码, 例:样本焦企炼焦煤库存可用天数/全国->fwsjybjqljmkckytsquanguo
  944. name := strings.ReplaceAll(indexColIndex[k].IndexName, "/", "")
  945. var middle, suffix string
  946. for _, p := range provinces {
  947. if strings.Contains(name, p) {
  948. suffix = utils.GetFullPingYin(p)
  949. name = strings.ReplaceAll(name, p, "")
  950. break
  951. }
  952. }
  953. if suffix == "" {
  954. utils.FileLog.Info(fmt.Sprintf("指标名称转换编码有误, IndexName: %s", indexColIndex[k].IndexName))
  955. continue
  956. }
  957. middle = utils.GetFirstPingYin(name)
  958. indexColIndex[k].IndexCode = fmt.Sprint(FenweiIndexNamePrefix, middle, suffix)
  959. }
  960. }
  961. maxCols := 4 // 每行只读取前4列即可
  962. startDateRow := 0 // 数据日期起始行(第一行), 后面依次6行读取
  963. startDataRow := 3 // 数据值起始行(第四行), 后面依次加1行读取
  964. sumDateRow := 0 // 数据日期累加的行, 每加4次重置0
  965. sumDataRow := 0 // 数据值累加的行, 每加4次重置0
  966. currDate := "" // 最近的一次日期
  967. stop := false // 结束读取excel
  968. xlsxFile, e := xlsx.OpenFile(filePath)
  969. if e != nil {
  970. err = fmt.Errorf("open xls file err: %s", e.Error())
  971. return
  972. }
  973. sheet := xlsxFile.Sheets[0]
  974. //if e != nil {
  975. // err = fmt.Errorf("GetSheet err: %s", e.Error())
  976. // return
  977. //}
  978. rows := sheet.Rows
  979. for i, row := range rows {
  980. sumDateRow += 1
  981. sumDataRow += 1
  982. // 第一个日期行/后续每4行取一次日期
  983. if i == startDateRow || sumDateRow == 7 {
  984. if stop {
  985. break
  986. }
  987. sumDateRow = 0
  988. for k, cell := range row.Cells {
  989. // 日期只取第一个单元格中的
  990. if k > 0 {
  991. break
  992. }
  993. strDates := strings.TrimSpace(cell.String())
  994. strDates = strings.ReplaceAll(strDates, "\x00", "")
  995. strDateArr := strings.Split(strDates, "-")
  996. // 正常情况下, 长度不为2说明读取到了最后一行了
  997. if len(strDateArr) != 2 {
  998. utils.FileLog.Info(fmt.Sprintf("第%d行数据日期长度有误, 结束读取", i+1))
  999. stop = true
  1000. break
  1001. }
  1002. t, e := time.Parse("2006.1.2", strDateArr[1])
  1003. if e != nil {
  1004. utils.FileLog.Info(fmt.Sprintf("第%d行数据日期有误, err: %s", i+1, e.Error()))
  1005. break
  1006. }
  1007. currDate = t.Format(utils.FormatDate)
  1008. }
  1009. }
  1010. // 第一个数据行/后续每4行取一次数据
  1011. if i == startDataRow || sumDataRow > 3 {
  1012. if currDate == "" {
  1013. utils.FileLog.Info(fmt.Sprintf("第%d行数据对应日期有误", i+1))
  1014. continue
  1015. }
  1016. for k, cell := range row.Cells {
  1017. if k >= maxCols {
  1018. break
  1019. }
  1020. if k == 2 {
  1021. val,_ := cell.Float()
  1022. // 保留四位小数
  1023. provinceIndex := 0
  1024. if sumDataRow > 3 {
  1025. provinceIndex = sumDataRow - 4
  1026. }
  1027. indexColIndex[provinceIndex].ExcelDataMap[currDate] = fmt.Sprintf("%.4f", val)
  1028. if sumDataRow == 7 {
  1029. sumDataRow = 0
  1030. // 重置日期
  1031. currDate = ""
  1032. }
  1033. break
  1034. }
  1035. }
  1036. }
  1037. }
  1038. indexes = make([]*models.FenweiExcelIndex, 0)
  1039. for _, v := range indexColIndex {
  1040. if v.IndexCode == "" {
  1041. continue
  1042. }
  1043. indexes = append(indexes, v)
  1044. }
  1045. return
  1046. }