作者 yangfu

分红管理修改

... ... @@ -32,3 +32,12 @@ func CreateAdminPermissionRepository(options map[string]interface{}) (domain.Adm
}
return repository.NewAdminPermissionRepository(transactionContext)
}
//CreatePartnerInfoRepository 合伙人信息
func CreateOrderPaymentRepository(options map[string]interface{}) (domain.OrderPaymentRepository, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
return repository.NewOrderPaymentRepository(transactionContext)
}
... ...
package command
type CreateOrderPaymentCommand struct {
//订单编号
OrderId int64 `json:"orderId"`
// 货款
PaymentForGoods float64 `json:"paymentForGoods,omitempty"`
// 支付状态
StateOfPayment int `json:"stateOfPayment,omitempty"`
//支付批次
PaymentSn int `json:"paymentSn,omitempty"`
}
func (command CreateOrderPaymentCommand) ValidateCommand() error {
return nil
}
... ...
package query
type ListOrderPaymentQuery struct {
// 合伙人类别
OrderId int64 `json:"orderId"`
}
func (q *ListOrderPaymentQuery) ValidateQuery() error {
return nil
}
... ...
package service
import (
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/command"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/query"
"time"
//"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/query"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
// 客户价值服务
type OrderPaymentService struct {
}
func NewOrderPaymentService(options map[string]interface{}) *OrderPaymentService {
newOrderPaymentService := &OrderPaymentService{}
return newOrderPaymentService
}
// 创建订单支付数据
func (OrderPaymentService *OrderPaymentService) CreateOrderPayment(command *command.CreateOrderPaymentCommand) (data interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
if err = command.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
//检查订单是否存在
var OrderPaymentRepository domain.OrderPaymentRepository
if OrderPaymentRepository, err = factory.CreateOrderPaymentRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
dm := &domain.OrderPayment{
OrderId: command.OrderId,
PartnerId: 0,
PaymentAmount: 0,
BonusAmount: 0, //计算分红金额
BonusStatus: 0,
CreateAt: time.Now(),
PaymentSn: command.PaymentSn,
UpdateAt: time.Now(),
}
//检查货款 已存在 / 未存在
if findDm, e := OrderPaymentRepository.FindOne(domain.OrderPaymentFindOneQuery{OrderId: command.OrderId, PaymentSn: command.PaymentSn}); e == nil {
if dm.BonusStatus == domain.BonusPaid {
return
}
dm = findDm
}
dm.PaymentAmount = command.PaymentForGoods
dm.BonusStatus = command.StateOfPayment
dm.BonusAmount = 0
if data, err = OrderPaymentRepository.Save(dm); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
return
}
//// GetOrderPayment 返回合伙人
//func (OrderPaymentService *OrderPaymentService) GetOrderPayment(command query.GetOrderPaymentQuery) (data *domain.OrderPayment, err error) {
// var (
// transactionContext, _ = factory.CreateTransactionContext(nil)
// )
// if err = command.ValidateQuery(); err != nil {
// return nil, lib.ThrowError(lib.ARG_ERROR, err.Error())
// }
// if err := transactionContext.StartTransaction(); err != nil {
// return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
// }
// defer func() {
// transactionContext.RollbackTransaction()
// }()
// var OrderPaymentRepository domain.OrderPaymentRepository
// if OrderPaymentRepository, err = factory.CreateOrderPaymentRepository(map[string]interface{}{
// "transactionContext": transactionContext,
// }); err != nil {
// return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
// }
// data, err = OrderPaymentRepository.FindOne(domain.PartnerFindOneQuery{UserId: command.Id})
// if err != nil {
// return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
// }
// err = transactionContext.CommitTransaction()
// return
//}
//
//// 更新客户价值
//func (OrderPaymentService *OrderPaymentService) UpdateOrderPayment(updateOrderPaymentCommand *command.UpdateOrderPaymentCommand) (err error) {
// var (
// transactionContext, _ = factory.CreateTransactionContext(nil)
// )
// if err = updateOrderPaymentCommand.ValidateCommand(); err != nil {
// return application.ThrowError(application.ARG_ERROR, err.Error())
// }
// if err := transactionContext.StartTransaction(); err != nil {
// return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
// }
// defer func() {
// transactionContext.RollbackTransaction()
// }()
// var OrderPaymentRepository domain.OrderPaymentRepository
// if OrderPaymentRepository, err = factory.CreateOrderPaymentRepository(map[string]interface{}{
// "transactionContext": transactionContext,
// }); err != nil {
// return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
// }
// OrderPayment, err := OrderPaymentRepository.FindOne(domain.PartnerFindOneQuery{
// UserId: updateOrderPaymentCommand.Id,
// })
// if err != nil {
// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
// }
// OrderPayment.PartnerCategory = updateOrderPaymentCommand.PartnerCategory
// OrderPayment.Salesman = updateOrderPaymentCommand.Salesman
// OrderPayment.Status = updateOrderPaymentCommand.Status
// OrderPayment.RegionInfo = updateOrderPaymentCommand.RegionInfo
// if _, err = OrderPaymentRepository.Save(OrderPayment); err != nil {
// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
// }
// transactionContext.CommitTransaction()
// return
//}
//
// 返回订单支付列表
func (OrderPaymentService *OrderPaymentService) ListOrderPayment(listOrderPaymentQuery *query.ListOrderPaymentQuery) (int, []*domain.OrderPayment, error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
OrderPayments []*domain.OrderPayment
count int
err error
)
if err = listOrderPaymentQuery.ValidateQuery(); err != nil {
return 0, nil, lib.ThrowError(lib.ARG_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return 0, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
if err != nil {
transactionContext.RollbackTransaction()
}
}()
var OrderPaymentRepository domain.OrderPaymentRepository
if OrderPaymentRepository, err = factory.CreateOrderPaymentRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
queryOption := domain.OrderPaymentQuery{
OrderId: listOrderPaymentQuery.OrderId,
}
if OrderPayments, err = OrderPaymentRepository.Find(queryOption); err != nil {
return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if count, err = OrderPaymentRepository.CountAll(queryOption); err != nil {
return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if err = transactionContext.CommitTransaction(); err != nil {
return 0, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
return count, OrderPayments, nil
}
... ...
package domain
import "time"
const (
BonusWaitPay = iota + 1 //等待支付分红
BonusPaid //已经支付分红
)
type OrderPayment struct {
//编号
Id int64 `json:"id"`
//订单编号
OrderId int64 `json:"orderId"`
//合伙人编号
PartnerId int64 `json:"partnerId"`
//支付序号
PaymentSn int `json:"paymentSn"`
//支付货款
PaymentAmount float64 `json:"paymentAmount"`
//分红金额
BonusAmount float64 `json:"bonusAmount"`
//分红状态 1.等待支付分红 2.已支付分红
BonusStatus int `json:"bonusStatus"`
//创建时间
CreateAt time.Time `json:"createAt"`
//更新时间
UpdateAt time.Time `json:"updateAt"`
}
func (m *OrderPayment) Identify() interface{} {
if m.Id == 0 {
return nil
}
return m.Id
}
func (m *OrderPayment) Update(data map[string]interface{}) error {
if m.BonusStatus != BonusWaitPay {
return nil
}
if paymentAmount, ok := data["paymentAmount"]; ok && paymentAmount != 0 {
m.PaymentAmount = paymentAmount.(float64)
}
if bonusAmount, ok := data["bonusAmount"]; ok && bonusAmount != 0 {
m.BonusAmount = bonusAmount.(float64)
}
if bonusStatus, ok := data["bonusStatus"]; ok && bonusStatus != 0 {
m.BonusStatus = bonusStatus.(int)
}
m.UpdateAt = time.Now()
return nil
}
type OrderPaymentFindOneQuery struct {
Id int64
OrderId int64
PaymentSn int
}
type OrderPaymentQuery struct {
Offset int
Limit int
OrderId int64
//PartnerCategory []int //合伙人类型
//RegionInfo *RegionInfo //区域
//PartnerName string //合伙人姓名
}
type OrderPaymentRepository interface {
Save(dm *OrderPayment) (*OrderPayment, error)
FindOne(queryOptions OrderPaymentFindOneQuery) (*OrderPayment, error)
Find(queryOptions OrderPaymentQuery) ([]*OrderPayment, error)
CountAll(queryOptions OrderPaymentQuery) (int, error)
}
... ...
package models
import "time"
type OrderPayment struct {
tableName struct{} `pg:"order_payment"`
//编号
Id int64 `pg:",pk"`
//订单编号
OrderId int64
//合伙人编号
PartnerId int64
//支付序号
PaymentSn int
//支付货款
PaymentAmount float64 `pg:",notnull,default:0"`
//分红金额
BonusAmount float64
//分红状态 1.等待支付分红 2.已支付分红
BonusStatus int `pg:",notnull,default:1"`
//创建时间
CreateAt time.Time
//更新时间
UpdateAt time.Time
}
... ...
package repository
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
type OrderPaymentRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *OrderPaymentRepository) Save(dm *domain.OrderPayment) (*domain.OrderPayment, error) {
var (
err error
tx = repository.transactionContext.PgTx
)
m := &models.OrderPayment{
Id: dm.Id,
OrderId: dm.OrderId,
PartnerId: dm.PartnerId,
PaymentAmount: dm.PaymentAmount,
BonusAmount: dm.BonusAmount,
BonusStatus: dm.BonusStatus,
CreateAt: dm.CreateAt,
UpdateAt: dm.UpdateAt,
}
if m.Id == 0 {
err = tx.Insert(m)
dm.Id = m.Id
if err != nil {
return nil, err
}
} else {
err = tx.Update(m)
if err != nil {
return nil, err
}
}
return dm, nil
}
func (repository *OrderPaymentRepository) Remove(OrderPayment *domain.OrderPayment) (*domain.OrderPayment, error) {
var (
tx = repository.transactionContext.PgTx
OrderPaymentModel = &models.OrderPayment{Id: OrderPayment.Identify().(int64)}
)
if _, err := tx.Model(OrderPaymentModel).Where("id = ?", OrderPayment.Id).Delete(); err != nil {
return OrderPayment, err
}
return OrderPayment, nil
}
func (repository *OrderPaymentRepository) FindOne(queryOptions domain.OrderPaymentFindOneQuery) (*domain.OrderPayment, error) {
tx := repository.transactionContext.PgTx
OrderPaymentModel := new(models.OrderPayment)
query := tx.Model(OrderPaymentModel)
if queryOptions.Id > 0 {
query.Where("order_payment.id = ?", "id")
}
if err := query.First(); err != nil {
return nil, err
}
if OrderPaymentModel.Id == 0 {
return nil, nil
}
return repository.transformPgModelToDomainModel(OrderPaymentModel)
}
func (repository *OrderPaymentRepository) Find(queryOptions domain.OrderPaymentQuery) ([]*domain.OrderPayment, error) {
tx := repository.transactionContext.PgTx
var OrderPaymentModels []*models.OrderPayment
query := tx.Model(&OrderPaymentModels)
query.Where("order_payment.partner_id = ?", "partnerId")
var (
err error
rsp = make([]*domain.OrderPayment, 0)
)
err = query.Select()
if err != nil {
return rsp, err
}
for i := range OrderPaymentModels {
dm, err := repository.transformPgModelToDomainModel(OrderPaymentModels[i])
if err != nil {
return rsp, err
}
rsp = append(rsp, dm)
}
return rsp, nil
}
func (repository OrderPaymentRepository) CountAll(queryOption domain.OrderPaymentQuery) (int, error) {
db := repository.transactionContext.PgDd
partnerModels := models.PartnerInfo{}
query := db.Model(&partnerModels)
//if len(queryOption.PartnerName) > 0 {
// query = query.Where("partner_name like ?", "%"+queryOption.PartnerName+"%")
//}
//if queryOption.RegionInfo != nil {
// query = query.Where("region_info::jsonb->>'regionName' like ?", "%"+queryOption.RegionInfo.RegionName+"%")
//}
//if len(queryOption.PartnerCategory) > 0 {
// query = query.WhereIn("partner_category in(?)", queryOption.PartnerCategory)
//}
cnt, err := query.Count()
return cnt, err
}
func (repository *OrderPaymentRepository) transformPgModelToDomainModel(dm *models.OrderPayment) (*domain.OrderPayment, error) {
m := &domain.OrderPayment{
Id: dm.Id,
OrderId: dm.OrderId,
PartnerId: dm.PartnerId,
PaymentAmount: dm.PaymentAmount,
BonusAmount: dm.BonusAmount,
BonusStatus: dm.BonusStatus,
CreateAt: dm.CreateAt,
UpdateAt: dm.UpdateAt,
}
return m, nil
}
func NewOrderPaymentRepository(transactionContext *transaction.TransactionContext) (*OrderPaymentRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &OrderPaymentRepository{transactionContext: transactionContext}, nil
}
... ...
package controllers
import (
"errors"
"github.com/astaxie/beego/logs"
OrderPaymentCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/command"
OrderPaymentQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/query"
OrderPaymentSvr "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/service"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"strconv"
)
type DividendsController struct {
BaseController
}
////Prepare 重写 BaseController 的Prepare方法
func (c *DividendsController) Prepare() {
c.BaseController.Prepare()
if ok := c.ValidJWTToken(); !ok {
return
}
if ok := c.ValidAdminPermission(domain.PERMINSSION_PARTNER); !ok {
return
}
}
//Edit 编辑分红支付
func (c *DividendsController) Edit() {
//用与适配前端定义的数据结构
type DividendPaymentItem struct {
PaymentForGoods string `json:"paymentForGoods"`
StateOfPayment string `json:"stateOfPayment"`
}
type Parameter struct {
Id int64 `json:"id"` //订单编号
DividendPayment []DividendPaymentItem `json:"dividendPayment"`
}
var (
param Parameter
err error
totalPaymentAmount float64
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
//编辑
for i := range param.DividendPayment {
item := param.DividendPayment[i]
cmd := OrderPaymentCmd.CreateOrderPaymentCommand{
OrderId: param.Id,
PaymentSn: i,
}
cmd.PaymentForGoods, _ = strconv.ParseFloat(item.PaymentForGoods, 64)
cmd.StateOfPayment, _ = strconv.Atoi(item.StateOfPayment)
serve := OrderPaymentSvr.NewOrderPaymentService(nil)
_, err = serve.CreateOrderPayment(&cmd)
if err != nil {
c.ResponseError(err)
return
}
if cmd.StateOfPayment == domain.BonusPaid {
totalPaymentAmount += cmd.PaymentForGoods
}
}
//保存累计支付金额到订单里面
c.ResponseData(nil)
return
}
//Edit 分红支付详情
func (c *DividendsController) Detail() {
type Parameter struct {
Id int64 `json:"id"` //订单编号
}
var (
param Parameter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
cmd := OrderPaymentQuery.ListOrderPaymentQuery{
OrderId: param.Id,
}
serve := OrderPaymentSvr.NewOrderPaymentService(nil)
var data []*domain.OrderPayment
_, data, err = serve.ListOrderPayment(&cmd)
if err != nil {
c.ResponseError(err)
return
}
type DividendPayment struct {
PaymentForGoods float64 `json:"paymentForGoods"`
UpdateTime string `json:"updateTime"`
StateOfPayment int `json:"stateOfPayment"`
Dividend float64 `json:"dividend"`
DividendProportion float64 `json:"dividendProportion"`
}
type Response struct {
DividendPayment []DividendPayment `json:"dividendPayment"`
}
rsp := Response{DividendPayment: make([]DividendPayment, 0)}
for i := range data {
item := data[i]
payment := DividendPayment{
PaymentForGoods: item.PaymentAmount,
UpdateTime: item.UpdateAt.Format("2006-01-02 15:04:05"),
StateOfPayment: item.BonusStatus,
Dividend: item.BonusAmount,
DividendProportion: 2,
}
rsp.DividendPayment = append(rsp.DividendPayment, payment)
}
c.ResponseData(rsp)
return
}
... ...