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 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{}) 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"] = "小计" } 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() 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) } baseInfo := tmp["v_baseinfo"].(map[string]interface{}) tagInfo := tmp["v_taginfo"].(map[string]interface{}) if ExportType == 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" { id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"])) row.AddCell().SetValue(id) } else { row.AddCell().SetValue(baseInfo[v]) } } } else if ExportType == 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" { 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" { 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" { id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"])) row.AddCell().SetValue(id) } else { row.AddCell().SetValue(baseInfo[v]) } } } } else { util.Debug("是否标注isMark字段值有问题~", IsMark) break } } else if ExportType == 2 { //if IsMark == 1 { //} 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" && bidamount != 0 { 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" { 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" { id := SE.EncodeString(mongodb.BsonIdToSId(tmp["_id"])) row.AddCell().SetValue(id) } else { row.AddCell().SetValue(baseInfo[v]) } } } } } util.Debug("over ---", count) fname := fmt.Sprintf("./数据导出%s.xlsx", util.NowFormat(util.DATEFORMAT)) err = file.Save(fname) if err != nil { panic(err) } }