order_dividend_controller.go 7.9 KB
package controllers

import (
	"errors"
	"fmt"
	"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"
)

//OrderDividendController 订单分红管理
type OrderDividendController struct {
	BaseController
}

//Prepare 重写 BaseController 的Prepare方法
func (c *OrderDividendController) Prepare() {
	c.BaseController.Prepare()
	return
	if ok := c.ValidJWTToken(); !ok {
		return
	}
	if ok := c.ValidAdminPermission(domain.PERMISSION_DIVIDEND); !ok {
		return
	}
}

//PageListOrderDividend 获取实发订单分红列表
func (c *OrderDividendController) PageListOrderDividend() {
	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,
		DeliveryCode: param.SearchText,
		OrderType:    domain.OrderReal,
		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{}{
			"updateTime":           orderinfo.UpdateTime.Local().Format("2006-01-02 15:04:05"),
			"id":                   orderinfo.Id,
			"shipmentsId":          orderinfo.DeliveryCode,
			"partner":              orderinfo.PartnerInfo.PartnerName,
			"dividendsReceivable":  orderinfo.OrderCompute.PlanPartnerBonus,
			"dividendSpending":     orderinfo.OrderCompute.PartnerBonusExpense,
			"receiveDividends":     orderinfo.OrderCompute.PartnerBonusHas,
			"uncollectedDividends": orderinfo.OrderCompute.PartnerBonusNot,
			"stateOfPayment":       orderinfo.BonusStatus,
		}
		if orderinfo.OrderCompute.UsePartnerBonus >= 0 {
			m["dividendsReceivable"] = orderinfo.OrderCompute.UsePartnerBonus
		}
		rsp = append(rsp, m)
	}
	c.ResponsePageList(rsp, cnt, param.PageNumber)
	return
}

//OrderDividendDetail 订单的分红详情
func (c *OrderDividendController) OrderDividendDetail() {
	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)
	if orderid == 0 {
		c.ResponseError(errors.New("参数错误"))
		return
	}
	orderSrv := orderService.NewOrderInfoService(nil)
	orderinfo, err := orderSrv.GetOrderDetail(orderQuery.GetOrderQuery{
		OrderId: orderid,
	})
	if err != nil {
		c.ResponseError(err)
		return
	}
	if orderinfo.OrderType != domain.OrderReal {
		c.ResponseError(errors.New("参数错误"))
		return
	}
	allGoods := []map[string]interface{}{}
	for _, v := range orderinfo.Goods {
		detail := map[string]interface{}{
			"productName":  v.GoodName,
			"orderNum":     v.PlanGoodNumber,
			"univalence":   v.Price,
			"partnerRatio": v.PartnerBonusPercent,
			"amount":       v.GoodCompute.PlanAmount,

			"partnerDividendsReceivable": v.GoodCompute.PlanPartnerBonus,
		}
		m := map[string]interface{}{
			"detail":                 detail,
			"quantityControl":        "",
			"id":                     v.Id,
			"stateOfPayment":         v.BonusStatus,
			"remark":                 v.Remark,
			"amountControl":          "",
			"partnerDividendControl": "",
		}
		if v.GoodCompute.UseAmount >= 0 {
			m["amountControl"] = fmt.Sprint(v.GoodCompute.UseAmount)
		}
		if v.UseGoodNumber >= 0 {
			m["quantityControl"] = fmt.Sprint(v.UseGoodNumber)
		}
		if v.GoodCompute.UsePartnerBonus >= 0 {
			m["partnerDividendControl"] = fmt.Sprint(v.GoodCompute.UsePartnerBonus)
		}
		allGoods = append(allGoods, m)
	}
	orderData := map[string]interface{}{
		"buyer":       orderinfo.Buyer.BuyerName,
		"shipmentsId": orderinfo.DeliveryCode,
		"orderDist":   orderinfo.RegionInfo.RegionName,
		"partner":     orderinfo.PartnerInfo.PartnerName,
		"id":          orderinfo.Id,
		"orderId":     orderinfo.OrderCode,
	}
	dividendCount := map[string]interface{}{
		"orderNum":                     orderinfo.OrderCompute.PlanOrderCount,
		"orderAmountAdjustment":        orderinfo.OrderCompute.PlanOrderAmount,
		"orderNumControl":              "",
		"orderAmountAdjustmentControl": "",
		"partnerDividends":             orderinfo.OrderCompute.PlanPartnerBonus,
		"partnerDividendsControl":      "",
		"receivedDividends":            orderinfo.OrderCompute.PartnerBonusHas,
		"notReceivedDividend":          orderinfo.OrderCompute.PartnerBonusNot,
		"dividendSpending":             orderinfo.OrderCompute.PartnerBonusExpense,
		"commissionProportion":         orderinfo.SalesmanBonusPercent,
		"expectedCommission":           orderinfo.OrderCompute.SalesmanBonus,
	}
	if orderinfo.OrderCompute.UseOrderAmount >= 0 {
		dividendCount["orderAmountAdjustmentControl"] = fmt.Sprint(orderinfo.OrderCompute.UseOrderAmount)
	}
	if orderinfo.OrderCompute.UsePartnerBonus >= 0 {
		dividendCount["partnerDividendsControl"] = fmt.Sprint(orderinfo.OrderCompute.UsePartnerBonus)
	}
	if orderinfo.OrderCompute.UseOrderCount >= 0 {
		dividendCount["orderNumControl"] = fmt.Sprint(orderinfo.OrderCompute.UseOrderCount)
	}
	rsp := map[string]interface{}{
		"order":         orderData,
		"product":       allGoods,
		"dividendCount": dividendCount,
		"operationTime": orderinfo.UpdateTime.Local().Format("2006-01-02 15:04:06"),
	}
	if orderinfo.OrderCompute.UseOrderAmount >= 0 {
		rsp["orderNumCountControl"] = fmt.Sprint(orderinfo.OrderCompute.UseOrderAmount)
	}
	if orderinfo.OrderCompute.UseOrderCount >= 0 {
		rsp["orderAmountAdjustmentCountControl"] = fmt.Sprint(orderinfo.OrderCompute.UseOrderCount)
	}
	c.ResponseData(rsp)
	return
}

type postOrderDividend struct {
	Id               string                `json:"id"`
	DividendPayments []postDividendPayment `json:"dividendPayment"`
}

type postDividendPayment struct {
	QuantityControl string `json:"quantityControl"`
	StateOfPayment  int    `json:"stateOfPayment"`
	ProductId       string `json:"productId"`
	Remark          string `json:"remark"`
}

func (c *OrderDividendController) EditOrderDividend() {
	var (
		param postOrderDividend
		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)
	if orderId == 0 {
		c.ResponseError(errors.New("参数错误"))
		return
	}
	goodbouns := []orderCmd.GoodBouns{}
	for _, v := range param.DividendPayments {
		goodId, _ := strconv.ParseInt(v.ProductId, 10, 64)
		if goodId == 0 {
			c.ResponseError(errors.New("参数错误"))
			return
		}
		if !(v.StateOfPayment == domain.OrderGoodWaitPay || v.StateOfPayment == domain.OrderGoodHasPay) {
			c.ResponseError(errors.New("参数错误"))
			return
		}
		g := orderCmd.GoodBouns{
			GoodId:      goodId,
			Remark:      v.Remark,
			BounsStatus: v.StateOfPayment,
		}
		if len(v.QuantityControl) == 0 {
			g.UseGoodNumber = -1
		} else {
			num, err := strconv.Atoi(v.QuantityControl)
			if err != nil {
				c.ResponseError(errors.New("参数错误"))
				return
			}
			g.UseGoodNumber = num
		}
		goodbouns = append(goodbouns, g)
	}
	cmd := orderCmd.UpdateGoodBouns{
		Id:        orderId,
		GoodBouns: goodbouns,
	}
	orderSrv := orderService.NewOrderInfoService(nil)
	err = orderSrv.UpdateGoodBouns(cmd)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(nil)
	return
}