address.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package service
  2. import (
  3. "cold-delivery/app/admin/model"
  4. "cold-delivery/app/admin/service/dto"
  5. "cold-delivery/common/actions"
  6. cDto "cold-delivery/common/dto"
  7. "cold-delivery/common/global"
  8. "errors"
  9. "fmt"
  10. "gogs.baozhida.cn/zoie/OAuth-core/service"
  11. "gorm.io/gorm"
  12. "gorm.io/gorm/clause"
  13. )
  14. type Address struct {
  15. service.Service
  16. }
  17. func AddressNameScopes(name string) func(db *gorm.DB) *gorm.DB {
  18. return func(db *gorm.DB) *gorm.DB {
  19. if len(name) == 0 {
  20. return db
  21. }
  22. name = "%" + name + "%"
  23. return db.Where("phone like ? or address like ? or name like ? ",
  24. name, name, name)
  25. }
  26. }
  27. func AddressDataScopes(dataType string, userId int) func(db *gorm.DB) *gorm.DB {
  28. return func(db *gorm.DB) *gorm.DB {
  29. if dataType == "all" {
  30. return db
  31. }
  32. return db.Where("create_by = ?", userId)
  33. }
  34. }
  35. // GetPage 获取Address列表
  36. func (e *Address) GetPage(c *dto.AddressGetPageReq, list *[]model.Address, count *int64, p *actions.DataPermission) error {
  37. var err error
  38. var data model.Address
  39. var ad model.AddressDefault
  40. err = e.Orm.Model(&ad).Where("user_id = ?", p.UserId).First(&ad).Error
  41. if err != nil && !errors.Is(gorm.ErrRecordNotFound, err) {
  42. e.Log.Errorf("db error: %s", err)
  43. return global.GetFailedErr
  44. }
  45. if ad.AddressId > 0 {
  46. err = e.Orm.Model(&data).
  47. Scopes(
  48. AddressNameScopes(c.Name),
  49. actions.Permission(data.TableName(), p),
  50. //AddressDataScopes(c.DataType, p.UserIds),
  51. cDto.MakeCondition(c.GetNeedSearch()),
  52. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  53. ).
  54. Order(fmt.Sprintf("id = %d desc", ad.AddressId)).
  55. Find(list).Limit(-1).Offset(-1).
  56. Count(count).Error
  57. } else {
  58. err = e.Orm.Model(&data).
  59. Scopes(
  60. AddressNameScopes(c.Name),
  61. actions.Permission(data.TableName(), p),
  62. //AddressDataScopes(c.DataType, p.UserIds),
  63. cDto.MakeCondition(c.GetNeedSearch()),
  64. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  65. ).
  66. Find(list).Limit(-1).Offset(-1).
  67. Count(count).Error
  68. }
  69. if err != nil {
  70. e.Log.Errorf("db error: %s", err)
  71. return global.GetFailedErr
  72. }
  73. if *count > 0 {
  74. if (*list)[0].Id == ad.AddressId {
  75. (*list)[0].IsDefault = true
  76. }
  77. }
  78. return nil
  79. }
  80. // Get 获取Address对象
  81. func (e *Address) Get(d *dto.AddressGetReq, carInfoModel *model.Address, p *actions.DataPermission) error {
  82. var ad model.AddressDefault
  83. err := e.Orm.Model(&ad).Where("user_id = ?", p.UserId).First(&ad).Error
  84. if err != nil && !errors.Is(gorm.ErrRecordNotFound, err) {
  85. e.Log.Errorf("db error: %s", err)
  86. return global.GetFailedErr
  87. }
  88. err = e.Orm.
  89. Scopes(actions.Permission(carInfoModel.TableName(), p)).
  90. First(carInfoModel, d.GetId()).Error
  91. if err != nil {
  92. e.Log.Errorf("db error: %s", err)
  93. if errors.Is(err, gorm.ErrRecordNotFound) {
  94. return global.GetNotFoundOrNoPermissionErr
  95. }
  96. return global.GetFailedErr
  97. }
  98. if ad.AddressId == carInfoModel.Id {
  99. carInfoModel.IsDefault = true
  100. }
  101. return nil
  102. }
  103. // Insert 创建Address对象
  104. func (e *Address) Insert(c *dto.AddressInsertReq) error {
  105. var err error
  106. var data model.Address
  107. tx := e.Orm.Begin()
  108. defer func() {
  109. if err != nil {
  110. tx.Rollback()
  111. } else {
  112. tx.Commit()
  113. }
  114. }()
  115. // 添加地址
  116. c.Generate(&data)
  117. err = tx.Create(&data).Error
  118. if err != nil {
  119. e.Log.Errorf("db error: %s", err)
  120. return global.CreateFailedErr
  121. }
  122. c.Id = data.Id
  123. if c.AddressType == model.AddressTypeSender && c.IsDefault == true {
  124. ad := &model.AddressDefault{UserId: c.CreateBy, AddressId: c.Id}
  125. //err = tx.Model(&ad).
  126. // Where("user_id = ?", c.CreateBy).Update("address_id", c.Id).Error
  127. //if err != nil {
  128. // if errors.Is(gorm.ErrRecordNotFound, err) {
  129. // err = tx.Create(&ad).Error
  130. // if err != nil {
  131. // e.Log.Errorf("db error: %s", err)
  132. // return global.CreateFailedErr
  133. // }
  134. // return nil
  135. // }
  136. // e.Log.Errorf("db error: %s", err)
  137. // return global.CreateFailedErr
  138. //}
  139. err = tx.Debug().Clauses(clause.OnConflict{
  140. Columns: []clause.Column{{Name: "user_id"}},
  141. //DoUpdates: clause.AssignmentColumns([]string{"address_id"}),
  142. UpdateAll: true,
  143. }).Create(&ad).Error
  144. if err != nil {
  145. e.Log.Errorf("db error: %s", err)
  146. return global.CreateFailedErr
  147. }
  148. }
  149. return nil
  150. }
  151. // Update 修改Address对象
  152. func (e *Address) Update(c *dto.AddressUpdateReq, p *actions.DataPermission) error {
  153. var err error
  154. tx := e.Orm.Begin()
  155. defer func() {
  156. if err != nil {
  157. tx.Rollback()
  158. } else {
  159. tx.Commit()
  160. }
  161. }()
  162. var ad model.AddressDefault
  163. err = e.Orm.Model(&ad).Where("user_id = ?", p.UserId).First(&ad).Error
  164. if err != nil && !errors.Is(gorm.ErrRecordNotFound, err) {
  165. e.Log.Errorf("db error: %s", err)
  166. return global.GetFailedErr
  167. }
  168. var carInfoModel = model.Address{}
  169. // 查询地址是否存在
  170. err = e.Orm.Scopes(actions.Permission(carInfoModel.TableName(), p)).
  171. First(&carInfoModel, c.GetId()).Error
  172. if err != nil {
  173. e.Log.Errorf("db error: %s", err)
  174. if errors.Is(err, gorm.ErrRecordNotFound) {
  175. return global.UpdateNotFoundOrNoPermissionErr
  176. }
  177. return global.UpdateFailedErr
  178. }
  179. // 默认地址更换
  180. if c.IsDefault == true && carInfoModel.Id != ad.AddressId && ad.AddressId > 0 {
  181. err = tx.Model(&model.AddressDefault{}).Where("id = ?", ad.Id).
  182. Update("address_id", carInfoModel.Id).Error
  183. if err != nil {
  184. e.Log.Errorf("db error: %s", err)
  185. return global.UpdateFailedErr
  186. }
  187. }
  188. // 取消默认地址
  189. if c.IsDefault == false && carInfoModel.Id == ad.AddressId {
  190. err = tx.Unscoped().Where("id = ?", ad.Id).Delete(&model.AddressDefault{}).Error
  191. if err != nil {
  192. e.Log.Errorf("db error: %s", err)
  193. return global.UpdateFailedErr
  194. }
  195. }
  196. c.Generate(&carInfoModel)
  197. err = tx.Save(&carInfoModel).Error
  198. if err != nil {
  199. e.Log.Errorf("db error: %s", err)
  200. return global.UpdateFailedErr
  201. }
  202. c.Id = carInfoModel.Id
  203. return nil
  204. }
  205. // Remove 删除Address
  206. func (e *Address) Remove(c *dto.AddressDeleteReq, p *actions.DataPermission) error {
  207. var err error
  208. tx := e.Orm.Begin()
  209. defer func() {
  210. if err != nil {
  211. tx.Rollback()
  212. } else {
  213. tx.Commit()
  214. }
  215. }()
  216. var carInfoModel model.Address
  217. // 查询地址是否存在
  218. err = e.Orm.Scopes(actions.Permission(carInfoModel.TableName(), p)).
  219. First(&carInfoModel, c.GetId()).Error
  220. if err != nil {
  221. e.Log.Errorf("db error: %s", err)
  222. if errors.Is(err, gorm.ErrRecordNotFound) {
  223. return global.DeleteNotFoundOrNoPermissionErr
  224. }
  225. return global.DeleteFailedErr
  226. }
  227. db := tx.Delete(&carInfoModel)
  228. if err = db.Error; err != nil {
  229. e.Log.Errorf("db error: %s", err)
  230. return global.DeleteFailedErr
  231. }
  232. if db.RowsAffected == 0 {
  233. return global.DeleteNotFoundOrNoPermissionErr
  234. }
  235. return nil
  236. }