作者 Your Name

更新统计

  1 +package domain
  2 +
  3 +import (
  4 + "time"
  5 +)
  6 +
  7 +// RewardSummary 功过奖惩明细
  8 +type RewardSummary struct {
  9 + Id int `json:"id"`
  10 + CompanyId int `json:"companyId"`
  11 + OrgId int `json:"orgId"`
  12 + RecordDate time.Time `json:"recordDate"` //日期
  13 + WorkStation WorkStation `json:"workStation"` // 工作位置
  14 + Worker User `json:"user"` //员工
  15 + UpToStandard float64 `json:"upToStandard"` //合格率
  16 + Yield float64 `json:"yield"` //产能
  17 + AccidentNum1 int `json:"accidentNum1"` //质量事故 次数
  18 + AccidentAmount1 float64 `json:"accidentAmount1"` //质量事故 损失金额
  19 + AccidentNum2 int `json:"accidentNum2"` //安全事故 次数
  20 + AccidentAmount2 float64 `json:"accidentAmount2"` //安全事故 损失金额
  21 + AccidentNum3 int `json:"accidentNum3"` //异物金属事故 次数
  22 + AccidentNum4 int `json:"accidentNum4"` //异物非金属事故 次数
  23 + SummaryResult float64 `json:"summaryResult"` //奖惩计算结果
  24 + CreatedAt time.Time `json:"createdAt"` //
  25 + UpdatedAt time.Time `json:"UpdatedAt"` //
  26 +}
  27 +
  28 +type RewardSummaryRepository interface {
  29 + Save(param *RewardSummary) (*RewardSummary, error)
  30 + FindOne(queryOptions map[string]interface{}) (*RewardSummary, error)
  31 + Find(queryOptions map[string]interface{}) (int64, []*RewardSummary, error)
  32 +}
  1 +package models
  2 +
  3 +import (
  4 + "time"
  5 +
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  7 +)
  8 +
  9 +// RewardSummary 功过奖惩明细
  10 +type RewardSummary struct {
  11 + tableName string `pg:"manufacture.reward_summary,alias:reward_summary"`
  12 + Id int `pg:"pk:id"`
  13 + CompanyId int ``
  14 + OrgId int ``
  15 + RecordDate time.Time //日期
  16 + WorkStation domain.WorkStation // 工作位置
  17 + Worker domain.User //员工
  18 + UpToStandard float64 //合格率
  19 + Yield float64 //产能
  20 + AccidentNum1 int //质量事故 次数
  21 + AccidentAmount1 float64 //质量事故 损失金额
  22 + AccidentNum2 int //安全事故 次数
  23 + AccidentAmount2 float64 //安全事故 损失金额
  24 + AccidentNum3 int //异物金属事故 次数
  25 + AccidentNum4 int //异物非金属事故 次数
  26 + CreatedAt time.Time //
  27 + UpdatedAt time.Time //
  28 + SummaryResult float64 //奖惩计算结果
  29 +}
  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 RewardSummaryRepository struct {
  13 + transactionContext *pgTransaction.TransactionContext
  14 +}
  15 +
  16 +var _ domain.RewardSummaryRepository = (*RewardSummaryRepository)(nil)
  17 +
  18 +func NewRewardSummaryRepository(transactionContext *pgTransaction.TransactionContext) (*RewardSummaryRepository, error) {
  19 + if transactionContext == nil {
  20 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  21 + } else {
  22 + return &RewardSummaryRepository{
  23 + transactionContext: transactionContext,
  24 + }, nil
  25 + }
  26 +}
  27 +
  28 +func (repo *RewardSummaryRepository) Save(param *domain.RewardSummary) (*domain.RewardSummary, error) {
  29 + m := models.RewardSummary{
  30 + Id: param.Id,
  31 + CompanyId: param.CompanyId,
  32 + OrgId: param.OrgId,
  33 + RecordDate: param.RecordDate,
  34 + WorkStation: param.WorkStation,
  35 + Worker: param.Worker,
  36 + UpToStandard: param.UpToStandard,
  37 + Yield: param.Yield,
  38 + AccidentNum1: param.AccidentNum1,
  39 + AccidentAmount1: param.AccidentAmount1,
  40 + AccidentNum2: param.AccidentNum2,
  41 + AccidentAmount2: param.AccidentAmount2,
  42 + AccidentNum3: param.AccidentNum3,
  43 + AccidentNum4: param.AccidentNum4,
  44 + CreatedAt: param.CreatedAt,
  45 + UpdatedAt: param.UpdatedAt,
  46 + SummaryResult: param.SummaryResult,
  47 + }
  48 + tx := repo.transactionContext.PgTx
  49 + if param.Id == 0 {
  50 + _, err := tx.Model(&m).Insert()
  51 + if err != nil {
  52 + return nil, err
  53 + }
  54 + param.Id = m.Id
  55 + } else {
  56 + _, err := tx.Model(&m).WherePK().Update()
  57 + if err != nil {
  58 + return nil, err
  59 + }
  60 + }
  61 +
  62 + return param, nil
  63 +}
  64 +
  65 +func (repo *RewardSummaryRepository) FindOne(queryOptions map[string]interface{}) (*domain.RewardSummary, error) {
  66 + tx := repo.transactionContext.PgTx
  67 + m := new(models.RewardSummary)
  68 + query := tx.Model(m)
  69 + if v, ok := queryOptions["id"]; ok {
  70 + query.Where("id=?", v)
  71 + }
  72 + err := query.First()
  73 + if err != nil {
  74 + if err == pg.ErrNoRows {
  75 + return nil, domain.ErrorNotFound
  76 + } else {
  77 + return nil, err
  78 + }
  79 + }
  80 + result := repo.TransformToDomain(m)
  81 + return result, nil
  82 +}
  83 +
  84 +func (repo *RewardSummaryRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.RewardSummary, error) {
  85 + tx := repo.transactionContext.PgTx
  86 + m := []models.RewardSummary{}
  87 + query := tx.Model(&m).
  88 + Limit(20)
  89 + if v, ok := queryOptions["limit"].(int); ok {
  90 + query.Limit(v)
  91 + }
  92 + if v, ok := queryOptions["offset"].(int); ok {
  93 + query.Offset(v)
  94 + }
  95 + if v, ok := queryOptions["companyId"]; ok {
  96 + query.Where("company_id=?", v)
  97 + }
  98 + if v, ok := queryOptions["orgId"]; ok {
  99 + query.Where("org_id=?", v)
  100 + }
  101 +
  102 + if v, ok := queryOptions["workshopName"]; ok && len(v.(string)) > 0 {
  103 + query.Where(`Work_station->>'workshopName' like '%?%'`, v)
  104 + }
  105 + cnt, err := query.SelectAndCount()
  106 + if err != nil {
  107 + return 0, nil, err
  108 + }
  109 +
  110 + var listData []*domain.RewardSummary
  111 + for i := range m {
  112 + temp := repo.TransformToDomain(&m[i])
  113 + listData = append(listData, temp)
  114 + }
  115 + return int64(cnt), listData, nil
  116 +}
  117 +
  118 +func (repo *RewardSummaryRepository) TransformToDomain(param *models.RewardSummary) *domain.RewardSummary {
  119 + return &domain.RewardSummary{
  120 + Id: param.Id,
  121 + CompanyId: param.CompanyId,
  122 + OrgId: param.OrgId,
  123 + RecordDate: param.RecordDate,
  124 + WorkStation: param.WorkStation,
  125 + Worker: param.Worker,
  126 + UpToStandard: param.UpToStandard,
  127 + Yield: param.Yield,
  128 + AccidentNum1: param.AccidentNum1,
  129 + AccidentAmount1: param.AccidentAmount1,
  130 + AccidentNum2: param.AccidentNum2,
  131 + AccidentAmount2: param.AccidentAmount2,
  132 + AccidentNum3: param.AccidentNum3,
  133 + AccidentNum4: param.AccidentNum4,
  134 + CreatedAt: param.CreatedAt,
  135 + UpdatedAt: param.UpdatedAt,
  136 + SummaryResult: param.SummaryResult,
  137 + }
  138 +}