作者 唐旭辉

日常更新报存

... ... @@ -14,7 +14,7 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
//OrderService 管理员相关服务
//OrderService 自建订单,意向单,实发订单
type OrderInfoService struct {
}
... ... @@ -151,6 +151,7 @@ func (service OrderInfoService) GetOrderDetail(getOrderQuery query.GetOrderQuery
return order, nil
}
//CreateNewOrder 创建订单
func (service OrderInfoService) CreateNewOrder(cmd command.CreateOrderCommand) (*domain.OrderBase, error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
... ... @@ -436,9 +437,11 @@ func (service OrderInfoService) UpdateOrderData(cmd command.UpdateOrderCommand)
oldOrderData.Goods = newOrderGoods
//删不需要的订单总不需要的商品
delGoods = service.deleteOldOrderGoods(newOrderGoods, oldOrderGoods)
err = orderGoodRepository.Remove(oldOrderData.Id, cmd.CompanyId, delGoods...)
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("删除订单中的商品数据失败:%s", err))
if len(delGoods) > 0 {
err = orderGoodRepository.Remove(oldOrderData.Id, cmd.CompanyId, delGoods...)
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("删除订单中的商品数据失败:%s", err))
}
}
err = transactionContext.CommitTransaction()
if err != nil {
... ...
... ... @@ -41,8 +41,6 @@ type Company struct {
UpdateAt time.Time `json:"updateAt"`
// 删除时间
DeleteAt time.Time `json:"deleteAt"`
//模块授权
// Permission map[string]bool `json:"permission"`
}
func (c Company) EnableIsOk() bool {
... ...
... ... @@ -4,7 +4,8 @@ const (
PAY_ORDER_GOOD_BONUS_EVENT string = "pay-order-good-bonus-event"
)
//PayOrderGoodBonus 事件:支付订单中货品的分红
//WxPayOrderGoodBonus
//事件:支付来自微信小程序的订单中货品的分红
type PayOrderGoodBonus struct {
//订单id
OrderId int64
... ...
... ... @@ -4,21 +4,35 @@ const (
ORDER_GOOD_MODIFY_EVENT string = "order-good-modify-event"
)
//OrderGoodModify 事件:修改订单中货品的数据
type OrderGoodModify struct {
//订单id
OrderId int64
type WxOrderGoodModify struct {
//订单id
GoodId int64
//货品名称
GoodName string
//货品名称 [0]旧值,[1]新值
GoodName [2]string
//商品数量 [0]旧值,[1]新值
GoodNumber [2]string
//合伙人分红比例 [0]旧值,[1]新值
PartnerBonusPercent [2]string
//合伙人应收分红 [0]旧值,[1]新值
PartnerBonus [2]string
//总价
Amount [2]string
}
//OrderModify
//事件:修改微信小程序的订单数据
type WxOrderModify struct {
//订单id
OrderId int64
//管理员id
AdminId int64
//更改的订单基础数据
UpdateGoodData map[string]string
GoodModify []WxOrderGoodModify
//订单备注 [0]旧值,[1]新值
Remark [2]string
}
//EventType 事件名称
func (m OrderGoodModify) EventType() string {
func (m WxOrderModify) EventType() string {
return ORDER_GOOD_MODIFY_EVENT
}
... ...
... ... @@ -26,6 +26,42 @@ type Buyer struct {
ShippingAddress string `json:"shippingAddress"`
}
//OrderDataFrom 订单数据来源
type OrderDataFrom struct {
Platform string `json:"platform"` //订单数据来源平台
PlatformId string `json:"platformId"` //平台唯一标识
DataId int64 `json:"dataId"` //订单数据id标识
}
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"`
}
//OrderBase 订单基础
type OrderBase struct {
//表id
... ... @@ -61,35 +97,61 @@ type OrderBase struct {
BonusStatus int `json:"bonusStatus"`
//公司
CompanyId int64 `json:"companyId"`
//数据来源
DataFrom OrderDataFrom `json:"dataFrom"`
}
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"`
//Update 更新订单数据
//orderData 订单数据
//goodsMap 货品的数据,以货品的id做键,map[id]map[string]interface{}
func (order *OrderBase) Update(orderData map[string]interface{}, goodsMap map[int64]map[string]interface{}) error {
if v, ok := orderData["OrderCode"]; ok {
order.OrderCode = v.(string)
}
if v, ok := orderData["DeliveryCode"]; ok {
order.DeliveryCode = v.(string)
}
if v, ok := orderData["Buyer"]; ok {
order.Buyer = v.(Buyer)
}
if v, ok := orderData["SalesmanBonusPercent"]; ok {
order.SalesmanBonusPercent = v.(float64)
}
if v, ok := orderData["DeliveryTime"]; ok {
order.DeliveryTime = v.(time.Time)
}
if v, ok := orderData["IsDisable"]; ok {
order.IsDisable = v.(int)
}
for i := range order.Goods {
goodId := order.Goods[i].Id
if _, ok := goodsMap[goodId]; !ok {
continue
}
if err := order.Goods[i].Update(goodsMap[goodId]); err != nil {
return err
}
}
err := order.Compute()
return err
}
func (order *OrderBase) AddGoods(goods []OrderGood) {
order.Goods = append(order.Goods, goods...)
order.Compute()
}
func (order *OrderBase) DeleteGoods(goodIds []int64) {
var newGoods []OrderGood
for i := range order.Goods {
for _, goodId := range goodIds {
order.Goods[i].Id = goodId
continue
}
newGoods = append(newGoods, order.Goods[i])
}
order.Goods = newGoods
order.Compute()
}
//Compute 数据汇总核算
... ...
... ... @@ -22,6 +22,28 @@ const (
OrderGoodHasPay int = 2
)
//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"`
}
//OrderGood 订单中的货品
type OrderGood struct {
//货品id
... ... @@ -52,25 +74,28 @@ type OrderGood struct {
CompanyId int64
}
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"`
//Update 更新商品相关的数据
func (good *OrderGood) Update(m map[string]interface{}) error {
if v, ok := m["GoodName"]; ok {
good.GoodName = v.(string)
}
if v, ok := m["PlanGoodNumber"]; ok {
good.PlanGoodNumber = v.(int)
}
if v, ok := m["UseGoodNumber"]; ok {
good.UseGoodNumber = v.(int)
}
if v, ok := m["Price"]; ok {
good.Price = v.(float64)
}
if v, ok := m["PartnerBonusPercent"]; ok {
good.PartnerBonusPercent = v.(float64)
}
if v, ok := m["Remark"]; ok {
good.Remark = v.(string)
}
err := good.Compute()
return err
}
//OrderGoodBonusWaitPay 货品分红待支付
... ... @@ -177,9 +202,12 @@ type OrderGoodFindQuery struct {
Limit int
CompanyId int64
}
type OrderGoodFindOneQuery struct {
GoodId int64
}
type OrderGoodRepository interface {
Save(order []OrderGood) error
Find(queryOptions OrderGoodFindQuery) ([]OrderGood, int, error)
FindOne(queryOptions OrderGoodFindOneQuery) (OrderGood, error)
Remove(orderid int64, companyId int64, ids ...int64) error
}
... ...
package domain
import "time"
// 日志数据来源
const (
//管理后台
ORDER_LOG_FROM string = "web_admin"
)
//操作人员的类型
const (
//操作人类型
ORDER_LOG_OPERATOR_ADMIN string = "admin"
)
//OrderLogDescript 描述日志内容
type OrderLogDescript struct {
Title string `json:"title"` //标题
Action string `json:"action"` //执行的动作描述
Modifyitem []string `json:"modifyItem"` //动作执行结果
}
//OrderLogContentItem 记录订单的相关属性值的修改
type OrderLogContentItem struct {
Table string `json:"table"` //修改的表数据主体,"order_good","order_base"
TableId string `json:"tableId"` //表id
Title string `json:"title"` //名称标题
Item string `json:"item"` //修改的项目
FormerValue string `json:"formerValue"` //旧值
NewValue string `json:"newValue"` //新值
}
//OrderLog 订单修改记录
type OrderLog struct {
OrderId int64 `json:"order_id"` //订单id
AlterTime time.Time `json:"alter_time"` //时间
Operator string `json:"operator"` //操作人员
OperatorType string `json:"operatorType"` //操作人员的类型
LogAction string `json:"logAction"` //执行动作
Descript []OrderLogDescript `json:"descript"` //描述日志内容
Content []OrderLogContentItem `json:"content"` //记录订单的操作动作
DataFrom string `json:"dataFrom"` //修改操作的来源:"web_admin"
}
... ...
package domainService
import (
"fmt"
coreDomain "github.com/linmadan/egglib-go/core/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain/event"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
type OrderServices struct {
//WxOrderServices 处理来自微信小程序(海鲜干货)的订单
type WxOrderServices struct {
coreDomain.BaseEventPublisher
transactionContext *transaction.TransactionContext
}
//UpdateOrderGoodData 修改订单中的货品数据
func (serve OrderServices) UpdateOrderGoodData(orderId int64, goodId int64, adminId int64, updateData map[string]string) error {
var err error
//ModifyOrderGoodData 修改订单中的单个货品的数量
func (serve WxOrderServices) ModifyOrderGoodNumber(orderId int64, goodId int64, adminId int64, goodNumber int) error {
var (
userRepository domain.UsersRepository
orderBaseReponsitory domain.OrderBaseRepository
orderGoodRepository domain.OrderGoodRepository
oldOrderGoods domain.OrderGood
oldOrder *domain.OrderBase
adminUser domain.Users
err error
)
if orderGoodRepository, err = repository.NewOrderGoodRepository(serve.transactionContext); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if orderBaseReponsitory, err = repository.NewOrderBaseRepository(serve.transactionContext); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if userRepository, err = repository.NewUsersRepository(serve.transactionContext); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
oldOrder, err = orderBaseReponsitory.FindOne(domain.OrderBaseFindOneQuery{OrderId: orderId})
if err != nil {
e := fmt.Sprintf("获取订单(id=%d)数据失败,%s", orderId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
oldOrder.Goods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{OrderId: orderId})
if err != nil {
e := fmt.Sprintf("获取订单中的货品(id=%d)数据失败,%s", goodId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
adminUser, err = userRepository.FindOne(domain.UsersFindOneQuery{Id: adminId})
if err != nil {
e := fmt.Sprintf("获取管理员用户(id=%d)数据失败,%s", adminId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
if adminUser.CompanyId != oldOrderGoods.CompanyId {
e := fmt.Sprintf("获取管理员用户(id=%d)和货品(id=%d)不在同一家公司", adminId, goodId)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
//TODO数据更新操作
//事件发布
modifyEvent := event.OrderGoodModify{
OrderId: orderId,
GoodId: goodId,
AdminId: adminId,
UpdateGoodData: updateData,
// modifyEvent := event.OrderModify{
// OrderId: oldOrderGoods.OrderId,
// AdminId: adminId,
// //
// }
// if err = serve.Publish(modifyEvent); err != nil {
// return err
// }
return nil
}
//ModifyOrderGoodData 修改订单中的单个货品的合伙人分成比例
func (serve WxOrderServices) ModifyOrderGoodPartnerBonusPercent(orderId int64, goodId int64, adminId int64, goodNumber int) error {
var (
userRepository domain.UsersRepository
orderBaseReponsitory domain.OrderBaseRepository
orderGoodRepository domain.OrderGoodRepository
oldOrderGoods domain.OrderGood
oldOrder *domain.OrderBase
adminUser domain.Users
err error
)
if orderGoodRepository, err = repository.NewOrderGoodRepository(serve.transactionContext); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if err = serve.Publish(modifyEvent); err != nil {
return err
if orderBaseReponsitory, err = repository.NewOrderBaseRepository(serve.transactionContext); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if userRepository, err = repository.NewUsersRepository(serve.transactionContext); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
oldOrder, err = orderBaseReponsitory.FindOne(domain.OrderBaseFindOneQuery{OrderId: orderId})
if err != nil {
e := fmt.Sprintf("获取订单(id=%d)数据失败,%s", orderId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
oldOrder.Goods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{OrderId: orderId})
if err != nil {
e := fmt.Sprintf("获取订单中的货品(id=%d)数据失败,%s", goodId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
adminUser, err = userRepository.FindOne(domain.UsersFindOneQuery{Id: adminId})
if err != nil {
e := fmt.Sprintf("获取管理员用户(id=%d)数据失败,%s", adminId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
if adminUser.CompanyId != oldOrderGoods.CompanyId {
e := fmt.Sprintf("获取管理员用户(id=%d)和货品(id=%d)不在同一家公司", adminId, goodId)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
//TODO数据更新操作
//事件发布
// modifyEvent := event.OrderModify{
// OrderId: oldOrderGoods.OrderId,
// AdminId: adminId,
// //
// }
// if err = serve.Publish(modifyEvent); err != nil {
// return err
// }
return nil
}
//PayOrderGoodBonus 支付订单中货品的分红
func (serve OrderServices) PayOrderGoodBonus(orderId int64, goodId int64, adminId int64) error {
func (serve WxOrderServices) PayOrderGoodBonus(goodId int64, adminId int64) error {
var err error
//TODO
payEvent := event.PayOrderGoodBonus{
OrderId: orderId,
GoodId: goodId,
AdminId: adminId,
}
... ...
... ... @@ -63,6 +63,9 @@ type OrderBase struct {
//分红支付状态
BonusStatus int
CompanyId int64
//数据来源
DataFrom domain.OrderDataFrom ``
}
var _ pg.BeforeUpdateHook = (*OrderBase)(nil)
... ...
package models
import (
"time"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
)
//OrderLog 订单修改记录
type OrderLog struct {
tableName struct{} `pg:"order_log"`
OrderId int64 `` //订单id
AlterTime time.Time `` //时间
Operator string `` //操作人员
OperatorType string `` //操作人员的类型
Action string `` //执行动作
Descript []domain.OrderLogDescript `` //描述日志内容
//Content []domain.OrderLogContent `` //记录订单的操作动作
DataFrom string `` //修改操作的来源:"web_admin"
}
... ...
package repository
import (
"errors"
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
... ... @@ -138,3 +139,27 @@ func (repository OrderGoodRepository) Remove(orderId int64, companyId int64, goo
_, err = query.Delete()
return err
}
func (repository OrderGoodRepository) FindOne(queryOption domain.OrderGoodFindOneQuery) (domain.OrderGood, error) {
var (
good domain.OrderGood
goodModel models.OrderGood
err error
hasCondition bool
)
tx := repository.transactionContext.GetDB()
query := tx.Model(&goodModel)
if queryOption.GoodId > 0 {
query = query.Where("id=?", queryOption.GoodId)
hasCondition = true
}
if !hasCondition {
return good, errors.New("OrderGoodRepository.FindOne 必须使用查询条件")
}
err = query.First()
if err != nil {
return good, fmt.Errorf("获取订单的货品数据失败,%s", err)
}
good, err = repository.transformPgModelToDomainModel(&goodModel)
return good, err
}
... ...
package repository
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
//订单日志
type OrderLogRepository struct {
transactionContext *transaction.TransactionContext
}
func NewOrderInfoRepository(transactionContext *transaction.TransactionContext) (*OrderLogRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &OrderLogRepository{transactionContext: transactionContext}, nil
}
func (reponsitory OrderLogRepository) transformPgModelToDomainModel() (data domain.OrderLog, err error) {
return domain.OrderLog{}, nil
}
... ...
... ... @@ -141,7 +141,7 @@ func (reponsitory UsersRepository) FindOne(queryOptions domain.UsersFindOneQuery
query = query.Where("open_id=?", queryOptions.OpenId)
}
if !hasCondition {
return domain.Users{}, errors.New("FindOne 必须要有查询条件")
return domain.Users{}, errors.New("UsersRepository.FindOne 必须要有查询条件")
}
err = query.First()
if err != nil {
... ...