package main import ( "encoding/json" "log" "mongodb" "os" qu "qfw/util" "qfw/util/elastic" "regexp" "strconv" "strings" // "github.com/lauyoume/gopinyin" "time" "github.com/tealeg/xlsx" ) type Config struct { MgoAddr string `json:"mgoAddr"` MgoDbName string `json:"mgoDbName"` MgoColl string `json:"mgoColl"` MgoSize int `json:"mgoSize"` DataType int `json:"dataType"` ExportType int `json:"exportType"` TermFind map[string]interface{} `json:"termFind"` KeyWordFind map[string]map[string]interface{} `json:"keyWordFind"` Fields map[string]string `json:"fields"` FieldSort []string `json:"fieldSort"` } var ( sysConfig Config Mgo *mongodb.MongodbSim SE = qu.SimpleEncrypt{Key: "topJYBX2019"} ClearHtml = regexp.MustCompile("<[^>]*>") ClearOther = regexp.MustCompile("[\n\r\\s\u3000\u2003\u00a0]") Es *elastic.Elastic MaxWorker = os.Getenv("MAX_WORKERS") MaxQueue = os.Getenv("MAX_QUEUE") // industry = []string{"烟草", "军队", "教育", "公安", "税务", "能源", "金融", "数字政府", "水利", "应急管理", "公路", "生态环境", "纪委", "自然资源", "运营商", "机场", "法院", "检察院", "铁路", "轨道交通"} ) func init() { qu.ReadConfig(&sysConfig) log.Println("配置加载成功") Mgo = &mongodb.MongodbSim{ MongodbAddr: "172.17.145.163:27083,172.17.4.187:27082", DbName: "mixdata", Size: 20, UserName: "JS3Z_Rbid_ProG", Password: "JS3Z@B5I3aR7Ch", } Mgo.InitPool() log.Println("数据库加载成功") } // 生成xlsx func GetXlsx(mMap []map[string]interface{}, item string) { xf, err := xlsx.OpenFile("./fields.xlsx") if err != nil { log.Println("fields file not foud", err.Error()) } dataType := sysConfig.DataType if dataType == 1 { sh := xf.Sheets[0] for i, v := range mMap { row := sh.AddRow() row.AddCell().SetInt(i + 1) row.AddCell().SetValue(v["matchkey"]) row.AddCell().SetValue(v["area"]) row.AddCell().SetValue(v["city"]) row.AddCell().SetValue(v["title"]) row.AddCell().SetValue(v["subtype"]) if v["publishtime"] != nil { row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02")) } else { row.AddCell() } row.AddCell().SetValue(v["buyer"]) row.AddCell().SetValue(v["winner"]) if v["bidamount"] != nil { row.AddCell().SetFloat(qu.Float64All(v["bidamount"])) } else { row.AddCell() } row.AddCell().SetValue(v["projectname"]) row.AddCell().SetValue(v["detail"]) row.AddCell().SetValue(v["jybxhref"]) ids := SE.EncodeString(qu.ObjToString(v["id"])) row.AddCell().SetValue(ids) // row.AddCell().SetValue(v["buyerclass"]) // row.AddCell().SetValue(v["buyer_level"]) // row.AddCell().SetValue(v["s_winner_new"]) // row.AddCell().SetValue(v["winner_level"]) // row.AddCell().SetValue(v["company_area"]) // row.AddCell().SetValue(v["company_city"]) // row.AddCell().SetValue(v["key_all"]) // row.AddCell().SetValue(v["href"]) // row.AddCell().SetValue(v["id_1"]) } xf.Sheets = xf.Sheets[0:1] } else if dataType == 2 { sh := xf.Sheets[1] for _, v := range mMap { row := sh.AddRow() // row.AddCell().SetInt(i + 1) row.AddCell().SetValue(v["matchkey"]) row.AddCell().SetValue(v["area"]) row.AddCell().SetValue(v["city"]) row.AddCell().SetValue(v["title"]) row.AddCell().SetValue(v["subtype"]) row.AddCell().SetValue(v["detail"]) if v["publishtime"] != nil { row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02")) } else { row.AddCell() } row.AddCell().SetValue(v["href"]) row.AddCell().SetValue(v["jybxhref"]) row.AddCell().SetValue(v["projectname"]) row.AddCell().SetValue(v["projectcode"]) row.AddCell().SetValue(v["projectscope"]) if v["budget"] != nil { row.AddCell().SetFloat(qu.Float64All(v["budget"])) } else { row.AddCell() } if v["bidamount"] != nil { row.AddCell().SetFloat(qu.Float64All(v["bidamount"])) } else { row.AddCell() } if v["bidopentime"] != nil { row.AddCell().SetValue(time.Unix(qu.Int64All(v["bidopentime"]), 0).Format("2006-01-02")) } else { row.AddCell() } row.AddCell().SetValue(v["buyer"]) row.AddCell().SetValue(v["buyerperson"]) row.AddCell().SetValue(v["buyertel"]) row.AddCell().SetValue(v["agency"]) row.AddCell().SetValue(v["s_winner"]) row.AddCell().SetValue(v["winnerperson"]) row.AddCell().SetValue(v["winnertel"]) row.AddCell().SetValue(v["legal_person"]) row.AddCell().SetValue(v["company_phone"]) row.AddCell().SetValue(v["company_email"]) ids := SE.EncodeString(qu.ObjToString(v["id"])) row.AddCell().SetValue(ids) if v["contact"] != nil { jsonStr, _ := json.Marshal(v["contact"]) json1 := strings.ReplaceAll(string(jsonStr), "contact_person", "联系人") json2 := strings.ReplaceAll(json1, "phone", "联系电话") json3 := strings.ReplaceAll(json2, "winner", "中标企业") row.AddCell().SetValue(json3) } else { row.AddCell() } // row.AddCell().SetValue(v["buyerclass"]) // row.AddCell().SetValue(v["filetext"]) // row.AddCell().SetValue(v["company_address"]) // row.AddCell().SetValue(v["tagname"]) // row.AddCell().SetValue(time.Unix(qu.Int64All(v["createtime"]), 0).Format("2006-01-02")) // projectIds := SE.EncodeString(qu.ObjToString(v["projectId"])) // row.AddCell().SetValue(projectIds) //项目标识 // row.AddCell().SetValue(v["bud_to_bid"]) // row.AddCell().SetValue(v["company_area"]) // row.AddCell().SetValue(v["company_city"]) // row.AddCell().SetValue(v["company_district"]) // row.AddCell().SetValue(v["company_address"]) } xf.Sheets = xf.Sheets[1:2] } else if dataType == 3 { sh := xf.Sheets[2] for _, v := range mMap { row := sh.AddRow() // row.AddCell().SetInt(i + 1) row.AddCell().SetValue(v["departname"]) row.AddCell().SetValue(v["rulename"]) row.AddCell().SetValue(v["matchkey"]) row.AddCell().SetValue(v["toptype"]) row.AddCell().SetValue(v["area"]) row.AddCell().SetValue(v["city"]) row.AddCell().SetValue(v["district"]) row.AddCell().SetValue(v["title"]) row.AddCell().SetValue(v["detail"]) if v["publishtime"] != nil { row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02")) } else { row.AddCell() } row.AddCell().SetValue(v["href"]) if v["bidamount"] != nil { row.AddCell().SetFloat(qu.Float64All(v["bidamount"])) } else { row.AddCell() } row.AddCell().SetValue(v["purchasing"]) row.AddCell().SetValue(v["buyer"]) row.AddCell().SetValue(v["buyerclass"]) row.AddCell().SetValue(v["buyerperson"]) row.AddCell().SetValue(v["buyertel"]) row.AddCell().SetValue(v["buyer_credit_no"]) row.AddCell().SetValue(v["rank"]) row.AddCell().SetValue(v["s_winner"]) row.AddCell().SetValue(v["legal_person"]) row.AddCell().SetValue(v["company_phone"]) row.AddCell().SetValue(v["winner_credit_no"]) row.AddCell().SetValue(v["company_address"]) row.AddCell().SetValue(v["capital"]) row.AddCell().SetValue(v["establish_date"]) row.AddCell().SetValue(v["business_scope"]) row.AddCell().SetValue(v["stock_name"]) ids := SE.EncodeString(qu.ObjToString(v["id"])) row.AddCell().SetValue(ids) } xf.Sheets = xf.Sheets[2:3] } else if dataType == 4 { sh := xf.Sheets[3] for _, v := range mMap { row := sh.AddRow() row.AddCell().SetValue(v["departname"]) row.AddCell().SetValue(v["rulename"]) row.AddCell().SetValue(v["matchkey"]) row.AddCell().SetValue(v["area"]) row.AddCell().SetValue(v["city"]) row.AddCell().SetValue(v["title"]) row.AddCell().SetValue(v["subtype"]) row.AddCell().SetValue(v["detail"]) if v["publishtime"] != nil { row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02")) } else { row.AddCell() } row.AddCell().SetValue(v["href"]) row.AddCell().SetValue(v["jybxhref"]) row.AddCell().SetValue(v["projectname"]) row.AddCell().SetValue(v["projectcode"]) row.AddCell().SetValue(v["projectscope"]) if v["budget"] != nil { row.AddCell().SetFloat(qu.Float64All(v["budget"])) } else { row.AddCell() } if v["bidamount"] != nil { row.AddCell().SetFloat(qu.Float64All(v["bidamount"])) } else { row.AddCell() } if v["bidopentime"] != nil { row.AddCell().SetValue(time.Unix(qu.Int64All(v["bidopentime"]), 0).Format("2006-01-02")) } else { row.AddCell() } row.AddCell().SetValue(v["buyer"]) row.AddCell().SetValue(v["buyerperson"]) row.AddCell().SetValue(v["buyertel"]) row.AddCell().SetValue(v["agency"]) row.AddCell().SetValue(v["winner"]) row.AddCell().SetValue(v["winnerperson"]) row.AddCell().SetValue(v["winnertel"]) row.AddCell().SetValue(v["legal_person"]) row.AddCell().SetValue(v["company_phone"]) row.AddCell().SetValue(v["company_email"]) ids := SE.EncodeString(qu.ObjToString(v["id"])) row.AddCell().SetValue(ids) row.AddCell().SetValue(v["company_address"]) jsonStr, _ := json.Marshal(v["contact"]) row.AddCell().SetValue(jsonStr) } xf.Sheets = xf.Sheets[3:4] } else if dataType == 5 { sh := xf.Sheets[4] //if isfile { // cell := sh.Rows[0].AddCell() // cell.SetValue("附件") // cell.SetStyle(style) //} for _, v := range mMap { row := sh.AddRow() // row.AddCell().SetInt(i + 1) row.AddCell().SetValue(v["matchkey"]) //信息匹配词 row.AddCell().SetValue(v["city"]) //城市 row.AddCell().SetValue(v["district"]) //县区 row.AddCell().SetValue(v["projectname"]) //项目名称 row.AddCell().SetValue(v["buyer"]) //采购单位 row.AddCell().SetValue(v["tagname"]) // row.AddCell().SetValue(v["buyer_type"]) //采购单位类别---私有标签tagname if v["bidamount"] != nil { //中标金额 row.AddCell().SetFloat(qu.Float64All(v["bidamount"])) } else { row.AddCell() } if v["budget"] != nil { //预算 row.AddCell().SetFloat(qu.Float64All(v["budget"])) } else { row.AddCell() } if v["publishtime"] != nil { //公告发布时间 row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02")) } else { row.AddCell() } if v["bidopentime"] != nil { //开标日期 row.AddCell().SetValue(time.Unix(qu.Int64All(v["bidopentime"]), 0).Format("2006-01-02")) } else { row.AddCell() } row.AddCell().SetValue(v["s_winner"]) //中标单位 row.AddCell().SetValue(v["winner_type"]) //中标模式--移动、电信、联通 row.AddCell().SetValue(v["title"]) //公告标题 row.AddCell().SetValue(v["subtype"]) //公告类别 row.AddCell().SetValue(v["area"]) //省份 row.AddCell().SetValue(v["projectcode"]) //项目编号 row.AddCell().SetValue(v["projectscope"]) //项目范围 row.AddCell().SetValue(v["agency"]) //招标代理机构 row.AddCell().SetValue(v["detail"]) //公告内容 row.AddCell().SetValue(v["href"]) //公告地址 row.AddCell().SetValue(v["buyerperson"]) //采购单位联系人 row.AddCell().SetValue(v["buyertel"]) //采购单位联系电话 row.AddCell().SetValue(v["winnerperson"]) //中标单位联系人 row.AddCell().SetValue(v["winnertel"]) //中标单位联系电话 row.AddCell().SetValue(v["legal_person"]) //中标企业联系人 row.AddCell().SetValue(v["company_phone"]) //中标企业联系电话 row.AddCell().SetValue(v["company_email"]) //中标企业邮箱 ids := SE.EncodeString(qu.ObjToString(v["id"])) //唯一标识 row.AddCell().SetValue(ids) projectIds := SE.EncodeString(qu.ObjToString(v["projectId"])) row.AddCell().SetValue(projectIds) //项目标识 row.AddCell().SetValue(v["jybxhref"]) //剑鱼标讯地址 //row.AddCell().SetValue(v["projectcode"]) } xf.Sheets = xf.Sheets[4:5] } else { sh := xf.Sheets[5] for _, v := range mMap { row := sh.AddRow() row.AddCell().SetValue(v["title"]) row.AddCell().SetValue(v["toptype"]) row.AddCell().SetValue(v["subtype"]) row.AddCell().SetValue(v["area"]) row.AddCell().SetValue(v["city"]) row.AddCell().SetValue(v["district"]) row.AddCell().SetValue(v["s_topscopeclass"]) row.AddCell().SetValue(v["publishtime_1"]) row.AddCell().SetValue(v["comeintime_1"]) row.AddCell().SetValue(v["projectcode"]) row.AddCell().SetValue(v["buyer"]) row.AddCell().SetValue(v["buyerperson"]) row.AddCell().SetValue(v["buyertel"]) row.AddCell().SetValue(v["buyeraddr"]) row.AddCell().SetValue(v["projectname"]) row.AddCell().SetValue(v["agency"]) row.AddCell().SetValue(v["agencyperson"]) row.AddCell().SetValue(v["agencytel"]) row.AddCell().SetValue(v["agencyaddr"]) row.AddCell().SetValue(v["s_winner"]) row.AddCell().SetValue(v["winnerperson"]) row.AddCell().SetValue(v["winnertel"]) row.AddCell().SetValue(v["winneraddr"]) row.AddCell().SetValue(v["winnerorder"]) row.AddCell().SetValue(v["budget"]) row.AddCell().SetValue(v["bidamount"]) row.AddCell().SetValue(v["href"]) row.AddCell().SetValue(v["site"]) row.AddCell().SetValue(v["detail"]) row.AddCell().SetValue(v["purchasing"]) row.AddCell().SetValue(v["funds"]) row.AddCell().SetValue(v["projectscope"]) row.AddCell().SetValue(v["jybxhref"]) row.AddCell().SetValue(v["id_1"]) // mapB, _ := json.Marshal(v["purchasinglist"]) // row.AddCell().SetValue(string(mapB)) } xf.Sheets = xf.Sheets[5:6] } xf.Sheets[0].Name = "详细数据" //生文件 // t := strconv.FormatInt(time.Now().Unix(), 10) t := "20210419" dir := "./xlsx/" + t + "/" if b, _ := PathExists(dir); !b { err1 := os.MkdirAll(dir, os.ModePerm) if err1 != nil { log.Println("mkdir err", dir) } } // fname := "_" + t + ".xlsx" fname := item + ".xlsx" err = xf.Save(dir + fname) if err != nil { log.Println("xls error", fname) } } func GetXlsxs(mMap []map[string]interface{}) { xf, err := xlsx.OpenFile("./fields.xlsx") if err != nil { log.Println("fields file not foud", err.Error()) } xf.Sheets[0] = &xlsx.Sheet{} sh := xf.Sheets[0] rows := sh.AddRow() for _, f := range sysConfig.FieldSort { if sysConfig.Fields[f] != "" { rows.AddCell().SetValue(sysConfig.Fields[f]) } } for _, v := range mMap { v["cancel_date"] = strings.ReplaceAll(qu.ObjToString(v["cancel_date"]), "-", "/") v["establish_date"] = strings.ReplaceAll(qu.ObjToString(v["establish_date"]), "-", "/") v["issue_date"] = strings.ReplaceAll(qu.ObjToString(v["issue_date"]), "-", "/") v["operation_startdate"] = strings.ReplaceAll(qu.ObjToString(v["operation_startdate"]), "-", "/") v["operation_enddate"] = strings.ReplaceAll(qu.ObjToString(v["operation_enddate"]), "-", "/") v["revoke_date"] = strings.ReplaceAll(qu.ObjToString(v["revoke_date"]), "-", "/") v["company_id"] = qu.EncodeArticleId2ByCheck(mongodb.BsonIdToSId(v["_id"])) row := sh.AddRow() for _, f := range sysConfig.FieldSort { cell := row.AddCell() for kk, vvv := range v { if f == kk { if vvv != nil { cell.SetValue(vvv) } } } } delete(v, "_id") } xf.Sheets = xf.Sheets[0:1] xf.Sheets[0].Name = "详细数据" //生文件 t := strconv.FormatInt(time.Now().Unix(), 10) dir := "./xlsx/" + t + "/" if b, _ := PathExists(dir); !b { err1 := os.MkdirAll(dir, os.ModePerm) if err1 != nil { log.Println("mkdir err", dir) } } fname := "_" + t + ".xlsx" err = xf.Save(dir + fname) if err != nil { log.Println("xls error", fname) } } func PathExists(path string) (bool, error) { _, err := os.Stat(path) if err == nil { return true, nil } if os.IsNotExist(err) { return false, nil } return false, err } func main() { query := map[string]interface{}{} log.Println("config", sysConfig) if sysConfig.TermFind != nil && len(sysConfig.TermFind) > 0 { for k, v := range sysConfig.TermFind { query[k] = v } } if sysConfig.KeyWordFind != nil && len(sysConfig.KeyWordFind) > 0 { for k, v := range sysConfig.KeyWordFind { keyMap := map[string]interface{}{} for kk, vv := range v { keyMap[kk] = vv } query[k] = keyMap } } log.Println("查询条件 ", query) count, session := 0, Mgo.GetMgoConn() defer func() { Mgo.DestoryMongoConn(session) }() iter := session.DB("mixdata").C("qyxy_wuhu").Find(&query).Sort("_id").Iter() data := []map[string]interface{}{} thisData := map[string]interface{}{} for { if !iter.Next(&thisData) { break } count++ log.Println("第", count, "条") data = append(data, thisData) } // data, ok := Mgo.Find("qyxy_wuhu", query, nil, `{"_id":1,"winnerorder":1}`, false, -1, -1) // if ok && data != nil && len(*data) > 0 { // log.Println("数据条数 ", len(*data)) // if sysConfig.ExportType == 0 { // GetXlsx(*data, "1") // } else if sysConfig.ExportType == 1 { // GetXlsxs(*data) // } // log.Println("生成excel成功") // } if len(data) > 0 { log.Println("数据条数 ", len(data)) if sysConfig.ExportType == 0 { GetXlsx(data, "1") } else if sysConfig.ExportType == 1 { GetXlsxs(data) } log.Println("生成excel成功") } }