address.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package service
  2. import (
  3. "cold-logistics/app/admin/model"
  4. "cold-logistics/app/admin/service/dto"
  5. "cold-logistics/common/actions"
  6. cDto "cold-logistics/common/dto"
  7. "cold-logistics/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 province_name like ? or city_name like ? or region_name like ?",
  24. name, name, 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. err = e.Orm.Model(&data).
  46. Scopes(
  47. AddressNameScopes(c.Name),
  48. AddressDataScopes(c.DataType, p.UserId),
  49. cDto.MakeCondition(c.GetNeedSearch()),
  50. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  51. ).
  52. Order(fmt.Sprintf("id = %d desc", ad.AddressId)).
  53. Find(list).Limit(-1).Offset(-1).
  54. Count(count).Error
  55. if err != nil {
  56. e.Log.Errorf("db error: %s", err)
  57. return global.GetFailedErr
  58. }
  59. if *count > 0 {
  60. if (*list)[0].Id == ad.AddressId {
  61. (*list)[0].IsDefault = true
  62. }
  63. }
  64. return nil
  65. }
  66. // Get 获取Address对象
  67. func (e *Address) Get(d *dto.AddressGetReq, carInfoModel *model.Address, p *actions.DataPermission) error {
  68. var ad model.AddressDefault
  69. err := e.Orm.Model(&ad).Where("user_id = ?", p.UserId).First(&ad).Error
  70. if err != nil && !errors.Is(gorm.ErrRecordNotFound, err) {
  71. e.Log.Errorf("db error: %s", err)
  72. return global.GetFailedErr
  73. }
  74. err = e.Orm.
  75. Scopes(actions.Permission(carInfoModel.TableName(), p)).
  76. First(carInfoModel, d.GetId()).Error
  77. if err != nil {
  78. e.Log.Errorf("db error: %s", err)
  79. if errors.Is(err, gorm.ErrRecordNotFound) {
  80. return global.GetNotFoundOrNoPermissionErr
  81. }
  82. return global.GetFailedErr
  83. }
  84. if ad.AddressId == carInfoModel.Id {
  85. carInfoModel.IsDefault = true
  86. }
  87. return nil
  88. }
  89. // Insert 创建Address对象
  90. func (e *Address) Insert(c *dto.AddressInsertReq) error {
  91. var err error
  92. var data model.Address
  93. tx := e.Orm.Begin()
  94. defer func() {
  95. if err != nil {
  96. tx.Rollback()
  97. } else {
  98. tx.Commit()
  99. }
  100. }()
  101. // 添加地址
  102. c.Generate(&data)
  103. err = tx.Create(&data).Error
  104. if err != nil {
  105. e.Log.Errorf("db error: %s", err)
  106. return global.CreateFailedErr
  107. }
  108. c.Id = data.Id
  109. if c.AddressType == model.AddressTypeSender && c.IsDefault == true {
  110. ad := &model.AddressDefault{UserId: c.CreateBy, AddressId: c.Id}
  111. //err = tx.Model(&ad).
  112. // Where("user_id = ?", c.CreateBy).Update("address_id", c.Id).Error
  113. //if err != nil {
  114. // if errors.Is(gorm.ErrRecordNotFound, err) {
  115. // err = tx.Create(&ad).Error
  116. // if err != nil {
  117. // e.Log.Errorf("db error: %s", err)
  118. // return global.CreateFailedErr
  119. // }
  120. // return nil
  121. // }
  122. // e.Log.Errorf("db error: %s", err)
  123. // return global.CreateFailedErr
  124. //}
  125. err = tx.Debug().Clauses(clause.OnConflict{
  126. Columns: []clause.Column{{Name: "user_id"}},
  127. //DoUpdates: clause.AssignmentColumns([]string{"address_id"}),
  128. UpdateAll: true,
  129. }).Create(&ad).Error
  130. if err != nil {
  131. e.Log.Errorf("db error: %s", err)
  132. return global.CreateFailedErr
  133. }
  134. }
  135. return nil
  136. }
  137. // Update 修改Address对象
  138. func (e *Address) Update(c *dto.AddressUpdateReq, p *actions.DataPermission) error {
  139. var err error
  140. tx := e.Orm.Begin()
  141. defer func() {
  142. if err != nil {
  143. tx.Rollback()
  144. } else {
  145. tx.Commit()
  146. }
  147. }()
  148. var ad model.AddressDefault
  149. err = e.Orm.Model(&ad).Where("user_id = ?", p.UserId).First(&ad).Error
  150. if err != nil && !errors.Is(gorm.ErrRecordNotFound, err) {
  151. e.Log.Errorf("db error: %s", err)
  152. return global.GetFailedErr
  153. }
  154. var carInfoModel = model.Address{}
  155. // 查询地址是否存在
  156. err = e.Orm.Scopes(actions.Permission(carInfoModel.TableName(), p)).
  157. First(&carInfoModel, c.GetId()).Error
  158. if err != nil {
  159. e.Log.Errorf("db error: %s", err)
  160. if errors.Is(err, gorm.ErrRecordNotFound) {
  161. return global.UpdateNotFoundOrNoPermissionErr
  162. }
  163. return global.UpdateFailedErr
  164. }
  165. // 默认地址更换
  166. if c.IsDefault == true && carInfoModel.Id != ad.AddressId && ad.AddressId > 0 {
  167. err = tx.Model(&model.AddressDefault{}).Where("id = ?", ad.Id).
  168. Update("address_id", carInfoModel.Id).Error
  169. if err != nil {
  170. e.Log.Errorf("db error: %s", err)
  171. return global.UpdateFailedErr
  172. }
  173. }
  174. // 取消默认地址
  175. if c.IsDefault == false && carInfoModel.Id == ad.AddressId {
  176. err = tx.Unscoped().Where("id = ?", ad.Id).Delete(&model.AddressDefault{}).Error
  177. if err != nil {
  178. e.Log.Errorf("db error: %s", err)
  179. return global.UpdateFailedErr
  180. }
  181. }
  182. c.Generate(&carInfoModel)
  183. err = tx.Save(&carInfoModel).Error
  184. if err != nil {
  185. e.Log.Errorf("db error: %s", err)
  186. return global.UpdateFailedErr
  187. }
  188. c.Id = carInfoModel.Id
  189. return nil
  190. }
  191. // Remove 删除Address
  192. func (e *Address) Remove(c *dto.AddressDeleteReq, p *actions.DataPermission) error {
  193. var err error
  194. tx := e.Orm.Begin()
  195. defer func() {
  196. if err != nil {
  197. tx.Rollback()
  198. } else {
  199. tx.Commit()
  200. }
  201. }()
  202. var carInfoModel model.Address
  203. // 查询地址是否存在
  204. err = e.Orm.Scopes(actions.Permission(carInfoModel.TableName(), p)).
  205. First(&carInfoModel, c.GetId()).Error
  206. if err != nil {
  207. e.Log.Errorf("db error: %s", err)
  208. if errors.Is(err, gorm.ErrRecordNotFound) {
  209. return global.DeleteNotFoundOrNoPermissionErr
  210. }
  211. return global.DeleteFailedErr
  212. }
  213. db := tx.Delete(&carInfoModel)
  214. if err = db.Error; err != nil {
  215. e.Log.Errorf("db error: %s", err)
  216. return global.DeleteFailedErr
  217. }
  218. if db.RowsAffected == 0 {
  219. return global.DeleteNotFoundOrNoPermissionErr
  220. }
  221. return nil
  222. }