order_info_controlller.go 7.3 KB
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()
	return
	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:"buyer"`
	//对应合伙人 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
}