package main import ( "fmt" "github.com/shopspring/decimal" "github.com/tealeg/xlsx" "mongodb" "qfw/util" "strings" ) var ( Sysconfig map[string]interface{} Mgo *mongodb.MongodbSim Dbname, DbColl string ExportType, IsMark int Search map[string]interface{} Fields map[string]interface{} FieldsArr []string FiedlsPurchase []string SE = util.SimpleEncrypt{Key: "topJYBX2019"} ) func init() { util.ReadConfig(&Sysconfig) Mgo = &mongodb.MongodbSim{ MongodbAddr: Sysconfig["mgoAddr"].(string), Size: util.IntAllDef(Sysconfig["mgoSize"], 5), DbName: Sysconfig["mgoDbName"].(string), } Mgo.InitPool() Dbname = Sysconfig["mgoDbName"].(string) DbColl = Sysconfig["mgoColl"].(string) ExportType = util.IntAll(Sysconfig["exportType"]) IsMark = util.IntAll(Sysconfig["isMark"]) Search = Sysconfig["search"].(map[string]interface{}) FieldsArr = util.ObjArrToStringArr(Sysconfig["fieldsSort"].([]interface{})) Fields = Sysconfig["fields"].(map[string]interface{}) FiedlsPurchase = util.ObjArrToStringArr(Sysconfig["fields_purchase"].([]interface{})) if ExportType == 1 { FieldsArr[len(FieldsArr)-1] = "itemname" FieldsArr = append(FieldsArr, "brandname", "model", "unitname", "unitprice", "number", "totalprice", "id") Fields["itemname"] = "产品名称" Fields["brandname"] = "品牌" Fields["model"] = "规格型号" Fields["unitname"] = "单位" Fields["unitprice"] = "单价" Fields["number"] = "数量" Fields["totalprice"] = "小计" } else if ExportType == 3 { FiedlsPurchase[len(FiedlsPurchase)-1] = "projectname_purchase" FiedlsPurchase = append(FiedlsPurchase, "projectscope_purchase", "item", "buyer_purchase", "remark", "totalprice", "expurasingtime", "cgyxxqhref", "id") Fields["projectname_purchase"] = "采购意向名称" Fields["projectscope_purchase"] = "采购内容" Fields["item"] = "采购品目" Fields["remark"] = "备注" Fields["buyer_purchase"] = "采购单位" Fields["totalprice"] = "预计采购金额(元)" Fields["expurasingtime"] = "预计采购时间" Fields["cgyxxqhref"] = "采购意向详情链接" } util.Debug(FieldsArr) } func main() { sess := Mgo.GetMgoConn() defer Mgo.DestoryMongoConn(sess) file := xlsx.NewFile() sheet, err := file.AddSheet("sheet1") if err != nil { panic(err) } row := sheet.AddRow() if ExportType == 3 { // 标的物 采购意向 for _, v := range FiedlsPurchase { row.AddCell().SetValue(Fields[v]) } } else { for _, v := range FieldsArr { row.AddCell().SetValue(Fields[v]) } } q := map[string]interface{}{} if len(Search) > 0 { q = Search } c := Mgo.Count(DbColl, q) util.Debug("search size result ---", DbColl, q, c) if c == 0 { return } query := sess.DB(Dbname).C(DbColl).Find(&q).Select(nil).Iter() count := 0 for tmp := make(map[string]interface{}); query.Next(&tmp); count++ { if count%500 == 0 { util.Debug("current ---", count) } var baseInfo map[string]interface{} if tmp["v_baseinfo"] != nil { baseInfo = tmp["v_baseinfo"].(map[string]interface{}) } else { baseInfo = tmp } tagInfo, _ := tmp["v_taginfo"].(map[string]interface{}) switch ExportType { case 0: // 不拆分 row := sheet.AddRow() for _, v := range FieldsArr { if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" || v == "signaturedate" || v == "comeintime" || v == "createtime" { str := "" if baseInfo[v] != nil { date := util.Int64All(baseInfo[v]) str = util.FormatDateByInt64(&date, util.Date_Short_Layout) } row.AddCell().SetValue(str) } else if v == "id" { if tmp["id"] != nil { id := SE.EncodeString(util.ObjToString(tmp["id"])) row.AddCell().SetValue(id) } else { id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"])) row.AddCell().SetValue(id) } } else { row.AddCell().SetValue(baseInfo[v]) } } case 1: // 拆分标的物 if IsMark == 1 { if tagInfo["purchasinglist"] != nil { if baseInfo["purchasinglist"] != nil { plist := baseInfo["purchasinglist"].([]interface{}) for _, p := range plist { row := sheet.AddRow() p1 := p.(map[string]interface{}) for _, v := range FieldsArr { if v == "itemname" || v == "brandname" || v == "model" || v == "unitname" || v == "unitprice" || v == "number" { row.AddCell().SetValue(p1[v]) } else if v == "totalprice" { if p1["totalprice"] != nil { row.AddCell().SetValue(p1[v]) } else { if p1["unitprice"] != nil && p1["number"] != nil { d1 := decimal.NewFromFloat(util.Float64All(p1["unitprice"])).Mul(decimal.NewFromInt(int64(util.IntAll(p1["number"])))) row.AddCell().SetValue(d1) } else { row.AddCell().SetValue("") } } } else if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" || v == "signaturedate" || v == "comeintime" || v == "createtime" { str := "" if baseInfo[v] != nil { date := util.Int64All(baseInfo[v]) str = util.FormatDateByInt64(&date, util.Date_Short_Layout) } row.AddCell().SetValue(str) } else if v == "id" { if tmp["id"] != nil { id := SE.EncodeString(util.ObjToString(tmp["id"])) row.AddCell().SetValue(id) } else { id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"])) row.AddCell().SetValue(id) } } else { row.AddCell().SetValue(baseInfo[v]) } } } } else { row := sheet.AddRow() for _, v := range FieldsArr { if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" || v == "signaturedate" || v == "comeintime" || v == "createtime" { str := "" if baseInfo[v] != nil { date := util.Int64All(baseInfo[v]) str = util.FormatDateByInt64(&date, util.Date_Short_Layout) } row.AddCell().SetValue(str) } else if v == "id" { if tmp["id"] != nil { id := SE.EncodeString(util.ObjToString(tmp["id"])) row.AddCell().SetValue(id) } else { id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"])) row.AddCell().SetValue(id) } } else { row.AddCell().SetValue(baseInfo[v]) } } } } else { row := sheet.AddRow() for _, v := range FieldsArr { if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" || v == "signaturedate" || v == "comeintime" || v == "createtime" { str := "" if baseInfo[v] != nil { date := util.Int64All(baseInfo[v]) str = util.FormatDateByInt64(&date, util.Date_Short_Layout) } row.AddCell().SetValue(str) } else if v == "id" { if tmp["id"] != nil { id := SE.EncodeString(util.ObjToString(tmp["id"])) row.AddCell().SetValue(id) } else { id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"])) row.AddCell().SetValue(id) } } else { row.AddCell().SetValue(baseInfo[v]) } } } } else { if baseInfo["purchasinglist"] != nil { plist := baseInfo["purchasinglist"].([]interface{}) for _, p := range plist { row := sheet.AddRow() p1 := p.(map[string]interface{}) for _, v := range FieldsArr { if v == "itemname" || v == "brandname" || v == "model" || v == "unitname" || v == "unitprice" || v == "number" { row.AddCell().SetValue(p1[v]) } else if v == "totalprice" { if p1["totalprice"] != nil { row.AddCell().SetValue(p1[v]) } else { if p1["unitprice"] != nil && p1["number"] != nil { d1 := decimal.NewFromFloat(util.Float64All(p1["unitprice"])).Mul(decimal.NewFromInt(int64(util.IntAll(p1["number"])))) row.AddCell().SetValue(d1) } else { row.AddCell().SetValue("") } } } else if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" || v == "signaturedate" || v == "comeintime" || v == "createtime" { str := "" if baseInfo[v] != nil { date := util.Int64All(baseInfo[v]) str = util.FormatDateByInt64(&date, util.Date_Short_Layout) } row.AddCell().SetValue(str) } else if v == "id" { if tmp["id"] != nil { id := SE.EncodeString(util.ObjToString(tmp["id"])) row.AddCell().SetValue(id) } else { id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"])) row.AddCell().SetValue(id) } } else { row.AddCell().SetValue(baseInfo[v]) } } } } else { row := sheet.AddRow() for _, v := range FieldsArr { if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" || v == "signaturedate" || v == "comeintime" || v == "createtime" { str := "" if baseInfo[v] != nil { date := util.Int64All(baseInfo[v]) str = util.FormatDateByInt64(&date, util.Date_Short_Layout) } row.AddCell().SetValue(str) } else if v == "id" { if tmp["id"] != nil { id := SE.EncodeString(util.ObjToString(tmp["id"])) row.AddCell().SetValue(id) } else { id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"])) row.AddCell().SetValue(id) } } else { row.AddCell().SetValue(baseInfo[v]) } } } break } case 2: // 多包拆分 if baseInfo["package"] != nil { pkg := baseInfo["package"].(map[string]interface{}) for _, p := range pkg { row := sheet.AddRow() p1 := p.(map[string]interface{}) winner := []string{} bidamount := float64(0) if p1["winner_all"] != nil { if all := p1["winner_all"].([]interface{}); all != nil { if len(all) > 0 { for _, a := range all { a1 := a.(map[string]interface{}) if util.ObjToString(a1["winner"]) != "" { winner = append(winner, util.ObjToString(a1["winner"])) } bidamount = util.Float64All(a1["bidamount"]) } } } } for _, v := range FieldsArr { if v == "s_winner" && len(winner) > 0 { row.AddCell().SetValue(strings.Join(winner, ",")) } else if v == "bidamount" { row.AddCell().SetValue(bidamount) } else if v == "winnerperson" || v == "winnertel" { row.AddCell().SetValue("") } else if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" || v == "signaturedate" || v == "comeintime" || v == "createtime" { str := "" if baseInfo[v] != nil { date := util.Int64All(baseInfo[v]) str = util.FormatDateByInt64(&date, util.Date_Short_Layout) } row.AddCell().SetValue(str) } else if v == "id" { if tmp["id"] != nil { id := SE.EncodeString(util.ObjToString(tmp["id"])) row.AddCell().SetValue(id) } else { id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"])) row.AddCell().SetValue(id) } } else { row.AddCell().SetValue(baseInfo[v]) } } } } else { row := sheet.AddRow() for _, v := range FieldsArr { if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" || v == "signaturedate" || v == "comeintime" || v == "createtime" { str := "" if baseInfo[v] != nil { date := util.Int64All(baseInfo[v]) str = util.FormatDateByInt64(&date, util.Date_Short_Layout) } row.AddCell().SetValue(str) } else if v == "id" { if tmp["id"] != nil { id := SE.EncodeString(util.ObjToString(tmp["id"])) row.AddCell().SetValue(id) } else { id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"])) row.AddCell().SetValue(id) } } else { row.AddCell().SetValue(baseInfo[v]) } } } case 3: // 标的物 采购意向数据拆分 if baseInfo["procurementlist"] != nil { plist := baseInfo["procurementlist"].([]interface{}) for _, p := range plist { row := sheet.AddRow() m := make(map[string]interface{}) p1 := p.(map[string]interface{}) for _, v := range FiedlsPurchase { if v == "projectname_purchase" || v == "projectscope_purchase" || v == "item" || v == "buyer_purchase" || v == "totalprice" || v == "expurasingtime" || v == "cgyxxqhref" || v == "remark" { v1 := strings.ReplaceAll(v, "_purchase", "") row.AddCell().SetValue(p1[v1]) m[v] = p1[v1] } else if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" || v == "signaturedate" || v == "comeintime" || v == "createtime" { str := "" if baseInfo[v] != nil { date := util.Int64All(baseInfo[v]) str = util.FormatDateByInt64(&date, util.Date_Short_Layout) } row.AddCell().SetValue(str) m[v] = str } else if v == "id" { if tmp["id"] != nil { id := SE.EncodeString(util.ObjToString(tmp["id"])) row.AddCell().SetValue(id) m[v] = id } else { id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"])) row.AddCell().SetValue(id) m[v] = id } } else { row.AddCell().SetValue(baseInfo[v]) m[v] = baseInfo[v] } } Mgo.Save("bidding_v1", m) } } else { row := sheet.AddRow() m := make(map[string]interface{}) for _, v := range FiedlsPurchase { if v == "publishtime" || v == "bidopentime" || v == "project_startdate" || v == "project_completedate" || v == "signaturedate" || v == "comeintime" || v == "createtime" { str := "" if baseInfo[v] != nil { date := util.Int64All(baseInfo[v]) str = util.FormatDateByInt64(&date, util.Date_Short_Layout) } row.AddCell().SetValue(str) m[v] = str } else if v == "id" { if tmp["id"] != nil { id := SE.EncodeString(util.ObjToString(tmp["id"])) row.AddCell().SetValue(id) m[v] = id } else { id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"])) row.AddCell().SetValue(id) m[v] = id } } else { row.AddCell().SetValue(baseInfo[v]) m[v] = baseInfo[v] } } Mgo.Save(DbColl+"_cf", m) } } } util.Debug("over ---", count) fname := fmt.Sprintf("./数据导出%s.xlsx", util.NowFormat(util.DATEFORMAT)) err = file.Save(fname) if err != nil { panic(err) } }