正在显示
16 个修改的文件
包含
1100 行增加
和
22 行删除
@@ -50,3 +50,21 @@ func CreateOrderRepository(options map[string]interface{}) (domain.OrderReposito | @@ -50,3 +50,21 @@ func CreateOrderRepository(options map[string]interface{}) (domain.OrderReposito | ||
50 | } | 50 | } |
51 | return repository.NewOrderRepository(transactionContext) | 51 | return repository.NewOrderRepository(transactionContext) |
52 | } | 52 | } |
53 | + | ||
54 | +//CreateOrderBaseRepository 订单信息 | ||
55 | +func CreateOrderBaseRepository(options map[string]interface{}) (domain.OrderBaseRepository, error) { | ||
56 | + var transactionContext *transaction.TransactionContext | ||
57 | + if value, ok := options["transactionContext"]; ok { | ||
58 | + transactionContext = value.(*transaction.TransactionContext) | ||
59 | + } | ||
60 | + return repository.NewOrderBaseRepository(transactionContext) | ||
61 | +} | ||
62 | + | ||
63 | +//CreateOrderGoodRepository 订单信息 | ||
64 | +func CreateOrderGoodRepository(options map[string]interface{}) (domain.OrderGoodRepository, error) { | ||
65 | + var transactionContext *transaction.TransactionContext | ||
66 | + if value, ok := options["transactionContext"]; ok { | ||
67 | + transactionContext = value.(*transaction.TransactionContext) | ||
68 | + } | ||
69 | + return repository.NewOrderGoodRepository(transactionContext) | ||
70 | +} |
1 | +package command | ||
2 | + | ||
3 | +type CreateOrderCommand struct { | ||
4 | + //订单类型 | ||
5 | + OrderType int `json:"orderType"` | ||
6 | + //订单编号 | ||
7 | + OrderCode string `json:"orderCode"` | ||
8 | + //交货编号 | ||
9 | + DeliveryCode string `json:"deliveryCode"` | ||
10 | + //买家 | ||
11 | + BuyerName string `json:"buyerName"` | ||
12 | + //订单区域信息 | ||
13 | + OrderRegion string `json:"orderRegion"` | ||
14 | + //订单对应的合伙人 | ||
15 | + PartnerId int64 `json:"partnerId"` | ||
16 | + //业务员抽成比例 | ||
17 | + SalesmanBonusPercent float64 `json:"salesmanBonusPercent"` | ||
18 | + //货品 | ||
19 | + Goods []OrderGoodData `json:"goods"` | ||
20 | +} |
1 | +package command | ||
2 | + | ||
3 | +type OrderGoodData struct { | ||
4 | + //货品id | ||
5 | + Id int64 `json:"id"` | ||
6 | + //货品名称 长度可能较长 | ||
7 | + GoodName string `json:"goodName"` | ||
8 | + //预计的货品数量 | ||
9 | + PlanGoodNumber int `json:"planGoodNumber"` | ||
10 | + //调整后的货品数量 | ||
11 | + //业务判定时0是有效值, | ||
12 | + //所以用空串表示无值,转换到数据库中为负值 | ||
13 | + UseGoodNumber int `json:"useGoodNumber"` | ||
14 | + //货品单价 | ||
15 | + Price float64 `json:"price"` | ||
16 | + //合伙人分红比例 | ||
17 | + PartnerBonusPercent float64 `json:"partnerBonusPercent"` | ||
18 | + //分红支付状态 | ||
19 | + BonusStatus int `json:"bonusStatus"` | ||
20 | + //备注信息 | ||
21 | + Remark string `json:"remark"` | ||
22 | +} |
1 | +package command | ||
2 | + | ||
3 | +type UpdateOrderCommand struct { | ||
4 | + //订单类型 | ||
5 | + OrderType int `json:"orderType"` | ||
6 | + //订单编号 | ||
7 | + OrderCode string `json:"orderCode"` | ||
8 | + //交货编号 | ||
9 | + DeliveryCode string `json:"deliveryCode"` | ||
10 | + //买家 | ||
11 | + BuyerName string `json:"buyerName"` | ||
12 | + //订单区域信息 | ||
13 | + OrderRegion string `json:"orderRegion"` | ||
14 | + //订单对应的合伙人 | ||
15 | + PartnerId int64 `json:"partnerId"` | ||
16 | + //业务员抽成比例 | ||
17 | + SalesmanBonusPercent float64 `json:"salesmanBonusPercent"` | ||
18 | + //货品 | ||
19 | + Goods []OrderGoodData `json:"goods"` | ||
20 | +} |
pkg/application/orderinfo/query/get_order.go
0 → 100644
1 | +package query | ||
2 | + | ||
3 | +//ListOrderQuery 获取用户列表 | ||
4 | +type ListOrderQuery struct { | ||
5 | + //合伙人id | ||
6 | + PartnerId int64 `json:"partnerId" ` | ||
7 | + //订单编号 | ||
8 | + OrderCode string `json:"order_code"` | ||
9 | + // 查询偏离量 | ||
10 | + Offset int `json:"offset" ` | ||
11 | + // 查询限制 | ||
12 | + Limit int `json:"limit"` | ||
13 | + //订单类型 | ||
14 | + OrderType int `json:"orderType"` | ||
15 | +} |
1 | +package service | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory" | ||
7 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/command" | ||
8 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/query" | ||
9 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" | ||
10 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib" | ||
11 | +) | ||
12 | + | ||
13 | +//OrderService 管理员相关服务 | ||
14 | +type OrderInfoService struct { | ||
15 | +} | ||
16 | + | ||
17 | +func NewOrderInfoService(option map[string]interface{}) *OrderInfoService { | ||
18 | + newAdminUserService := new(OrderInfoService) | ||
19 | + return newAdminUserService | ||
20 | +} | ||
21 | + | ||
22 | +// PageListOrderBase 获取订单列表 | ||
23 | +func (service OrderInfoService) PageListOrderBase(listOrderQuery query.ListOrderQuery) ([]domain.OrderBase, int, error) { | ||
24 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
25 | + if err != nil { | ||
26 | + return nil, 0, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) | ||
27 | + } | ||
28 | + var ( | ||
29 | + orderRepository domain.OrderBaseRepository | ||
30 | + orders []domain.OrderBase | ||
31 | + cnt int | ||
32 | + ) | ||
33 | + if value, err := factory.CreateOrderBaseRepository(map[string]interface{}{ | ||
34 | + "transactionContext": transactionContext, | ||
35 | + }); err != nil { | ||
36 | + return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
37 | + } else { | ||
38 | + orderRepository = value | ||
39 | + } | ||
40 | + query := domain.OrderFindQuery{ | ||
41 | + PartnerId: listOrderQuery.PartnerId, | ||
42 | + OrderCode: listOrderQuery.OrderCode, | ||
43 | + Offset: listOrderQuery.Offset, | ||
44 | + Limit: listOrderQuery.Limit, | ||
45 | + OrderType: listOrderQuery.OrderType, | ||
46 | + } | ||
47 | + orders, cnt, err = orderRepository.Find(query) | ||
48 | + if err != nil { | ||
49 | + return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
50 | + } | ||
51 | + var PartnerInfoRepository domain.PartnerInfoRepository | ||
52 | + if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{ | ||
53 | + "transactionContext": transactionContext, | ||
54 | + }); err != nil { | ||
55 | + return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
56 | + } | ||
57 | + for i := range orders { | ||
58 | + var partnerData *domain.PartnerInfo | ||
59 | + partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{ | ||
60 | + UserId: orders[i].PartnerId, | ||
61 | + }) | ||
62 | + if err != nil { | ||
63 | + return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
64 | + } | ||
65 | + orders[i].PartnerInfo = partnerData.Partner | ||
66 | + } | ||
67 | + return orders, cnt, nil | ||
68 | +} | ||
69 | + | ||
70 | +//GetOrderDetail 获取订单详情 | ||
71 | +func (service OrderInfoService) GetOrderDetail(getOrderQuery query.GetOrderQuery) (*domain.OrderBase, error) { | ||
72 | + //实际业务 | ||
73 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
74 | + if err != nil { | ||
75 | + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) | ||
76 | + } | ||
77 | + var ( | ||
78 | + orderBaseRepository domain.OrderBaseRepository | ||
79 | + PartnerInfoRepository domain.PartnerInfoRepository | ||
80 | + orderGoodRepository domain.OrderGoodRepository | ||
81 | + order *domain.OrderBase | ||
82 | + ) | ||
83 | + if err = transactionContext.StartTransaction(); err != nil { | ||
84 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
85 | + } | ||
86 | + defer func() { | ||
87 | + transactionContext.RollbackTransaction() | ||
88 | + }() | ||
89 | + if value, err := factory.CreateOrderBaseRepository(map[string]interface{}{ | ||
90 | + "transactionContext": transactionContext, | ||
91 | + }); err != nil { | ||
92 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
93 | + } else { | ||
94 | + orderBaseRepository = value | ||
95 | + } | ||
96 | + if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{ | ||
97 | + "transactionContext": transactionContext, | ||
98 | + }); err != nil { | ||
99 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
100 | + } | ||
101 | + if orderGoodRepository, err = factory.CreateOrderGoodRepository(map[string]interface{}{ | ||
102 | + "transactionContext": transactionContext, | ||
103 | + }); err != nil { | ||
104 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
105 | + } | ||
106 | + order, err = orderBaseRepository.FindOne(domain.OrderFindOneQuery{ | ||
107 | + OrderId: getOrderQuery.OrderId, | ||
108 | + }) | ||
109 | + if err != nil { | ||
110 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
111 | + } | ||
112 | + var ( | ||
113 | + partnerData *domain.PartnerInfo | ||
114 | + goods []domain.OrderGood | ||
115 | + ) | ||
116 | + partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: order.PartnerInfo.Id}) | ||
117 | + if err != nil { | ||
118 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
119 | + } | ||
120 | + order.PartnerInfo = partnerData.Partner | ||
121 | + goods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{ | ||
122 | + OrderId: order.Id, | ||
123 | + }) | ||
124 | + if err != nil { | ||
125 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取订单中的商品列表失败:%s", err)) | ||
126 | + } | ||
127 | + order.Goods = goods | ||
128 | + err = transactionContext.CommitTransaction() | ||
129 | + if err != nil { | ||
130 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
131 | + } | ||
132 | + return order, nil | ||
133 | +} | ||
134 | + | ||
135 | +func (service OrderInfoService) CreateNewOrder(cmd command.CreateOrderCommand) (*domain.OrderBase, error) { | ||
136 | + var ( | ||
137 | + transactionContext, _ = factory.CreateTransactionContext(nil) | ||
138 | + err error | ||
139 | + ) | ||
140 | + | ||
141 | + if err = transactionContext.StartTransaction(); err != nil { | ||
142 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
143 | + } | ||
144 | + defer func() { | ||
145 | + transactionContext.RollbackTransaction() | ||
146 | + }() | ||
147 | + // orderDao, _ := factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext}) | ||
148 | + // ok, err := orderDao.OrderCodeIsExist(command.OrderCode, 0) | ||
149 | + // if err != nil { | ||
150 | + // return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
151 | + // } | ||
152 | + // if ok { | ||
153 | + // return lib.ThrowError(lib.BUSINESS_ERROR, "订单编号已存在") | ||
154 | + // } | ||
155 | + var PartnerInfoRepository domain.PartnerInfoRepository | ||
156 | + if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{ | ||
157 | + "transactionContext": transactionContext, | ||
158 | + }); err != nil { | ||
159 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
160 | + } | ||
161 | + var partnerData *domain.PartnerInfo | ||
162 | + partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: cmd.PartnerId}) | ||
163 | + if err != nil { | ||
164 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("检索合伙人数据失败")) | ||
165 | + } | ||
166 | + var ( | ||
167 | + orderBaseRepository domain.OrderBaseRepository | ||
168 | + orderGoodRepository domain.OrderGoodRepository | ||
169 | + ) | ||
170 | + if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{ | ||
171 | + "transactionContext": transactionContext, | ||
172 | + }); err != nil { | ||
173 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
174 | + } | ||
175 | + if orderGoodRepository, err = factory.CreateOrderGoodRepository(map[string]interface{}{ | ||
176 | + "transactionContext": transactionContext, | ||
177 | + }); err != nil { | ||
178 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
179 | + } | ||
180 | + newOrder := &domain.OrderBase{ | ||
181 | + OrderType: cmd.OrderType, OrderCode: cmd.OrderCode, | ||
182 | + DeliveryCode: "", | ||
183 | + Buyer: &domain.Buyer{ | ||
184 | + BuyerName: cmd.BuyerName, | ||
185 | + }, | ||
186 | + RegionInfo: &domain.RegionInfo{ | ||
187 | + RegionName: cmd.OrderRegion, | ||
188 | + }, | ||
189 | + PartnerId: cmd.PartnerId, | ||
190 | + PartnerInfo: partnerData.Partner, | ||
191 | + SalesmanBonusPercent: cmd.SalesmanBonusPercent, | ||
192 | + } | ||
193 | + var orderGoods []domain.OrderGood | ||
194 | + for _, good := range cmd.Goods { | ||
195 | + m := domain.NewOrderGood() | ||
196 | + m.OrderId = newOrder.Id | ||
197 | + m.GoodName = good.GoodName | ||
198 | + m.PlanGoodNumber = good.PlanGoodNumber | ||
199 | + m.Price = good.Price | ||
200 | + m.PartnerBonusPercent = good.PartnerBonusPercent | ||
201 | + m.Remark = good.Remark | ||
202 | + err = m.Compute() | ||
203 | + if err != nil { | ||
204 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中商品的数值失败:%s", err)) | ||
205 | + } | ||
206 | + orderGoods = append(orderGoods, m) | ||
207 | + } | ||
208 | + newOrder.Goods = orderGoods | ||
209 | + err = newOrder.Compute() | ||
210 | + if err != nil { | ||
211 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中合计的数值失败:%s", err)) | ||
212 | + } | ||
213 | + err = orderBaseRepository.Save(newOrder) | ||
214 | + if err != nil { | ||
215 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单数据失败:%s", err)) | ||
216 | + } | ||
217 | + err = orderGoodRepository.Save(orderGoods) | ||
218 | + if err != nil { | ||
219 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单中的商品数据失败:%s", err)) | ||
220 | + } | ||
221 | + newOrder.Goods = orderGoods | ||
222 | + err = transactionContext.CommitTransaction() | ||
223 | + if err != nil { | ||
224 | + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
225 | + } | ||
226 | + return newOrder, nil | ||
227 | +} | ||
228 | + | ||
229 | +//DeleteOrder 删除订单 | ||
230 | +func (service OrderInfoService) DeleteOrder(orderId int64) error { | ||
231 | + var ( | ||
232 | + transactionContext, _ = factory.CreateTransactionContext(nil) | ||
233 | + err error | ||
234 | + ) | ||
235 | + if err = transactionContext.StartTransaction(); err != nil { | ||
236 | + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
237 | + } | ||
238 | + defer func() { | ||
239 | + transactionContext.RollbackTransaction() | ||
240 | + }() | ||
241 | + var ( | ||
242 | + orderBaseRepository domain.OrderBaseRepository | ||
243 | + orderGoodRepository domain.OrderGoodRepository | ||
244 | + ) | ||
245 | + if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{ | ||
246 | + "transactionContext": transactionContext, | ||
247 | + }); err != nil { | ||
248 | + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
249 | + } | ||
250 | + if orderGoodRepository, err = factory.CreateOrderGoodRepository(map[string]interface{}{ | ||
251 | + "transactionContext": transactionContext, | ||
252 | + }); err != nil { | ||
253 | + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
254 | + } | ||
255 | + err = orderBaseRepository.Remove(orderId) | ||
256 | + if err != nil { | ||
257 | + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("删除订单数据失败:%s", err)) | ||
258 | + } | ||
259 | + err = orderGoodRepository.Remove(orderId) | ||
260 | + if err != nil { | ||
261 | + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("删除订单中商品数据失败:%s", err)) | ||
262 | + } | ||
263 | + err = transactionContext.CommitTransaction() | ||
264 | + if err != nil { | ||
265 | + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
266 | + } | ||
267 | + return nil | ||
268 | +} |
@@ -12,21 +12,6 @@ const ( | @@ -12,21 +12,6 @@ const ( | ||
12 | OrderStatusDeliverAll //全部发货 | 12 | OrderStatusDeliverAll //全部发货 |
13 | ) | 13 | ) |
14 | 14 | ||
15 | -const ( | ||
16 | - OrderReal = iota + 1 //实发订单 | ||
17 | - OrderIntention //意向订单 | ||
18 | -) | ||
19 | - | ||
20 | -//买家 | ||
21 | -type Buyer struct { | ||
22 | - //买家姓名 | ||
23 | - BuyerName string `json:"buyerName"` | ||
24 | - //联系方式 | ||
25 | - ContactInfo string `json:"contactInfo"` | ||
26 | - //收获地址 | ||
27 | - ShippingAddress string `json:"shippingAddress"` | ||
28 | -} | ||
29 | - | ||
30 | type Order struct { | 15 | type Order struct { |
31 | Id int64 `json:"id"` | 16 | Id int64 `json:"id"` |
32 | //订单类型 | 17 | //订单类型 |
@@ -157,11 +142,12 @@ type OrderFindOneQuery struct { | @@ -157,11 +142,12 @@ type OrderFindOneQuery struct { | ||
157 | } | 142 | } |
158 | 143 | ||
159 | type OrderFindQuery struct { | 144 | type OrderFindQuery struct { |
160 | - PartnerId int64 | ||
161 | - OrderCode string | ||
162 | - Offset int | ||
163 | - Limit int | ||
164 | - OrderType int | 145 | + PartnerId int64 |
146 | + OrderCode string | ||
147 | + DeliveryCode string | ||
148 | + Offset int | ||
149 | + Limit int | ||
150 | + OrderType int | ||
165 | } | 151 | } |
166 | 152 | ||
167 | type OrderRepository interface { | 153 | type OrderRepository interface { |
pkg/domain/order_base.go
0 → 100644
1 | +package domain | ||
2 | + | ||
3 | +import ( | ||
4 | + "time" | ||
5 | + | ||
6 | + "github.com/shopspring/decimal" | ||
7 | +) | ||
8 | + | ||
9 | +const ( | ||
10 | + OrderReal = iota + 1 //实发订单 | ||
11 | + OrderIntention //意向订单 | ||
12 | +) | ||
13 | + | ||
14 | +//买家 | ||
15 | +type Buyer struct { | ||
16 | + //买家姓名 | ||
17 | + BuyerName string `json:"buyerName"` | ||
18 | + //联系方式 | ||
19 | + ContactInfo string `json:"contactInfo"` | ||
20 | + //收获地址 | ||
21 | + ShippingAddress string `json:"shippingAddress"` | ||
22 | +} | ||
23 | + | ||
24 | +//OrderBase 订单基础 | ||
25 | +type OrderBase struct { | ||
26 | + //表id | ||
27 | + Id int64 `json:"id"` | ||
28 | + //订单类型 | ||
29 | + OrderType int `json:"orderType"` | ||
30 | + //订单编号 | ||
31 | + OrderCode string `json:"orderCode"` | ||
32 | + //交货编号 | ||
33 | + DeliveryCode string `json:"deliveryCode"` | ||
34 | + //买家 | ||
35 | + Buyer *Buyer `json:"buyer"` | ||
36 | + //订单区域信息 | ||
37 | + RegionInfo *RegionInfo `json:"regionInfo"` | ||
38 | + //订单对应的合伙人 | ||
39 | + PartnerId int64 `json:"partnerId"` | ||
40 | + PartnerInfo Partner `json:"partnerInfo"` | ||
41 | + //业务员抽成比例 | ||
42 | + SalesmanBonusPercent float64 `json:"salesmanBonusPercent"` | ||
43 | + //订单的创建时间 | ||
44 | + CreateTime time.Time `json:"createTime"` | ||
45 | + //发货时间 | ||
46 | + DeliveryTime time.Time `json:"deliveryTime"` | ||
47 | + //更新时间 | ||
48 | + UpdateTime time.Time `json:"updateTime"` | ||
49 | + //货品 | ||
50 | + Goods []OrderGood `json:"goods"` | ||
51 | + //核算订单相关数据 | ||
52 | + OrderCompute OrderCompute `json:"orderCompute"` | ||
53 | +} | ||
54 | + | ||
55 | +type OrderCompute struct { | ||
56 | + //合伙人应收分红 | ||
57 | + PlanPartnerBonus float64 `json:"planPartnerBonus"` | ||
58 | + //调整后的合伙人应收分红 (初始值=-1); | ||
59 | + //业务判定时0是有效值, | ||
60 | + //所以用空串表示无值,转换到数据库中为负值 | ||
61 | + UsePartnerBonus float64 `json:"usePartnerBonus"` | ||
62 | + //合伙人已收分红 | ||
63 | + PartnerBonusHas float64 `json:"partnerBonusHas"` | ||
64 | + //合伙人未收分红 | ||
65 | + PartnerBonusNot float64 `json:"partnerBonusNot"` | ||
66 | + //合伙人分红支出 | ||
67 | + PartnerBonusExpense float64 `json:"partnerBonusExpense"` | ||
68 | + //业务员抽成 | ||
69 | + SalesmanBonus float64 `json:"salesmanBonus"` | ||
70 | + //预计的订单内货品总数 | ||
71 | + PlanOrderCount int `json:"planOrderCount"` | ||
72 | + //预计的订单的总金额 | ||
73 | + PlanOrderAmount float64 `json:"planOrderAmount"` | ||
74 | + //按需使用的订单内货品总数 (初始值=-1) | ||
75 | + //业务判定时0是有效值, | ||
76 | + //所以用空串表示无值,转换到数据库中为负值 | ||
77 | + UseOrderCount int `json:"useOrderCount"` | ||
78 | + //按需使用的订单内货总金额 (初始值=-1) | ||
79 | + //业务判定时0是有效值, | ||
80 | + //所以用空串表示无值,转换到数据库中为负值 | ||
81 | + UseOrderAmount float64 `json:"useOrderAmount"` | ||
82 | +} | ||
83 | + | ||
84 | +//Compute 数据汇总核算 | ||
85 | +func (order *OrderBase) Compute() error { | ||
86 | + planPartnerBonus := decimal.NewFromFloat(0) | ||
87 | + planOrderAmount := decimal.NewFromFloat(0) | ||
88 | + var ( | ||
89 | + planOrderCount int = 0 | ||
90 | + useOrderCount int = 0 | ||
91 | + HasUseOrderCount bool = false | ||
92 | + ) | ||
93 | + usePartnerBonus := decimal.NewFromFloat(0) | ||
94 | + var hasUsePartnerBonus bool = false | ||
95 | + useOrderAmount := decimal.NewFromFloat(0) | ||
96 | + var hasUseOrderAmount bool = false | ||
97 | + partnerBonusHas := decimal.NewFromFloat(0) | ||
98 | + partnerBonusNot := decimal.NewFromFloat(0) | ||
99 | + partnerBonusExpense := decimal.NewFromFloat(0) | ||
100 | + //统计所以货品的值 | ||
101 | + for i := range order.Goods { | ||
102 | + planPartnerBonus.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PlanPartnerBonus)) | ||
103 | + planOrderAmount.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PlanAmount)) | ||
104 | + planOrderCount += order.Goods[i].PlanGoodNumber | ||
105 | + //调整值 非负值得情况 | ||
106 | + goodUseAmount := decimal.NewFromFloat(order.Goods[i].GoodCompute.UseAmount) | ||
107 | + if goodUseAmount.GreaterThanOrEqual(decimal.NewFromFloat(0)) { | ||
108 | + hasUseOrderAmount = true | ||
109 | + useOrderAmount.Add(goodUseAmount) | ||
110 | + } | ||
111 | + goodUsePartnerBonus := decimal.NewFromFloat(order.Goods[i].GoodCompute.UsePartnerBonus) | ||
112 | + if goodUsePartnerBonus.GreaterThanOrEqual(decimal.NewFromFloat(0)) { | ||
113 | + hasUsePartnerBonus = true | ||
114 | + usePartnerBonus.Add(goodUsePartnerBonus) | ||
115 | + } | ||
116 | + if order.Goods[i].UseGoodNumber >= 0 { | ||
117 | + HasUseOrderCount = true | ||
118 | + useOrderCount += order.Goods[i].UseGoodNumber | ||
119 | + } | ||
120 | + partnerBonusHas.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PartnerBonusHas)) | ||
121 | + partnerBonusNot.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PartnerBonusNot)) | ||
122 | + partnerBonusExpense.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PartnerBonusExpense)) | ||
123 | + } | ||
124 | + //汇总赋值 | ||
125 | + order.OrderCompute.PartnerBonusExpense, _ = partnerBonusExpense.Round(2).Float64() | ||
126 | + order.OrderCompute.PartnerBonusHas, _ = partnerBonusHas.Round(2).Float64() | ||
127 | + order.OrderCompute.PartnerBonusNot, _ = partnerBonusNot.Round(2).Float64() | ||
128 | + if hasUsePartnerBonus { | ||
129 | + | ||
130 | + } else { | ||
131 | + | ||
132 | + } | ||
133 | + if hasUseOrderAmount { | ||
134 | + | ||
135 | + } else { | ||
136 | + | ||
137 | + } | ||
138 | + if HasUseOrderCount { | ||
139 | + | ||
140 | + } else { | ||
141 | + | ||
142 | + } | ||
143 | + return nil | ||
144 | +} | ||
145 | + | ||
146 | +type OrderBaseFindOneQuery struct { | ||
147 | + OrderId int64 | ||
148 | +} | ||
149 | + | ||
150 | +type OrderBaseFindQuery struct { | ||
151 | + PartnerId int64 | ||
152 | + OrderCode string | ||
153 | + Offset int | ||
154 | + Limit int | ||
155 | + OrderType int | ||
156 | +} | ||
157 | + | ||
158 | +type OrderBaseRepository interface { | ||
159 | + Save(order *OrderBase) error | ||
160 | + FindOne(qureyOptions OrderFindOneQuery) (*OrderBase, error) | ||
161 | + Find(queryOptions OrderFindQuery) ([]OrderBase, int, error) | ||
162 | + Remove(id int64) error | ||
163 | +} |
pkg/domain/order_good.go
0 → 100644
1 | +package domain | ||
2 | + | ||
3 | +import ( | ||
4 | + "errors" | ||
5 | + | ||
6 | + "github.com/shopspring/decimal" | ||
7 | +) | ||
8 | + | ||
9 | +//分红状态 | ||
10 | +type OrderGoodBonusStatus interface { | ||
11 | + //状态变更为待支付 | ||
12 | + WartPayPartnerBonus(orderGood *OrderGood) error | ||
13 | + //状态变更为已支付 | ||
14 | + PayPartnerBonus(orderGood *OrderGood) error | ||
15 | +} | ||
16 | + | ||
17 | +//货单的支付状态 | ||
18 | +const ( | ||
19 | + //待支付 | ||
20 | + OrderGoodWaitPay int = 1 | ||
21 | + //已支付 | ||
22 | + OrderGoodHasPay int = 2 | ||
23 | +) | ||
24 | + | ||
25 | +//OrderGood 订单中的货品 | ||
26 | +type OrderGood struct { | ||
27 | + //货品id | ||
28 | + Id int64 `json:"id"` | ||
29 | + //所属订单id | ||
30 | + OrderId int64 `json:"orderId"` | ||
31 | + //货品名称 长度可能较长 | ||
32 | + GoodName string `json:"goodName"` | ||
33 | + //预计的货品数量 | ||
34 | + PlanGoodNumber int `json:"planGoodNumber"` | ||
35 | + //调整后的货品数量 | ||
36 | + //业务判定时0是有效值, | ||
37 | + //所以用空串表示无值,转换到数据库中为负值 | ||
38 | + UseGoodNumber int `json:"useGoodNumber"` | ||
39 | + //货品单价 | ||
40 | + Price float64 `json:"price"` | ||
41 | + //合伙人分红比例 | ||
42 | + PartnerBonusPercent float64 `json:"partnerBonusPercent"` | ||
43 | + //分红支付状态 | ||
44 | + BonusStatus int `json:"bonusStatus"` | ||
45 | + //备注信息 | ||
46 | + Remark string `json:"remark"` | ||
47 | + //当前分红支付状态 | ||
48 | + CurrentBonusStatus OrderGoodBonusStatus `json:"-"` | ||
49 | + ///核算订单相关数据 | ||
50 | + GoodCompute GoodCompute `json:"goodCompute"` | ||
51 | +} | ||
52 | + | ||
53 | +type GoodCompute struct { | ||
54 | + //预计的货品总额 | ||
55 | + PlanAmount float64 `json:"planAmount"` | ||
56 | + //调整后的货品总额 (初始值=-1) | ||
57 | + //业务判定时0是有效值, | ||
58 | + //所以用空串表示无值,转换到数据库中为负值 | ||
59 | + UseAmount float64 `json:"useAmount"` | ||
60 | + //预计的合伙人分红 | ||
61 | + PlanPartnerBonus float64 `json:"planPartnerBonus"` | ||
62 | + //合伙人应收分红调整 (初始值=-1), | ||
63 | + //业务判定时0是有效值, | ||
64 | + //所以用空串表示无值,转换到数据库中为负值 | ||
65 | + UsePartnerBonus float64 `json:"usePartnerBonus"` | ||
66 | + //合伙人已收分红 | ||
67 | + PartnerBonusHas float64 `json:"partnerBonusHas"` | ||
68 | + //合伙人未收分红 | ||
69 | + PartnerBonusNot float64 `json:"partnerBonusNot"` | ||
70 | + //合伙人分红支出 | ||
71 | + PartnerBonusExpense float64 `json:"partnerBonusExpense"` | ||
72 | +} | ||
73 | + | ||
74 | +//OrderGoodBonusWaitPay 货品分红待支付 | ||
75 | +type OrderGoodBonusWaitPay struct{} | ||
76 | + | ||
77 | +var _ OrderGoodBonusStatus = (*OrderGoodBonusWaitPay)(nil) | ||
78 | + | ||
79 | +//OrderGoodBonusHasPay 货品分红已支付 | ||
80 | +type OrderGoodBonusHasPay struct{} | ||
81 | + | ||
82 | +var _ OrderGoodBonusStatus = (*OrderGoodBonusHasPay)(nil) | ||
83 | + | ||
84 | +func (waitPay OrderGoodBonusWaitPay) PayPartnerBonus(good *OrderGood) error { | ||
85 | + good.GoodCompute.PartnerBonusExpense = 0 | ||
86 | + good.GoodCompute.PartnerBonusHas = 0 | ||
87 | + //初始状态为待支付时 ,合伙人未收分红等于合伙人应收分红(或者调整的) | ||
88 | + if good.UseGoodNumber < 0 { | ||
89 | + good.GoodCompute.PartnerBonusNot = good.GoodCompute.PlanPartnerBonus | ||
90 | + } else { | ||
91 | + good.GoodCompute.PartnerBonusNot = good.GoodCompute.UsePartnerBonus | ||
92 | + } | ||
93 | + return nil | ||
94 | +} | ||
95 | + | ||
96 | +func (waitPay OrderGoodBonusWaitPay) WartPayPartnerBonus(good *OrderGood) error { | ||
97 | + //待支付状态转支付时 ,合伙人已收收分红等于合伙人应收分红(或者调整的) | ||
98 | + if good.GoodCompute.UsePartnerBonus < 0 { | ||
99 | + good.GoodCompute.PartnerBonusHas = good.GoodCompute.PlanPartnerBonus | ||
100 | + } else { | ||
101 | + good.GoodCompute.PartnerBonusHas = good.GoodCompute.UsePartnerBonus | ||
102 | + } | ||
103 | + good.GoodCompute.PartnerBonusExpense = 0 | ||
104 | + return nil | ||
105 | +} | ||
106 | + | ||
107 | +func (hasPay OrderGoodBonusHasPay) PayPartnerBonus(good *OrderGood) error { | ||
108 | + //已支付的值保持不变 | ||
109 | + //未支付的值保持不变 | ||
110 | + //计算分红支出 | ||
111 | + //分红支出=应收分红(有出现调整,则取调整后的应收分红)和已收分红的差额 | ||
112 | + if good.UseGoodNumber >= 0 { | ||
113 | + good.GoodCompute.PartnerBonusExpense = good.GoodCompute.PartnerBonusHas - good.GoodCompute.UsePartnerBonus | ||
114 | + } | ||
115 | + return nil | ||
116 | +} | ||
117 | + | ||
118 | +func (hasPay OrderGoodBonusHasPay) WartPayPartnerBonus(good *OrderGood) error { | ||
119 | + return errors.New("已支付的货单不能将状态回退为待支付") | ||
120 | +} | ||
121 | + | ||
122 | +//NewOrderGood 初始值设定 | ||
123 | +func NewOrderGood() OrderGood { | ||
124 | + return OrderGood{ | ||
125 | + UseGoodNumber: -1, | ||
126 | + BonusStatus: OrderGoodWaitPay, | ||
127 | + GoodCompute: GoodCompute{ | ||
128 | + UsePartnerBonus: -1, | ||
129 | + UseAmount: -1, | ||
130 | + }, | ||
131 | + CurrentBonusStatus: OrderGoodBonusWaitPay{}, | ||
132 | + } | ||
133 | +} | ||
134 | + | ||
135 | +//Compute 数据汇总核算 | ||
136 | +func (good *OrderGood) Compute() error { | ||
137 | + price := decimal.NewFromInt(int64(good.PlanGoodNumber)) | ||
138 | + planamount := price.Mul(decimal.NewFromInt(int64(good.PlanGoodNumber))) //price*planGoodNumber | ||
139 | + //price*useGoodNumber | ||
140 | + planPartnerBonus := planamount.Mul(decimal.NewFromFloat(good.PartnerBonusPercent)) //price*planGoodNumber*PartnerBonusPercent | ||
141 | + | ||
142 | + good.GoodCompute.PlanAmount, _ = planamount.Round(2).Float64() | ||
143 | + good.GoodCompute.PlanPartnerBonus, _ = planPartnerBonus.Round(2).Float64() | ||
144 | + if good.UseGoodNumber < 0 { | ||
145 | + //没有出现数量调整 | ||
146 | + good.GoodCompute.UsePartnerBonus = -1 | ||
147 | + good.GoodCompute.UseAmount = -1 | ||
148 | + } else { | ||
149 | + useamount := price.Mul(decimal.NewFromInt(int64(good.UseGoodNumber))) //price*useGoodNumber/price*useGoodNumber | ||
150 | + usePartnerBonus := useamount.Mul(decimal.NewFromFloat(good.PartnerBonusPercent)) //price*useGoodNumber*PartnerBonusPercent | ||
151 | + good.GoodCompute.UsePartnerBonus, _ = usePartnerBonus.Round(2).Float64() | ||
152 | + good.GoodCompute.UseAmount, _ = useamount.Round(2).Float64() | ||
153 | + } | ||
154 | + | ||
155 | + return nil | ||
156 | +} | ||
157 | + | ||
158 | +type OrderGoodFindQuery struct { | ||
159 | + OrderId int64 | ||
160 | + Offset int | ||
161 | + Limit int | ||
162 | +} | ||
163 | + | ||
164 | +type OrderGoodRepository interface { | ||
165 | + Save(order []OrderGood) error | ||
166 | + Find(queryOptions OrderGoodFindQuery) ([]OrderGood, int, error) | ||
167 | + Remove(orderid int64, ids ...int64) error | ||
168 | +} |
pkg/infrastructure/pg/models/order_base.go
0 → 100644
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "context" | ||
5 | + "time" | ||
6 | + | ||
7 | + "github.com/go-pg/pg/v10" | ||
8 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" | ||
9 | +) | ||
10 | + | ||
11 | +//OrderBase 订单基础 | ||
12 | +type OrderBase struct { | ||
13 | + tableName struct{} `pg:"order_base"` | ||
14 | + //表id | ||
15 | + Id int64 `pg:",pk"` | ||
16 | + //订单类型 | ||
17 | + OrderType int | ||
18 | + //订单编号 | ||
19 | + OrderCode string | ||
20 | + //交货编号 | ||
21 | + DeliveryCode string | ||
22 | + //买家 | ||
23 | + Buyer *domain.Buyer | ||
24 | + //订单区域信息 | ||
25 | + RegionInfo *domain.RegionInfo | ||
26 | + //订单对应的合伙人 | ||
27 | + PartnerId int64 | ||
28 | + //业务员抽成比例 | ||
29 | + SalesmanBonusPercent float64 | ||
30 | + //业务员抽成 | ||
31 | + SalesmanBonus float64 | ||
32 | + //预计的订单内货品总数 | ||
33 | + PlanOrderCount int | ||
34 | + //预计的订单的总金额 | ||
35 | + PlanOrderAmount float64 | ||
36 | + //按需使用的订单内货品总数 (初始值=-1) | ||
37 | + //业务判定时0是有效值,负值无效 | ||
38 | + UseOrderCount int | ||
39 | + //按需使用的订单内货总金额 (初始值=-1) | ||
40 | + //业务判定时0是有效值,负值无效 | ||
41 | + UseOrderAmount float64 | ||
42 | + //订单的创建时间 | ||
43 | + CreateTime time.Time | ||
44 | + //发货时间 | ||
45 | + DeliveryTime time.Time | ||
46 | + //更新时间 | ||
47 | + UpdateTime time.Time | ||
48 | + //合伙人应收分红 | ||
49 | + PlanPartnerBonus float64 | ||
50 | + //调整后的合伙人应收分红 (初始值=-1); | ||
51 | + //业务判定时0是有效值,负值无效 | ||
52 | + UsePartnerBonus float64 | ||
53 | + //合伙人已收分红 | ||
54 | + PartnerBonusHas float64 | ||
55 | + //合伙人未收分红 | ||
56 | + PartnerBonusNot float64 | ||
57 | + //合伙人分红支出 | ||
58 | + PartnerBonusExpense float64 | ||
59 | +} | ||
60 | + | ||
61 | +var _ pg.BeforeUpdateHook = (*OrderBase)(nil) | ||
62 | + | ||
63 | +func (or *OrderBase) BeforeUpdate(ctx context.Context) (context.Context, error) { | ||
64 | + or.UpdateTime = time.Now() | ||
65 | + return ctx, nil | ||
66 | +} | ||
67 | + | ||
68 | +var _ pg.BeforeInsertHook = (*OrderBase)(nil) | ||
69 | + | ||
70 | +func (or *OrderBase) BeforeInsert(ctx context.Context) (context.Context, error) { | ||
71 | + or.CreateTime = time.Now() | ||
72 | + or.UpdateTime = time.Now() | ||
73 | + return ctx, nil | ||
74 | +} |
pkg/infrastructure/pg/models/order_good.go
0 → 100644
1 | +package models | ||
2 | + | ||
3 | +//OrderGood 订单中的货品 | ||
4 | +type OrderGood struct { | ||
5 | + tableName struct{} `pg:"order_good"` | ||
6 | + //货品id | ||
7 | + Id int64 | ||
8 | + //所属订单id | ||
9 | + OrderId int64 | ||
10 | + //货品名称 长度可能较长 | ||
11 | + GoodName string | ||
12 | + //预计的货品数量 | ||
13 | + PlanGoodNumber int | ||
14 | + //货品单价 | ||
15 | + Price float64 | ||
16 | + //预计的货品总额 | ||
17 | + PlanAmount float64 | ||
18 | + //调整后的货品数量(初始值=-1) | ||
19 | + //业务判定时0是有效值,负值无效 | ||
20 | + UseGoodNumber int | ||
21 | + //调整后的货品总额 | ||
22 | + UseAmount float64 | ||
23 | + //合伙人分红比例 | ||
24 | + PartnerBonusPercent float64 | ||
25 | + //预计的合伙人分红 | ||
26 | + PlanPartnerBonus float64 | ||
27 | + //合伙人应收分红调整 (初始值=-1) | ||
28 | + ////业务判定时0是有效值,负值无效 | ||
29 | + UsePartnerBonus float64 | ||
30 | + //合伙人已收分红 | ||
31 | + PartnerBonusHas float64 | ||
32 | + //合伙人未收分红 | ||
33 | + PartnerBonusNot float64 | ||
34 | + //合伙人分红支出 | ||
35 | + PartnerBonusExpense float64 | ||
36 | + //分红状态 | ||
37 | + BonusStatus int | ||
38 | + //备注信息 | ||
39 | + Remark string | ||
40 | +} |
1 | +package repository | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" | ||
7 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models" | ||
8 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction" | ||
9 | +) | ||
10 | + | ||
11 | +type OrderBaseRepository struct { | ||
12 | + transactionContext *transaction.TransactionContext | ||
13 | +} | ||
14 | + | ||
15 | +var ( | ||
16 | + _ domain.OrderBaseRepository = (*OrderBaseRepository)(nil) | ||
17 | +) | ||
18 | + | ||
19 | +func NewOrderBaseRepository(transactionContext *transaction.TransactionContext) (*OrderBaseRepository, error) { | ||
20 | + if transactionContext == nil { | ||
21 | + return nil, fmt.Errorf("transactionContext参数不能为nil") | ||
22 | + } | ||
23 | + return &OrderBaseRepository{transactionContext: transactionContext}, nil | ||
24 | +} | ||
25 | + | ||
26 | +func (reponsitory OrderBaseRepository) transformPgModelToDomainModel(orderModel *models.OrderBase) (order domain.OrderBase, err error) { | ||
27 | + order = domain.OrderBase{ | ||
28 | + Id: orderModel.Id, OrderType: orderModel.OrderType, OrderCode: orderModel.OrderCode, | ||
29 | + DeliveryCode: orderModel.DeliveryCode, Buyer: orderModel.Buyer, RegionInfo: orderModel.RegionInfo, | ||
30 | + PartnerId: orderModel.PartnerId, SalesmanBonusPercent: orderModel.SalesmanBonusPercent, | ||
31 | + CreateTime: orderModel.CreateTime, DeliveryTime: orderModel.DeliveryTime, UpdateTime: orderModel.UpdateTime, | ||
32 | + OrderCompute: domain.OrderCompute{ | ||
33 | + PlanPartnerBonus: orderModel.PlanPartnerBonus, UsePartnerBonus: orderModel.UsePartnerBonus, | ||
34 | + PartnerBonusHas: orderModel.PartnerBonusHas, PartnerBonusNot: orderModel.PartnerBonusNot, | ||
35 | + PartnerBonusExpense: orderModel.PartnerBonusExpense, SalesmanBonus: orderModel.SalesmanBonus, | ||
36 | + PlanOrderCount: orderModel.PlanOrderCount, PlanOrderAmount: orderModel.PlanOrderAmount, | ||
37 | + UseOrderCount: orderModel.UseOrderCount, UseOrderAmount: orderModel.UseOrderAmount, | ||
38 | + }, | ||
39 | + } | ||
40 | + return order, nil | ||
41 | +} | ||
42 | + | ||
43 | +func (repository OrderBaseRepository) Save(orderInfo *domain.OrderBase) error { | ||
44 | + var ( | ||
45 | + err error | ||
46 | + tx = repository.transactionContext.PgTx | ||
47 | + ) | ||
48 | + m := &models.OrderBase{ | ||
49 | + Id: orderInfo.Id, OrderType: orderInfo.OrderType, OrderCode: orderInfo.OrderCode, | ||
50 | + DeliveryCode: orderInfo.DeliveryCode, Buyer: orderInfo.Buyer, RegionInfo: orderInfo.RegionInfo, | ||
51 | + PartnerId: orderInfo.PartnerId, SalesmanBonusPercent: orderInfo.SalesmanBonusPercent, | ||
52 | + SalesmanBonus: orderInfo.OrderCompute.SalesmanBonus, PlanOrderCount: orderInfo.OrderCompute.PlanOrderCount, | ||
53 | + PlanOrderAmount: orderInfo.OrderCompute.PlanOrderAmount, UseOrderCount: orderInfo.OrderCompute.UseOrderCount, | ||
54 | + UseOrderAmount: orderInfo.OrderCompute.UseOrderAmount, DeliveryTime: orderInfo.DeliveryTime, | ||
55 | + PlanPartnerBonus: orderInfo.OrderCompute.PlanPartnerBonus, UsePartnerBonus: orderInfo.OrderCompute.UsePartnerBonus, | ||
56 | + PartnerBonusHas: orderInfo.OrderCompute.PartnerBonusHas, PartnerBonusNot: orderInfo.OrderCompute.PartnerBonusNot, | ||
57 | + PartnerBonusExpense: orderInfo.OrderCompute.PartnerBonusExpense, | ||
58 | + } | ||
59 | + if m.Id == 0 { | ||
60 | + _, err = tx.Model(m). | ||
61 | + Returning("*"). | ||
62 | + Insert() | ||
63 | + orderInfo.Id = m.Id | ||
64 | + orderInfo.CreateTime = m.CreateTime | ||
65 | + orderInfo.UpdateTime = m.UpdateTime | ||
66 | + } else { | ||
67 | + _, err = tx.Model(m). | ||
68 | + WherePK(). | ||
69 | + Returning("*"). | ||
70 | + Update() | ||
71 | + orderInfo.UpdateTime = m.UpdateTime | ||
72 | + } | ||
73 | + return err | ||
74 | +} | ||
75 | + | ||
76 | +func (repository OrderBaseRepository) Find(queryOption domain.OrderFindQuery) ([]domain.OrderBase, int, error) { | ||
77 | + db := repository.transactionContext.PgDd | ||
78 | + orderModels := []models.OrderBase{} | ||
79 | + query := db.Model(&orderModels) | ||
80 | + if queryOption.PartnerId > 0 { | ||
81 | + query = query.Where("partner_id=?", queryOption.PartnerId) | ||
82 | + } | ||
83 | + if len(queryOption.OrderCode) > 0 { | ||
84 | + query = query.Where("order_code like ?", "%"+queryOption.OrderCode+"%") | ||
85 | + } | ||
86 | + if len(queryOption.DeliveryCode) > 0 { | ||
87 | + query = query.Where("delivery_code like ?", "%"+queryOption.DeliveryCode+"%") | ||
88 | + } | ||
89 | + if queryOption.OrderType > 0 { | ||
90 | + query = query.Where("order_type=?", queryOption.OrderType) | ||
91 | + } | ||
92 | + if queryOption.Offset > -1 { | ||
93 | + query = query.Offset(queryOption.Offset) | ||
94 | + } | ||
95 | + if queryOption.Limit > 0 { | ||
96 | + query = query.Limit(queryOption.Limit) | ||
97 | + } else { | ||
98 | + query = query.Limit(20) | ||
99 | + } | ||
100 | + var ( | ||
101 | + err error | ||
102 | + ordersReturn = make([]domain.OrderBase, 0) | ||
103 | + ) | ||
104 | + query = query.Order("order_base.id DESC") | ||
105 | + count, err := query.SelectAndCount() | ||
106 | + if err != nil { | ||
107 | + return ordersReturn, 0, err | ||
108 | + } | ||
109 | + for i := range orderModels { | ||
110 | + domainOrder, err := repository.transformPgModelToDomainModel(&orderModels[i]) | ||
111 | + if err != nil { | ||
112 | + return ordersReturn, 0, err | ||
113 | + } | ||
114 | + ordersReturn = append(ordersReturn, domainOrder) | ||
115 | + } | ||
116 | + return ordersReturn, count, nil | ||
117 | +} | ||
118 | + | ||
119 | +func (repository OrderBaseRepository) FindOne(qureyOptions domain.OrderFindOneQuery) (*domain.OrderBase, error) { | ||
120 | + var ( | ||
121 | + err error | ||
122 | + tx = repository.transactionContext.PgDd | ||
123 | + ) | ||
124 | + m := new(models.OrderBase) | ||
125 | + err = tx.Model(m). | ||
126 | + Where("id=?", qureyOptions.OrderId). | ||
127 | + First() | ||
128 | + if err != nil { | ||
129 | + return nil, err | ||
130 | + } | ||
131 | + result, err := repository.transformPgModelToDomainModel(m) | ||
132 | + return &result, err | ||
133 | +} | ||
134 | + | ||
135 | +func (repository OrderBaseRepository) Remove(id int64) error { | ||
136 | + var ( | ||
137 | + err error | ||
138 | + tx = repository.transactionContext.PgTx | ||
139 | + ) | ||
140 | + m := &models.OrderBase{ | ||
141 | + Id: id, | ||
142 | + } | ||
143 | + _, err = tx.Model(m).WherePK().Delete() | ||
144 | + return err | ||
145 | +} |
1 | +package repository | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" | ||
7 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models" | ||
8 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction" | ||
9 | +) | ||
10 | + | ||
11 | +type OrderGoodRepository struct { | ||
12 | + transactionContext *transaction.TransactionContext | ||
13 | +} | ||
14 | + | ||
15 | +var ( | ||
16 | + _ domain.OrderGoodRepository = (*OrderGoodRepository)(nil) | ||
17 | +) | ||
18 | + | ||
19 | +func NewOrderGoodRepository(transactionContext *transaction.TransactionContext) (*OrderGoodRepository, error) { | ||
20 | + if transactionContext == nil { | ||
21 | + return nil, fmt.Errorf("transactionContext参数不能为nil") | ||
22 | + } | ||
23 | + return &OrderGoodRepository{transactionContext: transactionContext}, nil | ||
24 | +} | ||
25 | + | ||
26 | +func (reponsitory OrderGoodRepository) transformPgModelToDomainModel(orderModel *models.OrderGood) (order domain.OrderGood, err error) { | ||
27 | + order = domain.OrderGood{ | ||
28 | + Id: orderModel.Id, OrderId: orderModel.OrderId, GoodName: orderModel.GoodName, | ||
29 | + PlanGoodNumber: orderModel.PlanGoodNumber, UseGoodNumber: orderModel.UseGoodNumber, | ||
30 | + Price: orderModel.Price, PartnerBonusPercent: orderModel.PartnerBonusPercent, | ||
31 | + BonusStatus: orderModel.BonusStatus, Remark: orderModel.Remark, | ||
32 | + GoodCompute: domain.GoodCompute{ | ||
33 | + PlanAmount: orderModel.PlanAmount, | ||
34 | + UseAmount: orderModel.UseAmount, | ||
35 | + PlanPartnerBonus: orderModel.PlanPartnerBonus, | ||
36 | + UsePartnerBonus: orderModel.UsePartnerBonus, | ||
37 | + PartnerBonusHas: orderModel.PartnerBonusHas, | ||
38 | + PartnerBonusNot: orderModel.PartnerBonusNot, | ||
39 | + PartnerBonusExpense: orderModel.PartnerBonusExpense, | ||
40 | + }, | ||
41 | + } | ||
42 | + switch orderModel.BonusStatus { | ||
43 | + case domain.OrderGoodWaitPay: | ||
44 | + order.CurrentBonusStatus = domain.OrderGoodBonusWaitPay{} | ||
45 | + case domain.OrderGoodHasPay: | ||
46 | + order.CurrentBonusStatus = domain.OrderGoodBonusHasPay{} | ||
47 | + } | ||
48 | + return order, nil | ||
49 | +} | ||
50 | + | ||
51 | +func (repository OrderGoodRepository) Save(data []domain.OrderGood) error { | ||
52 | + var ( | ||
53 | + err error | ||
54 | + tx = repository.transactionContext.PgTx | ||
55 | + ) | ||
56 | + for k, v := range data { | ||
57 | + m := &models.OrderGood{ | ||
58 | + Id: v.Id, OrderId: v.OrderId, GoodName: v.GoodName, | ||
59 | + PlanGoodNumber: v.PlanGoodNumber, Price: v.Price, | ||
60 | + PlanAmount: v.GoodCompute.PlanAmount, UseGoodNumber: v.UseGoodNumber, | ||
61 | + UseAmount: v.GoodCompute.UseAmount, PartnerBonusPercent: v.PartnerBonusPercent, | ||
62 | + PlanPartnerBonus: v.GoodCompute.PlanPartnerBonus, UsePartnerBonus: v.GoodCompute.UsePartnerBonus, | ||
63 | + PartnerBonusHas: v.GoodCompute.PartnerBonusHas, PartnerBonusNot: v.GoodCompute.PartnerBonusNot, | ||
64 | + PartnerBonusExpense: v.GoodCompute.PartnerBonusExpense, BonusStatus: v.BonusStatus, | ||
65 | + Remark: v.Remark, | ||
66 | + } | ||
67 | + if v.Id == 0 { | ||
68 | + _, err = tx.Model(m). | ||
69 | + Returning("*"). | ||
70 | + Insert() | ||
71 | + if err != nil { | ||
72 | + return err | ||
73 | + } | ||
74 | + data[k].Id = m.Id | ||
75 | + } else { | ||
76 | + _, err = tx.Model(m). | ||
77 | + WherePK(). | ||
78 | + Update() | ||
79 | + if err != nil { | ||
80 | + return err | ||
81 | + } | ||
82 | + } | ||
83 | + } | ||
84 | + return nil | ||
85 | +} | ||
86 | + | ||
87 | +func (repository OrderGoodRepository) Find(queryOption domain.OrderGoodFindQuery) ([]domain.OrderGood, int, error) { | ||
88 | + db := repository.transactionContext.PgDd | ||
89 | + goodModes := []models.OrderGood{} | ||
90 | + query := db.Model(&goodModes) | ||
91 | + if queryOption.OrderId > 0 { | ||
92 | + query = query.Where("order_id=?", queryOption.OrderId) | ||
93 | + } | ||
94 | + if queryOption.Offset > -1 { | ||
95 | + query = query.Offset(queryOption.Offset) | ||
96 | + } | ||
97 | + if queryOption.Limit > 0 { | ||
98 | + query = query.Limit(queryOption.Limit) | ||
99 | + } else { | ||
100 | + query = query.Limit(50) | ||
101 | + } | ||
102 | + var ( | ||
103 | + err error | ||
104 | + orderGoods = make([]domain.OrderGood, 0) | ||
105 | + ) | ||
106 | + query = query.Order("order_good.id DESC") | ||
107 | + count, err := query.SelectAndCount() | ||
108 | + if err != nil { | ||
109 | + return orderGoods, 0, err | ||
110 | + } | ||
111 | + for i := range goodModes { | ||
112 | + domainOrderGood, err := repository.transformPgModelToDomainModel(&goodModes[i]) | ||
113 | + if err != nil { | ||
114 | + return orderGoods, 0, err | ||
115 | + } | ||
116 | + orderGoods = append(orderGoods, domainOrderGood) | ||
117 | + } | ||
118 | + return orderGoods, count, nil | ||
119 | +} | ||
120 | + | ||
121 | +func (repository OrderGoodRepository) Remove(orderId int64, goodids ...int64) error { | ||
122 | + var ( | ||
123 | + err error | ||
124 | + tx = repository.transactionContext.PgTx | ||
125 | + ) | ||
126 | + m := &models.OrderBase{} | ||
127 | + query := tx.Model(m).Where("order_id=?", orderId) | ||
128 | + if len(goodids) > 0 { | ||
129 | + query = query.WhereIn("id in(?)", goodids) | ||
130 | + } | ||
131 | + _, err = query.Delete() | ||
132 | + return err | ||
133 | +} |
-
请 注册 或 登录 后发表评论