data_source_longzhong.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/tealeg/xlsx"
  8. "hongze/hongze_task/models"
  9. "hongze/hongze_task/utils"
  10. "net/url"
  11. "os"
  12. "path/filepath"
  13. "github.com/rdlucklib/rdluck_tools/http"
  14. "strconv"
  15. "strings"
  16. "sync"
  17. "time"
  18. )
  19. //获取隆众信息表中指标
  20. func GetLongZhongTargetInfoByExcel() {
  21. var err error
  22. defer func() {
  23. if err != nil {
  24. fmt.Println("err:" + err.Error())
  25. go utils.SendEmail("获取隆众指标信息失败"+time.Now().Format("2006-01-02 15:04:05"), ";Err:"+err.Error(), utils.EmailSendToUsers)
  26. return
  27. }
  28. }()
  29. excelPath := `./static/longzhong_0824.xlsx`
  30. file, err := xlsx.OpenFile(excelPath)
  31. if err != nil {
  32. fmt.Println("err:", err.Error())
  33. return
  34. }
  35. sheets := file.Sheets
  36. for k, v := range sheets {
  37. fmt.Println(k, v.Name)
  38. classify, err := models.GetEdbdataClassifyByClassifyName(v.Name)
  39. if err != nil {
  40. if err.Error() == utils.ErrNoRow() {
  41. err = errors.New("分类不存在:Err:" + err.Error())
  42. } else {
  43. err = err
  44. }
  45. return
  46. }
  47. if classify == nil {
  48. err = errors.New("分类不存在,请重新核对")
  49. return
  50. }
  51. classifyId := classify.ClassifyId
  52. classifyName := classify.ClassifyName
  53. fmt.Println(classifyId, classifyName)
  54. maxRow := v.MaxRow
  55. //maxCol := v.MaxCol
  56. fmt.Println(classifyName, maxRow)
  57. for i := 0; i < maxRow; i++ {
  58. row := v.Row(i)
  59. longzhongClassify := ""
  60. if i > 0 {
  61. cells := row.Cells
  62. longzhongClassifyNew := cells[0].String()
  63. if longzhongClassifyNew != "" {
  64. longzhongClassify = longzhongClassifyNew
  65. }
  66. secName := cells[1].Value
  67. frequency := cells[2].Value
  68. LongzhongId := cells[3].Value
  69. remark := cells[4].String()
  70. item := new(models.DataSourceLongzhong)
  71. item.ClassifyId = classifyId
  72. item.ClassifyName = classifyName
  73. item.SecName = secName
  74. item.Frequency = frequency
  75. item.LongzhongId = LongzhongId
  76. item.Remark = remark
  77. item.LongzhongClassify = longzhongClassify
  78. _, err = models.AddDataSourceLongzhong(item)
  79. if err != nil {
  80. fmt.Println("Err:", err.Error())
  81. return
  82. }
  83. }
  84. }
  85. }
  86. }
  87. func GetLzProductList(cont context.Context) (err error) {
  88. utils.FileLog.Info("start GetLzProductList")
  89. var bodyStr string
  90. defer func() {
  91. if err != nil {
  92. fmt.Println("Err:" + err.Error())
  93. go utils.SendEmail("获取隆众指标数据失败"+time.Now().Format("2006-01-02 15:04:05"), ";Err:"+err.Error()+";body:"+bodyStr, utils.EmailSendToUsers)
  94. }
  95. }()
  96. postUrl := `http://info.oilchem.net/cust/interfacenew/getProductUnitListNew`
  97. username := "hzmd"
  98. password := "HZMD2020"
  99. timestamp := fmt.Sprintf("%d", time.Now().Unix())
  100. fmt.Println(timestamp)
  101. token := utils.MD5(username + timestamp + utils.MD5(password))
  102. fmt.Println("token:", token)
  103. param := url.Values{}
  104. param.Add("username", username)
  105. param.Add("password", password)
  106. param.Add("timestamp", timestamp)
  107. param.Add("token", token)
  108. utils.FileLog.Info("%s", param.Encode())
  109. body, err := http.Post(postUrl, param.Encode())
  110. if err != nil {
  111. fmt.Println("err:", err.Error())
  112. return
  113. }
  114. result := string(body)
  115. if result == "" {
  116. err = errors.New("返回内容为空")
  117. return
  118. }
  119. resp := new(models.LzProductInfoResp)
  120. err = json.Unmarshal(body, &resp)
  121. if err != nil {
  122. bodyStr = string(body)
  123. fmt.Println("json.Unmarshal Err:" + err.Error())
  124. return
  125. }
  126. if resp.Code != "1" {
  127. fmt.Println("获取信息失败")
  128. err = errors.New("Code:" + resp.Code + ";Msg:" + resp.Msg)
  129. return
  130. }
  131. fmt.Println("len")
  132. fmt.Println(len(resp.Data))
  133. utils.FileLog.Info("%s", string(body))
  134. for _, v := range resp.Data {
  135. fmt.Println(v.Unitid, v.ProName, v.ProUnitName)
  136. var classifyId int
  137. var classifyName string
  138. classify, err := models.GetEdbdataClassifyByClassifyName(v.ProName)
  139. if err != nil && err.Error() != utils.ErrNoRow() {
  140. fmt.Println("获取分类失败,Err:" + err.Error())
  141. return err
  142. }
  143. if classify == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  144. classifyItem := new(models.EdbdataClassify)
  145. classifyItem.ClassifyName = v.ProName
  146. classifyItem.ParentId = 4
  147. classifyItem.CreateTime = time.Now()
  148. newId, err := models.AddEdbdataClassify(classifyItem)
  149. if err != nil {
  150. fmt.Println("新增分类失败,Err:" + err.Error())
  151. return err
  152. }
  153. classifyId = int(newId)
  154. classifyName = v.ProName
  155. } else {
  156. classifyId = classify.ClassifyId
  157. classifyName = classify.ClassifyName
  158. }
  159. count, err := models.GetLongzhonginfoCount(classifyId, v.Unitid)
  160. if err != nil {
  161. fmt.Println("判断指标是否存在失败,Err:" + err.Error())
  162. return err
  163. }
  164. secNameCount, _ := models.GetLongzhonginfoBySecNameCount(v.ProUnitName)
  165. if count > 0 || secNameCount > 0 { //修改
  166. lzProduct := new(models.Longzhonginfo)
  167. lzProduct.SecName = v.ProUnitName
  168. lzProduct.Frequency = v.LzType
  169. lzProduct.ClassifyId = classifyId
  170. lzProduct.ClassifyName = classifyName
  171. lzProduct.Unit = v.Unit
  172. lzProduct.CreateTime = time.Now()
  173. lzProduct.IsNormal = v.IsNormal
  174. lzProduct.LastModifyDate = v.Maxdate
  175. lzProduct.Unitid = v.Unitid
  176. lzProduct.TempId = v.Tempid
  177. lzProduct.TempName = v.TempName
  178. lzProduct.ModifyTime = time.Now()
  179. err = models.ModifyLongzhonginfo(lzProduct)
  180. if err != nil {
  181. fmt.Println("修改失败,Err:" + err.Error())
  182. return err
  183. }
  184. } else { //新增
  185. maxId, err := models.GetLongzhonginfoMaxId()
  186. if err != nil {
  187. fmt.Println("获取编码失败,err:" + err.Error())
  188. return err
  189. }
  190. tradeCode := fmt.Sprintf("L%06d", maxId+1)
  191. lzProduct := new(models.Longzhonginfo)
  192. lzProduct.TradeCode = tradeCode
  193. lzProduct.SecName = v.ProUnitName
  194. lzProduct.Frequency = v.LzType
  195. lzProduct.ClassifyId = classifyId
  196. lzProduct.ClassifyName = classifyName
  197. lzProduct.Unit = v.Unit
  198. lzProduct.CreateTime = time.Now()
  199. if v.IsNormal == "" {
  200. v.IsNormal = "1"
  201. }
  202. lzProduct.IsNormal = v.IsNormal
  203. lzProduct.LastModifyDate = v.Maxdate
  204. lzProduct.Unitid = v.Unitid
  205. lzProduct.TempId = v.Tempid
  206. lzProduct.TempName = v.TempName
  207. lzProduct.ModifyTime = time.Now()
  208. _, err = models.AddLongzhonginfo(lzProduct)
  209. if err != nil {
  210. fmt.Println("新增指标信息失败")
  211. return err
  212. }
  213. }
  214. }
  215. return
  216. }
  217. func GetLzProductDetail(cont context.Context) (err error) {
  218. utils.FileLogLz.Info("start GetLzProductDetail")
  219. defer func() {
  220. if err != nil {
  221. utils.FileLogLz.Info("Err:" + err.Error())
  222. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"GetLzProductDetail:失败提醒", "ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  223. }
  224. }()
  225. startDate := time.Now().AddDate(0, -1, 0).Format(utils.FormatDate)
  226. endDate := time.Now().AddDate(0, 0, 0).Format(utils.FormatDate)
  227. time.Sleep(5 * time.Second)
  228. pageSize := 30
  229. postUrl := `http://info.oilchem.net/cust/interfacenew/getProductUnitDetailNew`
  230. username := "hzmd"
  231. password := "HZMD2020"
  232. infoList, err := models.GetLongzhonginfoList()
  233. if err != nil {
  234. return
  235. }
  236. for _, v := range infoList {
  237. totalPage := 10
  238. for i := 1; i <= totalPage; i++ {
  239. displayTime, _ := models.GetDisplayTime(v.LongzhonginfoId)
  240. if displayTime != "" {
  241. startDate = displayTime
  242. }
  243. fmt.Println("page:", i, totalPage)
  244. utils.FileLogLz.Info("Page:%s", strconv.Itoa(i))
  245. timestamp := fmt.Sprintf("%d", time.Now().Unix())
  246. token := utils.MD5(username + timestamp + utils.MD5(password))
  247. param := url.Values{}
  248. param.Add("username", username)
  249. param.Add("password", password)
  250. param.Add("timestamp", timestamp)
  251. param.Add("token", token)
  252. param.Add("id", strconv.Itoa(v.Unitid))
  253. param.Add("startDate", startDate)
  254. param.Add("endDate", endDate)
  255. param.Add("pagesize", strconv.Itoa(pageSize))
  256. param.Add("page", strconv.Itoa(i))
  257. utils.FileLogLz.Info("GetLzProductDetail Param: %s", param.Encode())
  258. body, err := http.Post(postUrl, param.Encode())
  259. if err != nil {
  260. utils.FileLogLz.Info("Post Err:%s", err.Error())
  261. return err
  262. }
  263. utils.FileLogLz.Info("GetLzProductDetail Result: %s", string(body))
  264. resp := new(models.LzProductInfoDetailResp)
  265. err = json.Unmarshal(body, &resp)
  266. if err != nil {
  267. utils.FileLogLz.Info("Unmarshal Err:%s", err.Error())
  268. return err
  269. }
  270. if resp.Code != "1" {
  271. fmt.Println("获取信息失败")
  272. err = errors.New("Code:" + resp.Code + ";Msg:" + resp.Msg)
  273. return err
  274. }
  275. for k, v := range resp.Data {
  276. fmt.Println(k, v.UnitId, v.ProCnName)
  277. target, err := models.GetLongzhonginfoByUnitId(v.ProCnName, v.UnitId)
  278. if err != nil {
  279. if err.Error() != utils.ErrNoRow() {
  280. fmt.Println("获取target失败,err:" + err.Error())
  281. continue
  282. }
  283. fmt.Println("err:" + err.Error())
  284. return err
  285. }
  286. if target == nil {
  287. fmt.Println("target is empty:"+v.ProCnName, v.UnitId)
  288. continue
  289. }
  290. count, err := models.GetLongzhongdataCount(target.LongzhonginfoId, v.DataTime)
  291. if err != nil {
  292. fmt.Println("GetLongzhongdataCount Err:" + err.Error())
  293. return err
  294. }
  295. if count > 0 {
  296. lzdetail := new(models.Longzhongdata)
  297. lzdetail.CreateTime = time.Now()
  298. lzdetail.ModifyTime = time.Now()
  299. lzdetail.TradeCode = target.TradeCode
  300. lzdetail.LongzhonginfoId = target.LongzhonginfoId
  301. lzdetail.Dt = v.DataTime
  302. lzdetail.Close = v.UnitValue
  303. lzdetail.UnitDesc = v.UnitDesc
  304. lzdetail.UpdTime = v.UpdTime
  305. lzdetail.AddTime = v.AddTime
  306. lzdetail.DisplayTime = v.DisplayTime
  307. err = models.ModifyLongzhongdata(lzdetail)
  308. if err != nil {
  309. utils.FileLogLz.Info("ModifyLongzhongdata Err:" + err.Error())
  310. continue
  311. }
  312. } else {
  313. lzdetail := new(models.Longzhongdata)
  314. lzdetail.CreateTime = time.Now()
  315. lzdetail.ModifyTime = time.Now()
  316. lzdetail.TradeCode = target.TradeCode
  317. lzdetail.LongzhonginfoId = target.LongzhonginfoId
  318. lzdetail.Dt = v.DataTime
  319. lzdetail.Close = v.UnitValue
  320. lzdetail.UnitDesc = v.UnitDesc
  321. lzdetail.UpdTime = v.UpdTime
  322. lzdetail.AddTime = v.AddTime
  323. lzdetail.DisplayTime = v.DisplayTime
  324. err = models.AddLongzhongdata(lzdetail)
  325. if err != nil {
  326. utils.FileLogLz.Info("AddLongzhongdata Err:" + err.Error())
  327. continue
  328. }
  329. }
  330. }
  331. totalPage = resp.Page
  332. //time.Sleep(1 * time.Second)
  333. }
  334. }
  335. utils.FileLogLz.Info("end GetLzProductDetail")
  336. return
  337. }
  338. //获取历史隆众石化数据
  339. func GetHistoryLzProductDetail() (err error) {
  340. utils.FileLog.Info("start GetHistoryLzProductDetail")
  341. var msg string
  342. defer func() {
  343. if err != nil {
  344. fmt.Println("Err:" + err.Error())
  345. }
  346. if msg != "" {
  347. fmt.Println("msg:" + msg)
  348. }
  349. }()
  350. infoList, err := models.GetLongzhonginfoList()
  351. if err != nil {
  352. msg = "获取指标列表失败,Err:" + err.Error()
  353. return
  354. }
  355. for ik, iv := range infoList {
  356. fmt.Println(ik, iv.SecName, iv.LongzhonginfoId)
  357. startDate := "2020-01-01" //time.Now().AddDate(0, 0, 0).Format(utils.FormatDate)
  358. endDate := "2020-12-31" //time.Now().AddDate(0, 0, 0).Format(utils.FormatDate)
  359. //time.Sleep(1 * time.Second)
  360. pageSize := 30
  361. postUrl := `http://info.oilchem.net/cust/interfacenew/getProductUnitDetailNew`
  362. username := "hzmd"
  363. password := "HZMD2020"
  364. totalPage := 10
  365. for i := 1; i <= totalPage; i++ {
  366. fmt.Println("page:", i)
  367. timestamp := fmt.Sprintf("%d", time.Now().Unix())
  368. token := utils.MD5(username + timestamp + utils.MD5(password))
  369. param := url.Values{}
  370. param.Add("username", username)
  371. param.Add("password", password)
  372. param.Add("timestamp", timestamp)
  373. param.Add("token", token)
  374. param.Add("id", strconv.Itoa(iv.Unitid))
  375. param.Add("startDate", startDate)
  376. param.Add("endDate", endDate)
  377. if i > 1 {
  378. param.Add("page", strconv.Itoa(i))
  379. }
  380. param.Add("pagesize", strconv.Itoa(pageSize))
  381. utils.FileLog.Info("%s", param.Encode())
  382. fmt.Println("post url:", postUrl)
  383. fmt.Println("post params:", param.Encode())
  384. body, err := http.Post(postUrl, param.Encode())
  385. if err != nil {
  386. fmt.Println("err:", err.Error())
  387. return err
  388. }
  389. utils.FileLog.Info("%s", string(body))
  390. fmt.Println("body:", string(body))
  391. resp := new(models.LzProductInfoDetailResp)
  392. err = json.Unmarshal(body, &resp)
  393. if err != nil {
  394. fmt.Println("Unmarshal Err:" + err.Error())
  395. fmt.Println("body:", string(body))
  396. return err
  397. }
  398. if resp.Code != "1" {
  399. fmt.Println("获取信息失败")
  400. err = errors.New("Code:" + resp.Code + ";Msg:" + resp.Msg)
  401. return err
  402. }
  403. if i == totalPage && resp.Pagesize >= pageSize {
  404. totalPage = totalPage + 5
  405. }
  406. for k, v := range resp.Data {
  407. fmt.Println(k, v.UnitId, v.ProCnName)
  408. target, err := models.GetLongzhonginfoByUnitId(v.ProCnName, v.UnitId)
  409. if err != nil {
  410. if err.Error() != utils.ErrNoRow() {
  411. fmt.Println("获取target失败,err:" + err.Error())
  412. continue
  413. }
  414. fmt.Println("err:" + err.Error())
  415. return err
  416. }
  417. if target == nil {
  418. fmt.Println("target is empty:"+v.ProCnName, v.UnitId)
  419. continue
  420. }
  421. count, err := models.GetLongzhongdataCount(target.LongzhonginfoId, v.DataTime)
  422. if err != nil {
  423. fmt.Println("GetLongzhongdataCount Err:" + err.Error())
  424. return err
  425. }
  426. if count > 0 {
  427. fmt.Println("数据已存在:", iv.Unitid, iv.SecName, v.UnitValue, v.DataTime)
  428. lzdetail := new(models.Longzhongdata)
  429. lzdetail.CreateTime = time.Now()
  430. lzdetail.ModifyTime = time.Now()
  431. lzdetail.TradeCode = target.TradeCode
  432. lzdetail.LongzhonginfoId = target.LongzhonginfoId
  433. lzdetail.Dt = v.DataTime
  434. lzdetail.Close = v.UnitValue
  435. lzdetail.UnitDesc = v.UnitDesc
  436. lzdetail.UpdTime = v.UpdTime
  437. lzdetail.AddTime = v.AddTime
  438. lzdetail.DisplayTime = v.DisplayTime
  439. err = models.ModifyLongzhongdata(lzdetail)
  440. if err != nil {
  441. fmt.Println("ModifyLongzhongdata Err:" + err.Error())
  442. return err
  443. }
  444. } else {
  445. lzdetail := new(models.Longzhongdata)
  446. lzdetail.CreateTime = time.Now()
  447. lzdetail.ModifyTime = time.Now()
  448. lzdetail.TradeCode = target.TradeCode
  449. lzdetail.LongzhonginfoId = target.LongzhonginfoId
  450. lzdetail.Dt = v.DataTime
  451. lzdetail.Close = v.UnitValue
  452. lzdetail.UnitDesc = v.UnitDesc
  453. lzdetail.UpdTime = v.UpdTime
  454. lzdetail.AddTime = v.AddTime
  455. lzdetail.DisplayTime = v.DisplayTime
  456. err = models.AddLongzhongdata(lzdetail)
  457. if err != nil {
  458. fmt.Println("AddLongzhongdata Err:" + err.Error())
  459. return err
  460. }
  461. }
  462. }
  463. if i == 1 && len(resp.Data) == 0 {
  464. utils.FileLog.Info("%d", iv.Unitid)
  465. }
  466. if resp.Pagesize < pageSize || len(resp.Data) == 0 {
  467. break
  468. }
  469. //time.Sleep(1 * time.Second)
  470. }
  471. err = models.ModifyLongzhonginfoIsNormal(iv.LongzhonginfoId)
  472. if err != nil {
  473. fmt.Println("ModifyLongzhonginfoIsNormal:Err" + err.Error())
  474. }
  475. fmt.Println("sleep", iv.SecName)
  476. time.Sleep(1 * time.Second)
  477. }
  478. return
  479. }
  480. func GetLzPrice() {
  481. fmt.Println("start")
  482. /*getUrl:=`https://openapi.oilchem.net/price/getProduct?password=HZMD2020&type=guoji&username=hzmd`
  483. result,err:=http.Get(getUrl)
  484. fmt.Println(string(result),err)
  485. utils.FileLog.Info("%s",string(result))*/
  486. getUrl := `https://openapi.oilchem.net/price/getPrice?password=HZMD2020&type=guoji&username=hzmd&inStartDate=2020-08-20&inEndDate=2020-08-26`
  487. result, err := http.Get(getUrl)
  488. fmt.Println(string(result), err)
  489. utils.FileLog.Info("%s", string(result))
  490. fmt.Println("end")
  491. }
  492. func LzExportExcel() {
  493. var err error
  494. defer func() {
  495. if err != nil {
  496. fmt.Println("err:" + err.Error())
  497. }
  498. }()
  499. parentId := 4
  500. classifyList, err := models.GetEdbdataClassifyByParentId(parentId)
  501. if err != nil {
  502. return
  503. }
  504. xlsxFile := xlsx.NewFile()
  505. if err != nil {
  506. fmt.Println("生成文件失败" + err.Error())
  507. return
  508. }
  509. dir, _ := os.Executable()
  510. exPath := filepath.Dir(dir)
  511. downLoadnFilePath := exPath + "/static/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  512. fmt.Println("downLoadnFilePath")
  513. fmt.Println(downLoadnFilePath)
  514. for _, v := range classifyList {
  515. fmt.Println(v.ClassifyId, v.ClassifyName)
  516. //获取指标
  517. secNameList, err := models.GetLongzhonginfoByClassifyId(v.ClassifyId)
  518. if err != nil {
  519. fmt.Println("获取数据失败,Err:" + err.Error())
  520. return
  521. }
  522. if len(secNameList) <= 0 {
  523. continue
  524. }
  525. sheetNew, err := xlsxFile.AddSheet(v.ClassifyName)
  526. if err != nil {
  527. fmt.Println("新增Sheet失败", err.Error())
  528. return
  529. }
  530. //获取指标数据
  531. windRow := sheetNew.AddRow()
  532. secNameRow := sheetNew.AddRow()
  533. frequencyRow := sheetNew.AddRow()
  534. unitRow := sheetNew.AddRow()
  535. lastModifyDateRow := sheetNew.AddRow()
  536. //获取分类下指标最大数据量
  537. dataMax, err := models.GetLongzhongDataMaxCount(v.ClassifyId)
  538. if err != nil {
  539. fmt.Println("获取指标最大数据量失败", err.Error())
  540. return
  541. }
  542. for _, sv := range secNameList {
  543. fmt.Println("line 399:", v.ClassifyId, v.ClassifyName, sv.SecName, sv.Frequency)
  544. windRow.AddCell().SetValue("Wind")
  545. secNameRow.AddCell().SetValue("指标名称")
  546. frequencyRow.AddCell().SetValue("频率")
  547. unitRow.AddCell().SetValue("单位")
  548. lastModifyDateRow.AddCell().SetValue("更新时间")
  549. secNameRow.AddCell().SetValue(sv.SecName)
  550. frequencyRow.AddCell().SetValue(sv.Frequency)
  551. unitRow.AddCell().SetValue(sv.Unit)
  552. lastModifyDateRow.AddCell().SetValue(sv.LastModifyDate)
  553. windRow.AddCell()
  554. windRow.AddCell()
  555. secNameRow.AddCell()
  556. frequencyRow.AddCell()
  557. unitRow.AddCell()
  558. lastModifyDateRow.AddCell()
  559. //获取数据
  560. dataList, err := models.GetLongzhongDataById(sv.LongzhonginfoId)
  561. if err != nil {
  562. fmt.Println("GetLongzhongDataById Err:" + err.Error())
  563. return
  564. }
  565. if len(dataList) <= 0 {
  566. for n := 0; n < dataMax; n++ {
  567. rowIndex := 5 + n
  568. row := sheetNew.Row(rowIndex)
  569. row.AddCell()
  570. row.AddCell()
  571. row.AddCell()
  572. }
  573. } else {
  574. endRowIndex := 0
  575. for rk, dv := range dataList {
  576. rowIndex := 5 + rk
  577. row := sheetNew.Row(rowIndex)
  578. row.AddCell().SetValue(dv.Dt)
  579. row.AddCell().SetFloat(dv.Close)
  580. row.AddCell()
  581. endRowIndex = rowIndex
  582. }
  583. if len(dataList) < dataMax {
  584. dataLen := dataMax - len(dataList)
  585. for n := 0; n < dataLen; n++ {
  586. rowIndex := (endRowIndex + 1) + n
  587. row := sheetNew.Row(rowIndex)
  588. row.AddCell()
  589. row.AddCell()
  590. row.AddCell()
  591. }
  592. }
  593. }
  594. }
  595. }
  596. err = xlsxFile.Save(downLoadnFilePath)
  597. fmt.Print("downLoadnFilePath:", downLoadnFilePath)
  598. if err != nil {
  599. return
  600. }
  601. }
  602. func GetLzProductPriceProduct(cont context.Context) (err error) {
  603. var msg string
  604. defer func() {
  605. if err != nil {
  606. fmt.Println("Err:" + err.Error())
  607. go utils.SendEmail("获取隆众价格指标失败"+time.Now().Format("2006-01-02 15:04:05"), ";Err:"+err.Error(), utils.EmailSendToUsers)
  608. }
  609. if msg != "" {
  610. fmt.Println("msg:", msg)
  611. go utils.SendEmail("获取隆众价格指标失败"+time.Now().Format("2006-01-02 15:04:05"), ";msg:"+msg, utils.EmailSendToUsers)
  612. }
  613. }()
  614. //chuchang, guonei, guoji, oil ,lingshou,guojioil,base,国内出厂,国内市场,国际市场,国内成品油批发价,国内成品油零售价,国际原油,基准价
  615. typeMap := make(map[string]string)
  616. typeMap["chuchang"] = "国内出厂"
  617. typeMap["guonei"] = "国内市场"
  618. typeMap["guoji"] = "国际市场"
  619. typeMap["oil"] = "国内成品油批发价"
  620. typeMap["lingshou"] = "国内成品油零售价"
  621. typeMap["guojioil"] = "国际原油"
  622. typeMap["base"] = "基准价"
  623. username := "hzmd"
  624. password := "HZMD2020"
  625. for k, v := range typeMap {
  626. fmt.Println(k, v)
  627. getUrl := `https://openapi.oilchem.net/price/getProduct?password=` + password + `&type=` + k + `&username=` + username + ``
  628. body, err := http.Get(getUrl)
  629. if err != nil {
  630. msg = "获取隆众价格指标失败:Err" + err.Error()
  631. return err
  632. }
  633. fmt.Println("getUrl:", getUrl)
  634. fmt.Println("body", string(body))
  635. utils.FileLog.Info("%s", string(body))
  636. infoList := new(models.LzPriceInfoResp)
  637. err = json.Unmarshal(body, &infoList)
  638. if err != nil {
  639. msg = "获取隆众价格指标失败:Unmarshal Err " + err.Error() + " ;body:" + string(body)
  640. return err
  641. }
  642. if infoList.Code != 1 {
  643. msg = "获取隆众价格指标失败:body " + string(body)
  644. return nil
  645. }
  646. for _, n := range infoList.Data {
  647. count, err := models.GetLongzhongpriceinfoCount(n.Id)
  648. if err != nil {
  649. msg = "判断隆众价格指标是否存在失败:err " + err.Error()
  650. return err
  651. }
  652. item := new(models.Longzhongpriceinfo)
  653. item.Standard = n.Standard
  654. item.ModelName = n.ModelName
  655. item.Unit = n.Unit
  656. item.AreaName = n.AreaName
  657. item.PriceType = n.PriceType
  658. item.Memo = n.Memo
  659. item.PriceId = n.Id
  660. item.ProductName = n.ProductName
  661. item.InfoType = k
  662. item.InfoTypeRemark = v
  663. item.MarketName = n.MarketName
  664. item.ManufactureName = n.ManufactureName
  665. if count > 0 {
  666. fmt.Println("存在", n.ProductName, n.Id)
  667. //修改
  668. err = models.ModifyLongzhongpriceinfo(item)
  669. if err != nil {
  670. msg = "修改隆众价格指标失败:err " + err.Error()
  671. return err
  672. }
  673. } else {
  674. _, err = models.AddLongzhongpriceinfo(item)
  675. if err != nil {
  676. msg = "新增隆众价格指标失败:err " + err.Error()
  677. return err
  678. }
  679. }
  680. }
  681. time.Sleep(31 * time.Second)
  682. }
  683. return
  684. }
  685. func GetLzProductPriceData(cont context.Context) (err error) {
  686. var msg string
  687. defer func() {
  688. if err != nil {
  689. fmt.Println("Err:" + err.Error())
  690. go utils.SendEmail("获取隆众价格指标失败"+time.Now().Format("2006-01-02 15:04:05"), ";Err:"+err.Error(), utils.EmailSendToUsers)
  691. }
  692. if msg != "" {
  693. fmt.Println("msg:", msg)
  694. go utils.SendEmail("获取隆众价格指标失败"+time.Now().Format("2006-01-02 15:04:05"), ";msg:"+msg, utils.EmailSendToUsers)
  695. utils.FileLog.Info("%s", msg)
  696. }
  697. }()
  698. priceInfoList, err := models.GetLongzhongpriceinfo()
  699. if err != nil {
  700. msg = "获取价格指标失败,Err:" + err.Error()
  701. return
  702. }
  703. username := "hzmd"
  704. password := "HZMD2020"
  705. startDate := time.Now().AddDate(0, 0, -7).Format(utils.FormatDate)
  706. endDate := time.Now().AddDate(0, 0, 0).Format(utils.FormatDate)
  707. for k, v := range priceInfoList {
  708. fmt.Println(k, v)
  709. getUrl := `https://openapi.oilchem.net/price/getPrice?password=` + password + `&type=` + v.InfoType + `&username=` + username + `&inStartDate=` + startDate + `&inEndDate=` + endDate + `&id=` + v.PriceId
  710. body, err := http.Get(getUrl)
  711. if err != nil {
  712. msg = "获取隆众价格数据失败:Err" + err.Error()
  713. return err
  714. }
  715. fmt.Println("getUrl:", getUrl)
  716. fmt.Println("body", string(body))
  717. utils.FileLog.Info("%s", string(body))
  718. infoList := new(models.LzPriceDataResp)
  719. err = json.Unmarshal(body, &infoList)
  720. if err != nil {
  721. msg = "获取隆众价格数据失败:Unmarshal Err " + err.Error() + " ;body:" + string(body)
  722. return err
  723. }
  724. if infoList.Code != 1 {
  725. msg = "获取隆众价格数据失败:body " + string(body)
  726. return nil
  727. }
  728. for _, n := range infoList.Data {
  729. count, err := models.GetLongzhongpricedataCount(v.LongzhongpriceinfoId, n.PriceDate)
  730. if err != nil {
  731. msg = "获取隆众价格数据失败:err " + err.Error()
  732. return err
  733. }
  734. if count > 0 {
  735. fmt.Println("存在", n.PriceDate, n.Id)
  736. } else {
  737. item := new(models.Longzhongpricedata)
  738. item.LongzhongpriceinfoId = v.LongzhongpriceinfoId
  739. item.PriceDate = n.PriceDate
  740. item.Memo = n.Memo
  741. item.Price = n.Price
  742. item.CnyPrice = n.CnyPrice
  743. item.ZsyPrice = n.ZsyPrice
  744. item.ZshPrice = n.ZshPrice
  745. item.LowPrice = n.LowPrice
  746. item.HighPrice = n.HighPrice
  747. item.RisePrice = n.RisePrice
  748. item.TonPrice = n.TonPrice
  749. item.PriceType = n.PriceType
  750. item.UpdateDate = n.UpdateDate
  751. _, err = models.AddLongzhongpricedata(item)
  752. if err != nil {
  753. msg = "新增隆众价格数据失败:err " + err.Error()
  754. return err
  755. }
  756. }
  757. }
  758. }
  759. return
  760. }
  761. func LzPriceExportExcel() {
  762. var err error
  763. defer func() {
  764. if err != nil {
  765. fmt.Println("err:" + err.Error())
  766. }
  767. }()
  768. classifyList, err := models.GetLzPriceClassify()
  769. if err != nil {
  770. return
  771. }
  772. xlsxFile := xlsx.NewFile()
  773. if err != nil {
  774. fmt.Println("生成文件失败" + err.Error())
  775. return
  776. }
  777. dir, _ := os.Executable()
  778. exPath := filepath.Dir(dir)
  779. downLoadnFilePath := exPath + "/static/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  780. fmt.Println("downLoadnFilePath")
  781. fmt.Println(downLoadnFilePath)
  782. for _, v := range classifyList {
  783. fmt.Println(v.ProductName)
  784. //获取指标
  785. secNameList, err := models.GetLongzhongpriceinfoByClassifyName(v.ProductName)
  786. if err != nil {
  787. fmt.Println("获取数据失败,Err:" + err.Error())
  788. return
  789. }
  790. if len(secNameList) <= 0 {
  791. continue
  792. }
  793. sheetNew, err := xlsxFile.AddSheet(v.ProductName)
  794. if err != nil {
  795. fmt.Println("新增Sheet失败", err.Error())
  796. return
  797. }
  798. //获取指标数据
  799. windRow := sheetNew.AddRow()
  800. secNameRow := sheetNew.AddRow()
  801. frequencyRow := sheetNew.AddRow()
  802. unitRow := sheetNew.AddRow()
  803. lastModifyDateRow := sheetNew.AddRow()
  804. marketRow := sheetNew.AddRow()
  805. manufactureRow := sheetNew.AddRow()
  806. titleRow := sheetNew.AddRow()
  807. //获取分类下指标最大数据量
  808. dataMax, err := models.GetLongzhongPriceDataMaxCount(v.ProductName)
  809. if err != nil {
  810. fmt.Println("获取指标最大数据量失败", err.Error())
  811. return
  812. }
  813. for _, sv := range secNameList {
  814. fmt.Println("line 399:", v.ProductName, sv.InfoTypeRemark, sv.AreaName)
  815. windRow.AddCell().SetString("Wind")
  816. secNameRow.AddCell().SetValue("分类")
  817. frequencyRow.AddCell().SetValue("价格类型")
  818. unitRow.AddCell().SetValue("单位")
  819. lastModifyDateRow.AddCell().SetValue("地区")
  820. marketRow.AddCell().SetValue("市场名称")
  821. manufactureRow.AddCell().SetValue("厂家名称")
  822. secNameRow.AddCell().SetValue(sv.InfoTypeRemark)
  823. frequencyRow.AddCell().SetValue(sv.PriceType)
  824. unitRow.AddCell().SetValue(sv.Unit)
  825. lastModifyDateRow.AddCell().SetValue(sv.AreaName)
  826. marketRow.AddCell().SetValue(sv.MarketName)
  827. manufactureRow.AddCell().SetValue(sv.ManufactureName)
  828. windRow.AddCell()
  829. windRow.AddCell()
  830. windRow.AddCell()
  831. windRow.AddCell()
  832. secNameRow.AddCell()
  833. frequencyRow.AddCell()
  834. unitRow.AddCell()
  835. lastModifyDateRow.AddCell()
  836. marketRow.AddCell()
  837. manufactureRow.AddCell()
  838. secNameRow.AddCell()
  839. frequencyRow.AddCell()
  840. unitRow.AddCell()
  841. lastModifyDateRow.AddCell()
  842. marketRow.AddCell()
  843. manufactureRow.AddCell()
  844. secNameRow.AddCell()
  845. frequencyRow.AddCell()
  846. unitRow.AddCell()
  847. lastModifyDateRow.AddCell()
  848. marketRow.AddCell()
  849. manufactureRow.AddCell()
  850. titleRow.AddCell().SetValue("价格日期")
  851. titleRow.AddCell().SetValue("价格")
  852. titleRow.AddCell().SetValue("低端价格")
  853. titleRow.AddCell().SetValue("高端价格")
  854. titleRow.AddCell()
  855. //获取数据
  856. dataList, err := models.GetLongzhongPriceDataById(sv.LongzhongpriceinfoId)
  857. if err != nil {
  858. fmt.Println("GetLongzhongDataById Err:" + err.Error())
  859. return
  860. }
  861. if len(dataList) <= 0 {
  862. for n := 0; n < dataMax; n++ {
  863. rowIndex := 8 + n
  864. row := sheetNew.Row(rowIndex)
  865. row.AddCell()
  866. row.AddCell()
  867. row.AddCell()
  868. row.AddCell()
  869. row.AddCell()
  870. }
  871. } else {
  872. endRowIndex := 0
  873. for rk, dv := range dataList {
  874. rowIndex := 8 + rk
  875. row := sheetNew.Row(rowIndex)
  876. row.AddCell().SetValue(dv.PriceDate)
  877. row.AddCell().SetFloat(dv.Price)
  878. row.AddCell().SetFloat(dv.LowPrice)
  879. row.AddCell().SetFloat(dv.HighPrice)
  880. row.AddCell()
  881. endRowIndex = rowIndex
  882. }
  883. if len(dataList) < dataMax {
  884. dataLen := dataMax - len(dataList)
  885. for n := 0; n < dataLen; n++ {
  886. rowIndex := (endRowIndex + 1) + n
  887. row := sheetNew.Row(rowIndex)
  888. row.AddCell()
  889. row.AddCell()
  890. row.AddCell()
  891. row.AddCell()
  892. row.AddCell()
  893. }
  894. }
  895. }
  896. }
  897. }
  898. err = xlsxFile.Save(downLoadnFilePath)
  899. fmt.Print("downLoadnFilePath:", downLoadnFilePath)
  900. if err != nil {
  901. return
  902. }
  903. }
  904. //隆众-调研指标
  905. func GetLzSurveyProduct(cont context.Context) (err error) {
  906. utils.FileLogLz.Info("GetLzSurveyProduct start:" + time.Now().Format(utils.FormatDateTime))
  907. var msg string
  908. defer func() {
  909. if err != nil {
  910. fmt.Println("Err:" + err.Error())
  911. go utils.SendEmail("获取隆众调研指标失败"+time.Now().Format("2006-01-02 15:04:05"), ";Err:"+err.Error(), utils.EmailSendToUsers)
  912. }
  913. if msg != "" {
  914. fmt.Println("msg:", msg)
  915. go utils.SendEmail("获取隆众调研指标失败"+time.Now().Format("2006-01-02 15:04:05"), ";msg:"+msg, utils.EmailSendToUsers)
  916. }
  917. }()
  918. username := "hzmd"
  919. password := "HZMD2020"
  920. pageIndex := 0
  921. for {
  922. pageIndex++
  923. getUrl := `https://openapi.oilchem.net/research/getProduct?passWord=` + password + `&userName=` + username + `&pageSize=100&pageNum=` + strconv.Itoa(pageIndex)
  924. utils.FileLogLz.Info("GetLzSurveyProduct getUrl:" + getUrl)
  925. body, err := http.Get(getUrl)
  926. if err != nil {
  927. msg = "获取隆众调研指标失败:Err" + err.Error()
  928. return err
  929. }
  930. utils.FileLogLz.Info("GetLzSurveyProduct:" + string(body))
  931. infoItem := new(models.SurveyProduct)
  932. err = json.Unmarshal(body, &infoItem)
  933. if err != nil {
  934. msg = "获取隆众调研指标失败:Unmarshal Err " + err.Error() + " ;body:" + string(body)
  935. return err
  936. }
  937. if infoItem.Status != "200" {
  938. msg = "获取隆众调研指标失败:body " + string(body)
  939. return nil
  940. }
  941. for _, n := range infoItem.Response.List {
  942. count, err := models.GetLongzhongSurveyProductCount(n.QuotaSampleID)
  943. if err != nil {
  944. msg = "判断隆众调研指标是否存在失败:err " + err.Error()
  945. return err
  946. }
  947. item := new(models.LongzhongSurveyProduct)
  948. item.ProjectQuotaId = n.ProjectQuotaID
  949. item.BreedId = n.BreedID
  950. item.BreedName = n.BreedName
  951. item.QuotaId = n.QuotaID
  952. item.QuotaName = n.QuotaName
  953. item.UnitId = n.UnitID
  954. item.UnitName = n.UnitName
  955. item.SampleType = n.SampleType
  956. item.SampleId = n.SampleID
  957. item.SampleName = n.SampleName
  958. item.DeviceId = n.DeviceID
  959. item.Device = n.Device
  960. item.ProductCraftId = n.ProductCraftID
  961. item.ProductCraft = n.ProductCraft
  962. item.ProductLine = n.ProductLine
  963. item.InputMode = n.InputMode
  964. item.Frequency = n.Frequency
  965. item.CustomId = n.CustomID
  966. item.CustomType = n.CustomType
  967. item.Custom = n.Custom
  968. item.QuotaSampleId = n.QuotaSampleID
  969. item.ModifyTime = time.Now()
  970. if count > 0 {
  971. fmt.Println("存在", n.QuotaName, n.QuotaSampleID)
  972. //修改
  973. err = models.ModifLongzhongSurveyProduct(item)
  974. if err != nil {
  975. msg = "修改隆众价格指标失败:err " + err.Error()
  976. return err
  977. }
  978. existItem, err := models.GetLongzhongSurveyProductItem(n.QuotaSampleID)
  979. if err != nil {
  980. msg = "获取已存在隆众指标信息失败:err " + err.Error()
  981. return err
  982. }
  983. if existItem != nil {
  984. surveyProductId := existItem.SurveyProductId
  985. lzCode := fmt.Sprintf("lz%04d", surveyProductId)
  986. models.ModifLongzhongSurveyProductCode(lzCode, surveyProductId)
  987. }
  988. } else {
  989. item.StartDate = "2018-01-01"
  990. item.EndDate = "2018-01-01"
  991. lastId, err := models.AddLongzhongSurveyProduct(item)
  992. if err != nil {
  993. msg = "新增隆众价格指标失败:err " + err.Error()
  994. return err
  995. }
  996. lzCode := fmt.Sprintf("lz%04d", lastId)
  997. models.ModifLongzhongSurveyProductCode(lzCode, int(lastId))
  998. }
  999. }
  1000. totalPage := utils.PageCount(int(infoItem.Response.Total), 100)
  1001. if pageIndex > totalPage {
  1002. break
  1003. }
  1004. }
  1005. return
  1006. }
  1007. var lzLock sync.Mutex
  1008. //隆众-调研指标数据
  1009. func GetLzSurveyProductData(cont context.Context) (err error) {
  1010. lzLock.Lock()
  1011. utils.FileLogLz.Info("GetLzSurveyProductData start:" + time.Now().Format(utils.FormatDateTime))
  1012. go utils.SendEmail("隆众数据开始获取"+time.Now().Format("2006-01-02 15:04:05"), time.Now().Format("2006-01-02 15:04:05"), utils.EmailSendToUsers)
  1013. var msg string
  1014. defer func() {
  1015. go utils.SendEmail("隆众数据获取完成"+time.Now().Format("2006-01-02 15:04:05"), ";msg:"+msg, utils.EmailSendToUsers)
  1016. if err != nil {
  1017. fmt.Println("Err:" + err.Error())
  1018. go utils.SendEmail("获取隆众调研指标数据失败"+time.Now().Format("2006-01-02 15:04:05"), ";Err:"+err.Error(), utils.EmailSendToUsers)
  1019. }
  1020. if msg != "" {
  1021. fmt.Println("msg:", msg)
  1022. go utils.SendEmail("获取隆众调研指标数据失败"+time.Now().Format("2006-01-02 15:04:05"), ";msg:"+msg, utils.EmailSendToUsers)
  1023. utils.FileLog.Info("%s", msg)
  1024. }
  1025. }()
  1026. priceInfoList, err := models.GetLongzhongSurveyList()
  1027. if err != nil {
  1028. msg = "获取隆众调研指标数据失败,Err:" + err.Error()
  1029. return
  1030. }
  1031. username := "hzmd"
  1032. password := "HZMD2020"
  1033. paramMap := make(map[string]interface{})
  1034. paramMap["userName"] = username
  1035. paramMap["passWord"] = password
  1036. paramMap["pageSize"] = 100
  1037. startDate := time.Now().AddDate(-4, 0, 0).UnixNano() / 1e6
  1038. //startDateTime, _ := time.Parse(utils.FormatDate, "2021-12-01")
  1039. //startDate := startDateTime.UnixNano() / 1e6
  1040. endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).UnixNano() / 1e6
  1041. for k, v := range priceInfoList {
  1042. //existList, err := data_manage.GetLzSurveyDataExistByTradeCode(v.SurveyProductId)
  1043. //existMap := make(map[string]*data_manage.LongzhongSurveyData)
  1044. //for _, v := range existList {
  1045. // key := strconv.Itoa(int(v.QuotaSampleId)) + v.DataTime
  1046. // existMap[key] = v
  1047. //}
  1048. //fmt.Println(existMap)
  1049. pageIndex := 1
  1050. //if v.EndDate != "" {
  1051. // endDateTime, _ := time.Parse(utils.FormatDate, v.EndDate)
  1052. // startDate = endDateTime.UnixNano() / 1e6
  1053. //}
  1054. if v.EndDate != "" {
  1055. endDateTime, _ := time.Parse(utils.FormatDate, v.EndDate)
  1056. startDate = endDateTime.AddDate(0, -8, 0).UnixNano() / 1e6
  1057. }
  1058. if startDate < 0 {
  1059. startDate = time.Now().AddDate(0, -8, 0).UnixNano() / 1e6
  1060. }
  1061. fmt.Println("endDate:" + time.Now().Format(utils.FormatDate))
  1062. for {
  1063. paramMap["pageNum"] = pageIndex
  1064. paramMap["projectQuotaId"] = v.ProjectQuotaId
  1065. paramMap["quotaSampleId"] = v.QuotaSampleId
  1066. paramMap["researchStartDateTime"] = startDate
  1067. paramMap["researchEndDateTime"] = endDate
  1068. fmt.Println(k, v)
  1069. getUrl := `https://openapi.oilchem.net/research/productPage`
  1070. postData, err := json.Marshal(paramMap)
  1071. if err != nil {
  1072. fmt.Println("postData Err:", err.Error())
  1073. }
  1074. fmt.Println("getUrl:" + getUrl)
  1075. utils.FileLogLz.Info("GetLzSurveyProductData postData:" + string(postData))
  1076. body, err := http.Post(getUrl, string(postData), "application/json")
  1077. if err != nil {
  1078. msg = "获取隆众调研指标数据失败:Err" + err.Error()
  1079. return err
  1080. }
  1081. utils.FileLogLz.Info("GetLzSurveyProductData:" + string(body))
  1082. fmt.Println("result:" + string(body))
  1083. if !strings.Contains(string(body), "<html>") {
  1084. dataList := new(models.LzSurveyData)
  1085. err = json.Unmarshal(body, &dataList)
  1086. if err != nil {
  1087. msg = "获取隆众调研指标数据失败:Unmarshal Err " + err.Error() + " ;body:" + string(body)
  1088. return err
  1089. }
  1090. if dataList.Status != "200" {
  1091. msg = "获取隆众调研指标数据失败:body " + string(body)
  1092. return nil
  1093. }
  1094. for _, n := range dataList.Response.List {
  1095. //dateTime := time.Unix(n.TaskActualFinishTime/1000, 0)
  1096. //dateTimeStr := dateTime.Format(utils.FormatDate)
  1097. shouldDateTime := time.Unix(n.TaskShouldFinishTime/1000, 0)
  1098. shouldDateTimeStr := shouldDateTime.Format(utils.FormatDate)
  1099. //fmt.Println("QuotaSampleID:", n.QuotaSampleID, "taskActualFinishTime:", dateTimeStr, "taskShouldFinishTime:", shouldDateTimeStr)
  1100. count, err := models.GetLzSurveyDataCount(v.SurveyProductId, int(v.QuotaSampleId), shouldDateTimeStr)
  1101. if err != nil {
  1102. msg = "获取隆众调研指标数据失败:err " + err.Error()
  1103. return err
  1104. }
  1105. key := strconv.Itoa(int(v.QuotaSampleId)) + shouldDateTimeStr
  1106. if count <= 0 {
  1107. fmt.Println("not exist:" + key)
  1108. item := new(models.LongzhongSurveyData)
  1109. item.SurveyProductId = v.SurveyProductId
  1110. item.ProjectQuotaId = n.ProjectQuotaID
  1111. item.BreedId = n.BreedID
  1112. item.BreedName = n.BreedName
  1113. item.QuotaId = n.QuotaID
  1114. item.QuotaName = n.QuotaName
  1115. item.UnitId = n.UnitID
  1116. item.UnitName = n.UnitName
  1117. item.SampleType = n.SampleType
  1118. item.SampleId = n.SampleID
  1119. item.SampleName = n.SampleName
  1120. item.DeviceId = n.DeviceID
  1121. item.Device = n.Device
  1122. item.ProductCraftId = n.ProductCraftID
  1123. item.ProductCraft = n.ProductCraft
  1124. item.ProductLine = n.ProductLine
  1125. item.InputMode = n.InputMode
  1126. item.Frequency = n.Frequency
  1127. item.InputValue = n.InputValue
  1128. item.TaskShouldFinishTime = n.TaskShouldFinishTime
  1129. item.CustomId = n.CustomID
  1130. item.CustomType = n.CustomType
  1131. item.Custom = n.Custom
  1132. item.QuotaSampleId = n.QuotaSampleID
  1133. item.TaskActualFinishTime = n.TaskActualFinishTime
  1134. //item.AreaName = n.AreaName.(string)
  1135. //item.ProvinceName = n.ProvinceName.(string)
  1136. item.ResearchStartData = n.ResearchStartDate
  1137. item.ResearchStopData = n.ResearchStopDate
  1138. item.DataTime = shouldDateTimeStr
  1139. err = models.AddLongzhongSurveyData(item)
  1140. if err != nil {
  1141. msg = "新增调研指标数据失败:err " + err.Error()
  1142. return err
  1143. }
  1144. } else {
  1145. fmt.Println("exist:" + key)
  1146. err = models.ModifyLzSurveyDataV1(n.InputValue, int(n.QuotaSampleID), shouldDateTimeStr)
  1147. if err != nil {
  1148. fmt.Println("ModifyLzSurveyDataV1 Err:" + err.Error())
  1149. }
  1150. }
  1151. //key:=strconv.Itoa(int(v.QuotaSampleId))+shouldDateTimeStr
  1152. //if findItem, ok := existMap[key]; !ok && n.InputValue != "0" {
  1153. // fmt.Println("not exist:"+key)
  1154. // item := new(models.LongzhongSurveyData)
  1155. // item.SurveyProductId = v.SurveyProductId
  1156. // item.ProjectQuotaId = n.ProjectQuotaID
  1157. // item.BreedId = n.BreedID
  1158. // item.BreedName = n.BreedName
  1159. // item.QuotaId = n.QuotaID
  1160. // item.QuotaName = n.QuotaName
  1161. // item.UnitId = n.UnitID
  1162. // item.UnitName = n.UnitName
  1163. // item.SampleType = n.SampleType
  1164. // item.SampleId = n.SampleID
  1165. // item.SampleName = n.SampleName
  1166. // item.DeviceId = n.DeviceID
  1167. // item.Device = n.Device
  1168. // item.ProductCraftId = n.ProductCraftID
  1169. // item.ProductCraft = n.ProductCraft
  1170. // item.ProductLine = n.ProductLine
  1171. // item.InputMode = n.InputMode
  1172. // item.Frequency = n.Frequency
  1173. // item.InputValue = n.InputValue
  1174. // item.TaskShouldFinishTime = n.TaskShouldFinishTime
  1175. // item.CustomId = n.CustomID
  1176. // item.CustomType = n.CustomType
  1177. // item.Custom = n.Custom
  1178. // item.QuotaSampleId = n.QuotaSampleID
  1179. // item.TaskActualFinishTime = n.TaskActualFinishTime
  1180. // //item.AreaName = n.AreaName.(string)
  1181. // //item.ProvinceName = n.ProvinceName.(string)
  1182. // item.ResearchStartData = n.ResearchStartDate
  1183. // item.ResearchStopData = n.ResearchStopDate
  1184. // item.DataTime = shouldDateTimeStr
  1185. // err = models.AddLongzhongSurveyData(item)
  1186. // if err != nil {
  1187. // msg = "新增调研指标数据失败:err " + err.Error()
  1188. // return err
  1189. // }
  1190. //} else {
  1191. // fmt.Println("exist:"+key)
  1192. // if findItem != nil && findItem.InputValue != n.InputValue && n.InputValue != "0" {
  1193. // models.ModifyLzSurveyData(n.InputValue, findItem.SurveyDataId)
  1194. // }
  1195. // fmt.Println("exist:", shouldDateTimeStr)
  1196. //}
  1197. }
  1198. pageIndex = pageIndex + 1
  1199. totalPage := utils.PageCount(int(dataList.Response.Total), 100)
  1200. if pageIndex > totalPage {
  1201. break
  1202. }
  1203. }
  1204. }
  1205. item, err := models.GetLzSurveyMaxAndMinInfo(v.SurveyProductId, int(v.QuotaSampleId))
  1206. if err != nil {
  1207. return err
  1208. }
  1209. if item != nil && item.MaxDate != "" && item.MinDate != "" {
  1210. err = models.ModifyLzSurveyMaxAndMinInfo(item, v.SurveyProductId)
  1211. if err != nil {
  1212. return err
  1213. }
  1214. }
  1215. time.Sleep(2 * time.Second)
  1216. }
  1217. lzLock.Unlock()
  1218. fmt.Println("GetLzSurveyProductData start:" + time.Now().Format(utils.FormatDateTime))
  1219. utils.FileLogLz.Info("GetLzSurveyProductData start:" + time.Now().Format(utils.FormatDateTime))
  1220. return
  1221. }