| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- package address_model
- import (
- "fmt"
- "time"
- "github.com/astaxie/beego"
- "github.com/astaxie/beego/orm"
- )
- const (
- addresses_tablename = "addresses"
- )
- type Address struct {
- Id int64 `orm:"column(id);pk" json:"id"` // int(11)
- WxUserId int64 `orm:"column(wx_user_id);null" json:"wx_user_id"` // int(11)
- UserId int64 `orm:"column(user_id);null" json:"user_id"` // int(11)
- Contact string `orm:"column(contact);null" json:"contact"` // varchar(255)
- Tel string `orm:"column(tel);null" json:"tel"` // varchar(255)
- Address string `orm:"column(address);null" json:"address"` // varchar(255)
- Remark string `orm:"column(remark);null" json:"remark"` // text
- State int64 `orm:"column(state);null" json:"state"` // tinyint(1)
- CreatedAt time.Time `orm:"column(created_at);auto_now_add;type(datetime)" json:"-"` // datetime
- UpdatedAt time.Time `orm:"column(updated_at);auto_now;type(datetime)" json:"-"` // datetime
- Province string `orm:"column(province);null" json:"province"` // varchar(10)
- City string `orm:"column(city);null" json:"city"` // varchar(10)
- Postcode string `orm:"column(postcode);null" json:"postcode"` // varchar(20)
- District string `orm:"column(district);null" json:"district"` // varchar(20)
- }
- func (self *Address) TableName() string {
- return addresses_tablename
- }
- func (self *Address) SetDefault() *Address {
- addresses := GetAddressesByWxUId(self.WxUserId)
- for _, address := range addresses {
- if address.Id == self.Id {
- address.State = 1
- } else {
- address.State = 0
- }
- address.Save()
- }
- return self
- }
- func GetUserAddressById(addressId int64) *Address {
- address := &Address{}
- if err := orm.NewOrm().QueryTable(address).Filter("id", addressId).Limit(1).
- One(address); err != nil {
- beego.BeeLogger.Error("get address by id=[%s] err=[%s]", addressId, err)
- return nil
- }
- return address
- }
- // 找出微信用户的默认地址
- func GetUserDefaultAddress(wxUId int64) *Address {
- item := &Address{}
- if err := orm.NewOrm().QueryTable(item).
- Filter("wx_user_id", wxUId).OrderBy("-state").
- Limit(1).One(item); err != nil {
- // beego.BeeLogger.Error("get user default address user_id=[%d] err=[%s]", uId, err)
- return nil
- }
- return item
- }
- // 找出用户的所有地址
- func GetAddressesByWxUId(wxUId int64) (items []*Address) {
- o := orm.NewOrm()
- _, err := o.QueryTable(new(Address)).
- Filter("wx_user_id", wxUId).All(&items)
- if err != nil {
- beego.BeeLogger.Error("GetAddressesByWxUId err=%s", wxUId, err)
- return nil
- }
- return items
- }
- // 根据userid, id找地址
- func GetAddressByWxUIdAndId(wxUId, id int64) *Address {
- item := &Address{}
- if err := orm.NewOrm().QueryTable(item).
- Filter("wx_user_id", wxUId).Filter("id", id).
- Limit(1).One(item); err != nil {
- beego.BeeLogger.Error("get address wx_user_id=[%d] id=[%d] err=[%s]", wxUId, id, err)
- return nil
- }
- return item
- }
- func GetUserAddressList(wxUId int64, sort string) (items []*Address) {
- o := orm.NewOrm()
- orderBy := fmt.Sprintf("-%s", sort)
- _, err := o.QueryTable(new(Address)).
- Filter("wx_user_id", wxUId).OrderBy(orderBy).All(&items)
- if err != nil {
- beego.BeeLogger.Error("GetUserAddressList err=%s", wxUId, err)
- return nil
- }
- return items
- }
- func CreateAddress(wxUId, uId int64, contact, tel, province, city, address, remark, postcode, district string) *Address {
- item := &Address{
- WxUserId: wxUId,
- UserId: uId,
- Contact: contact,
- Tel: tel,
- Province: province,
- City: city,
- Postcode: postcode,
- District: district,
- Address: address,
- Remark: remark}
- id, err := orm.NewOrm().Insert(item)
- if err != nil {
- beego.BeeLogger.Error("insert Address err=[%s]", err)
- return nil
- }
- item.Id = id
- return item
- }
- func (self *Address) Update(contact, tel, province, city, address, remark, postcode, district string) *Address {
- self.Contact = contact
- self.Tel = tel
- self.Province = province
- self.City = city
- self.Address = address
- self.Remark = remark
- self.Postcode = postcode
- self.District = district
- self.Save()
- return self
- }
- func (self *Address) Delete() error {
- o := orm.NewOrm()
- if _, err := o.Delete(&Address{Id: self.Id}); err != nil {
- beego.BeeLogger.Error("Delete Address id=[%d] .err=[%s]", self.Id, err)
- return err
- }
- return nil
- }
- func (self *Address) Save() error {
- if _, err := orm.NewOrm().Update(self); err != nil {
- beego.BeeLogger.Error("Save Address id=[%d] .err=[%s]", self.Id, err)
- return err
- }
- return nil
- }
|