作者 唐旭辉

业务分行 部分

package command
type UpdateBusinessBonusCommand struct {
Id int64 `json:"id"`
Bonus float64 `json:"bonus"`
BonusStatus int8 `json:"bonusStatus"`
CompanyId int64 `json:"companyId"`
}
func (cmd UpdateBusinessBonusCommand) ValidateCommand() error {
... ...
package query
type GetBusinessBonusQuery struct {
Id int64 `json:"id"`
CompanyId int64 `json:"companyId"`
}
... ...
package query
type ListBusinessBonusQuery struct {
//用户名称匹配
PartnerNameMatch string `json:"userNameMatch" `
// 查询偏离量
Offset int `json:"offset" `
// 查询限制
Limit int `json:"limit"`
PartnerId int64 `json:"partnerId"`
CompanyId int64 `json:"companyId"`
}
... ...
package service
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/businessBonus/command"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/businessBonus/query"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
type BusinessBonusService struct {
}
func NewBusinessBonusService(option map[string]interface{}) *BusinessBonusService {
newService := new(BusinessBonusService)
return newService
}
func (srv BusinessBonusService) ListBusinessBonus(queryOption query.ListBusinessBonusQuery) (int, interface{}, error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return 0, nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
bonusDaoao *dao.BusinessBonusDao
)
if bonusDaoao, err = factory.CreateBusinessBonusDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return 0, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
result, err := bonusDaoao.SearchBusinessBonus(queryOption.PartnerId, queryOption.PartnerNameMatch,
queryOption.CompanyId, queryOption.Limit, queryOption.Offset)
if err != nil {
return 0, nil, err
}
err = transactionContext.CommitTransaction()
returnData := []map[string]interface{}{}
for _, v := range result {
m := map[string]interface{}{
"id": v.Id,
"uncollectedDividends": v.BonusNot,
"receiveDividends": v.BonusHas,
"dividendsReceivable": v.Bonus,
"stateOfPayment": v.BonusStatus,
"stateOfPaymentName": domain.DescribeBusinessBonusStatus(v.BonusStatus),
"partner": v.PartnerName,
"updateTime": v.UpdateAt.Format("2006-01-02 15:04:05"),
}
returnData = append(returnData, m)
}
return 0, returnData, nil
}
func (srv BusinessBonusService) UpdateBusinessBonus(cmd command.UpdateBusinessBonusCommand) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
bonusRespository domain.BusinessBonusRepository
bonusData *domain.BusinessBonus
)
if bonusRespository, err = factory.CreateBusinessBonusRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
bonusData, err = bonusRespository.FindOne(domain.BusinessBonusFindOneQuery{
Id: cmd.Id, CompanyId: cmd.CompanyId,
})
if err != nil {
e := fmt.Sprintf("获取业务分红(id=%d;company_id=%d)数据失败:%s", cmd.Id, cmd.CompanyId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
bonusData.Bonus = cmd.Bonus
switch cmd.BonusStatus {
case domain.BUSINESS_BONUS_HAS_PAY:
bonusData.BusinessBonusPayStatus.PayPartnerBonus(bonusData)
case domain.BUSINESS_BONUS_WAIT_PAY:
bonusData.BusinessBonusPayStatus.WartPayPartnerBonus(bonusData)
default:
return lib.ThrowError(lib.BUSINESS_ERROR, "支付状态错误")
}
err = bonusRespository.Edit(bonusData)
if err != nil {
e := fmt.Sprintf("更新业务分行(id=%d)数据失败:%s", bonusData.Id, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
err = transactionContext.CommitTransaction()
return nil
}
func (srv BusinessBonusService) GetBusinessBonus(queryOption query.GetBusinessBonusQuery) (map[string]interface{}, error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
bonusRespository domain.BusinessBonusRepository
partnerInfoRepository domain.PartnerInfoRepository
bonusData *domain.BusinessBonus
partnerData *domain.PartnerInfo
)
if bonusRespository, err = factory.CreateBusinessBonusRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
bonusData, err = bonusRespository.FindOne(domain.BusinessBonusFindOneQuery{
Id: queryOption.Id, CompanyId: queryOption.CompanyId,
})
if err != nil {
e := fmt.Sprintf("获取业务分红(id=%d;company_id=%d)数据失败:%s", queryOption.Id, queryOption.CompanyId, err)
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
if partnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
partnerData, err = partnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: bonusData.PartnerInfoId})
if err != nil {
e := fmt.Sprintf("获取合伙人(id=%d)数据失败:%s", bonusData.PartnerInfoId, err)
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
err = transactionContext.CommitTransaction()
returnData := map[string]interface{}{
"partner": partnerData.Partner.PartnerName,
"dividendsReceivable": bonusData.Bonus,
"receiveDividends": bonusData.BonusHas,
"uncollectedDividends": bonusData.BonusNot,
"updateTime": bonusData.UpdateAt.Format("2006-01-02 15:04:05"),
"stateOfPayment": bonusData.BonusStatus,
"stateOfPaymentName": domain.DescribeBusinessBonusStatus(bonusData.BonusStatus),
"id": bonusData.Id,
}
return returnData, nil
}
... ...
... ... @@ -36,3 +36,11 @@ func CreateUsersDao(options map[string]interface{}) (*dao.UsersDao, error) {
}
return dao.NewUsersDao(transactionContext)
}
func CreateBusinessBonusDao(options map[string]interface{}) (*dao.BusinessBonusDao, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
return dao.NewBusinessBonusDao(transactionContext)
}
... ...
... ... @@ -77,3 +77,12 @@ func CreatePartnerCategoryRepository(options map[string]interface{}) (domain.Par
}
return repository.NewPartnerCategoryRepository(transactionContext)
}
// CreateBusinessBonusRepository 业务分红
func CreateBusinessBonusRepository(options map[string]interface{}) (domain.BusinessBonusRepository, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
return repository.NewBusinessBonusRepository(transactionContext)
}
... ...
... ... @@ -11,11 +11,32 @@ const (
// 分红状态 1:待支付分红 2:已支付分红
const (
//待支付
BUSINESS_BONUS_WAIT_PAY int = 1
BUSINESS_BONUS_WAIT_PAY int8 = 1
//已支付
BUSINESS_BONUS_HAS_PAY int = 2
BUSINESS_BONUS_HAS_PAY int8 = 2
)
func DescribeBusinessBonusStatus(i int8) string {
m := map[int8]string{
BUSINESS_BONUS_WAIT_PAY: "待支付分红",
BUSINESS_BONUS_HAS_PAY: "已支付分红",
}
if v, ok := m[i]; ok {
return v
}
return ""
}
//分红状态
type BusinessBonusBonusStatus interface {
//状态变更为空
NullPayPartnerBonus(*BusinessBonus) error
//状态变更为待支付
WartPayPartnerBonus(*BusinessBonus) error
//状态变更为已支付
PayPartnerBonus(*BusinessBonus) error
}
// 业务分红信息
type BusinessBonus struct {
// 唯一标识
... ... @@ -28,6 +49,8 @@ type BusinessBonus struct {
Bonus float64 `json:"bonus"`
// 未收分红
BonusNot float64 `json:"bonusNot"`
//已收分红
BonusHas float64 `json:"bonusHas"`
// 分红支出
BonusExpense float64 `json:"bonusExpense"`
// 是否关闭【0;否】【1:是】
... ... @@ -40,11 +63,93 @@ type BusinessBonus struct {
UpdateAt time.Time `json:"updateAt"`
// 删除时间
DeleteAt time.Time `json:"deleteAt"`
BusinessBonusPayStatus BusinessBonusBonusStatus
}
type BusinessBonusNullPay struct{}
var _ BusinessBonusBonusStatus = (*BusinessBonusNullPay)(nil)
type BusinessBonusWaitPay struct{}
var _ BusinessBonusBonusStatus = (*BusinessBonusWaitPay)(nil)
//OrderGoodBonusHasPay 货品分红已支付
type BusinessBonusHasPay struct{}
var _ BusinessBonusBonusStatus = (*BusinessBonusHasPay)(nil)
// -----默认空状态-----
//状态变更为空
func (pay BusinessBonusNullPay) NullPayPartnerBonus(bonus *BusinessBonus) error {
bonus.BonusStatus = 0
return nil
}
//状态变更为待支付
func (pay BusinessBonusNullPay) WartPayPartnerBonus(bonus *BusinessBonus) error {
bonus.BonusNot = bonus.Bonus
bonus.BonusStatus = BUSINESS_BONUS_WAIT_PAY
return nil
}
//状态变更为已支付
func (pay BusinessBonusNullPay) PayPartnerBonus(bonus *BusinessBonus) error {
bonus.BonusNot = 0
bonus.BonusHas = bonus.Bonus
bonus.BonusStatus = BUSINESS_BONUS_HAS_PAY
return nil
}
// -----待支付状态-----
//状态变更为空
func (pay BusinessBonusWaitPay) NullPayPartnerBonus(bonus *BusinessBonus) error {
bonus.BonusStatus = 0
return nil
}
//状态变更为待支付
func (pay BusinessBonusWaitPay) WartPayPartnerBonus(bonus *BusinessBonus) error {
bonus.BonusNot = bonus.Bonus
bonus.BonusStatus = BUSINESS_BONUS_WAIT_PAY
return nil
}
//状态变更为已支付
func (pay BusinessBonusWaitPay) PayPartnerBonus(bonus *BusinessBonus) error {
bonus.BonusNot = 0
bonus.BonusHas = bonus.Bonus
bonus.BonusStatus = BUSINESS_BONUS_HAS_PAY
return nil
}
// -----已经支付状态-----
//状态变更为空
func (pay BusinessBonusHasPay) NullPayPartnerBonus(bonus *BusinessBonus) error {
bonus.BonusStatus = 0
return nil
}
//状态变更为待支付
func (pay BusinessBonusHasPay) WartPayPartnerBonus(bonus *BusinessBonus) error {
bonus.BonusNot = bonus.Bonus
bonus.BonusStatus = BUSINESS_BONUS_WAIT_PAY
return nil
}
//状态变更为已支付
func (pay BusinessBonusHasPay) PayPartnerBonus(bonus *BusinessBonus) error {
bonus.BonusNot = 0
bonus.BonusHas = bonus.Bonus
bonus.BonusStatus = BUSINESS_BONUS_HAS_PAY
return nil
}
type BusinessBonusFindOneQuery struct {
Id int64
PartnerId int64
CompanyId int64
}
type BusinessBonusFindQuery struct {
Offset int
... ...
package dao
import (
"fmt"
"strings"
"time"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
type BusinessBonusDao struct {
transactionContext *transaction.TransactionContext
}
func NewBusinessBonusDao(transactionContext *transaction.TransactionContext) (*BusinessBonusDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &BusinessBonusDao{
transactionContext: transactionContext,
}, nil
}
}
type CustomBusinessBonus struct {
Id int64
Bonus string
BonusNot string
BonusExpense string
BonusHas string
BonusStatus int8
PartnerName string
UpdateAt time.Time
}
func (dao BusinessBonusDao) SearchBusinessBonus(partnerId int64, partnerNameMatch string,
companyId int64, limit int, offset int) ([]CustomBusinessBonus, error) {
sql := `SELECT business_bonus.id, business_bonus.bonus,business_bonus.bonus_not
,business_bonus.bonus_expense,business_bonus.bonus_status,business_bonus.update_at
,partner_info.partner_name,business_bonus.bonus_has
FROM business_bonus
JOIN partner_info ON business_bonus.partner_info_id=partner_info.id
WHERE business_bonus.company_id = ? `
partnerCondition := []string{}
allParam := []interface{}{companyId}
if partnerId > 0 {
partnerCondition = append(partnerCondition, ` business_bonus.partner_info_id=? `)
allParam = append(allParam, partnerId)
}
if len(partnerNameMatch) > 0 {
allParam = append(allParam, "%"+partnerNameMatch+"%")
partnerCondition = append(partnerCondition, ` partner_info.partner_name like ? `)
}
if len(partnerCondition) > 0 {
sql += fmt.Sprintf(" AND (%s)", strings.Join(partnerCondition, " OR "))
}
sql += ` limit ? OFFSET ? `
allParam = append(allParam, limit, offset)
tx := dao.transactionContext.PgTx
var (
result []CustomBusinessBonus
err error
)
_, err = tx.Query(&result, sql, allParam...)
return result, err
}
... ...
... ... @@ -22,6 +22,8 @@ type BusinessBonus struct {
BonusNot float64
// 分红支出
BonusExpense float64
//已收分红
BonusHas float64
// 是否关闭【0;否】【1:是】
IsDisable int8
// 分红状态 1:待支付分红 2:已支付分红
... ...
... ... @@ -13,9 +13,29 @@ type BusinessBonusRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *BusinessBonusRepository) transformPgModelToDomainModel(BusinessBonusModel *models.BusinessBonus) (domain.BusinessBonus, error) {
m := domain.BusinessBonus{}
return m, nil
func (repository *BusinessBonusRepository) transformPgModelToDomainModel(m *models.BusinessBonus) (domain.BusinessBonus, error) {
dm := domain.BusinessBonus{
Id: m.Id,
CompanyId: m.CompanyId,
PartnerInfoId: m.PartnerInfoId,
Bonus: m.Bonus,
BonusNot: m.BonusNot,
BonusHas: m.BonusHas,
BonusExpense: m.BonusExpense,
IsDisable: m.IsDisable,
BonusStatus: m.BonusStatus,
CreateAt: m.CreateAt,
UpdateAt: m.UpdateAt,
}
switch m.BonusStatus {
case domain.BUSINESS_BONUS_WAIT_PAY:
dm.BusinessBonusPayStatus = domain.BusinessBonusWaitPay{}
case domain.BUSINESS_BONUS_HAS_PAY:
dm.BusinessBonusPayStatus = domain.BusinessBonusHasPay{}
default:
dm.BusinessBonusPayStatus = domain.BusinessBonusNullPay{}
}
return dm, nil
}
func NewBusinessBonusRepository(transactionContext *transaction.TransactionContext) (*BusinessBonusRepository, error) {
... ... @@ -33,6 +53,7 @@ func (repository *BusinessBonusRepository) Add(dm *domain.BusinessBonus) error {
PartnerInfoId: dm.PartnerInfoId,
Bonus: dm.Bonus,
BonusNot: dm.Bonus,
BonusHas: dm.BonusHas,
BonusExpense: dm.BonusExpense,
IsDisable: dm.IsDisable,
BonusStatus: dm.BonusStatus,
... ... @@ -54,6 +75,7 @@ func (repository *BusinessBonusRepository) Edit(dm *domain.BusinessBonus) error
Bonus: dm.Bonus,
BonusNot: dm.Bonus,
BonusExpense: dm.BonusExpense,
BonusHas: dm.BonusHas,
IsDisable: dm.IsDisable,
BonusStatus: dm.BonusStatus,
CreateAt: dm.CreateAt,
... ... @@ -77,6 +99,10 @@ func (repository *BusinessBonusRepository) FindOne(queryOptions domain.BusinessB
hasCondition = true
query = query.Where("partner_info_id=?", queryOptions.PartnerId)
}
if queryOptions.CompanyId > 0 {
hasCondition = true
query = query.Where("company_id=?", queryOptions.CompanyId)
}
if !hasCondition {
return nil, errors.New("没有查询条件")
}
... ...
package controllers
import (
"errors"
"github.com/astaxie/beego/logs"
businessCommand "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/businessBonus/command"
businessQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/businessBonus/query"
businessService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/businessBonus/service"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
)
type BusinessBonusController struct {
BaseController
}
func (c *BusinessBonusController) Prepare() {
c.BaseController.Prepare()
if ok := c.ValidJWTToken(); !ok {
return
}
if ok := c.ValidAdminPermission(domain.PERMISSION_DIVIDEND); !ok {
return
}
}
func (c *BusinessBonusController) ListBusinessBonus() {
type Parameter struct {
PageNumber int `json:"pageNumber"`
Partner int64 `json:"partner"`
PageSize int `json:"pageSize"`
SearchText string `json:"searchText"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
companyId := c.GetUserCompany()
srv := businessService.NewBusinessBonusService(nil)
_, result, err := srv.ListBusinessBonus(businessQuery.ListBusinessBonusQuery{
CompanyId: companyId,
})
if err != nil {
c.ResponseError(err)
return
}
c.ResponsePageList(result, 0, param.PageNumber)
}
func (c *BusinessBonusController) UpdateBusinessBonus() {
type Parameter struct {
Id int64 `json:"id"`
DividendsReceivable string `json:"dividendsReceivable"`
StateOfPayment string `json:"stateOfPayment"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
companyId := c.GetUserCompany()
srv := businessService.NewBusinessBonusService(nil)
err = srv.UpdateBusinessBonus(businessCommand.UpdateBusinessBonusCommand{
CompanyId: companyId,
})
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
}
func (c *BusinessBonusController) GetBusinessBonus() {
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
}
companyId := c.GetUserCompany()
srv := businessService.NewBusinessBonusService(nil)
data, err := srv.GetBusinessBonus(businessQuery.GetBusinessBonusQuery{
Id: param.Id,
CompanyId: companyId,
})
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(data)
}
... ...
... ... @@ -32,6 +32,10 @@ func init() {
beego.NSRouter("/list", &controllers.OrderDividendController{}, "POST:PageListOrderDividend"),
beego.NSRouter("/edit", &controllers.OrderDividendController{}, "POST:EditOrderDividend"),
beego.NSRouter("/detail", &controllers.OrderDividendController{}, "POST:OrderDividendDetail"),
beego.NSRouter("business/detail", &controllers.BusinessBonusController{}, "POST:GetBusinessBonus"),
beego.NSRouter("business/edit", &controllers.BusinessBonusController{}, "POST:UpdateBusinessBonus"),
beego.NSRouter("business/list", &controllers.BusinessBonusController{}, "POST:ListBusinessBonus"),
),
beego.NSNamespace("/order",
beego.NSRouter("/purpose/list", &controllers.OrderInfoController{}, "POST:PageListOrderPurpose"),
... ...