作者 tangxvhui

更新意向订单

... ... @@ -7,16 +7,10 @@ type OrderGoodData struct {
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
//UpdateOrderPurposeCommand 更新意向单
type UpdateOrderCommand struct {
//订单类型
OrderType int `json:"orderType"`
Id int64 `json:"id"`
//订单编号
OrderCode string `json:"orderCode"`
//交货编号
... ... @@ -15,6 +15,8 @@ type UpdateOrderCommand struct {
PartnerId int64 `json:"partnerId"`
//业务员抽成比例
SalesmanBonusPercent float64 `json:"salesmanBonusPercent"`
//订单类型
OrderType int `json:"orderType"`
//货品
Goods []OrderGoodData `json:"goods"`
}
... ...
package query
//ListOrderQuery 获取用户列表
type ListOrderQuery struct {
type ListOrderBaseQuery struct {
//合伙人id
PartnerId int64 `json:"partnerId" `
//订单编号
... ...
... ... @@ -20,7 +20,7 @@ func NewOrderInfoService(option map[string]interface{}) *OrderInfoService {
}
// PageListOrderBase 获取订单列表
func (service OrderInfoService) PageListOrderBase(listOrderQuery query.ListOrderQuery) ([]domain.OrderBase, int, error) {
func (service OrderInfoService) PageListOrderBase(listOrderQuery query.ListOrderBaseQuery) ([]domain.OrderBase, int, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, 0, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
... ... @@ -37,7 +37,7 @@ func (service OrderInfoService) PageListOrderBase(listOrderQuery query.ListOrder
} else {
orderRepository = value
}
query := domain.OrderFindQuery{
query := domain.OrderBaseFindQuery{
PartnerId: listOrderQuery.PartnerId,
OrderCode: listOrderQuery.OrderCode,
Offset: listOrderQuery.Offset,
... ... @@ -103,11 +103,11 @@ func (service OrderInfoService) GetOrderDetail(getOrderQuery query.GetOrderQuery
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
order, err = orderBaseRepository.FindOne(domain.OrderFindOneQuery{
order, err = orderBaseRepository.FindOne(domain.OrderBaseFindOneQuery{
OrderId: getOrderQuery.OrderId,
})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取订单数据失败:%s", err))
}
var (
partnerData *domain.PartnerInfo
... ... @@ -115,7 +115,7 @@ func (service OrderInfoService) GetOrderDetail(getOrderQuery query.GetOrderQuery
)
partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: order.PartnerInfo.Id})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("检索合伙人数据失败:%s", err))
}
order.PartnerInfo = partnerData.Partner
goods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{
... ... @@ -193,7 +193,7 @@ func (service OrderInfoService) CreateNewOrder(cmd command.CreateOrderCommand) (
var orderGoods []domain.OrderGood
for _, good := range cmd.Goods {
m := domain.NewOrderGood()
m.OrderId = newOrder.Id
m.OrderId = 0
m.GoodName = good.GoodName
m.PlanGoodNumber = good.PlanGoodNumber
m.Price = good.Price
... ... @@ -214,6 +214,9 @@ func (service OrderInfoService) CreateNewOrder(cmd command.CreateOrderCommand) (
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单数据失败:%s", err))
}
for i := range newOrder.Goods {
newOrder.Goods[i].OrderId = newOrder.Id
}
err = orderGoodRepository.Save(orderGoods)
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单中的商品数据失败:%s", err))
... ... @@ -266,3 +269,135 @@ func (service OrderInfoService) DeleteOrder(orderId int64) error {
}
return nil
}
//发货
//编辑订单
func (service OrderInfoService) UpdateOrderData(cmd command.UpdateOrderCommand) (*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
oldOrderData *domain.OrderBase
oldOrderGoods []domain.OrderGood
newOrderGoods []domain.OrderGood
delGoods []int64
)
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())
}
//获取旧的订单
oldOrderData, err = orderBaseRepository.FindOne(domain.OrderBaseFindOneQuery{
OrderId: cmd.Id,
})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("未找到指定的订单:%s", err))
}
//获取旧的订单中的商品
oldOrderGoods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{
OrderId: cmd.Id,
})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("未找到指定的订单中的商品列表失败:%s", err))
}
for _, good := range cmd.Goods {
m := domain.NewOrderGood()
m.OrderId = oldOrderData.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))
}
newOrderGoods = append(newOrderGoods, m)
}
oldOrderData.OrderCode = cmd.OrderCode
oldOrderData.DeliveryCode = cmd.DeliveryCode
oldOrderData.Buyer.BuyerName = cmd.BuyerName
oldOrderData.RegionInfo.RegionName = cmd.OrderRegion
oldOrderData.PartnerId = cmd.PartnerId
oldOrderData.PartnerInfo = partnerData.Partner
oldOrderData.SalesmanBonusPercent = cmd.SalesmanBonusPercent
oldOrderData.Goods = newOrderGoods
err = oldOrderData.Compute()
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中合计的数值失败:%s", err))
}
err = orderBaseRepository.Save(oldOrderData)
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单数据失败:%s", err))
}
err = orderGoodRepository.Save(newOrderGoods)
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单中的商品数据失败:%s", err))
}
oldOrderData.Goods = newOrderGoods
//删不需要的订单总不需要的商品
delGoods = service.deleteOldOrderGoods(newOrderGoods, oldOrderGoods)
err = orderGoodRepository.Remove(oldOrderData.Id, delGoods...)
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("删除订单中的商品数据失败:%s", err))
}
err = transactionContext.CommitTransaction()
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return oldOrderData, nil
}
//新旧商品列表对比
func (service OrderInfoService) deleteOldOrderGoods(newGoods []domain.OrderGood, oldGoods []domain.OrderGood) (goodIds []int64) {
for _, old := range oldGoods {
var hasIn bool
for _, new := range newGoods {
if old.Id == new.Id {
hasIn = true
break
}
}
if !hasIn {
goodIds = append(goodIds, old.Id)
}
}
return
}
... ...
... ... @@ -148,16 +148,17 @@ type OrderBaseFindOneQuery struct {
}
type OrderBaseFindQuery struct {
PartnerId int64
OrderCode string
Offset int
Limit int
OrderType int
PartnerId int64
OrderCode string
DeliveryCode 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)
FindOne(qureyOptions OrderBaseFindOneQuery) (*OrderBase, error)
Find(queryOptions OrderBaseFindQuery) ([]OrderBase, int, error)
Remove(id int64) error
}
... ...
... ... @@ -73,7 +73,7 @@ func (repository OrderBaseRepository) Save(orderInfo *domain.OrderBase) error {
return err
}
func (repository OrderBaseRepository) Find(queryOption domain.OrderFindQuery) ([]domain.OrderBase, int, error) {
func (repository OrderBaseRepository) Find(queryOption domain.OrderBaseFindQuery) ([]domain.OrderBase, int, error) {
db := repository.transactionContext.PgDd
orderModels := []models.OrderBase{}
query := db.Model(&orderModels)
... ... @@ -116,7 +116,7 @@ func (repository OrderBaseRepository) Find(queryOption domain.OrderFindQuery) ([
return ordersReturn, count, nil
}
func (repository OrderBaseRepository) FindOne(qureyOptions domain.OrderFindOneQuery) (*domain.OrderBase, error) {
func (repository OrderBaseRepository) FindOne(qureyOptions domain.OrderBaseFindOneQuery) (*domain.OrderBase, error) {
var (
err error
tx = repository.transactionContext.PgDd
... ...
package controllers
import (
"errors"
"strconv"
"github.com/astaxie/beego/logs"
orderCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/command"
orderQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/query"
orderService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/service"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
)
type OrderInfoController struct {
BaseController
}
////Prepare 重写 BaseController 的Prepare方法
func (c *OrderInfoController) Prepare() {
c.BaseController.Prepare()
if ok := c.ValidJWTToken(); !ok {
return
}
if ok := c.ValidAdminPermission(domain.PERMISSION_ORDER); !ok {
return
}
}
//PageListOrderPurpose 分页获取意向订单列表
func (c *OrderInfoController) PageListOrderPurpose() {
type Parameter struct {
SearchText string `json:"searchText"`
Partner int64 `json:"partner"`
PageSize int `json:"pageSize"`
PageNumber int `json:"pageNumber"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
if param.PageNumber == 0 {
param.PageNumber = 1
}
if param.PageSize == 0 {
param.PageSize = 20
}
orderSrv := orderService.NewOrderInfoService(nil)
orderinfos, cnt, err := orderSrv.PageListOrderBase(orderQuery.ListOrderBaseQuery{
PartnerId: param.Partner,
OrderCode: param.SearchText,
OrderType: domain.OrderIntention,
Limit: param.PageSize,
Offset: (param.PageNumber - 1) * param.PageSize,
})
if err != nil {
c.ResponseError(err)
return
}
rsp := []map[string]interface{}{}
for i := range orderinfos {
orderinfo := orderinfos[i]
m := map[string]interface{}{
"createTime": orderinfo.CreateTime.Local().Format("2006-01-02 15:04:05"),
"updateTime": orderinfo.UpdateTime.Local().Format("2006-01-02 15:04:05"),
"buyer": orderinfo.Buyer.BuyerName,
"id": orderinfo.Id,
"orderId": orderinfo.OrderCode,
"partner": orderinfo.PartnerInfo.PartnerName,
"orderNum": orderinfo.OrderCompute.PlanOrderCount,
"orderPrice": orderinfo.OrderCompute.PlanOrderAmount,
"orderDist": orderinfo.RegionInfo.RegionName,
}
rsp = append(rsp, m)
}
c.ResponsePageList(rsp, cnt, param.PageNumber)
}
//GetOrderPurpose 获取意向订单详情
func (c *OrderInfoController) GetOrderPurpose() {
type Parameter struct {
Id string `json:"id"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
orderid, _ := strconv.ParseInt(param.Id, 10, 64)
orderSrv := orderService.NewOrderInfoService(nil)
orderinfo, err := orderSrv.GetOrderDetail(orderQuery.GetOrderQuery{
OrderId: orderid,
})
if err != nil {
c.ResponseError(err)
return
}
allGoods := []map[string]interface{}{}
for _, v := range orderinfo.Goods {
m := map[string]interface{}{
"productName": v.GoodName,
"orderNum": v.PlanGoodNumber,
"univalence": v.Price,
"partnerRatio": v.PartnerBonusPercent,
"orderPrice": v.GoodCompute.PlanAmount,
}
allGoods = append(allGoods, m)
}
rsp := map[string]interface{}{
"buyer": orderinfo.Buyer.BuyerName,
"id": orderinfo.Id,
"partnerID": orderinfo.PartnerInfo.Id,
"partner": orderinfo.PartnerInfo.PartnerName,
"orderDist": orderinfo.RegionInfo.RegionName,
"orderId": orderinfo.OrderCode,
"product": allGoods,
"commissionProportion": orderinfo.SalesmanBonusPercent,
"orderNumCount": orderinfo.OrderCompute.PlanOrderCount,
"orderAmountAdjustmentCount": orderinfo.OrderCompute.PlanOrderAmount,
}
c.ResponseData(rsp)
}
//RemoveOrderPurpose 删除意向订单
func (c *OrderInfoController) RemoveOrderPurpose() {
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
}
orderSrv := orderService.NewOrderInfoService(nil)
err = orderSrv.DeleteOrder(param.Id)
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
}
//PostPurposeOrderDetail 请求添加/更新的订单数据
type postPurposeOrderDetail struct {
Id int64 `json:"id"`
//订单编号
OrderId string `json:"orderId"`
//买家姓名
BuyerName string `json:"buyerName"`
//对应合伙人 id
PartnerId int64 `json:"partnerID"`
PartnerName string `json:"partnerName"`
//业务员抽成比例
CommissionProportion float64 `json:"commissionProportion"`
//订单区域
OrderDist string `json:"orderDist"`
//商品
Product []postPurposeOrderGood `json:"product"`
}
//PostPurposeOrderGood 请求添加/更新订单中的货品
type postPurposeOrderGood struct {
Id int64 `json:"id"`
ProductName string `json:"productName"`
OrderNum int `json:"orderNum"`
Univalence float64 `json:"univalence"`
PartnerRatio float64 `json:"partnerRatio"`
}
//UpdateOrderPurpose 更新意向订单
func (c *OrderInfoController) UpdateOrderPurpose() {
//用与适配前端定义的数据结构
var (
param postPurposeOrderDetail
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
if param.Id == 0 {
err = c.addOrderPurpose(param)
} else {
err = c.editOrderPurpose(param)
}
if err != nil {
c.ResponseError(err)
}
c.ResponseData(nil)
return
}
func (c *OrderInfoController) addOrderPurpose(param postPurposeOrderDetail) error {
orderSrv := orderService.NewOrderInfoService(nil)
newGoods := []orderCmd.OrderGoodData{}
for _, v := range param.Product {
g := orderCmd.OrderGoodData{
GoodName: v.ProductName,
PlanGoodNumber: v.OrderNum,
Price: v.Univalence,
PartnerBonusPercent: v.PartnerRatio,
}
newGoods = append(newGoods, g)
}
createcmd := orderCmd.CreateOrderCommand{
OrderType: domain.OrderIntention,
OrderCode: param.OrderId,
DeliveryCode: "",
BuyerName: param.BuyerName,
OrderRegion: param.OrderDist,
PartnerId: param.PartnerId,
SalesmanBonusPercent: param.CommissionProportion,
Goods: newGoods,
}
_, err := orderSrv.CreateNewOrder(createcmd)
return err
}
func (c *OrderInfoController) editOrderPurpose(param postPurposeOrderDetail) error {
orderSrv := orderService.NewOrderInfoService(nil)
newGoods := []orderCmd.OrderGoodData{}
for _, v := range param.Product {
g := orderCmd.OrderGoodData{
GoodName: v.ProductName,
PlanGoodNumber: v.OrderNum,
Price: v.Univalence,
PartnerBonusPercent: v.PartnerRatio,
}
newGoods = append(newGoods, g)
}
updatecmd := orderCmd.UpdateOrderCommand{
Id: param.Id,
OrderType: domain.OrderIntention,
OrderCode: param.OrderId,
DeliveryCode: "",
BuyerName: param.BuyerName,
OrderRegion: param.OrderDist,
PartnerId: param.PartnerId,
SalesmanBonusPercent: param.CommissionProportion,
Goods: newGoods,
}
_, err := orderSrv.UpdateOrderData(updatecmd)
return err
}
... ...
... ... @@ -46,5 +46,15 @@ func init() {
beego.NSRouter("/partner", &controllers.CommonController{}, "POST:GetPartnerList"),
),
)
adminRouterV2 := beego.NewNamespace("/v2",
beego.NSNamespace("/order",
beego.NSRouter("/purpose/list", &controllers.OrderInfoController{}, "POST:PageListOrderPurpose"),
beego.NSRouter("/purpose/update", &controllers.OrderInfoController{}, "POST:UpdateOrderPurpose"),
beego.NSRouter("/purpose/detail", &controllers.OrderInfoController{}, "POST:GetOrderPurpose"),
beego.NSRouter("/purpose/del", &controllers.OrderInfoController{}, "POST:RemoveOrderPurpose"),
),
)
beego.AddNamespace(adminRouter)
beego.AddNamespace(adminRouterV2)
}
... ...