express_order.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package dao
  2. import (
  3. "Cold_Logistic/internal/pkg/common/constant"
  4. "Cold_Logistic/internal/pkg/common/global"
  5. "Cold_Logistic/internal/pkg/utils/sqlutil"
  6. "Cold_Logistic/internal/server/infra/models"
  7. "context"
  8. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
  9. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  10. "time"
  11. )
  12. // ExpressOrderStore 快递单表
  13. type ExpressOrderStore interface {
  14. DbBaseStore
  15. MyPage(ctx context.Context, dto ExpressOrderPageDTO) ([]MyOrderPageVO, int64, error)
  16. ManagePage(ctx context.Context, dto ExpressOrderPageDTO) ([]ManagePageVO, int64, error)
  17. FindByOrderNo(ctx context.Context, orderNo string) (models.ExpressOrder, error)
  18. UpdateInTransitById(ctx context.Context, id, logisticId int) error
  19. FindOrderSendAddressById(ctx context.Context, id int) (models.AddressBook, error)
  20. FindOrderConsigneeAddressById(ctx context.Context, id int) (models.AddressBook, error)
  21. StaticNumByTime(ctx context.Context, dto StaticOrderNumDTO) ([]StaticNumData, error)
  22. StaticNumByUser(ctx context.Context, dto StaticOrderNumDTO) ([]StaticNumByUserData, int64, error)
  23. }
  24. var _ ExpressOrderStore = &expressOrder{}
  25. type expressOrder struct {
  26. dbBase
  27. }
  28. func newExpressOrder(ds *DataStore) *expressOrder {
  29. return &expressOrder{dbBase: dbBase{
  30. store: ds,
  31. baseEntity: &models.ExpressOrder{},
  32. }}
  33. }
  34. func (ds *DataStore) ExpressOrder() ExpressOrderStore {
  35. return newExpressOrder(ds)
  36. }
  37. type ExpressOrderPageDTO struct {
  38. Page core.Page
  39. OrderNo string // 订单号
  40. OrderStatus []int
  41. PlaceOrderStartTime models.MyTime
  42. PlaceOrderEndTime models.MyTime
  43. }
  44. type MyOrderPageVO struct {
  45. OrderId int `gorm:"column:id" json:"orderId"` //
  46. OrderNo string `gorm:"column:order_no" json:"orderNo"` // 单号
  47. OrderSubNo string `gorm:"-" json:"orderSubNo"` // 订单揽件码
  48. OrderStatus int `gorm:"column:order_status" json:"orderStatus"` // 订单状态
  49. LogisticName string `gorm:"column:logistic_name" json:"logisticName"` // 物流名称
  50. Location string `gorm:"column:location" json:"location"` // 当前所在地
  51. LocationTime models.MyTime `gorm:"column:location_time" json:"locationTime"` // 所在地时间
  52. ContactPerson string `gorm:"column:contact_person" json:"contactPerson"` // 联系人
  53. ContactPhone string `gorm:"column:contact_phone" json:"contactPhone"` // 联系电话
  54. Extend string `gorm:"column:extend" json:"extend"` //
  55. }
  56. // MyPage 我的寄件列表
  57. func (a *expressOrder) MyPage(ctx context.Context, dto ExpressOrderPageDTO) ([]MyOrderPageVO, int64, error) {
  58. db := a.store.optionDB(ctx, a.store.withAccountDataPermis(ctx, "eo.created_by"))
  59. stmt := db.Table(a.baseEntity.TableName()+" AS eo").
  60. Joins("LEFT JOIN logistic_company AS logistic ON logistic.id = eo.logistic_id").
  61. Joins("LEFT JOIN order_logistic_log AS log ON log.id = eo.latest_log_id").
  62. Where("eo.deleted = ?", models.DeleteNo)
  63. stmt.Select("eo.id, eo.order_no, eo.order_status, logistic.name AS logistic_name, " +
  64. "log.created_time AS location_time, log.location, log.contact_phone, log.contact_person, log.extend")
  65. if dto.OrderNo != "" {
  66. stmt.Where("eo.order_no LIKE ?", sqlutil.LikeFormat(dto.OrderNo))
  67. }
  68. if len(dto.OrderStatus) > 0 {
  69. stmt.Where("eo.order_status IN(?)", dto.OrderStatus)
  70. }
  71. if !dto.PlaceOrderStartTime.IsZero() {
  72. stmt.Where("eo.created_time >= ?", dto.PlaceOrderStartTime)
  73. }
  74. if !dto.PlaceOrderEndTime.IsZero() {
  75. stmt.Where("eo.created_time <= ?", dto.PlaceOrderEndTime)
  76. }
  77. var total int64
  78. var ret []MyOrderPageVO
  79. if err := stmt.Count(&total).Error; err != nil {
  80. return nil, 0, err
  81. }
  82. if total == 0 {
  83. return ret, total, nil
  84. }
  85. stmt.Limit(dto.Page.Size).Offset(dto.Page.Offset)
  86. stmt.Order("eo.id desc")
  87. if err := stmt.Find(&ret).Error; err != nil {
  88. return nil, 0, err
  89. }
  90. return ret, total, nil
  91. }
  92. type ManagePageVO struct {
  93. OrderId int `gorm:"column:id" json:"orderId"` //
  94. OrderNo string `gorm:"column:order_no" json:"orderNo"` // 单号
  95. OrderStatus int `gorm:"column:order_status" json:"orderStatus"` // 订单状态
  96. SenderAddressId int `gorm:"column:sender_address_id" json:"-"` //
  97. SenderName string `gorm:"-" json:"senderName"` // 寄件人
  98. ConsigneeAddressId int `gorm:"column:consignee_address_id" json:"-"` //
  99. ConsigneeName string `gorm:"column:consignee_name" json:"consigneeName"` // 收件人
  100. PlaceOrderTime models.MyTime `gorm:"column:create_time" json:"placeOrderTime"` // 下单时间
  101. }
  102. // ManagePage 管理后台订单列表
  103. func (a *expressOrder) ManagePage(ctx context.Context, dto ExpressOrderPageDTO) ([]ManagePageVO, int64, error) {
  104. db := a.store.optionDB(ctx, a.store.withCompanyDataPermis(ctx, "pid"))
  105. stmt := db.Model(a.baseEntity).Where("deleted = ?", models.DeleteNo)
  106. stmt.Select("id, order_no, order_status, sender_address_id, consignee_address_id, created_time")
  107. if dto.OrderNo != "" {
  108. stmt.Where("order_no LIKE ?", sqlutil.LikeFormat(dto.OrderNo))
  109. }
  110. if len(dto.OrderStatus) > 0 {
  111. stmt.Where("order_status IN (?)", dto.OrderStatus)
  112. }
  113. if !dto.PlaceOrderStartTime.IsZero() {
  114. stmt.Where("created_time >= ?", dto.PlaceOrderStartTime)
  115. }
  116. if !dto.PlaceOrderEndTime.IsZero() {
  117. stmt.Where("created_time <= ?", dto.PlaceOrderEndTime)
  118. }
  119. var total int64
  120. var ret []ManagePageVO
  121. if err := stmt.Count(&total).Error; err != nil {
  122. return nil, 0, err
  123. }
  124. if total == 0 {
  125. return ret, total, nil
  126. }
  127. stmt.Limit(dto.Page.Size).Offset(dto.Page.Offset)
  128. stmt.Order("id desc")
  129. if err := stmt.Find(&ret).Error; err != nil {
  130. return nil, 0, err
  131. }
  132. return ret, total, nil
  133. }
  134. func (a *expressOrder) FindByOrderNo(ctx context.Context, orderNo string) (models.ExpressOrder, error) {
  135. ret := models.ExpressOrder{}
  136. err := a.store.optionDB(ctx).Model(&models.ExpressOrder{}).
  137. Where("deleted = ?", models.DeleteNo).
  138. Where("order_no = ?", orderNo).Find(&ret).Error
  139. return ret, errors.WithStackOnce(err)
  140. }
  141. func (a *expressOrder) UpdateInTransitById(ctx context.Context, id, logisticId int) error {
  142. up := map[string]interface{}{
  143. "updated_time": models.NewMyTime(time.Now()),
  144. "updated_by": global.GetTokenInfoFromContext(ctx).AccountId,
  145. "order_status": constant.OrderStatusInTransit,
  146. "logistic_id": logisticId,
  147. "order_sub_no": "",
  148. }
  149. err := a.store.optionDB(ctx).Model(&models.ExpressOrder{}).
  150. Where("deleted = ?", models.DeleteNo).
  151. Where("id = ?", id).
  152. Updates(&up).Error
  153. return errors.WithStackOnce(err)
  154. }
  155. func (a *expressOrder) FindOrderSendAddressById(ctx context.Context, id int) (models.AddressBook, error) {
  156. stmt := a.store.optionDB(ctx).Table(a.baseEntity.TableName()+" AS eo").
  157. Joins("address_book AS book ON book.id = eo.sender_address_id").
  158. Where("eo.deleted = ?", models.DeleteNo).
  159. Where("id = ?", id)
  160. ret := models.AddressBook{}
  161. stmt.Select("province_name, city_name, region_name, address")
  162. err := stmt.Find(&ret).Error
  163. return ret, errors.WithStackOnce(err)
  164. }
  165. func (a *expressOrder) FindOrderConsigneeAddressById(ctx context.Context, id int) (models.AddressBook, error) {
  166. stmt := a.store.optionDB(ctx).Table(a.baseEntity.TableName()+" AS eo").
  167. Joins("address_book AS book ON book.id = eo.consignee_address_id").
  168. Where("eo.deleted = ?", models.DeleteNo).
  169. Where("id = ?", id)
  170. ret := models.AddressBook{}
  171. stmt.Select("province_name, city_name, region_name, address")
  172. err := stmt.Find(&ret).Error
  173. return ret, errors.WithStackOnce(err)
  174. }
  175. type StaticOrderNumDTO struct {
  176. Page core.Page
  177. StatType models.TimeStatType // 年,月,天
  178. TimeStart models.MyTime
  179. TimeEnd models.MyTime
  180. OrderStatus int
  181. }
  182. type StaticNumData struct {
  183. XKey string `gorm:"column:x_key" json:"xKey"`
  184. OrderCount int64 `gorm:"column:order_count" json:"orderCount"`
  185. }
  186. func (a *expressOrder) StaticNumByTime(ctx context.Context, dto StaticOrderNumDTO) ([]StaticNumData, error) {
  187. stmt := a.store.optionDB(ctx, a.store.withCompanyDataPermis(ctx, "pid")).
  188. Model(a.baseEntity).Where("deleted = ?", models.DeleteNo)
  189. group := dto.StatType.Format("created_time", "x_key")
  190. stmt.Select(group, "COUNT(*) AS order_count")
  191. if !dto.TimeStart.IsZero() {
  192. stmt.Where("created_time >= ?", dto.TimeStart)
  193. }
  194. if !dto.TimeEnd.IsZero() {
  195. stmt.Where("created_time <= ?", dto.TimeEnd)
  196. }
  197. if dto.OrderStatus > 0 {
  198. stmt.Where("order_status = ?", dto.OrderStatus)
  199. }
  200. stmt.Order("x_key")
  201. ret := make([]StaticNumData, 0)
  202. err := stmt.Find(&ret).Error
  203. return ret, errors.WithStackOnce(err)
  204. }
  205. type StaticNumByUserData struct {
  206. XKey string `gorm:"column:x_key" json:"xKey"`
  207. XTime string `gorm:"column:x_time" json:"xTime"`
  208. OrderCount int64 `gorm:"column:order_count" json:"orderCount"`
  209. }
  210. func (a *expressOrder) StaticNumByUser(ctx context.Context, dto StaticOrderNumDTO) ([]StaticNumByUserData, int64, error) {
  211. stmt := a.store.optionDB(ctx, a.store.withCompanyDataPermis(ctx, "express_order.pid")).
  212. Table(a.baseEntity.TableName()).
  213. Joins("LEFT JOIN account ON account.id = express_order.created_by").
  214. Where("express_order.deleted = ?", models.DeleteNo)
  215. group := dto.StatType.Format("express_order.created_time", "express_order.x_time")
  216. stmt.Select("account.name AS x_key", group, "COUNT(*) AS order_count")
  217. if !dto.TimeStart.IsZero() {
  218. stmt.Where("express_order.created_time >= ?", dto.TimeStart)
  219. }
  220. if !dto.TimeEnd.IsZero() {
  221. stmt.Where("express_order.created_time <= ?", dto.TimeEnd)
  222. }
  223. if dto.OrderStatus > 0 {
  224. stmt.Where("express_order.order_status = ?", dto.OrderStatus)
  225. }
  226. var total int64
  227. ret := make([]StaticNumByUserData, 0)
  228. if err := stmt.Count(&total).Error; err != nil {
  229. return nil, 0, err
  230. }
  231. if total == 0 {
  232. return ret, 0, nil
  233. }
  234. stmt.Order("x_key, x_time")
  235. stmt.Limit(dto.Page.Size).Offset(dto.Page.Offset)
  236. err := stmt.Find(&ret).Error
  237. return ret, total, errors.WithStackOnce(err)
  238. }