作者 Your Name

更新统计

package domain
import (
"time"
)
// RewardSummary 功过奖惩明细
type RewardSummary struct {
Id int `json:"id"`
CompanyId int `json:"companyId"`
OrgId int `json:"orgId"`
RecordDate time.Time `json:"recordDate"` //日期
WorkStation WorkStation `json:"workStation"` // 工作位置
Worker User `json:"user"` //员工
UpToStandard float64 `json:"upToStandard"` //合格率
Yield float64 `json:"yield"` //产能
AccidentNum1 int `json:"accidentNum1"` //质量事故 次数
AccidentAmount1 float64 `json:"accidentAmount1"` //质量事故 损失金额
AccidentNum2 int `json:"accidentNum2"` //安全事故 次数
AccidentAmount2 float64 `json:"accidentAmount2"` //安全事故 损失金额
AccidentNum3 int `json:"accidentNum3"` //异物金属事故 次数
AccidentNum4 int `json:"accidentNum4"` //异物非金属事故 次数
SummaryResult float64 `json:"summaryResult"` //奖惩计算结果
CreatedAt time.Time `json:"createdAt"` //
UpdatedAt time.Time `json:"UpdatedAt"` //
}
type RewardSummaryRepository interface {
Save(param *RewardSummary) (*RewardSummary, error)
FindOne(queryOptions map[string]interface{}) (*RewardSummary, error)
Find(queryOptions map[string]interface{}) (int64, []*RewardSummary, error)
}
... ...
package models
import (
"time"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
)
// RewardSummary 功过奖惩明细
type RewardSummary struct {
tableName string `pg:"manufacture.reward_summary,alias:reward_summary"`
Id int `pg:"pk:id"`
CompanyId int ``
OrgId int ``
RecordDate time.Time //日期
WorkStation domain.WorkStation // 工作位置
Worker domain.User //员工
UpToStandard float64 //合格率
Yield float64 //产能
AccidentNum1 int //质量事故 次数
AccidentAmount1 float64 //质量事故 损失金额
AccidentNum2 int //安全事故 次数
AccidentAmount2 float64 //安全事故 损失金额
AccidentNum3 int //异物金属事故 次数
AccidentNum4 int //异物非金属事故 次数
CreatedAt time.Time //
UpdatedAt time.Time //
SummaryResult float64 //奖惩计算结果
}
... ...
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 RewardSummaryRepository struct {
transactionContext *pgTransaction.TransactionContext
}
var _ domain.RewardSummaryRepository = (*RewardSummaryRepository)(nil)
func NewRewardSummaryRepository(transactionContext *pgTransaction.TransactionContext) (*RewardSummaryRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &RewardSummaryRepository{
transactionContext: transactionContext,
}, nil
}
}
func (repo *RewardSummaryRepository) Save(param *domain.RewardSummary) (*domain.RewardSummary, error) {
m := models.RewardSummary{
Id: param.Id,
CompanyId: param.CompanyId,
OrgId: param.OrgId,
RecordDate: param.RecordDate,
WorkStation: param.WorkStation,
Worker: param.Worker,
UpToStandard: param.UpToStandard,
Yield: param.Yield,
AccidentNum1: param.AccidentNum1,
AccidentAmount1: param.AccidentAmount1,
AccidentNum2: param.AccidentNum2,
AccidentAmount2: param.AccidentAmount2,
AccidentNum3: param.AccidentNum3,
AccidentNum4: param.AccidentNum4,
CreatedAt: param.CreatedAt,
UpdatedAt: param.UpdatedAt,
SummaryResult: param.SummaryResult,
}
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 *RewardSummaryRepository) FindOne(queryOptions map[string]interface{}) (*domain.RewardSummary, error) {
tx := repo.transactionContext.PgTx
m := new(models.RewardSummary)
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 *RewardSummaryRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.RewardSummary, error) {
tx := repo.transactionContext.PgTx
m := []models.RewardSummary{}
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)
}
if v, ok := queryOptions["orgId"]; ok {
query.Where("org_id=?", v)
}
if v, ok := queryOptions["workshopName"]; ok && len(v.(string)) > 0 {
query.Where(`Work_station->>'workshopName' like '%?%'`, v)
}
cnt, err := query.SelectAndCount()
if err != nil {
return 0, nil, err
}
var listData []*domain.RewardSummary
for i := range m {
temp := repo.TransformToDomain(&m[i])
listData = append(listData, temp)
}
return int64(cnt), listData, nil
}
func (repo *RewardSummaryRepository) TransformToDomain(param *models.RewardSummary) *domain.RewardSummary {
return &domain.RewardSummary{
Id: param.Id,
CompanyId: param.CompanyId,
OrgId: param.OrgId,
RecordDate: param.RecordDate,
WorkStation: param.WorkStation,
Worker: param.Worker,
UpToStandard: param.UpToStandard,
Yield: param.Yield,
AccidentNum1: param.AccidentNum1,
AccidentAmount1: param.AccidentAmount1,
AccidentNum2: param.AccidentNum2,
AccidentAmount2: param.AccidentAmount2,
AccidentNum3: param.AccidentNum3,
AccidentNum4: param.AccidentNum4,
CreatedAt: param.CreatedAt,
UpdatedAt: param.UpdatedAt,
SummaryResult: param.SummaryResult,
}
}
... ...