作者 Your Name

更新 奖惩汇总

... ... @@ -181,3 +181,11 @@ func CreateProductTroubleRepository(options map[string]interface{}) (domain.Prod
}
return repository.NewProductTroubleRepository(transactionContext)
}
func CreateRewardSummaryRepository(options map[string]interface{}) (domain.RewardSummaryRepository, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewRewardSummaryRepository(transactionContext)
}
... ...
package dto
type RewardSummaryList struct {
Id int `json:"id"`
RecordDate string `json:"recordDate"` //日期
WorkshopName string `json:"workshopName"` //车间名称
LineName string `json:"lineName"` //线别名称
SectionName string `json:"sectionName"` //工段
Yield string `json:"yield"` //产效
UpToStandard string `json:"upToStandard"` //合格率
Accident1 string `json:"accident1"` //质量事故
Accident2 string `json:"accident2"` //安全事故
Accident3 string `json:"accident3"` //异物事故
SummaryResult string `json:"summaryResult"` //奖惩结果
ResultDesc string `json:"resultDesc"` //产效结果
}
... ...
package query
type ListRewardSummaryQuery struct {
// 当前公司
CompanyId int `cname:"当前公司" json:"companyId"`
// 当前登录的组织
OrgId int `cname:"当前登录的组织" json:"orgId"`
// 页码
PageNumber int `cname:"页码" json:"pageNumber"`
// 页数
PageSize int `cname:"页数" json:"pageSize"`
// 车间名称
WorkshopName string `cname:"车间名称" json:"workshopName"`
LineName string `cname:"线别名称" json:"workerName"`
BeginDate string `cname:"开始的日期" json:"beginDate"`
EndDate string `cname:"结束的日期" json:"endDate"`
}
... ...
package service
import (
"fmt"
"strconv"
"strings"
"time"
... ... @@ -83,7 +84,7 @@ func (srv ProductTroubleService) SaveProductTrouble(operateInfo *domain.OperateI
troubleData.AmountLoss = param.AmountLoss
troubleData.ProductWorker = *workerUser
troubleData.RecordData = recordDate
troubleData.RecordDate = recordDate
troubleData.Remark = param.Remark
troubleData.UpdatedAt = time.Now()
troubleData.WorkStation = *workStation
... ... @@ -149,7 +150,7 @@ func (srv ProductTroubleService) GetProductTrouble(id int) (*dto.ProductTroubleI
SectionId: troubleData.WorkStation.SectionId,
SectionName: troubleData.WorkStation.SectionName,
Remark: troubleData.Remark,
ProductDate: troubleData.RecordData.Format("2006-01-02"),
ProductDate: troubleData.RecordDate.Format("2006-01-02"),
AmountLoss: troubleData.AmountLoss,
Types: string(troubleData.Types),
WorkerId: troubleData.ProductWorker.UserId,
... ... @@ -188,10 +189,7 @@ func (srv ProductTroubleService) DeleteProductTrouble(id int64) error {
return nil
}
// func (srv ProductTroubleService) approveProductTrouble(operateInfo *domain.OperateInfo, param *domain.ProductTrouble) error {
// return nil
// }
// 审核事故
func (srv ProductTroubleService) ApproveProductTrouble(operateInfo *domain.OperateInfo, id int64) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
... ... @@ -228,6 +226,11 @@ func (srv ProductTroubleService) ApproveProductTrouble(operateInfo *domain.Opera
if err != nil {
return application.ThrowError(application.ARG_ERROR, err.Error())
}
//汇总奖惩明细
err = srv.CreateRewardSummaryByProductTrouble(troubleData)
if err != nil {
return application.ThrowError(application.ARG_ERROR, "计算功过奖惩明细失败,"+err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
... ... @@ -286,7 +289,7 @@ func (srv ProductTroubleService) ListProductTrouble(param *query.ListProductTrou
SectionId: v.WorkStation.SectionId,
SectionName: v.WorkStation.SectionName,
Remark: v.Remark,
ProductDate: v.RecordData.Format("2006-01-02"),
ProductDate: v.RecordDate.Format("2006-01-02"),
AmountLoss: v.AmountLoss,
Types: v.GetTypesName(),
WorkerId: v.ProductWorker.UserId,
... ... @@ -415,7 +418,7 @@ func (srv ProductTroubleService) BatchAddProductTrouble(operateInfo *domain.Oper
ProductWorker: *worker,
AmountLoss: amountLoss,
Types: "",
RecordData: recordDate,
RecordDate: recordDate,
Remark: "",
ApproveStatus: domain.TroubleIsApprove,
ApproveAt: &nowTime,
... ... @@ -439,15 +442,180 @@ func (srv ProductTroubleService) BatchAddProductTrouble(operateInfo *domain.Oper
productTroubleRepo, _ := factory.CreateProductTroubleRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
//添加事故数据
for i := range troubleDataList {
//添加事故数据
_, err = productTroubleRepo.Save(troubleDataList[i])
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
//汇总奖惩明细
err = srv.CreateRewardSummaryByProductTrouble(troubleDataList[i])
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, "计算功过奖惩明细失败,"+err.Error())
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 奖惩汇总数据列表
func (srv ProductTroubleService) ListRewardSummary(param query.ListRewardSummaryQuery) (int64, []dto.RewardSummaryList, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
rewardSummaryRepo, _ := factory.CreateRewardSummaryRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
limit := param.PageSize
offset := param.PageSize * (param.PageNumber - 1)
condition := map[string]interface{}{
"orgId": param.OrgId,
"companyId": param.CompanyId,
"offset": offset,
"limit": limit,
}
//搜索条件日期
if len(param.BeginDate) > 0 {
_, err = time.Parse("2006-01-02", param.BeginDate)
if err != nil {
return 0, nil, application.ThrowError(application.ARG_ERROR, "日期格式错误")
}
condition["beginDate"] = param.BeginDate
}
//搜索条件日期
if len(param.EndDate) > 0 {
_, err = time.Parse("2006-01-02", param.EndDate)
if err != nil {
return 0, nil, application.ThrowError(application.ARG_ERROR, "日期格式错误")
}
condition["endDate"] = param.EndDate
}
//搜索条件 车间名称
if len(param.WorkshopName) > 0 {
condition["workshopName"] = param.WorkshopName
}
//搜索条件 线别名称
if len(param.LineName) > 0 {
condition["lineName"] = param.LineName
}
cnt, rewardSummaryList, err := rewardSummaryRepo.Find(condition)
if err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
result := []dto.RewardSummaryList{}
for _, v := range rewardSummaryList {
item := dto.RewardSummaryList{
Id: v.Id,
RecordDate: v.RecordDate.Format("2006-01-02"),
WorkshopName: v.WorkStation.WorkshopName,
LineName: v.WorkStation.LineName,
SectionName: v.WorkStation.SectionName,
Yield: fmt.Sprintf("%.2f", v.Yield),
UpToStandard: fmt.Sprintf("%.2f%%", v.UpToStandard),
Accident1: fmt.Sprintf("%d次%.2f元", v.AccidentNum1, v.AccidentAmount1),
Accident2: fmt.Sprintf("%d次%.2f元", v.AccidentNum2, v.AccidentAmount2),
Accident3: fmt.Sprintf("金属%d次,非金属%d次", v.AccidentNum3, v.AccidentNum4),
SummaryResult: fmt.Sprintf("%v", v.SummaryResult),
ResultDesc: "",
}
if v.SummaryResult == 0 {
item.ResultDesc = "不奖不惩"
} else if v.SummaryResult > 0 {
item.ResultDesc = "奖"
} else {
item.ResultDesc = "惩"
}
result = append(result, item)
}
return cnt, result, nil
}
// 根据事故数据创建奖惩汇总数据
func (srv ProductTroubleService) CreateRewardSummaryByProductTrouble(param *domain.ProductTrouble) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
rewardSummaryRepo, _ := factory.CreateRewardSummaryRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
//查询是否已经有汇总数据
condtion := map[string]interface{}{
"orgId": param.OrgId,
"companyId": param.CompanyId,
"lineId": param.WorkStation.LineId,
"sectionId": param.WorkStation.SectionId,
"workshopId": param.WorkStation.WorkshopId,
"workerId": param.ProductWorker.UserId,
"recordDateStr": param.RecordDate.Format("200-01-02"),
}
_, summaryList, err := rewardSummaryRepo.Find(condtion)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var summaryData *domain.RewardSummary
nowTime := time.Now()
if len(summaryList) > 0 {
summaryData = summaryList[0]
} else {
summaryData = &domain.RewardSummary{
Id: 0,
CompanyId: param.CompanyId,
OrgId: param.OrgId,
WorkStation: param.WorkStation,
Worker: param.ProductWorker,
RecordDate: param.RecordDate,
RecordDateStr: param.RecordDate.Format("2006-01-02"),
UpToStandard: 0.0,
Yield: 0.0,
AccidentNum1: 0,
AccidentAmount1: 0.0,
AccidentNum2: 0,
AccidentAmount2: 0.0,
AccidentNum3: 0,
AccidentNum4: 0,
SummaryResult: 0.0,
CreatedAt: nowTime,
UpdatedAt: nowTime,
}
}
// 将审核过的事故放入汇总数据计算
summaryData.SummaryAccident(param)
summaryData.UpdatedAt = nowTime
_, err = rewardSummaryRepo.Save(summaryData)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
... ...
... ... @@ -14,7 +14,7 @@ type ProductTrouble struct {
ProductWorker User `json:"productWorker,omitempty"` // 生产工人
AmountLoss float64 `json:"amountLoss"` // 损失的金额
Types TroubleType `json:"types"` // 事故类型 1 安全事故 ,2 质量事故, 3 金属事故 ,4 非金属事故
RecordData time.Time `json:"recordData"` // 事故发生的日期
RecordDate time.Time `json:"recordDate"` // 事故发生的日期
Remark string `json:"remakr"` // 备注
ApproveStatus ProductTroubleApprove `json:"approveStatus"` // 审核状态 1:未审核 2:已审核 3.自动审核
ApproveAt *time.Time `json:"approveAt"` // 审核时间
... ...
... ... @@ -10,7 +10,8 @@ type RewardSummary struct {
CompanyId int `json:"companyId"`
OrgId int `json:"orgId"`
RecordDate time.Time `json:"recordDate"` //日期
WorkStation WorkStation `json:"workStation"` // 工作位置
RecordDateStr string `json:"recordDateStr"` //
WorkStation WorkStation `json:"workStation"` //工作位置
Worker User `json:"user"` //员工
UpToStandard float64 `json:"upToStandard"` //合格率
Yield float64 `json:"yield"` //产能
... ... @@ -30,3 +31,19 @@ type RewardSummaryRepository interface {
FindOne(queryOptions map[string]interface{}) (*RewardSummary, error)
Find(queryOptions map[string]interface{}) (int64, []*RewardSummary, error)
}
// SummaryAccident 汇总事故数据
func (c *RewardSummary) SummaryAccident(trouble *ProductTrouble) {
switch trouble.Types {
case TroubleType1:
c.AccidentNum2 = +1
c.AccidentAmount2 = +trouble.AmountLoss
case TroubleType2:
c.AccidentNum1 = +1
c.AccidentAmount1 = +trouble.AmountLoss
case TroubleType3:
c.AccidentNum3 = +1
case TroubleType4:
c.AccidentNum4 = +1
}
}
... ...
... ... @@ -16,7 +16,7 @@ type ProductTrouble struct {
ProductWorker domain.User // 生产工人
AmountLoss float64 // 损失的金额
Types string // 事故类型
RecordData time.Time // 事故发生的日期
RecordDate time.Time // 事故发生的日期
Remark string // 备注
ApproveStatus int // 审核状态 1:未审核 2:已审核 3.自动审核
ApproveAt *time.Time // 审核时间
... ...
... ... @@ -13,6 +13,7 @@ type RewardSummary struct {
CompanyId int ``
OrgId int ``
RecordDate time.Time //日期
RecordDateStr string //
WorkStation domain.WorkStation // 工作位置
Worker domain.User //员工
UpToStandard float64 //合格率
... ...
... ... @@ -35,7 +35,7 @@ func (repo *ProductTroubleRepository) Save(param *domain.ProductTrouble) (*domai
ProductWorker: param.ProductWorker,
AmountLoss: param.AmountLoss,
Types: string(param.Types),
RecordData: param.RecordData,
RecordDate: param.RecordDate,
Remark: param.Remark,
ApproveStatus: int(param.ApproveStatus),
ApproveAt: param.ApproveAt,
... ... @@ -147,7 +147,7 @@ func (repo *ProductTroubleRepository) TransformToDomain(param *models.ProductTro
ProductWorker: param.ProductWorker,
AmountLoss: param.AmountLoss,
Types: domain.TroubleType(param.Types),
RecordData: param.RecordData,
RecordDate: param.RecordDate,
Remark: param.Remark,
ApproveStatus: domain.ProductTroubleApprove(param.ApproveStatus),
ApproveAt: param.ApproveAt,
... ...
... ... @@ -31,6 +31,7 @@ func (repo *RewardSummaryRepository) Save(param *domain.RewardSummary) (*domain.
CompanyId: param.CompanyId,
OrgId: param.OrgId,
RecordDate: param.RecordDate,
RecordDateStr: param.RecordDateStr,
WorkStation: param.WorkStation,
Worker: param.Worker,
UpToStandard: param.UpToStandard,
... ... @@ -86,10 +87,10 @@ func (repo *RewardSummaryRepository) Find(queryOptions map[string]interface{}) (
m := []models.RewardSummary{}
query := tx.Model(&m).
Limit(20)
if v, ok := queryOptions["limit"].(int); ok {
if v, ok := queryOptions["limit"].(int); ok && v >= 0 {
query.Limit(v)
}
if v, ok := queryOptions["offset"].(int); ok {
if v, ok := queryOptions["offset"].(int); ok && v >= 0 {
query.Offset(v)
}
if v, ok := queryOptions["companyId"]; ok {
... ... @@ -98,10 +99,39 @@ func (repo *RewardSummaryRepository) Find(queryOptions map[string]interface{}) (
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)
}
//线别名称
if v, ok := queryOptions["lineName"]; ok && len(v.(string)) > 0 {
query.Where(`Work_station->>'lineName' like '%?%'`, v)
}
//工段id
if v, ok := queryOptions["sectionId"]; ok && len(v.(string)) > 0 {
query.Where(`Work_station->>'sectionId' = '?'`, v)
}
//车间id
if v, ok := queryOptions["workshopId"]; ok {
query.Where(`Work_station->>'workshopId'='?'`, v)
}
//线别id
if v, ok := queryOptions["lineId"]; ok {
query.Where(`Work_station->>'lineId' = '?'`, v)
}
//日期开始
if v, ok := queryOptions["beginDate"]; ok {
query.Where("record_date>=?", v)
}
//日期结束
if v, ok := queryOptions["endDate"]; ok {
query.Where("record_date<=?", v)
}
//指定查询某个日期
if v, ok := queryOptions["recordDateStr"]; ok {
query.Where("record_date_str=?", v)
}
cnt, err := query.SelectAndCount()
if err != nil {
return 0, nil, err
... ... @@ -134,5 +164,6 @@ func (repo *RewardSummaryRepository) TransformToDomain(param *models.RewardSumma
CreatedAt: param.CreatedAt,
UpdatedAt: param.UpdatedAt,
SummaryResult: param.SummaryResult,
RecordDateStr: param.RecordDateStr,
}
}
... ...