data_source_longzhong.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/tealeg/xlsx"
  7. "hongze/hongze_task/models"
  8. "hongze/hongze_task/utils"
  9. "net/url"
  10. "os"
  11. "path/filepath"
  12. "rdluck_tools/http"
  13. "strconv"
  14. "time"
  15. )
  16. //获取隆众信息表中指标
  17. func GetLongZhongTargetInfoByExcel() {
  18. var err error
  19. defer func() {
  20. if err != nil {
  21. fmt.Println("err:" + err.Error())
  22. go utils.SendEmail("获取隆众指标信息失败"+time.Now().Format("2006-01-02 15:04:05"), ";Err:"+err.Error(), utils.EmailSendToUsers)
  23. return
  24. }
  25. }()
  26. excelPath := `./static/longzhong_0824.xlsx`
  27. file, err := xlsx.OpenFile(excelPath)
  28. if err != nil {
  29. fmt.Println("err:", err.Error())
  30. return
  31. }
  32. sheets := file.Sheets
  33. for k, v := range sheets {
  34. fmt.Println(k, v.Name)
  35. classify, err := models.GetEdbdataClassifyByClassifyName(v.Name)
  36. if err != nil {
  37. if err.Error() == utils.ErrNoRow() {
  38. err = errors.New("分类不存在:Err:" + err.Error())
  39. } else {
  40. err = err
  41. }
  42. return
  43. }
  44. if classify == nil {
  45. err = errors.New("分类不存在,请重新核对")
  46. return
  47. }
  48. classifyId := classify.ClassifyId
  49. classifyName := classify.ClassifyName
  50. fmt.Println(classifyId, classifyName)
  51. maxRow := v.MaxRow
  52. //maxCol := v.MaxCol
  53. fmt.Println(classifyName, maxRow)
  54. for i := 0; i < maxRow; i++ {
  55. row, err := v.Row(i)
  56. if err != nil {
  57. return
  58. }
  59. longzhongClassify := ""
  60. if i > 0 {
  61. longzhongClassifyNew := row.GetCell(0).String()
  62. if longzhongClassifyNew != "" {
  63. longzhongClassify = longzhongClassifyNew
  64. }
  65. secName := row.GetCell(1).Value
  66. frequency := row.GetCell(2).Value
  67. LongzhongId := row.GetCell(3).Value
  68. remark := row.GetCell(4).String()
  69. item := new(models.DataSourceLongzhong)
  70. item.ClassifyId = classifyId
  71. item.ClassifyName = classifyName
  72. item.SecName = secName
  73. item.Frequency = frequency
  74. item.LongzhongId = LongzhongId
  75. item.Remark = remark
  76. item.LongzhongClassify = longzhongClassify
  77. _, err = models.AddDataSourceLongzhong(item)
  78. if err != nil {
  79. fmt.Println("Err:", err.Error())
  80. return
  81. }
  82. }
  83. }
  84. }
  85. }
  86. func GetLzProductList()(err error) {
  87. utils.FileLog.Info("start GetLzProductList")
  88. defer func() {
  89. if err != nil {
  90. fmt.Println("Err:" + err.Error())
  91. go utils.SendEmail("获取隆众指标数据失败"+time.Now().Format("2006-01-02 15:04:05"), ";Err:"+err.Error(), utils.EmailSendToUsers)
  92. }
  93. }()
  94. postUrl := `http://info.oilchem.net/cust/interfacenew/getProductUnitListNew`
  95. username := "hzmd"
  96. password := "HZMD2020"
  97. timestamp := fmt.Sprintf("%d", time.Now().Unix())
  98. fmt.Println(timestamp)
  99. token := utils.MD5(username + timestamp + utils.MD5(password))
  100. fmt.Println("token:", token)
  101. param := url.Values{}
  102. param.Add("username", username)
  103. param.Add("password", password)
  104. param.Add("timestamp", timestamp)
  105. param.Add("token", token)
  106. utils.FileLog.Info("%s", param.Encode())
  107. body, err := http.Post(postUrl, param.Encode())
  108. if err != nil {
  109. fmt.Println("err:", err.Error())
  110. return
  111. }
  112. result := string(body)
  113. if result == "" {
  114. err = errors.New("返回内容为空")
  115. return
  116. }
  117. resp := new(models.LzProductInfoResp)
  118. err = json.Unmarshal(body, &resp)
  119. if err != nil {
  120. fmt.Println("json.Unmarshal Err:" + err.Error())
  121. return
  122. }
  123. if resp.Code != "1" {
  124. fmt.Println("获取信息失败")
  125. err = errors.New("Code:" + resp.Code + ";Msg:" + resp.Msg)
  126. return
  127. }
  128. fmt.Println("len")
  129. fmt.Println(len(resp.Data))
  130. for _, v := range resp.Data {
  131. fmt.Println(v.Unitid, v.ProName, v.ProUnitName)
  132. var classifyId int
  133. var classifyName string
  134. classify, err := models.GetEdbdataClassifyByClassifyName(v.ProName)
  135. if err != nil && err.Error() != utils.ErrNoRow() {
  136. fmt.Println("获取分类失败,Err:" + err.Error())
  137. return err
  138. }
  139. if classify == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  140. classifyItem := new(models.EdbdataClassify)
  141. classifyItem.ClassifyName = v.ProName
  142. classifyItem.ParentId = 4
  143. classifyItem.CreateTime = time.Now()
  144. newId, err := models.AddEdbdataClassify(classifyItem)
  145. if err != nil {
  146. fmt.Println("新增分类失败,Err:" + err.Error())
  147. return err
  148. }
  149. classifyId = int(newId)
  150. classifyName = v.ProName
  151. } else {
  152. classifyId = classify.ClassifyId
  153. classifyName = classify.ClassifyName
  154. }
  155. count, err := models.GetLongzhonginfoCount(classifyId, v.Unitid)
  156. if err != nil {
  157. fmt.Println("判断指标是否存在失败,Err:" + err.Error())
  158. return err
  159. }
  160. if count > 0 { //修改
  161. lzProduct := new(models.Longzhonginfo)
  162. lzProduct.SecName = v.ProUnitName
  163. lzProduct.Frequency = v.LzType
  164. lzProduct.ClassifyId = classifyId
  165. lzProduct.ClassifyName = classifyName
  166. lzProduct.Unit = v.Unit
  167. lzProduct.CreateTime = time.Now()
  168. if v.IsNormal == "" {
  169. v.IsNormal = "1"
  170. }
  171. lzProduct.IsNormal = v.IsNormal
  172. lzProduct.LastModifyDate = v.Maxdate
  173. lzProduct.Unitid = v.Unitid
  174. lzProduct.TempId = v.Tempid
  175. lzProduct.TempName = v.TempName
  176. lzProduct.ModifyTime = time.Now()
  177. err = models.ModifyLongzhonginfo(lzProduct)
  178. if err != nil {
  179. fmt.Println("修改失败,Err:" + err.Error())
  180. return err
  181. }
  182. } else { //新增
  183. maxId, err := models.GetLongzhonginfoMaxId()
  184. if err != nil {
  185. fmt.Println("获取编码失败,err:" + err.Error())
  186. return err
  187. }
  188. tradeCode := fmt.Sprintf("L%06d", maxId+1)
  189. lzProduct := new(models.Longzhonginfo)
  190. lzProduct.TradeCode = tradeCode
  191. lzProduct.SecName = v.ProUnitName
  192. lzProduct.Frequency = v.LzType
  193. lzProduct.ClassifyId = classifyId
  194. lzProduct.ClassifyName = classifyName
  195. lzProduct.Unit = v.Unit
  196. lzProduct.CreateTime = time.Now()
  197. if v.IsNormal == "" {
  198. v.IsNormal = "1"
  199. }
  200. lzProduct.IsNormal = v.IsNormal
  201. lzProduct.LastModifyDate = v.Maxdate
  202. lzProduct.Unitid = v.Unitid
  203. lzProduct.TempId = v.Tempid
  204. lzProduct.TempName = v.TempName
  205. lzProduct.ModifyTime = time.Now()
  206. _, err = models.AddLongzhonginfo(lzProduct)
  207. if err != nil {
  208. fmt.Println("新增指标信息失败")
  209. return err
  210. }
  211. }
  212. }
  213. return
  214. }
  215. func GetLzProductDetail()(err error) {
  216. utils.FileLog.Info("start GetLzProductDetail")
  217. defer func() {
  218. if err != nil {
  219. fmt.Println("Err:" + err.Error())
  220. }
  221. }()
  222. startDate := time.Now().AddDate(0, 0, -2).Format(utils.FormatDate)
  223. endDate := time.Now().AddDate(0, 0, 0).Format(utils.FormatDate)
  224. time.Sleep(5 * time.Second)
  225. pageSize := 30
  226. postUrl := `http://info.oilchem.net/cust/interfacenew/getProductUnitDetailNew`
  227. username := "hzmd"
  228. password := "HZMD2020"
  229. totalPage := 10
  230. for i := 1; i <= totalPage; i++ {
  231. fmt.Println("page:", i)
  232. timestamp := fmt.Sprintf("%d", time.Now().Unix())
  233. token := utils.MD5(username + timestamp + utils.MD5(password))
  234. param := url.Values{}
  235. param.Add("username", username)
  236. param.Add("password", password)
  237. param.Add("timestamp", timestamp)
  238. param.Add("token", token)
  239. //param.Add("id", "334")
  240. param.Add("startDate", startDate)
  241. param.Add("endDate", endDate)
  242. param.Add("pagesize", strconv.Itoa(pageSize))
  243. param.Add("page", strconv.Itoa(i))
  244. utils.FileLog.Info("%s", param.Encode())
  245. body, err := http.Post(postUrl, param.Encode())
  246. if err != nil {
  247. fmt.Println("err:", err.Error())
  248. return err
  249. }
  250. utils.FileLog.Info("%s", string(body))
  251. resp := new(models.LzProductInfoDetailResp)
  252. err = json.Unmarshal(body, &resp)
  253. if err != nil {
  254. fmt.Println("Unmarshal Err:" + err.Error())
  255. return err
  256. }
  257. if resp.Code != "1" {
  258. fmt.Println("获取信息失败")
  259. err = errors.New("Code:" + resp.Code + ";Msg:" + resp.Msg)
  260. return err
  261. }
  262. if i == totalPage && resp.Pagesize >= pageSize {
  263. totalPage = totalPage + 5
  264. }
  265. for k, v := range resp.Data {
  266. fmt.Println(k, v.UnitId, v.ProCnName)
  267. target, err := models.GetLongzhonginfoByUnitId(v.ProCnName, v.UnitId)
  268. if err != nil {
  269. if err.Error() != utils.ErrNoRow() {
  270. fmt.Println("获取target失败,err:" + err.Error())
  271. continue
  272. }
  273. fmt.Println("err:" + err.Error())
  274. return err
  275. }
  276. if target == nil {
  277. fmt.Println("target is empty:"+v.ProCnName, v.UnitId)
  278. continue
  279. }
  280. count, err := models.GetLongzhongdataCount(target.LongzhonginfoId, v.DataTime)
  281. if err != nil {
  282. fmt.Println("GetLongzhongdataCount Err:" + err.Error())
  283. return err
  284. }
  285. if count > 0 {
  286. lzdetail := new(models.Longzhongdata)
  287. lzdetail.CreateTime = time.Now()
  288. lzdetail.ModifyTime = time.Now()
  289. lzdetail.TradeCode = target.TradeCode
  290. lzdetail.LongzhonginfoId = target.LongzhonginfoId
  291. lzdetail.Dt = v.DataTime
  292. lzdetail.Close = v.UnitValue
  293. lzdetail.UnitDesc = v.UnitDesc
  294. lzdetail.UpdTime = v.UpdTime
  295. err = models.ModifyLongzhongdata(lzdetail)
  296. if err != nil {
  297. fmt.Println("ModifyLongzhongdata Err:" + err.Error())
  298. return err
  299. }
  300. } else {
  301. lzdetail := new(models.Longzhongdata)
  302. lzdetail.CreateTime = time.Now()
  303. lzdetail.ModifyTime = time.Now()
  304. lzdetail.TradeCode = target.TradeCode
  305. lzdetail.LongzhonginfoId = target.LongzhonginfoId
  306. lzdetail.Dt = v.DataTime
  307. lzdetail.Close = v.UnitValue
  308. lzdetail.UnitDesc = v.UnitDesc
  309. lzdetail.UpdTime = v.UpdTime
  310. err = models.AddLongzhongdata(lzdetail)
  311. if err != nil {
  312. fmt.Println("AddLongzhongdata Err:" + err.Error())
  313. return err
  314. }
  315. }
  316. }
  317. if resp.Pagesize < pageSize || len(resp.Data) == 0 {
  318. return err
  319. }
  320. time.Sleep(2 * time.Second)
  321. }
  322. return
  323. }
  324. func GetLzPrice() {
  325. fmt.Println("start")
  326. /*getUrl:=`https://openapi.oilchem.net/price/getProduct?password=HZMD2020&type=guoji&username=hzmd`
  327. result,err:=http.Get(getUrl)
  328. fmt.Println(string(result),err)
  329. utils.FileLog.Info("%s",string(result))*/
  330. getUrl := `https://openapi.oilchem.net/price/getPrice?password=HZMD2020&type=guoji&username=hzmd&inStartDate=2020-08-20&inEndDate=2020-08-26`
  331. result, err := http.Get(getUrl)
  332. fmt.Println(string(result), err)
  333. utils.FileLog.Info("%s", string(result))
  334. fmt.Println("end")
  335. }
  336. func LzExportExcel() {
  337. var err error
  338. defer func() {
  339. if err != nil {
  340. fmt.Println("err:" + err.Error())
  341. }
  342. }()
  343. parentId := 4
  344. classifyList, err := models.GetEdbdataClassifyByParentId(parentId)
  345. if err != nil {
  346. return
  347. }
  348. xlsxFile := xlsx.NewFile()
  349. if err != nil {
  350. fmt.Println("生成文件失败" + err.Error())
  351. return
  352. }
  353. dir, _ := os.Executable()
  354. exPath := filepath.Dir(dir)
  355. downLoadnFilePath := exPath + "/static/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  356. fmt.Println("downLoadnFilePath")
  357. fmt.Println(downLoadnFilePath)
  358. for _, v := range classifyList {
  359. fmt.Println(v.ClassifyId, v.ClassifyName)
  360. //获取指标
  361. secNameList, err := models.GetLongzhonginfoByClassifyId(v.ClassifyId)
  362. if err != nil {
  363. fmt.Println("获取数据失败,Err:" + err.Error())
  364. return
  365. }
  366. if len(secNameList) <= 0 {
  367. continue
  368. }
  369. sheetNew, err := xlsxFile.AddSheet(v.ClassifyName)
  370. if err != nil {
  371. fmt.Println("新增Sheet失败", err.Error())
  372. return
  373. }
  374. //获取指标数据
  375. windRow := sheetNew.AddRow()
  376. secNameRow := sheetNew.AddRow()
  377. frequencyRow := sheetNew.AddRow()
  378. unitRow := sheetNew.AddRow()
  379. lastModifyDateRow := sheetNew.AddRow()
  380. //获取分类下指标最大数据量
  381. dataMax, err := models.GetLongzhongDataMaxCount(v.ClassifyId)
  382. if err != nil {
  383. fmt.Println("获取指标最大数据量失败", err.Error())
  384. return
  385. }
  386. for _, sv := range secNameList {
  387. fmt.Println("line 399:", v.ClassifyId, v.ClassifyName, sv.SecName, sv.Frequency)
  388. windRow.AddCell().SetValue("Wind")
  389. secNameRow.AddCell().SetValue("指标名称")
  390. frequencyRow.AddCell().SetValue("频率")
  391. unitRow.AddCell().SetValue("单位")
  392. lastModifyDateRow.AddCell().SetValue("更新时间")
  393. secNameRow.AddCell().SetValue(sv.SecName)
  394. frequencyRow.AddCell().SetValue(sv.Frequency)
  395. unitRow.AddCell().SetValue(sv.Unit)
  396. lastModifyDateRow.AddCell().SetValue(sv.LastModifyDate)
  397. windRow.AddCell()
  398. windRow.AddCell()
  399. secNameRow.AddCell()
  400. frequencyRow.AddCell()
  401. unitRow.AddCell()
  402. lastModifyDateRow.AddCell()
  403. //获取数据
  404. dataList, err := models.GetLongzhongDataById(sv.LongzhonginfoId)
  405. if err != nil {
  406. fmt.Println("GetLongzhongDataById Err:" + err.Error())
  407. return
  408. }
  409. if len(dataList) <= 0 {
  410. for n := 0; n < dataMax; n++ {
  411. rowIndex := 5 + n
  412. row, err := sheetNew.Row(rowIndex)
  413. if err != nil {
  414. fmt.Println("add row err:" + err.Error())
  415. return
  416. }
  417. row.AddCell()
  418. row.AddCell()
  419. row.AddCell()
  420. }
  421. } else {
  422. for rk, dv := range dataList {
  423. rowIndex := 5 + rk
  424. row, err := sheetNew.Row(rowIndex)
  425. if err != nil {
  426. fmt.Println("add row err:" + err.Error())
  427. return
  428. }
  429. row.AddCell().SetValue(dv.Dt)
  430. row.AddCell().SetFloat(dv.Close)
  431. row.AddCell()
  432. }
  433. }
  434. }
  435. err = xlsxFile.Save(downLoadnFilePath)
  436. if err != nil {
  437. return
  438. }
  439. }
  440. }