|
|
package service
|
|
|
|
|
|
import (
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
"github.com/linmadan/egglib-go/core/application"
|
...
|
...
|
@@ -149,7 +151,7 @@ func (srv ProductTroubleService) GetProductTrouble(id int) (*dto.ProductTroubleI |
|
|
Remark: troubleData.Remark,
|
|
|
ProductDate: troubleData.RecordData.Format("2006-01-02"),
|
|
|
AmountLoss: troubleData.AmountLoss,
|
|
|
Types: int(troubleData.Types),
|
|
|
Types: string(troubleData.Types),
|
|
|
WorkerId: troubleData.ProductWorker.UserId,
|
|
|
WorkerName: troubleData.ProductWorker.UserName,
|
|
|
}
|
...
|
...
|
@@ -294,5 +296,158 @@ func (srv ProductTroubleService) ListProductTrouble(param *query.ListProductTrou |
|
|
}
|
|
|
result = append(result, item)
|
|
|
}
|
|
|
|
|
|
return cnt, result, nil
|
|
|
}
|
|
|
|
|
|
// 批量添加事故数据
|
|
|
func (srv ProductTroubleService) BatchAddProductTrouble(operateInfo *domain.OperateInfo, param []command.BatchAddProductTroubleCommand) (failRows []interface{}, 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()
|
|
|
}()
|
|
|
|
|
|
//获取当前操作人
|
|
|
userSrv := domainService.NewUserService()
|
|
|
operateUser, err := userSrv.User(operateInfo.UserId)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, "查询操作人数据失败。"+err.Error())
|
|
|
}
|
|
|
|
|
|
//生产班组 数据
|
|
|
productGroupRepo, _ := factory.CreateProductGroupRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
})
|
|
|
_, productGroupList, err := productGroupRepo.Find(map[string]interface{}{
|
|
|
"companyId": operateInfo.CompanyId,
|
|
|
"orgId": operateInfo.OrgId,
|
|
|
})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
}
|
|
|
//车间名称+/+线别名称+/+工段名称 作为键名
|
|
|
workStationMap := map[string]*domain.WorkStation{}
|
|
|
//车间名称+/+工人名 作为键名
|
|
|
workerMap := map[string][]*domain.User{}
|
|
|
//班组名称 作为键名
|
|
|
productGroupMap := map[string]*domain.ProductGroup{}
|
|
|
for _, v := range productGroupList {
|
|
|
workStationName := strings.Join([]string{
|
|
|
v.WorkStation.WorkshopName, v.WorkStation.LineName, v.WorkStation.SectionName,
|
|
|
}, "/")
|
|
|
workStationMap[workStationName] = v.WorkStation
|
|
|
productGroupMap[v.GroupName] = v
|
|
|
for _, vv := range v.GroupMembers {
|
|
|
k := v.WorkStation.WorkshopName + "/" + vv.UserName
|
|
|
isIn := false
|
|
|
for _, vvv := range workerMap[k] {
|
|
|
if vvv.UserId == vv.UserId {
|
|
|
isIn = true
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
if !isIn {
|
|
|
workerMap[k] = append(workerMap[k], vv)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
troubleDataList := make([]*domain.ProductTrouble, 0, len(param))
|
|
|
nowTime := time.Now()
|
|
|
for i := range param {
|
|
|
//检查字段
|
|
|
err = param[i].ValidField()
|
|
|
if err != nil {
|
|
|
param[i].FailReason = err.Error()
|
|
|
failRows = append(failRows, param[i])
|
|
|
continue
|
|
|
}
|
|
|
//检查日期格式
|
|
|
recordDate, err := time.ParseInLocation("2006-01-02", param[i].RecordDate, time.Local)
|
|
|
if err != nil {
|
|
|
param[i].FailReason = "日期格式错误,例 2006-01-02。"
|
|
|
failRows = append(failRows, param[i])
|
|
|
continue
|
|
|
}
|
|
|
//检查工位
|
|
|
var workStation *domain.WorkStation
|
|
|
workStationName := param[i].WorkerName + "/" + param[i].LineName + "/" + param[i].SectionName
|
|
|
if v, ok := workStationMap[workStationName]; ok {
|
|
|
workStation = v
|
|
|
} else {
|
|
|
param[i].FailReason = "车间、线别、工段不存在"
|
|
|
failRows = append(failRows, param[i])
|
|
|
continue
|
|
|
}
|
|
|
//检查员工姓名
|
|
|
var worker *domain.User
|
|
|
workKey := param[i].WorkshopName + "/" + param[i].WorkerName
|
|
|
if u, ok := workerMap[workKey]; ok {
|
|
|
if len(u) > 1 {
|
|
|
param[i].FailReason = "当前车间存在重复的用户名"
|
|
|
failRows = append(failRows, param[i])
|
|
|
continue
|
|
|
}
|
|
|
worker = u[0]
|
|
|
} else {
|
|
|
param[i].FailReason = "当前车间不存在用户" + param[i].WorkerName
|
|
|
failRows = append(failRows, param[i])
|
|
|
continue
|
|
|
}
|
|
|
//损失金额
|
|
|
amountLoss, err := strconv.ParseFloat(param[i].AmountLoss, 64)
|
|
|
if err != nil {
|
|
|
param[i].FailReason = "损失金额填写错误"
|
|
|
failRows = append(failRows, param[i])
|
|
|
continue
|
|
|
}
|
|
|
item := &domain.ProductTrouble{
|
|
|
Id: 0,
|
|
|
CompanyId: 0,
|
|
|
OrgId: 0,
|
|
|
WorkStation: *workStation,
|
|
|
ProductWorker: *worker,
|
|
|
AmountLoss: amountLoss,
|
|
|
Types: "",
|
|
|
RecordData: recordDate,
|
|
|
Remark: "",
|
|
|
ApproveStatus: domain.TroubleIsApprove,
|
|
|
ApproveAt: &nowTime,
|
|
|
ApproveUser: operateUser,
|
|
|
CreatedAt: nowTime,
|
|
|
UpdatedAt: nowTime,
|
|
|
DeletedAt: nil,
|
|
|
}
|
|
|
err = item.SetTypes(param[i].TypesName)
|
|
|
if err != nil {
|
|
|
param[i].FailReason = "事故类型填写错误"
|
|
|
failRows = append(failRows, param[i])
|
|
|
continue
|
|
|
}
|
|
|
troubleDataList = append(troubleDataList, item)
|
|
|
}
|
|
|
|
|
|
if len(failRows) > 0 {
|
|
|
return failRows, nil
|
|
|
}
|
|
|
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())
|
|
|
}
|
|
|
}
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return nil, nil
|
|
|
} |
...
|
...
|
|