作者 tangxvhui

更新奖惩规则

@@ -165,3 +165,11 @@ func CreateRewardStandardRepository(options map[string]interface{}) (domain.Rewa @@ -165,3 +165,11 @@ func CreateRewardStandardRepository(options map[string]interface{}) (domain.Rewa
165 } 165 }
166 return repository.NewRewardStandardRepository(transactionContext) 166 return repository.NewRewardStandardRepository(transactionContext)
167 } 167 }
  168 +
  169 +func CreateRewardRuleRepository(options map[string]interface{}) (domain.RewardRuleRepository, error) {
  170 + var transactionContext *pg.TransactionContext
  171 + if value, ok := options["transactionContext"]; ok {
  172 + transactionContext = value.(*pg.TransactionContext)
  173 + }
  174 + return repository.NewRewardRuleRepository(transactionContext)
  175 +}
  1 +package command
  2 +
  3 +type SaveRewardRuleCommand struct {
  4 + // RewardTag string `json:"rewardTag"`
  5 + RewardNum int `json:"rewardNum"`
  6 + RewardAmount string `json:"rewardAmount"`
  7 + // FaultTag string `json:"faultTag"`
  8 + FaultNum int `json:"faultNum"`
  9 + FaultAmount string `json:"faultAmount"`
  10 + Remark string `json:"remark"`
  11 +}
  1 +package dto
  2 +
  3 +type RewardRuleInfo struct {
  4 + RewardNum int `json:"rewardNum"`
  5 + RewardAmount string `json:"rewardAmount"`
  6 + FaultNum int `json:"faultNum"`
  7 + FaultAmount string `json:"faultAmount"`
  8 + Remark string `json:"remark"`
  9 +}
@@ -268,3 +268,91 @@ func (srv RewardStandardService) ListRewardStandard(param *query.ListRewardStand @@ -268,3 +268,91 @@ func (srv RewardStandardService) ListRewardStandard(param *query.ListRewardStand
268 } 268 }
269 return cnt, listResult, nil 269 return cnt, listResult, nil
270 } 270 }
  271 +
  272 +//SaveRewardRule 保存奖惩规则
  273 +func (srv RewardStandardService) SaveRewardRule(operateInfo *domain.OperateInfo, param *command.SaveRewardRuleCommand) (*command.SaveRewardRuleCommand, error) {
  274 + transactionContext, err := factory.CreateTransactionContext(nil)
  275 + if err != nil {
  276 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  277 + }
  278 + if err := transactionContext.StartTransaction(); err != nil {
  279 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  280 + }
  281 + defer func() {
  282 + transactionContext.RollbackTransaction()
  283 + }()
  284 + rewardRuleRepo, _ := factory.CreateRewardRuleRepository(map[string]interface{}{
  285 + "transactionContext": transactionContext,
  286 + })
  287 + _, ruleList, err := rewardRuleRepo.Find(map[string]interface{}{
  288 + "companyId": operateInfo.CompanyId,
  289 + })
  290 + if err != nil {
  291 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  292 + }
  293 + var newRewardRule *domain.RewardRule
  294 + if len(ruleList) > 0 {
  295 + newRewardRule = ruleList[0]
  296 + } else {
  297 + newRewardRule = &domain.RewardRule{
  298 + CreatedAt: time.Now(),
  299 + CompanyId: operateInfo.CompanyId,
  300 + OrgId: operateInfo.OrgId,
  301 + FaultTag: ">",
  302 + RewardTag: ">",
  303 + }
  304 + }
  305 + newRewardRule.UpdatedAt = time.Now()
  306 + newRewardRule.FaultAmount = param.FaultAmount
  307 + newRewardRule.FaultNum = param.FaultNum
  308 + newRewardRule.RewardAmount = param.RewardAmount
  309 + newRewardRule.RewardNum = param.RewardNum
  310 + newRewardRule.Remark = param.Remark
  311 + _, err = rewardRuleRepo.Save(newRewardRule)
  312 + if err != nil {
  313 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  314 + }
  315 + if err := transactionContext.CommitTransaction(); err != nil {
  316 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  317 + }
  318 + return param, err
  319 +}
  320 +
  321 +//GetRewardRule 获取奖惩规则
  322 +func (srv RewardStandardService) GetRewardRule(operateInfo *domain.OperateInfo) (*dto.RewardRuleInfo, error) {
  323 + transactionContext, err := factory.CreateTransactionContext(nil)
  324 + if err != nil {
  325 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  326 + }
  327 + if err := transactionContext.StartTransaction(); err != nil {
  328 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  329 + }
  330 + defer func() {
  331 + transactionContext.RollbackTransaction()
  332 + }()
  333 + rewardRuleRepo, _ := factory.CreateRewardRuleRepository(map[string]interface{}{
  334 + "transactionContext": transactionContext,
  335 + })
  336 + _, ruleList, err := rewardRuleRepo.Find(map[string]interface{}{
  337 + "companyId": operateInfo.CompanyId,
  338 + })
  339 + if err != nil {
  340 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  341 + }
  342 + if err := transactionContext.CommitTransaction(); err != nil {
  343 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  344 + }
  345 + var newRewardRule *dto.RewardRuleInfo
  346 + if len(ruleList) > 0 {
  347 + newRewardRule = &dto.RewardRuleInfo{
  348 + RewardNum: ruleList[0].RewardNum,
  349 + RewardAmount: ruleList[0].RewardAmount,
  350 + FaultNum: ruleList[0].FaultNum,
  351 + FaultAmount: ruleList[0].RewardAmount,
  352 + Remark: ruleList[0].Remark,
  353 + }
  354 + } else {
  355 + newRewardRule = &dto.RewardRuleInfo{}
  356 + }
  357 + return newRewardRule, nil
  358 +}
1 package domain 1 package domain
2 2
  3 +import "time"
  4 +
3 //RewardStandard 奖惩规则 5 //RewardStandard 奖惩规则
4 type RewardRule struct { 6 type RewardRule struct {
  7 + Id int `json:"id"` //奖惩规则id
  8 + CompanyId int `json:"companyId"` //企业id
  9 + OrgId int `json:"orgId"` //组织ID
  10 + RewardTag string `json:"rewardTag"`
  11 + RewardNum int `json:"rewardNum"`
  12 + RewardAmount string `json:"rewardAmount"`
  13 + FaultTag string `json:"faultTag"`
  14 + FaultNum int `json:"faultNum"`
  15 + FaultAmount string `json:"faultAmount"`
  16 + Remark string `json:"remark"`
  17 + CreatedAt time.Time
  18 + UpdatedAt time.Time
  19 +}
  20 +
  21 +type RewardRuleRepository interface {
  22 + Save(param *RewardRule) (*RewardRule, error)
  23 + FindOne(queryOptions map[string]interface{}) (*RewardRule, error)
  24 + Find(queryOptions map[string]interface{}) (int64, []*RewardRule, error)
  25 +}
  26 +
  27 +func (m *RewardRule) ValidRewardTag() bool {
  28 + switch m.FaultTag {
  29 + case ">":
  30 + default:
  31 + return false
  32 + }
  33 + return true
  34 +}
  35 +
  36 +func (m *RewardRule) ValidFaultTag() bool {
  37 + switch m.FaultTag {
  38 + case ">":
  39 + default:
  40 + return false
  41 + }
  42 + return true
5 } 43 }
@@ -52,6 +52,7 @@ func init() { @@ -52,6 +52,7 @@ func init() {
52 (*models.ProductMaterialGroup)(nil), 52 (*models.ProductMaterialGroup)(nil),
53 (*models.ProductMaterial)(nil), 53 (*models.ProductMaterial)(nil),
54 (*models.RewardStandard)(nil), 54 (*models.RewardStandard)(nil),
  55 + (*models.RewardRule)(nil),
55 } { 56 } {
56 err := DB.Model(model).CreateTable(&orm.CreateTableOptions{ 57 err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
57 Temp: false, 58 Temp: false,
  1 +package models
  2 +
  3 +import (
  4 + "time"
  5 +)
  6 +
  7 +//RewardRule 奖惩规则
  8 +type RewardRule struct {
  9 + tableName string `pg:"manufacture.reward_rule,alias:reward_rule"`
  10 + Id int `pg:"pk:id"` //奖惩标准 id
  11 + CompanyId int `comment:"企业id"` // 企业id
  12 + OrgId int `comment:"组织ID"` // 组织ID
  13 + RewardTag string
  14 + RewardNum int
  15 + RewardAmount string
  16 + FaultTag string
  17 + FaultNum int
  18 + FaultAmount string
  19 + Remark string
  20 + CreatedAt time.Time
  21 + UpdatedAt time.Time
  22 +}
  1 +package repository
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/go-pg/pg/v10"
  7 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
  10 +)
  11 +
  12 +type RewardRuleRepository struct {
  13 + transactionContext *pgTransaction.TransactionContext
  14 +}
  15 +
  16 +var _ domain.RewardRuleRepository = (*RewardRuleRepository)(nil)
  17 +
  18 +func NewRewardRuleRepository(transactionContext *pgTransaction.TransactionContext) (*RewardRuleRepository, error) {
  19 + if transactionContext == nil {
  20 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  21 + } else {
  22 + return &RewardRuleRepository{
  23 + transactionContext: transactionContext,
  24 + }, nil
  25 + }
  26 +}
  27 +
  28 +// func (repo *RewardStandardRepository) nextIdentify() (int64, error) {
  29 +// IdWorker, err := snowflake.NewIdWorker(1)
  30 +// if err != nil {
  31 +// return 0, err
  32 +// }
  33 +// id, err := IdWorker.NextId()
  34 +// return id, err
  35 +// }
  36 +
  37 +func (repo *RewardRuleRepository) Save(param *domain.RewardRule) (*domain.RewardRule, error) {
  38 + m := models.RewardRule{
  39 + Id: param.Id,
  40 + CompanyId: param.CompanyId,
  41 + OrgId: param.OrgId,
  42 + RewardTag: param.RewardTag,
  43 + RewardNum: param.RewardNum,
  44 + RewardAmount: param.RewardAmount,
  45 + FaultTag: param.FaultTag,
  46 + FaultNum: param.FaultNum,
  47 + FaultAmount: param.FaultAmount,
  48 + CreatedAt: param.CreatedAt,
  49 + UpdatedAt: param.UpdatedAt,
  50 + Remark: param.Remark,
  51 + }
  52 + tx := repo.transactionContext.PgTx
  53 + if param.Id == 0 {
  54 + _, err := tx.Model(&m).Insert()
  55 + if err != nil {
  56 + return nil, err
  57 + }
  58 + param.Id = m.Id
  59 + } else {
  60 + _, err := tx.Model(&m).WherePK().Update()
  61 + if err != nil {
  62 + return nil, err
  63 + }
  64 + }
  65 +
  66 + return param, nil
  67 +}
  68 +
  69 +// func (repo *RewardRuleRepository) Remove(param *domain.RewardStandard) (*domain.RewardStandard, error) {
  70 +// tx := repo.transactionContext.PgTx
  71 +// m := new(models.RewardStandard)
  72 +// m.Id = param.Id
  73 +// nowTime := time.Now()
  74 +// param.DeletedAt = &nowTime
  75 +// _, err := tx.Model(m).
  76 +// WherePK().Set("deleted_at=?", nowTime).
  77 +// Update()
  78 +// if err != nil {
  79 +// return param, err
  80 +// }
  81 +
  82 +// return nil, nil
  83 +// }
  84 +
  85 +func (repo *RewardRuleRepository) FindOne(queryOptions map[string]interface{}) (*domain.RewardRule, error) {
  86 + tx := repo.transactionContext.PgTx
  87 + m := new(models.RewardRule)
  88 + query := tx.Model(m)
  89 + if v, ok := queryOptions["id"]; ok {
  90 + query.Where("id=?", v)
  91 + }
  92 + err := query.First()
  93 + if err != nil {
  94 + if err == pg.ErrNoRows {
  95 + return nil, domain.ErrorNotFound
  96 + } else {
  97 + return nil, err
  98 + }
  99 + }
  100 + result := repo.TransformToDomain(m)
  101 + return result, nil
  102 +}
  103 +
  104 +func (repo *RewardRuleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.RewardRule, error) {
  105 + tx := repo.transactionContext.PgTx
  106 + m := []models.RewardRule{}
  107 + query := tx.Model(&m).
  108 + Limit(20)
  109 + if v, ok := queryOptions["limit"].(int); ok {
  110 + query.Limit(v)
  111 + }
  112 + if v, ok := queryOptions["offset"].(int); ok {
  113 + query.Offset(v)
  114 + }
  115 + if v, ok := queryOptions["companyId"]; ok {
  116 + query.Where("company_id=?", v)
  117 + }
  118 + cnt, err := query.SelectAndCount()
  119 + if err != nil {
  120 + return 0, nil, err
  121 + }
  122 +
  123 + var listData []*domain.RewardRule
  124 + for i := range m {
  125 + temp := repo.TransformToDomain(&m[i])
  126 + listData = append(listData, temp)
  127 + }
  128 + return int64(cnt), listData, nil
  129 +}
  130 +
  131 +func (repo *RewardRuleRepository) TransformToDomain(param *models.RewardRule) *domain.RewardRule {
  132 + return &domain.RewardRule{
  133 + Id: param.Id,
  134 + CompanyId: param.CompanyId,
  135 + OrgId: param.OrgId,
  136 + RewardTag: param.RewardTag,
  137 + RewardNum: param.RewardNum,
  138 + RewardAmount: param.RewardAmount,
  139 + FaultTag: param.FaultTag,
  140 + FaultNum: param.FaultNum,
  141 + FaultAmount: param.FaultAmount,
  142 + CreatedAt: param.CreatedAt,
  143 + UpdatedAt: param.UpdatedAt,
  144 + Remark: param.Remark,
  145 + }
  146 +}
@@ -65,3 +65,21 @@ func (c RewardStandardController) ListRewardStandard() { @@ -65,3 +65,21 @@ func (c RewardStandardController) ListRewardStandard() {
65 total, data, err := srv.ListRewardStandard(cmd) 65 total, data, err := srv.ListRewardStandard(cmd)
66 ResponseGrid(c.BaseController, total, data, err) 66 ResponseGrid(c.BaseController, total, data, err)
67 } 67 }
  68 +
  69 +//SaveRewardRule 保存奖惩规则
  70 +func (c RewardStandardController) SaveRewardRule() {
  71 + srv := service.NewRewardStandardService(nil)
  72 + createCommand := &command.SaveRewardRuleCommand{}
  73 + Must(c.Unmarshal(createCommand))
  74 + operater := ParseOperateInfo(c.BaseController)
  75 + data, err := srv.SaveRewardRule(operater, createCommand)
  76 + c.Response(data, err)
  77 +}
  78 +
  79 +//GetRewardRule 获取奖惩规则
  80 +func (c RewardStandardController) GetRewardRule() {
  81 + srv := service.NewRewardStandardService(nil)
  82 + operater := ParseOperateInfo(c.BaseController)
  83 + data, err := srv.GetRewardRule(operater)
  84 + c.Response(data, err)
  85 +}
@@ -11,5 +11,6 @@ func init() { @@ -11,5 +11,6 @@ func init() {
11 web.Router("/reward-standard/:id", &controllers.RewardStandardController{}, "Get:GetRewardStandard") 11 web.Router("/reward-standard/:id", &controllers.RewardStandardController{}, "Get:GetRewardStandard")
12 web.Router("/reward-standard/remove", &controllers.RewardStandardController{}, "Post:DeleteRewardStandard") 12 web.Router("/reward-standard/remove", &controllers.RewardStandardController{}, "Post:DeleteRewardStandard")
13 web.Router("/reward-standard/search", &controllers.RewardStandardController{}, "Post:ListRewardStandard") 13 web.Router("/reward-standard/search", &controllers.RewardStandardController{}, "Post:ListRewardStandard")
14 - 14 + web.Router("/reward-rule/", &controllers.RewardStandardController{}, "Post:SaveRewardRule")
  15 + web.Router("/reward-rule/", &controllers.RewardStandardController{}, "Get:GetRewardRule")
15 } 16 }