作者 tangxvhui

日常保存

... ... @@ -36,3 +36,8 @@ func (attendanceService *AttendanceService) WorkshopWorkTimeRecordStatics(cmd *c
}
return struct{}{}, nil
}
//导入工时数据
func (attendanceService *AttendanceService) ImportDataAttendance() error {
return nil
}
... ...
... ... @@ -157,3 +157,11 @@ func CreateProductMaterialRepository(options map[string]interface{}) (domain.Pro
}
return repository.NewProductMaterialRepository(transactionContext)
}
func CreateRewardStandardRepository(options map[string]interface{}) (domain.RewardStandardRepository, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewRewardStandardRepository(transactionContext)
}
... ...
package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type SaveRewardStandardCommand struct {
Id int `json:"id"` //奖惩标准 id
WorkshopId int `json:"workshopId" valid:"Required"` //车间id
LineId int `json:"lineId" valid:"Required"` //生产线ID
SectionId int `json:"sectionId" valid:"Required"` //工段ID
Remark string `json:"remark"` //备注
TargetType int `json:"targetType" valid:"Required"` //指标类别 1:产效 2:合格率 3:安全事故 4:质量事故 5:异物次数
TargeVal1 string `json:"targeVal1"` //填写的指标值1
TargeVal2 string `json:"targeVal2"` //填写的指标值2
TargeVal3 string `json:"targeVal3"` //填写的指标值3
TargeVal4 string `json:"targeVal4"` //填写的指标值4
}
func (removeProductCommand *SaveRewardStandardCommand) Valid(validation *validation.Validation) {
}
func (removeProductCommand *SaveRewardStandardCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(removeProductCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(removeProductCommand).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
package dto
type RewardStandardInfo struct {
Id int `json:"id"` //奖惩标准id
CompanyId int `json:"companyId"` //企业id
OrgId int `json:"orgId"` //组织ID
WorkshopId int `json:"workshopId"` //车间id
WorkshopName string `json:"workshopName"` //
LineId int `json:"lineId"` //生产线ID
LineName string `json:"lineName"` //
SectionId int `json:"sectionId"` //工段ID
SectionName string `json:"sectionName"` //
Remark string `json:"remark"` //备注
TargetType int `json:"targetType"` //指标类别 1:产效 2:合格率 3:安全事故 4:质量事故 5:异物次数
TargeVal1 string `json:"targeVal1"` //填写的指标值1
TargeVal2 string `json:"targeVal2"` //填写的指标值2
TargeVal3 string `json:"targeVal3"` //填写的指标值3
TargeVal4 string `json:"targeVal4"` //填写的指标值4
}
... ...
package query
type GetRewardStandard struct {
Id int `json:"id"`
}
... ...
package service
import (
"time"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/rewardStandard/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/rewardStandard/dto"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/rewardStandard/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
)
type RewardStandardService struct{}
func NewProductService(options map[string]interface{}) *RewardStandardService {
newService := &RewardStandardService{}
return newService
}
//SaveRewardStandard 保存奖惩标准数据
func (srv RewardStandardService) SaveRewardStandard(operateInfo domain.OperateInfo, param *command.SaveRewardStandardCommand) (*command.SaveRewardStandardCommand, error) {
if err := param.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.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()
}()
rewardStandardRepo, _ := factory.CreateRewardStandardRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
_, dataList, err := rewardStandardRepo.Find(map[string]interface{}{
"limit": 1,
"workshopId": param.WorkshopId,
"lineId": param.LineId,
"sectionId": param.SectionId,
"targetType": param.TargetType,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if len(dataList) > 0 {
if dataList[0].Id != param.Id {
return nil, application.ThrowError(application.BUSINESS_ERROR, "当前指标已存在")
}
}
var rewardStandardData *domain.RewardStandard
if param.Id > 0 {
rewardStandardData, err = rewardStandardRepo.FindOne(map[string]interface{}{
"id": param.Id,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, "指标数据不存在"+err.Error())
}
if rewardStandardData.CompanyId != operateInfo.CompanyId {
return nil, application.ThrowError(application.BUSINESS_ERROR, "指标数据填写错误")
}
} else {
rewardStandardData = &domain.RewardStandard{
CreatedAt: time.Now(),
}
}
workShopRepo, _ := factory.CreateWorkshopRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
//获取车间数据
workShopData, err := workShopRepo.FindOne(map[string]interface{}{
"workshopId": param.WorkshopId,
})
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, "车间数据不能存在"+err.Error())
}
if workShopData.CompanyId != operateInfo.CompanyId {
return nil, application.ThrowError(application.ARG_ERROR, "车间数据填写错误")
}
productLineData, err := workShopData.FindLine(param.LineId)
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
productSection, err := productLineData.FindSection(param.SectionId)
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
rewardStandardData.UpdatedAt = time.Now()
rewardStandardData.ProductSection = *productSection
rewardStandardData.ProductLine = productLineData.SimpleProductLine()
rewardStandardData.Workshop = workShopData.SimpleWorkshop()
rewardStandardData.Remark = param.Remark
err = rewardStandardData.UpdateTarge(
param.TargetType,
param.TargeVal1,
param.TargeVal2,
param.TargeVal3,
param.TargeVal4)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
_, err = rewardStandardRepo.Save(rewardStandardData)
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 nil, nil
}
//GetRewardStandard 根据id获取奖惩标准数据
func (srv RewardStandardService) GetRewardStandard(param *query.GetRewardStandard) (*dto.RewardStandardInfo, 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()
}()
rewardStandardRepo, _ := factory.CreateRewardStandardRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
rewardStandardData, err := rewardStandardRepo.FindOne(map[string]interface{}{
"id": param.Id,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, "指标数据不存在"+err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
result := dto.RewardStandardInfo{
Id: rewardStandardData.Id,
WorkshopId: rewardStandardData.Workshop.WorkshopId,
WorkshopName: rewardStandardData.Workshop.WorkshopName,
LineId: rewardStandardData.ProductLine.LineId,
LineName: rewardStandardData.ProductLine.LineName,
SectionId: rewardStandardData.ProductSection.SectionId,
SectionName: rewardStandardData.ProductSection.SectionName,
Remark: rewardStandardData.Remark,
TargetType: rewardStandardData.TargetType,
TargeVal1: rewardStandardData.TargeVal1,
TargeVal2: rewardStandardData.TargeVal2,
TargeVal3: rewardStandardData.TargeVal3,
TargeVal4: rewardStandardData.TargeVal4,
}
return &result, nil
}
//DeleteRewardStandard 根据id删除奖惩标准
func (srv RewardStandardService) DeleteRewardStandard(param *query.GetRewardStandard) (*domain.RewardStandard, 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()
}()
rewardStandardRepo, _ := factory.CreateRewardStandardRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
rewardStandardData, err := rewardStandardRepo.FindOne(map[string]interface{}{
"id": param.Id,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, "指标数据不存在"+err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return rewardStandardData, nil
}
... ...
... ... @@ -14,6 +14,12 @@ type ProductLine struct {
Removed int `json:"removed,omitempty"`
}
//输出简化的模型
type SimpleProductLine struct {
LineId int `json:"lineId"` // 生产线ID
LineName string `json:"lineName"` // 生产线名称
}
// 查询生产线
func (productLine *ProductLine) FindSection(sectionId int) (*ProductSection, error) {
for i := range productLine.ProductSections {
... ... @@ -37,3 +43,11 @@ func (productLine *ProductLine) GetProductSections(removed int) []*ProductSectio
}
return result
}
//SimpleProductLine 输出简化的模型
func (productLine *ProductLine) SimpleProductLine() SimpleProductLine {
return SimpleProductLine{
LineId: productLine.LineId,
LineName: productLine.LineName,
}
}
... ...
package domain
//RewardStandard 奖惩规则
type RewardRule struct {
}
... ...
package domain
import (
"errors"
"strconv"
"time"
)
//RewardStandard 奖惩标准
type RewardStandard struct {
Id int `json:"id"` //奖惩标准id
CompanyId int `json:"companyId"` //企业id
OrgId int `json:"orgId"` //组织ID
Workshop SimpleWorkshop `json:"workshop"` //车间id
ProductLine SimpleProductLine `json:"productLine"` //生产线
ProductSection ProductSection `json:"ProductSection"` //工段
Remark string `json:"remark"` //备注
TargetType int `json:"targetType"` //指标类别 1:产效 2:合格率 3:安全事故 4:质量事故 5:异物次数
TargeVal1 string `json:"targeVal1"` //填写的指标值1
TargeVal2 string `json:"targeVal2"` //填写的指标值2
TargeVal3 string `json:"targeVal3"` //填写的指标值3
TargeVal4 string `json:"targeVal4"` //填写的指标值4
CreatedAt time.Time `json:"createdAt"` //创建时间
UpdatedAt time.Time `json:"updatedAt"` //更新时间
DeletedAt *time.Time `json:"-"` //删除时间
}
/*
填写的指标值描述
产效=> 功: 大于等于 x kg/小时;过:小于 x kg/小时
合格率=> 功:大于等于 x% ;过:小于 x%
安全事故 => 功:小于等于 x 次 或者 损失小于等于 x 元;过:大于 x 次 或 损失大于 x 元
质量事故 => 功:小于等于 x 次 或者 损失 小于等于 x 元;过:大于 x 次 或者 损失大于 x 元
异物次数 => 功:发现金属小于等于 x 次 或者 发现非金属小于等于 x 次
过:发现金属大于 x 次 或 发现非金数大于 x 次
*/
type RewardStandardRepository interface {
Save(param *RewardStandard) (*RewardStandard, error)
Remove(param *RewardStandard) (*RewardStandard, error)
FindOne(queryOptions map[string]interface{}) (*RewardStandard, error)
Find(queryOptions map[string]interface{}) (int64, []*RewardStandard, error)
}
//指标类别 1:产效 2:合格率 3:安全事故 4:质量事故 5:异物次数
const (
TargetType1 int = 1
TargetType2 int = 2
TargetType3 int = 3
TargetType4 int = 4
TargetType5 int = 5
)
//UpdateTarge 更新指标内容
func (m *RewardStandard) UpdateTarge(targetType int, targeVal1 string, targeVal2 string, targeVal3 string, targeVal4 string) error {
switch targetType {
case TargetType1, TargetType2:
if len(targeVal1) == 0 || len(targeVal2) == 0 {
return errors.New("功过指标内容不能为空")
}
_, err := strconv.Atoi(targeVal1)
if err != nil {
return errors.New("功过指标内容需要为整数")
}
_, err = strconv.Atoi(targeVal2)
if err != nil {
return errors.New("功过指标内容需要为整数")
}
m.TargetType = targetType
m.TargeVal1 = targeVal1
m.TargeVal2 = targeVal2
m.TargeVal3 = ""
m.TargeVal4 = ""
case TargetType3, TargetType4, TargetType5:
if len(targeVal1) == 0 || len(targeVal2) == 0 || len(targeVal3) == 0 || len(targeVal4) == 0 {
return errors.New("功过指标内容不能为空")
}
_, err := strconv.Atoi(targeVal1)
if err != nil {
return errors.New("功过指标内容需要为整数")
}
_, err = strconv.Atoi(targeVal2)
if err != nil {
return errors.New("功过指标内容需要为整数")
}
_, err = strconv.Atoi(targeVal3)
if err != nil {
return errors.New("功过指标内容需要为整数")
}
_, err = strconv.Atoi(targeVal4)
if err != nil {
return errors.New("功过指标内容需要为整数")
}
m.TargetType = targetType
m.TargeVal1 = targeVal1
m.TargeVal2 = targeVal2
m.TargeVal3 = targeVal3
m.TargeVal4 = targeVal4
default:
return errors.New("指标类型填写错误")
}
return nil
}
func (m *RewardStandard) TargetTypeName() string {
switch m.TargetType {
case TargetType1:
return "产效"
case TargetType2:
return "合格率"
case TargetType3:
return "安全事故"
case TargetType4:
return "质量事故"
case TargetType5:
return "异物次数"
}
return ""
}
... ...
... ... @@ -39,6 +39,13 @@ type Workshop struct {
Ext *Ext `json:"ext,omitempty"`
}
type SimpleWorkshop struct {
// 车间ID
WorkshopId int `json:"workshopId"`
// 车间名称
WorkshopName string `json:"workshopName"`
}
type WorkshopRepository interface {
Save(workshop *Workshop) (*Workshop, error)
Remove(workshop *Workshop) (*Workshop, error)
... ... @@ -301,3 +308,11 @@ func (workshop *Workshop) CloneSample() *Workshop {
WorkshopName: workshop.WorkshopName,
}
}
//SimpleWorkshop 输出简化的模型
func (workShop *Workshop) SimpleWorkshop() SimpleWorkshop {
return SimpleWorkshop{
WorkshopId: workShop.WorkshopId,
WorkshopName: workShop.WorkshopName,
}
}
... ...
package models
import (
"time"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
)
//RewardStandard 奖惩标准
type RewardStandard struct {
tableName string `pg:"manufacture.reward_standard,alias:reward_standard"`
Id int `pg:"pk:id"` //奖惩标准 id
Workshop domain.SimpleWorkshop //车间id
ProductLine domain.SimpleProductLine //生产线
ProductSection domain.ProductSection //工段
Remark string //备注
TargetType int //指标类别 1:产效 2:合格率 3:安全事故 4:质量事故 5:异物次数
TargeVal1 string //填写的指标值1
TargeVal2 string //填写的指标值2
TargeVal3 string //填写的指标值3
TargeVal4 string //填写的指标值4
CreatedAt time.Time //创建时间
UpdatedAt time.Time //更新时间
DeletedAt *time.Time //删除时间
}
... ...
package repository
import (
"fmt"
"time"
"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 RewardStandardRepository struct {
transactionContext *pgTransaction.TransactionContext
}
var _ domain.RewardStandardRepository = (*RewardStandardRepository)(nil)
func NewRewardStandardRepository(transactionContext *pgTransaction.TransactionContext) (*RewardStandardRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &RewardStandardRepository{
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 *RewardStandardRepository) Save(param *domain.RewardStandard) (*domain.RewardStandard, error) {
m := models.RewardStandard{
Id: param.Id,
Workshop: param.Workshop,
ProductLine: param.ProductLine,
ProductSection: param.ProductSection,
Remark: param.Remark,
TargetType: param.TargetType,
TargeVal1: param.TargeVal1,
TargeVal2: param.TargeVal2,
TargeVal3: param.TargeVal3,
TargeVal4: param.TargeVal4,
CreatedAt: param.CreatedAt,
UpdatedAt: param.UpdatedAt,
DeletedAt: param.DeletedAt,
}
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 *RewardStandardRepository) 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 *RewardStandardRepository) FindOne(queryOptions map[string]interface{}) (*domain.RewardStandard, error) {
tx := repo.transactionContext.PgTx
m := new(models.RewardStandard)
query := tx.Model(m).Where("deleted_at isnull")
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 *RewardStandardRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.RewardStandard, error) {
tx := repo.transactionContext.PgTx
m := []models.RewardStandard{}
query := tx.Model(&m).
Where("deleted_at isnull").
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["workshopId"]; ok {
query.Where("work_shop->>'workshopId'='?'", v)
}
if v, ok := queryOptions["lineId"]; ok {
query.Where("product_line->>lineId='?'", v)
}
if v, ok := queryOptions["sectionId"]; ok {
query.Where("product_section->>sectionId='?'", v)
}
if v, ok := queryOptions["targetType"]; ok {
query.Where("target_type=?", v)
}
cnt, err := query.SelectAndCount()
if err != nil {
return 0, nil, err
}
var listData []*domain.RewardStandard
for i := range m {
temp := repo.TransformToDomain(&m[i])
listData = append(listData, temp)
}
return int64(cnt), listData, nil
}
func (repo *RewardStandardRepository) TransformToDomain(param *models.RewardStandard) *domain.RewardStandard {
return &domain.RewardStandard{
Id: param.Id,
Workshop: param.Workshop,
ProductLine: param.ProductLine,
ProductSection: param.ProductSection,
Remark: param.Remark,
TargetType: param.TargetType,
TargeVal1: param.TargeVal1,
TargeVal2: param.TargeVal2,
TargeVal3: param.TargeVal3,
TargeVal4: param.TargeVal4,
CreatedAt: param.CreatedAt,
UpdatedAt: param.UpdatedAt,
DeletedAt: param.DeletedAt,
}
}
... ...