GORM,MySQL,PG

package main

import (
	"GORM/orm/dao"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/postgres"
	"time"
)

type Gist struct {
	Id       int64
	Url      string
	Snip     string
	Kword    string
	Md5      string
	language string
}

func main() {
	const (
		mysqladdr = "root:[email protected](127.0.0.1:3306)/goods?charset=utf8mb4&parseTime=true&loc=Local"

		postgaddr = `host=5432 user=postgres dbname=goods sslmode=disable password=123456`
	)
	//psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", "127.0.0.1", 5432, "postgres", "123456", "goods")

	db, err := gorm.Open("mysql", mysqladdr)
	db.LogMode(true)
	if err != nil {
		fmt.Println(err)
	}
	defer db.Close()

	tt := &dao.Array{[]interface{}{"1","2","3"}}
	c :=&dao.Class{Name:"wangwu",Arrs:tt}
	
	db.Table("class").Create(c)
	r :=&dao.Class{}
	db.Table("class").Where("name =?","李四").First(r)
	fmt.Println(r.Test.T)

	result := &dao.Product{}
	g := &Gist{Url: "https://gist.github.com/43801f3fdf2c3f09409ca6a3288797f5", Kword: "gorm", Md5: "d9b9f1b0c992aff24378c92508d4e881", language: "go"}
	
	db.Table("users").Where(` jstext @@ '北京'`).First(user)
	fmt.Println(user)
	db.Table("gist").Create(g)
	
	db.Table("product").Find(result, map[string]interface{}{"id": 1003})
	fmt.Println(result)
	type Id int64
	db.Table("product").Select("id").Where("id = ?",1001).Find(Id)
	fmt.Println(result)
	//sql := "id = 1001 for update;"
	tx := db.Begin()
	tx.Table("product").Raw("select * from product where id =1001 for update").Find(&result)
	time.Sleep(time.Second*3)
	if err := tx.Table("product").Model(&dao.Product{}).Where("id = ?", 1001).Update("name", "L6").Error; err != nil {
		fmt.Println(err)
	}
	
	tx.Commit()

	if  err := dao.DB().Table("pingtuan").Where(dao.Pingtuan{CreaterId:444}).FirstOrCreate(&tuan).Error;err !=nil{
		fmt.Println(err)
	}
	
	fmt.Println(tuan)


	var count PaidCount
	if err := dao.DB().Table("miaosha").Raw("SELECT count(1) as num from `miaosha` where UNIX_TIMESTAMP(`during_time`) > ? AND pay_status >= 0 AND tuan_id = ?", ).Count(&count).Error; err != nil {
	
	fmt.Println(err)
	}

	var miaosha dao.Miaosha
	//一个人不能两次重复加入
	filter := map[string]interface{}{}
	filter["tuan_id"] = 37
	filter["user_id"] = 234
	dao.DB().Table("miaosha").Where(filter).First(&miaosha)
	
	fmt.Println(miaosha)

	var orders []dao.Order
	if  err := dao.DB().Table("order").Raw("SELECT ping_id FROM `order` WHERE paid = ? and refunded = ?","true","false").Find(&orders).Error; err !=nil{
		fmt.Println(err)
	}
	
	for _,v := range orders{
		fmt.Println(v.PingId)
	}

	if err := dao.DB().Table("miaosha").Where("id=?",30).Model(&dao.Miaosha{}).Update("pay_status", 1).Error; err != nil {
		fmt.Println(err)
		return
	}

	tuan := &dao.Pingtuan{}
	if err := dao.DB().Table("pingtuan").Where("id = ?", 125).First(tuan).Error; err != nil {
		fmt.Println(err)
	
	}
	查询
	m := map[string]interface{}{"count":"L3提高"}
	aff := dao.DB().Table("product").Model(&dao.Product{}).Where("id = ?", 1002).UpdateColumns(m).RowsAffected
	fmt.Println(aff)

	var count int
	t2 := time.Now().UnixNano() / 1e9
	if err := dao.DB().Table("miaosha").Where(" UNIX_TIMESTAMP(`due_time`) > ? AND pay_status >= 0 AND tuan_id = ?", t2, 143).Count(&count).Error; err != nil {
		fmt.Println(err)
		//	return err, 0
	}

	t := time.Now().Unix()
	fmt.Println(t)
	miaosha := &dao.Miaosha{}
	m := map[string]interface{}{"price":99}
	result := dao.GetProducts(m)
	fmt.Println(result)
	//if err := dao.DB().Table("miaosha").Where("UNIX_TIMESTAMP(`ctime`) <?",t).First(miaosha).Error; err != nil {

	//}
	
	fmt.Println(miaosha)
	type Result struct {
		UserId int
	}
	var users []*Result
	dao.DB().Table("tuan_member").Select("user_id").Where(" tuan_id =?",30).Order("ctime asc").Scan(&users)
	//
	for _,v := range users{
		fmt.Println(v)
	}

	dao.Table("product").Where("name like ?", "%提高%").Find(&result)
	fmt.Println(result)

	为Select语句添加扩展SQL选项
	db.Set("gorm:query_option", "FOR UPDATE").First(&user, 10)
	SELECT * FROM users WHERE id =

}