作者 tangxvhui

新增更新

... ... @@ -50,3 +50,21 @@ func CreateOrderRepository(options map[string]interface{}) (domain.OrderReposito
}
return repository.NewOrderRepository(transactionContext)
}
//CreateOrderBaseRepository 订单信息
func CreateOrderBaseRepository(options map[string]interface{}) (domain.OrderBaseRepository, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
return repository.NewOrderBaseRepository(transactionContext)
}
//CreateOrderGoodRepository 订单信息
func CreateOrderGoodRepository(options map[string]interface{}) (domain.OrderGoodRepository, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
return repository.NewOrderGoodRepository(transactionContext)
}
... ...
package query
//ListOrderQuery 获取用户列表
//GetOrderQuery 获取订单详情
type GetOrderQuery struct {
OrderId int64 `json:"orderId"`
}
... ...
package query
//ListOrderQuery 获取用户列表
//ListOrderQuery 获取订单列表
type ListOrderQuery struct {
//合伙人id
PartnerId int64 `json:"partnerId" `
... ...
package command
type CreateOrderCommand struct {
//订单类型
OrderType int `json:"orderType"`
//订单编号
OrderCode string `json:"orderCode"`
//交货编号
DeliveryCode string `json:"deliveryCode"`
//买家
BuyerName string `json:"buyerName"`
//订单区域信息
OrderRegion string `json:"orderRegion"`
//订单对应的合伙人
PartnerId int64 `json:"partnerId"`
//业务员抽成比例
SalesmanBonusPercent float64 `json:"salesmanBonusPercent"`
//货品
Goods []OrderGoodData `json:"goods"`
}
... ...
package command
type OrderGoodData struct {
//货品id
Id int64 `json:"id"`
//货品名称 长度可能较长
GoodName string `json:"goodName"`
//预计的货品数量
PlanGoodNumber int `json:"planGoodNumber"`
//调整后的货品数量
//业务判定时0是有效值,
//所以用空串表示无值,转换到数据库中为负值
UseGoodNumber int `json:"useGoodNumber"`
//货品单价
Price float64 `json:"price"`
//合伙人分红比例
PartnerBonusPercent float64 `json:"partnerBonusPercent"`
//分红支付状态
BonusStatus int `json:"bonusStatus"`
//备注信息
Remark string `json:"remark"`
}
... ...
package command
type UpdateOrderCommand struct {
//订单类型
OrderType int `json:"orderType"`
//订单编号
OrderCode string `json:"orderCode"`
//交货编号
DeliveryCode string `json:"deliveryCode"`
//买家
BuyerName string `json:"buyerName"`
//订单区域信息
OrderRegion string `json:"orderRegion"`
//订单对应的合伙人
PartnerId int64 `json:"partnerId"`
//业务员抽成比例
SalesmanBonusPercent float64 `json:"salesmanBonusPercent"`
//货品
Goods []OrderGoodData `json:"goods"`
}
... ...
package query
//GetOrderQuery 获取订单详情
type GetOrderQuery struct {
OrderId int64 `json:"orderId"`
}
... ...
package query
//ListOrderQuery 获取用户列表
type ListOrderQuery struct {
//合伙人id
PartnerId int64 `json:"partnerId" `
//订单编号
OrderCode string `json:"order_code"`
// 查询偏离量
Offset int `json:"offset" `
// 查询限制
Limit int `json:"limit"`
//订单类型
OrderType int `json:"orderType"`
}
... ...
package service
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/command"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/query"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
//OrderService 管理员相关服务
type OrderInfoService struct {
}
func NewOrderInfoService(option map[string]interface{}) *OrderInfoService {
newAdminUserService := new(OrderInfoService)
return newAdminUserService
}
// PageListOrderBase 获取订单列表
func (service OrderInfoService) PageListOrderBase(listOrderQuery query.ListOrderQuery) ([]domain.OrderBase, int, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, 0, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
var (
orderRepository domain.OrderBaseRepository
orders []domain.OrderBase
cnt int
)
if value, err := factory.CreateOrderBaseRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else {
orderRepository = value
}
query := domain.OrderFindQuery{
PartnerId: listOrderQuery.PartnerId,
OrderCode: listOrderQuery.OrderCode,
Offset: listOrderQuery.Offset,
Limit: listOrderQuery.Limit,
OrderType: listOrderQuery.OrderType,
}
orders, cnt, err = orderRepository.Find(query)
if err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var PartnerInfoRepository domain.PartnerInfoRepository
if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
for i := range orders {
var partnerData *domain.PartnerInfo
partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{
UserId: orders[i].PartnerId,
})
if err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
orders[i].PartnerInfo = partnerData.Partner
}
return orders, cnt, nil
}
//GetOrderDetail 获取订单详情
func (service OrderInfoService) GetOrderDetail(getOrderQuery query.GetOrderQuery) (*domain.OrderBase, error) {
//实际业务
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
var (
orderBaseRepository domain.OrderBaseRepository
PartnerInfoRepository domain.PartnerInfoRepository
orderGoodRepository domain.OrderGoodRepository
order *domain.OrderBase
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if value, err := factory.CreateOrderBaseRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else {
orderBaseRepository = value
}
if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if orderGoodRepository, err = factory.CreateOrderGoodRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
order, err = orderBaseRepository.FindOne(domain.OrderFindOneQuery{
OrderId: getOrderQuery.OrderId,
})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var (
partnerData *domain.PartnerInfo
goods []domain.OrderGood
)
partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: order.PartnerInfo.Id})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
order.PartnerInfo = partnerData.Partner
goods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{
OrderId: order.Id,
})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取订单中的商品列表失败:%s", err))
}
order.Goods = goods
err = transactionContext.CommitTransaction()
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return order, nil
}
func (service OrderInfoService) CreateNewOrder(cmd command.CreateOrderCommand) (*domain.OrderBase, error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
// orderDao, _ := factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})
// ok, err := orderDao.OrderCodeIsExist(command.OrderCode, 0)
// if err != nil {
// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
// }
// if ok {
// return lib.ThrowError(lib.BUSINESS_ERROR, "订单编号已存在")
// }
var PartnerInfoRepository domain.PartnerInfoRepository
if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var partnerData *domain.PartnerInfo
partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: cmd.PartnerId})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("检索合伙人数据失败"))
}
var (
orderBaseRepository domain.OrderBaseRepository
orderGoodRepository domain.OrderGoodRepository
)
if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if orderGoodRepository, err = factory.CreateOrderGoodRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
newOrder := &domain.OrderBase{
OrderType: cmd.OrderType, OrderCode: cmd.OrderCode,
DeliveryCode: "",
Buyer: &domain.Buyer{
BuyerName: cmd.BuyerName,
},
RegionInfo: &domain.RegionInfo{
RegionName: cmd.OrderRegion,
},
PartnerId: cmd.PartnerId,
PartnerInfo: partnerData.Partner,
SalesmanBonusPercent: cmd.SalesmanBonusPercent,
}
var orderGoods []domain.OrderGood
for _, good := range cmd.Goods {
m := domain.NewOrderGood()
m.OrderId = newOrder.Id
m.GoodName = good.GoodName
m.PlanGoodNumber = good.PlanGoodNumber
m.Price = good.Price
m.PartnerBonusPercent = good.PartnerBonusPercent
m.Remark = good.Remark
err = m.Compute()
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中商品的数值失败:%s", err))
}
orderGoods = append(orderGoods, m)
}
newOrder.Goods = orderGoods
err = newOrder.Compute()
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中合计的数值失败:%s", err))
}
err = orderBaseRepository.Save(newOrder)
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单数据失败:%s", err))
}
err = orderGoodRepository.Save(orderGoods)
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单中的商品数据失败:%s", err))
}
newOrder.Goods = orderGoods
err = transactionContext.CommitTransaction()
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return newOrder, nil
}
//DeleteOrder 删除订单
func (service OrderInfoService) DeleteOrder(orderId int64) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
orderBaseRepository domain.OrderBaseRepository
orderGoodRepository domain.OrderGoodRepository
)
if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if orderGoodRepository, err = factory.CreateOrderGoodRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = orderBaseRepository.Remove(orderId)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("删除订单数据失败:%s", err))
}
err = orderGoodRepository.Remove(orderId)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("删除订单中商品数据失败:%s", err))
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
... ...
... ... @@ -12,21 +12,6 @@ const (
OrderStatusDeliverAll //全部发货
)
const (
OrderReal = iota + 1 //实发订单
OrderIntention //意向订单
)
//买家
type Buyer struct {
//买家姓名
BuyerName string `json:"buyerName"`
//联系方式
ContactInfo string `json:"contactInfo"`
//收获地址
ShippingAddress string `json:"shippingAddress"`
}
type Order struct {
Id int64 `json:"id"`
//订单类型
... ... @@ -157,11 +142,12 @@ type OrderFindOneQuery struct {
}
type OrderFindQuery struct {
PartnerId int64
OrderCode string
Offset int
Limit int
OrderType int
PartnerId int64
OrderCode string
DeliveryCode string
Offset int
Limit int
OrderType int
}
type OrderRepository interface {
... ...
package domain
import (
"time"
"github.com/shopspring/decimal"
)
const (
OrderReal = iota + 1 //实发订单
OrderIntention //意向订单
)
//买家
type Buyer struct {
//买家姓名
BuyerName string `json:"buyerName"`
//联系方式
ContactInfo string `json:"contactInfo"`
//收获地址
ShippingAddress string `json:"shippingAddress"`
}
//OrderBase 订单基础
type OrderBase struct {
//表id
Id int64 `json:"id"`
//订单类型
OrderType int `json:"orderType"`
//订单编号
OrderCode string `json:"orderCode"`
//交货编号
DeliveryCode string `json:"deliveryCode"`
//买家
Buyer *Buyer `json:"buyer"`
//订单区域信息
RegionInfo *RegionInfo `json:"regionInfo"`
//订单对应的合伙人
PartnerId int64 `json:"partnerId"`
PartnerInfo Partner `json:"partnerInfo"`
//业务员抽成比例
SalesmanBonusPercent float64 `json:"salesmanBonusPercent"`
//订单的创建时间
CreateTime time.Time `json:"createTime"`
//发货时间
DeliveryTime time.Time `json:"deliveryTime"`
//更新时间
UpdateTime time.Time `json:"updateTime"`
//货品
Goods []OrderGood `json:"goods"`
//核算订单相关数据
OrderCompute OrderCompute `json:"orderCompute"`
}
type OrderCompute struct {
//合伙人应收分红
PlanPartnerBonus float64 `json:"planPartnerBonus"`
//调整后的合伙人应收分红 (初始值=-1);
//业务判定时0是有效值,
//所以用空串表示无值,转换到数据库中为负值
UsePartnerBonus float64 `json:"usePartnerBonus"`
//合伙人已收分红
PartnerBonusHas float64 `json:"partnerBonusHas"`
//合伙人未收分红
PartnerBonusNot float64 `json:"partnerBonusNot"`
//合伙人分红支出
PartnerBonusExpense float64 `json:"partnerBonusExpense"`
//业务员抽成
SalesmanBonus float64 `json:"salesmanBonus"`
//预计的订单内货品总数
PlanOrderCount int `json:"planOrderCount"`
//预计的订单的总金额
PlanOrderAmount float64 `json:"planOrderAmount"`
//按需使用的订单内货品总数 (初始值=-1)
//业务判定时0是有效值,
//所以用空串表示无值,转换到数据库中为负值
UseOrderCount int `json:"useOrderCount"`
//按需使用的订单内货总金额 (初始值=-1)
//业务判定时0是有效值,
//所以用空串表示无值,转换到数据库中为负值
UseOrderAmount float64 `json:"useOrderAmount"`
}
//Compute 数据汇总核算
func (order *OrderBase) Compute() error {
planPartnerBonus := decimal.NewFromFloat(0)
planOrderAmount := decimal.NewFromFloat(0)
var (
planOrderCount int = 0
useOrderCount int = 0
HasUseOrderCount bool = false
)
usePartnerBonus := decimal.NewFromFloat(0)
var hasUsePartnerBonus bool = false
useOrderAmount := decimal.NewFromFloat(0)
var hasUseOrderAmount bool = false
partnerBonusHas := decimal.NewFromFloat(0)
partnerBonusNot := decimal.NewFromFloat(0)
partnerBonusExpense := decimal.NewFromFloat(0)
//统计所以货品的值
for i := range order.Goods {
planPartnerBonus.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PlanPartnerBonus))
planOrderAmount.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PlanAmount))
planOrderCount += order.Goods[i].PlanGoodNumber
//调整值 非负值得情况
goodUseAmount := decimal.NewFromFloat(order.Goods[i].GoodCompute.UseAmount)
if goodUseAmount.GreaterThanOrEqual(decimal.NewFromFloat(0)) {
hasUseOrderAmount = true
useOrderAmount.Add(goodUseAmount)
}
goodUsePartnerBonus := decimal.NewFromFloat(order.Goods[i].GoodCompute.UsePartnerBonus)
if goodUsePartnerBonus.GreaterThanOrEqual(decimal.NewFromFloat(0)) {
hasUsePartnerBonus = true
usePartnerBonus.Add(goodUsePartnerBonus)
}
if order.Goods[i].UseGoodNumber >= 0 {
HasUseOrderCount = true
useOrderCount += order.Goods[i].UseGoodNumber
}
partnerBonusHas.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PartnerBonusHas))
partnerBonusNot.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PartnerBonusNot))
partnerBonusExpense.Add(decimal.NewFromFloat(order.Goods[i].GoodCompute.PartnerBonusExpense))
}
//汇总赋值
order.OrderCompute.PartnerBonusExpense, _ = partnerBonusExpense.Round(2).Float64()
order.OrderCompute.PartnerBonusHas, _ = partnerBonusHas.Round(2).Float64()
order.OrderCompute.PartnerBonusNot, _ = partnerBonusNot.Round(2).Float64()
if hasUsePartnerBonus {
} else {
}
if hasUseOrderAmount {
} else {
}
if HasUseOrderCount {
} else {
}
return nil
}
type OrderBaseFindOneQuery struct {
OrderId int64
}
type OrderBaseFindQuery struct {
PartnerId int64
OrderCode string
Offset int
Limit int
OrderType int
}
type OrderBaseRepository interface {
Save(order *OrderBase) error
FindOne(qureyOptions OrderFindOneQuery) (*OrderBase, error)
Find(queryOptions OrderFindQuery) ([]OrderBase, int, error)
Remove(id int64) error
}
... ...
package domain
import (
"errors"
"github.com/shopspring/decimal"
)
//分红状态
type OrderGoodBonusStatus interface {
//状态变更为待支付
WartPayPartnerBonus(orderGood *OrderGood) error
//状态变更为已支付
PayPartnerBonus(orderGood *OrderGood) error
}
//货单的支付状态
const (
//待支付
OrderGoodWaitPay int = 1
//已支付
OrderGoodHasPay int = 2
)
//OrderGood 订单中的货品
type OrderGood struct {
//货品id
Id int64 `json:"id"`
//所属订单id
OrderId int64 `json:"orderId"`
//货品名称 长度可能较长
GoodName string `json:"goodName"`
//预计的货品数量
PlanGoodNumber int `json:"planGoodNumber"`
//调整后的货品数量
//业务判定时0是有效值,
//所以用空串表示无值,转换到数据库中为负值
UseGoodNumber int `json:"useGoodNumber"`
//货品单价
Price float64 `json:"price"`
//合伙人分红比例
PartnerBonusPercent float64 `json:"partnerBonusPercent"`
//分红支付状态
BonusStatus int `json:"bonusStatus"`
//备注信息
Remark string `json:"remark"`
//当前分红支付状态
CurrentBonusStatus OrderGoodBonusStatus `json:"-"`
///核算订单相关数据
GoodCompute GoodCompute `json:"goodCompute"`
}
type GoodCompute struct {
//预计的货品总额
PlanAmount float64 `json:"planAmount"`
//调整后的货品总额 (初始值=-1)
//业务判定时0是有效值,
//所以用空串表示无值,转换到数据库中为负值
UseAmount float64 `json:"useAmount"`
//预计的合伙人分红
PlanPartnerBonus float64 `json:"planPartnerBonus"`
//合伙人应收分红调整 (初始值=-1),
//业务判定时0是有效值,
//所以用空串表示无值,转换到数据库中为负值
UsePartnerBonus float64 `json:"usePartnerBonus"`
//合伙人已收分红
PartnerBonusHas float64 `json:"partnerBonusHas"`
//合伙人未收分红
PartnerBonusNot float64 `json:"partnerBonusNot"`
//合伙人分红支出
PartnerBonusExpense float64 `json:"partnerBonusExpense"`
}
//OrderGoodBonusWaitPay 货品分红待支付
type OrderGoodBonusWaitPay struct{}
var _ OrderGoodBonusStatus = (*OrderGoodBonusWaitPay)(nil)
//OrderGoodBonusHasPay 货品分红已支付
type OrderGoodBonusHasPay struct{}
var _ OrderGoodBonusStatus = (*OrderGoodBonusHasPay)(nil)
func (waitPay OrderGoodBonusWaitPay) PayPartnerBonus(good *OrderGood) error {
good.GoodCompute.PartnerBonusExpense = 0
good.GoodCompute.PartnerBonusHas = 0
//初始状态为待支付时 ,合伙人未收分红等于合伙人应收分红(或者调整的)
if good.UseGoodNumber < 0 {
good.GoodCompute.PartnerBonusNot = good.GoodCompute.PlanPartnerBonus
} else {
good.GoodCompute.PartnerBonusNot = good.GoodCompute.UsePartnerBonus
}
return nil
}
func (waitPay OrderGoodBonusWaitPay) WartPayPartnerBonus(good *OrderGood) error {
//待支付状态转支付时 ,合伙人已收收分红等于合伙人应收分红(或者调整的)
if good.GoodCompute.UsePartnerBonus < 0 {
good.GoodCompute.PartnerBonusHas = good.GoodCompute.PlanPartnerBonus
} else {
good.GoodCompute.PartnerBonusHas = good.GoodCompute.UsePartnerBonus
}
good.GoodCompute.PartnerBonusExpense = 0
return nil
}
func (hasPay OrderGoodBonusHasPay) PayPartnerBonus(good *OrderGood) error {
//已支付的值保持不变
//未支付的值保持不变
//计算分红支出
//分红支出=应收分红(有出现调整,则取调整后的应收分红)和已收分红的差额
if good.UseGoodNumber >= 0 {
good.GoodCompute.PartnerBonusExpense = good.GoodCompute.PartnerBonusHas - good.GoodCompute.UsePartnerBonus
}
return nil
}
func (hasPay OrderGoodBonusHasPay) WartPayPartnerBonus(good *OrderGood) error {
return errors.New("已支付的货单不能将状态回退为待支付")
}
//NewOrderGood 初始值设定
func NewOrderGood() OrderGood {
return OrderGood{
UseGoodNumber: -1,
BonusStatus: OrderGoodWaitPay,
GoodCompute: GoodCompute{
UsePartnerBonus: -1,
UseAmount: -1,
},
CurrentBonusStatus: OrderGoodBonusWaitPay{},
}
}
//Compute 数据汇总核算
func (good *OrderGood) Compute() error {
price := decimal.NewFromInt(int64(good.PlanGoodNumber))
planamount := price.Mul(decimal.NewFromInt(int64(good.PlanGoodNumber))) //price*planGoodNumber
//price*useGoodNumber
planPartnerBonus := planamount.Mul(decimal.NewFromFloat(good.PartnerBonusPercent)) //price*planGoodNumber*PartnerBonusPercent
good.GoodCompute.PlanAmount, _ = planamount.Round(2).Float64()
good.GoodCompute.PlanPartnerBonus, _ = planPartnerBonus.Round(2).Float64()
if good.UseGoodNumber < 0 {
//没有出现数量调整
good.GoodCompute.UsePartnerBonus = -1
good.GoodCompute.UseAmount = -1
} else {
useamount := price.Mul(decimal.NewFromInt(int64(good.UseGoodNumber))) //price*useGoodNumber/price*useGoodNumber
usePartnerBonus := useamount.Mul(decimal.NewFromFloat(good.PartnerBonusPercent)) //price*useGoodNumber*PartnerBonusPercent
good.GoodCompute.UsePartnerBonus, _ = usePartnerBonus.Round(2).Float64()
good.GoodCompute.UseAmount, _ = useamount.Round(2).Float64()
}
return nil
}
type OrderGoodFindQuery struct {
OrderId int64
Offset int
Limit int
}
type OrderGoodRepository interface {
Save(order []OrderGood) error
Find(queryOptions OrderGoodFindQuery) ([]OrderGood, int, error)
Remove(orderid int64, ids ...int64) error
}
... ...
package models
import (
"context"
"time"
"github.com/go-pg/pg/v10"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
)
//OrderBase 订单基础
type OrderBase struct {
tableName struct{} `pg:"order_base"`
//表id
Id int64 `pg:",pk"`
//订单类型
OrderType int
//订单编号
OrderCode string
//交货编号
DeliveryCode string
//买家
Buyer *domain.Buyer
//订单区域信息
RegionInfo *domain.RegionInfo
//订单对应的合伙人
PartnerId int64
//业务员抽成比例
SalesmanBonusPercent float64
//业务员抽成
SalesmanBonus float64
//预计的订单内货品总数
PlanOrderCount int
//预计的订单的总金额
PlanOrderAmount float64
//按需使用的订单内货品总数 (初始值=-1)
//业务判定时0是有效值,负值无效
UseOrderCount int
//按需使用的订单内货总金额 (初始值=-1)
//业务判定时0是有效值,负值无效
UseOrderAmount float64
//订单的创建时间
CreateTime time.Time
//发货时间
DeliveryTime time.Time
//更新时间
UpdateTime time.Time
//合伙人应收分红
PlanPartnerBonus float64
//调整后的合伙人应收分红 (初始值=-1);
//业务判定时0是有效值,负值无效
UsePartnerBonus float64
//合伙人已收分红
PartnerBonusHas float64
//合伙人未收分红
PartnerBonusNot float64
//合伙人分红支出
PartnerBonusExpense float64
}
var _ pg.BeforeUpdateHook = (*OrderBase)(nil)
func (or *OrderBase) BeforeUpdate(ctx context.Context) (context.Context, error) {
or.UpdateTime = time.Now()
return ctx, nil
}
var _ pg.BeforeInsertHook = (*OrderBase)(nil)
func (or *OrderBase) BeforeInsert(ctx context.Context) (context.Context, error) {
or.CreateTime = time.Now()
or.UpdateTime = time.Now()
return ctx, nil
}
... ...
package models
//OrderGood 订单中的货品
type OrderGood struct {
tableName struct{} `pg:"order_good"`
//货品id
Id int64
//所属订单id
OrderId int64
//货品名称 长度可能较长
GoodName string
//预计的货品数量
PlanGoodNumber int
//货品单价
Price float64
//预计的货品总额
PlanAmount float64
//调整后的货品数量(初始值=-1)
//业务判定时0是有效值,负值无效
UseGoodNumber int
//调整后的货品总额
UseAmount float64
//合伙人分红比例
PartnerBonusPercent float64
//预计的合伙人分红
PlanPartnerBonus float64
//合伙人应收分红调整 (初始值=-1)
////业务判定时0是有效值,负值无效
UsePartnerBonus float64
//合伙人已收分红
PartnerBonusHas float64
//合伙人未收分红
PartnerBonusNot float64
//合伙人分红支出
PartnerBonusExpense float64
//分红状态
BonusStatus int
//备注信息
Remark string
}
... ...
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 OrderBaseRepository struct {
transactionContext *transaction.TransactionContext
}
var (
_ domain.OrderBaseRepository = (*OrderBaseRepository)(nil)
)
func NewOrderBaseRepository(transactionContext *transaction.TransactionContext) (*OrderBaseRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &OrderBaseRepository{transactionContext: transactionContext}, nil
}
func (reponsitory OrderBaseRepository) transformPgModelToDomainModel(orderModel *models.OrderBase) (order domain.OrderBase, err error) {
order = domain.OrderBase{
Id: orderModel.Id, OrderType: orderModel.OrderType, OrderCode: orderModel.OrderCode,
DeliveryCode: orderModel.DeliveryCode, Buyer: orderModel.Buyer, RegionInfo: orderModel.RegionInfo,
PartnerId: orderModel.PartnerId, SalesmanBonusPercent: orderModel.SalesmanBonusPercent,
CreateTime: orderModel.CreateTime, DeliveryTime: orderModel.DeliveryTime, UpdateTime: orderModel.UpdateTime,
OrderCompute: domain.OrderCompute{
PlanPartnerBonus: orderModel.PlanPartnerBonus, UsePartnerBonus: orderModel.UsePartnerBonus,
PartnerBonusHas: orderModel.PartnerBonusHas, PartnerBonusNot: orderModel.PartnerBonusNot,
PartnerBonusExpense: orderModel.PartnerBonusExpense, SalesmanBonus: orderModel.SalesmanBonus,
PlanOrderCount: orderModel.PlanOrderCount, PlanOrderAmount: orderModel.PlanOrderAmount,
UseOrderCount: orderModel.UseOrderCount, UseOrderAmount: orderModel.UseOrderAmount,
},
}
return order, nil
}
func (repository OrderBaseRepository) Save(orderInfo *domain.OrderBase) error {
var (
err error
tx = repository.transactionContext.PgTx
)
m := &models.OrderBase{
Id: orderInfo.Id, OrderType: orderInfo.OrderType, OrderCode: orderInfo.OrderCode,
DeliveryCode: orderInfo.DeliveryCode, Buyer: orderInfo.Buyer, RegionInfo: orderInfo.RegionInfo,
PartnerId: orderInfo.PartnerId, SalesmanBonusPercent: orderInfo.SalesmanBonusPercent,
SalesmanBonus: orderInfo.OrderCompute.SalesmanBonus, PlanOrderCount: orderInfo.OrderCompute.PlanOrderCount,
PlanOrderAmount: orderInfo.OrderCompute.PlanOrderAmount, UseOrderCount: orderInfo.OrderCompute.UseOrderCount,
UseOrderAmount: orderInfo.OrderCompute.UseOrderAmount, DeliveryTime: orderInfo.DeliveryTime,
PlanPartnerBonus: orderInfo.OrderCompute.PlanPartnerBonus, UsePartnerBonus: orderInfo.OrderCompute.UsePartnerBonus,
PartnerBonusHas: orderInfo.OrderCompute.PartnerBonusHas, PartnerBonusNot: orderInfo.OrderCompute.PartnerBonusNot,
PartnerBonusExpense: orderInfo.OrderCompute.PartnerBonusExpense,
}
if m.Id == 0 {
_, err = tx.Model(m).
Returning("*").
Insert()
orderInfo.Id = m.Id
orderInfo.CreateTime = m.CreateTime
orderInfo.UpdateTime = m.UpdateTime
} else {
_, err = tx.Model(m).
WherePK().
Returning("*").
Update()
orderInfo.UpdateTime = m.UpdateTime
}
return err
}
func (repository OrderBaseRepository) Find(queryOption domain.OrderFindQuery) ([]domain.OrderBase, int, error) {
db := repository.transactionContext.PgDd
orderModels := []models.OrderBase{}
query := db.Model(&orderModels)
if queryOption.PartnerId > 0 {
query = query.Where("partner_id=?", queryOption.PartnerId)
}
if len(queryOption.OrderCode) > 0 {
query = query.Where("order_code like ?", "%"+queryOption.OrderCode+"%")
}
if len(queryOption.DeliveryCode) > 0 {
query = query.Where("delivery_code like ?", "%"+queryOption.DeliveryCode+"%")
}
if queryOption.OrderType > 0 {
query = query.Where("order_type=?", queryOption.OrderType)
}
if queryOption.Offset > -1 {
query = query.Offset(queryOption.Offset)
}
if queryOption.Limit > 0 {
query = query.Limit(queryOption.Limit)
} else {
query = query.Limit(20)
}
var (
err error
ordersReturn = make([]domain.OrderBase, 0)
)
query = query.Order("order_base.id DESC")
count, err := query.SelectAndCount()
if err != nil {
return ordersReturn, 0, err
}
for i := range orderModels {
domainOrder, err := repository.transformPgModelToDomainModel(&orderModels[i])
if err != nil {
return ordersReturn, 0, err
}
ordersReturn = append(ordersReturn, domainOrder)
}
return ordersReturn, count, nil
}
func (repository OrderBaseRepository) FindOne(qureyOptions domain.OrderFindOneQuery) (*domain.OrderBase, error) {
var (
err error
tx = repository.transactionContext.PgDd
)
m := new(models.OrderBase)
err = tx.Model(m).
Where("id=?", qureyOptions.OrderId).
First()
if err != nil {
return nil, err
}
result, err := repository.transformPgModelToDomainModel(m)
return &result, err
}
func (repository OrderBaseRepository) Remove(id int64) error {
var (
err error
tx = repository.transactionContext.PgTx
)
m := &models.OrderBase{
Id: id,
}
_, err = tx.Model(m).WherePK().Delete()
return err
}
... ...
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 OrderGoodRepository struct {
transactionContext *transaction.TransactionContext
}
var (
_ domain.OrderGoodRepository = (*OrderGoodRepository)(nil)
)
func NewOrderGoodRepository(transactionContext *transaction.TransactionContext) (*OrderGoodRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &OrderGoodRepository{transactionContext: transactionContext}, nil
}
func (reponsitory OrderGoodRepository) transformPgModelToDomainModel(orderModel *models.OrderGood) (order domain.OrderGood, err error) {
order = domain.OrderGood{
Id: orderModel.Id, OrderId: orderModel.OrderId, GoodName: orderModel.GoodName,
PlanGoodNumber: orderModel.PlanGoodNumber, UseGoodNumber: orderModel.UseGoodNumber,
Price: orderModel.Price, PartnerBonusPercent: orderModel.PartnerBonusPercent,
BonusStatus: orderModel.BonusStatus, Remark: orderModel.Remark,
GoodCompute: domain.GoodCompute{
PlanAmount: orderModel.PlanAmount,
UseAmount: orderModel.UseAmount,
PlanPartnerBonus: orderModel.PlanPartnerBonus,
UsePartnerBonus: orderModel.UsePartnerBonus,
PartnerBonusHas: orderModel.PartnerBonusHas,
PartnerBonusNot: orderModel.PartnerBonusNot,
PartnerBonusExpense: orderModel.PartnerBonusExpense,
},
}
switch orderModel.BonusStatus {
case domain.OrderGoodWaitPay:
order.CurrentBonusStatus = domain.OrderGoodBonusWaitPay{}
case domain.OrderGoodHasPay:
order.CurrentBonusStatus = domain.OrderGoodBonusHasPay{}
}
return order, nil
}
func (repository OrderGoodRepository) Save(data []domain.OrderGood) error {
var (
err error
tx = repository.transactionContext.PgTx
)
for k, v := range data {
m := &models.OrderGood{
Id: v.Id, OrderId: v.OrderId, GoodName: v.GoodName,
PlanGoodNumber: v.PlanGoodNumber, Price: v.Price,
PlanAmount: v.GoodCompute.PlanAmount, UseGoodNumber: v.UseGoodNumber,
UseAmount: v.GoodCompute.UseAmount, PartnerBonusPercent: v.PartnerBonusPercent,
PlanPartnerBonus: v.GoodCompute.PlanPartnerBonus, UsePartnerBonus: v.GoodCompute.UsePartnerBonus,
PartnerBonusHas: v.GoodCompute.PartnerBonusHas, PartnerBonusNot: v.GoodCompute.PartnerBonusNot,
PartnerBonusExpense: v.GoodCompute.PartnerBonusExpense, BonusStatus: v.BonusStatus,
Remark: v.Remark,
}
if v.Id == 0 {
_, err = tx.Model(m).
Returning("*").
Insert()
if err != nil {
return err
}
data[k].Id = m.Id
} else {
_, err = tx.Model(m).
WherePK().
Update()
if err != nil {
return err
}
}
}
return nil
}
func (repository OrderGoodRepository) Find(queryOption domain.OrderGoodFindQuery) ([]domain.OrderGood, int, error) {
db := repository.transactionContext.PgDd
goodModes := []models.OrderGood{}
query := db.Model(&goodModes)
if queryOption.OrderId > 0 {
query = query.Where("order_id=?", queryOption.OrderId)
}
if queryOption.Offset > -1 {
query = query.Offset(queryOption.Offset)
}
if queryOption.Limit > 0 {
query = query.Limit(queryOption.Limit)
} else {
query = query.Limit(50)
}
var (
err error
orderGoods = make([]domain.OrderGood, 0)
)
query = query.Order("order_good.id DESC")
count, err := query.SelectAndCount()
if err != nil {
return orderGoods, 0, err
}
for i := range goodModes {
domainOrderGood, err := repository.transformPgModelToDomainModel(&goodModes[i])
if err != nil {
return orderGoods, 0, err
}
orderGoods = append(orderGoods, domainOrderGood)
}
return orderGoods, count, nil
}
func (repository OrderGoodRepository) Remove(orderId int64, goodids ...int64) error {
var (
err error
tx = repository.transactionContext.PgTx
)
m := &models.OrderBase{}
query := tx.Model(m).Where("order_id=?", orderId)
if len(goodids) > 0 {
query = query.WhereIn("id in(?)", goodids)
}
_, err = query.Delete()
return err
}
... ...