作者 唐旭辉

日常提交报存

package command
import "time"
//接收海鲜干货的订单
type CreateOrderFromBestshop struct {
//订单编号
OrderCode string `json:"orderCode"`
//下单时间
OrderTime string `json:"orderTime"`
//订单状态
OrderState int8 `json:"orderState"`
//发货状态
DeliveryState int8 `json:"deliveryState"`
//买家名称
BuyerName string `json:"buyerName"`
BuyerId int64 `json:"buyerId"`
//买家电话
BuyerPhone string `json:"buyerPhone"`
//买家地址
BuyerAddress string `json:"buyerAddress"`
//买家备注
BuyerRemark string `json:"buyerRemark"`
//商品总数
OrderCount int `json:"orderCount"`
//d订单总额
OrderAmount float64 `json:"orderAmount"`
//发货时间
DeliveryTime time.Time `json:"deliveryTime"`
PartnerId int64 `json:"partnerId"`
Goods []struct {
//货品编号
Sn string `json:"sn"`
//商品编号
Bn string `json:"bn"`
//货品名称
Name string `json:"name"`
//单价
Price float64 `json:"price"`
//货品数量
Nums int `json:"nums"`
//订单总价
Amount float64 `json:"amount"`
} `json:"goods"`
}
... ...
package service
import (
"fmt"
"time"
"github.com/astaxie/beego/logs"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/syncOrder/command"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
//从其他系统接收订单数据
const (
BEST_SHOP_UNIONID string = "gh_5268964adfe3" //海鲜干货小程序原始id
)
type SyncOrderService struct {
}
func NewOrderInfoService(option map[string]interface{}) *SyncOrderService {
newService := new(SyncOrderService)
return newService
}
//SyncOrderFromBestshop 接收来源于海鲜干货小程序的订单数据
func (s SyncOrderService) SyncOrderFromBestshop(cmd command.CreateOrderFromBestshop) 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 (
orderBestshopRepository domain.OrderBestshopRepository
orderGoodBestshopRepository domain.OrderGoodBestshopRepository
)
if orderBestshopRepository, err = factory.CreateOrderBestshopRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if orderGoodBestshopRepository, err = factory.CreateOrderGoodBestshopRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
order := domain.OrderBestShop{
OrderCode: cmd.OrderCode,
OrderTime: cmd.OrderTime,
OrderState: cmd.OrderState,
OrderCount: cmd.OrderCount,
OrderAmount: cmd.OrderAmount,
CreateTime: time.Now(),
PartnerId: cmd.PartnerId,
BuyerName: cmd.BuyerName,
BuyerPhone: cmd.BuyerPhone,
BuyerAddress: cmd.BuyerAddress,
BuyerRemark: cmd.BuyerRemark,
BuyerId: cmd.BuyerId,
DeliveryState: cmd.DeliveryState,
DeliveryTime: cmd.DeliveryTime,
IsCopy: false,
}
err = orderBestshopRepository.Add(&order)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "添加order_bestshop失败:"+err.Error())
}
goods := []domain.OrderGoodBestShop{}
for i := range cmd.Goods {
good := domain.OrderGoodBestShop{
OrderId: order.Id,
Sn: cmd.Goods[i].Sn,
Bn: cmd.Goods[i].Bn,
Name: cmd.Goods[i].Name,
Price: cmd.Goods[i].Price,
Nums: cmd.Goods[i].Nums,
Amount: cmd.Goods[i].Amount,
}
err = orderGoodBestshopRepository.Add(&good)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "添加order_good失败:"+err.Error())
}
goods = append(goods, good)
}
order.Goods = goods
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = s.copyOrderBestshopToOrderBase(&order)
return err
}
//copyOrderBestshopToOrderBase 复制数据
func (s SyncOrderService) copyOrderBestshopToOrderBase(orderBestshop *domain.OrderBestShop) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
orderBaseRepository domain.OrderBaseRepository
orderGoodRepository domain.OrderGoodRepository
orderBestshopRepository domain.OrderBestshopRepository
partnerRepository domain.PartnerInfoRepository
companyRepository domain.CompanyRepository
)
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())
}
if partnerRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if companyRepository, err = factory.CreateCompanyRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if orderBestshopRepository, err = factory.CreateOrderBestshopRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var (
partnerData *domain.PartnerInfo
companyData domain.Company
canCopyOrder bool
)
partnerData, err = partnerRepository.FindOne(domain.PartnerFindOneQuery{UserId: orderBestshop.Id})
if err != nil {
e := fmt.Sprintf("未找到指定的合伙人(id=%d)数据,%s", orderBestshop.PartnerId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
companyData, err = companyRepository.FindOne(domain.CompanyFindOneOptions{
Id: partnerData.CompanyId,
})
if err != nil {
e := fmt.Sprintf("未找到指定的合伙人的公司(partner_id=%d,company_id=%d)数据,%s", orderBestshop.PartnerId, partnerData.CompanyId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
for _, v := range companyData.Applets {
if v.Id == BEST_SHOP_UNIONID {
canCopyOrder = true
}
}
if !canCopyOrder {
logs.Warning("公司未设置海鲜干货的小程序原始id; order_bestshop.id=%d", orderBestshop.Id)
return nil
}
var (
orderbase domain.OrderBase
ordergoods []domain.OrderGood
)
//TODO 添加orderBase
err = orderBaseRepository.Save(&orderbase)
if err != nil {
e := fmt.Sprintf("添加order_base数据失败%s", err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
//TODO 添加goods
err = orderGoodRepository.Save(ordergoods)
if err != nil {
e := fmt.Sprintf("添加order_good数据失败%s", err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
//TODO 更新isCopy
err = orderBestshopRepository.Edit(orderBestshop)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
... ...
package syncOrder
//从其他系统接收订单数据
//接收来源于海鲜干货小程序的订单数据
... ... @@ -10,9 +10,9 @@ type OrderBestShop struct {
//下单时间
OrderTime string `json:"orderTime"`
//订单状态
OrderState int8 `json:"order_state"`
OrderState int8 `json:"orderState"`
//发货状态
DeliveryState int8 `json:"delivery_state"`
DeliveryState int8 `json:"deliveryState"`
//买家名称
BuyerName string `json:"buyerName"`
//买家电话
... ... @@ -21,6 +21,8 @@ type OrderBestShop struct {
BuyerAddress string `json:"buyerAddress"`
//买家备注
BuyerRemark string `json:"buyerRemark"`
//
BuyerId int64 `json:"buyerId"`
//商品总数
OrderCount int `json:"orderCount"`
//d订单总额
... ... @@ -29,7 +31,10 @@ type OrderBestShop struct {
DeliveryTime time.Time `json:"deliveryTime"`
//创建时间
CreateTime time.Time `json:"createTime"`
PartnerId int64 `json:"partnerId"`
Goods []OrderGoodBestShop `json:"goods"`
//是否将数据同步到 order_base ,order_good
IsCopy bool `json:"isCopy"`
}
func (order OrderBestShop) CopyToOrderBase(o *OrderBase) {
... ... @@ -56,6 +61,7 @@ type OrderBestshopFindOneQuery struct {
type OrderBestshopRepository interface {
Add(order *OrderBestShop) error
Edit(order *OrderBestShop) error
FindOne(qureyOptions OrderBestshopFindOneQuery) (*OrderBestShop, error)
}
... ...
... ... @@ -21,6 +21,8 @@ type OrderBestshop struct {
BuyerAddress string
//买家备注
BuyerRemark string
//
BuyerId int64
//订单总数
OrderCount int
//d订单总额
... ... @@ -29,4 +31,7 @@ type OrderBestshop struct {
DeliveryTime time.Time
//创建时间
CreateTime time.Time
PartnerId int64
//是否将数据同步到 order_base ,order_good
IsCopy bool `pg:",use_zero"`
}
... ...
... ... @@ -35,10 +35,13 @@ func (respository OrderBestshopRepository) transformPgModelToDomainModel(orderMo
BuyerPhone: orderModel.BuyerPhone,
BuyerAddress: orderModel.BuyerAddress,
BuyerRemark: orderModel.BuyerRemark,
BuyerId: orderModel.BuyerId,
OrderCount: orderModel.OrderCount,
OrderAmount: orderModel.OrderAmount,
DeliveryTime: orderModel.DeliveryTime,
CreateTime: orderModel.CreateTime,
PartnerId: orderModel.PartnerId,
IsCopy: orderModel.IsCopy,
}, nil
}
... ... @@ -53,16 +56,44 @@ func (respository OrderBestshopRepository) Add(order *domain.OrderBestShop) erro
BuyerPhone: order.BuyerPhone,
BuyerAddress: order.BuyerAddress,
BuyerRemark: order.BuyerRemark,
BuyerId: order.BuyerId,
OrderCount: order.OrderCount,
OrderAmount: order.OrderAmount,
DeliveryTime: order.DeliveryTime,
CreateTime: time.Now(),
PartnerId: order.PartnerId,
IsCopy: order.IsCopy,
}
_, err := tx.Model(&m).Insert()
order.Id = m.Id
return err
}
func (respository OrderBestshopRepository) Edit(order *domain.OrderBestShop) error {
tx := respository.transactionContext.GetDB()
m := models.OrderBestshop{
Id: order.Id,
OrderCode: order.OrderCode,
OrderTime: order.OrderTime,
OrderState: order.OrderState,
DeliveryState: order.DeliveryState,
BuyerName: order.BuyerName,
BuyerPhone: order.BuyerPhone,
BuyerAddress: order.BuyerAddress,
BuyerRemark: order.BuyerRemark,
BuyerId: order.BuyerId,
OrderCount: order.OrderCount,
OrderAmount: order.OrderAmount,
DeliveryTime: order.DeliveryTime,
CreateTime: order.CreateTime,
PartnerId: order.PartnerId,
IsCopy: order.IsCopy,
}
_, err := tx.Model(&m).Where("id=?", order.Id).Update()
order.Id = m.Id
return err
}
func (respository OrderBestshopRepository) FindOne(queryOption domain.OrderBestshopFindOneQuery) (*domain.OrderBestShop, error) {
tx := respository.transactionContext.GetDB()
m := models.OrderBestshop{}
... ...