作者 Your Name

更新是个处理

... ... @@ -6,7 +6,7 @@ type CreateProductTroubleCommand struct {
WorkshopId int `json:"workshopId" valid:"Required"` //车间id
LineId int `json:"lineId" valid:"Required"` //生产线ID
SectionId int `json:"sectionId" valid:"Required"` //工段ID
WorkerId int `json:"workerId"` //员工id
WorkerId []int `json:"workerId"` //员工id //多选
Remark string `json:"remark"` //备注
AmountLoss float64 `json:"amountLoss"` // 损失的金额
Types string `json:"types"` // 事故类型 1 安全事故 ,2 质量事故, 3 金属事故 ,4 非金属事故
... ...
... ... @@ -558,74 +558,98 @@ func (srv ProductTroubleService) ListRewardSummary(param *query.ListRewardSummar
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
// }
// 创建事故数据
func (srv ProductTroubleService) CreateProductTrouble(operateInfo *domain.OperateInfo, param *command.CreateProductTroubleCommand) (map[string]interface{}, 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()
}()
productTroubleRepo, _ := factory.CreateProductTroubleRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
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, "车间数据填写错误")
}
workStation, err := workShopData.FindWorkStation(param.WorkshopId, param.LineId, param.SectionId)
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
recordDate, err := time.ParseInLocation("2006-01-02", param.RecordDate, time.Local)
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, "日期格式错误")
}
var operaterUser *domain.User
userService := domainService.NewUserService()
operaterUser, err = userService.User(operateInfo.UserId)
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, "获取审核人信息失败,"+err.Error())
}
for _, workerId := range param.WorkerId {
var workerUser *domain.User
workerUser, err = userService.User(workerId)
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, "获取员工信息失败,"+err.Error())
}
var troubleData *domain.ProductTrouble
if param.Id > 0 {
troubleData, err = productTroubleRepo.FindOne(map[string]interface{}{
"id": param.Id,
})
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, "事故数据填写错误")
}
} else {
troubleData = &domain.ProductTrouble{
CompanyId: operateInfo.CompanyId,
OrgId: operateInfo.OrgId,
CreatedAt: time.Now(),
ApproveStatus: domain.TroubleWaitApprove,
}
}
troubleData.AmountLoss = param.AmountLoss
troubleData.ProductWorker = *workerUser
troubleData.RecordDate = recordDate
troubleData.Remark = param.Remark
troubleData.UpdatedAt = time.Now()
troubleData.WorkStation = *workStation
err = troubleData.SetTypes(param.Types)
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
if param.SaveAndApprove {
err = troubleData.Approve(operaterUser)
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
}
_, err = productTroubleRepo.Save(troubleData)
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 map[string]interface{}{}, nil
}
... ...
... ... @@ -14,10 +14,10 @@ type ProductTroubleController struct {
// 创建事故记录
func (c *ProductTroubleController) CreatedProductTrouble() {
srv := service.NewProductTroubleService(nil)
createCommand := &command.SaveProductTroubleCommand{}
createCommand := &command.CreateProductTroubleCommand{}
Must(c.Unmarshal(createCommand))
operater := ParseOperateInfo(c.BaseController)
data, err := srv.SaveProductTrouble(operater, createCommand)
data, err := srv.CreateProductTrouble(operater, createCommand)
c.Response(data, err)
}
... ...