commodity_coal.go 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_index_lib/models"
  5. "eta_gn/eta_index_lib/utils"
  6. "fmt"
  7. "github.com/mozillazg/go-pinyin"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // 煤炭网
  14. type CoalMineDataController struct {
  15. BaseAuthController
  16. }
  17. // @Title 刷新数据
  18. // @Description 刷新数据接口
  19. // @Param request body models.CoalMineDataReq true "type json string"
  20. // @Success 200 {object} models.EdbClassify
  21. // @router /jsm/history [post]
  22. func (this *CoalMineDataController) JsmHistory() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. var req models.CoalMineDataReq
  29. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  30. if err != nil {
  31. br.Msg = "参数解析异常!"
  32. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  33. return
  34. }
  35. //path := "/home/code/python/coal_mail/emailFile/沿海八省动力煤终端用户供耗存数据更新.xlsx"
  36. var mappingItems []*models.BaseFromCoalmineMapping
  37. var indexItems []*models.BaseFromCoalmineJsmIndex
  38. var codeMap = make(map[string]string)
  39. var indexMap = make(map[string]string)
  40. var nameMap = make(map[int]string)
  41. var coalInfoMap = make(map[string][]string)
  42. var dataTime string
  43. var unit string
  44. var province string
  45. var exchange string
  46. codeList, err := models.GetBaseFromCoalmineMapping()
  47. if err != nil && err.Error() != utils.ErrNoRow() {
  48. utils.FileLog.Info("获取煤炭指标失败:", err)
  49. return
  50. }
  51. if len(codeList) > 0 {
  52. for _, v := range codeList {
  53. codeMap[v.IndexName] = v.IndexCode
  54. }
  55. }
  56. indexCompanyList, err := models.GetBaseFromCoalmineIndex()
  57. if err != nil && err.Error() != utils.ErrNoRow() {
  58. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  59. return
  60. }
  61. if len(indexCompanyList) > 0 {
  62. for _, v := range indexCompanyList {
  63. indexMap[v.IndexName+v.DataTime] = v.DealValue
  64. }
  65. }
  66. sheet := req.SheetData[0]
  67. //遍历行读取
  68. maxCol := sheet.MaxRow
  69. for i := 0; i < maxCol; i++ {
  70. if i == 0 {
  71. row := sheet.Rows[i]
  72. cells := row.Cells
  73. for k, cell := range cells {
  74. text := cell.Value
  75. if k > 0 {
  76. switch k {
  77. case 1:
  78. province = "山西"
  79. text = strings.TrimLeft(text, "山西")
  80. case 4:
  81. province = "内蒙古"
  82. text = strings.TrimLeft(text, "内蒙古")
  83. case 7:
  84. province = "陕西"
  85. text = strings.TrimLeft(text, "陕西")
  86. case 10:
  87. province = "442家"
  88. text = "产量"
  89. }
  90. if k != 1 && k != 4 && k != 7 && k != 10 {
  91. unit = "%"
  92. } else {
  93. unit = "万吨"
  94. }
  95. exchange = text
  96. text = province + text
  97. var item models.BaseFromCoalmineMapping
  98. item.IndexName = text
  99. nameMap[k] = item.IndexName
  100. coalInfoMap[item.IndexName] = append(coalInfoMap[item.IndexName], exchange, province, unit)
  101. //code
  102. exists := ContainsSpecialName(province)
  103. var code string
  104. if exists {
  105. abbr := trimProvinceName(text)
  106. provinceAbbr := trimProvinceName(province)
  107. //取处理后公司名首字母缩写
  108. a := pinyin.NewArgs()
  109. rows := pinyin.Pinyin(abbr, a)
  110. for i := 0; i < len(rows); i++ {
  111. if len(rows[i]) != 0 {
  112. str := rows[i][0]
  113. pi := str[0:1]
  114. code += pi
  115. }
  116. }
  117. item.IndexCode = provinceAbbr + code + "jsm"
  118. } else {
  119. a := pinyin.NewArgs()
  120. rows := pinyin.Pinyin(text, a)
  121. for i := 0; i < len(rows); i++ {
  122. if len(rows[i]) != 0 {
  123. str := rows[i][0]
  124. pi := str[0:1]
  125. code += pi
  126. }
  127. }
  128. item.IndexCode = code + "jsm"
  129. }
  130. item.CreateTime = time.Now()
  131. mappingItems = append(mappingItems, &item)
  132. }
  133. }
  134. }
  135. if i > 0 {
  136. row := sheet.Rows[i]
  137. cells := row.Cells
  138. for k, cell := range cells {
  139. var item models.BaseFromCoalmineJsmIndex
  140. if k == 0 {
  141. text := cell.Value
  142. parsedTime, err := time.Parse("01-02-06", text)
  143. if err != nil {
  144. fmt.Println("解析时间字符串出错:", err)
  145. return
  146. }
  147. dataTime = parsedTime.Format(utils.FormatDate)
  148. }
  149. if k > 0 {
  150. text := cell.Value
  151. item.IndexName = nameMap[k]
  152. item.IndexCode = codeMap[nameMap[k]]
  153. item.DealValue = text
  154. item.Source = "三省周度"
  155. item.DataTime = dataTime
  156. item.Frequency = "周度"
  157. item.ModifyTime = time.Now()
  158. item.CreateTime = time.Now()
  159. if len(coalInfoMap[item.IndexName]) == 3 {
  160. item.Exchange = coalInfoMap[item.IndexName][0]
  161. item.Province = coalInfoMap[item.IndexName][1]
  162. item.Unit = coalInfoMap[item.IndexName][2]
  163. }
  164. indexItems = append(indexItems, &item)
  165. }
  166. }
  167. }
  168. }
  169. ////添加数据到数据库
  170. for _, v := range mappingItems {
  171. if codeMap[v.IndexName] == "" {
  172. codeMap[v.IndexName] = v.IndexCode
  173. newId, err := models.AddBaseFromCoalmineMapping(v)
  174. if err != nil {
  175. for i := 0; i < 10; i++ {
  176. v.IndexCode = v.IndexCode + strconv.Itoa(i)
  177. codeMap[v.IndexName] = v.IndexCode
  178. newId, err := models.AddBaseFromCoalmineMapping(v)
  179. if err != nil {
  180. fmt.Println("再次添加公司指标名称错误", err)
  181. continue
  182. } else {
  183. fmt.Println("新增公司成功", newId)
  184. break
  185. }
  186. }
  187. } else {
  188. fmt.Println("新增公司成功", newId)
  189. }
  190. }
  191. }
  192. //fmt.Println("指标操作完成")
  193. //给indexItem中的code赋值并插入index表
  194. for _, v := range indexItems {
  195. v.IndexCode = codeMap[v.IndexName]
  196. if indexMap[v.IndexName+v.DataTime] == "" && v.DealValue != "" {
  197. newId, err := models.AddBaseFromCoalmineIndex(v)
  198. if err != nil {
  199. fmt.Println("添加数据错误", err)
  200. } else {
  201. fmt.Println("新增成功", newId)
  202. }
  203. } else {
  204. if indexMap[v.IndexName+v.DataTime] != v.DealValue && v.DealValue != "" {
  205. err = models.UpdateBaseFromCoalmineIndex(v)
  206. if err != nil {
  207. fmt.Println("修改数据错误错误", err)
  208. return
  209. }
  210. }
  211. }
  212. }
  213. br.Ret = 200
  214. br.Success = true
  215. br.Msg = "获取成功"
  216. return
  217. }
  218. // @Title 刷新数据
  219. // @Description 刷新数据接口
  220. // @Param request body models.CoalMineDataReq true "type json string"
  221. // @Success 200 {object} models.EdbClassify
  222. // @router /coastal/history [post]
  223. func (this *CoalMineDataController) CoastalHistory() {
  224. br := new(models.BaseResponse).Init()
  225. defer func() {
  226. this.Data["json"] = br
  227. this.ServeJSON()
  228. }()
  229. var req models.CoalMineDataReq
  230. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  231. if err != nil {
  232. br.Msg = "参数解析异常!"
  233. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  234. return
  235. }
  236. var mappingItems []*models.BaseFromCoalmineMapping
  237. var indexItems []*models.BaseFromCoalmineCoastalIndex
  238. var codeMap = make(map[string]string)
  239. var indexMap = make(map[string]string)
  240. var nameMap = make(map[int]string)
  241. var groupMap = make(map[int]string)
  242. var dataTime string
  243. var unit string
  244. codeList, err := models.GetBaseFromCoalmineMapping()
  245. if err != nil && err.Error() != utils.ErrNoRow() {
  246. utils.FileLog.Info("获取煤炭指标失败:", err)
  247. return
  248. }
  249. if len(codeList) > 0 {
  250. for _, v := range codeList {
  251. codeMap[v.IndexName] = v.IndexCode
  252. }
  253. }
  254. indexCompanyList, err := models.GetBaseFromCoalmineCoastalIndex()
  255. if err != nil && err.Error() != utils.ErrNoRow() {
  256. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  257. return
  258. }
  259. if len(indexCompanyList) > 0 {
  260. for _, v := range indexCompanyList {
  261. indexMap[v.IndexName+v.DataTime] = v.DealValue
  262. }
  263. }
  264. for ii, sheet := range req.SheetData {
  265. //遍历行读取
  266. maxCol := len(sheet.Rows)
  267. for i := 0; i < maxCol; i++ {
  268. if i == 0 {
  269. row := sheet.Rows[i]
  270. cells := row.Cells
  271. for k, cell := range cells {
  272. ignore := 1
  273. ignore2 := 10
  274. if ii == 3 {
  275. // 第四页供煤没有星期,所以只跳过第一行
  276. ignore = 0
  277. ignore2 = 9
  278. }
  279. if k > ignore && k < ignore2 {
  280. text := cell.Value
  281. text = strings.Replace(text, ":", "", -1)
  282. text = strings.Replace(text, ":", "", -1)
  283. if ii == 3 {
  284. text += "供煤"
  285. }
  286. if ii == 2 {
  287. text += "天数"
  288. }
  289. province := strings.Replace(text, "日耗", "", -1)
  290. province = strings.Replace(province, "库存", "", -1)
  291. province = strings.Replace(province, "可用天数", "", -1)
  292. province = strings.Replace(province, "供煤", "", -1)
  293. groupMap[k] = province
  294. var item models.BaseFromCoalmineMapping
  295. item.IndexName = text
  296. //合计命名
  297. nameMap[k] = item.IndexName
  298. //code
  299. exists := ContainsSpecialName(text)
  300. var code string
  301. if exists {
  302. abbr := trimProvinceName(text)
  303. //取处理后公司名首字母缩写
  304. a := pinyin.NewArgs()
  305. rows := pinyin.Pinyin(abbr, a)
  306. for i := 0; i < len(rows); i++ {
  307. if len(rows[i]) != 0 {
  308. str := rows[i][0]
  309. pi := str[0:1]
  310. code += pi
  311. }
  312. }
  313. item.IndexCode = abbr + "coastal"
  314. } else {
  315. a := pinyin.NewArgs()
  316. rows := pinyin.Pinyin(item.IndexName, a)
  317. for i := 0; i < len(rows); i++ {
  318. if len(rows[i]) != 0 {
  319. str := rows[i][0]
  320. pi := str[0:1]
  321. code += pi
  322. }
  323. }
  324. item.IndexCode = code + "coastal"
  325. }
  326. item.CreateTime = time.Now()
  327. mappingItems = append(mappingItems, &item)
  328. }
  329. if ii == 2 {
  330. unit = "天"
  331. } else {
  332. unit = "万吨"
  333. }
  334. }
  335. }
  336. if i > 0 {
  337. row := sheet.Rows[i]
  338. cells := row.Cells
  339. for k, cell := range cells {
  340. ingore := 10
  341. if ii == 3 {
  342. ingore = 9
  343. }
  344. if k < ingore {
  345. var item models.BaseFromCoalmineCoastalIndex
  346. if k == 0 {
  347. text := cell.Value
  348. if text == "" {
  349. continue
  350. }
  351. if len(text) < 6 {
  352. dataTime = ExcelDateToDate(text).Format(utils.FormatDate)
  353. } else {
  354. parsedTime, err := time.Parse("01-02-06", text)
  355. if err != nil {
  356. parsedTime, err = time.Parse("2006/01/02", text)
  357. if err != nil {
  358. fmt.Println("解析时间字符串出错:", err)
  359. return
  360. }
  361. }
  362. dataTime = parsedTime.Format(utils.FormatDate)
  363. }
  364. }
  365. ignore := 1
  366. if ii == 3 {
  367. // 第四页供煤没有星期,所以只跳过第一行
  368. ignore = 0
  369. }
  370. if k > ignore {
  371. text := cell.Value
  372. item.IndexName = nameMap[k]
  373. item.IndexCode = codeMap[nameMap[k]]
  374. item.DealValue = text
  375. item.GroupName = groupMap[k]
  376. item.Source = "沿海八省"
  377. item.DataTime = dataTime
  378. item.Frequency = "日度"
  379. item.ModifyTime = time.Now()
  380. item.CreateTime = time.Now()
  381. item.Unit = unit
  382. indexItems = append(indexItems, &item)
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }
  389. //添加数据到数据库
  390. for _, v := range mappingItems {
  391. if codeMap[v.IndexName] == "" {
  392. codeMap[v.IndexName] = v.IndexCode
  393. newId, err := models.AddBaseFromCoalmineMapping(v)
  394. if err != nil {
  395. for i := 0; i < 10; i++ {
  396. v.IndexCode = v.IndexCode + strconv.Itoa(i)
  397. codeMap[v.IndexName] = v.IndexCode
  398. newId, err := models.AddBaseFromCoalmineMapping(v)
  399. if err != nil {
  400. fmt.Println("再次添加公司指标名称错误", err)
  401. continue
  402. } else {
  403. fmt.Println("新增公司成功", newId)
  404. break
  405. }
  406. }
  407. } else {
  408. fmt.Println("新增公司成功", newId)
  409. }
  410. }
  411. }
  412. fmt.Println("指标操作完成")
  413. //给indexItem中的code赋值并插入index表
  414. for _, v := range indexItems {
  415. v.IndexCode = codeMap[v.IndexName]
  416. if indexMap[v.IndexName+v.DataTime] == "" && v.DealValue != "" {
  417. newId, err := models.AddBaseFromCoalCoastalIndex(v)
  418. if err != nil {
  419. fmt.Println("添加数据错误", err)
  420. } else {
  421. fmt.Println("新增成功", newId)
  422. }
  423. } else {
  424. if indexMap[v.IndexName+v.DataTime] != v.DealValue && v.DealValue != "" {
  425. err = models.UpdateBaseFromCoalCoastalIndex(v)
  426. if err != nil {
  427. fmt.Println("修改数据错误错误", err)
  428. return
  429. }
  430. }
  431. }
  432. }
  433. br.Ret = 200
  434. br.Success = true
  435. br.Msg = "获取成功"
  436. return
  437. }
  438. // @Title 刷新数据
  439. // @Description 刷新数据接口
  440. // @Param request body models.CoalMineDataReq true "type json string"
  441. // @Success 200 {object} models.EdbClassify
  442. // @router /inland/history [post]
  443. func (this *CoalMineDataController) InlandHistory() {
  444. br := new(models.BaseResponse).Init()
  445. defer func() {
  446. this.Data["json"] = br
  447. this.ServeJSON()
  448. }()
  449. var req models.CoalMineDataReq
  450. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  451. if err != nil {
  452. br.Msg = "参数解析异常!"
  453. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  454. return
  455. }
  456. var mappingItems []*models.BaseFromCoalmineMapping
  457. var indexItems []*models.BaseFromCoalmineInlandIndex
  458. var codeMap = make(map[string]string)
  459. var indexMap = make(map[string]string)
  460. var nameMap = make(map[int]string)
  461. var groupMap = make(map[int]string)
  462. var dataTime string
  463. var sheetName string
  464. codeList, err := models.GetBaseFromCoalmineMapping()
  465. if err != nil && err.Error() != utils.ErrNoRow() {
  466. utils.FileLog.Info("获取煤炭指标失败:", err)
  467. return
  468. }
  469. if len(codeList) > 0 {
  470. for _, v := range codeList {
  471. codeMap[v.IndexName] = v.IndexCode
  472. }
  473. }
  474. indexCompanyList, err := models.GetBaseFromCoalmineInlandIndex()
  475. if err != nil && err.Error() != utils.ErrNoRow() {
  476. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  477. return
  478. }
  479. if len(indexCompanyList) > 0 {
  480. for _, v := range indexCompanyList {
  481. indexMap[v.IndexName+v.DataTime] = v.DealValue
  482. }
  483. }
  484. for _, sheet := range req.SheetData {
  485. sheetName = sheet.Name
  486. fmt.Println("sheetName:", sheetName)
  487. var unit string
  488. sheetName = strings.TrimLeft(sheetName, "内陆")
  489. if sheetName == "可用天数" {
  490. unit = "天"
  491. } else {
  492. unit = "万吨"
  493. }
  494. //遍历行读取
  495. maxCol := sheet.MaxRow
  496. for i := 0; i < maxCol; i++ {
  497. if i == 0 {
  498. row := sheet.Rows[i]
  499. cells := row.Cells
  500. for k, cell := range cells {
  501. if k > 0 && k < 18 {
  502. text := cell.Value
  503. groupMap[k] = text
  504. var item models.BaseFromCoalmineMapping
  505. //合计命名
  506. if k == 9 {
  507. item.IndexName = sheetName
  508. }
  509. item.IndexName = text + sheetName
  510. nameMap[k] = item.IndexName
  511. //code
  512. exists := ContainsSpecialName(text)
  513. var code string
  514. if exists {
  515. abbr := trimProvinceName(text)
  516. //取处理后公司名首字母缩写
  517. a := pinyin.NewArgs()
  518. rows := pinyin.Pinyin(sheetName, a)
  519. for i := 0; i < len(rows); i++ {
  520. if len(rows[i]) != 0 {
  521. str := rows[i][0]
  522. pi := str[0:1]
  523. code += pi
  524. }
  525. }
  526. item.IndexCode = abbr + code + "inland"
  527. } else {
  528. a := pinyin.NewArgs()
  529. rows := pinyin.Pinyin(text+sheetName, a)
  530. for i := 0; i < len(rows); i++ {
  531. if len(rows[i]) != 0 {
  532. str := rows[i][0]
  533. pi := str[0:1]
  534. code += pi
  535. }
  536. }
  537. item.IndexCode = code + "inland"
  538. }
  539. item.CreateTime = time.Now()
  540. mappingItems = append(mappingItems, &item)
  541. }
  542. }
  543. }
  544. if i > 0 {
  545. row := sheet.Rows[i]
  546. cells := row.Cells
  547. for k, cell := range cells {
  548. if k < 18 {
  549. var item models.BaseFromCoalmineInlandIndex
  550. if k == 0 {
  551. text := cell.Value
  552. if text == "" {
  553. continue
  554. }
  555. if len(text) < 6 {
  556. dataTime = ExcelDateToDate(text).Format(utils.FormatDate)
  557. } else {
  558. parsedTime, err := time.Parse("01-02-06", text)
  559. if err != nil {
  560. parsedTime, err = time.Parse("2006/01/02", text)
  561. if err != nil {
  562. fmt.Println("解析时间字符串出错:", err)
  563. return
  564. }
  565. }
  566. dataTime = parsedTime.Format(utils.FormatDate)
  567. }
  568. }
  569. if k > 0 {
  570. text := cell.Value
  571. item.IndexName = nameMap[k]
  572. item.IndexCode = codeMap[nameMap[k]]
  573. item.DealValue = text
  574. item.GroupName = groupMap[k]
  575. item.Source = "内陆十七省"
  576. item.DataTime = dataTime
  577. item.Unit = unit
  578. item.Frequency = "日度"
  579. item.ModifyTime = time.Now()
  580. item.CreateTime = time.Now()
  581. }
  582. indexItems = append(indexItems, &item)
  583. }
  584. }
  585. }
  586. }
  587. }
  588. //添加数据到数据库
  589. for _, v := range mappingItems {
  590. if codeMap[v.IndexName] == "" {
  591. codeMap[v.IndexName] = v.IndexCode
  592. newId, err := models.AddBaseFromCoalmineMapping(v)
  593. if err != nil {
  594. for i := 0; i < 10; i++ {
  595. v.IndexCode = v.IndexCode + strconv.Itoa(i)
  596. codeMap[v.IndexName] = v.IndexCode
  597. newId, err := models.AddBaseFromCoalmineMapping(v)
  598. if err != nil {
  599. fmt.Println("再次添加公司指标名称错误", err)
  600. continue
  601. } else {
  602. fmt.Println("新增公司成功", newId)
  603. break
  604. }
  605. }
  606. } else {
  607. fmt.Println("新增公司成功", newId)
  608. }
  609. }
  610. }
  611. fmt.Println("指标操作完成")
  612. //给indexItem中的code赋值并插入index表
  613. for _, v := range indexItems {
  614. v.IndexCode = codeMap[v.IndexName]
  615. if indexMap[v.IndexName+v.DataTime] == "" && v.DealValue != "" {
  616. newId, err := models.AddBaseFromCoalInlandIndex(v)
  617. if err != nil {
  618. fmt.Println("添加数据错误", err)
  619. } else {
  620. fmt.Println("新增成功", newId)
  621. }
  622. } else {
  623. if indexMap[v.IndexName+v.DataTime] != v.DealValue && v.DealValue != "" {
  624. err = models.UpdateBaseFromCoalInlandIndex(v)
  625. if err != nil {
  626. fmt.Println("修改数据错误错误", err)
  627. return
  628. }
  629. }
  630. }
  631. }
  632. br.Ret = 200
  633. br.Success = true
  634. br.Msg = "获取成功"
  635. return
  636. }
  637. // @Title 晋陕蒙数据
  638. // @Description 刷新晋陕蒙数据接口
  639. // @Param request body models.CoalMineDataReq true "type json string"
  640. // @Success 200 {object} models.EdbClassify
  641. // @router /jsm [post]
  642. func (this *CoalMineDataController) Jsm() {
  643. br := new(models.BaseResponse).Init()
  644. defer func() {
  645. this.Data["json"] = br
  646. this.ServeJSON()
  647. }()
  648. var req models.CoalMineDataReq
  649. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  650. if err != nil {
  651. br.Msg = "参数解析异常!"
  652. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  653. return
  654. }
  655. //path := "/home/code/python/coal_mail/emailFile/沿海八省动力煤终端用户供耗存数据更新.xlsx"
  656. var province string
  657. var description string
  658. var exchange string
  659. var city string
  660. var companyName string
  661. var group string
  662. var dateMap = make(map[int]string)
  663. var codeMap = make(map[string]string)
  664. var indexMap = make(map[string]string)
  665. var codeCompanyMap = make(map[string]string)
  666. var indexCompanyMap = make(map[string]string)
  667. var items []*models.BaseFromCoalmineMapping
  668. var itemsCompany []*models.BaseFromCoalmineMapping
  669. var itemsIndex []*models.BaseFromCoalmineJsmIndex
  670. var itemsCompanyIndex []*models.BaseFromCoalmineCompanyIndex
  671. codeList, err := models.GetBaseFromCoalmineMapping()
  672. if err != nil && err.Error() != utils.ErrNoRow() {
  673. utils.FileLog.Info("获取煤炭指标失败:", err)
  674. return
  675. }
  676. if len(codeList) > 0 {
  677. for _, v := range codeList {
  678. codeMap[v.IndexName] = v.IndexCode
  679. }
  680. }
  681. codeCompanyList, err := models.GetBaseFromCoalmineMapping()
  682. if err != nil && err.Error() != utils.ErrNoRow() {
  683. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  684. return
  685. }
  686. if len(codeCompanyList) > 0 {
  687. for _, v := range codeCompanyList {
  688. codeCompanyMap[v.IndexName] = v.IndexCode
  689. }
  690. }
  691. indexList, err := models.GetBaseFromCoalmineIndex()
  692. if err != nil && err.Error() != utils.ErrNoRow() {
  693. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  694. return
  695. }
  696. if len(indexList) > 0 {
  697. for _, v := range indexList {
  698. indexMap[v.IndexName+v.DataTime] = v.DealValue
  699. }
  700. }
  701. indexCompanyList, err := models.GetBaseFromCoalmineCompanyIndex()
  702. if err != nil && err.Error() != utils.ErrNoRow() {
  703. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  704. return
  705. }
  706. if len(indexCompanyList) > 0 {
  707. for _, v := range indexCompanyList {
  708. indexCompanyMap[v.IndexName+v.DataTime] = v.DealValue
  709. }
  710. }
  711. for _, sheet := range req.SheetData {
  712. //遍历行读取
  713. maxRow := sheet.MaxRow
  714. for i := 0; i < maxRow; i++ {
  715. //获取样本情况
  716. if i > 3 && i < 16 {
  717. row := sheet.Rows[i]
  718. cells := row.Cells
  719. Loop2:
  720. for k, cell := range cells {
  721. text := cell.Value
  722. if text != "" {
  723. if k == 1 {
  724. province = text
  725. }
  726. if k == 3 {
  727. description = text
  728. }
  729. if k == 4 {
  730. item := new(models.BaseFromCoalmineMapping)
  731. exchange = text
  732. exists := ContainsSpecialName(province)
  733. var strResult string
  734. if exists {
  735. abbr := TrimProvinceName(province)
  736. //取处理后公司名首字母缩写
  737. a := pinyin.NewArgs()
  738. rows := pinyin.Pinyin(exchange, a)
  739. for i := 0; i < len(rows); i++ {
  740. if len(rows[i]) != 0 {
  741. str := rows[i][0]
  742. pi := str[0:1]
  743. strResult += pi
  744. }
  745. }
  746. item.IndexCode = abbr + strResult + "jsm"
  747. } else {
  748. //取处理后公司名首字母缩写
  749. a := pinyin.NewArgs()
  750. rows := pinyin.Pinyin(province+exchange, a)
  751. for i := 0; i < len(rows); i++ {
  752. if len(rows[i]) != 0 {
  753. str := rows[i][0]
  754. pi := str[0:1]
  755. strResult += pi
  756. }
  757. }
  758. item.IndexCode = strResult + "jsm"
  759. }
  760. item.IndexName = province + exchange
  761. item.CreateTime = time.Now()
  762. items = append(items, item)
  763. break Loop2
  764. }
  765. }
  766. }
  767. }
  768. //获取日期
  769. if i == 3 {
  770. row := sheet.Rows[i]
  771. cells := row.Cells
  772. for k, cell := range cells {
  773. text := cell.Value
  774. stamp, _ := time.ParseInLocation("01-02-06", text, time.Now().Location())
  775. if k > 4 {
  776. dateMap[k] = stamp.Format(utils.FormatDate)
  777. }
  778. }
  779. }
  780. //获取具体产量
  781. if i > 3 && i < 16 {
  782. row := sheet.Rows[i]
  783. cells := row.Cells
  784. for k, cell := range cells {
  785. text := cell.Value
  786. if k > 4 {
  787. item := new(models.BaseFromCoalmineJsmIndex)
  788. item.IndexName = province + exchange
  789. item.IndexCode = codeMap[item.IndexName]
  790. item.Exchange = exchange
  791. item.DealValue = text
  792. item.DataTime = dateMap[k]
  793. item.Exchange = exchange
  794. item.Description = description
  795. item.Province = province
  796. item.Source = "三省周度"
  797. if exchange == "产量" {
  798. item.Unit = "万吨"
  799. } else {
  800. item.Unit = "百分比"
  801. }
  802. item.Frequency = "周度"
  803. item.ModifyTime = time.Now()
  804. item.CreateTime = time.Now()
  805. itemsIndex = append(itemsIndex, item)
  806. }
  807. }
  808. }
  809. //获取公司指标名称
  810. if i > 17 {
  811. row := sheet.Rows[i]
  812. cells := row.Cells
  813. Loop3:
  814. for k, cell := range cells {
  815. text := cell.Value
  816. if text != "" {
  817. if k == 1 {
  818. province = text
  819. }
  820. if k == 2 {
  821. city = text
  822. }
  823. if k == 3 {
  824. companyName = text
  825. }
  826. if k == 4 {
  827. item := new(models.BaseFromCoalmineMapping)
  828. group = text
  829. trimName := TrimCompanyName(companyName)
  830. item.IndexName = trimName
  831. //取处理后公司名首字母缩写
  832. strResult := ""
  833. a := pinyin.NewArgs()
  834. rows := pinyin.Pinyin(trimName, a)
  835. for i := 0; i < len(rows); i++ {
  836. if len(rows[i]) != 0 {
  837. str := rows[i][0]
  838. pi := str[0:1]
  839. strResult += pi
  840. }
  841. }
  842. item.IndexCode = strResult + "company"
  843. item.CreateTime = time.Now()
  844. itemsCompany = append(itemsCompany, item)
  845. break Loop3
  846. }
  847. }
  848. }
  849. }
  850. //获取公司具体产量
  851. if i > 18 {
  852. row := sheet.Rows[i]
  853. cells := row.Cells
  854. for k, cell := range cells {
  855. text := cell.Value
  856. if k > 4 {
  857. item := new(models.BaseFromCoalmineCompanyIndex)
  858. companyName = TrimCompanyName(companyName)
  859. item.IndexName = companyName
  860. item.IndexCode = codeCompanyMap[item.IndexName]
  861. item.DealValue = text
  862. item.DataTime = dateMap[k]
  863. item.Province = province
  864. item.City = city
  865. //处理无类别名时的情况
  866. if group == "无" {
  867. item.GroupName = companyName
  868. } else {
  869. item.GroupName = group
  870. }
  871. item.Source = "三省企业"
  872. item.Unit = "万吨"
  873. item.Frequency = "周度"
  874. item.ModifyTime = time.Now()
  875. item.CreateTime = time.Now()
  876. //fmt.Println(item)
  877. itemsCompanyIndex = append(itemsCompanyIndex, item)
  878. }
  879. }
  880. }
  881. }
  882. }
  883. //添加数据到数据库
  884. for _, v := range items {
  885. if codeMap[v.IndexName] == "" {
  886. codeMap[v.IndexName] = v.IndexCode
  887. newId, err := models.AddBaseFromCoalmineMapping(v)
  888. if err != nil {
  889. fmt.Println("添加指标名称错误")
  890. } else {
  891. fmt.Println("添加指标名称成功", newId)
  892. }
  893. }
  894. }
  895. fmt.Println("指标操作完成")
  896. for _, v := range itemsIndex {
  897. v.IndexCode = codeMap[v.IndexName]
  898. if indexMap[v.IndexName+v.DataTime] == "" && v.DealValue != "" {
  899. newId, err := models.AddBaseFromCoalmineIndex(v)
  900. if err != nil {
  901. fmt.Println("添加数据错误", err)
  902. } else {
  903. fmt.Println("新增成功", newId)
  904. }
  905. } else {
  906. if indexMap[v.IndexName+v.DataTime] != v.DealValue && v.DealValue != "" {
  907. err = models.UpdateBaseFromCoalmineIndex(v)
  908. if err != nil {
  909. fmt.Println("修改数据错误错误", err)
  910. return
  911. }
  912. }
  913. }
  914. }
  915. fmt.Println("数据操作完成")
  916. for _, v := range itemsCompany {
  917. if codeCompanyMap[v.IndexName] == "" {
  918. codeCompanyMap[v.IndexName] = v.IndexCode
  919. newId, err := models.AddBaseFromCoalmineMapping(v)
  920. if err != nil {
  921. for i := 0; i < 10; i++ {
  922. v.IndexCode = v.IndexCode + strconv.Itoa(i)
  923. codeCompanyMap[v.IndexName] = v.IndexCode
  924. newId, err := models.AddBaseFromCoalmineMapping(v)
  925. if err != nil {
  926. fmt.Println("再次添加公司指标名称错误", err)
  927. fmt.Println(v.IndexName, v.IndexCode)
  928. continue
  929. } else {
  930. fmt.Println("新增公司成功", newId)
  931. break
  932. }
  933. }
  934. } else {
  935. fmt.Println("新增公司成功", newId)
  936. }
  937. }
  938. }
  939. fmt.Println("公司指标操作完成")
  940. for _, v := range itemsCompanyIndex {
  941. v.IndexCode = codeCompanyMap[v.IndexName]
  942. if indexCompanyMap[v.IndexName+v.DataTime] == "" && v.DealValue != "" {
  943. newId, err := models.AddBaseFromCoalmineCompanyIndex(v)
  944. if err != nil {
  945. fmt.Println("添加公司数据错误", err)
  946. } else {
  947. fmt.Println("新增公司数据成功", newId)
  948. }
  949. } else {
  950. if indexCompanyMap[v.IndexName+v.DataTime] != v.DealValue && v.DealValue != "" {
  951. err = models.UpdateBaseFromCoalmineCompanyIndex(v)
  952. if err != nil {
  953. fmt.Println("修改数据错误错误", err)
  954. }
  955. }
  956. }
  957. }
  958. br.Ret = 200
  959. br.Success = true
  960. br.Msg = "获取成功"
  961. return
  962. }
  963. // @Title 沿海数据
  964. // @Description 刷新沿海数据接口
  965. // @Param request body models.CoalMineDataReq true "type json string"
  966. // @Success 200 {object} models.EdbClassify
  967. // @router /coastal [post]
  968. func (this *CoalMineDataController) Coastal() {
  969. br := new(models.BaseResponse).Init()
  970. defer func() {
  971. this.Data["json"] = br
  972. this.ServeJSON()
  973. }()
  974. var req models.CoalMineDataReq
  975. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  976. if err != nil {
  977. br.Msg = "参数解析异常!"
  978. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  979. return
  980. }
  981. var mappingItems []*models.BaseFromCoalmineMapping
  982. var indexItems []*models.BaseFromCoalmineCoastalIndex
  983. var codeMap = make(map[string]string)
  984. var indexMap = make(map[string]string)
  985. var nameMap = make(map[int]string)
  986. var groupMap = make(map[int]string)
  987. var dataTime string
  988. var sheetName, unit string
  989. codeList, err := models.GetBaseFromCoalmineMapping()
  990. if err != nil && err.Error() != utils.ErrNoRow() {
  991. utils.FileLog.Info("获取煤炭指标失败:", err)
  992. return
  993. }
  994. if len(codeList) > 0 {
  995. for _, v := range codeList {
  996. codeMap[v.IndexName] = v.IndexCode
  997. }
  998. }
  999. indexCompanyList, err := models.GetBaseFromCoalmineCoastalIndex()
  1000. if err != nil && err.Error() != utils.ErrNoRow() {
  1001. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  1002. return
  1003. }
  1004. if len(indexCompanyList) > 0 {
  1005. for _, v := range indexCompanyList {
  1006. indexMap[v.IndexName+v.DataTime] = v.DealValue
  1007. }
  1008. }
  1009. for _, sheet := range req.SheetData {
  1010. sheetName = sheet.Name
  1011. //遍历行读取
  1012. maxRow := sheet.MaxRow
  1013. for i := 0; i < maxRow; i++ {
  1014. if i == 0 {
  1015. row := sheet.Rows[i]
  1016. cells := row.Cells
  1017. for k, cell := range cells {
  1018. if k > 0 && k < 9 {
  1019. text := cell.Value
  1020. if text == "" {
  1021. continue
  1022. }
  1023. groupMap[k] = text
  1024. var item models.BaseFromCoalmineMapping
  1025. //合计命名
  1026. if k == 9 {
  1027. item.IndexName = sheetName
  1028. }
  1029. item.IndexName = text + sheetName
  1030. nameMap[k] = item.IndexName
  1031. //code
  1032. exists := ContainsSpecialName(text)
  1033. var code string
  1034. if exists {
  1035. abbr := trimProvinceName(text)
  1036. //取处理后公司名首字母缩写
  1037. a := pinyin.NewArgs()
  1038. rows := pinyin.Pinyin(sheetName, a)
  1039. for i := 0; i < len(rows); i++ {
  1040. if len(rows[i]) != 0 {
  1041. str := rows[i][0]
  1042. pi := str[0:1]
  1043. code += pi
  1044. }
  1045. }
  1046. item.IndexCode = abbr + code + "coastal"
  1047. } else {
  1048. a := pinyin.NewArgs()
  1049. rows := pinyin.Pinyin(text+sheetName, a)
  1050. for i := 0; i < len(rows); i++ {
  1051. if len(rows[i]) != 0 {
  1052. str := rows[i][0]
  1053. pi := str[0:1]
  1054. code += pi
  1055. }
  1056. }
  1057. item.IndexCode = code + "coastal"
  1058. }
  1059. item.CreateTime = time.Now()
  1060. mappingItems = append(mappingItems, &item)
  1061. }
  1062. if k == 10 {
  1063. unit = strings.Replace(cell.Value, "单位:", "", -1)
  1064. }
  1065. }
  1066. }
  1067. if i > 0 {
  1068. var row models.Row
  1069. if i < len(sheet.Rows) {
  1070. row = sheet.Rows[i]
  1071. }
  1072. cells := row.Cells
  1073. for k, cell := range cells {
  1074. if k < 9 {
  1075. var item models.BaseFromCoalmineCoastalIndex
  1076. if k == 0 {
  1077. text := cell.Value
  1078. if text == "" {
  1079. continue
  1080. }
  1081. if len(text) < 6 {
  1082. dataTime = ExcelDateToDate(text).Format(utils.FormatDate)
  1083. } else {
  1084. parsedTime, err := time.Parse("01-02-06", text)
  1085. if err != nil {
  1086. parsedTime, err = time.Parse("2006/01/02", text)
  1087. if err != nil {
  1088. fmt.Println("解析时间字符串出错:", err)
  1089. return
  1090. }
  1091. }
  1092. dataTime = parsedTime.Format(utils.FormatDate)
  1093. }
  1094. }
  1095. if k > 0 {
  1096. text := cell.Value
  1097. item.IndexName = nameMap[k]
  1098. item.IndexCode = codeMap[nameMap[k]]
  1099. item.DealValue = text
  1100. item.GroupName = groupMap[k]
  1101. item.Source = "沿海八省"
  1102. item.DataTime = dataTime
  1103. item.Frequency = "日度"
  1104. item.ModifyTime = time.Now()
  1105. item.CreateTime = time.Now()
  1106. }
  1107. item.Unit = unit
  1108. indexItems = append(indexItems, &item)
  1109. }
  1110. }
  1111. }
  1112. }
  1113. }
  1114. //添加数据到数据库
  1115. for _, v := range mappingItems {
  1116. if codeMap[v.IndexName] == "" {
  1117. codeMap[v.IndexName] = v.IndexCode
  1118. newId, err := models.AddBaseFromCoalmineMapping(v)
  1119. if err != nil {
  1120. for i := 0; i < 10; i++ {
  1121. v.IndexCode = v.IndexCode + strconv.Itoa(i)
  1122. codeMap[v.IndexName] = v.IndexCode
  1123. newId, err := models.AddBaseFromCoalmineMapping(v)
  1124. if err != nil {
  1125. utils.FileLog.Info("再次添加公司指标名称错误:", err)
  1126. fmt.Println("再次添加公司指标名称错误", err)
  1127. continue
  1128. } else {
  1129. fmt.Println("新增公司成功", newId)
  1130. break
  1131. }
  1132. }
  1133. } else {
  1134. fmt.Println("新增公司成功", newId)
  1135. }
  1136. }
  1137. }
  1138. utils.FileLog.Info("指标操作完成")
  1139. fmt.Println("指标操作完成")
  1140. //给indexItem中的code赋值并插入index表
  1141. for _, v := range indexItems {
  1142. v.IndexCode = codeMap[v.IndexName]
  1143. if indexMap[v.IndexName+v.DataTime] == "" && v.DealValue != "" {
  1144. newId, err := models.AddBaseFromCoalCoastalIndex(v)
  1145. if err != nil {
  1146. fmt.Println("添加数据错误", err)
  1147. utils.FileLog.Info("添加数据错误:", err)
  1148. } else {
  1149. fmt.Println("新增成功", newId)
  1150. }
  1151. } else {
  1152. if indexMap[v.IndexName+v.DataTime] != v.DealValue && v.DealValue != "" {
  1153. err = models.UpdateBaseFromCoalCoastalIndex(v)
  1154. if err != nil {
  1155. utils.FileLog.Info("修改数据错误错误:", err)
  1156. fmt.Println("修改数据错误错误", err)
  1157. return
  1158. }
  1159. }
  1160. }
  1161. }
  1162. br.Ret = 200
  1163. br.Success = true
  1164. br.Msg = "获取成功"
  1165. return
  1166. }
  1167. // @Title 内陆数据
  1168. // @Description 刷新内陆数据接口
  1169. // @Param request body models.CoalMineDataReq true "type json string"
  1170. // @Success 200 {object} models.EdbClassify
  1171. // @router /inland [post]
  1172. func (this *CoalMineDataController) Inland() {
  1173. br := new(models.BaseResponse).Init()
  1174. defer func() {
  1175. this.Data["json"] = br
  1176. this.ServeJSON()
  1177. }()
  1178. var req models.CoalMineDataReq
  1179. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1180. if err != nil {
  1181. br.Msg = "参数解析异常!"
  1182. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1183. return
  1184. }
  1185. var mappingItems []*models.BaseFromCoalmineMapping
  1186. var indexItems []*models.BaseFromCoalmineInlandIndex
  1187. var codeMap = make(map[string]string)
  1188. var indexMap = make(map[string]string)
  1189. var nameMap = make(map[int]string)
  1190. var groupMap = make(map[int]string)
  1191. var dataTime string
  1192. var sheetName string
  1193. codeList, err := models.GetBaseFromCoalmineMapping()
  1194. if err != nil && err.Error() != utils.ErrNoRow() {
  1195. utils.FileLog.Info("获取煤炭指标失败:", err)
  1196. return
  1197. }
  1198. if len(codeList) > 0 {
  1199. for _, v := range codeList {
  1200. codeMap[v.IndexName] = v.IndexCode
  1201. }
  1202. }
  1203. indexCompanyList, err := models.GetBaseFromCoalmineInlandIndex()
  1204. if err != nil && err.Error() != utils.ErrNoRow() {
  1205. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  1206. return
  1207. }
  1208. if len(indexCompanyList) > 0 {
  1209. for _, v := range indexCompanyList {
  1210. indexMap[v.IndexName+v.DataTime] = v.DealValue
  1211. }
  1212. }
  1213. for _, sheet := range req.SheetData {
  1214. sheetName = sheet.Name
  1215. fmt.Println("sheetName:", sheetName)
  1216. var unit string
  1217. if sheetName == "可用天数" {
  1218. unit = "天"
  1219. } else {
  1220. unit = "万吨"
  1221. }
  1222. //遍历行读取
  1223. maxCol := sheet.MaxCol
  1224. for i := 0; i < maxCol; i++ {
  1225. if i == 0 {
  1226. row := sheet.Rows[i]
  1227. cells := row.Cells
  1228. for k, cell := range cells {
  1229. if k > 0 && k < 18 {
  1230. text := cell.Value
  1231. groupMap[k] = text
  1232. var item models.BaseFromCoalmineMapping
  1233. //合计命名
  1234. if k == 9 {
  1235. item.IndexName = sheetName
  1236. }
  1237. item.IndexName = text + sheetName
  1238. nameMap[k] = item.IndexName
  1239. //code
  1240. exists := ContainsSpecialName(text)
  1241. var code string
  1242. if exists {
  1243. abbr := trimProvinceName(text)
  1244. //取处理后公司名首字母缩写
  1245. a := pinyin.NewArgs()
  1246. rows := pinyin.Pinyin(sheetName, a)
  1247. for i := 0; i < len(rows); i++ {
  1248. if len(rows[i]) != 0 {
  1249. str := rows[i][0]
  1250. pi := str[0:1]
  1251. code += pi
  1252. }
  1253. }
  1254. item.IndexCode = abbr + code + "inland"
  1255. } else {
  1256. a := pinyin.NewArgs()
  1257. rows := pinyin.Pinyin(text+sheetName, a)
  1258. for i := 0; i < len(rows); i++ {
  1259. if len(rows[i]) != 0 {
  1260. str := rows[i][0]
  1261. pi := str[0:1]
  1262. code += pi
  1263. }
  1264. }
  1265. item.IndexCode = code + "inland"
  1266. }
  1267. item.CreateTime = time.Now()
  1268. mappingItems = append(mappingItems, &item)
  1269. }
  1270. }
  1271. }
  1272. if i > 0 {
  1273. var row models.Row
  1274. if i < len(sheet.Rows) {
  1275. row = sheet.Rows[i]
  1276. }
  1277. cells := row.Cells
  1278. for k, cell := range cells {
  1279. if k < 18 {
  1280. var item models.BaseFromCoalmineInlandIndex
  1281. if k == 0 {
  1282. text := cell.Value
  1283. if text == "" {
  1284. continue
  1285. }
  1286. if len(text) < 6 {
  1287. dataTime = ExcelDateToDate(text).Format(utils.FormatDate)
  1288. } else {
  1289. parsedTime, err := time.Parse("01-02-06", text)
  1290. if err != nil {
  1291. parsedTime, err = time.Parse("2006/01/02", text)
  1292. if err != nil {
  1293. utils.FileLog.Info("解析时间字符串出错:", err)
  1294. fmt.Println("解析时间字符串出错:", err)
  1295. return
  1296. }
  1297. }
  1298. dataTime = parsedTime.Format(utils.FormatDate)
  1299. }
  1300. }
  1301. if k > 0 {
  1302. text := cell.Value
  1303. item.IndexName = nameMap[k]
  1304. item.IndexCode = codeMap[nameMap[k]]
  1305. item.DealValue = text
  1306. item.GroupName = groupMap[k]
  1307. item.Source = "内陆十七省"
  1308. item.DataTime = dataTime
  1309. item.Unit = unit
  1310. item.Frequency = "日度"
  1311. item.ModifyTime = time.Now()
  1312. item.CreateTime = time.Now()
  1313. }
  1314. indexItems = append(indexItems, &item)
  1315. }
  1316. }
  1317. }
  1318. }
  1319. }
  1320. //添加数据到数据库
  1321. for _, v := range mappingItems {
  1322. if codeMap[v.IndexName] == "" {
  1323. codeMap[v.IndexName] = v.IndexCode
  1324. newId, err := models.AddBaseFromCoalmineMapping(v)
  1325. if err != nil {
  1326. for i := 0; i < 10; i++ {
  1327. v.IndexCode = v.IndexCode + strconv.Itoa(i)
  1328. codeMap[v.IndexName] = v.IndexCode
  1329. newId, err := models.AddBaseFromCoalmineMapping(v)
  1330. if err != nil {
  1331. utils.FileLog.Info("再次添加公司指标名称错误:", err)
  1332. fmt.Println("再次添加公司指标名称错误", err)
  1333. continue
  1334. } else {
  1335. fmt.Println("新增公司成功", newId)
  1336. break
  1337. }
  1338. }
  1339. } else {
  1340. fmt.Println("新增公司成功", newId)
  1341. }
  1342. }
  1343. }
  1344. utils.FileLog.Info("指标操作完成:")
  1345. fmt.Println("指标操作完成")
  1346. //给indexItem中的code赋值并插入index表
  1347. for _, v := range indexItems {
  1348. v.IndexCode = codeMap[v.IndexName]
  1349. if indexMap[v.IndexName+v.DataTime] == "" && v.DealValue != "" {
  1350. newId, err := models.AddBaseFromCoalInlandIndex(v)
  1351. if err != nil {
  1352. utils.FileLog.Info("添加数据错误:", err)
  1353. fmt.Println("添加数据错误", err)
  1354. } else {
  1355. fmt.Println("新增成功", newId)
  1356. }
  1357. } else {
  1358. if indexMap[v.IndexName+v.DataTime] != v.DealValue && v.DealValue != "" {
  1359. err = models.UpdateBaseFromCoalInlandIndex(v)
  1360. if err != nil {
  1361. utils.FileLog.Info("修改数据错误错误:", err)
  1362. fmt.Println("修改数据错误错误", err)
  1363. return
  1364. }
  1365. }
  1366. }
  1367. }
  1368. br.Ret = 200
  1369. br.Success = true
  1370. br.Msg = "获取成功"
  1371. return
  1372. }
  1373. // @Title 内陆数据
  1374. // @Description 刷新内陆数据接口
  1375. // @Param request body models.CoalMineDataReq true "type json string"
  1376. // @Success 200 {object} models.EdbClassify
  1377. // @router /firm [post]
  1378. func (this *CoalMineDataController) Firm() {
  1379. br := new(models.BaseResponse).Init()
  1380. defer func() {
  1381. this.Data["json"] = br
  1382. this.ServeJSON()
  1383. }()
  1384. var req models.CoalMineDataReq
  1385. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1386. if err != nil {
  1387. br.Msg = "参数解析异常!"
  1388. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1389. return
  1390. }
  1391. var year, month, day string
  1392. var yearMap = make(map[int]string)
  1393. var monthMap = make(map[int]string)
  1394. var dateMap = make(map[int]string)
  1395. var mappingItems []*models.BaseFromCoalmineMapping
  1396. var indexItems []*models.BaseFromCoalmineFirmIndex
  1397. var codeMap = make(map[string]string)
  1398. var indexMap = make(map[string]string)
  1399. var groupName string
  1400. codeList, err := models.GetBaseFromCoalmineMapping()
  1401. if err != nil && err.Error() != utils.ErrNoRow() {
  1402. utils.FileLog.Info("获取煤炭指标失败:", err)
  1403. return
  1404. }
  1405. if len(codeList) > 0 {
  1406. for _, v := range codeList {
  1407. codeMap[v.IndexName] = v.IndexCode
  1408. }
  1409. }
  1410. indexCompanyList, err := models.GetBaseFromCoalmineFirmIndex()
  1411. if err != nil && err.Error() != utils.ErrNoRow() {
  1412. utils.FileLog.Info("获取煤炭公司指标失败:", err)
  1413. return
  1414. }
  1415. if len(indexCompanyList) > 0 {
  1416. for _, v := range indexCompanyList {
  1417. indexMap[v.IndexName+v.DataTime] = v.DealValue
  1418. }
  1419. }
  1420. for _, sheet := range req.SheetData {
  1421. //遍历行读取
  1422. maxRow := sheet.MaxRow
  1423. for i := 0; i < maxRow; i++ {
  1424. //获取年份
  1425. //if i == 2 {
  1426. // row := sheet.Row(i)
  1427. // cells := row.Cells
  1428. // for k, cell := range cells {
  1429. // text := cell.String()
  1430. // if k > 0 && text != "" {
  1431. // year = text
  1432. // fmt.Println("year:",year)
  1433. // fmt.Println("k:",k)
  1434. // yearMap[k] = year
  1435. // }
  1436. // }
  1437. //}
  1438. //获取月份和年度
  1439. if i == 3 {
  1440. row := sheet.Rows[i]
  1441. cells := row.Cells
  1442. for k, cell := range cells {
  1443. text := cell.Value
  1444. if k > 0 && text != "" {
  1445. month = text
  1446. month = utils.ConvertToFormatDay(month)
  1447. time, _ := time.Parse(utils.FormatDate, month)
  1448. yearInt := time.Year()
  1449. monthInt := int(time.Month())
  1450. year = strconv.Itoa(yearInt)
  1451. yearMap[k] = year
  1452. month = strconv.Itoa(monthInt)
  1453. monthMap[k] = month
  1454. }
  1455. }
  1456. }
  1457. //获取旬度
  1458. if i == 4 {
  1459. row := sheet.Rows[i]
  1460. cells := row.Cells
  1461. for k, cell := range cells {
  1462. text := cell.Value
  1463. if k > 0 && text != "" {
  1464. if yearMap[k] != "" {
  1465. year = yearMap[k]
  1466. }
  1467. if monthMap[k] != "" {
  1468. month = monthMap[k]
  1469. }
  1470. day = text
  1471. dateMap[k] = year + "年" + month + "月" + day
  1472. }
  1473. }
  1474. }
  1475. //获取企业或地区名及信息
  1476. if i > 4 {
  1477. row := sheet.Rows[i]
  1478. cells := row.Cells
  1479. var companyName string
  1480. for k, cell := range cells {
  1481. if k == 0 {
  1482. companyName = cell.Value
  1483. //省名
  1484. if !strings.Contains(companyName, " ") {
  1485. groupName = companyName
  1486. }
  1487. if groupName == "全国合计" {
  1488. groupName = "全国"
  1489. }
  1490. //若有下面四个名称要拼上省名不然会重复
  1491. if companyName == " 地方合计" || companyName == " 地方国有" ||
  1492. companyName == " 地方乡镇" || companyName == " 国有重点" {
  1493. companyName = groupName + companyName
  1494. }
  1495. companyName = strings.Replace(companyName, " ", "", -1)
  1496. var item models.BaseFromCoalmineMapping
  1497. //生成code
  1498. exists := ContainsSpecialName(companyName)
  1499. var code string
  1500. if exists {
  1501. abbr := trimProvinceName(companyName)
  1502. //取处理后公司名首字母缩写
  1503. a := pinyin.NewArgs()
  1504. rows := pinyin.Pinyin(companyName[9:], a)
  1505. for i := 0; i < len(rows); i++ {
  1506. if len(rows[i]) != 0 {
  1507. str := rows[i][0]
  1508. pi := str[0:1]
  1509. code += pi
  1510. }
  1511. }
  1512. item.IndexCode = abbr[:2] + code + "firm"
  1513. } else {
  1514. a := pinyin.NewArgs()
  1515. rows := pinyin.Pinyin(companyName, a)
  1516. for i := 0; i < len(rows); i++ {
  1517. if len(rows[i]) != 0 {
  1518. str := rows[i][0]
  1519. pi := str[0:1]
  1520. code += pi
  1521. }
  1522. }
  1523. item.IndexCode = code + "firm"
  1524. }
  1525. item.IndexName = companyName
  1526. item.CreateTime = time.Now()
  1527. mappingItems = append(mappingItems, &item)
  1528. } else {
  1529. dealValue := cell.Value
  1530. item := models.BaseFromCoalmineFirmIndex{
  1531. IndexName: companyName,
  1532. IndexCode: codeMap[companyName],
  1533. DataTime: dateMap[k],
  1534. DealValue: dealValue,
  1535. GroupName: groupName,
  1536. Source: "全国分企业",
  1537. Unit: "万吨",
  1538. Frequency: "旬度",
  1539. CreateTime: time.Now(),
  1540. ModifyTime: time.Now(),
  1541. }
  1542. indexItems = append(indexItems, &item)
  1543. }
  1544. }
  1545. }
  1546. }
  1547. }
  1548. //添加数据到数据库
  1549. for _, v := range mappingItems {
  1550. if codeMap[v.IndexName] == "" {
  1551. codeMap[v.IndexName] = v.IndexCode
  1552. newId, err := models.AddBaseFromCoalmineMapping(v)
  1553. if err != nil {
  1554. for i := 0; i < 10; i++ {
  1555. v.IndexCode = v.IndexCode + strconv.Itoa(i)
  1556. codeMap[v.IndexName] = v.IndexCode
  1557. newId, err := models.AddBaseFromCoalmineMapping(v)
  1558. if err != nil {
  1559. fmt.Println("再次添加公司指标名称错误", err)
  1560. continue
  1561. } else {
  1562. fmt.Println("新增公司成功", newId)
  1563. break
  1564. }
  1565. }
  1566. } else {
  1567. fmt.Println("新增公司成功", newId)
  1568. }
  1569. }
  1570. }
  1571. fmt.Println("指标操作完成")
  1572. //给indexItem中的code赋值并插入index表
  1573. for _, v := range indexItems {
  1574. v.IndexCode = codeMap[v.IndexName]
  1575. if indexMap[v.IndexName+v.DataTime] == "" && v.DealValue != "" {
  1576. newId, err := models.AddBaseFromCoalFirmIndex(v)
  1577. if err != nil {
  1578. fmt.Println("添加数据错误", err)
  1579. } else {
  1580. fmt.Println("新增成功", newId)
  1581. }
  1582. } else {
  1583. if indexMap[v.IndexName+v.DataTime] != v.DealValue && v.DealValue != "" {
  1584. err = models.UpdateBaseFromCoalFirmIndex(v)
  1585. if err != nil {
  1586. fmt.Println("修改数据错误错误", err)
  1587. return
  1588. }
  1589. }
  1590. }
  1591. }
  1592. return
  1593. }
  1594. func ttrimProvinceName(name string) string {
  1595. name = strings.Replace(name, "陕西省", "sn", -1)
  1596. name = strings.Replace(name, "陕西", "sn", -1)
  1597. name = strings.Replace(name, "海南市", "hi", -1)
  1598. name = strings.Replace(name, "海南", "hi", -1)
  1599. name = strings.Replace(name, "河南省", "ha", -1)
  1600. name = strings.Replace(name, "河南", "ha", -1)
  1601. name = strings.Replace(name, "河北省", "he", -1)
  1602. name = strings.Replace(name, "河北", "he", -1)
  1603. name = strings.Replace(name, "澳门", "mo", -1)
  1604. name = strings.Replace(name, "内蒙古自治区", "nm", -1)
  1605. name = strings.Replace(name, "内蒙古", "nm", -1)
  1606. name = strings.Replace(name, "黑龙江", "hl", -1)
  1607. name = strings.Replace(name, "(", "", -1)
  1608. name = strings.Replace(name, ")", "", -1)
  1609. name = strings.Replace(name, "+", "", -1)
  1610. return name
  1611. }
  1612. func ExcelDateToDate(excelDate string) time.Time {
  1613. excelTime := time.Date(1899, time.December, 30, 0, 0, 0, 0, time.UTC)
  1614. var days, _ = strconv.Atoi(excelDate)
  1615. return excelTime.Add(time.Second * time.Duration(days*86400))
  1616. }
  1617. func PathExists(path string) (bool, error) {
  1618. _, err := os.Stat(path)
  1619. if err == nil { //文件或者目录存在
  1620. return true, nil
  1621. }
  1622. if os.IsNotExist(err) {
  1623. return false, nil
  1624. }
  1625. return false, err
  1626. }
  1627. func TrimCompanyName(name string) string {
  1628. name = strings.Replace(name, "有限", "", -1)
  1629. name = strings.Replace(name, "股份", "", -1)
  1630. name = strings.Replace(name, "责任", "", -1)
  1631. name = strings.Replace(name, "公司", "", -1)
  1632. name = strings.Replace(name, "(", "", -1)
  1633. name = strings.Replace(name, ")", "", -1)
  1634. return name
  1635. }
  1636. func ContainsSpecialName(name string) bool {
  1637. return strings.Contains(name, "陕西") ||
  1638. strings.Contains(name, "海南") ||
  1639. strings.Contains(name, "河南") ||
  1640. strings.Contains(name, "河北") ||
  1641. strings.Contains(name, "澳门") ||
  1642. strings.Contains(name, "内蒙古") ||
  1643. strings.Contains(name, "黑龙江")
  1644. }
  1645. func TrimProvinceName(name string) string {
  1646. name = strings.Replace(name, "陕西省", "sns", -1)
  1647. name = strings.Replace(name, "陕西", "sn", -1)
  1648. name = strings.Replace(name, "海南市", "hi", -1)
  1649. name = strings.Replace(name, "海南", "hi", -1)
  1650. name = strings.Replace(name, "河南省", "has", -1)
  1651. name = strings.Replace(name, "河南", "ha", -1)
  1652. name = strings.Replace(name, "河北省", "hes", -1)
  1653. name = strings.Replace(name, "河北", "he", -1)
  1654. name = strings.Replace(name, "澳门", "mo", -1)
  1655. name = strings.Replace(name, "内蒙古自治区", "nm", -1)
  1656. name = strings.Replace(name, "内蒙古", "nm", -1)
  1657. name = strings.Replace(name, "黑龙江", "hl", -1)
  1658. name = strings.Replace(name, "(", "", -1)
  1659. name = strings.Replace(name, ")", "", -1)
  1660. name = strings.Replace(name, "+", "", -1)
  1661. return name
  1662. }
  1663. func trimProvinceName(name string) string {
  1664. name = strings.Replace(name, "陕西省", "sn", -1)
  1665. name = strings.Replace(name, "陕西", "sn", -1)
  1666. name = strings.Replace(name, "海南市", "hi", -1)
  1667. name = strings.Replace(name, "海南可用", "hiky", -1)
  1668. name = strings.Replace(name, "海南", "hi", -1)
  1669. name = strings.Replace(name, "河南省", "ha", -1)
  1670. name = strings.Replace(name, "河南", "ha", -1)
  1671. name = strings.Replace(name, "河北省", "he", -1)
  1672. name = strings.Replace(name, "河北", "he", -1)
  1673. name = strings.Replace(name, "澳门", "mo", -1)
  1674. name = strings.Replace(name, "内蒙古自治区", "nm", -1)
  1675. name = strings.Replace(name, "内蒙古", "nm", -1)
  1676. name = strings.Replace(name, "黑龙江", "hl", -1)
  1677. name = strings.Replace(name, "(", "", -1)
  1678. name = strings.Replace(name, ")", "", -1)
  1679. name = strings.Replace(name, "+", "", -1)
  1680. return name
  1681. }