作者 yangfu

分红编辑/详情修改

@@ -20,9 +20,9 @@ require ( @@ -20,9 +20,9 @@ require (
20 github.com/moul/http2curl v1.0.0 // indirect 20 github.com/moul/http2curl v1.0.0 // indirect
21 github.com/onsi/ginkgo v1.13.0 21 github.com/onsi/ginkgo v1.13.0
22 github.com/onsi/gomega v1.10.1 22 github.com/onsi/gomega v1.10.1
23 - github.com/prometheus/common v0.10.0 // indirect  
24 github.com/sergi/go-diff v1.1.0 // indirect 23 github.com/sergi/go-diff v1.1.0 // indirect
25 github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect 24 github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect
  25 + github.com/shopspring/decimal v1.2.0
26 github.com/smartystreets/goconvey v1.6.4 // indirect 26 github.com/smartystreets/goconvey v1.6.4 // indirect
27 github.com/valyala/fasthttp v1.14.0 // indirect 27 github.com/valyala/fasthttp v1.14.0 // indirect
28 github.com/xeipuuv/gojsonschema v1.2.0 // indirect 28 github.com/xeipuuv/gojsonschema v1.2.0 // indirect
@@ -20,3 +20,11 @@ func CreatePartnerInfoDao(options map[string]interface{}) (*dao.PartnerInfoDao, @@ -20,3 +20,11 @@ func CreatePartnerInfoDao(options map[string]interface{}) (*dao.PartnerInfoDao,
20 } 20 }
21 return dao.NewPartnerInfoDao(transactionContext) 21 return dao.NewPartnerInfoDao(transactionContext)
22 } 22 }
  23 +
  24 +func CreateOrderDao(options map[string]interface{}) (*dao.OrderDao, error) {
  25 + var transactionContext *transaction.TransactionContext
  26 + if value, ok := options["transactionContext"]; ok {
  27 + transactionContext = value.(*transaction.TransactionContext)
  28 + }
  29 + return dao.NewOrderDao(transactionContext)
  30 +}
@@ -2,7 +2,12 @@ package command @@ -2,7 +2,12 @@ package command
2 2
3 type CreateOrderPaymentCommand struct { 3 type CreateOrderPaymentCommand struct {
4 //订单编号 4 //订单编号
5 - OrderId int64 `json:"orderId"` 5 + OrderId int64 `json:"orderId"`
  6 + DivdendPaymentItem []DivdendPyamentItem `json:"dividendPayment"`
  7 + TotalPaymentAmount float64 `json:"payment_amount"`
  8 +}
  9 +
  10 +type DivdendPyamentItem struct {
6 // 货款 11 // 货款
7 PaymentForGoods float64 `json:"paymentForGoods,omitempty"` 12 PaymentForGoods float64 `json:"paymentForGoods,omitempty"`
8 // 支付状态 13 // 支付状态
@@ -25,6 +25,7 @@ func NewOrderPaymentService(options map[string]interface{}) *OrderPaymentService @@ -25,6 +25,7 @@ func NewOrderPaymentService(options map[string]interface{}) *OrderPaymentService
25 func (OrderPaymentService *OrderPaymentService) CreateOrderPayment(command *command.CreateOrderPaymentCommand) (data interface{}, err error) { 25 func (OrderPaymentService *OrderPaymentService) CreateOrderPayment(command *command.CreateOrderPaymentCommand) (data interface{}, err error) {
26 var ( 26 var (
27 transactionContext, _ = factory.CreateTransactionContext(nil) 27 transactionContext, _ = factory.CreateTransactionContext(nil)
  28 + OrderDao, _ = factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})
28 ) 29 )
29 if err = command.ValidateCommand(); err != nil { 30 if err = command.ValidateCommand(); err != nil {
30 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 31 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
@@ -33,7 +34,12 @@ func (OrderPaymentService *OrderPaymentService) CreateOrderPayment(command *comm @@ -33,7 +34,12 @@ func (OrderPaymentService *OrderPaymentService) CreateOrderPayment(command *comm
33 return nil, err 34 return nil, err
34 } 35 }
35 defer func() { 36 defer func() {
36 - transactionContext.RollbackTransaction() 37 + if err == nil {
  38 + err = transactionContext.CommitTransaction()
  39 + }
  40 + if err != nil {
  41 + transactionContext.RollbackTransaction()
  42 + }
37 }() 43 }()
38 //检查订单是否存在 44 //检查订单是否存在
39 45
@@ -43,34 +49,48 @@ func (OrderPaymentService *OrderPaymentService) CreateOrderPayment(command *comm @@ -43,34 +49,48 @@ func (OrderPaymentService *OrderPaymentService) CreateOrderPayment(command *comm
43 }); err != nil { 49 }); err != nil {
44 return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) 50 return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
45 } 51 }
46 -  
47 - dm := &domain.OrderPayment{  
48 - OrderId: command.OrderId,  
49 - PartnerId: 0,  
50 - PaymentAmount: 0,  
51 - BonusAmount: 0, //计算分红金额  
52 - BonusStatus: 0,  
53 - CreateAt: time.Now(),  
54 - PaymentSn: command.PaymentSn,  
55 - UpdateAt: time.Now(), 52 + orderBase, e := OrderDao.GetOrderBaseInfo(command.OrderId)
  53 + if e != nil {
  54 + err = e
  55 + return
56 } 56 }
57 57
58 - //检查货款 已存在 / 未存在  
59 - if findDm, e := OrderPaymentRepository.FindOne(domain.OrderPaymentFindOneQuery{OrderId: command.OrderId, PaymentSn: command.PaymentSn}); e == nil {  
60 - if dm.BonusStatus == domain.BonusPaid {  
61 - return 58 + for i := range command.DivdendPaymentItem {
  59 + paymentItem := command.DivdendPaymentItem[i]
  60 + dm := &domain.OrderPayment{
  61 + OrderId: command.OrderId,
  62 + PartnerId: 0,
  63 + PaymentAmount: 0,
  64 + BonusAmount: 0, //计算分红金额
  65 + BonusStatus: 0,
  66 + CreateAt: time.Now(),
  67 + PaymentSn: paymentItem.PaymentSn,
  68 + UpdateAt: time.Now(),
62 } 69 }
63 - dm = findDm  
64 - }  
65 70
66 - dm.PaymentAmount = command.PaymentForGoods  
67 - dm.BonusStatus = command.StateOfPayment  
68 - dm.BonusAmount = 0 71 + //检查货款 已存在 / 未存在
  72 + if findDm, e := OrderPaymentRepository.FindOne(domain.OrderPaymentFindOneQuery{OrderId: command.OrderId, PaymentSn: paymentItem.PaymentSn}); e == nil {
  73 + if findDm.BonusStatus == domain.BonusPaid {
  74 + continue
  75 + }
  76 + dm = findDm
  77 + }
69 78
70 - if data, err = OrderPaymentRepository.Save(dm); err != nil {  
71 - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) 79 + dm.PartnerId = orderBase["PartnerId"].(int64)
  80 + bonousPercent := orderBase["PartnerBonusPercent"].(float64)
  81 + dm.BonusAmount = paymentItem.PaymentForGoods * (bonousPercent / 100.0)
  82 + dm.PaymentAmount = paymentItem.PaymentForGoods
  83 + dm.BonusStatus = paymentItem.StateOfPayment
  84 +
  85 + if data, err = OrderPaymentRepository.Save(dm); err != nil {
  86 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  87 + }
72 } 88 }
73 - err = transactionContext.CommitTransaction() 89 +
  90 + if err = OrderDao.Update(map[string]interface{}{"id": command.OrderId, "orderPaymentAmount": command.TotalPaymentAmount}); err != nil {
  91 + return
  92 + }
  93 +
74 return 94 return
75 } 95 }
76 96
  1 +package dao
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
  6 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
  7 +)
  8 +
  9 +type OrderDao struct {
  10 + transactionContext *transaction.TransactionContext
  11 +}
  12 +
  13 +func (dao *OrderDao) Update(options map[string]interface{}) (err error) {
  14 + tx := dao.transactionContext.PgTx
  15 + order := new(models.Order)
  16 + q := tx.Model(order)
  17 + if v, ok := options["orderPaymentAmount"]; ok {
  18 + q.Set("order_payment_amount = ?", v)
  19 + }
  20 + if v, ok := options["id"]; ok {
  21 + q.Where("id = ?", v)
  22 + }
  23 + _, err = q.Update()
  24 + return
  25 +}
  26 +
  27 +func (dao *OrderDao) GetOrderBaseInfo(id int64) (data map[string]interface{}, err error) {
  28 + tx := dao.transactionContext.PgTx
  29 + order := new(models.Order)
  30 +
  31 + data = make(map[string]interface{})
  32 + q := tx.Model(order)
  33 + q.Column("partner_id", "partner_bonus_percent", "order_payment_amount", "buyer")
  34 + q.Where("id = ?", id)
  35 + err = q.Select()
  36 + if err == nil {
  37 + data["PartnerId"] = order.PartnerId
  38 + data["PartnerBonusPercent"] = order.PartnerBonusPercent
  39 + data["OrderPaymentAmount"] = order.OrderPaymentAmount
  40 + data["Buyer"] = order.Buyer
  41 + }
  42 + return
  43 +}
  44 +
  45 +func NewOrderDao(transactionContext *transaction.TransactionContext) (*OrderDao, error) {
  46 + if transactionContext == nil {
  47 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  48 + } else {
  49 + return &OrderDao{
  50 + transactionContext: transactionContext,
  51 + }, nil
  52 + }
  53 +}
  1 +package models
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
  5 + "time"
  6 +)
  7 +
  8 +type Order struct {
  9 + tableName struct{} `pg:"order"`
  10 + //id
  11 + Id int64 `pg:",pk"`
  12 + //订单类型 1:实际订单 2:意向订单
  13 + OrderType int `pg:",notnull,default:1"`
  14 + //订单编号
  15 + OrderCode string `pg:",notnull`
  16 + //订单名称
  17 + OrderName string
  18 + //订单状态
  19 + OrderStatus int `pg:",notnull,default:1"`
  20 + //订单数量
  21 + OrderCount int
  22 + //实际订单数量
  23 + OrderActualCount int
  24 + //订单金额
  25 + OrderAmount float64
  26 + //订单实际金额
  27 + OrderActualAmount float64
  28 + //订单已支付金额(货款)
  29 + OrderPaymentAmount float64
  30 + //订单区域信息
  31 + OrderRegionInfo *domain.RegionInfo
  32 +
  33 + Buyer *domain.Buyer
  34 + //合伙人编号
  35 + PartnerId int64
  36 + //合伙人分红百分比
  37 + PartnerBonusPercent float64
  38 + //业务员分红百分比
  39 + SalesmanBonusPercent float64
  40 +
  41 + //创建时间
  42 + CreateAt time.Time
  43 + //更新时间
  44 + UpdateAt time.Time
  45 +
  46 + //上一次查看时间 已读情况
  47 + LastViewTime time.Time
  48 +}
@@ -24,6 +24,7 @@ func (repository *OrderPaymentRepository) Save(dm *domain.OrderPayment) (*domain @@ -24,6 +24,7 @@ func (repository *OrderPaymentRepository) Save(dm *domain.OrderPayment) (*domain
24 BonusAmount: dm.BonusAmount, 24 BonusAmount: dm.BonusAmount,
25 BonusStatus: dm.BonusStatus, 25 BonusStatus: dm.BonusStatus,
26 CreateAt: dm.CreateAt, 26 CreateAt: dm.CreateAt,
  27 + PaymentSn: dm.PaymentSn,
27 UpdateAt: dm.UpdateAt, 28 UpdateAt: dm.UpdateAt,
28 } 29 }
29 if m.Id == 0 { 30 if m.Id == 0 {
@@ -57,8 +58,11 @@ func (repository *OrderPaymentRepository) FindOne(queryOptions domain.OrderPayme @@ -57,8 +58,11 @@ func (repository *OrderPaymentRepository) FindOne(queryOptions domain.OrderPayme
57 OrderPaymentModel := new(models.OrderPayment) 58 OrderPaymentModel := new(models.OrderPayment)
58 query := tx.Model(OrderPaymentModel) 59 query := tx.Model(OrderPaymentModel)
59 60
60 - if queryOptions.Id > 0 {  
61 - query.Where("order_payment.id = ?", "id") 61 + if queryOptions.OrderId > 0 {
  62 + query.Where("order_payment.order_id = ?", queryOptions.OrderId)
  63 + }
  64 + if queryOptions.PaymentSn > 0 {
  65 + query.Where("order_payment.payment_sn = ?", queryOptions.PaymentSn)
62 } 66 }
63 if err := query.First(); err != nil { 67 if err := query.First(); err != nil {
64 return nil, err 68 return nil, err
@@ -73,7 +77,7 @@ func (repository *OrderPaymentRepository) Find(queryOptions domain.OrderPaymentQ @@ -73,7 +77,7 @@ func (repository *OrderPaymentRepository) Find(queryOptions domain.OrderPaymentQ
73 tx := repository.transactionContext.PgTx 77 tx := repository.transactionContext.PgTx
74 var OrderPaymentModels []*models.OrderPayment 78 var OrderPaymentModels []*models.OrderPayment
75 query := tx.Model(&OrderPaymentModels) 79 query := tx.Model(&OrderPaymentModels)
76 - query.Where("order_payment.partner_id = ?", "partnerId") 80 + query.Where("order_payment.order_id = ?", queryOptions.OrderId)
77 var ( 81 var (
78 err error 82 err error
79 rsp = make([]*domain.OrderPayment, 0) 83 rsp = make([]*domain.OrderPayment, 0)
@@ -117,6 +121,7 @@ func (repository *OrderPaymentRepository) transformPgModelToDomainModel(dm *mode @@ -117,6 +121,7 @@ func (repository *OrderPaymentRepository) transformPgModelToDomainModel(dm *mode
117 PaymentAmount: dm.PaymentAmount, 121 PaymentAmount: dm.PaymentAmount,
118 BonusAmount: dm.BonusAmount, 122 BonusAmount: dm.BonusAmount,
119 BonusStatus: dm.BonusStatus, 123 BonusStatus: dm.BonusStatus,
  124 + PaymentSn: dm.PaymentSn,
120 CreateAt: dm.CreateAt, 125 CreateAt: dm.CreateAt,
121 UpdateAt: dm.UpdateAt, 126 UpdateAt: dm.UpdateAt,
122 } 127 }
  1 +package utils
  2 +
  3 +import (
  4 + "github.com/shopspring/decimal"
  5 + "math"
  6 + "math/rand"
  7 + "time"
  8 +)
  9 +
  10 +func decimal1(value float64) float64 {
  11 + return math.Trunc(value*1e1+0.5) * 1e-1
  12 +}
  13 +
  14 +func Round(value float64, places int32) float64 {
  15 + quantity := decimal.NewFromFloat(value)
  16 + d := quantity.Round(places)
  17 + rsp, _ := d.Float64()
  18 + return rsp
  19 +}
  20 +
  21 +func Decimal(value float64) float64 {
  22 + return Round(value, 2)
  23 +}
  24 +
  25 +func DecimalToNumber(value float64) float64 {
  26 + return Round(value, 2)
  27 +}
  28 +
  29 +func GenerateRangeNum(min, max int) int {
  30 + rand.Seed(time.Now().Unix())
  31 + randNum := rand.Intn(max-min) + min
  32 + return randNum
  33 +}
@@ -17,12 +17,12 @@ type DividendsController struct { @@ -17,12 +17,12 @@ type DividendsController struct {
17 ////Prepare 重写 BaseController 的Prepare方法 17 ////Prepare 重写 BaseController 的Prepare方法
18 func (c *DividendsController) Prepare() { 18 func (c *DividendsController) Prepare() {
19 c.BaseController.Prepare() 19 c.BaseController.Prepare()
20 - if ok := c.ValidJWTToken(); !ok {  
21 - return  
22 - }  
23 - if ok := c.ValidAdminPermission(domain.PERMINSSION_PARTNER); !ok {  
24 - return  
25 - } 20 + //if ok := c.ValidJWTToken(); !ok {
  21 + // return
  22 + //}
  23 + //if ok := c.ValidAdminPermission(domain.PERMINSSION_PARTNER); !ok {
  24 + // return
  25 + //}
26 } 26 }
27 27
28 //Edit 编辑分红支付 28 //Edit 编辑分红支付
@@ -37,38 +37,38 @@ func (c *DividendsController) Edit() { @@ -37,38 +37,38 @@ func (c *DividendsController) Edit() {
37 DividendPayment []DividendPaymentItem `json:"dividendPayment"` 37 DividendPayment []DividendPaymentItem `json:"dividendPayment"`
38 } 38 }
39 var ( 39 var (
40 - param Parameter  
41 - err error  
42 - totalPaymentAmount float64 40 + param Parameter
  41 + err error
43 ) 42 )
44 if err = c.BindJsonData(&param); err != nil { 43 if err = c.BindJsonData(&param); err != nil {
45 logs.Error(err) 44 logs.Error(err)
46 c.ResponseError(errors.New("json数据解析失败")) 45 c.ResponseError(errors.New("json数据解析失败"))
47 return 46 return
48 } 47 }
  48 + cmd := OrderPaymentCmd.CreateOrderPaymentCommand{
  49 + OrderId: param.Id,
  50 + DivdendPaymentItem: make([]OrderPaymentCmd.DivdendPyamentItem, 0),
  51 + }
49 52
50 //编辑 53 //编辑
51 for i := range param.DividendPayment { 54 for i := range param.DividendPayment {
52 item := param.DividendPayment[i] 55 item := param.DividendPayment[i]
53 - cmd := OrderPaymentCmd.CreateOrderPaymentCommand{  
54 - OrderId: param.Id,  
55 - PaymentSn: i,  
56 - }  
57 - cmd.PaymentForGoods, _ = strconv.ParseFloat(item.PaymentForGoods, 64)  
58 - cmd.StateOfPayment, _ = strconv.Atoi(item.StateOfPayment)  
59 - serve := OrderPaymentSvr.NewOrderPaymentService(nil)  
60 - _, err = serve.CreateOrderPayment(&cmd)  
61 - if err != nil {  
62 - c.ResponseError(err)  
63 - return  
64 - }  
65 - if cmd.StateOfPayment == domain.BonusPaid {  
66 - totalPaymentAmount += cmd.PaymentForGoods 56 + paymentItem := OrderPaymentCmd.DivdendPyamentItem{}
  57 + paymentItem.PaymentForGoods, _ = strconv.ParseFloat(item.PaymentForGoods, 64)
  58 + paymentItem.StateOfPayment, _ = strconv.Atoi(item.StateOfPayment)
  59 + paymentItem.PaymentSn = i + 1
  60 +
  61 + if paymentItem.StateOfPayment == domain.BonusPaid {
  62 + cmd.TotalPaymentAmount += paymentItem.PaymentForGoods
67 } 63 }
  64 + cmd.DivdendPaymentItem = append(cmd.DivdendPaymentItem, paymentItem)
  65 + }
  66 + serve := OrderPaymentSvr.NewOrderPaymentService(nil)
  67 + _, err = serve.CreateOrderPayment(&cmd)
  68 + if err != nil {
  69 + c.ResponseError(err)
  70 + return
68 } 71 }
69 -  
70 - //保存累计支付金额到订单里面  
71 -  
72 c.ResponseData(nil) 72 c.ResponseData(nil)
73 return 73 return
74 } 74 }
@@ -27,6 +27,10 @@ func init() { @@ -27,6 +27,10 @@ func init() {
27 beego.NSRouter("/detail", &controllers.PartnerInfoController{}, "POST:GetPartnerInfo"), 27 beego.NSRouter("/detail", &controllers.PartnerInfoController{}, "POST:GetPartnerInfo"),
28 beego.NSRouter("/set-status", &controllers.PartnerInfoController{}, "POST:PartnerInfoSetState"), 28 beego.NSRouter("/set-status", &controllers.PartnerInfoController{}, "POST:PartnerInfoSetState"),
29 ), 29 ),
  30 + beego.NSNamespace("/dividends",
  31 + beego.NSRouter("/edit", &controllers.DividendsController{}, "POST:Edit"),
  32 + beego.NSRouter("/detail", &controllers.DividendsController{}, "POST:Detail"),
  33 + ),
30 ) 34 )
31 beego.AddNamespace(adminRouter) 35 beego.AddNamespace(adminRouter)
32 } 36 }