作者 tangxvhui

更新奖惩规则

... ... @@ -165,3 +165,11 @@ func CreateRewardStandardRepository(options map[string]interface{}) (domain.Rewa
}
return repository.NewRewardStandardRepository(transactionContext)
}
func CreateRewardRuleRepository(options map[string]interface{}) (domain.RewardRuleRepository, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewRewardRuleRepository(transactionContext)
}
... ...
package command
type SaveRewardRuleCommand struct {
// RewardTag string `json:"rewardTag"`
RewardNum int `json:"rewardNum"`
RewardAmount string `json:"rewardAmount"`
// FaultTag string `json:"faultTag"`
FaultNum int `json:"faultNum"`
FaultAmount string `json:"faultAmount"`
Remark string `json:"remark"`
}
... ...
package dto
type RewardRuleInfo struct {
RewardNum int `json:"rewardNum"`
RewardAmount string `json:"rewardAmount"`
FaultNum int `json:"faultNum"`
FaultAmount string `json:"faultAmount"`
Remark string `json:"remark"`
}
... ...
... ... @@ -268,3 +268,91 @@ func (srv RewardStandardService) ListRewardStandard(param *query.ListRewardStand
}
return cnt, listResult, nil
}
//SaveRewardRule 保存奖惩规则
func (srv RewardStandardService) SaveRewardRule(operateInfo *domain.OperateInfo, param *command.SaveRewardRuleCommand) (*command.SaveRewardRuleCommand, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
rewardRuleRepo, _ := factory.CreateRewardRuleRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
_, ruleList, err := rewardRuleRepo.Find(map[string]interface{}{
"companyId": operateInfo.CompanyId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var newRewardRule *domain.RewardRule
if len(ruleList) > 0 {
newRewardRule = ruleList[0]
} else {
newRewardRule = &domain.RewardRule{
CreatedAt: time.Now(),
CompanyId: operateInfo.CompanyId,
OrgId: operateInfo.OrgId,
FaultTag: ">",
RewardTag: ">",
}
}
newRewardRule.UpdatedAt = time.Now()
newRewardRule.FaultAmount = param.FaultAmount
newRewardRule.FaultNum = param.FaultNum
newRewardRule.RewardAmount = param.RewardAmount
newRewardRule.RewardNum = param.RewardNum
newRewardRule.Remark = param.Remark
_, err = rewardRuleRepo.Save(newRewardRule)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return param, err
}
//GetRewardRule 获取奖惩规则
func (srv RewardStandardService) GetRewardRule(operateInfo *domain.OperateInfo) (*dto.RewardRuleInfo, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
rewardRuleRepo, _ := factory.CreateRewardRuleRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
_, ruleList, err := rewardRuleRepo.Find(map[string]interface{}{
"companyId": operateInfo.CompanyId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
var newRewardRule *dto.RewardRuleInfo
if len(ruleList) > 0 {
newRewardRule = &dto.RewardRuleInfo{
RewardNum: ruleList[0].RewardNum,
RewardAmount: ruleList[0].RewardAmount,
FaultNum: ruleList[0].FaultNum,
FaultAmount: ruleList[0].RewardAmount,
Remark: ruleList[0].Remark,
}
} else {
newRewardRule = &dto.RewardRuleInfo{}
}
return newRewardRule, nil
}
... ...
package domain
import "time"
//RewardStandard 奖惩规则
type RewardRule struct {
Id int `json:"id"` //奖惩规则id
CompanyId int `json:"companyId"` //企业id
OrgId int `json:"orgId"` //组织ID
RewardTag string `json:"rewardTag"`
RewardNum int `json:"rewardNum"`
RewardAmount string `json:"rewardAmount"`
FaultTag string `json:"faultTag"`
FaultNum int `json:"faultNum"`
FaultAmount string `json:"faultAmount"`
Remark string `json:"remark"`
CreatedAt time.Time
UpdatedAt time.Time
}
type RewardRuleRepository interface {
Save(param *RewardRule) (*RewardRule, error)
FindOne(queryOptions map[string]interface{}) (*RewardRule, error)
Find(queryOptions map[string]interface{}) (int64, []*RewardRule, error)
}
func (m *RewardRule) ValidRewardTag() bool {
switch m.FaultTag {
case ">":
default:
return false
}
return true
}
func (m *RewardRule) ValidFaultTag() bool {
switch m.FaultTag {
case ">":
default:
return false
}
return true
}
... ...
... ... @@ -52,6 +52,7 @@ func init() {
(*models.ProductMaterialGroup)(nil),
(*models.ProductMaterial)(nil),
(*models.RewardStandard)(nil),
(*models.RewardRule)(nil),
} {
err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
Temp: false,
... ...
package models
import (
"time"
)
//RewardRule 奖惩规则
type RewardRule struct {
tableName string `pg:"manufacture.reward_rule,alias:reward_rule"`
Id int `pg:"pk:id"` //奖惩标准 id
CompanyId int `comment:"企业id"` // 企业id
OrgId int `comment:"组织ID"` // 组织ID
RewardTag string
RewardNum int
RewardAmount string
FaultTag string
FaultNum int
FaultAmount string
Remark string
CreatedAt time.Time
UpdatedAt time.Time
}
... ...
package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
)
type RewardRuleRepository struct {
transactionContext *pgTransaction.TransactionContext
}
var _ domain.RewardRuleRepository = (*RewardRuleRepository)(nil)
func NewRewardRuleRepository(transactionContext *pgTransaction.TransactionContext) (*RewardRuleRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &RewardRuleRepository{
transactionContext: transactionContext,
}, nil
}
}
// func (repo *RewardStandardRepository) nextIdentify() (int64, error) {
// IdWorker, err := snowflake.NewIdWorker(1)
// if err != nil {
// return 0, err
// }
// id, err := IdWorker.NextId()
// return id, err
// }
func (repo *RewardRuleRepository) Save(param *domain.RewardRule) (*domain.RewardRule, error) {
m := models.RewardRule{
Id: param.Id,
CompanyId: param.CompanyId,
OrgId: param.OrgId,
RewardTag: param.RewardTag,
RewardNum: param.RewardNum,
RewardAmount: param.RewardAmount,
FaultTag: param.FaultTag,
FaultNum: param.FaultNum,
FaultAmount: param.FaultAmount,
CreatedAt: param.CreatedAt,
UpdatedAt: param.UpdatedAt,
Remark: param.Remark,
}
tx := repo.transactionContext.PgTx
if param.Id == 0 {
_, err := tx.Model(&m).Insert()
if err != nil {
return nil, err
}
param.Id = m.Id
} else {
_, err := tx.Model(&m).WherePK().Update()
if err != nil {
return nil, err
}
}
return param, nil
}
// func (repo *RewardRuleRepository) Remove(param *domain.RewardStandard) (*domain.RewardStandard, error) {
// tx := repo.transactionContext.PgTx
// m := new(models.RewardStandard)
// m.Id = param.Id
// nowTime := time.Now()
// param.DeletedAt = &nowTime
// _, err := tx.Model(m).
// WherePK().Set("deleted_at=?", nowTime).
// Update()
// if err != nil {
// return param, err
// }
// return nil, nil
// }
func (repo *RewardRuleRepository) FindOne(queryOptions map[string]interface{}) (*domain.RewardRule, error) {
tx := repo.transactionContext.PgTx
m := new(models.RewardRule)
query := tx.Model(m)
if v, ok := queryOptions["id"]; ok {
query.Where("id=?", v)
}
err := query.First()
if err != nil {
if err == pg.ErrNoRows {
return nil, domain.ErrorNotFound
} else {
return nil, err
}
}
result := repo.TransformToDomain(m)
return result, nil
}
func (repo *RewardRuleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.RewardRule, error) {
tx := repo.transactionContext.PgTx
m := []models.RewardRule{}
query := tx.Model(&m).
Limit(20)
if v, ok := queryOptions["limit"].(int); ok {
query.Limit(v)
}
if v, ok := queryOptions["offset"].(int); ok {
query.Offset(v)
}
if v, ok := queryOptions["companyId"]; ok {
query.Where("company_id=?", v)
}
cnt, err := query.SelectAndCount()
if err != nil {
return 0, nil, err
}
var listData []*domain.RewardRule
for i := range m {
temp := repo.TransformToDomain(&m[i])
listData = append(listData, temp)
}
return int64(cnt), listData, nil
}
func (repo *RewardRuleRepository) TransformToDomain(param *models.RewardRule) *domain.RewardRule {
return &domain.RewardRule{
Id: param.Id,
CompanyId: param.CompanyId,
OrgId: param.OrgId,
RewardTag: param.RewardTag,
RewardNum: param.RewardNum,
RewardAmount: param.RewardAmount,
FaultTag: param.FaultTag,
FaultNum: param.FaultNum,
FaultAmount: param.FaultAmount,
CreatedAt: param.CreatedAt,
UpdatedAt: param.UpdatedAt,
Remark: param.Remark,
}
}
... ...
... ... @@ -65,3 +65,21 @@ func (c RewardStandardController) ListRewardStandard() {
total, data, err := srv.ListRewardStandard(cmd)
ResponseGrid(c.BaseController, total, data, err)
}
//SaveRewardRule 保存奖惩规则
func (c RewardStandardController) SaveRewardRule() {
srv := service.NewRewardStandardService(nil)
createCommand := &command.SaveRewardRuleCommand{}
Must(c.Unmarshal(createCommand))
operater := ParseOperateInfo(c.BaseController)
data, err := srv.SaveRewardRule(operater, createCommand)
c.Response(data, err)
}
//GetRewardRule 获取奖惩规则
func (c RewardStandardController) GetRewardRule() {
srv := service.NewRewardStandardService(nil)
operater := ParseOperateInfo(c.BaseController)
data, err := srv.GetRewardRule(operater)
c.Response(data, err)
}
... ...
... ... @@ -11,5 +11,6 @@ func init() {
web.Router("/reward-standard/:id", &controllers.RewardStandardController{}, "Get:GetRewardStandard")
web.Router("/reward-standard/remove", &controllers.RewardStandardController{}, "Post:DeleteRewardStandard")
web.Router("/reward-standard/search", &controllers.RewardStandardController{}, "Post:ListRewardStandard")
web.Router("/reward-rule/", &controllers.RewardStandardController{}, "Post:SaveRewardRule")
web.Router("/reward-rule/", &controllers.RewardStandardController{}, "Get:GetRewardRule")
}
... ...