order_dividend_controller.go 11.3 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()
	if ok := c.ValidJWTToken(); !ok {
		return
	}
	if ok := c.ValidAdminPermission(domain.PERMISSION_DIVIDEND); !ok {
		return
	}
}

//PageListOrderDividend 获取实发订单分红列表
func (c *OrderDividendController) PageListOrderDividend() {
	type Parameter struct {
		SearchWord string `json:"searchWord"`
		OrderType  int    `json:"orderType"`
		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.OrderType == 0 ||
		param.OrderType == domain.OrderReal ||
		param.OrderType == domain.OrderTypeBestShop) {
		c.ResponseError(errors.New("参数异常"))
		return
	}
	if param.PageNumber == 0 {
		param.PageNumber = 1
	}
	if param.PageSize == 0 {
		param.PageSize = 20
	}
	companyId := c.GetUserCompany()
	orderSrv := orderService.NewOrderInfoService(nil)
	resp, cnt, err := orderSrv.PageListOrderBonus(orderQuery.ListOrderBonusQuery{
		OrderType:     param.OrderType,
		PartnerOrCode: param.SearchWord,
		Limit:         param.PageSize,
		Offset:        (param.PageNumber - 1) * param.PageSize,
		CompanyId:     companyId,
	})
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponsePageList(resp, 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
	}
	companyId := c.GetUserCompany()
	orderSrv := orderService.NewOrderInfoService(nil)
	orderinfo, err := orderSrv.GetOrderDetail(orderQuery.GetOrderQuery{
		OrderId:   orderid,
		CompanyId: companyId,
	})
	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,
			"orderPrice":                 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": "",
			"receivedDividends":      v.GoodCompute.PartnerBonusHas,
			"notReceivedDividend":    v.GoodCompute.PartnerBonusNot,
			"dividendSpending":       v.GoodCompute.PartnerBonusExpense,
		}
		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               int64                 `json:"id"`
	DividendPayments []postDividendPayment `json:"dividendPayment"`
}

type postDividendPayment struct {
	QuantityControl string `json:"quantityControl"`
	StateOfPayment  int    `json:"stateOfPayment"`
	ProductId       int64  `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
	}

	if param.Id == 0 {
		c.ResponseError(errors.New("参数错误"))
		return
	}
	goodbouns := []orderCmd.GoodBouns{}
	for _, v := range param.DividendPayments {
		if v.ProductId == 0 {
			c.ResponseError(errors.New("参数错误"))
			return
		}
		if !(v.StateOfPayment == domain.OrderGoodWaitPay || v.StateOfPayment == domain.OrderGoodHasPay) {
			c.ResponseError(errors.New("参数错误"))
			return
		}
		g := orderCmd.GoodBouns{
			GoodId:      v.ProductId,
			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)
	}
	companyId := c.GetUserCompany()
	cmd := orderCmd.UpdateGoodBouns{
		Id:        param.Id,
		GoodBouns: goodbouns,
		CompanyId: companyId,
	}
	orderSrv := orderService.NewOrderInfoService(nil)
	err = orderSrv.UpdateGoodBouns(cmd)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(nil)
	return
}

//OrderDividendDetailForBestshop 海鲜干货的订单分红详情
func (c *OrderDividendController) OrderDividendDetailForBestshop() {
	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
	}
	companyId := c.GetUserCompany()
	orderSrv := orderService.NewOrderInfoService(nil)
	respData, err := orderSrv.GetOrderBestshopInfoWithBonus(orderid, companyId)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(respData)
	return
}

//EditOrderForBestshop 编辑海鲜干货的订单 中的 货品数量和分红比例
func (c *OrderDividendController) EditOrderDividendForBestshop() {
	type Parameter struct {
		State               int     `json:"state"`
		OrderId             string  `json:"orderId"`
		ProductId           string  `json:"productId"`
		Reason              string  `json:"reason"`
		GoodNumber          int     `json:"goodNumber"`
		PartnerBonusPercent float64 `json:"partnerBonusPercent"`
	}
	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.OrderId, 10, 64)
	if orderid == 0 {
		c.ResponseError(errors.New("参数错误"))
		return
	}
	productId, _ := strconv.ParseInt(param.ProductId, 10, 64)
	if productId == 0 {
		c.ResponseError(errors.New("参数错误"))
		return
	}
	adminId := c.GetUserId()
	orderSrv := orderService.NewOrderInfoService(nil)

	switch param.State {
	case 1:
		err = orderSrv.UpdateBonusByGoodNumber(orderid, productId, adminId, param.GoodNumber, param.Reason)
	case 2:
		err = orderSrv.UpdateBonusByPartnerBonusPercent(orderid, productId, adminId, param.PartnerBonusPercent, param.Reason)
	}
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(nil)
	return
}

//PayOrderGoodBonusForBestshop 支付海鲜干货订单中的分红
func (c *OrderDividendController) PayOrderGoodBonusForBestshop() {
	type Parameter struct {
		OrderId   string `json:"orderId"`
		ProductId string `json:"productId"`
	}
	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.OrderId, 10, 64)
	if orderid == 0 {
		c.ResponseError(errors.New("参数错误"))
		return
	}
	productId, _ := strconv.ParseInt(param.ProductId, 10, 64)
	if productId == 0 {
		c.ResponseError(errors.New("参数错误"))
		return
	}
	adminId := c.GetUserId()
	orderSrv := orderService.NewOrderInfoService(nil)
	err = orderSrv.PayPartnerBonusWithOrderBestshop(orderid, productId, adminId)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(nil)
	return
}

//EditOrderRemarkBonusForBestshop 编辑海鲜干货订单中的备注
func (c *OrderDividendController) EditOrderRemarkBonusForBestshop() {
	type Parameter struct {
		OrderId string `json:"orderId"`
		Remark  string `json:"remark"`
	}
	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.OrderId, 10, 64)
	if orderid == 0 {
		c.ResponseError(errors.New("参数错误"))
		return
	}

	adminId := c.GetUserId()
	orderSrv := orderService.NewOrderInfoService(nil)
	err = orderSrv.UpdateOrderRemarkBonus(orderid, adminId, param.Remark)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(nil)
	return
}