main.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "mongodb"
  6. "os"
  7. qu "qfw/util"
  8. "qfw/util/elastic"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. // "github.com/lauyoume/gopinyin"
  13. "time"
  14. "github.com/tealeg/xlsx"
  15. )
  16. type Config struct {
  17. MgoAddr string `json:"mgoAddr"`
  18. MgoDbName string `json:"mgoDbName"`
  19. MgoColl string `json:"mgoColl"`
  20. MgoSize int `json:"mgoSize"`
  21. DataType int `json:"dataType"`
  22. ExportType int `json:"exportType"`
  23. TermFind map[string]interface{} `json:"termFind"`
  24. KeyWordFind map[string]map[string]interface{} `json:"keyWordFind"`
  25. Fields map[string]string `json:"fields"`
  26. FieldSort []string `json:"fieldSort"`
  27. }
  28. var (
  29. sysConfig Config
  30. Mgo *mongodb.MongodbSim
  31. SE = qu.SimpleEncrypt{Key: "topJYBX2019"}
  32. ClearHtml = regexp.MustCompile("<[^>]*>")
  33. ClearOther = regexp.MustCompile("[\n\r\\s\u3000\u2003\u00a0]")
  34. Es *elastic.Elastic
  35. MaxWorker = os.Getenv("MAX_WORKERS")
  36. MaxQueue = os.Getenv("MAX_QUEUE")
  37. // industry = []string{"烟草", "军队", "教育", "公安", "税务", "能源", "金融", "数字政府", "水利", "应急管理", "公路", "生态环境", "纪委", "自然资源", "运营商", "机场", "法院", "检察院", "铁路", "轨道交通"}
  38. )
  39. func init() {
  40. qu.ReadConfig(&sysConfig)
  41. log.Println("配置加载成功")
  42. Mgo = &mongodb.MongodbSim{
  43. MongodbAddr: sysConfig.MgoAddr,
  44. DbName: sysConfig.MgoDbName,
  45. Size: sysConfig.MgoSize,
  46. }
  47. Mgo.InitPool()
  48. Es = &elastic.Elastic{
  49. S_esurl: "http://127.0.0.1:9800",
  50. I_size: 15,
  51. }
  52. Es.InitElasticSize()
  53. log.Println("数据库加载成功")
  54. }
  55. //生成xlsx
  56. func GetXlsx(mMap []map[string]interface{}, item string) {
  57. xf, err := xlsx.OpenFile("./fields.xlsx")
  58. if err != nil {
  59. log.Println("fields file not foud", err.Error())
  60. }
  61. dataType := sysConfig.DataType
  62. if dataType == 1 {
  63. sh := xf.Sheets[0]
  64. for i, v := range mMap {
  65. row := sh.AddRow()
  66. row.AddCell().SetInt(i + 1)
  67. row.AddCell().SetValue(v["matchkey"])
  68. row.AddCell().SetValue(v["area"])
  69. row.AddCell().SetValue(v["city"])
  70. row.AddCell().SetValue(v["title"])
  71. row.AddCell().SetValue(v["subtype"])
  72. if v["publishtime"] != nil {
  73. row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02"))
  74. } else {
  75. row.AddCell()
  76. }
  77. row.AddCell().SetValue(v["buyer"])
  78. row.AddCell().SetValue(v["winner"])
  79. if v["bidamount"] != nil {
  80. row.AddCell().SetFloat(qu.Float64All(v["bidamount"]))
  81. } else {
  82. row.AddCell()
  83. }
  84. row.AddCell().SetValue(v["projectname"])
  85. row.AddCell().SetValue(v["detail"])
  86. row.AddCell().SetValue(v["jybxhref"])
  87. ids := SE.EncodeString(qu.ObjToString(v["id"]))
  88. row.AddCell().SetValue(ids)
  89. // row.AddCell().SetValue(v["buyerclass"])
  90. // row.AddCell().SetValue(v["buyer_level"])
  91. // row.AddCell().SetValue(v["s_winner_new"])
  92. // row.AddCell().SetValue(v["winner_level"])
  93. // row.AddCell().SetValue(v["company_area"])
  94. // row.AddCell().SetValue(v["company_city"])
  95. // row.AddCell().SetValue(v["key_all"])
  96. // row.AddCell().SetValue(v["href"])
  97. // row.AddCell().SetValue(v["id_1"])
  98. }
  99. xf.Sheets = xf.Sheets[0:1]
  100. } else if dataType == 2 {
  101. sh := xf.Sheets[1]
  102. for _, v := range mMap {
  103. row := sh.AddRow()
  104. // row.AddCell().SetInt(i + 1)
  105. row.AddCell().SetValue(v["matchkey"])
  106. row.AddCell().SetValue(v["area"])
  107. row.AddCell().SetValue(v["city"])
  108. row.AddCell().SetValue(v["title"])
  109. row.AddCell().SetValue(v["subtype"])
  110. row.AddCell().SetValue(v["detail"])
  111. if v["publishtime"] != nil {
  112. row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02"))
  113. } else {
  114. row.AddCell()
  115. }
  116. row.AddCell().SetValue(v["href"])
  117. row.AddCell().SetValue(v["jybxhref"])
  118. row.AddCell().SetValue(v["projectname"])
  119. row.AddCell().SetValue(v["projectcode"])
  120. row.AddCell().SetValue(v["projectscope"])
  121. if v["budget"] != nil {
  122. row.AddCell().SetFloat(qu.Float64All(v["budget"]))
  123. } else {
  124. row.AddCell()
  125. }
  126. if v["bidamount"] != nil {
  127. row.AddCell().SetFloat(qu.Float64All(v["bidamount"]))
  128. } else {
  129. row.AddCell()
  130. }
  131. if v["bidopentime"] != nil {
  132. row.AddCell().SetValue(time.Unix(qu.Int64All(v["bidopentime"]), 0).Format("2006-01-02"))
  133. } else {
  134. row.AddCell()
  135. }
  136. row.AddCell().SetValue(v["buyer"])
  137. row.AddCell().SetValue(v["buyerperson"])
  138. row.AddCell().SetValue(v["buyertel"])
  139. row.AddCell().SetValue(v["agency"])
  140. row.AddCell().SetValue(v["s_winner"])
  141. row.AddCell().SetValue(v["winnerperson"])
  142. row.AddCell().SetValue(v["winnertel"])
  143. row.AddCell().SetValue(v["legal_person"])
  144. row.AddCell().SetValue(v["company_phone"])
  145. row.AddCell().SetValue(v["company_email"])
  146. ids := SE.EncodeString(qu.ObjToString(v["id"]))
  147. row.AddCell().SetValue(ids)
  148. if v["contact"] != nil {
  149. jsonStr, _ := json.Marshal(v["contact"])
  150. json1 := strings.ReplaceAll(string(jsonStr), "contact_person", "联系人")
  151. json2 := strings.ReplaceAll(json1, "phone", "联系电话")
  152. json3 := strings.ReplaceAll(json2, "winner", "中标企业")
  153. row.AddCell().SetValue(json3)
  154. } else {
  155. row.AddCell()
  156. }
  157. // row.AddCell().SetValue(v["buyerclass"])
  158. // row.AddCell().SetValue(v["filetext"])
  159. // row.AddCell().SetValue(v["company_address"])
  160. // row.AddCell().SetValue(v["tagname"])
  161. // row.AddCell().SetValue(time.Unix(qu.Int64All(v["createtime"]), 0).Format("2006-01-02"))
  162. // projectIds := SE.EncodeString(qu.ObjToString(v["projectId"]))
  163. // row.AddCell().SetValue(projectIds) //项目标识
  164. // row.AddCell().SetValue(v["bud_to_bid"])
  165. // row.AddCell().SetValue(v["company_area"])
  166. // row.AddCell().SetValue(v["company_city"])
  167. // row.AddCell().SetValue(v["company_district"])
  168. // row.AddCell().SetValue(v["company_address"])
  169. }
  170. xf.Sheets = xf.Sheets[1:2]
  171. } else if dataType == 3 {
  172. sh := xf.Sheets[2]
  173. for _, v := range mMap {
  174. row := sh.AddRow()
  175. // row.AddCell().SetInt(i + 1)
  176. row.AddCell().SetValue(v["departname"])
  177. row.AddCell().SetValue(v["rulename"])
  178. row.AddCell().SetValue(v["matchkey"])
  179. row.AddCell().SetValue(v["toptype"])
  180. row.AddCell().SetValue(v["area"])
  181. row.AddCell().SetValue(v["city"])
  182. row.AddCell().SetValue(v["district"])
  183. row.AddCell().SetValue(v["title"])
  184. row.AddCell().SetValue(v["detail"])
  185. if v["publishtime"] != nil {
  186. row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02"))
  187. } else {
  188. row.AddCell()
  189. }
  190. row.AddCell().SetValue(v["href"])
  191. if v["bidamount"] != nil {
  192. row.AddCell().SetFloat(qu.Float64All(v["bidamount"]))
  193. } else {
  194. row.AddCell()
  195. }
  196. row.AddCell().SetValue(v["purchasing"])
  197. row.AddCell().SetValue(v["buyer"])
  198. row.AddCell().SetValue(v["buyerclass"])
  199. row.AddCell().SetValue(v["buyerperson"])
  200. row.AddCell().SetValue(v["buyertel"])
  201. row.AddCell().SetValue(v["buyer_credit_no"])
  202. row.AddCell().SetValue(v["rank"])
  203. row.AddCell().SetValue(v["s_winner"])
  204. row.AddCell().SetValue(v["legal_person"])
  205. row.AddCell().SetValue(v["company_phone"])
  206. row.AddCell().SetValue(v["winner_credit_no"])
  207. row.AddCell().SetValue(v["company_address"])
  208. row.AddCell().SetValue(v["capital"])
  209. row.AddCell().SetValue(v["establish_date"])
  210. row.AddCell().SetValue(v["business_scope"])
  211. row.AddCell().SetValue(v["stock_name"])
  212. ids := SE.EncodeString(qu.ObjToString(v["id"]))
  213. row.AddCell().SetValue(ids)
  214. }
  215. xf.Sheets = xf.Sheets[2:3]
  216. } else if dataType == 4 {
  217. sh := xf.Sheets[3]
  218. for _, v := range mMap {
  219. row := sh.AddRow()
  220. row.AddCell().SetValue(v["departname"])
  221. row.AddCell().SetValue(v["rulename"])
  222. row.AddCell().SetValue(v["matchkey"])
  223. row.AddCell().SetValue(v["area"])
  224. row.AddCell().SetValue(v["city"])
  225. row.AddCell().SetValue(v["title"])
  226. row.AddCell().SetValue(v["subtype"])
  227. row.AddCell().SetValue(v["detail"])
  228. if v["publishtime"] != nil {
  229. row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02"))
  230. } else {
  231. row.AddCell()
  232. }
  233. row.AddCell().SetValue(v["href"])
  234. row.AddCell().SetValue(v["jybxhref"])
  235. row.AddCell().SetValue(v["projectname"])
  236. row.AddCell().SetValue(v["projectcode"])
  237. row.AddCell().SetValue(v["projectscope"])
  238. if v["budget"] != nil {
  239. row.AddCell().SetFloat(qu.Float64All(v["budget"]))
  240. } else {
  241. row.AddCell()
  242. }
  243. if v["bidamount"] != nil {
  244. row.AddCell().SetFloat(qu.Float64All(v["bidamount"]))
  245. } else {
  246. row.AddCell()
  247. }
  248. if v["bidopentime"] != nil {
  249. row.AddCell().SetValue(time.Unix(qu.Int64All(v["bidopentime"]), 0).Format("2006-01-02"))
  250. } else {
  251. row.AddCell()
  252. }
  253. row.AddCell().SetValue(v["buyer"])
  254. row.AddCell().SetValue(v["buyerperson"])
  255. row.AddCell().SetValue(v["buyertel"])
  256. row.AddCell().SetValue(v["agency"])
  257. row.AddCell().SetValue(v["winner"])
  258. row.AddCell().SetValue(v["winnerperson"])
  259. row.AddCell().SetValue(v["winnertel"])
  260. row.AddCell().SetValue(v["legal_person"])
  261. row.AddCell().SetValue(v["company_phone"])
  262. row.AddCell().SetValue(v["company_email"])
  263. ids := SE.EncodeString(qu.ObjToString(v["id"]))
  264. row.AddCell().SetValue(ids)
  265. row.AddCell().SetValue(v["company_address"])
  266. jsonStr, _ := json.Marshal(v["contact"])
  267. row.AddCell().SetValue(jsonStr)
  268. }
  269. xf.Sheets = xf.Sheets[3:4]
  270. } else if dataType == 5 {
  271. sh := xf.Sheets[4]
  272. //if isfile {
  273. // cell := sh.Rows[0].AddCell()
  274. // cell.SetValue("附件")
  275. // cell.SetStyle(style)
  276. //}
  277. for _, v := range mMap {
  278. row := sh.AddRow()
  279. // row.AddCell().SetInt(i + 1)
  280. row.AddCell().SetValue(v["matchkey"]) //信息匹配词
  281. row.AddCell().SetValue(v["city"]) //城市
  282. row.AddCell().SetValue(v["district"]) //县区
  283. row.AddCell().SetValue(v["projectname"]) //项目名称
  284. row.AddCell().SetValue(v["buyer"]) //采购单位
  285. row.AddCell().SetValue(v["tagname"])
  286. // row.AddCell().SetValue(v["buyer_type"]) //采购单位类别---私有标签tagname
  287. if v["bidamount"] != nil { //中标金额
  288. row.AddCell().SetFloat(qu.Float64All(v["bidamount"]))
  289. } else {
  290. row.AddCell()
  291. }
  292. if v["budget"] != nil { //预算
  293. row.AddCell().SetFloat(qu.Float64All(v["budget"]))
  294. } else {
  295. row.AddCell()
  296. }
  297. if v["publishtime"] != nil { //公告发布时间
  298. row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02"))
  299. } else {
  300. row.AddCell()
  301. }
  302. if v["bidopentime"] != nil { //开标日期
  303. row.AddCell().SetValue(time.Unix(qu.Int64All(v["bidopentime"]), 0).Format("2006-01-02"))
  304. } else {
  305. row.AddCell()
  306. }
  307. row.AddCell().SetValue(v["s_winner"]) //中标单位
  308. row.AddCell().SetValue(v["winner_type"]) //中标模式--移动、电信、联通
  309. row.AddCell().SetValue(v["title"]) //公告标题
  310. row.AddCell().SetValue(v["subtype"]) //公告类别
  311. row.AddCell().SetValue(v["area"]) //省份
  312. row.AddCell().SetValue(v["projectcode"]) //项目编号
  313. row.AddCell().SetValue(v["projectscope"]) //项目范围
  314. row.AddCell().SetValue(v["agency"]) //招标代理机构
  315. row.AddCell().SetValue(v["detail"]) //公告内容
  316. row.AddCell().SetValue(v["href"]) //公告地址
  317. row.AddCell().SetValue(v["buyerperson"]) //采购单位联系人
  318. row.AddCell().SetValue(v["buyertel"]) //采购单位联系电话
  319. row.AddCell().SetValue(v["winnerperson"]) //中标单位联系人
  320. row.AddCell().SetValue(v["winnertel"]) //中标单位联系电话
  321. row.AddCell().SetValue(v["legal_person"]) //中标企业联系人
  322. row.AddCell().SetValue(v["company_phone"]) //中标企业联系电话
  323. row.AddCell().SetValue(v["company_email"]) //中标企业邮箱
  324. ids := SE.EncodeString(qu.ObjToString(v["id"])) //唯一标识
  325. row.AddCell().SetValue(ids)
  326. projectIds := SE.EncodeString(qu.ObjToString(v["projectId"]))
  327. row.AddCell().SetValue(projectIds) //项目标识
  328. row.AddCell().SetValue(v["jybxhref"]) //剑鱼标讯地址
  329. //row.AddCell().SetValue(v["projectcode"])
  330. }
  331. xf.Sheets = xf.Sheets[4:5]
  332. } else {
  333. sh := xf.Sheets[5]
  334. for _, v := range mMap {
  335. row := sh.AddRow()
  336. row.AddCell().SetValue(v["title"])
  337. row.AddCell().SetValue(v["toptype"])
  338. row.AddCell().SetValue(v["subtype"])
  339. row.AddCell().SetValue(v["area"])
  340. row.AddCell().SetValue(v["city"])
  341. row.AddCell().SetValue(v["district"])
  342. row.AddCell().SetValue(v["s_topscopeclass"])
  343. row.AddCell().SetValue(v["publishtime_1"])
  344. row.AddCell().SetValue(v["comeintime_1"])
  345. row.AddCell().SetValue(v["projectcode"])
  346. row.AddCell().SetValue(v["buyer"])
  347. row.AddCell().SetValue(v["buyerperson"])
  348. row.AddCell().SetValue(v["buyertel"])
  349. row.AddCell().SetValue(v["buyeraddr"])
  350. row.AddCell().SetValue(v["projectname"])
  351. row.AddCell().SetValue(v["agency"])
  352. row.AddCell().SetValue(v["agencyperson"])
  353. row.AddCell().SetValue(v["agencytel"])
  354. row.AddCell().SetValue(v["agencyaddr"])
  355. row.AddCell().SetValue(v["s_winner"])
  356. row.AddCell().SetValue(v["winnerperson"])
  357. row.AddCell().SetValue(v["winnertel"])
  358. row.AddCell().SetValue(v["winneraddr"])
  359. row.AddCell().SetValue(v["winnerorder"])
  360. row.AddCell().SetValue(v["budget"])
  361. row.AddCell().SetValue(v["bidamount"])
  362. row.AddCell().SetValue(v["href"])
  363. row.AddCell().SetValue(v["site"])
  364. row.AddCell().SetValue(v["detail"])
  365. row.AddCell().SetValue(v["purchasing"])
  366. row.AddCell().SetValue(v["funds"])
  367. row.AddCell().SetValue(v["projectscope"])
  368. row.AddCell().SetValue(v["jybxhref"])
  369. row.AddCell().SetValue(v["id_1"])
  370. // mapB, _ := json.Marshal(v["purchasinglist"])
  371. // row.AddCell().SetValue(string(mapB))
  372. }
  373. xf.Sheets = xf.Sheets[5:6]
  374. }
  375. xf.Sheets[0].Name = "详细数据"
  376. //生文件
  377. // t := strconv.FormatInt(time.Now().Unix(), 10)
  378. t := "20210419"
  379. dir := "./xlsx/" + t + "/"
  380. if b, _ := PathExists(dir); !b {
  381. err1 := os.MkdirAll(dir, os.ModePerm)
  382. if err1 != nil {
  383. log.Println("mkdir err", dir)
  384. }
  385. }
  386. // fname := "_" + t + ".xlsx"
  387. fname := item + ".xlsx"
  388. err = xf.Save(dir + fname)
  389. if err != nil {
  390. log.Println("xls error", fname)
  391. }
  392. }
  393. func GetXlsxs(mMap []map[string]interface{}) {
  394. xf, err := xlsx.OpenFile("./fields.xlsx")
  395. if err != nil {
  396. log.Println("fields file not foud", err.Error())
  397. }
  398. xf.Sheets[0] = &xlsx.Sheet{}
  399. sh := xf.Sheets[0]
  400. rows := sh.AddRow()
  401. for _, f := range sysConfig.FieldSort {
  402. if sysConfig.Fields[f] != "" {
  403. rows.AddCell().SetValue(sysConfig.Fields[f])
  404. }
  405. }
  406. for _, v := range mMap {
  407. row := sh.AddRow()
  408. for _, f := range sysConfig.FieldSort {
  409. cell := row.AddCell()
  410. for kk, vv := range v {
  411. if f == kk {
  412. if f == "publishtime" || f == "bidopentime" || f == "bidendtime" {
  413. if vv != nil {
  414. cell.SetValue(time.Unix(qu.Int64All(vv), 0).Format("2006-01-02"))
  415. }
  416. } else if f == "budget" || f == "bidamount" {
  417. if vv != nil {
  418. cell.SetFloat(qu.Float64All(vv))
  419. }
  420. } else if f == "id" || f == "projectId" {
  421. ids := SE.EncodeString(qu.ObjToString(vv))
  422. cell.SetValue(ids)
  423. } else if f == "contact" || f == "filehref" {
  424. jsonStr, _ := json.Marshal(vv)
  425. cell.SetValue(jsonStr)
  426. } else {
  427. if vv != nil {
  428. cell.SetValue(vv)
  429. }
  430. }
  431. }
  432. }
  433. }
  434. }
  435. xf.Sheets = xf.Sheets[0:1]
  436. xf.Sheets[0].Name = "详细数据"
  437. //生文件
  438. t := strconv.FormatInt(time.Now().Unix(), 10)
  439. dir := "./xlsx/" + t + "/"
  440. if b, _ := PathExists(dir); !b {
  441. err1 := os.MkdirAll(dir, os.ModePerm)
  442. if err1 != nil {
  443. log.Println("mkdir err", dir)
  444. }
  445. }
  446. fname := "_" + t + ".xlsx"
  447. err = xf.Save(dir + fname)
  448. if err != nil {
  449. log.Println("xls error", fname)
  450. }
  451. }
  452. func PathExists(path string) (bool, error) {
  453. _, err := os.Stat(path)
  454. if err == nil {
  455. return true, nil
  456. }
  457. if os.IsNotExist(err) {
  458. return false, nil
  459. }
  460. return false, err
  461. }
  462. func main() {
  463. query := map[string]interface{}{}
  464. log.Println("config", sysConfig)
  465. if sysConfig.TermFind != nil && len(sysConfig.TermFind) > 0 {
  466. for k, v := range sysConfig.TermFind {
  467. query[k] = v
  468. }
  469. }
  470. if sysConfig.KeyWordFind != nil && len(sysConfig.KeyWordFind) > 0 {
  471. for k, v := range sysConfig.KeyWordFind {
  472. keyMap := map[string]interface{}{}
  473. for kk, vv := range v {
  474. keyMap[kk] = vv
  475. }
  476. query[k] = keyMap
  477. }
  478. }
  479. log.Println("查询条件 ", query)
  480. data, ok := Mgo.Find(sysConfig.MgoColl, query, nil, nil, false, -1, -1)
  481. if ok && data != nil && len(*data) > 0 {
  482. log.Println("数据条数 ", len(*data))
  483. if sysConfig.ExportType == 0 {
  484. GetXlsx(*data, "1")
  485. } else if sysConfig.ExportType == 1 {
  486. GetXlsxs(*data)
  487. }
  488. log.Println("生成excel成功")
  489. }
  490. }