dividends_controller.go 4.2 KB
package controllers

import (
	"errors"
	"github.com/astaxie/beego/logs"
	OrderPaymentCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/command"
	OrderPaymentQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/query"
	OrderPaymentSvr "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/service"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
	"strconv"
)

type DividendsController struct {
	BaseController
}

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

//Edit 编辑分红支付
func (c *DividendsController) Edit() {
	//用与适配前端定义的数据结构
	type DividendPaymentItem struct {
		PaymentForGoods string `json:"paymentForGoods"`
		StateOfPayment  string `json:"stateOfPayment"`
	}
	type Parameter struct {
		Id              int64                 `json:"id"` //订单编号
		DividendPayment []DividendPaymentItem `json:"dividendPayment"`
	}
	var (
		param Parameter
		err   error
	)
	if err = c.BindJsonData(&param); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	cmd := OrderPaymentCmd.CreateOrderPaymentCommand{
		OrderId:            param.Id,
		DivdendPaymentItem: make([]OrderPaymentCmd.DivdendPyamentItem, 0),
	}

	//编辑
	for i := range param.DividendPayment {
		item := param.DividendPayment[i]
		paymentItem := OrderPaymentCmd.DivdendPyamentItem{}
		paymentItem.PaymentForGoods, _ = strconv.ParseFloat(item.PaymentForGoods, 64)
		paymentItem.StateOfPayment, _ = strconv.Atoi(item.StateOfPayment)
		paymentItem.PaymentSn = i + 1

		if paymentItem.StateOfPayment == domain.BonusPaid {
			cmd.TotalPaymentAmount += paymentItem.PaymentForGoods
		}
		cmd.DivdendPaymentItem = append(cmd.DivdendPaymentItem, paymentItem)
	}
	serve := OrderPaymentSvr.NewOrderPaymentService(nil)
	_, err = serve.CreateOrderPayment(&cmd)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(nil)
	return
}

//Edit 分红支付详情
func (c *DividendsController) Detail() {
	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
	}

	cmd := OrderPaymentQuery.GetOrderPaymentQuery{
		//OrderId: param.Id,
	}
	cmd.OrderId, _ = strconv.ParseInt(param.Id, 10, 64)
	serve := OrderPaymentSvr.NewOrderPaymentService(nil)
	var data []*domain.OrderPayment
	var extral map[string]interface{}
	extral, data, err = serve.ListOrderPayment(&cmd)
	if err != nil {
		c.ResponseError(err)
		return
	}

	type DividendPayment struct {
		PaymentForGoods    float64 `json:"paymentForGoods"`
		UpdateTime         string  `json:"updateTime"`
		StateOfPayment     int     `json:"stateOfPayment"`
		Dividend           float64 `json:"dividend"`
		DividendProportion float64 `json:"dividendProportion"`
	}
	type Response struct {
		DividendPayment []DividendPayment `json:"dividendPayment"`
	}
	rsp := Response{DividendPayment: make([]DividendPayment, 0)}
	for i := range data {
		item := data[i]
		payment := DividendPayment{
			PaymentForGoods:    item.PaymentAmount,
			UpdateTime:         item.UpdateAt.Local().Format("2006-01-02 15:04:05"),
			StateOfPayment:     item.BonusStatus,
			Dividend:           item.BonusAmount,
			DividendProportion: extral["PartnerBonusPercent"].(float64),
		}
		rsp.DividendPayment = append(rsp.DividendPayment, payment)
	}

	c.ResponseData(rsp)
	return
}

//分红管理
func (c *DividendsController) List() {
	var (
		param OrderPaymentQuery.ListDividendOrdersQuery
		err   error
		count int
	)
	if err = c.BindJsonData(&param); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}

	if param.PageSize == 0 {
		param.PageSize = 20
	}
	if param.PageNumber == 0 {
		param.PageNumber = 1
	}
	serve := OrderPaymentSvr.NewOrderPaymentService(nil)
	var data interface{}
	count, data, err = serve.ListDividendOrders(&param)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponsePageList(data, count, param.PageNumber)
	return
}