commodity_coal.go 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/models"
  5. "eta/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. }