123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- package main
- import (
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/date"
- "app.yhyue.com/moapp/jybase/encrypt"
- "app.yhyue.com/moapp/jybase/mongodb"
- "fmt"
- "github.com/shopspring/decimal"
- "github.com/tealeg/xlsx"
- "log"
- "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 = encrypt.SimpleEncrypt{Key: "topJYBX2019"}
- )
- func init() {
- common.ReadConfig(&Sysconfig)
- Mgo = &mongodb.MongodbSim{
- MongodbAddr: Sysconfig["mgoAddr"].(string),
- Size: common.IntAllDef(Sysconfig["mgoSize"], 5),
- DbName: Sysconfig["mgoDbName"].(string),
- }
- Mgo.InitPool()
- Dbname = Sysconfig["mgoDbName"].(string)
- DbColl = Sysconfig["mgoColl"].(string)
- ExportType = common.IntAll(Sysconfig["exportType"])
- IsMark = common.IntAll(Sysconfig["isMark"])
- Search = Sysconfig["search"].(map[string]interface{})
- FieldsArr = common.ObjArrToStringArr(Sysconfig["fieldsSort"].([]interface{}))
- Fields = Sysconfig["fields"].(map[string]interface{})
- FiedlsPurchase = common.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"] = "采购意向详情链接"
- }
- log.Println(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)
- log.Println("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 {
- log.Println("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" || v == "signendtime" || v == "docendtime" ||
- v == "bidendtime" {
- str := ""
- if baseInfo[v] != nil {
- dd := common.Int64All(baseInfo[v])
- str = date.FormatDateByInt64(&dd, date.Date_Short_Layout)
- }
- row.AddCell().SetValue(str)
- } else if v == "id" {
- if tmp["id"] != nil {
- id := SE.EncodeString(common.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(common.Float64All(p1["unitprice"])).Mul(decimal.NewFromInt(int64(common.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" || v == "signendtime" || v == "docendtime" ||
- v == "bidendtime" {
- str := ""
- if baseInfo[v] != nil {
- dd := common.Int64All(baseInfo[v])
- str = date.FormatDateByInt64(&dd, date.Date_Short_Layout)
- }
- row.AddCell().SetValue(str)
- } else if v == "id" {
- if tmp["id"] != nil {
- id := SE.EncodeString(common.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" || v == "signendtime" || v == "docendtime" ||
- v == "bidendtime" {
- str := ""
- if baseInfo[v] != nil {
- dd := common.Int64All(baseInfo[v])
- str = date.FormatDateByInt64(&dd, date.Date_Short_Layout)
- }
- row.AddCell().SetValue(str)
- } else if v == "id" {
- if tmp["id"] != nil {
- id := SE.EncodeString(common.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" || v == "signendtime" || v == "docendtime" ||
- v == "bidendtime" {
- str := ""
- if baseInfo[v] != nil {
- dd := common.Int64All(baseInfo[v])
- str = date.FormatDateByInt64(&dd, date.Date_Short_Layout)
- }
- row.AddCell().SetValue(str)
- } else if v == "id" {
- if tmp["id"] != nil {
- id := SE.EncodeString(common.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(common.Float64All(p1["unitprice"])).Mul(decimal.NewFromInt(int64(common.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" || v == "signendtime" || v == "docendtime" ||
- v == "bidendtime" {
- str := ""
- if baseInfo[v] != nil {
- dd := common.Int64All(baseInfo[v])
- str = date.FormatDateByInt64(&dd, date.Date_Short_Layout)
- }
- row.AddCell().SetValue(str)
- } else if v == "id" {
- if tmp["id"] != nil {
- id := SE.EncodeString(common.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" || v == "signendtime" || v == "docendtime" ||
- v == "bidendtime" {
- str := ""
- if baseInfo[v] != nil {
- dd := common.Int64All(baseInfo[v])
- str = date.FormatDateByInt64(&dd, date.Date_Short_Layout)
- }
- row.AddCell().SetValue(str)
- } else if v == "id" {
- if tmp["id"] != nil {
- id := SE.EncodeString(common.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 common.ObjToString(a1["winner"]) != "" {
- winner = append(winner, common.ObjToString(a1["winner"]))
- }
- bidamount = common.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" || v == "signendtime" || v == "docendtime" ||
- v == "bidendtime" {
- str := ""
- if baseInfo[v] != nil {
- dd := common.Int64All(baseInfo[v])
- str = date.FormatDateByInt64(&dd, date.Date_Short_Layout)
- }
- row.AddCell().SetValue(str)
- } else if v == "id" {
- if tmp["id"] != nil {
- id := SE.EncodeString(common.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" || v == "signendtime" || v == "docendtime" ||
- v == "bidendtime" {
- str := ""
- if baseInfo[v] != nil {
- dd := common.Int64All(baseInfo[v])
- str = date.FormatDateByInt64(&dd, date.Date_Short_Layout)
- }
- row.AddCell().SetValue(str)
- } else if v == "id" {
- if tmp["id"] != nil {
- id := SE.EncodeString(common.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" || v == "signendtime" || v == "docendtime" ||
- v == "bidendtime" {
- str := ""
- if baseInfo[v] != nil {
- dd := common.Int64All(baseInfo[v])
- str = date.FormatDateByInt64(&dd, date.Date_Short_Layout)
- }
- row.AddCell().SetValue(str)
- m[v] = str
- } else if v == "id" {
- if tmp["id"] != nil {
- id := SE.EncodeString(common.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" || v == "signendtime" || v == "docendtime" ||
- v == "bidendtime" {
- str := ""
- if baseInfo[v] != nil {
- dd := common.Int64All(baseInfo[v])
- str = date.FormatDateByInt64(&dd, date.Date_Short_Layout)
- }
- row.AddCell().SetValue(str)
- m[v] = str
- } else if v == "id" {
- if tmp["id"] != nil {
- id := SE.EncodeString(common.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)
- }
- }
- }
- log.Println("over ---", count)
- fname := fmt.Sprintf("./数据导出%s.xlsx", date.NowFormat(date.Date_Short_Layout))
- err = file.Save(fname)
- if err != nil {
- panic(err)
- }
- }
|