作者 yangfu

Merge remote-tracking branch 'origin/test'

正在显示 38 个修改的文件 包含 1889 行增加2307 行删除

要显示太多修改。

为保证性能只显示 38 of 38+ 个文件。

@@ -22,6 +22,6 @@ @@ -22,6 +22,6 @@
22 *.sum 22 *.sum
23 23
24 24
25 -/vendor 25 +
26 /*.exe~ 26 /*.exe~
27 /logs 27 /logs
@@ -5,11 +5,12 @@ WORKDIR $APP_DIR/ @@ -5,11 +5,12 @@ WORKDIR $APP_DIR/
5 COPY ./pkg pkg 5 COPY ./pkg pkg
6 COPY ./conf conf 6 COPY ./conf conf
7 COPY ./go.mod go.mod 7 COPY ./go.mod go.mod
  8 +COPY ./vendor vendor
8 COPY ./main.go main.go 9 COPY ./main.go main.go
9 RUN ["ln","-sf","/usr/share/zoneinfo/Asia/Shanghai","/etc/localtime"] 10 RUN ["ln","-sf","/usr/share/zoneinfo/Asia/Shanghai","/etc/localtime"]
10 ENV GO111MODULE on 11 ENV GO111MODULE on
11 ENV GOPROXY https://goproxy.cn 12 ENV GOPROXY https://goproxy.cn
12 -RUN ["go","mod","tidy"]  
13 -RUN ["go","build"] 13 +# RUN ["go","mod","tidy"]
  14 +RUN ["go","build","-mod=vendor"]
14 EXPOSE 8082 15 EXPOSE 8082
15 ENTRYPOINT ["./partnermg"] 16 ENTRYPOINT ["./partnermg"]
@@ -21,18 +21,10 @@ func CreatePartnerInfoDao(options map[string]interface{}) (*dao.PartnerInfoDao, @@ -21,18 +21,10 @@ func CreatePartnerInfoDao(options map[string]interface{}) (*dao.PartnerInfoDao,
21 return dao.NewPartnerInfoDao(transactionContext) 21 return dao.NewPartnerInfoDao(transactionContext)
22 } 22 }
23 23
24 -func CreateOrderDao(options map[string]interface{}) (*dao.OrderDao, error) { 24 +func CreateOrderBaseDao(options map[string]interface{}) (*dao.OrderBaseDao, error) {
25 var transactionContext *transaction.TransactionContext 25 var transactionContext *transaction.TransactionContext
26 if value, ok := options["transactionContext"]; ok { 26 if value, ok := options["transactionContext"]; ok {
27 transactionContext = value.(*transaction.TransactionContext) 27 transactionContext = value.(*transaction.TransactionContext)
28 } 28 }
29 - return dao.NewOrderDao(transactionContext)  
30 -}  
31 -  
32 -func CreateOrderPaymentDao(options map[string]interface{}) (*dao.OrderPayment, error) {  
33 - var transactionContext *transaction.TransactionContext  
34 - if value, ok := options["transactionContext"]; ok {  
35 - transactionContext = value.(*transaction.TransactionContext)  
36 - }  
37 - return dao.NewOrderPayment(transactionContext) 29 + return dao.NewOrderBaseDao(transactionContext)
38 } 30 }
@@ -33,20 +33,20 @@ func CreateAdminPermissionRepository(options map[string]interface{}) (domain.Adm @@ -33,20 +33,20 @@ func CreateAdminPermissionRepository(options map[string]interface{}) (domain.Adm
33 return repository.NewAdminPermissionRepository(transactionContext) 33 return repository.NewAdminPermissionRepository(transactionContext)
34 } 34 }
35 35
36 -//CreateOrderPaymentRepository 分红单信息  
37 -func CreateOrderPaymentRepository(options map[string]interface{}) (domain.OrderPaymentRepository, error) { 36 +//CreateOrderBaseRepository 订单信息
  37 +func CreateOrderBaseRepository(options map[string]interface{}) (domain.OrderBaseRepository, error) {
38 var transactionContext *transaction.TransactionContext 38 var transactionContext *transaction.TransactionContext
39 if value, ok := options["transactionContext"]; ok { 39 if value, ok := options["transactionContext"]; ok {
40 transactionContext = value.(*transaction.TransactionContext) 40 transactionContext = value.(*transaction.TransactionContext)
41 } 41 }
42 - return repository.NewOrderPaymentRepository(transactionContext) 42 + return repository.NewOrderBaseRepository(transactionContext)
43 } 43 }
44 44
45 -//CreateOrderRepository 订单信息  
46 -func CreateOrderRepository(options map[string]interface{}) (domain.OrderRepository, error) { 45 +//CreateOrderGoodRepository 订单信息
  46 +func CreateOrderGoodRepository(options map[string]interface{}) (domain.OrderGoodRepository, error) {
47 var transactionContext *transaction.TransactionContext 47 var transactionContext *transaction.TransactionContext
48 if value, ok := options["transactionContext"]; ok { 48 if value, ok := options["transactionContext"]; ok {
49 transactionContext = value.(*transaction.TransactionContext) 49 transactionContext = value.(*transaction.TransactionContext)
50 } 50 }
51 - return repository.NewOrderRepository(transactionContext) 51 + return repository.NewOrderGoodRepository(transactionContext)
52 } 52 }
1 -package command  
2 -  
3 -import (  
4 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"  
5 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"  
6 -)  
7 -  
8 -type CreateOrderCommand struct {  
9 - //订单区域  
10 - OrderRegion string  
11 - //订单编号  
12 - OrderCode string `json:"orderCode"`  
13 - //订单名称  
14 - OrderName string `json:"oderName"`  
15 - //数量  
16 - OrderCount int `json:"orderCount"`  
17 - //实际订单数量  
18 - OrderActualCount int `json:"orderActualCount"`  
19 - //订单金额  
20 - OrderAmount float64 `json:"orderAmount"`  
21 - //订单实际金额  
22 - OrderActualAmount float64 `json:"orderActualAmount"`  
23 - //订单状态  
24 - OrderStatus int `json:"orderStatus"`  
25 - //买家  
26 - BuyerName string `json:"buyerName"`  
27 - //买家电话  
28 - BuyerPhone string `json:"buyerPhone"`  
29 - //地址  
30 - BuyerAddress string `json:"address"`  
31 - //合伙人数据  
32 - PartnerId int64 `json:"partId"`  
33 - //合伙人分红百分比  
34 - PartnerBonusPercent float64 `json:"partnerBonusPercent"`  
35 - //业务员分红百分比  
36 - SalesmanBonusPercent float64 `json:"salesmanBonusPercent"`  
37 - //订单类型  
38 - OrderType int `json:"orderType"`  
39 -}  
40 -  
41 -func (command CreateOrderCommand) ValidateCommand() error {  
42 - if len(command.BuyerName) == 0 || len(command.BuyerPhone) == 0 {  
43 - return lib.ThrowError(lib.ARG_ERROR, "买家信息必填")  
44 - }  
45 - if len(command.BuyerAddress) == 0 {  
46 - return lib.ThrowError(lib.ARG_ERROR, "买家地址必填")  
47 - }  
48 - if len(command.OrderCode) == 0 {  
49 - return lib.ThrowError(lib.ARG_ERROR, "订单编号必填")  
50 - }  
51 - if len(command.OrderRegion) == 0 {  
52 - return lib.ThrowError(lib.ARG_ERROR, "订单区域必填")  
53 - }  
54 - if !(command.OrderType == domain.OrderReal ||  
55 - command.OrderType == domain.OrderIntention) {  
56 - return lib.ThrowError(lib.ARG_ERROR, "订单类型错误")  
57 - }  
58 - if !(command.OrderStatus == domain.OrderStatusDeliverSome ||  
59 - command.OrderStatus == domain.OrderStatusDeliverAll ||  
60 - command.OrderStatus == domain.OrderStatusReserve) {  
61 - return lib.ThrowError(lib.ARG_ERROR, "订单状态设置错误")  
62 - }  
63 - return nil  
64 -}  
1 -package command  
2 -  
3 -import (  
4 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"  
5 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"  
6 -)  
7 -  
8 -type UpdateOrderCommand struct {  
9 - //id  
10 - Id int64 `json:"id"`  
11 - //订单编号  
12 - OrderCode string `json:"orderCode"`  
13 - //订单名称  
14 - OrderName string `json:"oderName"`  
15 - //数量  
16 - OrderCount int `json:"orderCount"`  
17 - //订单金额  
18 - OrderAmount float64 `json:"orderAmount"`  
19 - //买家  
20 - BuyerPhone string `json:"buyerPhone"`  
21 - //地址  
22 - BuyerAddress string `json:"address"`  
23 - //订单区域  
24 - OrderRegion string `json:"orderRegion"`  
25 - //合伙人分红百分比  
26 - PartnerBonusPercent float64 `json:"partnerBonusPercent"`  
27 - //业务员分红百分比  
28 - SalesmanBonusPercent float64 `json:"salesmanBonusPercent"`  
29 - //订单状态  
30 - OrderStatus int `json:"orderStatus"`  
31 - //订单类型  
32 - OrderType int `json:"orderType"`  
33 -}  
34 -  
35 -func (command UpdateOrderCommand) ValidateCommand() error {  
36 - if command.Id == 0 {  
37 - return lib.ThrowError(lib.ARG_ERROR, "订单id错误")  
38 - }  
39 - if len(command.BuyerPhone) == 0 {  
40 - return lib.ThrowError(lib.ARG_ERROR, "买家信息必填")  
41 - }  
42 - if len(command.BuyerAddress) == 0 {  
43 - return lib.ThrowError(lib.ARG_ERROR, "买家地址必填")  
44 - }  
45 - if len(command.OrderCode) == 0 {  
46 - return lib.ThrowError(lib.ARG_ERROR, "订单编号必填")  
47 - }  
48 - if len(command.OrderRegion) == 0 {  
49 - return lib.ThrowError(lib.ARG_ERROR, "订单区域必填")  
50 - }  
51 - if !(command.OrderType == domain.OrderReal ||  
52 - command.OrderType == domain.OrderIntention) {  
53 - return lib.ThrowError(lib.ARG_ERROR, "订单类型错误")  
54 - }  
55 - if !(command.OrderStatus == domain.OrderStatusDeliverSome ||  
56 - command.OrderStatus == domain.OrderStatusDeliverAll ||  
57 - command.OrderStatus == domain.OrderStatusReserve) {  
58 - return lib.ThrowError(lib.ARG_ERROR, "订单状态设置错误")  
59 - }  
60 - return nil  
61 -}  
1 -package command  
2 -  
3 -import (  
4 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"  
5 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"  
6 -)  
7 -  
8 -type UpdateOrderRealCommand struct {  
9 - //id  
10 - Id int64 `json:"id"`  
11 - //订单编号  
12 - OrderCode string `json:"orderCode"`  
13 - //订单名称  
14 - OrderName string `json:"oderName"`  
15 - //数量  
16 - OrderActualCount int `json:"orderActualCount"`  
17 - //订单金额  
18 - OrderActualAmount float64 `json:"orderActualAmount"`  
19 - //买家  
20 - BuyerPhone string `json:"buyerPhone"`  
21 - //地址  
22 - BuyerAddress string `json:"address"`  
23 - //订单区域  
24 - OrderRegion string `json:"orderRegion"`  
25 - //合伙人分红百分比  
26 - PartnerBonusPercent float64 `json:"partnerBonusPercent"`  
27 - //业务员分红百分比  
28 - SalesmanBonusPercent float64 `json:"salesmanBonusPercent"`  
29 - //状态  
30 - OrderStatus int `json:"orderStatus"`  
31 -  
32 - Reason string `json:"reason"`  
33 -}  
34 -  
35 -func (command UpdateOrderRealCommand) ValidateCommand() error {  
36 - if command.Id == 0 {  
37 - return lib.ThrowError(lib.ARG_ERROR, "订单id错误")  
38 - }  
39 - if len(command.BuyerPhone) == 0 {  
40 - return lib.ThrowError(lib.ARG_ERROR, "买家信息必填")  
41 - }  
42 - if len(command.BuyerAddress) == 0 {  
43 - return lib.ThrowError(lib.ARG_ERROR, "买家地址必填")  
44 - }  
45 - if len(command.OrderCode) == 0 {  
46 - return lib.ThrowError(lib.ARG_ERROR, "订单编号必填")  
47 - }  
48 - if len(command.OrderRegion) == 0 {  
49 - return lib.ThrowError(lib.ARG_ERROR, "订单区域必填")  
50 - }  
51 - if !(command.OrderStatus == domain.OrderStatusDeliverSome ||  
52 - command.OrderStatus == domain.OrderStatusDeliverAll) {  
53 - return lib.ThrowError(lib.ARG_ERROR, "订单状态设置错误")  
54 - }  
55 - return nil  
56 -}  
1 -package service  
2 -  
3 -import (  
4 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"  
5 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/command"  
6 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/query"  
7 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"  
8 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"  
9 -)  
10 -  
11 -//OrderService 管理员相关服务  
12 -type OrderService struct {  
13 -}  
14 -  
15 -func NewOrderService(option map[string]interface{}) *OrderService {  
16 - newAdminUserService := new(OrderService)  
17 - return newAdminUserService  
18 -}  
19 -  
20 -func (service OrderService) PageListOrder(listOrderQuery query.ListOrderQuery) ([]domain.Order, int, error) {  
21 - transactionContext, err := factory.CreateTransactionContext(nil)  
22 - if err != nil {  
23 - return nil, 0, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())  
24 - }  
25 - var (  
26 - orderRepository domain.OrderRepository  
27 - orders []domain.Order  
28 - cnt int  
29 - )  
30 - if value, err := factory.CreateOrderRepository(map[string]interface{}{  
31 - "transactionContext": transactionContext,  
32 - }); err != nil {  
33 - return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
34 - } else {  
35 - orderRepository = value  
36 - }  
37 - query := domain.OrderFindQuery{  
38 - PartnerId: listOrderQuery.PartnerId,  
39 - OrderCode: listOrderQuery.OrderCode,  
40 - Offset: listOrderQuery.Offset,  
41 - Limit: listOrderQuery.Limit,  
42 - OrderType: listOrderQuery.OrderType,  
43 - }  
44 - orders, err = orderRepository.Find(query)  
45 - if err != nil {  
46 - return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
47 - }  
48 -  
49 - cnt, err = orderRepository.CountAll(query)  
50 - if err != nil {  
51 - return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
52 - }  
53 - var PartnerInfoRepository domain.PartnerInfoRepository  
54 - if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{  
55 - "transactionContext": transactionContext,  
56 - }); err != nil {  
57 - return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
58 - }  
59 - for i := range orders {  
60 - var partnerData *domain.PartnerInfo  
61 - partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: orders[i].PartnerInfo.Id})  
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 -func (service OrderService) GetOrder(getOrderQuery query.GetOrderQuery) (*domain.Order, error) {  
71 - //实际业务  
72 - transactionContext, err := factory.CreateTransactionContext(nil)  
73 - if err != nil {  
74 - return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())  
75 - }  
76 - var (  
77 - orderRepository domain.OrderRepository  
78 - order *domain.Order  
79 - )  
80 - if err = transactionContext.StartTransaction(); err != nil {  
81 - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
82 - }  
83 - defer func() {  
84 - transactionContext.RollbackTransaction()  
85 - }()  
86 - if value, err := factory.CreateOrderRepository(map[string]interface{}{  
87 - "transactionContext": transactionContext,  
88 - }); err != nil {  
89 - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
90 - } else {  
91 - orderRepository = value  
92 - }  
93 - order, err = orderRepository.FindOne(domain.OrderFindOneQuery{  
94 - OrderId: getOrderQuery.OrderId,  
95 - })  
96 - if err != nil {  
97 - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
98 - }  
99 -  
100 - var PartnerInfoRepository domain.PartnerInfoRepository  
101 - if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{  
102 - "transactionContext": transactionContext,  
103 - }); err != nil {  
104 - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
105 - }  
106 - var partnerData *domain.PartnerInfo  
107 - partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: order.PartnerInfo.Id})  
108 - if err != nil {  
109 - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
110 - }  
111 - order.PartnerInfo = partnerData.Partner  
112 - err = transactionContext.CommitTransaction()  
113 - if err != nil {  
114 - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
115 - }  
116 - return order, nil  
117 -}  
118 -  
119 -//CreateOrder 创建意向单  
120 -func (service OrderService) CreateOrder(command command.CreateOrderCommand) error {  
121 - var (  
122 - transactionContext, _ = factory.CreateTransactionContext(nil)  
123 - err error  
124 - )  
125 - if err = command.ValidateCommand(); err != nil {  
126 - return lib.ThrowError(lib.ARG_ERROR, err.Error())  
127 - }  
128 - if err = transactionContext.StartTransaction(); err != nil {  
129 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
130 - }  
131 - defer func() {  
132 - transactionContext.RollbackTransaction()  
133 - }()  
134 -  
135 - orderDao, _ := factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})  
136 - ok, err := orderDao.OrderCodeIsExist(command.OrderCode, 0)  
137 - if err != nil {  
138 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
139 - }  
140 - if ok {  
141 - return lib.ThrowError(lib.BUSINESS_ERROR, "订单编号已存在")  
142 - }  
143 - var PartnerInfoRepository domain.PartnerInfoRepository  
144 - if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{  
145 - "transactionContext": transactionContext,  
146 - }); err != nil {  
147 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
148 - }  
149 - var partnerData *domain.PartnerInfo  
150 - partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: command.PartnerId})  
151 - if err != nil {  
152 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
153 - }  
154 - var orderRepository domain.OrderRepository  
155 - if orderRepository, err = factory.CreateOrderRepository(map[string]interface{}{  
156 - "transactionContext": transactionContext,  
157 - }); err != nil {  
158 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
159 - }  
160 - newOrder := domain.Order{  
161 - //订单类型  
162 - OrderType: command.OrderType,  
163 - //订单编号  
164 - OrderCode: command.OrderCode,  
165 - //订单名称  
166 - OrderName: command.OrderName,  
167 - //订单状态  
168 - OrderStatus: command.OrderStatus,  
169 - //数量  
170 - OrderCount: command.OrderCount,  
171 - //实际数量  
172 - OrderActualCount: command.OrderActualCount,  
173 - //订单金额  
174 - OrderAmount: command.OrderAmount,  
175 - //实际订单金额  
176 - OrderActualAmount: command.OrderActualAmount,  
177 - //订单已支付分红金额(货款)  
178 - OrderPaymentAmount: 0,  
179 - //订单区域信息  
180 - OrderRegionInfo: domain.RegionInfo{  
181 - RegionName: command.OrderRegion,  
182 - },  
183 - //买家  
184 - Buyer: domain.Buyer{  
185 - BuyerName: command.BuyerName,  
186 - ShippingAddress: command.BuyerAddress,  
187 - ContactInfo: command.BuyerPhone,  
188 - },  
189 - PartnerInfo: partnerData.Partner,  
190 - //合伙人分红百分比  
191 - PartnerBonusPercent: command.PartnerBonusPercent,  
192 - //业务员分红百分比  
193 - SalesmanBonusPercent: command.SalesmanBonusPercent,  
194 - }  
195 - err = orderRepository.Save(newOrder)  
196 - if err != nil {  
197 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
198 - }  
199 - err = transactionContext.CommitTransaction()  
200 - if err != nil {  
201 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
202 - }  
203 - return nil  
204 -}  
205 -  
206 -//UpdateOrderPurpose 更新意向单  
207 -func (service OrderService) UpdateOrderPurpose(command command.UpdateOrderCommand) error {  
208 - var (  
209 - transactionContext, _ = factory.CreateTransactionContext(nil)  
210 - err error  
211 - )  
212 - if err = command.ValidateCommand(); err != nil {  
213 - return lib.ThrowError(lib.ARG_ERROR, err.Error())  
214 - }  
215 - if err = transactionContext.StartTransaction(); err != nil {  
216 - return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())  
217 - }  
218 - defer func() {  
219 - transactionContext.RollbackTransaction()  
220 - }()  
221 - orderDao, _ := factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})  
222 - ok, err := orderDao.OrderCodeIsExist(command.OrderCode, command.Id)  
223 - if err != nil {  
224 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
225 - }  
226 - if ok {  
227 - return lib.ThrowError(lib.BUSINESS_ERROR, "订单编号已存在")  
228 - }  
229 - var (  
230 - orderRepository domain.OrderRepository  
231 - orderData *domain.Order  
232 - )  
233 - if orderRepository, err = factory.CreateOrderRepository(map[string]interface{}{  
234 - "transactionContext": transactionContext,  
235 - }); err != nil {  
236 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
237 - }  
238 - orderData, err = orderRepository.FindOne(domain.OrderFindOneQuery{  
239 - OrderId: command.Id,  
240 - })  
241 - if err != nil {  
242 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
243 - }  
244 - if orderData.OrderType != domain.OrderIntention {  
245 - return lib.ThrowError(lib.BUSINESS_ERROR, "订单不是意向单")  
246 - }  
247 - updateMap := map[string]interface{}{  
248 - //订单编号  
249 - "orderCode": command.OrderCode,  
250 - "oderName": command.OrderName,  
251 - "orderCount": command.OrderCount,  
252 - "orderAmount": command.OrderAmount,  
253 - "orderActualCount": command.OrderCount,  
254 - "orderActualAmount": command.OrderAmount,  
255 - "buyer": domain.Buyer{  
256 - BuyerName: orderData.Buyer.BuyerName,  
257 - ContactInfo: command.BuyerPhone,  
258 - ShippingAddress: command.BuyerAddress,  
259 - },  
260 - "orderRegion": domain.RegionInfo{  
261 - RegionName: command.OrderRegion,  
262 - },  
263 - "partnerBonusPercent": command.PartnerBonusPercent,  
264 - "salesmanBonusPercent": command.SalesmanBonusPercent,  
265 - "orderStatus": command.OrderStatus,  
266 - "orderType": command.OrderType,  
267 - }  
268 -  
269 - err = orderData.Update(updateMap)  
270 - if err != nil {  
271 - return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())  
272 - }  
273 - err = orderRepository.Save(*orderData)  
274 - if err != nil {  
275 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
276 - }  
277 - err = transactionContext.CommitTransaction()  
278 - if err != nil {  
279 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
280 - }  
281 - return nil  
282 -}  
283 -  
284 -//RemoveOrder 删除意向单  
285 -func (service OrderService) RemoveOrder(id int64) error {  
286 - //实际业务  
287 - transactionContext, err := factory.CreateTransactionContext(nil)  
288 - if err != nil {  
289 - return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())  
290 - }  
291 - if err = transactionContext.StartTransaction(); err != nil {  
292 - return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())  
293 - }  
294 - defer func() {  
295 - transactionContext.RollbackTransaction()  
296 - }()  
297 - var (  
298 - orderRepository domain.OrderRepository  
299 - order *domain.Order  
300 - )  
301 - if value, err := factory.CreateOrderRepository(map[string]interface{}{  
302 - "transactionContext": transactionContext,  
303 - }); err != nil {  
304 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
305 - } else {  
306 - orderRepository = value  
307 - }  
308 - order, err = orderRepository.FindOne(domain.OrderFindOneQuery{  
309 - OrderId: id,  
310 - })  
311 - if err != nil {  
312 - return lib.ThrowError(lib.RES_NO_FIND_ERROR, err.Error())  
313 - }  
314 - if order.OrderType != domain.OrderIntention {  
315 - return lib.ThrowError(lib.BUSINESS_ERROR, "订单不是意向单")  
316 - }  
317 - err = orderRepository.Remove(order.Id)  
318 - if err != nil {  
319 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
320 - }  
321 - err = transactionContext.CommitTransaction()  
322 - if err != nil {  
323 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
324 - }  
325 - return nil  
326 -}  
327 -  
328 -//UpdateOrderReal 更新为实发单  
329 -func (service OrderService) UpdateOrderReal(command command.UpdateOrderRealCommand) error {  
330 - var (  
331 - transactionContext, _ = factory.CreateTransactionContext(nil)  
332 - err error  
333 - )  
334 - if err = command.ValidateCommand(); err != nil {  
335 - return lib.ThrowError(lib.ARG_ERROR, err.Error())  
336 - }  
337 - if err = transactionContext.StartTransaction(); err != nil {  
338 - return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())  
339 - }  
340 - defer func() {  
341 - transactionContext.RollbackTransaction()  
342 - }()  
343 - orderDao, _ := factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})  
344 - ok, err := orderDao.OrderCodeIsExist(command.OrderCode, command.Id)  
345 - if err != nil {  
346 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
347 - }  
348 - if ok {  
349 - return lib.ThrowError(lib.BUSINESS_ERROR, "订单编号已存在")  
350 - }  
351 - var (  
352 - orderRepository domain.OrderRepository  
353 - orderData *domain.Order  
354 - )  
355 - if orderRepository, err = factory.CreateOrderRepository(map[string]interface{}{  
356 - "transactionContext": transactionContext,  
357 - }); err != nil {  
358 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
359 - }  
360 - orderData, err = orderRepository.FindOne(domain.OrderFindOneQuery{  
361 - OrderId: command.Id,  
362 - })  
363 - if err != nil {  
364 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
365 - }  
366 - err = orderData.Update(map[string]interface{}{  
367 - //订单编号  
368 - "orderCode": command.OrderCode,  
369 - "oderName": command.OrderName,  
370 - "orderActualCount": command.OrderActualCount,  
371 - "orderActualAmount": command.OrderActualAmount,  
372 - "buyer": domain.Buyer{  
373 - BuyerName: orderData.Buyer.BuyerName,  
374 - ContactInfo: command.BuyerPhone,  
375 - ShippingAddress: command.BuyerAddress,  
376 - },  
377 - "orderRegion": domain.RegionInfo{  
378 - RegionName: command.OrderRegion,  
379 - },  
380 - "partnerBonusPercent": command.PartnerBonusPercent,  
381 - "salesmanBonusPercent": command.SalesmanBonusPercent,  
382 - "orderStatus": command.OrderStatus,  
383 - "orderType": domain.OrderReal,  
384 - "reason": command.Reason,  
385 - })  
386 - if err != nil {  
387 - return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())  
388 - }  
389 - err = orderRepository.Save(*orderData)  
390 - if err != nil {  
391 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
392 - }  
393 - err = transactionContext.CommitTransaction()  
394 - if err != nil {  
395 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
396 - }  
397 - return nil  
398 -}  
1 -package command  
2 -  
3 -type CreateOrderPaymentCommand struct {  
4 - //订单编号  
5 - OrderId int64 `json:"orderId"`  
6 - DivdendPaymentItem []DivdendPyamentItem `json:"dividendPayment"`  
7 - TotalPaymentAmount float64 `json:"payment_amount"`  
8 -}  
9 -  
10 -type DivdendPyamentItem struct {  
11 - // 货款  
12 - PaymentForGoods float64 `json:"paymentForGoods,omitempty"`  
13 - // 支付状态  
14 - StateOfPayment int `json:"stateOfPayment,omitempty"`  
15 - //支付批次  
16 - PaymentSn int `json:"paymentSn,omitempty"`  
17 - //支付编号  
18 - PaymentId int `json:"id,omitempty"`  
19 -}  
20 -  
21 -func (command CreateOrderPaymentCommand) ValidateCommand() error {  
22 - return nil  
23 -}  
1 -package query  
2 -  
3 -type ListDividendOrdersQuery struct {  
4 - // 合伙人类别  
5 - PartnerId int `json:"partner"`  
6 - SearchText string `json:"searchText"`  
7 - PageSize int `json:"pageSize"`  
8 - PageNumber int `json:"pageNumber"`  
9 -}  
10 -  
11 -func (q *ListDividendOrdersQuery) ValidateQuery() error {  
12 -  
13 - return nil  
14 -}  
1 -package query  
2 -  
3 -type GetOrderPaymentQuery struct {  
4 - // 合伙人类别  
5 - OrderId int64 `json:"orderId"`  
6 -}  
7 -  
8 -func (q *GetOrderPaymentQuery) ValidateQuery() error {  
9 -  
10 - return nil  
11 -}  
1 -package service  
2 -  
3 -import (  
4 - "fmt"  
5 - "github.com/linmadan/egglib-go/core/application"  
6 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"  
7 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/command"  
8 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/query"  
9 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"  
10 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/utils"  
11 - "time"  
12 -  
13 - //"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/query"  
14 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"  
15 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"  
16 -)  
17 -  
18 -// 客户价值服务  
19 -type OrderPaymentService struct {  
20 -}  
21 -  
22 -func NewOrderPaymentService(options map[string]interface{}) *OrderPaymentService {  
23 - newOrderPaymentService := &OrderPaymentService{}  
24 - return newOrderPaymentService  
25 -}  
26 -  
27 -// 创建订单支付数据  
28 -func (OrderPaymentService *OrderPaymentService) CreateOrderPayment(command *command.CreateOrderPaymentCommand) (data interface{}, err error) {  
29 - var (  
30 - transactionContext, _ = factory.CreateTransactionContext(nil)  
31 - OrderDao, _ = factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})  
32 - OrderPaymentDao, _ = factory.CreateOrderPaymentDao(map[string]interface{}{"transactionContext": transactionContext})  
33 - )  
34 - if err = command.ValidateCommand(); err != nil {  
35 - return nil, application.ThrowError(application.ARG_ERROR, err.Error())  
36 - }  
37 - if err = transactionContext.StartTransaction(); err != nil {  
38 - return nil, err  
39 - }  
40 - defer func() {  
41 - if err == nil {  
42 - err = transactionContext.CommitTransaction()  
43 - }  
44 - if err != nil {  
45 - transactionContext.RollbackTransaction()  
46 - }  
47 - }()  
48 - //检查订单是否存在  
49 -  
50 - var OrderPaymentRepository domain.OrderPaymentRepository  
51 - if OrderPaymentRepository, err = factory.CreateOrderPaymentRepository(map[string]interface{}{  
52 - "transactionContext": transactionContext,  
53 - }); err != nil {  
54 - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
55 - }  
56 - orderBase, e := OrderDao.GetOrderBaseInfo(command.OrderId)  
57 - if e != nil {  
58 - err = e  
59 - return  
60 - }  
61 - var excludeIdList []int  
62 - var bonusStatus int = domain.BonusPaid //分红状态  
63 - excludeIdList = append(excludeIdList, 0)  
64 - for i := range command.DivdendPaymentItem {  
65 - paymentItem := command.DivdendPaymentItem[i]  
66 - dm := &domain.OrderPayment{  
67 - OrderId: command.OrderId,  
68 - CreateAt: time.Now(),  
69 - UpdateAt: time.Now(),  
70 - }  
71 - if bonusStatus == domain.BonusPaid && paymentItem.StateOfPayment == domain.BonusWaitPay {  
72 - bonusStatus = domain.BonusWaitPay  
73 - }  
74 - if paymentItem.PaymentId > 0 {  
75 - //检查货款 已存在 / 未存在  
76 - if findDm, e := OrderPaymentRepository.FindOne(domain.OrderPaymentFindOneQuery{OrderId: command.OrderId, PaymentId: paymentItem.PaymentId}); e == nil {  
77 - //状态更金额一样的时候 不做更新  
78 - if findDm.BonusStatus == paymentItem.StateOfPayment && findDm.PaymentAmount == paymentItem.PaymentForGoods {  
79 - excludeIdList = append(excludeIdList, paymentItem.PaymentId)  
80 - continue  
81 - }  
82 - dm = findDm  
83 - }  
84 - }  
85 - dm.PartnerId = orderBase["PartnerId"].(int64)  
86 - bonusPercent := orderBase["PartnerBonusPercent"].(float64)  
87 - dm.BonusAmount = utils.Decimal(paymentItem.PaymentForGoods * (bonusPercent / 100.0))  
88 - dm.PaymentAmount = paymentItem.PaymentForGoods  
89 - dm.BonusStatus = paymentItem.StateOfPayment  
90 - dm.UpdateAt = time.Now()  
91 - if data, err = OrderPaymentRepository.Save(dm); err != nil {  
92 - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
93 - }  
94 - excludeIdList = append(excludeIdList, int(dm.Id))  
95 - }  
96 -  
97 - if err = OrderPaymentDao.Remove(command.OrderId, domain.BonusWaitPay, excludeIdList); err != nil {  
98 - return  
99 - }  
100 - if err = OrderDao.Update(map[string]interface{}{"id": command.OrderId, "orderPaymentAmount": command.TotalPaymentAmount, "bonusStatus": bonusStatus}); err != nil {  
101 - return  
102 - }  
103 -  
104 - return  
105 -}  
106 -  
107 -// 返回订单支付列表  
108 -func (OrderPaymentService *OrderPaymentService) ListOrderPayment(listOrderPaymentQuery *query.GetOrderPaymentQuery) ([]*domain.OrderPayment, error) {  
109 - var (  
110 - transactionContext, _ = factory.CreateTransactionContext(nil)  
111 - OrderDao, _ = factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})  
112 - OrderPayments []*domain.OrderPayment  
113 - err error  
114 - )  
115 - if err = listOrderPaymentQuery.ValidateQuery(); err != nil {  
116 - return nil, lib.ThrowError(lib.ARG_ERROR, err.Error())  
117 - }  
118 - if err := transactionContext.StartTransaction(); err != nil {  
119 - return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())  
120 - }  
121 - defer func() {  
122 - transactionContext.RollbackTransaction()  
123 - }()  
124 - var OrderPaymentRepository domain.OrderPaymentRepository  
125 - if OrderPaymentRepository, err = factory.CreateOrderPaymentRepository(map[string]interface{}{  
126 - "transactionContext": transactionContext,  
127 - }); err != nil {  
128 - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
129 - }  
130 - queryOption := domain.OrderPaymentQuery{  
131 - OrderId: listOrderPaymentQuery.OrderId,  
132 - }  
133 - _, e := OrderDao.GetOrderBaseInfo(listOrderPaymentQuery.OrderId)  
134 - if e != nil {  
135 - return nil, e  
136 - }  
137 - if OrderPayments, err = OrderPaymentRepository.Find(queryOption); err != nil {  
138 - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
139 - }  
140 - if err = transactionContext.CommitTransaction(); err != nil {  
141 - return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())  
142 - }  
143 - return OrderPayments, nil  
144 -}  
145 -  
146 -// 返回分红管理列表  
147 -func (OrderPaymentService *OrderPaymentService) ListDividendOrders(listOrderPaymentQuery *query.ListDividendOrdersQuery) (int, interface{}, error) {  
148 - var (  
149 - transactionContext, _ = factory.CreateTransactionContext(nil)  
150 - //OrderPayments []*domain.OrderPayment  
151 - count int  
152 - err error  
153 - OrderDao, _ = factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})  
154 - orders []*models.Order  
155 - )  
156 - if err = listOrderPaymentQuery.ValidateQuery(); err != nil {  
157 - return 0, nil, lib.ThrowError(lib.ARG_ERROR, err.Error())  
158 - }  
159 - if err := transactionContext.StartTransaction(); err != nil {  
160 - return 0, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())  
161 - }  
162 - defer func() {  
163 - transactionContext.RollbackTransaction()  
164 - }()  
165 - if count, orders, err = OrderDao.GetDividendOrders(map[string]interface{}{  
166 - "orderCode": listOrderPaymentQuery.SearchText,  
167 - "partnerId": listOrderPaymentQuery.PartnerId,  
168 - "orderType": 1,  
169 - "offset": (listOrderPaymentQuery.PageNumber - 1) * listOrderPaymentQuery.PageSize,  
170 - "limit": listOrderPaymentQuery.PageSize,  
171 - }); err != nil {  
172 - return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
173 - }  
174 -  
175 - type DividendOrderItem struct {  
176 - OrderId string `json:"id"` //订单编号  
177 - OrderNumber string `json:"orderNumber"` //订单号  
178 - OrderState int `json:"orderState"` //订单状态  
179 - StateOfPayment int `json:"stateOfPayment"` //支付状态  
180 - CreateTime string `json:"createTime"` //订单创建时间  
181 - PartnerName string `json:"partnerName"` //合伙人姓名  
182 - DividendProportion float64 `json:"dividendProportion"` //分红比例  
183 - DividendsReceivable float64 `json:"dividendsReceivable"` //应收分红  
184 - DividendSpending float64 `json:"dividendSpending"` //分红支出  
185 - ReceiveDividends float64 `json:"receiveDividends"` //实收分红  
186 - CommissionProportion float64 `json:"commissionProportion"` //业务员抽成比例  
187 - }  
188 - var list = make([]DividendOrderItem, 0)  
189 - for i := range orders {  
190 - order := orders[i]  
191 - item := DividendOrderItem{  
192 - OrderId: fmt.Sprintf("%v", order.Id),  
193 - OrderNumber: order.OrderCode,  
194 - OrderState: order.OrderStatus,  
195 - StateOfPayment: order.BonusStatus,  
196 - CreateTime: order.CreateAt.Local().Format("2006-01-02 15:04:05"),  
197 - PartnerName: order.PartnerInfo.PartnerName,  
198 - DividendProportion: order.PartnerBonusPercent,  
199 - DividendsReceivable: utils.Decimal(order.OrderAmount * (order.PartnerBonusPercent / 100.0)),  
200 - DividendSpending: 0,  
201 - ReceiveDividends: utils.Decimal(order.OrderActualAmount * (order.PartnerBonusPercent / 100.0)),  
202 - CommissionProportion: order.SalesmanBonusPercent,  
203 - }  
204 - if order.OrderActualAmount < order.OrderAmount {  
205 - item.DividendSpending = utils.Decimal((order.OrderAmount - order.OrderActualAmount) * (order.PartnerBonusPercent / 100.0))  
206 - }  
207 - list = append(list, item)  
208 - }  
209 -  
210 - if err = transactionContext.CommitTransaction(); err != nil {  
211 - return 0, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())  
212 - }  
213 - return count, list, nil  
214 -}  
  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 +//DisableOrderCommand 开启关闭订单
  4 +type DisableOrderCommand struct {
  5 + OrderId int64 `json:"orderId"`
  6 + IsDisable int `json:"isDisable"`
  7 + OrderType int `json:"orderType"`
  8 +}
  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 + Price float64 `json:"price"`
  12 + //合伙人分红比例
  13 + PartnerBonusPercent float64 `json:"partnerBonusPercent"`
  14 + //备注信息
  15 + Remark string `json:"remark"`
  16 +}
  1 +package command
  2 +
  3 +import "time"
  4 +
  5 +type OrderDeliveryCommand struct {
  6 + OrderId int64 `json:"orderId"`
  7 + DeliveryTime time.Time `json:"deliveryTime"`
  8 + DeliveryCode string `json:"deliveryCode"`
  9 + Goods []OrderGoodData `json:"goods"`
  10 +}
  1 +package command
  2 +
  3 +//更新订单的商品数字并更新分红的数据
  4 +type UpdateGoodBouns struct {
  5 + Id int64 `json:"id"` //订单id
  6 + GoodBouns []GoodBouns `json:"goodBouns"`
  7 +}
  8 +
  9 +//GoodBoun 商品数量调整
  10 +type GoodBouns struct {
  11 + GoodId int64 `json:"goodId"` //货品id
  12 + UseGoodNumber int `json:"useGoodNumber"` //货品数量调整的数值
  13 + Remark string `json:"remark"` //原因
  14 + BounsStatus int `json:"BounsStatus"`
  15 +}
  1 +package command
  2 +
  3 +//UpdateOrderPurposeCommand 更新意向单
  4 +type UpdateOrderCommand struct {
  5 + Id int64 `json:"id"`
  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 + OrderType int `json:"orderType"`
  20 + //货品
  21 + Goods []OrderGoodData `json:"goods"`
  22 +}
1 package query 1 package query
2 2
3 -//ListOrderQuery 获取用户列表 3 +//GetOrderQuery 获取订单详情
4 type GetOrderQuery struct { 4 type GetOrderQuery struct {
5 OrderId int64 `json:"orderId"` 5 OrderId int64 `json:"orderId"`
6 } 6 }
1 package query 1 package query
2 2
3 //ListOrderQuery 获取用户列表 3 //ListOrderQuery 获取用户列表
4 -type ListOrderQuery struct { 4 +type ListOrderBaseQuery struct {
5 //合伙人id 5 //合伙人id
6 PartnerId int64 `json:"partnerId" ` 6 PartnerId int64 `json:"partnerId" `
7 //订单编号 7 //订单编号
@@ -12,4 +12,6 @@ type ListOrderQuery struct { @@ -12,4 +12,6 @@ type ListOrderQuery struct {
12 Limit int `json:"limit"` 12 Limit int `json:"limit"`
13 //订单类型 13 //订单类型
14 OrderType int `json:"orderType"` 14 OrderType int `json:"orderType"`
  15 + //发货单号
  16 + DeliveryCode string `json:"deliveryCode"`
15 } 17 }
  1 +package service
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
  8 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/command"
  9 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/query"
  10 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
  11 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/dao"
  12 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
  13 +)
  14 +
  15 +//OrderService 管理员相关服务
  16 +type OrderInfoService struct {
  17 +}
  18 +
  19 +func NewOrderInfoService(option map[string]interface{}) *OrderInfoService {
  20 + newAdminUserService := new(OrderInfoService)
  21 + return newAdminUserService
  22 +}
  23 +
  24 +// PageListOrderBase 获取订单列表
  25 +func (service OrderInfoService) PageListOrderBase(listOrderQuery query.ListOrderBaseQuery) ([]domain.OrderBase, int, error) {
  26 + transactionContext, err := factory.CreateTransactionContext(nil)
  27 + if err != nil {
  28 + return nil, 0, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  29 + }
  30 + var (
  31 + orderRepository domain.OrderBaseRepository
  32 + orders []domain.OrderBase
  33 + cnt int
  34 + )
  35 + if value, err := factory.CreateOrderBaseRepository(map[string]interface{}{
  36 + "transactionContext": transactionContext,
  37 + }); err != nil {
  38 + return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  39 + } else {
  40 + orderRepository = value
  41 + }
  42 + query := domain.OrderBaseFindQuery{
  43 + PartnerId: listOrderQuery.PartnerId,
  44 + OrderCode: listOrderQuery.OrderCode,
  45 + Offset: listOrderQuery.Offset,
  46 + Limit: listOrderQuery.Limit,
  47 + OrderType: listOrderQuery.OrderType,
  48 + DeliveryCode: listOrderQuery.DeliveryCode,
  49 + }
  50 + orders, cnt, err = orderRepository.Find(query)
  51 + if err != nil {
  52 + return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  53 + }
  54 + var PartnerInfoRepository domain.PartnerInfoRepository
  55 + if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
  56 + "transactionContext": transactionContext,
  57 + }); err != nil {
  58 + return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  59 + }
  60 + for i := range orders {
  61 + var partnerData *domain.PartnerInfo
  62 + partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{
  63 + UserId: orders[i].PartnerId,
  64 + })
  65 + if err != nil {
  66 + return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  67 + }
  68 + orders[i].PartnerInfo = partnerData.Partner
  69 + }
  70 + return orders, cnt, nil
  71 +}
  72 +
  73 +//GetOrderDetail 获取订单详情
  74 +func (service OrderInfoService) GetOrderDetail(getOrderQuery query.GetOrderQuery) (*domain.OrderBase, error) {
  75 + //实际业务
  76 + transactionContext, err := factory.CreateTransactionContext(nil)
  77 + if err != nil {
  78 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  79 + }
  80 + var (
  81 + orderBaseRepository domain.OrderBaseRepository
  82 + PartnerInfoRepository domain.PartnerInfoRepository
  83 + orderGoodRepository domain.OrderGoodRepository
  84 + order *domain.OrderBase
  85 + )
  86 + if err = transactionContext.StartTransaction(); err != nil {
  87 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  88 + }
  89 + defer func() {
  90 + transactionContext.RollbackTransaction()
  91 + }()
  92 + if value, err := factory.CreateOrderBaseRepository(map[string]interface{}{
  93 + "transactionContext": transactionContext,
  94 + }); err != nil {
  95 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  96 + } else {
  97 + orderBaseRepository = value
  98 + }
  99 + if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
  100 + "transactionContext": transactionContext,
  101 + }); err != nil {
  102 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  103 + }
  104 + if orderGoodRepository, err = factory.CreateOrderGoodRepository(map[string]interface{}{
  105 + "transactionContext": transactionContext,
  106 + }); err != nil {
  107 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  108 + }
  109 + order, err = orderBaseRepository.FindOne(domain.OrderBaseFindOneQuery{
  110 + OrderId: getOrderQuery.OrderId,
  111 + })
  112 + if err != nil {
  113 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取订单数据失败:%s", err))
  114 + }
  115 + var (
  116 + partnerData *domain.PartnerInfo
  117 + goods []domain.OrderGood
  118 + )
  119 +
  120 + partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: order.PartnerId})
  121 + if err != nil {
  122 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("检索合伙人数据失败:%s", err))
  123 + }
  124 + order.PartnerInfo = partnerData.Partner
  125 + goods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{
  126 + OrderId: order.Id,
  127 + })
  128 + if err != nil {
  129 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取订单中的商品列表失败:%s", err))
  130 + }
  131 + order.Goods = goods
  132 + err = transactionContext.CommitTransaction()
  133 + if err != nil {
  134 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  135 + }
  136 + return order, nil
  137 +}
  138 +
  139 +func (service OrderInfoService) CreateNewOrder(cmd command.CreateOrderCommand) (*domain.OrderBase, error) {
  140 + var (
  141 + transactionContext, _ = factory.CreateTransactionContext(nil)
  142 + err error
  143 + )
  144 +
  145 + if err = transactionContext.StartTransaction(); err != nil {
  146 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  147 + }
  148 + defer func() {
  149 + transactionContext.RollbackTransaction()
  150 + }()
  151 +
  152 + var PartnerInfoRepository domain.PartnerInfoRepository
  153 + if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
  154 + "transactionContext": transactionContext,
  155 + }); err != nil {
  156 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  157 + }
  158 + var partnerData *domain.PartnerInfo
  159 + partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: cmd.PartnerId})
  160 + if err != nil {
  161 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("检索合伙人数据失败"))
  162 + }
  163 + var (
  164 + orderBaseRepository domain.OrderBaseRepository
  165 + orderGoodRepository domain.OrderGoodRepository
  166 + orderBaseDao *dao.OrderBaseDao
  167 + )
  168 + if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{
  169 + "transactionContext": transactionContext,
  170 + }); err != nil {
  171 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  172 + }
  173 + if orderGoodRepository, err = factory.CreateOrderGoodRepository(map[string]interface{}{
  174 + "transactionContext": transactionContext,
  175 + }); err != nil {
  176 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  177 + }
  178 + if orderBaseDao, err = factory.CreateOrderBaseDao(map[string]interface{}{
  179 + "transactionContext": transactionContext,
  180 + }); err != nil {
  181 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  182 + }
  183 + //检查order_code是否重复
  184 + if ok, err := orderBaseDao.OrderCodeExist(cmd.OrderCode); err != nil {
  185 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  186 + } else if ok {
  187 + return nil, lib.ThrowError(lib.BUSINESS_ERROR, "订单号已存在")
  188 + }
  189 + //检查delivery_code是否重复
  190 + if len(cmd.DeliveryCode) > 0 {
  191 + if ok, err := orderBaseDao.DeliveryCodeExist(cmd.DeliveryCode); err != nil {
  192 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  193 + } else if ok {
  194 + return nil, lib.ThrowError(lib.BUSINESS_ERROR, "发货号已存在")
  195 + }
  196 + }
  197 + newOrder := &domain.OrderBase{
  198 + OrderType: cmd.OrderType, OrderCode: cmd.OrderCode,
  199 + DeliveryCode: cmd.DeliveryCode,
  200 + Buyer: &domain.Buyer{
  201 + BuyerName: cmd.BuyerName,
  202 + },
  203 + RegionInfo: &domain.RegionInfo{
  204 + RegionName: cmd.OrderRegion,
  205 + },
  206 + PartnerId: cmd.PartnerId,
  207 + PartnerInfo: partnerData.Partner,
  208 + SalesmanBonusPercent: cmd.SalesmanBonusPercent,
  209 + }
  210 + var orderGoods []domain.OrderGood
  211 + for _, good := range cmd.Goods {
  212 + m := domain.NewOrderGood()
  213 + m.OrderId = 0
  214 + m.GoodName = good.GoodName
  215 + m.PlanGoodNumber = good.PlanGoodNumber
  216 + m.Price = good.Price
  217 + m.PartnerBonusPercent = good.PartnerBonusPercent
  218 + m.Remark = good.Remark
  219 + err = m.Compute()
  220 + if err != nil {
  221 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中商品的数值失败:%s", err))
  222 + }
  223 + err = m.CurrentBonusStatus.WartPayPartnerBonus(&m)
  224 + if err != nil {
  225 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中商品的分红数值失败:%s", err))
  226 + }
  227 + orderGoods = append(orderGoods, m)
  228 + }
  229 + newOrder.Goods = orderGoods
  230 + err = newOrder.Compute()
  231 + if err != nil {
  232 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中合计的数值失败:%s", err))
  233 + }
  234 + err = orderBaseRepository.Save(newOrder)
  235 + if err != nil {
  236 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单数据失败:%s", err))
  237 + }
  238 + for i := range newOrder.Goods {
  239 + newOrder.Goods[i].OrderId = newOrder.Id
  240 + }
  241 + err = orderGoodRepository.Save(orderGoods)
  242 + if err != nil {
  243 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单中的商品数据失败:%s", err))
  244 + }
  245 + newOrder.Goods = orderGoods
  246 + err = transactionContext.CommitTransaction()
  247 + if err != nil {
  248 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  249 + }
  250 + return newOrder, nil
  251 +}
  252 +
  253 +//DeleteOrder 删除订单
  254 +func (service OrderInfoService) DeleteOrder(orderId int64) error {
  255 + var (
  256 + transactionContext, _ = factory.CreateTransactionContext(nil)
  257 + err error
  258 + )
  259 + if err = transactionContext.StartTransaction(); err != nil {
  260 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  261 + }
  262 + defer func() {
  263 + transactionContext.RollbackTransaction()
  264 + }()
  265 + var (
  266 + orderBaseRepository domain.OrderBaseRepository
  267 + orderGoodRepository domain.OrderGoodRepository
  268 + )
  269 + if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{
  270 + "transactionContext": transactionContext,
  271 + }); err != nil {
  272 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  273 + }
  274 + if orderGoodRepository, err = factory.CreateOrderGoodRepository(map[string]interface{}{
  275 + "transactionContext": transactionContext,
  276 + }); err != nil {
  277 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  278 + }
  279 + err = orderBaseRepository.Remove(orderId)
  280 + if err != nil {
  281 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("删除订单数据失败:%s", err))
  282 + }
  283 + err = orderGoodRepository.Remove(orderId)
  284 + if err != nil {
  285 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("删除订单中商品数据失败:%s", err))
  286 + }
  287 + err = transactionContext.CommitTransaction()
  288 + if err != nil {
  289 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  290 + }
  291 + return nil
  292 +}
  293 +
  294 +//UpdateOrderData 编辑订单
  295 +func (service OrderInfoService) UpdateOrderData(cmd command.UpdateOrderCommand) (*domain.OrderBase, error) {
  296 + var (
  297 + transactionContext, _ = factory.CreateTransactionContext(nil)
  298 + err error
  299 + )
  300 +
  301 + if err = transactionContext.StartTransaction(); err != nil {
  302 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  303 + }
  304 + defer func() {
  305 + transactionContext.RollbackTransaction()
  306 + }()
  307 +
  308 + var PartnerInfoRepository domain.PartnerInfoRepository
  309 + if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
  310 + "transactionContext": transactionContext,
  311 + }); err != nil {
  312 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  313 + }
  314 + var partnerData *domain.PartnerInfo
  315 + partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: cmd.PartnerId})
  316 + if err != nil {
  317 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("检索合伙人数据失败"))
  318 + }
  319 + var (
  320 + orderBaseRepository domain.OrderBaseRepository
  321 + orderGoodRepository domain.OrderGoodRepository
  322 + oldOrderData *domain.OrderBase
  323 + oldOrderGoods []domain.OrderGood
  324 + newOrderGoods []domain.OrderGood
  325 + delGoods []int64
  326 + orderBaseDao *dao.OrderBaseDao
  327 + )
  328 + if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{
  329 + "transactionContext": transactionContext,
  330 + }); err != nil {
  331 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  332 + }
  333 + if orderGoodRepository, err = factory.CreateOrderGoodRepository(map[string]interface{}{
  334 + "transactionContext": transactionContext,
  335 + }); err != nil {
  336 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  337 + }
  338 + //获取旧的订单
  339 + oldOrderData, err = orderBaseRepository.FindOne(domain.OrderBaseFindOneQuery{
  340 + OrderId: cmd.Id,
  341 + })
  342 + if err != nil {
  343 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("未找到指定的订单:%s", err))
  344 + }
  345 + //判定要求的更新的订单类型
  346 + if oldOrderData.OrderType != cmd.OrderType {
  347 + return nil, lib.ThrowError(lib.BUSINESS_ERROR, fmt.Sprintf("操作失败,待更新的订单的类型已变更"))
  348 + }
  349 + if orderBaseDao, err = factory.CreateOrderBaseDao(map[string]interface{}{
  350 + "transactionContext": transactionContext,
  351 + }); err != nil {
  352 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  353 + }
  354 + //检查order_code是否重复
  355 + if ok, err := orderBaseDao.OrderCodeExist(cmd.OrderCode, cmd.Id); err != nil {
  356 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  357 + } else if ok {
  358 + return nil, lib.ThrowError(lib.BUSINESS_ERROR, "订单号已存在")
  359 + }
  360 + //检查delivery_code是否重复
  361 + if len(cmd.DeliveryCode) > 0 {
  362 + if ok, err := orderBaseDao.DeliveryCodeExist(cmd.DeliveryCode, cmd.Id); err != nil {
  363 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  364 + } else if ok {
  365 + return nil, lib.ThrowError(lib.BUSINESS_ERROR, "发货号已存在")
  366 + }
  367 + }
  368 + //获取旧的订单中的商品
  369 + oldOrderGoods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{
  370 + OrderId: cmd.Id,
  371 + })
  372 + if err != nil {
  373 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("未找到指定的订单中的商品列表失败:%s", err))
  374 + }
  375 + for _, good := range cmd.Goods {
  376 + m := domain.NewOrderGood()
  377 + m.OrderId = oldOrderData.Id
  378 + m.GoodName = good.GoodName
  379 + m.PlanGoodNumber = good.PlanGoodNumber
  380 + m.Price = good.Price
  381 + m.PartnerBonusPercent = good.PartnerBonusPercent
  382 + m.Remark = good.Remark
  383 + err = m.Compute()
  384 + if err != nil {
  385 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中商品的数值失败:%s", err))
  386 + }
  387 + err = m.CurrentBonusStatus.WartPayPartnerBonus(&m)
  388 + if err != nil {
  389 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中商品的分红数值失败:%s", err))
  390 + }
  391 + newOrderGoods = append(newOrderGoods, m)
  392 + }
  393 + oldOrderData.OrderCode = cmd.OrderCode
  394 + oldOrderData.DeliveryCode = cmd.DeliveryCode
  395 + oldOrderData.Buyer.BuyerName = cmd.BuyerName
  396 + oldOrderData.RegionInfo.RegionName = cmd.OrderRegion
  397 + oldOrderData.PartnerId = cmd.PartnerId
  398 + oldOrderData.PartnerInfo = partnerData.Partner
  399 + oldOrderData.SalesmanBonusPercent = cmd.SalesmanBonusPercent
  400 + oldOrderData.Goods = newOrderGoods
  401 + err = oldOrderData.Compute()
  402 + if err != nil {
  403 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中合计的数值失败:%s", err))
  404 + }
  405 +
  406 + err = orderBaseRepository.Save(oldOrderData)
  407 + if err != nil {
  408 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单数据失败:%s", err))
  409 + }
  410 + err = orderGoodRepository.Save(newOrderGoods)
  411 + if err != nil {
  412 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单中的商品数据失败:%s", err))
  413 + }
  414 + oldOrderData.Goods = newOrderGoods
  415 + //删不需要的订单总不需要的商品
  416 + delGoods = service.deleteOldOrderGoods(newOrderGoods, oldOrderGoods)
  417 + err = orderGoodRepository.Remove(oldOrderData.Id, delGoods...)
  418 + if err != nil {
  419 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("删除订单中的商品数据失败:%s", err))
  420 + }
  421 + err = transactionContext.CommitTransaction()
  422 + if err != nil {
  423 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  424 + }
  425 + return oldOrderData, nil
  426 +}
  427 +
  428 +//deleteOldOrderGoods 新旧商品列表对比
  429 +func (service OrderInfoService) deleteOldOrderGoods(newGoods []domain.OrderGood, oldGoods []domain.OrderGood) (goodIds []int64) {
  430 + for _, old := range oldGoods {
  431 + var hasIn bool
  432 + for _, new := range newGoods {
  433 + if old.Id == new.Id {
  434 + hasIn = true
  435 + break
  436 + }
  437 + }
  438 + if !hasIn {
  439 + goodIds = append(goodIds, old.Id)
  440 + }
  441 + }
  442 + return
  443 +}
  444 +
  445 +//Delivery 发货
  446 +func (service OrderInfoService) Delivery(cmd command.OrderDeliveryCommand) error {
  447 + var (
  448 + transactionContext, _ = factory.CreateTransactionContext(nil)
  449 + err error
  450 + )
  451 + if err = transactionContext.StartTransaction(); err != nil {
  452 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  453 + }
  454 + defer func() {
  455 + transactionContext.RollbackTransaction()
  456 + }()
  457 + var (
  458 + orderBaseRepository domain.OrderBaseRepository
  459 + orderGoodRepository domain.OrderGoodRepository
  460 + oldOrderData *domain.OrderBase
  461 + oldOrderGoods []domain.OrderGood
  462 + orderBaseDao *dao.OrderBaseDao
  463 + )
  464 +
  465 + if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{
  466 + "transactionContext": transactionContext,
  467 + }); err != nil {
  468 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  469 + }
  470 + if orderGoodRepository, err = factory.CreateOrderGoodRepository(map[string]interface{}{
  471 + "transactionContext": transactionContext,
  472 + }); err != nil {
  473 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  474 + }
  475 + //获取旧的订单
  476 + oldOrderData, err = orderBaseRepository.FindOne(domain.OrderBaseFindOneQuery{
  477 + OrderId: cmd.OrderId,
  478 + })
  479 + if err != nil {
  480 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("未找到指定的订单:%s", err))
  481 + }
  482 + if oldOrderData.OrderType != domain.OrderIntention {
  483 + return lib.ThrowError(lib.BUSINESS_ERROR, "订单类型已发生变更")
  484 + }
  485 + if orderBaseDao, err = factory.CreateOrderBaseDao(map[string]interface{}{
  486 + "transactionContext": transactionContext,
  487 + }); err != nil {
  488 + return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  489 + }
  490 + //检查delivery_code是否重复
  491 + if ok, err := orderBaseDao.DeliveryCodeExist(cmd.DeliveryCode, cmd.OrderId); err != nil {
  492 + return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  493 + } else if ok {
  494 + return lib.ThrowError(lib.BUSINESS_ERROR, "发货号已存在")
  495 + }
  496 + //获取旧的订单中的商品
  497 + oldOrderGoods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{
  498 + OrderId: cmd.OrderId,
  499 + })
  500 + if err != nil {
  501 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("未找到指定的订单中的商品列表失败:%s", err))
  502 + }
  503 + for _, newGood := range cmd.Goods {
  504 + for i := range oldOrderGoods {
  505 + if newGood.Id != oldOrderGoods[i].Id {
  506 + continue
  507 + }
  508 + oldOrderGoods[i].GoodName = newGood.GoodName
  509 + oldOrderGoods[i].PlanGoodNumber = newGood.PlanGoodNumber
  510 + oldOrderGoods[i].Price = newGood.Price
  511 + oldOrderGoods[i].PartnerBonusPercent = newGood.PartnerBonusPercent
  512 + oldOrderGoods[i].Remark = newGood.Remark
  513 + err = oldOrderGoods[i].Compute()
  514 + if err != nil {
  515 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中商品的数值失败:%s", err))
  516 + }
  517 + err = oldOrderGoods[i].CurrentBonusStatus.WartPayPartnerBonus(&oldOrderGoods[i])
  518 + if err != nil {
  519 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中商品的分红数值失败:%s", err))
  520 + }
  521 + }
  522 + }
  523 + oldOrderData.DeliveryCode = cmd.DeliveryCode
  524 + oldOrderData.DeliveryTime = time.Now()
  525 + oldOrderData.Goods = oldOrderGoods
  526 + //变更订单类型
  527 + oldOrderData.OrderType = domain.OrderReal
  528 + err = oldOrderData.Compute()
  529 + if err != nil {
  530 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中合计的数值失败:%s", err))
  531 + }
  532 + err = orderBaseRepository.Save(oldOrderData)
  533 + if err != nil {
  534 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单数据失败:%s", err))
  535 + }
  536 + err = orderGoodRepository.Save(oldOrderGoods)
  537 + if err != nil {
  538 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单中的商品数据失败:%s", err))
  539 + }
  540 + err = transactionContext.CommitTransaction()
  541 + if err != nil {
  542 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  543 + }
  544 + return nil
  545 +}
  546 +
  547 +//DisableOrEnable 开启关闭订单
  548 +func (service OrderInfoService) DisableOrEnable(cmd command.DisableOrderCommand) error {
  549 + var (
  550 + transactionContext, _ = factory.CreateTransactionContext(nil)
  551 + err error
  552 + )
  553 + if err = transactionContext.StartTransaction(); err != nil {
  554 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  555 + }
  556 + defer func() {
  557 + transactionContext.RollbackTransaction()
  558 + }()
  559 + var (
  560 + orderBaseRepository domain.OrderBaseRepository
  561 + oldOrderData *domain.OrderBase
  562 + )
  563 + if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{
  564 + "transactionContext": transactionContext,
  565 + }); err != nil {
  566 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  567 + }
  568 + //获取旧的订单
  569 + oldOrderData, err = orderBaseRepository.FindOne(domain.OrderBaseFindOneQuery{
  570 + OrderId: cmd.OrderId,
  571 + })
  572 + if err != nil {
  573 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("未找到指定的订单:%s", err))
  574 + }
  575 + if oldOrderData.OrderType != cmd.OrderType {
  576 + return lib.ThrowError(lib.BUSINESS_ERROR, fmt.Sprintf("操作失败,指定的订单的类型发生变更"))
  577 + }
  578 + oldOrderData.IsDisable = cmd.IsDisable
  579 + err = orderBaseRepository.Save(oldOrderData)
  580 + if err != nil {
  581 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单数据失败:%s", err))
  582 + }
  583 + err = transactionContext.CommitTransaction()
  584 + if err != nil {
  585 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  586 + }
  587 + return nil
  588 +}
  589 +
  590 +//UpdateGoodBouns 更新货品的分红相关的数值
  591 +func (service OrderInfoService) UpdateGoodBouns(cmd command.UpdateGoodBouns) error {
  592 + var (
  593 + transactionContext, _ = factory.CreateTransactionContext(nil)
  594 + err error
  595 + )
  596 + if err = transactionContext.StartTransaction(); err != nil {
  597 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  598 + }
  599 + defer func() {
  600 + transactionContext.RollbackTransaction()
  601 + }()
  602 + var (
  603 + orderBaseRepository domain.OrderBaseRepository
  604 + orderGoodRepository domain.OrderGoodRepository
  605 + oldOrderData *domain.OrderBase
  606 + oldOrderGoods []domain.OrderGood
  607 + )
  608 + if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{
  609 + "transactionContext": transactionContext,
  610 + }); err != nil {
  611 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  612 + }
  613 + if orderGoodRepository, err = factory.CreateOrderGoodRepository(map[string]interface{}{
  614 + "transactionContext": transactionContext,
  615 + }); err != nil {
  616 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  617 + }
  618 + //获取旧的订单
  619 + oldOrderData, err = orderBaseRepository.FindOne(domain.OrderBaseFindOneQuery{
  620 + OrderId: cmd.Id,
  621 + })
  622 + if err != nil {
  623 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("未找到指定的订单:%s", err))
  624 + }
  625 + if oldOrderData.OrderType != domain.OrderReal {
  626 + return lib.ThrowError(lib.BUSINESS_ERROR, fmt.Sprintf("指定的订单的订单类型发生变更"))
  627 + }
  628 + //获取旧的订单中的商品
  629 + oldOrderGoods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{
  630 + OrderId: cmd.Id,
  631 + })
  632 + if err != nil {
  633 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("未找到指定的订单中的商品列表失败:%s", err))
  634 + }
  635 + for _, newGood := range cmd.GoodBouns {
  636 + for i := range oldOrderGoods {
  637 + if newGood.GoodId != oldOrderGoods[i].Id {
  638 + continue
  639 + }
  640 + oldOrderGoods[i].UseGoodNumber = newGood.UseGoodNumber
  641 + oldOrderGoods[i].Remark = newGood.Remark
  642 + oldOrderGoods[i].BonusStatus = newGood.BounsStatus
  643 + err = oldOrderGoods[i].Compute()
  644 + if err != nil {
  645 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中商品的数值失败:%s", err))
  646 + }
  647 + switch newGood.BounsStatus {
  648 + case domain.OrderGoodWaitPay:
  649 + err = oldOrderGoods[i].CurrentBonusStatus.WartPayPartnerBonus(&oldOrderGoods[i])
  650 + if err != nil {
  651 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中商品的分红数值失败:%s", err))
  652 + }
  653 + case domain.OrderGoodHasPay:
  654 + err = oldOrderGoods[i].CurrentBonusStatus.PayPartnerBonus(&oldOrderGoods[i])
  655 + if err != nil {
  656 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中商品的分红数值失败:%s", err))
  657 + }
  658 + default:
  659 + return lib.ThrowError(lib.ARG_ERROR, fmt.Sprintf("货品的支付状态错误"))
  660 + }
  661 + }
  662 + }
  663 + oldOrderData.Goods = oldOrderGoods
  664 + //变更订单类型
  665 + err = oldOrderData.Compute()
  666 + if err != nil {
  667 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中合计的数值失败:%s", err))
  668 + }
  669 + err = orderBaseRepository.Save(oldOrderData)
  670 + if err != nil {
  671 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单数据失败:%s", err))
  672 + }
  673 + err = orderGoodRepository.Save(oldOrderGoods)
  674 + if err != nil {
  675 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单中的商品数据失败:%s", err))
  676 + }
  677 + err = transactionContext.CommitTransaction()
  678 + if err != nil {
  679 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  680 + }
  681 + return nil
  682 +
  683 +}
1 -package domain  
2 -  
3 -import (  
4 - "time"  
5 -  
6 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/utils"  
7 -)  
8 -  
9 -const (  
10 - OrderStatusReserve int = iota + 1 //预定中  
11 - OrderStatusDeliverSome //部分发货  
12 - OrderStatusDeliverAll //全部发货  
13 -)  
14 -  
15 -const (  
16 - OrderReal int = 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 {  
31 - Id int64 `json:"id"`  
32 - //订单类型  
33 - OrderType int `json:"orderType"`  
34 - //订单编号  
35 - OrderCode string `json:"orderCode"`  
36 - //订单名称  
37 - OrderName string `json:"oderName"`  
38 - //订单状态  
39 - OrderStatus int `json:"orderStatus"`  
40 - //数量  
41 - OrderCount int `json:"orderCount"`  
42 - //实际数量  
43 - OrderActualCount int `json:"orderActualCount"`  
44 - //订单金额  
45 - OrderAmount float64 `json:"orderAmount"`  
46 - //实际订单金额  
47 - OrderActualAmount float64 `json:"orderActualAmount"`  
48 - //订单已支付分红金额(货款)  
49 - OrderPaymentAmount float64 `json:"orderPaymentAmount"`  
50 - //订单区域信息  
51 - OrderRegionInfo RegionInfo `json:"orderRegionInfo"`  
52 - //买家  
53 - Buyer Buyer `json:"buyer"`  
54 - //合伙人数据  
55 - PartnerInfo Partner `json:"partnerInfo"`  
56 - //合伙人分红百分比  
57 - PartnerBonusPercent float64 `json:"partnerBonusPercent"`  
58 - //业务员分红百分比  
59 - SalesmanBonusPercent float64 `json:"salesmanBonusPercent"`  
60 - //最后查看得时间  
61 - LastViewTime time.Time `json:"lastViewTime"`  
62 - //更新时间  
63 - UpdateAt time.Time `json:"updateAt"`  
64 - CreateAt time.Time `json:"createAt"`  
65 -  
66 - //理由  
67 - Reason string  
68 -}  
69 -  
70 -//TODO  
71 -func (order *Order) Update(data map[string]interface{}) error {  
72 - if v, ok := data["orderType"]; ok {  
73 - order.OrderType = v.(int)  
74 - }  
75 - if v, ok := data["orderCode"]; ok {  
76 - order.OrderCode = v.(string)  
77 - }  
78 - if v, ok := data["oderName"]; ok {  
79 - order.OrderName = v.(string)  
80 - }  
81 - if v, ok := data["orderStatus"]; ok {  
82 - order.OrderStatus = v.(int)  
83 - }  
84 - if v, ok := data["orderCount"]; ok {  
85 - order.OrderCount = v.(int)  
86 - }  
87 - if v, ok := data["orderActualCount"]; ok {  
88 - order.OrderActualCount = v.(int)  
89 - }  
90 - if v, ok := data["orderAmount"]; ok {  
91 - order.OrderAmount = v.(float64)  
92 - }  
93 - if v, ok := data["orderActualAmount"]; ok {  
94 - order.OrderActualAmount = v.(float64)  
95 - }  
96 - if v, ok := data["orderPaymentAmount"]; ok {  
97 - order.OrderPaymentAmount = v.(float64)  
98 - }  
99 - if v, ok := data["orderRegion"]; ok {  
100 - order.OrderRegionInfo = v.(RegionInfo)  
101 - }  
102 - if v, ok := data["buyer"]; ok {  
103 - order.Buyer = v.(Buyer)  
104 - }  
105 - if v, ok := data["partnerInfo"]; ok {  
106 - order.PartnerInfo = v.(Partner)  
107 - }  
108 - if v, ok := data["partnerBonusPercent"]; ok {  
109 - order.PartnerBonusPercent = v.(float64)  
110 - }  
111 - if v, ok := data["salesmanBonusPercent"]; ok {  
112 - order.SalesmanBonusPercent = v.(float64)  
113 - }  
114 - if v, ok := data["reason"]; ok {  
115 - order.Reason = v.(string)  
116 - }  
117 - return nil  
118 -}  
119 -  
120 -//订单累计分红  
121 -func (m *Order) OrderTotalBonus() float64 {  
122 - return utils.Decimal(m.OrderActualAmount * (m.PartnerBonusPercent / 100.0))  
123 -}  
124 -  
125 -//订单已收分红  
126 -func (m *Order) OrderBonusReceive() float64 {  
127 - return utils.Decimal(m.OrderPaymentAmount * (m.PartnerBonusPercent / 100.0))  
128 -}  
129 -  
130 -//订单未收分红  
131 -func (m *Order) OrderBonusWait() float64 {  
132 - bonusWait := m.OrderTotalBonus() - m.OrderBonusReceive()  
133 - if bonusWait < 0 {  
134 - return 0  
135 - }  
136 - return bonusWait  
137 -}  
138 -  
139 -//分红支出  
140 -func (m *Order) OrderBonusOutstanding() float64 {  
141 - if m.OrderAmount <= m.OrderActualAmount {  
142 - return 0  
143 - }  
144 - return utils.Decimal((m.OrderAmount - m.OrderActualAmount) * (m.PartnerBonusPercent / 100.0))  
145 -}  
146 -  
147 -//订单被取消金额  
148 -func (m *Order) OrderAmountCancel() float64 {  
149 - if m.OrderAmount <= m.OrderActualAmount {  
150 - return 0  
151 - }  
152 - return utils.Decimal((m.OrderAmount - m.OrderActualAmount))  
153 -}  
154 -  
155 -type OrderFindOneQuery struct {  
156 - OrderId int64  
157 -}  
158 -  
159 -type OrderFindQuery struct {  
160 - PartnerId int64  
161 - OrderCode string  
162 - Offset int  
163 - Limit int  
164 - OrderType int  
165 -}  
166 -  
167 -type OrderRepository interface {  
168 - Save(order Order) error  
169 - FindOne(qureyOptions OrderFindOneQuery) (*Order, error)  
170 - Find(queryOptions OrderFindQuery) ([]Order, error)  
171 - CountAll(queryOption OrderFindQuery) (int, error)  
172 - Remove(id int64) error  
173 -}  
  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 +const (
  15 + OrderDisableNot = iota //订单未关闭
  16 + OrderDisableYes //订单已关闭
  17 +)
  18 +
  19 +//Buyer 买家
  20 +type Buyer struct {
  21 + //买家姓名
  22 + BuyerName string `json:"buyerName"`
  23 + //联系方式
  24 + ContactInfo string `json:"contactInfo"`
  25 + //收获地址
  26 + ShippingAddress string `json:"shippingAddress"`
  27 +}
  28 +
  29 +//OrderBase 订单基础
  30 +type OrderBase struct {
  31 + //表id
  32 + Id int64 `json:"id"`
  33 + //订单类型
  34 + OrderType int `json:"orderType"`
  35 + //订单编号
  36 + OrderCode string `json:"orderCode"`
  37 + //交货编号
  38 + DeliveryCode string `json:"deliveryCode"`
  39 + //买家
  40 + Buyer *Buyer `json:"buyer"`
  41 + //订单区域信息
  42 + RegionInfo *RegionInfo `json:"regionInfo"`
  43 + //订单对应的合伙人
  44 + PartnerId int64 `json:"partnerId"`
  45 + PartnerInfo Partner `json:"partnerInfo"`
  46 + //业务员抽成比例
  47 + SalesmanBonusPercent float64 `json:"salesmanBonusPercent"`
  48 + //订单的创建时间
  49 + CreateTime time.Time `json:"createTime"`
  50 + //发货时间
  51 + DeliveryTime time.Time `json:"deliveryTime"`
  52 + //更新时间
  53 + UpdateTime time.Time `json:"updateTime"`
  54 + //货品
  55 + Goods []OrderGood `json:"goods"`
  56 + //核算订单相关数据
  57 + OrderCompute OrderCompute `json:"orderCompute"`
  58 + //是否关闭订单
  59 + IsDisable int `json:"isDisable"`
  60 + //分红支付状态
  61 + BonusStatus int `json:"bonusStatus"`
  62 +}
  63 +
  64 +type OrderCompute struct {
  65 + //合伙人应收分红
  66 + PlanPartnerBonus float64 `json:"planPartnerBonus"`
  67 + //调整后的合伙人应收分红 (初始值=-1);
  68 + //业务判定时0是有效值,
  69 + //所以用空串表示无值,转换到数据库中为负值
  70 + UsePartnerBonus float64 `json:"usePartnerBonus"`
  71 + //合伙人已收分红
  72 + PartnerBonusHas float64 `json:"partnerBonusHas"`
  73 + //合伙人未收分红
  74 + PartnerBonusNot float64 `json:"partnerBonusNot"`
  75 + //合伙人分红支出
  76 + PartnerBonusExpense float64 `json:"partnerBonusExpense"`
  77 + //业务员抽成
  78 + SalesmanBonus float64 `json:"salesmanBonus"`
  79 + //预计的订单内货品总数
  80 + PlanOrderCount int `json:"planOrderCount"`
  81 + //预计的订单的总金额
  82 + PlanOrderAmount float64 `json:"planOrderAmount"`
  83 + //按需使用的订单内货品总数 (初始值=-1)
  84 + //业务判定时0是有效值,
  85 + //所以用空串表示无值,转换到数据库中为负值
  86 + UseOrderCount int `json:"useOrderCount"`
  87 + //按需使用的订单内货总金额 (初始值=-1)
  88 + //业务判定时0是有效值,
  89 + //所以用空串表示无值,转换到数据库中为负值
  90 + UseOrderAmount float64 `json:"useOrderAmount"`
  91 +}
  92 +
  93 +//Compute 数据汇总核算
  94 +func (order *OrderBase) Compute() error {
  95 + planPartnerBonus := decimal.NewFromFloat(0)
  96 + planOrderAmount := decimal.NewFromFloat(0)
  97 + var (
  98 + planOrderCount int = 0
  99 + useOrderCount int = 0
  100 + HasUseOrderCount bool = false
  101 + )
  102 + usePartnerBonus := decimal.NewFromFloat(0)
  103 + var hasUsePartnerBonus bool = false
  104 + useOrderAmount := decimal.NewFromFloat(0)
  105 + var hasUseOrderAmount bool = false
  106 + partnerBonusHas := decimal.NewFromFloat(0)
  107 + partnerBonusNot := decimal.NewFromFloat(0)
  108 + partnerBonusExpense := decimal.NewFromFloat(0)
  109 + //初始订单的支付状态
  110 + order.BonusStatus = OrderGoodWaitPay
  111 + //统计所以货品的值
  112 + for i := range order.Goods {
  113 + if order.Goods[i].BonusStatus == OrderGoodHasPay {
  114 + //确定订单的支付状态
  115 + order.BonusStatus = OrderGoodHasPay
  116 + }
  117 + planPartnerBonus = planPartnerBonus.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PlanPartnerBonus))
  118 + planOrderAmount = planOrderAmount.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PlanAmount))
  119 + planOrderCount += order.Goods[i].PlanGoodNumber
  120 + goodUseAmount := decimal.NewFromFloat(order.Goods[i].GoodCompute.UseAmount)
  121 + if goodUseAmount.GreaterThanOrEqual(decimal.NewFromFloat(0)) {
  122 + //调整值非负值得情况
  123 + hasUseOrderAmount = true
  124 + useOrderAmount = useOrderAmount.Add(goodUseAmount)
  125 + } else {
  126 + useOrderAmount = useOrderAmount.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PlanAmount))
  127 + }
  128 + goodUsePartnerBonus := decimal.NewFromFloat(order.Goods[i].GoodCompute.UsePartnerBonus)
  129 + if goodUsePartnerBonus.GreaterThanOrEqual(decimal.NewFromFloat(0)) {
  130 + hasUsePartnerBonus = true
  131 + usePartnerBonus = usePartnerBonus.Add(goodUsePartnerBonus)
  132 + } else {
  133 + usePartnerBonus = usePartnerBonus.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PlanPartnerBonus))
  134 + }
  135 + if order.Goods[i].UseGoodNumber >= 0 {
  136 + HasUseOrderCount = true
  137 + useOrderCount += order.Goods[i].UseGoodNumber
  138 + } else {
  139 + useOrderCount += order.Goods[i].PlanGoodNumber
  140 + }
  141 + partnerBonusHas = partnerBonusHas.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PartnerBonusHas))
  142 + partnerBonusNot = partnerBonusNot.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PartnerBonusNot))
  143 + partnerBonusExpense = partnerBonusExpense.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PartnerBonusExpense))
  144 + }
  145 + //汇总赋值
  146 + order.OrderCompute.PartnerBonusExpense, _ = partnerBonusExpense.Round(2).BigFloat().Float64()
  147 + order.OrderCompute.PartnerBonusHas, _ = partnerBonusHas.Round(2).BigFloat().Float64()
  148 + order.OrderCompute.PartnerBonusNot, _ = partnerBonusNot.Round(2).BigFloat().Float64()
  149 + order.OrderCompute.PlanPartnerBonus, _ = planPartnerBonus.Round(2).BigFloat().Float64()
  150 + order.OrderCompute.PlanOrderAmount, _ = planOrderAmount.Round(2).BigFloat().Float64()
  151 + order.OrderCompute.PlanOrderCount = planOrderCount
  152 +
  153 + if hasUsePartnerBonus {
  154 + order.OrderCompute.UsePartnerBonus, _ = usePartnerBonus.Round(2).BigFloat().Float64()
  155 + } else {
  156 + order.OrderCompute.UsePartnerBonus = -1
  157 + }
  158 + if hasUseOrderAmount {
  159 + order.OrderCompute.UseOrderAmount, _ = useOrderAmount.Round(2).BigFloat().Float64()
  160 + //计算业务员的抽成
  161 + order.OrderCompute.SalesmanBonus, _ = useOrderAmount.
  162 + Mul(decimal.NewFromFloat(order.SalesmanBonusPercent)).
  163 + Div(decimal.NewFromInt(100)).
  164 + Round(2).BigFloat().Float64()
  165 + } else {
  166 + order.OrderCompute.UseOrderAmount = -1
  167 + order.OrderCompute.SalesmanBonus, _ = planOrderAmount.
  168 + Mul(decimal.NewFromFloat(order.SalesmanBonusPercent)).
  169 + Div(decimal.NewFromInt(100)).
  170 + Round(2).BigFloat().Float64()
  171 + }
  172 + if HasUseOrderCount {
  173 + order.OrderCompute.UseOrderCount = useOrderCount
  174 + } else {
  175 + order.OrderCompute.UseOrderCount = -1
  176 + }
  177 + return nil
  178 +}
  179 +
  180 +type OrderBaseFindOneQuery struct {
  181 + OrderId int64
  182 +}
  183 +
  184 +type OrderBaseFindQuery struct {
  185 + PartnerId int64
  186 + OrderCode string
  187 + DeliveryCode string
  188 + Offset int
  189 + Limit int
  190 + OrderType int
  191 +}
  192 +
  193 +type OrderBaseRepository interface {
  194 + Save(order *OrderBase) error
  195 + FindOne(qureyOptions OrderBaseFindOneQuery) (*OrderBase, error)
  196 + Find(queryOptions OrderBaseFindQuery) ([]OrderBase, int, error)
  197 + Remove(id int64) error
  198 +}
  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) WartPayPartnerBonus(good *OrderGood) error {
  85 + good.GoodCompute.PartnerBonusExpense = 0
  86 + good.GoodCompute.PartnerBonusHas = 0
  87 + //初始状态为待支付时 ,合伙人未收分红等于合伙人应收分红(或者调整的)
  88 + if good.UseGoodNumber < 0 {
  89 + //数量没有调整,未收等于预计应收分红
  90 + good.GoodCompute.PartnerBonusNot = good.GoodCompute.PlanPartnerBonus
  91 + } else {
  92 + //数量有调整,未收等于调整后的应收分红
  93 + good.GoodCompute.PartnerBonusNot = good.GoodCompute.UsePartnerBonus
  94 + }
  95 + good.CurrentBonusStatus = OrderGoodBonusWaitPay{}
  96 + return nil
  97 +}
  98 +
  99 +func (waitPay OrderGoodBonusWaitPay) PayPartnerBonus(good *OrderGood) error {
  100 + //待支付状态转支付时
  101 + //合伙人已收收分红等于合伙人应收分红(或者调整的)
  102 + //计算已支付
  103 + if good.UseGoodNumber < 0 {
  104 + //数量没有调整,已收等于预计应收分红
  105 + good.GoodCompute.PartnerBonusHas = good.GoodCompute.PlanPartnerBonus
  106 + } else {
  107 + //数量有调整,已收等于调整后的应收分红
  108 + good.GoodCompute.PartnerBonusHas = good.GoodCompute.UsePartnerBonus
  109 + }
  110 + good.GoodCompute.PartnerBonusExpense = 0
  111 + good.GoodCompute.PartnerBonusNot = 0
  112 + good.CurrentBonusStatus = OrderGoodBonusHasPay{}
  113 + return nil
  114 +}
  115 +
  116 +func (hasPay OrderGoodBonusHasPay) PayPartnerBonus(good *OrderGood) error {
  117 + //已支付的值保持不变
  118 + //未支付的值保持不变
  119 + //计算分红支出
  120 + //分红支出=应收分红(有出现调整,则取调整后的应收分红)和已收分红的差额
  121 + if good.UseGoodNumber >= 0 {
  122 + //有数量调整,分红支出等于 已支付分红-调整后的已收分红
  123 + good.GoodCompute.PartnerBonusExpense = good.GoodCompute.PartnerBonusHas - good.GoodCompute.UsePartnerBonus
  124 + }
  125 + good.CurrentBonusStatus = OrderGoodBonusHasPay{}
  126 + return nil
  127 +}
  128 +
  129 +func (hasPay OrderGoodBonusHasPay) WartPayPartnerBonus(good *OrderGood) error {
  130 + return errors.New("已支付的货单不能将状态回退为待支付")
  131 +}
  132 +
  133 +//NewOrderGood 初始值设定
  134 +func NewOrderGood() OrderGood {
  135 + return OrderGood{
  136 + UseGoodNumber: -1,
  137 + BonusStatus: OrderGoodWaitPay,
  138 + GoodCompute: GoodCompute{
  139 + UsePartnerBonus: -1,
  140 + UseAmount: -1,
  141 + },
  142 + CurrentBonusStatus: OrderGoodBonusWaitPay{},
  143 + }
  144 +}
  145 +
  146 +//Compute 数据汇总核算
  147 +func (good *OrderGood) Compute() error {
  148 + //计算预计货品总值
  149 + //计算预计合伙人分红
  150 + price := decimal.NewFromFloat(good.Price)
  151 + planamount := price.Mul(decimal.NewFromInt(int64(good.PlanGoodNumber))) //price*planGoodNumber
  152 + //price*useGoodNumber
  153 + planPartnerBonus := planamount.Mul(decimal.NewFromFloat(good.PartnerBonusPercent)).Div(decimal.NewFromInt(100)) //price*planGoodNumber*PartnerBonusPercent
  154 + good.GoodCompute.PlanAmount, _ = planamount.Round(2).BigFloat().Float64()
  155 + good.GoodCompute.PlanPartnerBonus, _ = planPartnerBonus.Round(2).BigFloat().Float64()
  156 + if good.UseGoodNumber < 0 {
  157 + //没有出现数量调整
  158 + good.GoodCompute.UsePartnerBonus = -1
  159 + good.GoodCompute.UseAmount = -1
  160 + } else {
  161 + //计算调整后的货品总值
  162 + //计算调整后的合伙人分红
  163 + useamount := price.Mul(decimal.NewFromInt(int64(good.UseGoodNumber))) //price*useGoodNumber/price*useGoodNumber
  164 + usePartnerBonus := useamount.Mul(decimal.NewFromFloat(good.PartnerBonusPercent)).Div(decimal.NewFromInt(100)) //price*useGoodNumber*PartnerBonusPercent
  165 + good.GoodCompute.UsePartnerBonus, _ = usePartnerBonus.Round(2).BigFloat().Float64()
  166 + good.GoodCompute.UseAmount, _ = useamount.Round(2).BigFloat().Float64()
  167 + }
  168 +
  169 + return nil
  170 +}
  171 +
  172 +type OrderGoodFindQuery struct {
  173 + OrderId int64
  174 + Offset int
  175 + Limit int
  176 +}
  177 +
  178 +type OrderGoodRepository interface {
  179 + Save(order []OrderGood) error
  180 + Find(queryOptions OrderGoodFindQuery) ([]OrderGood, int, error)
  181 + Remove(orderid int64, ids ...int64) error
  182 +}
1 -package domain  
2 -  
3 -import "time"  
4 -  
5 -const (  
6 - BonusWaitPay = iota + 1 //等待支付分红  
7 - BonusPaid //已经支付分红  
8 -)  
9 -  
10 -type OrderPayment struct {  
11 - //编号  
12 - Id int64 `json:"id"`  
13 - //订单编号  
14 - OrderId int64 `json:"orderId"`  
15 - //合伙人编号  
16 - PartnerId int64 `json:"partnerId"`  
17 - //支付货款  
18 - PaymentAmount float64 `json:"paymentAmount"`  
19 - //分红金额  
20 - BonusAmount float64 `json:"bonusAmount"`  
21 - //分红状态 1.等待支付分红 2.已支付分红  
22 - BonusStatus int `json:"bonusStatus"`  
23 - //创建时间  
24 - CreateAt time.Time `json:"createAt"`  
25 - //更新时间  
26 - UpdateAt time.Time `json:"updateAt"`  
27 -  
28 - //扩展  
29 - PartnerBonusPercent float64 `json:"-"`  
30 -}  
31 -  
32 -func (m *OrderPayment) Identify() interface{} {  
33 - if m.Id == 0 {  
34 - return nil  
35 - }  
36 - return m.Id  
37 -}  
38 -  
39 -func (m *OrderPayment) Update(data map[string]interface{}) error {  
40 - if m.BonusStatus != BonusWaitPay {  
41 - return nil  
42 - }  
43 - if paymentAmount, ok := data["paymentAmount"]; ok && paymentAmount != 0 {  
44 - m.PaymentAmount = paymentAmount.(float64)  
45 - }  
46 - if bonusAmount, ok := data["bonusAmount"]; ok && bonusAmount != 0 {  
47 - m.BonusAmount = bonusAmount.(float64)  
48 - }  
49 - if bonusStatus, ok := data["bonusStatus"]; ok && bonusStatus != 0 {  
50 - m.BonusStatus = bonusStatus.(int)  
51 - }  
52 - m.UpdateAt = time.Now()  
53 - return nil  
54 -}  
55 -  
56 -type OrderPaymentFindOneQuery struct {  
57 - Id int64  
58 - OrderId int64  
59 - PaymentId int  
60 -}  
61 -type OrderPaymentQuery struct {  
62 - Offset int  
63 - Limit int  
64 -  
65 - OrderId int64  
66 - //PartnerCategory []int //合伙人类型  
67 - //RegionInfo *RegionInfo //区域  
68 - //PartnerName string //合伙人姓名  
69 -}  
70 -  
71 -type OrderPaymentRepository interface {  
72 - Save(dm *OrderPayment) (*OrderPayment, error)  
73 - FindOne(queryOptions OrderPaymentFindOneQuery) (*OrderPayment, error)  
74 - Find(queryOptions OrderPaymentQuery) ([]*OrderPayment, error)  
75 - CountAll(queryOptions OrderPaymentQuery) (int, error)  
76 -}  
  1 +package dao
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
  7 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
  8 +)
  9 +
  10 +type OrderBaseDao struct {
  11 + transactionContext *transaction.TransactionContext
  12 +}
  13 +
  14 +func NewOrderBaseDao(transactionContext *transaction.TransactionContext) (*OrderBaseDao, error) {
  15 + if transactionContext == nil {
  16 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  17 + } else {
  18 + return &OrderBaseDao{
  19 + transactionContext: transactionContext,
  20 + }, nil
  21 + }
  22 +}
  23 +
  24 +func (dao OrderBaseDao) OrderCodeExist(code string, notId ...int64) (bool, error) {
  25 + tx := dao.transactionContext.PgDd
  26 + m := &models.OrderBase{}
  27 + query := tx.Model(m).Where("order_code=?", code)
  28 + if len(notId) > 0 {
  29 + query = query.WhereIn("id not in(?)", notId)
  30 + }
  31 + ok, err := query.Exists()
  32 + return ok, err
  33 +}
  34 +
  35 +func (dao OrderBaseDao) DeliveryCodeExist(code string, notId ...int64) (bool, error) {
  36 + tx := dao.transactionContext.PgDd
  37 + m := &models.OrderBase{}
  38 + query := tx.Model(m).Where("delivery_code=?", code)
  39 + if len(notId) > 0 {
  40 + query = query.WhereIn("id not in(?)", notId)
  41 + }
  42 + ok, err := query.Exists()
  43 + return ok, err
  44 +}
1 -package dao  
2 -  
3 -import (  
4 - "fmt"  
5 -  
6 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"  
7 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"  
8 -)  
9 -  
10 -type OrderDao struct {  
11 - transactionContext *transaction.TransactionContext  
12 -}  
13 -  
14 -func (dao *OrderDao) Update(options map[string]interface{}) (err error) {  
15 - tx := dao.transactionContext.PgTx  
16 - order := new(models.Order)  
17 - q := tx.Model(order)  
18 - if v, ok := options["orderPaymentAmount"]; ok {  
19 - q.Set("order_payment_amount = ?", v)  
20 - }  
21 - if v, ok := options["bonusStatus"]; ok {  
22 - q.Set("bonus_status = ?", v)  
23 - }  
24 - if v, ok := options["id"]; ok {  
25 - q.Where("id = ?", v)  
26 - }  
27 - _, err = q.Update()  
28 - return  
29 -}  
30 -  
31 -func (dao *OrderDao) OrderCodeIsExist(code string, notId int64) (bool, error) {  
32 - tx := dao.transactionContext.PgDd  
33 - ok, err := tx.Model(new(models.Order)).  
34 - Where("order_code=?", code).  
35 - Where("id<>?", notId).  
36 - Exists()  
37 - return ok, err  
38 -}  
39 -  
40 -func (dao *OrderDao) GetOrderBaseInfo(id int64) (data map[string]interface{}, err error) {  
41 - tx := dao.transactionContext.PgTx  
42 - order := new(models.Order)  
43 -  
44 - data = make(map[string]interface{})  
45 - q := tx.Model(order)  
46 - q.Column("partner_id", "partner_bonus_percent", "order_payment_amount", "buyer")  
47 - q.Where("id = ?", id)  
48 - err = q.Select()  
49 - if err == nil {  
50 - data["PartnerId"] = order.PartnerId  
51 - data["PartnerBonusPercent"] = order.PartnerBonusPercent  
52 - data["OrderPaymentAmount"] = order.OrderPaymentAmount  
53 - data["Buyer"] = order.Buyer  
54 - }  
55 - return  
56 -}  
57 -  
58 -func (dao *OrderDao) GetDividendOrders(options map[string]interface{}) (count int, orders []*models.Order, err error) {  
59 - tx := dao.transactionContext.PgTx  
60 - //Order:=new(models.Order)  
61 - q := tx.Model(&orders)  
62 - q.ColumnExpr(`"order".*`, `partner_info.partner_name`)  
63 - q.Relation("PartnerInfo")  
64 - if v, ok := options["orderCode"]; ok && len(v.(string)) > 0 {  
65 - q.Where(`"order".order_code like ?`, fmt.Sprintf("%%%v%%", v))  
66 - }  
67 - if v, ok := options["orderType"]; ok {  
68 - q.Where(`"order".order_type=?`, v)  
69 - }  
70 - if v, ok := options["partnerId"]; ok && v.(int) > 0 {  
71 - q.Where(`"order".partner_id =?`, v)  
72 - }  
73 - if v, ok := options["offset"]; ok {  
74 - q.Offset(v.(int))  
75 - }  
76 - if v, ok := options["limit"]; ok {  
77 - q.Limit(v.(int))  
78 - }  
79 - q.Order(`order.id DESC`)  
80 - count, err = q.SelectAndCount()  
81 - return  
82 -}  
83 -  
84 -func NewOrderDao(transactionContext *transaction.TransactionContext) (*OrderDao, error) {  
85 - if transactionContext == nil {  
86 - return nil, fmt.Errorf("transactionContext参数不能为nil")  
87 - } else {  
88 - return &OrderDao{  
89 - transactionContext: transactionContext,  
90 - }, nil  
91 - }  
92 -}  
1 -package dao  
2 -  
3 -import (  
4 - "fmt"  
5 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"  
6 -  
7 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"  
8 -)  
9 -  
10 -type OrderPayment struct {  
11 - transactionContext *transaction.TransactionContext  
12 -}  
13 -  
14 -//删除数据  
15 -func (o *OrderPayment) Remove(orderId int64, status int, idList []int) error {  
16 - if len(idList) == 0 {  
17 - return nil  
18 - }  
19 - tx := o.transactionContext.PgTx  
20 - m := new(models.OrderPayment)  
21 - q := tx.Model(m).Where("order_id=?", orderId).  
22 - WhereIn("id not in(?)", idList)  
23 - _, err := q.Delete()  
24 - return err  
25 -}  
26 -  
27 -func NewOrderPayment(transactionContext *transaction.TransactionContext) (*OrderPayment, error) {  
28 - if transactionContext == nil {  
29 - return nil, fmt.Errorf("transactionContext参数不能为nil")  
30 - } else {  
31 - return &OrderPayment{  
32 - transactionContext: transactionContext,  
33 - }, nil  
34 - }  
35 -}  
  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 `pg:",use_zero"`
  30 + //业务员抽成
  31 + SalesmanBonus float64 `pg:",use_zero"`
  32 + //预计的订单内货品总数
  33 + PlanOrderCount int `pg:",use_zero"`
  34 + //预计的订单的总金额
  35 + PlanOrderAmount float64 `pg:",use_zero"`
  36 + //按需使用的订单内货品总数 (初始值=-1)
  37 + //业务判定时0是有效值,负值无效
  38 + UseOrderCount int `pg:",use_zero"`
  39 + //按需使用的订单内货总金额 (初始值=-1)
  40 + //业务判定时0是有效值,负值无效
  41 + UseOrderAmount float64 `pg:",use_zero"`
  42 + //订单的创建时间
  43 + CreateTime time.Time
  44 + //发货时间
  45 + DeliveryTime time.Time
  46 + //更新时间
  47 + UpdateTime time.Time
  48 + //合伙人应收分红
  49 + PlanPartnerBonus float64 `pg:",use_zero"`
  50 + //调整后的合伙人应收分红 (初始值=-1);
  51 + //业务判定时0是有效值,负值无效
  52 + UsePartnerBonus float64 `pg:",use_zero"`
  53 + //合伙人已收分红
  54 + PartnerBonusHas float64 `pg:",use_zero"`
  55 + //合伙人未收分红
  56 + PartnerBonusNot float64
  57 + //合伙人分红支出
  58 + PartnerBonusExpense float64 `pg:",use_zero"`
  59 + //上一次查看时间 已读情况
  60 + LastViewTime time.Time
  61 + //是否关闭订单
  62 + IsDisable int `pg:",use_zero"`
  63 + //分红支付状态
  64 + BonusStatus int
  65 +}
  66 +
  67 +var _ pg.BeforeUpdateHook = (*OrderBase)(nil)
  68 +
  69 +func (order *OrderBase) BeforeUpdate(ctx context.Context) (context.Context, error) {
  70 + order.UpdateTime = time.Now()
  71 + return ctx, nil
  72 +}
  73 +
  74 +var _ pg.BeforeInsertHook = (*OrderBase)(nil)
  75 +
  76 +func (order *OrderBase) BeforeInsert(ctx context.Context) (context.Context, error) {
  77 + order.CreateTime = time.Now()
  78 + order.UpdateTime = time.Now()
  79 + return ctx, nil
  80 +}
  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 `pg:",use_zero"`
  14 + //货品单价
  15 + Price float64 `pg:",use_zero"`
  16 + //预计的货品总额
  17 + PlanAmount float64 `pg:",use_zero"`
  18 + //调整后的货品数量(初始值=-1)
  19 + //业务判定时0是有效值,负值无效
  20 + UseGoodNumber int `pg:",use_zero"`
  21 + //调整后的货品总额
  22 + UseAmount float64 `pg:",use_zero"`
  23 + //合伙人分红比例
  24 + PartnerBonusPercent float64 `pg:",use_zero"`
  25 + //预计的合伙人分红
  26 + PlanPartnerBonus float64 `pg:",use_zero"`
  27 + //合伙人应收分红调整 (初始值=-1)
  28 + ////业务判定时0是有效值,负值无效
  29 + UsePartnerBonus float64 `pg:",use_zero"`
  30 + //合伙人已收分红
  31 + PartnerBonusHas float64 `pg:",use_zero"`
  32 + //合伙人未收分红
  33 + PartnerBonusNot float64 `pg:",use_zero"`
  34 + //合伙人分红支出
  35 + PartnerBonusExpense float64 `pg:",use_zero"`
  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 + IsDisable: orderModel.IsDisable,
  33 + OrderCompute: domain.OrderCompute{
  34 + PlanPartnerBonus: orderModel.PlanPartnerBonus, UsePartnerBonus: orderModel.UsePartnerBonus,
  35 + PartnerBonusHas: orderModel.PartnerBonusHas, PartnerBonusNot: orderModel.PartnerBonusNot,
  36 + PartnerBonusExpense: orderModel.PartnerBonusExpense, SalesmanBonus: orderModel.SalesmanBonus,
  37 + PlanOrderCount: orderModel.PlanOrderCount, PlanOrderAmount: orderModel.PlanOrderAmount,
  38 + UseOrderCount: orderModel.UseOrderCount, UseOrderAmount: orderModel.UseOrderAmount,
  39 + },
  40 + PartnerInfo: domain.Partner{
  41 + Id: orderModel.PartnerId,
  42 + },
  43 + BonusStatus: orderModel.BonusStatus,
  44 + }
  45 + return order, nil
  46 +}
  47 +
  48 +func (repository OrderBaseRepository) Save(orderInfo *domain.OrderBase) error {
  49 + var (
  50 + err error
  51 + tx = repository.transactionContext.PgTx
  52 + )
  53 + m := &models.OrderBase{
  54 + Id: orderInfo.Id, OrderType: orderInfo.OrderType, OrderCode: orderInfo.OrderCode,
  55 + DeliveryCode: orderInfo.DeliveryCode, Buyer: orderInfo.Buyer, RegionInfo: orderInfo.RegionInfo,
  56 + PartnerId: orderInfo.PartnerId, SalesmanBonusPercent: orderInfo.SalesmanBonusPercent,
  57 + SalesmanBonus: orderInfo.OrderCompute.SalesmanBonus, PlanOrderCount: orderInfo.OrderCompute.PlanOrderCount,
  58 + PlanOrderAmount: orderInfo.OrderCompute.PlanOrderAmount, UseOrderCount: orderInfo.OrderCompute.UseOrderCount,
  59 + UseOrderAmount: orderInfo.OrderCompute.UseOrderAmount, DeliveryTime: orderInfo.DeliveryTime,
  60 + PlanPartnerBonus: orderInfo.OrderCompute.PlanPartnerBonus, UsePartnerBonus: orderInfo.OrderCompute.UsePartnerBonus,
  61 + PartnerBonusHas: orderInfo.OrderCompute.PartnerBonusHas, PartnerBonusNot: orderInfo.OrderCompute.PartnerBonusNot,
  62 + PartnerBonusExpense: orderInfo.OrderCompute.PartnerBonusExpense, IsDisable: orderInfo.IsDisable,
  63 + CreateTime: orderInfo.CreateTime, BonusStatus: orderInfo.BonusStatus,
  64 + }
  65 + if m.Id == 0 {
  66 + _, err = tx.Model(m).
  67 + Returning("*").
  68 + Insert()
  69 + orderInfo.Id = m.Id
  70 + orderInfo.CreateTime = m.CreateTime
  71 + orderInfo.UpdateTime = m.UpdateTime
  72 + } else {
  73 + _, err = tx.Model(m).
  74 + WherePK().
  75 + Returning("*").
  76 + Update()
  77 + orderInfo.UpdateTime = m.UpdateTime
  78 + }
  79 + return err
  80 +}
  81 +
  82 +func (repository OrderBaseRepository) Find(queryOption domain.OrderBaseFindQuery) ([]domain.OrderBase, int, error) {
  83 + db := repository.transactionContext.PgDd
  84 + orderModels := []models.OrderBase{}
  85 + query := db.Model(&orderModels)
  86 + if queryOption.PartnerId > 0 {
  87 + query = query.Where("partner_id=?", queryOption.PartnerId)
  88 + }
  89 + if len(queryOption.OrderCode) > 0 {
  90 + query = query.Where("order_code like ?", "%"+queryOption.OrderCode+"%")
  91 + }
  92 + if len(queryOption.DeliveryCode) > 0 {
  93 + query = query.Where("delivery_code like ?", "%"+queryOption.DeliveryCode+"%")
  94 + }
  95 + if queryOption.OrderType > 0 {
  96 + query = query.Where("order_type=?", queryOption.OrderType)
  97 + }
  98 + if queryOption.Offset > -1 {
  99 + query = query.Offset(queryOption.Offset)
  100 + }
  101 + if queryOption.Limit > 0 {
  102 + query = query.Limit(queryOption.Limit)
  103 + } else {
  104 + query = query.Limit(20)
  105 + }
  106 + var (
  107 + err error
  108 + ordersReturn = make([]domain.OrderBase, 0)
  109 + )
  110 + query = query.Order("order_base.id DESC")
  111 + count, err := query.SelectAndCount()
  112 + if err != nil {
  113 + return ordersReturn, 0, err
  114 + }
  115 + for i := range orderModels {
  116 + domainOrder, err := repository.transformPgModelToDomainModel(&orderModels[i])
  117 + if err != nil {
  118 + return ordersReturn, 0, err
  119 + }
  120 + ordersReturn = append(ordersReturn, domainOrder)
  121 + }
  122 + return ordersReturn, count, nil
  123 +}
  124 +
  125 +func (repository OrderBaseRepository) FindOne(qureyOptions domain.OrderBaseFindOneQuery) (*domain.OrderBase, error) {
  126 + var (
  127 + err error
  128 + tx = repository.transactionContext.PgDd
  129 + )
  130 + m := new(models.OrderBase)
  131 + err = tx.Model(m).
  132 + Where("id=?", qureyOptions.OrderId).
  133 + First()
  134 + if err != nil {
  135 + return nil, err
  136 + }
  137 + result, err := repository.transformPgModelToDomainModel(m)
  138 + return &result, err
  139 +}
  140 +
  141 +func (repository OrderBaseRepository) Remove(id int64) error {
  142 + var (
  143 + err error
  144 + tx = repository.transactionContext.PgTx
  145 + )
  146 + m := &models.OrderBase{
  147 + Id: id,
  148 + }
  149 + _, err = tx.Model(m).WherePK().Delete()
  150 + return err
  151 +}
  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(100)
  101 + }
  102 + var (
  103 + err error
  104 + orderGoods = make([]domain.OrderGood, 0)
  105 + )
  106 + query = query.Order("order_good.id")
  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.OrderGood{}
  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 +}
1 -package repository  
2 -  
3 -import (  
4 - "fmt"  
5 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"  
6 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"  
7 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"  
8 -)  
9 -  
10 -type OrderPaymentRepository struct {  
11 - transactionContext *transaction.TransactionContext  
12 -}  
13 -  
14 -func (repository *OrderPaymentRepository) Save(dm *domain.OrderPayment) (*domain.OrderPayment, error) {  
15 - var (  
16 - err error  
17 - tx = repository.transactionContext.PgTx  
18 - )  
19 - m := &models.OrderPayment{  
20 - Id: dm.Id,  
21 - OrderId: dm.OrderId,  
22 - PartnerId: dm.PartnerId,  
23 - PaymentAmount: dm.PaymentAmount,  
24 - BonusAmount: dm.BonusAmount,  
25 - BonusStatus: dm.BonusStatus,  
26 - CreateAt: dm.CreateAt,  
27 - UpdateAt: dm.UpdateAt,  
28 - }  
29 - if m.Id == 0 {  
30 - err = tx.Insert(m)  
31 - dm.Id = m.Id  
32 - if err != nil {  
33 - return nil, err  
34 - }  
35 - } else {  
36 - err = tx.Update(m)  
37 - if err != nil {  
38 - return nil, err  
39 - }  
40 - }  
41 - return dm, nil  
42 -}  
43 -  
44 -func (repository *OrderPaymentRepository) Remove(OrderPayment *domain.OrderPayment) (*domain.OrderPayment, error) {  
45 - var (  
46 - tx = repository.transactionContext.PgTx  
47 - OrderPaymentModel = &models.OrderPayment{Id: OrderPayment.Identify().(int64)}  
48 - )  
49 - if _, err := tx.Model(OrderPaymentModel).Where("id = ?", OrderPayment.Id).Delete(); err != nil {  
50 - return OrderPayment, err  
51 - }  
52 - return OrderPayment, nil  
53 -}  
54 -  
55 -func (repository *OrderPaymentRepository) FindOne(queryOptions domain.OrderPaymentFindOneQuery) (*domain.OrderPayment, error) {  
56 - tx := repository.transactionContext.PgTx  
57 - OrderPaymentModel := new(models.OrderPayment)  
58 - query := tx.Model(OrderPaymentModel)  
59 -  
60 - if queryOptions.OrderId > 0 {  
61 - query.Where("order_payment.order_id = ?", queryOptions.OrderId)  
62 - }  
63 - if queryOptions.PaymentId > 0 {  
64 - query.Where("order_payment.id = ?", queryOptions.PaymentId)  
65 - }  
66 - if err := query.First(); err != nil {  
67 - return nil, err  
68 - }  
69 - if OrderPaymentModel.Id == 0 {  
70 - return nil, nil  
71 - }  
72 - return repository.transformPgModelToDomainModel(OrderPaymentModel)  
73 -}  
74 -  
75 -func (repository *OrderPaymentRepository) Find(queryOptions domain.OrderPaymentQuery) ([]*domain.OrderPayment, error) {  
76 - tx := repository.transactionContext.PgTx  
77 - var OrderPaymentModels []*models.OrderPayment  
78 - query := tx.Model(&OrderPaymentModels)  
79 - query.Where("order_id=?", queryOptions.OrderId)  
80 - query.Order("id ASC")  
81 - var (  
82 - err error  
83 - rsp = make([]*domain.OrderPayment, 0)  
84 - )  
85 - err = query.Select()  
86 - if err != nil {  
87 - return rsp, err  
88 - }  
89 - for i := range OrderPaymentModels {  
90 - dm, err := repository.transformPgModelToDomainModel(OrderPaymentModels[i])  
91 - if err != nil {  
92 - return rsp, err  
93 - }  
94 - rsp = append(rsp, dm)  
95 - }  
96 - return rsp, nil  
97 -}  
98 -  
99 -func (repository OrderPaymentRepository) CountAll(queryOption domain.OrderPaymentQuery) (int, error) {  
100 - db := repository.transactionContext.PgDd  
101 - partnerModels := models.PartnerInfo{}  
102 - query := db.Model(&partnerModels)  
103 - //if len(queryOption.PartnerName) > 0 {  
104 - // query = query.Where("partner_name like ?", "%"+queryOption.PartnerName+"%")  
105 - //}  
106 - //if queryOption.RegionInfo != nil {  
107 - // query = query.Where("region_info::jsonb->>'regionName' like ?", "%"+queryOption.RegionInfo.RegionName+"%")  
108 - //}  
109 - //if len(queryOption.PartnerCategory) > 0 {  
110 - // query = query.WhereIn("partner_category in(?)", queryOption.PartnerCategory)  
111 - //}  
112 - cnt, err := query.Count()  
113 - return cnt, err  
114 -}  
115 -  
116 -func (repository *OrderPaymentRepository) transformPgModelToDomainModel(dm *models.OrderPayment) (*domain.OrderPayment, error) {  
117 - m := &domain.OrderPayment{  
118 - Id: dm.Id,  
119 - OrderId: dm.OrderId,  
120 - PartnerId: dm.PartnerId,  
121 - PaymentAmount: dm.PaymentAmount,  
122 - BonusAmount: dm.BonusAmount,  
123 - BonusStatus: dm.BonusStatus,  
124 - CreateAt: dm.CreateAt,  
125 - UpdateAt: dm.UpdateAt,  
126 - }  
127 - return m, nil  
128 -}  
129 -  
130 -func NewOrderPaymentRepository(transactionContext *transaction.TransactionContext) (*OrderPaymentRepository, error) {  
131 - if transactionContext == nil {  
132 - return nil, fmt.Errorf("transactionContext参数不能为nil")  
133 - }  
134 - return &OrderPaymentRepository{transactionContext: transactionContext}, nil  
135 -}  
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 OrderRepository struct {  
12 - transactionContext *transaction.TransactionContext  
13 -}  
14 -  
15 -var (  
16 - _ domain.OrderRepository = (*OrderRepository)(nil)  
17 -)  
18 -  
19 -func NewOrderRepository(transactionContext *transaction.TransactionContext) (*OrderRepository, error) {  
20 - if transactionContext == nil {  
21 - return nil, fmt.Errorf("transactionContext参数不能为nil")  
22 - }  
23 - return &OrderRepository{transactionContext: transactionContext}, nil  
24 -}  
25 -  
26 -func (reponsitory OrderRepository) transformPgModelToDomainModel(orderModel *models.Order) (domain.Order, error) {  
27 - result := domain.Order{  
28 - Id: orderModel.Id,  
29 - OrderType: orderModel.OrderType,  
30 - OrderCode: orderModel.OrderCode,  
31 - OrderName: orderModel.OrderName,  
32 - OrderStatus: orderModel.OrderStatus,  
33 - OrderCount: orderModel.OrderCount,  
34 - OrderActualCount: orderModel.OrderActualCount,  
35 - OrderAmount: orderModel.OrderAmount,  
36 - OrderActualAmount: orderModel.OrderActualAmount,  
37 - OrderPaymentAmount: orderModel.OrderPaymentAmount,  
38 - OrderRegionInfo: orderModel.OrderRegionInfo,  
39 - Buyer: orderModel.Buyer,  
40 - PartnerInfo: domain.Partner{  
41 - Id: orderModel.PartnerId,  
42 - },  
43 - PartnerBonusPercent: orderModel.PartnerBonusPercent,  
44 - SalesmanBonusPercent: orderModel.SalesmanBonusPercent,  
45 - LastViewTime: orderModel.LastViewTime,  
46 - UpdateAt: orderModel.UpdateAt,  
47 - CreateAt: orderModel.CreateAt,  
48 - Reason: orderModel.Reason,  
49 - }  
50 - return result, nil  
51 -}  
52 -  
53 -func (repository OrderRepository) Save(orderInfo domain.Order) error {  
54 - var (  
55 - err error  
56 - tx = repository.transactionContext.PgTx  
57 - )  
58 - m := &models.Order{  
59 - Id: orderInfo.Id,  
60 - OrderType: orderInfo.OrderType,  
61 - OrderCode: orderInfo.OrderCode,  
62 - OrderName: orderInfo.OrderName,  
63 - OrderStatus: orderInfo.OrderStatus,  
64 - OrderCount: orderInfo.OrderCount,  
65 - OrderActualCount: orderInfo.OrderActualCount,  
66 - OrderAmount: orderInfo.OrderAmount,  
67 - OrderActualAmount: orderInfo.OrderActualAmount,  
68 - OrderPaymentAmount: orderInfo.OrderPaymentAmount,  
69 - OrderRegionInfo: orderInfo.OrderRegionInfo,  
70 - Buyer: orderInfo.Buyer,  
71 - PartnerId: orderInfo.PartnerInfo.Id,  
72 - PartnerBonusPercent: orderInfo.PartnerBonusPercent,  
73 - SalesmanBonusPercent: orderInfo.SalesmanBonusPercent,  
74 - BonusStatus: 1,  
75 - Reason: orderInfo.Reason,  
76 - }  
77 - if m.Id == 0 {  
78 - err = tx.Insert(m)  
79 - orderInfo.Id = m.Id  
80 - } else {  
81 - _, err = tx.Model(m).WherePK().  
82 - Column("order_type", "order_code", "order_name", "order_status", "order_count",  
83 - "order_actual_count", "order_amount", "order_actual_amount", "order_payment_amount",  
84 - "order_region_info", "buyer", "partner_id", "partner_bonus_percent", "salesman_bonus_percent",  
85 - "update_at", "reason").  
86 - Update()  
87 - }  
88 - return err  
89 -}  
90 -  
91 -func (repository OrderRepository) Find(queryOption domain.OrderFindQuery) ([]domain.Order, error) {  
92 - db := repository.transactionContext.PgDd  
93 - orderModels := []models.Order{}  
94 - query := db.Model(&orderModels)  
95 - if queryOption.PartnerId > 0 {  
96 - query = query.Where("partner_id=?", queryOption.PartnerId)  
97 - }  
98 - if len(queryOption.OrderCode) > 0 {  
99 - query = query.Where("order_code like ?", "%"+queryOption.OrderCode+"%")  
100 - }  
101 - if queryOption.OrderType > 0 {  
102 - query = query.Where("order_type=?", queryOption.OrderType)  
103 - }  
104 - if queryOption.Offset > -1 {  
105 - query = query.Offset(queryOption.Offset)  
106 - }  
107 - if queryOption.Limit > 0 {  
108 - query = query.Limit(queryOption.Limit)  
109 - } else {  
110 - query = query.Limit(20)  
111 - }  
112 - var (  
113 - err error  
114 - ordersReturn = make([]domain.Order, 0)  
115 - )  
116 - query = query.Order("order.id DESC")  
117 - err = query.Select()  
118 - if err != nil {  
119 - return ordersReturn, err  
120 - }  
121 - for i := range orderModels {  
122 - domainOrder, err := repository.transformPgModelToDomainModel(&orderModels[i])  
123 - if err != nil {  
124 - return ordersReturn, err  
125 - }  
126 - ordersReturn = append(ordersReturn, domainOrder)  
127 - }  
128 - return ordersReturn, nil  
129 -}  
130 -  
131 -func (repository OrderRepository) CountAll(queryOption domain.OrderFindQuery) (int, error) {  
132 - db := repository.transactionContext.PgDd  
133 - orderModels := []models.Order{}  
134 - query := db.Model(&orderModels)  
135 - if queryOption.PartnerId > 0 {  
136 - query = query.Where("partner_id=?", queryOption.PartnerId)  
137 - }  
138 - if len(queryOption.OrderCode) > 0 {  
139 - query = query.Where("order_code like ?", "%"+queryOption.OrderCode+"%")  
140 - }  
141 - if queryOption.OrderType > 0 {  
142 - query = query.Where("order_type=?", queryOption.OrderType)  
143 - }  
144 - var (  
145 - err error  
146 - )  
147 - cnt, err := query.Count()  
148 - if err != nil {  
149 - return cnt, err  
150 - }  
151 - return cnt, nil  
152 -}  
153 -  
154 -func (repository OrderRepository) FindOne(qureyOptions domain.OrderFindOneQuery) (*domain.Order, error) {  
155 - var (  
156 - err error  
157 - tx = repository.transactionContext.PgDd  
158 - )  
159 - m := new(models.Order)  
160 - err = tx.Model(m).  
161 - Where("id=?", qureyOptions.OrderId).  
162 - First()  
163 - if err != nil {  
164 - return nil, err  
165 - }  
166 - result, err := repository.transformPgModelToDomainModel(m)  
167 - return &result, err  
168 -}  
169 -  
170 -func (repository OrderRepository) Remove(id int64) error {  
171 - var (  
172 - err error  
173 - tx = repository.transactionContext.PgTx  
174 - )  
175 - m := &models.Order{  
176 - Id: id,  
177 - }  
178 - _, err = tx.Model(m).WherePK().Delete()  
179 - return err  
180 -}  
1 package controllers 1 package controllers
2 2
3 import ( 3 import (
  4 + "bytes"
4 "encoding/json" 5 "encoding/json"
5 "errors" 6 "errors"
6 "fmt" 7 "fmt"
@@ -21,7 +22,10 @@ type BaseController struct { @@ -21,7 +22,10 @@ type BaseController struct {
21 } 22 }
22 23
23 func (controller BaseController) BindJsonData(v interface{}) error { 24 func (controller BaseController) BindJsonData(v interface{}) error {
24 - return json.Unmarshal(controller.Ctx.Input.RequestBody, v) 25 + newDecoder := json.NewDecoder(bytes.NewReader(controller.Ctx.Input.RequestBody))
  26 + newDecoder.UseNumber()
  27 + err := newDecoder.Decode(v)
  28 + return err
25 } 29 }
26 30
27 func (controller BaseController) ResponseError(err error) { 31 func (controller BaseController) ResponseError(err error) {
@@ -38,7 +42,6 @@ func (controller BaseController) ResponseError(err error) { @@ -38,7 +42,6 @@ func (controller BaseController) ResponseError(err error) {
38 } else { 42 } else {
39 logs.Error(e.Prefix, e.Message) 43 logs.Error(e.Prefix, e.Message)
40 } 44 }
41 -  
42 } 45 }
43 controller.Data["json"] = protocol.ResponseData{ 46 controller.Data["json"] = protocol.ResponseData{
44 Code: -1, 47 Code: -1,
1 -package controllers  
2 -  
3 -import (  
4 - "errors"  
5 - "fmt"  
6 - "github.com/astaxie/beego/logs"  
7 - OrderPaymentCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/command"  
8 - OrderPaymentQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/query"  
9 - OrderPaymentSvr "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/service"  
10 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/utils"  
11 -  
12 - OrderQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/query"  
13 - OrderSvr "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/service"  
14 -  
15 - PartnerInfoQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/partnerInfo/query"  
16 - PartnerInfoSvr "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/partnerInfo/service"  
17 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"  
18 - "strconv"  
19 -)  
20 -  
21 -type DividendsController struct {  
22 - BaseController  
23 -}  
24 -  
25 -////Prepare 重写 BaseController 的Prepare方法  
26 -func (c *DividendsController) Prepare() {  
27 - c.BaseController.Prepare()  
28 - if ok := c.ValidJWTToken(); !ok {  
29 - return  
30 - }  
31 - if ok := c.ValidAdminPermission(domain.PERMISSION_DIVIDEND); !ok {  
32 - return  
33 - }  
34 -}  
35 -  
36 -//Edit 编辑分红支付  
37 -func (c *DividendsController) Edit() {  
38 - //用与适配前端定义的数据结构  
39 - type DividendPaymentItem struct {  
40 - PaymentForGoods float64 `json:"paymentForGoods"`  
41 - StateOfPayment int `json:"stateOfPayment"`  
42 - Id int `json:"id"`  
43 - }  
44 - type Parameter struct {  
45 - Id string `json:"id"` //订单编号  
46 - DividendPayment []DividendPaymentItem `json:"dividendPayment"`  
47 - }  
48 - var (  
49 - param Parameter  
50 - err error  
51 - )  
52 - if err = c.BindJsonData(&param); err != nil {  
53 - logs.Error(err)  
54 - c.ResponseError(errors.New("json数据解析失败"))  
55 - return  
56 - }  
57 - if len(param.Id) == 0 {  
58 - c.ResponseError(errors.New("id is empty"))  
59 - return  
60 - }  
61 - orderId, _ := strconv.ParseInt(param.Id, 10, 64)  
62 - cmd := OrderPaymentCmd.CreateOrderPaymentCommand{  
63 - OrderId: orderId,  
64 - DivdendPaymentItem: make([]OrderPaymentCmd.DivdendPyamentItem, 0),  
65 - }  
66 -  
67 - //编辑  
68 - for i := range param.DividendPayment {  
69 - item := param.DividendPayment[i]  
70 - paymentItem := OrderPaymentCmd.DivdendPyamentItem{}  
71 - paymentItem.PaymentForGoods = item.PaymentForGoods  
72 - paymentItem.StateOfPayment = item.StateOfPayment  
73 - paymentItem.PaymentSn = i + 1  
74 - paymentItem.PaymentId = item.Id  
75 - if paymentItem.StateOfPayment == domain.BonusPaid {  
76 - cmd.TotalPaymentAmount += paymentItem.PaymentForGoods  
77 - }  
78 - cmd.DivdendPaymentItem = append(cmd.DivdendPaymentItem, paymentItem)  
79 - }  
80 - serve := OrderPaymentSvr.NewOrderPaymentService(nil)  
81 - _, err = serve.CreateOrderPayment(&cmd)  
82 - if err != nil {  
83 - c.ResponseError(err)  
84 - return  
85 - }  
86 - c.ResponseData(nil)  
87 - return  
88 -}  
89 -  
90 -//Edit 分红支付详情  
91 -func (c *DividendsController) Detail() {  
92 - type Parameter struct {  
93 - Id string `json:"id"` //订单编号  
94 - }  
95 - var (  
96 - param Parameter  
97 - err error  
98 - order *domain.Order  
99 - partner *domain.PartnerInfo  
100 - )  
101 - if err = c.BindJsonData(&param); err != nil {  
102 - logs.Error(err)  
103 - c.ResponseError(errors.New("json数据解析失败"))  
104 - return  
105 - }  
106 -  
107 - cmd := OrderPaymentQuery.GetOrderPaymentQuery{}  
108 - cmd.OrderId, _ = strconv.ParseInt(param.Id, 10, 64)  
109 -  
110 - var (  
111 - OrderPaymentSvr = OrderPaymentSvr.NewOrderPaymentService(nil)  
112 - OrderSvr = OrderSvr.NewOrderService(nil)  
113 - PartnerInfoSvr = PartnerInfoSvr.NewPartnerInfoService(nil)  
114 - )  
115 - var data []*domain.OrderPayment  
116 - data, err = OrderPaymentSvr.ListOrderPayment(&cmd)  
117 - if err != nil {  
118 - c.ResponseError(err)  
119 - return  
120 - }  
121 - if order, err = OrderSvr.GetOrder(OrderQuery.GetOrderQuery{OrderId: cmd.OrderId}); err != nil {  
122 - c.ResponseError(err)  
123 - return  
124 - }  
125 - if partner, err = PartnerInfoSvr.GetPartnerInfo(PartnerInfoQuery.GetPartnerInfoQuery{Id: order.PartnerInfo.Id}); err != nil {  
126 - c.ResponseError(err)  
127 - return  
128 - }  
129 -  
130 - type DividendPayment struct {  
131 - PaymentForGoods float64 `json:"paymentForGoods"`  
132 - UpdateTime string `json:"updateTime"`  
133 - StateOfPayment int `json:"stateOfPayment"`  
134 - Dividend float64 `json:"dividend"`  
135 - DividendProportion float64 `json:"dividendProportion"`  
136 - Id int64 `json:"id"`  
137 - }  
138 - type Order struct {  
139 - OrderNumber string `json:"orderNumber"` //订单号  
140 - OrderState int `json:"orderState"` //订单状态  
141 - CreateTime string `json:"createTime"` //订单创建时间  
142 - OrderName string `json:"orderName"` //订单名称  
143 - OrderNum string `json:"orderNum"` //订单数量  
144 - OrderAmount string `json:"orderAmount"` //订单金额  
145 - Id string `json:"id"` //订单编号  
146 - }  
147 - type Partner struct {  
148 - PartnerName string `json:"partnerName"` //合伙人姓名  
149 - DividendProportion float64 `json:"dividendProportion"` //分红比例  
150 - DividendsReceivable float64 `json:"dividendsReceivable"` //应收分红  
151 - DividendSpending float64 `json:"dividendSpending"` //分红支出  
152 - ReceiveDividends float64 `json:"receiveDividends"` //实收分红  
153 - NotReceivedDividends float64 `json:"notReceivedDividends"` //未收分红  
154 - RceivedDividends float64 `json:"receivedDividends"` //已收分红  
155 - }  
156 - type Commission struct {  
157 - SalesmanName string `json:"salesmanName"` //业务员名称  
158 - CommissionProportion float64 `json:"commissionProportion"` //业务员抽成比例  
159 - ExpectedCommission float64 `json:"expectedCommission"` //业务员预计抽成  
160 - }  
161 - type Response struct {  
162 - DividendPayment []DividendPayment `json:"dividendPayment"`  
163 - Order Order `json:"order"`  
164 - Partner Partner `json:"partner"`  
165 - Commission Commission `json:"commission"`  
166 - }  
167 - rsp := Response{DividendPayment: make([]DividendPayment, 0)}  
168 - for i := range data {  
169 - item := data[i]  
170 - payment := DividendPayment{  
171 - PaymentForGoods: item.PaymentAmount,  
172 - UpdateTime: item.UpdateAt.Local().Format("2006-01-02 15:04:05"),  
173 - StateOfPayment: item.BonusStatus,  
174 - Dividend: utils.Decimal(item.PaymentAmount * (order.PartnerBonusPercent / 100.0)),  
175 - DividendProportion: order.PartnerBonusPercent,  
176 - Id: item.Id,  
177 - }  
178 - rsp.DividendPayment = append(rsp.DividendPayment, payment)  
179 - }  
180 - rsp.Order = Order{  
181 - OrderNumber: order.OrderCode,  
182 - OrderState: order.OrderStatus,  
183 - CreateTime: order.CreateAt.Local().Format("2006-01-02 15:04:05"),  
184 - OrderName: order.OrderName,  
185 - OrderNum: fmt.Sprintf("%v", order.OrderActualCount),  
186 - OrderAmount: fmt.Sprintf("%v", order.OrderActualAmount),  
187 - Id: fmt.Sprintf("%v", order.Id),  
188 - }  
189 - rsp.Partner = Partner{  
190 - PartnerName: partner.Partner.PartnerName,  
191 - DividendProportion: order.PartnerBonusPercent,  
192 - DividendsReceivable: order.OrderTotalBonus() + order.OrderBonusOutstanding(), //应收分红 = 实收分红+分红支出  
193 - DividendSpending: order.OrderBonusOutstanding(), //分红支出  
194 - ReceiveDividends: order.OrderTotalBonus(), //实收分红 = 实际金额 * 比例  
195 - NotReceivedDividends: order.OrderBonusWait(), //未收分红  
196 - RceivedDividends: order.OrderBonusReceive(), //已收分红  
197 - }  
198 - rsp.Commission = Commission{  
199 - CommissionProportion: order.SalesmanBonusPercent,  
200 - ExpectedCommission: utils.Decimal(order.OrderActualAmount * (order.SalesmanBonusPercent / 100)),  
201 - }  
202 - if len(partner.Salesman) > 0 {  
203 - rsp.Commission.SalesmanName = partner.Salesman[0].Name  
204 - }  
205 -  
206 - c.ResponseData(rsp)  
207 - return  
208 -}  
209 -  
210 -//分红管理  
211 -func (c *DividendsController) List() {  
212 - var (  
213 - param OrderPaymentQuery.ListDividendOrdersQuery  
214 - err error  
215 - count int  
216 - )  
217 - if err = c.BindJsonData(&param); err != nil {  
218 - logs.Error(err)  
219 - c.ResponseError(errors.New("json数据解析失败"))  
220 - return  
221 - }  
222 -  
223 - if param.PageSize == 0 {  
224 - param.PageSize = 20  
225 - }  
226 - if param.PageNumber == 0 {  
227 - param.PageNumber = 1  
228 - }  
229 - serve := OrderPaymentSvr.NewOrderPaymentService(nil)  
230 - var data interface{}  
231 - count, data, err = serve.ListDividendOrders(&param)  
232 - if err != nil {  
233 - c.ResponseError(err)  
234 - return  
235 - }  
236 - c.ResponsePageList(data, count, param.PageNumber)  
237 - return  
238 -}  
1 -package controllers  
2 -  
3 -import (  
4 - "errors"  
5 - "strconv"  
6 -  
7 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"  
8 -  
9 - "github.com/astaxie/beego/logs"  
10 - orderCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/command"  
11 - orderQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/query"  
12 - orderService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/service"  
13 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"  
14 -)  
15 -  
16 -type OrderController struct {  
17 - BaseController  
18 -}  
19 -  
20 -////Prepare 重写 BaseController 的Prepare方法  
21 -func (c *OrderController) Prepare() {  
22 - c.BaseController.Prepare()  
23 - if ok := c.ValidJWTToken(); !ok {  
24 - return  
25 - }  
26 - if ok := c.ValidAdminPermission(domain.PERMISSION_ORDER); !ok {  
27 - return  
28 - }  
29 -}  
30 -  
31 -type orderDetail struct {  
32 - //买家姓名  
33 - Buyer string `json:"buyer"`  
34 - //买家联系方式  
35 - BuyerPhone string `json:"buyerPhone"`  
36 - //收货地址  
37 - Address string `json:"address"`  
38 - //对应合伙人 id  
39 - Partner int64 `json:"partner"`  
40 - //合伙人抽成比例  
41 - PartnerRatio float64 `json:"partnerRatio"`  
42 - //业务员抽成比例  
43 - SalesmanRatio float64 `json:"salesmanRatio"`  
44 - //订单号  
45 - OrderId string `json:"orderId"`  
46 - //订单名称  
47 - OrderName string `json:"orderName"`  
48 - //订单数量  
49 - OrderNum int `json:"orderNum"`  
50 - //订单金额  
51 - OrderPrice float64 `json:"orderPrice"`  
52 - //订单区域  
53 - OrderDist string `json:"orderDist"`  
54 - //id  
55 - Id int64 `json:"id"`  
56 - //订单状态  
57 - OrderStatue int `json:"orderStatue"`  
58 - //理由  
59 - Reason string `json:"reason"`  
60 -}  
61 -  
62 -//UpdateOrderPurpose 更新意向订单  
63 -func (c *OrderController) UpdateOrderPurpose() {  
64 - //用与适配前端定义的数据结构  
65 - var (  
66 - param orderDetail  
67 - err error  
68 - )  
69 - if err = c.BindJsonData(&param); err != nil {  
70 - logs.Error(err)  
71 - c.ResponseError(errors.New("json数据解析失败"))  
72 - return  
73 - }  
74 - if param.Id == 0 {  
75 - err = c.addOrderPurpose(param)  
76 - } else {  
77 - err = c.editOrderPurpose(param)  
78 - }  
79 - if err != nil {  
80 - c.ResponseError(err)  
81 - }  
82 - c.ResponseData(nil)  
83 - return  
84 -}  
85 -  
86 -func (c *OrderController) addOrderPurpose(param orderDetail) error {  
87 - orderSrv := orderService.NewOrderService(nil)  
88 - Createcmd := orderCmd.CreateOrderCommand{  
89 - //订单区域  
90 - OrderRegion: param.OrderDist,  
91 - //订单编号  
92 - OrderCode: param.OrderId,  
93 - //订单名称  
94 - OrderName: param.OrderName,  
95 - //数量  
96 - OrderCount: param.OrderNum,  
97 - OrderActualCount: param.OrderNum,  
98 - //订单金额  
99 - OrderAmount: param.OrderPrice,  
100 - OrderActualAmount: param.OrderPrice,  
101 - //买家  
102 - BuyerName: param.Buyer,  
103 - //买家电话  
104 - BuyerPhone: param.BuyerPhone,  
105 - //地址  
106 - BuyerAddress: param.Address,  
107 - //合伙人数据  
108 - PartnerId: param.Partner,  
109 - //合伙人分红百分比  
110 - PartnerBonusPercent: param.PartnerRatio,  
111 - //业务员分红百分比  
112 - SalesmanBonusPercent: param.SalesmanRatio,  
113 - //订单类型  
114 - OrderType: domain.OrderIntention,  
115 - OrderStatus: domain.OrderStatusReserve,  
116 - }  
117 - err := orderSrv.CreateOrder(Createcmd)  
118 - return err  
119 -}  
120 -  
121 -func (c *OrderController) editOrderPurpose(param orderDetail) error {  
122 - updateCmd := orderCmd.UpdateOrderCommand{  
123 - Id: param.Id,  
124 - //订单区域  
125 - OrderRegion: param.OrderDist,  
126 - //订单编号  
127 - OrderCode: param.OrderId,  
128 - //订单名称  
129 - OrderName: param.OrderName,  
130 - //数量  
131 - OrderCount: param.OrderNum,  
132 - //订单金额  
133 - OrderAmount: param.OrderPrice,  
134 - //买家电话  
135 - BuyerPhone: param.BuyerPhone,  
136 - //地址  
137 - BuyerAddress: param.Address,  
138 - //合伙人分红百分比  
139 - PartnerBonusPercent: param.PartnerRatio,  
140 - //业务员分红百分比  
141 - SalesmanBonusPercent: param.SalesmanRatio,  
142 -  
143 - OrderStatus: domain.OrderStatusReserve,  
144 -  
145 - OrderType: domain.OrderIntention,  
146 - }  
147 - orderSrv := orderService.NewOrderService(nil)  
148 - err := orderSrv.UpdateOrderPurpose(updateCmd)  
149 - return err  
150 -}  
151 -  
152 -//GetOrderPurpose 获取意向订单  
153 -func (c *OrderController) GetOrderPurpose() {  
154 - type Parameter struct {  
155 - Id string `json:"id"`  
156 - }  
157 - var (  
158 - param Parameter  
159 - err error  
160 - )  
161 - if err = c.BindJsonData(&param); err != nil {  
162 - logs.Error(err)  
163 - c.ResponseError(errors.New("json数据解析失败"))  
164 - return  
165 - }  
166 - orderid, _ := strconv.ParseInt(param.Id, 10, 64)  
167 - orderSrv := orderService.NewOrderService(nil)  
168 - orderinfo, err := orderSrv.GetOrder(orderQuery.GetOrderQuery{  
169 - OrderId: orderid,  
170 - })  
171 - if err != nil {  
172 - c.ResponseError(err)  
173 - return  
174 - }  
175 - rsp := map[string]interface{}{  
176 - "createTime": orderinfo.CreateAt.Local().Format("2006-01-02 15:04:05"),  
177 - "updateTime": orderinfo.UpdateAt.Local().Format("2006-01-02 15:04:05"),  
178 - "buyer": orderinfo.Buyer.BuyerName,  
179 - "buyerPhone": orderinfo.Buyer.ContactInfo,  
180 - "address": orderinfo.Buyer.ShippingAddress,  
181 - "id": orderinfo.Id,  
182 - "partner": orderinfo.PartnerInfo.Id,  
183 - "partnerRatio": orderinfo.PartnerBonusPercent,  
184 - "salesmanRatio": orderinfo.SalesmanBonusPercent,  
185 - "orderId": orderinfo.OrderCode,  
186 - "orderName": orderinfo.OrderName,  
187 - "orderNum": orderinfo.OrderCount,  
188 - "orderPrice": orderinfo.OrderAmount,  
189 - "orderDist": orderinfo.OrderRegionInfo.RegionName,  
190 - "orderStatue": orderinfo.OrderStatus,  
191 - }  
192 - c.ResponseData(rsp)  
193 -}  
194 -  
195 -//PageListOrderPurpose 分页获取意向订单列表  
196 -func (c *OrderController) PageListOrderPurpose() {  
197 - type Parameter struct {  
198 - SearchText string `json:"searchText"`  
199 - Partner int64 `json:"partner"`  
200 - PageSize int `json:"pageSize"`  
201 - PageNumber int `json:"pageNumber"`  
202 - }  
203 - var (  
204 - param Parameter  
205 - err error  
206 - )  
207 - if err = c.BindJsonData(&param); err != nil {  
208 - logs.Error(err)  
209 - c.ResponseError(errors.New("json数据解析失败"))  
210 - return  
211 - }  
212 - if param.PageNumber == 0 {  
213 - param.PageNumber = 1  
214 - }  
215 - if param.PageSize == 0 {  
216 - param.PageSize = 20  
217 - }  
218 -  
219 - orderSrv := orderService.NewOrderService(nil)  
220 - orderinfos, cnt, err := orderSrv.PageListOrder(orderQuery.ListOrderQuery{  
221 - PartnerId: param.Partner,  
222 - OrderCode: param.SearchText,  
223 - OrderType: domain.OrderIntention,  
224 - Limit: param.PageSize,  
225 - Offset: (param.PageNumber - 1) * param.PageSize,  
226 - })  
227 - if err != nil {  
228 - c.ResponseError(err)  
229 - return  
230 - }  
231 - rsp := []map[string]interface{}{}  
232 - for i := range orderinfos {  
233 - orderinfo := orderinfos[i]  
234 - m := map[string]interface{}{  
235 - "createTime": orderinfo.CreateAt.Local().Format("2006-01-02 15:04:05"),  
236 - "updateTime": orderinfo.UpdateAt.Local().Format("2006-01-02 15:04:05"),  
237 - "buyer": orderinfo.Buyer.BuyerName,  
238 - "id": orderinfo.Id,  
239 - "orderId": orderinfo.OrderCode,  
240 - "partner": orderinfo.PartnerInfo.PartnerName,  
241 - "partnerRatio": orderinfo.PartnerBonusPercent,  
242 - "orderName": orderinfo.OrderName,  
243 - "orderNum": orderinfo.OrderCount,  
244 - "orderPrice": orderinfo.OrderAmount,  
245 - "orderDist": orderinfo.OrderRegionInfo.RegionName,  
246 - "orderStatue": orderinfo.OrderStatus,  
247 - }  
248 - rsp = append(rsp, m)  
249 - }  
250 - c.ResponsePageList(rsp, cnt, param.PageNumber)  
251 -}  
252 -  
253 -//RemoveOrderPurpose 删除意向订单  
254 -func (c *OrderController) RemoveOrderPurpose() {  
255 - type Parameter struct {  
256 - Id int64 `json:"id"`  
257 - }  
258 - var (  
259 - param Parameter  
260 - err error  
261 - )  
262 - if err = c.BindJsonData(&param); err != nil {  
263 - logs.Error(err)  
264 - c.ResponseError(errors.New("json数据解析失败"))  
265 - return  
266 - }  
267 - orderSrv := orderService.NewOrderService(nil)  
268 - err = orderSrv.RemoveOrder(param.Id)  
269 - if err != nil {  
270 - c.ResponseError(err)  
271 - return  
272 - }  
273 - c.ResponseData(nil)  
274 -}  
275 -  
276 -//PageListOrderReal 分页获取实发订单列表  
277 -func (c *OrderController) PageListOrderReal() {  
278 - type Parameter struct {  
279 - SearchText string `json:"searchText"`  
280 - Partner int64 `json:"partner"`  
281 - PageSize int `json:"pageSize"`  
282 - PageNumber int `json:"pageNumber"`  
283 - }  
284 - var (  
285 - param Parameter  
286 - err error  
287 - )  
288 - if err = c.BindJsonData(&param); err != nil {  
289 - logs.Error(err)  
290 - c.ResponseError(errors.New("json数据解析失败"))  
291 - return  
292 - }  
293 - if param.PageNumber == 0 {  
294 - param.PageNumber = 1  
295 - }  
296 - if param.PageSize == 0 {  
297 - param.PageSize = 20  
298 - }  
299 -  
300 - orderSrv := orderService.NewOrderService(nil)  
301 - orderinfos, cnt, err := orderSrv.PageListOrder(orderQuery.ListOrderQuery{  
302 - PartnerId: param.Partner,  
303 - OrderCode: param.SearchText,  
304 - OrderType: domain.OrderReal,  
305 - Limit: param.PageSize,  
306 - Offset: (param.PageNumber - 1) * param.PageSize,  
307 - })  
308 - if err != nil {  
309 - c.ResponseError(err)  
310 - return  
311 - }  
312 - rsp := []map[string]interface{}{}  
313 - for i := range orderinfos {  
314 - orderinfo := orderinfos[i]  
315 - m := map[string]interface{}{  
316 - "createTime": orderinfo.CreateAt.Local().Format("2006-01-02 15:04:05"),  
317 - "updateTime": orderinfo.UpdateAt.Local().Format("2006-01-02 15:04:05"),  
318 - "buyer": orderinfo.Buyer.BuyerName,  
319 - "id": orderinfo.Id,  
320 - "orderId": orderinfo.OrderCode,  
321 - "partner": orderinfo.PartnerInfo.PartnerName,  
322 - "partnerRatio": orderinfo.PartnerBonusPercent,  
323 - "orderName": orderinfo.OrderName,  
324 - "orderNum": orderinfo.OrderCount,  
325 - "orderPrice": orderinfo.OrderAmount,  
326 - "orderActualNum": orderinfo.OrderActualCount,  
327 - "orderActualPrice": orderinfo.OrderActualAmount,  
328 - "orderDist": orderinfo.OrderRegionInfo.RegionName,  
329 - "orderStatue": orderinfo.OrderStatus,  
330 - }  
331 - rsp = append(rsp, m)  
332 - }  
333 - c.ResponsePageList(rsp, cnt, param.PageNumber)  
334 -}  
335 -  
336 -//UpdateOrderPurpose 意向订单转实发单  
337 -func (c *OrderController) OrderPurposeToReal() {  
338 - //用与适配前端定义的数据结构  
339 - var (  
340 - param orderDetail  
341 - err error  
342 - )  
343 - if err = c.BindJsonData(&param); err != nil {  
344 - logs.Error(err)  
345 - c.ResponseError(errors.New("json数据解析失败"))  
346 - return  
347 - }  
348 - orderSrv := orderService.NewOrderService(nil)  
349 - if param.Id > 0 {  
350 - err = orderSrv.UpdateOrderPurpose(orderCmd.UpdateOrderCommand{  
351 - Id: param.Id,  
352 - OrderCode: param.OrderId,  
353 - OrderName: param.OrderName,  
354 - OrderCount: param.OrderNum,  
355 - OrderAmount: param.OrderPrice,  
356 - BuyerPhone: param.BuyerPhone,  
357 - BuyerAddress: param.Address,  
358 - OrderRegion: param.OrderDist,  
359 - PartnerBonusPercent: param.PartnerRatio,  
360 - SalesmanBonusPercent: param.SalesmanRatio,  
361 - OrderStatus: param.OrderStatue,  
362 - OrderType: domain.OrderReal,  
363 - })  
364 - } else {  
365 - err = c.addOrderReal(param)  
366 - }  
367 - if err != nil {  
368 - c.ResponseError(err)  
369 - return  
370 - }  
371 - c.ResponseData(nil)  
372 - return  
373 -}  
374 -  
375 -//GetOrderReal 获取实发单详情  
376 -func (c *OrderController) GetOrderReal() {  
377 - type Parameter struct {  
378 - Id string `json:"id"`  
379 - }  
380 - var (  
381 - param Parameter  
382 - err error  
383 - )  
384 - if err = c.BindJsonData(&param); err != nil {  
385 - logs.Error(err)  
386 - c.ResponseError(errors.New("json数据解析失败"))  
387 - return  
388 - }  
389 - orderid, _ := strconv.ParseInt(param.Id, 10, 64)  
390 - orderSrv := orderService.NewOrderService(nil)  
391 - orderinfo, err := orderSrv.GetOrder(orderQuery.GetOrderQuery{  
392 - OrderId: orderid,  
393 - })  
394 - if err != nil {  
395 - c.ResponseError(err)  
396 - return  
397 - }  
398 - rsp := map[string]interface{}{  
399 - "createTime": orderinfo.CreateAt.Local().Format("2006-01-02 15:04:05"),  
400 - "updateTime": orderinfo.UpdateAt.Local().Format("2006-01-02 15:04:05"),  
401 - "buyer": orderinfo.Buyer.BuyerName,  
402 - "buyerPhone": orderinfo.Buyer.ContactInfo,  
403 - "address": orderinfo.Buyer.ShippingAddress,  
404 - "id": orderinfo.Id,  
405 - "partner": orderinfo.PartnerInfo.Id,  
406 - "partnerRatio": orderinfo.PartnerBonusPercent,  
407 - "salesmanRatio": orderinfo.SalesmanBonusPercent,  
408 - "orderId": orderinfo.OrderCode,  
409 - "orderName": orderinfo.OrderName,  
410 - "orderNum": orderinfo.OrderActualCount,  
411 - "orderPrice": orderinfo.OrderActualAmount,  
412 - "orderDist": orderinfo.OrderRegionInfo.RegionName,  
413 - "orderStatue": orderinfo.OrderStatus,  
414 - "reason": orderinfo.Reason,  
415 - }  
416 - c.ResponseData(rsp)  
417 -}  
418 -  
419 -//UpdateOrderReal 更新实发订单数据  
420 -func (c *OrderController) UpdateOrderReal() {  
421 - //用与适配前端定义的数据结构  
422 - var (  
423 - param orderDetail  
424 - err error  
425 - )  
426 - if err = c.BindJsonData(&param); err != nil {  
427 - logs.Error(err)  
428 - c.ResponseError(errors.New("json数据解析失败"))  
429 - return  
430 - }  
431 - if param.Id == 0 {  
432 - err = c.addOrderReal(param)  
433 - } else {  
434 - err = c.editOrderReal(param)  
435 - }  
436 - if err != nil {  
437 - c.ResponseError(err)  
438 - }  
439 - c.ResponseData(nil)  
440 - return  
441 -}  
442 -  
443 -//addOrderReal 添加实发订单  
444 -func (c *OrderController) addOrderReal(param orderDetail) error {  
445 - orderSrv := orderService.NewOrderService(nil)  
446 - if !(param.OrderStatue == domain.OrderStatusDeliverAll ||  
447 - param.OrderStatue == domain.OrderStatusDeliverSome) {  
448 - return lib.ThrowError(lib.ARG_ERROR, "订单状态错误")  
449 - }  
450 - Createcmd := orderCmd.CreateOrderCommand{  
451 - //订单区域  
452 - OrderRegion: param.OrderDist,  
453 - //订单编号  
454 - OrderCode: param.OrderId,  
455 - //订单名称  
456 - OrderName: param.OrderName,  
457 - //数量  
458 - OrderCount: param.OrderNum,  
459 - OrderActualCount: param.OrderNum,  
460 - //订单金额  
461 - OrderAmount: param.OrderPrice,  
462 - OrderActualAmount: param.OrderPrice,  
463 - //买家  
464 - BuyerName: param.Buyer,  
465 - //买家电话  
466 - BuyerPhone: param.BuyerPhone,  
467 - //地址  
468 - BuyerAddress: param.Address,  
469 - //合伙人数据  
470 - PartnerId: param.Partner,  
471 - //合伙人分红百分比  
472 - PartnerBonusPercent: param.PartnerRatio,  
473 - //业务员分红百分比  
474 - SalesmanBonusPercent: param.SalesmanRatio,  
475 - //订单类型  
476 - OrderType: domain.OrderReal,  
477 - //状态  
478 - OrderStatus: param.OrderStatue,  
479 - }  
480 - err := orderSrv.CreateOrder(Createcmd)  
481 - return err  
482 -}  
483 -  
484 -//editOrderReal 更新实发订单  
485 -func (c *OrderController) editOrderReal(param orderDetail) error {  
486 - updateCmd := orderCmd.UpdateOrderRealCommand{  
487 - Id: param.Id,  
488 - //订单区域  
489 - OrderRegion: param.OrderDist,  
490 - //订单编号  
491 - OrderCode: param.OrderId,  
492 - //订单名称  
493 - OrderName: param.OrderName,  
494 - //数量  
495 - OrderActualCount: param.OrderNum,  
496 - //订单金额  
497 - OrderActualAmount: param.OrderPrice,  
498 - //买家电话  
499 - BuyerPhone: param.BuyerPhone,  
500 - //地址  
501 - BuyerAddress: param.Address,  
502 - //合伙人分红百分比  
503 - PartnerBonusPercent: param.PartnerRatio,  
504 - //业务员分红百分比  
505 - SalesmanBonusPercent: param.SalesmanRatio,  
506 - //状态  
507 - OrderStatus: param.OrderStatue,  
508 - //理由  
509 - Reason: param.Reason,  
510 - }  
511 - orderSrv := orderService.NewOrderService(nil)  
512 - err := orderSrv.UpdateOrderReal(updateCmd)  
513 - return err  
514 -}  
  1 +package controllers
  2 +
  3 +import (
  4 + "errors"
  5 + "fmt"
  6 + "strconv"
  7 +
  8 + "github.com/astaxie/beego/logs"
  9 + orderCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/command"
  10 + orderQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/query"
  11 + orderService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/service"
  12 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
  13 +)
  14 +
  15 +//OrderDividendController 订单分红管理
  16 +type OrderDividendController struct {
  17 + BaseController
  18 +}
  19 +
  20 +//Prepare 重写 BaseController 的Prepare方法
  21 +func (c *OrderDividendController) Prepare() {
  22 + c.BaseController.Prepare()
  23 + if ok := c.ValidJWTToken(); !ok {
  24 + return
  25 + }
  26 + if ok := c.ValidAdminPermission(domain.PERMISSION_DIVIDEND); !ok {
  27 + return
  28 + }
  29 +}
  30 +
  31 +//PageListOrderDividend 获取实发订单分红列表
  32 +func (c *OrderDividendController) PageListOrderDividend() {
  33 + type Parameter struct {
  34 + SearchText string `json:"searchText"`
  35 + Partner int64 `json:"partner"`
  36 + PageSize int `json:"pageSize"`
  37 + PageNumber int `json:"pageNumber"`
  38 + }
  39 + var (
  40 + param Parameter
  41 + err error
  42 + )
  43 + if err = c.BindJsonData(&param); err != nil {
  44 + logs.Error(err)
  45 + c.ResponseError(errors.New("json数据解析失败"))
  46 + return
  47 + }
  48 + if param.PageNumber == 0 {
  49 + param.PageNumber = 1
  50 + }
  51 + if param.PageSize == 0 {
  52 + param.PageSize = 20
  53 + }
  54 +
  55 + orderSrv := orderService.NewOrderInfoService(nil)
  56 + orderinfos, cnt, err := orderSrv.PageListOrderBase(orderQuery.ListOrderBaseQuery{
  57 + PartnerId: param.Partner,
  58 + DeliveryCode: param.SearchText,
  59 + OrderType: domain.OrderReal,
  60 + Limit: param.PageSize,
  61 + Offset: (param.PageNumber - 1) * param.PageSize,
  62 + })
  63 + if err != nil {
  64 + c.ResponseError(err)
  65 + return
  66 + }
  67 + rsp := []map[string]interface{}{}
  68 + for i := range orderinfos {
  69 + orderinfo := orderinfos[i]
  70 + m := map[string]interface{}{
  71 + "updateTime": orderinfo.UpdateTime.Local().Format("2006-01-02 15:04:05"),
  72 + "id": orderinfo.Id,
  73 + "shipmentsId": orderinfo.DeliveryCode,
  74 + "partner": orderinfo.PartnerInfo.PartnerName,
  75 + "dividendsReceivable": orderinfo.OrderCompute.PlanPartnerBonus,
  76 + "dividendSpending": orderinfo.OrderCompute.PartnerBonusExpense,
  77 + "receiveDividends": orderinfo.OrderCompute.PartnerBonusHas,
  78 + "uncollectedDividends": orderinfo.OrderCompute.PartnerBonusNot,
  79 + "stateOfPayment": orderinfo.BonusStatus,
  80 + }
  81 + if orderinfo.OrderCompute.UsePartnerBonus >= 0 {
  82 + m["dividendsReceivable"] = orderinfo.OrderCompute.UsePartnerBonus
  83 + }
  84 + rsp = append(rsp, m)
  85 + }
  86 + c.ResponsePageList(rsp, cnt, param.PageNumber)
  87 + return
  88 +}
  89 +
  90 +//OrderDividendDetail 订单的分红详情
  91 +func (c *OrderDividendController) OrderDividendDetail() {
  92 + type Parameter struct {
  93 + Id string `json:"id"`
  94 + }
  95 + var (
  96 + param Parameter
  97 + err error
  98 + )
  99 + if err = c.BindJsonData(&param); err != nil {
  100 + logs.Error(err)
  101 + c.ResponseError(errors.New("json数据解析失败"))
  102 + return
  103 + }
  104 + orderid, _ := strconv.ParseInt(param.Id, 10, 64)
  105 + if orderid == 0 {
  106 + c.ResponseError(errors.New("参数错误"))
  107 + return
  108 + }
  109 + orderSrv := orderService.NewOrderInfoService(nil)
  110 + orderinfo, err := orderSrv.GetOrderDetail(orderQuery.GetOrderQuery{
  111 + OrderId: orderid,
  112 + })
  113 + if err != nil {
  114 + c.ResponseError(err)
  115 + return
  116 + }
  117 + if orderinfo.OrderType != domain.OrderReal {
  118 + c.ResponseError(errors.New("参数错误"))
  119 + return
  120 + }
  121 + allGoods := []map[string]interface{}{}
  122 + for _, v := range orderinfo.Goods {
  123 + detail := map[string]interface{}{
  124 + "productName": v.GoodName,
  125 + "orderNum": v.PlanGoodNumber,
  126 + "univalence": v.Price,
  127 + "partnerRatio": v.PartnerBonusPercent,
  128 + "orderPrice": v.GoodCompute.PlanAmount,
  129 + "partnerDividendsReceivable": v.GoodCompute.PlanPartnerBonus,
  130 + }
  131 + m := map[string]interface{}{
  132 + "detail": detail,
  133 + "quantityControl": "",
  134 + "id": v.Id,
  135 + "stateOfPayment": v.BonusStatus,
  136 + "remark": v.Remark,
  137 + "amountControl": "",
  138 + "partnerDividendControl": "",
  139 + "receivedDividends": v.GoodCompute.PartnerBonusHas,
  140 + "notReceivedDividend": v.GoodCompute.PartnerBonusNot,
  141 + "dividendSpending": v.GoodCompute.PartnerBonusExpense,
  142 + }
  143 + if v.GoodCompute.UseAmount >= 0 {
  144 + m["amountControl"] = fmt.Sprint(v.GoodCompute.UseAmount)
  145 + }
  146 + if v.UseGoodNumber >= 0 {
  147 + m["quantityControl"] = fmt.Sprint(v.UseGoodNumber)
  148 + }
  149 + if v.GoodCompute.UsePartnerBonus >= 0 {
  150 + m["partnerDividendControl"] = fmt.Sprint(v.GoodCompute.UsePartnerBonus)
  151 + }
  152 + allGoods = append(allGoods, m)
  153 + }
  154 + orderData := map[string]interface{}{
  155 + "buyer": orderinfo.Buyer.BuyerName,
  156 + "shipmentsId": orderinfo.DeliveryCode,
  157 + "orderDist": orderinfo.RegionInfo.RegionName,
  158 + "partner": orderinfo.PartnerInfo.PartnerName,
  159 + "id": orderinfo.Id,
  160 + "orderId": orderinfo.OrderCode,
  161 + }
  162 + dividendCount := map[string]interface{}{
  163 + "orderNum": orderinfo.OrderCompute.PlanOrderCount,
  164 + "orderAmountAdjustment": orderinfo.OrderCompute.PlanOrderAmount,
  165 + "orderNumControl": "",
  166 + "orderAmountAdjustmentControl": "",
  167 + "partnerDividends": orderinfo.OrderCompute.PlanPartnerBonus,
  168 + "partnerDividendsControl": "",
  169 + "receivedDividends": orderinfo.OrderCompute.PartnerBonusHas,
  170 + "notReceivedDividend": orderinfo.OrderCompute.PartnerBonusNot,
  171 + "dividendSpending": orderinfo.OrderCompute.PartnerBonusExpense,
  172 + "commissionProportion": orderinfo.SalesmanBonusPercent,
  173 + "expectedCommission": orderinfo.OrderCompute.SalesmanBonus,
  174 + }
  175 + if orderinfo.OrderCompute.UseOrderAmount >= 0 {
  176 + dividendCount["orderAmountAdjustmentControl"] = fmt.Sprint(orderinfo.OrderCompute.UseOrderAmount)
  177 + }
  178 + if orderinfo.OrderCompute.UsePartnerBonus >= 0 {
  179 + dividendCount["partnerDividendsControl"] = fmt.Sprint(orderinfo.OrderCompute.UsePartnerBonus)
  180 + }
  181 + if orderinfo.OrderCompute.UseOrderCount >= 0 {
  182 + dividendCount["orderNumControl"] = fmt.Sprint(orderinfo.OrderCompute.UseOrderCount)
  183 + }
  184 + rsp := map[string]interface{}{
  185 + "order": orderData,
  186 + "product": allGoods,
  187 + "dividendCount": dividendCount,
  188 + "operationTime": orderinfo.UpdateTime.Local().Format("2006-01-02 15:04:06"),
  189 + }
  190 + if orderinfo.OrderCompute.UseOrderAmount >= 0 {
  191 + rsp["orderNumCountControl"] = fmt.Sprint(orderinfo.OrderCompute.UseOrderAmount)
  192 + }
  193 + if orderinfo.OrderCompute.UseOrderCount >= 0 {
  194 + rsp["orderAmountAdjustmentCountControl"] = fmt.Sprint(orderinfo.OrderCompute.UseOrderCount)
  195 + }
  196 + c.ResponseData(rsp)
  197 + return
  198 +}
  199 +
  200 +type postOrderDividend struct {
  201 + Id int64 `json:"id"`
  202 + DividendPayments []postDividendPayment `json:"dividendPayment"`
  203 +}
  204 +
  205 +type postDividendPayment struct {
  206 + QuantityControl string `json:"quantityControl"`
  207 + StateOfPayment int `json:"stateOfPayment"`
  208 + ProductId int64 `json:"productId"`
  209 + Remark string `json:"remark"`
  210 +}
  211 +
  212 +func (c *OrderDividendController) EditOrderDividend() {
  213 + var (
  214 + param postOrderDividend
  215 + err error
  216 + )
  217 + if err = c.BindJsonData(&param); err != nil {
  218 + logs.Error(err)
  219 + c.ResponseError(errors.New("json数据解析失败"))
  220 + return
  221 + }
  222 +
  223 + if param.Id == 0 {
  224 + c.ResponseError(errors.New("参数错误"))
  225 + return
  226 + }
  227 + goodbouns := []orderCmd.GoodBouns{}
  228 + for _, v := range param.DividendPayments {
  229 + if v.ProductId == 0 {
  230 + c.ResponseError(errors.New("参数错误"))
  231 + return
  232 + }
  233 + if !(v.StateOfPayment == domain.OrderGoodWaitPay || v.StateOfPayment == domain.OrderGoodHasPay) {
  234 + c.ResponseError(errors.New("参数错误"))
  235 + return
  236 + }
  237 + g := orderCmd.GoodBouns{
  238 + GoodId: v.ProductId,
  239 + Remark: v.Remark,
  240 + BounsStatus: v.StateOfPayment,
  241 + }
  242 + if len(v.QuantityControl) == 0 {
  243 + g.UseGoodNumber = -1
  244 + } else {
  245 + num, err := strconv.Atoi(v.QuantityControl)
  246 + if err != nil {
  247 + c.ResponseError(errors.New("参数错误"))
  248 + return
  249 + }
  250 + g.UseGoodNumber = num
  251 + }
  252 + goodbouns = append(goodbouns, g)
  253 + }
  254 + cmd := orderCmd.UpdateGoodBouns{
  255 + Id: param.Id,
  256 + GoodBouns: goodbouns,
  257 + }
  258 + orderSrv := orderService.NewOrderInfoService(nil)
  259 + err = orderSrv.UpdateGoodBouns(cmd)
  260 + if err != nil {
  261 + c.ResponseError(err)
  262 + return
  263 + }
  264 + c.ResponseData(nil)
  265 + return
  266 +}