GORM 高级用法

```
%like%查询
result :=[]*dao.Product{}

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



## 自定义JsonP 类型
type JsonP struct {
	T map[string]interface{}
}

func (p JsonP) Value() (driver.Value, error) {
	if p ==nil{
		return "",nil
	}
	j, _ := json.Marshal(p.T)
	return j, nil
}

func (p *JsonP) Scan(src interface{}) error {
	if p ==nil{
		return errors.New("p is nil")
	}
	source, ok := src.([]byte)
	if !ok {
		return errors.New("Type assertion .([]byte) failed.")
	}

	var i interface{}
	err := json.Unmarshal(source, &i)
	if err != nil {
		return err
	}

	p.T, ok = i.(map[string]interface{})
	if !ok {
		return errors.New("Type assertion .(map[string]interface{}) failed.")
	}

	return nil
}

## 自定义JSONTIIME

type JSONTime struct {
	T time.Time
}

func (t *JSONTime) MarshalJSON() ([]byte, error) {
	//do your serializing here
	stamp := fmt.Sprintf("\"%d\"", t.T.UnixNano()/1e6)
	return []byte(stamp), nil
}

func (t *JSONTime) UnmarshalJSON(b []byte) error {
	if len(b) == 0 {
		return nil
	}
	if b[0] == b[len(b)-1] && b[0] == '"' {
		i, err := strconv.ParseInt(string(b[1:len(b)-1]), 10, 64)
		t.T = time.Unix(i/1e3, (i%1e3)*1e6)
		return err
	} else {
		i, err := strconv.ParseInt(string(b), 10, 64)
		t.T = time.Unix(i/1e3, (i%1e3)*1e6)
		return err
	}
	return nil
}


// sql.Scanner
func (t *JSONTime) Scan(value interface{}) error {
	t.T = value.(time.Time)
	return nil
}

// sql.driver.Valuer, MUST BE (t JSONTime), NOT (t *JSONTime)
func (t JSONTime) Value() (driver.Value, error) {
	if t == nil {
		return nil, nil
	} else {
		return t.T, nil
	}
}


##CallBack

func (u *User) AfterCreate(tx *gorm.DB) (err error) {
    tx.Model(u).Update("role", "admin")
    return
}



func (u *User) AfterCreate(scope *gorm.Scope) (err error) {
  scope.DB().Model(u).Update("role", "admin")
    return
}

##链式查询

func DBGetOrdersByOffset(db *gorm.DB, offset int, m map[string]interface{}, rawq string, order int, ordcond string, limit int, getCount bool) ([]*Order, int) {
	totalCount := 0
	result := make([]*Order, 0)
	db = db.Model(Order{})
	if m != nil {
		db = db.Where(m)
	}
	if len(rawq) > 0 {
		db = db.Where(rawq)
	}
	if order > 0 {
		db = db.Order("id asc")
	} else if order < 0 {
		db = db.Order("id desc")
	} else if len(ordcond) > 0 {
		db = db.Order(ordcond)
	}
	if getCount {
		db.Count(&totalCount)
	}
	if limit > 0 {
		db = db.Limit(limit)
	}
	if db.Offset(offset).Find(&result).RecordNotFound() {
		logs.Error("%v", db.Error)
	}
	return result, totalCount
}

## 事务

func CreateAnimals(db *gorm.DB) err {
  tx := db.Begin()
  // Note the use of tx as the database handle once you are within a transaction

  if tx.Error != nil {
    return err
  }

  if err := tx.Create(&Animal{Name: "Giraffe"}).Error; err != nil {
     tx.Rollback()
     return err
  }

  if err := tx.Create(&Animal{Name: "Lion"}).Error; err != nil {
     tx.Rollback()
     return err
  }

  return tx.Commit().Error
}
```