123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package main
- import (
- "box-cost/el"
- "box-cost/model"
- "context"
- "fmt"
- "log"
- "os"
- "time"
- "github.com/spf13/viper"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- "go.mongodb.org/mongo-driver/mongo"
- "go.mongodb.org/mongo-driver/mongo/options"
- )
- const (
- MONGODB_URI = "mongodb://root:boxcost@124.71.139.24:37030/box-cost?authSource=admin"
- CONFIG_FILE = "app.yaml"
- )
- type AppConf struct {
- Plans []string `yaml:"plans"`
- }
- func LoadConfFile() (*AppConf, error) {
- file, err := os.Open(CONFIG_FILE)
- if err != nil {
- fmt.Println("open file err:", err)
- return nil, err
- }
- v := viper.New()
- v.SetConfigType("yaml")
- err = v.ReadConfig(file)
- if err != nil {
- return nil, err
- }
- c := new(AppConf)
- err = v.Unmarshal(c)
- return c, err
- }
- func main() {
- // 读取配置
- conf, err := LoadConfFile()
- if err != nil {
- log.Fatal(err)
- }
- planIds := conf.Plans
- // 连接数据库
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- client, err := mongo.Connect(ctx, options.Client().ApplyURI(MONGODB_URI))
- if err != nil {
- log.Fatal(err)
- }
- defer cancel()
- if err != nil {
- log.Fatal(err)
- }
- defer client.Disconnect(ctx)
- collection := client.Database("box-cost").Collection("product-plan")
- plans := []*model.ProductPlan{}
- for _, planIdstr := range planIds {
- planId, _ := primitive.ObjectIDFromHex(planIdstr)
- fmt.Println(planIdstr)
- plan := &model.ProductPlan{}
- err := collection.FindOne(context.Background(), bson.M{"_id": planId}).Decode(plan)
- if err != nil {
- log.Fatal(err)
- }
- plans = append(plans, plan)
- }
- fmt.Println("更新中,请稍等...")
- // 更新excel
- el.UpdateExcel(client, plans)
- }
|