address.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package address_model
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/astaxie/beego"
  6. "github.com/astaxie/beego/orm"
  7. )
  8. const (
  9. addresses_tablename = "addresses"
  10. )
  11. type Address struct {
  12. Id int64 `orm:"column(id);pk" json:"id"` // int(11)
  13. WxUserId int64 `orm:"column(wx_user_id);null" json:"wx_user_id"` // int(11)
  14. UserId int64 `orm:"column(user_id);null" json:"user_id"` // int(11)
  15. Contact string `orm:"column(contact);null" json:"contact"` // varchar(255)
  16. Tel string `orm:"column(tel);null" json:"tel"` // varchar(255)
  17. Address string `orm:"column(address);null" json:"address"` // varchar(255)
  18. Remark string `orm:"column(remark);null" json:"remark"` // text
  19. State int64 `orm:"column(state);null" json:"state"` // tinyint(1)
  20. CreatedAt time.Time `orm:"column(created_at);auto_now_add;type(datetime)" json:"-"` // datetime
  21. UpdatedAt time.Time `orm:"column(updated_at);auto_now;type(datetime)" json:"-"` // datetime
  22. Province string `orm:"column(province);null" json:"province"` // varchar(10)
  23. City string `orm:"column(city);null" json:"city"` // varchar(10)
  24. Postcode string `orm:"column(postcode);null" json:"postcode"` // varchar(20)
  25. District string `orm:"column(district);null" json:"district"` // varchar(20)
  26. }
  27. func (self *Address) TableName() string {
  28. return addresses_tablename
  29. }
  30. func (self *Address) SetDefault() *Address {
  31. addresses := GetAddressesByWxUId(self.WxUserId)
  32. for _, address := range addresses {
  33. if address.Id == self.Id {
  34. address.State = 1
  35. } else {
  36. address.State = 0
  37. }
  38. address.Save()
  39. }
  40. return self
  41. }
  42. func GetUserAddressById(addressId int64) *Address {
  43. address := &Address{}
  44. if err := orm.NewOrm().QueryTable(address).Filter("id", addressId).Limit(1).
  45. One(address); err != nil {
  46. beego.BeeLogger.Error("get address by id=[%s] err=[%s]", addressId, err)
  47. return nil
  48. }
  49. return address
  50. }
  51. // 找出微信用户的默认地址
  52. func GetUserDefaultAddress(wxUId int64) *Address {
  53. item := &Address{}
  54. if err := orm.NewOrm().QueryTable(item).
  55. Filter("wx_user_id", wxUId).OrderBy("-state").
  56. Limit(1).One(item); err != nil {
  57. // beego.BeeLogger.Error("get user default address user_id=[%d] err=[%s]", uId, err)
  58. return nil
  59. }
  60. return item
  61. }
  62. // 找出用户的所有地址
  63. func GetAddressesByWxUId(wxUId int64) (items []*Address) {
  64. o := orm.NewOrm()
  65. _, err := o.QueryTable(new(Address)).
  66. Filter("wx_user_id", wxUId).All(&items)
  67. if err != nil {
  68. beego.BeeLogger.Error("GetAddressesByWxUId err=%s", wxUId, err)
  69. return nil
  70. }
  71. return items
  72. }
  73. // 根据userid, id找地址
  74. func GetAddressByWxUIdAndId(wxUId, id int64) *Address {
  75. item := &Address{}
  76. if err := orm.NewOrm().QueryTable(item).
  77. Filter("wx_user_id", wxUId).Filter("id", id).
  78. Limit(1).One(item); err != nil {
  79. beego.BeeLogger.Error("get address wx_user_id=[%d] id=[%d] err=[%s]", wxUId, id, err)
  80. return nil
  81. }
  82. return item
  83. }
  84. func GetUserAddressList(wxUId int64, sort string) (items []*Address) {
  85. o := orm.NewOrm()
  86. orderBy := fmt.Sprintf("-%s", sort)
  87. _, err := o.QueryTable(new(Address)).
  88. Filter("wx_user_id", wxUId).OrderBy(orderBy).All(&items)
  89. if err != nil {
  90. beego.BeeLogger.Error("GetUserAddressList err=%s", wxUId, err)
  91. return nil
  92. }
  93. return items
  94. }
  95. func CreateAddress(wxUId, uId int64, contact, tel, province, city, address, remark, postcode, district string) *Address {
  96. item := &Address{
  97. WxUserId: wxUId,
  98. UserId: uId,
  99. Contact: contact,
  100. Tel: tel,
  101. Province: province,
  102. City: city,
  103. Postcode: postcode,
  104. District: district,
  105. Address: address,
  106. Remark: remark}
  107. id, err := orm.NewOrm().Insert(item)
  108. if err != nil {
  109. beego.BeeLogger.Error("insert Address err=[%s]", err)
  110. return nil
  111. }
  112. item.Id = id
  113. return item
  114. }
  115. func (self *Address) Update(contact, tel, province, city, address, remark, postcode, district string) *Address {
  116. self.Contact = contact
  117. self.Tel = tel
  118. self.Province = province
  119. self.City = city
  120. self.Address = address
  121. self.Remark = remark
  122. self.Postcode = postcode
  123. self.District = district
  124. self.Save()
  125. return self
  126. }
  127. func (self *Address) Delete() error {
  128. o := orm.NewOrm()
  129. if _, err := o.Delete(&Address{Id: self.Id}); err != nil {
  130. beego.BeeLogger.Error("Delete Address id=[%d] .err=[%s]", self.Id, err)
  131. return err
  132. }
  133. return nil
  134. }
  135. func (self *Address) Save() error {
  136. if _, err := orm.NewOrm().Update(self); err != nil {
  137. beego.BeeLogger.Error("Save Address id=[%d] .err=[%s]", self.Id, err)
  138. return err
  139. }
  140. return nil
  141. }