作者 yangfu

feat: 计划调度计划

@@ -39,6 +39,9 @@ func (crontabService *CrontabService) initTask() { @@ -39,6 +39,9 @@ func (crontabService *CrontabService) initTask() {
39 39
40 autoApproveAttendanceRecord := task.NewTask("autoApproveAttendanceRecord", "0 */1 * * * *", AutoApproveProductAttendanceRecord) 40 autoApproveAttendanceRecord := task.NewTask("autoApproveAttendanceRecord", "0 */1 * * * *", AutoApproveProductAttendanceRecord)
41 task.AddTask("autoApproveAttendanceRecord", autoApproveAttendanceRecord) 41 task.AddTask("autoApproveAttendanceRecord", autoApproveAttendanceRecord)
  42 +
  43 + autoApproveRecord := task.NewTask("autoApproveRecord", "0 */2 * * * *", AutoApproveProductRecord)
  44 + task.AddTask("autoApproveRecord", autoApproveRecord)
42 } 45 }
43 46
44 func (crontabService *CrontabService) StartCrontabTask() { 47 func (crontabService *CrontabService) StartCrontabTask() {
@@ -283,6 +283,32 @@ func FastPgProductPlan(transactionContext application.TransactionContext, id int @@ -283,6 +283,32 @@ func FastPgProductPlan(transactionContext application.TransactionContext, id int
283 return rep, mod, err 283 return rep, mod, err
284 } 284 }
285 285
  286 +// FastPgProductPlan 快速返回生产计划对象
  287 +//
  288 +// transactionContext 事务
  289 +// id 对象唯一标识
  290 +func FastPgProductPlanDispatchRecord(transactionContext application.TransactionContext, id int, options ...option) (domain.ProductPlanDispatchRecordRepository, *domain.ProductPlanDispatchRecord, error) {
  291 + var rep domain.ProductPlanDispatchRecordRepository
  292 + var mod *domain.ProductPlanDispatchRecord
  293 + var err error
  294 + if value, err := CreateProductPlanDispatchRecordRepository(map[string]interface{}{
  295 + "transactionContext": transactionContext,
  296 + }); err != nil {
  297 + return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  298 + } else {
  299 + rep = value
  300 + }
  301 + if id > 0 {
  302 + if mod, err = rep.FindOne(map[string]interface{}{"productPlanDispatchRecordId": id}); err != nil {
  303 + if err == domain.ErrorNotFound {
  304 + return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该生产计划调度记录不存在")
  305 + }
  306 + return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  307 + }
  308 + }
  309 + return rep, mod, err
  310 +}
  311 +
286 // FastPgAttendance 快速返回考勤记录 312 // FastPgAttendance 快速返回考勤记录
287 // 313 //
288 // transactionContext 事务 314 // transactionContext 事务
@@ -109,3 +109,11 @@ func CreateWorkshopWorkTimeRecordRepository(options map[string]interface{}) (dom @@ -109,3 +109,11 @@ func CreateWorkshopWorkTimeRecordRepository(options map[string]interface{}) (dom
109 } 109 }
110 return repository.NewWorkshopWorkTimeRecordRepository(transactionContext) 110 return repository.NewWorkshopWorkTimeRecordRepository(transactionContext)
111 } 111 }
  112 +
  113 +func CreateProductPlanDispatchRecordRepository(options map[string]interface{}) (domain.ProductPlanDispatchRecordRepository, error) {
  114 + var transactionContext *pg.TransactionContext
  115 + if value, ok := options["transactionContext"]; ok {
  116 + transactionContext = value.(*pg.TransactionContext)
  117 + }
  118 + return repository.NewProductPlanDispatchRecordRepository(transactionContext)
  119 +}
@@ -10,7 +10,7 @@ import ( @@ -10,7 +10,7 @@ import (
10 10
11 type SetOfflineCommand struct { 11 type SetOfflineCommand struct {
12 // 生产计划ID 12 // 生产计划ID
13 - ProductPlanId int `cname:"生产计划ID" json:"productPlanId" valid:"Required"` 13 + ProductPlanId int `cname:"生产计划ID" json:"productPlanDispatchRecordId" valid:"Required"`
14 } 14 }
15 15
16 func (setOfflineCommand *SetOfflineCommand) Valid(validation *validation.Validation) { 16 func (setOfflineCommand *SetOfflineCommand) Valid(validation *validation.Validation) {
@@ -14,7 +14,7 @@ type SetOnlineCommand struct { @@ -14,7 +14,7 @@ type SetOnlineCommand struct {
14 // 车间ID 14 // 车间ID
15 WorkshopId int `cname:"车间ID" json:"workshopId" valid:"Required"` 15 WorkshopId int `cname:"车间ID" json:"workshopId" valid:"Required"`
16 // 生产线ID 16 // 生产线ID
17 - //LineId int `cname:"生产线ID" json:"lineId" valid:"Required"` 17 + LineId int `cname:"生产线ID" json:"lineId" valid:"Required"`
18 // 工段ID 18 // 工段ID
19 SectionId int `cname:"工段ID" json:"sectionId" valid:"Required"` 19 SectionId int `cname:"工段ID" json:"sectionId" valid:"Required"`
20 } 20 }
@@ -9,8 +9,8 @@ import ( @@ -9,8 +9,8 @@ import (
9 ) 9 )
10 10
11 type SwitchCommand struct { 11 type SwitchCommand struct {
12 - // 下线计划ID  
13 - FromProductPlanId int `cname:"下线计划ID" json:"fromProductPlanId,omitempty"` 12 + // 下线计划调度ID
  13 + FromProductPlanDispatchRecordId int `cname:"下线计划ID" json:"fromProductPlanId,omitempty"`
14 // 上线计划ID 14 // 上线计划ID
15 ToProductPlanId int `cname:"上线计划ID" json:"toProductPlanId,omitempty"` 15 ToProductPlanId int `cname:"上线计划ID" json:"toProductPlanId,omitempty"`
16 } 16 }
  1 +package dto
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  5 + "strings"
  6 +)
  7 +
  8 +type ProductPlanDispatchRecordDto struct {
  9 + // 生产计划ID
  10 + ProductPlanId int `json:"productPlanId,omitempty"`
  11 + // 批号
  12 + BatchNumber string `json:"batchNumber,omitempty"`
  13 + // 生产日期
  14 + ProductDate string `json:"productDate,omitempty"`
  15 + // 车间
  16 + //*domain.Workshop
  17 + *domain.WorkStation
  18 + // 上班班次 1:全天 2:白班 4:中班 8:夜班
  19 + WorkOn int `json:"workOn,omitempty"`
  20 + // 上班班次描述
  21 + WorkOnDescription string `json:"workOnDescription"`
  22 + // 机台 (A、B、C、D 区分机器大小)
  23 + Machine string `json:"machine,omitempty"`
  24 + // 计划的产品名称
  25 + PlanProductName string `json:"planProductName,omitempty"`
  26 + // 计划投入
  27 + *domain.UnitQuantity
  28 + // 计划状态 (1:上线 2:下线 默认下线)
  29 + PlanDispatchStatus int `json:"planDispatchStatus,omitempty"`
  30 + // 工作位置
  31 + //WorkStation *WorkStation `json:"workStation,omitempty"`
  32 + // 总产能
  33 + //TotalProduct float64 `json:"totalProduct"`
  34 + // 备注
  35 + Remark string `json:"remark,omitempty"`
  36 + // 组织名称
  37 + OrgName string `json:"orgName"`
  38 + // 权限标识 (当前登录组织匹配为true,否则false)
  39 + AuthFlag bool `json:"authFlag"`
  40 +}
  41 +
  42 +func (d *ProductPlanDispatchRecordDto) LoadDto(m *domain.ProductPlanDispatchRecord, orgId int) *ProductPlanDispatchRecordDto {
  43 + d.ProductPlanId = m.ProductPlanDispatchRecordId
  44 + d.BatchNumber = m.BatchNumber
  45 + d.ProductDate = m.ProductDate.Format("2006/01/02")
  46 + d.WorkStation = m.WorkStation
  47 + d.WorkOn = m.PlanDispatchRecordExt.WorkOn
  48 + d.PlanProductName = m.PlanDispatchRecordExt.PlanProductName
  49 + //d.UnitQuantity = m.PlanDevoted
  50 + d.PlanDispatchStatus = m.PlanDispatchStatus
  51 + //d.TotalProduct = 0
  52 + d.Machine = m.PlanDispatchRecordExt.Machine
  53 + d.Remark = m.PlanDispatchRecordExt.Remark
  54 + d.AuthFlag = domain.CheckOrgAuth(orgId, m.OrgId)
  55 + if m.Ext != nil {
  56 + d.OrgName = m.Ext.OrgName
  57 + }
  58 + workOnDesc := domain.WorkOnDescription(m.PlanDispatchRecordExt.WorkOn)
  59 + d.WorkOnDescription = strings.Join(workOnDesc, ",")
  60 + return d
  61 +}
@@ -12,6 +12,7 @@ import ( @@ -12,6 +12,7 @@ import (
12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" 12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
13 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService" 13 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService"
14 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils" 14 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
  15 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
15 "time" 16 "time"
16 ) 17 )
17 18
@@ -340,9 +341,9 @@ func (productPlanService *ProductPlanService) SetOffline(setOfflineCommand *comm @@ -340,9 +341,9 @@ func (productPlanService *ProductPlanService) SetOffline(setOfflineCommand *comm
340 transactionContext.RollbackTransaction() 341 transactionContext.RollbackTransaction()
341 }() 342 }()
342 343
343 - var productPlanRepository domain.ProductPlanRepository  
344 - var productPlan *domain.ProductPlan  
345 - productPlanRepository, productPlan, _ = factory.FastPgProductPlan(transactionContext, setOfflineCommand.ProductPlanId) 344 + var productPlanRepository domain.ProductPlanDispatchRecordRepository
  345 + var productPlan *domain.ProductPlanDispatchRecord
  346 + productPlanRepository, productPlan, _ = factory.FastPgProductPlanDispatchRecord(transactionContext, setOfflineCommand.ProductPlanId)
346 347
347 if err = productPlan.ChangeStatus(domain.PlanOffline); err != nil { 348 if err = productPlan.ChangeStatus(domain.PlanOffline); err != nil {
348 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 349 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
@@ -373,45 +374,32 @@ func (productPlanService *ProductPlanService) SetOnline(cmd *command.SetOnlineCo @@ -373,45 +374,32 @@ func (productPlanService *ProductPlanService) SetOnline(cmd *command.SetOnlineCo
373 defer func() { 374 defer func() {
374 transactionContext.RollbackTransaction() 375 transactionContext.RollbackTransaction()
375 }() 376 }()
376 - var productPlanRepository domain.ProductPlanRepository  
377 - var productPlan *domain.ProductPlan  
378 377
379 - productPlanRepository, productPlan, err = factory.FastPgProductPlan(transactionContext, cmd.ProductPlanId)  
380 - if err != nil {  
381 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
382 - }  
383 -  
384 - if err = productPlan.ChangeStatus(domain.PlanOnline); err != nil {  
385 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
386 - }  
387 -  
388 - var workshop *domain.Workshop  
389 - _, workshop, err = factory.FastPgWorkshop(transactionContext, cmd.WorkshopId)  
390 - if err != nil {  
391 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
392 - } 378 + var productPlan *domain.ProductPlan
393 379
394 - line, section, err := workshop.FindSectionById(cmd.SectionId) 380 + _, productPlan, err = factory.FastPgProductPlan(transactionContext, cmd.ProductPlanId)
395 if err != nil { 381 if err != nil {
396 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 382 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
397 } 383 }
398 384
399 var workStation *domain.WorkStation 385 var workStation *domain.WorkStation
400 - workStation, err = workshop.FindWorkStation(workshop.WorkshopId, line.LineId, section.SectionId) 386 + _, workStation, err = factory.FastPgWorkstation(transactionContext, cmd.WorkshopId, cmd.LineId, cmd.SectionId)
401 if err != nil { 387 if err != nil {
402 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 388 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
403 } 389 }
404 390
405 - productPlan.WorkStation = workStation 391 + var productPlanDispatch *domain.ProductPlanDispatchRecord
  392 + productPlanDispatch = domain.NewProductPlanDispatchRecord(productPlan, workStation)
406 393
407 - if productPlan, err = productPlanRepository.Save(productPlan); err != nil { 394 + var productPlanDispatchRepository, _, _ = factory.FastPgProductPlanDispatchRecord(transactionContext, 0)
  395 + if productPlanDispatch, err = productPlanDispatchRepository.Save(productPlanDispatch); err != nil {
408 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 396 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
409 } 397 }
410 398
411 if err := transactionContext.CommitTransaction(); err != nil { 399 if err := transactionContext.CommitTransaction(); err != nil {
412 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 400 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
413 } 401 }
414 - return productPlan, nil 402 + return productPlanDispatch, nil
415 } 403 }
416 404
417 // 换单 405 // 换单
@@ -429,9 +417,10 @@ func (productPlanService *ProductPlanService) Exchange(switchCommand *command.Sw @@ -429,9 +417,10 @@ func (productPlanService *ProductPlanService) Exchange(switchCommand *command.Sw
429 defer func() { 417 defer func() {
430 transactionContext.RollbackTransaction() 418 transactionContext.RollbackTransaction()
431 }() 419 }()
432 - var fromPlan, toPlan *domain.ProductPlan  
433 - var productPlanRepository domain.ProductPlanRepository  
434 - productPlanRepository, fromPlan, err = factory.FastPgProductPlan(transactionContext, switchCommand.FromProductPlanId) 420 + var fromPlan *domain.ProductPlanDispatchRecord
  421 + var toPlan *domain.ProductPlan
  422 + var productPlanDispatchRecordRepository domain.ProductPlanDispatchRecordRepository
  423 + productPlanDispatchRecordRepository, fromPlan, err = factory.FastPgProductPlanDispatchRecord(transactionContext, switchCommand.FromProductPlanDispatchRecordId)
435 if err != nil { 424 if err != nil {
436 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 425 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
437 } 426 }
@@ -444,22 +433,18 @@ func (productPlanService *ProductPlanService) Exchange(switchCommand *command.Sw @@ -444,22 +433,18 @@ func (productPlanService *ProductPlanService) Exchange(switchCommand *command.Sw
444 if err = fromPlan.ChangeStatus(domain.PlanOffline); err != nil { 433 if err = fromPlan.ChangeStatus(domain.PlanOffline); err != nil {
445 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 434 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
446 } 435 }
447 - if _, err = productPlanRepository.Save(fromPlan); err != nil {  
448 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
449 - }  
450 436
451 - var workStation *domain.WorkStation  
452 - fromWorkStation := fromPlan.WorkStation  
453 - _, workStation, err = factory.FastPgWorkstation(transactionContext, fromWorkStation.WorkshopId, fromWorkStation.LineId, fromWorkStation.SectionId)  
454 - if err != nil {  
455 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 437 + // 新的上线、如果存在一条同一个批次、工段的调度记录 则不做更新
  438 + if fromPlan.PlanDispatchRecordExt.ProductPlanId == toPlan.ProductPlanId && fromPlan.ProductDate == toPlan.ProductDate {
  439 + log.Logger.Info("换单的批次与当前批次一致,跳过")
  440 + return struct{}{}, nil
456 } 441 }
457 - // 计划上线  
458 - toPlan.WorkStation = workStation  
459 - if err = toPlan.ChangeStatus(domain.PlanOnline); err != nil { 442 +
  443 + if _, err = productPlanDispatchRecordRepository.Save(fromPlan); err != nil {
460 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 444 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
461 } 445 }
462 - if _, err = productPlanRepository.Save(toPlan); err != nil { 446 + toProductPlanDispatchRecord := domain.NewProductPlanDispatchRecord(toPlan, fromPlan.WorkStation)
  447 + if _, err = productPlanDispatchRecordRepository.Save(toProductPlanDispatchRecord); err != nil {
463 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 448 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
464 } 449 }
465 450
@@ -554,6 +539,46 @@ func (productPlanService *ProductPlanService) SubmitProductRecord(submitProductR @@ -554,6 +539,46 @@ func (productPlanService *ProductPlanService) SubmitProductRecord(submitProductR
554 return struct{}{}, nil 539 return struct{}{}, nil
555 } 540 }
556 541
  542 +// 搜索生产上线计划记录
  543 +func (productPlanService *ProductPlanService) SearchProductPlanOnlineDispatchRecord(operateInfo *domain.OperateInfo, cmd *query.SearchProductPlanQuery) (int64, interface{}, error) {
  544 + if err := cmd.ValidateQuery(); err != nil {
  545 + return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
  546 + }
  547 + transactionContext, err := factory.CreateTransactionContext(nil)
  548 + if err != nil {
  549 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  550 + }
  551 + if err := transactionContext.StartTransaction(); err != nil {
  552 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  553 + }
  554 + defer func() {
  555 + transactionContext.RollbackTransaction()
  556 + }()
  557 + var productPlanRepository domain.ProductPlanDispatchRecordRepository
  558 + if value, err := factory.CreateProductPlanDispatchRecordRepository(map[string]interface{}{
  559 + "transactionContext": transactionContext,
  560 + }); err != nil {
  561 + return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  562 + } else {
  563 + productPlanRepository = value
  564 + }
  565 + count, productPlans, err := productPlanRepository.Find(utils.ObjectToMap(cmd))
  566 + if err != nil {
  567 + return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  568 + }
  569 + if err := transactionContext.CommitTransaction(); err != nil {
  570 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  571 + }
  572 + var result = make([]*dto.ProductPlanDispatchRecordDto, 0)
  573 + for i := range productPlans {
  574 + item := productPlans[i]
  575 + newItem := &dto.ProductPlanDispatchRecordDto{}
  576 + newItem.LoadDto(item, operateInfo.OrgId)
  577 + result = append(result, newItem)
  578 + }
  579 + return count, result, nil
  580 +}
  581 +
557 func NewProductPlanService(options map[string]interface{}) *ProductPlanService { 582 func NewProductPlanService(options map[string]interface{}) *ProductPlanService {
558 newProductPlanService := &ProductPlanService{} 583 newProductPlanService := &ProductPlanService{}
559 return newProductPlanService 584 return newProductPlanService
  1 +package domain
  2 +
  3 +import (
  4 + "errors"
  5 + "time"
  6 +)
  7 +
  8 +// 生产计划
  9 +type ProductPlanDispatchRecord struct {
  10 + // 生产计划调度ID
  11 + ProductPlanDispatchRecordId int `json:"productPlanDispatchRecordId"`
  12 + // 企业id
  13 + CompanyId int `json:"companyId"`
  14 + // 组织ID
  15 + OrgId int `json:"orgId"`
  16 + // 批号
  17 + BatchNumber string `json:"batchNumber"`
  18 + // 生产日期
  19 + ProductDate time.Time `json:"productDate"`
  20 + // 计划状态 1:上线 2:下线
  21 + PlanDispatchStatus int `json:"planDispatchStatus"`
  22 + // 工作位置
  23 + WorkStation *WorkStation `json:"workStation"`
  24 + // 创建时间
  25 + CreatedAt time.Time `json:"createdAt"`
  26 + // 更新时间
  27 + UpdatedAt time.Time `json:"updatedAt"`
  28 + // 删除时间
  29 + DeletedAt time.Time `json:"deletedAt"`
  30 + // 计划调度记录扩展数据
  31 + PlanDispatchRecordExt *PlanDispatchRecordExt `json:"planDispatchRecordExt"`
  32 + // 其他扩展数据
  33 + Ext *Ext `json:"ext"`
  34 +}
  35 +
  36 +// 计划调度记录扩展数据
  37 +type PlanDispatchRecordExt struct {
  38 + // 生产计划ID
  39 + ProductPlanId int `json:"productPlanId,omitempty"`
  40 + // 计划的产品名称
  41 + PlanProductName string `json:"planProductName,omitempty"`
  42 + // 上班班次 1:全天 2:白班 4:中班 8:夜班
  43 + WorkOn int `json:"workOn,omitempty"`
  44 + // 机台 (A、B、C、D 区分机器大小)
  45 + Machine string `json:"machine,omitempty"`
  46 + // 备注
  47 + Remark string `json:"remark,omitempty"`
  48 +}
  49 +
  50 +type ProductPlanDispatchRecordRepository interface {
  51 + Save(productPlanDispatchRecord *ProductPlanDispatchRecord) (*ProductPlanDispatchRecord, error)
  52 + Remove(productPlanDispatchRecord *ProductPlanDispatchRecord) (*ProductPlanDispatchRecord, error)
  53 + FindOne(queryOptions map[string]interface{}) (*ProductPlanDispatchRecord, error)
  54 + Find(queryOptions map[string]interface{}) (int64, []*ProductPlanDispatchRecord, error)
  55 +}
  56 +
  57 +func (productPlanDispatchRecord *ProductPlanDispatchRecord) Identify() interface{} {
  58 + if productPlanDispatchRecord.ProductPlanDispatchRecordId == 0 {
  59 + return nil
  60 + }
  61 + return productPlanDispatchRecord.ProductPlanDispatchRecordId
  62 +}
  63 +
  64 +func (productPlanDispatchRecord *ProductPlanDispatchRecord) Update(data map[string]interface{}) error {
  65 + return nil
  66 +}
  67 +
  68 +func (productPlanDispatchRecord *ProductPlanDispatchRecord) ChangeStatus(status int) error {
  69 + if productPlanDispatchRecord.PlanDispatchStatus == status && status == PlanOnline {
  70 + return errors.New("计划已经上线")
  71 + }
  72 + if productPlanDispatchRecord.PlanDispatchStatus == status && status == PlanOffline {
  73 + return errors.New("计划已经下线")
  74 + }
  75 + if !(status == PlanOnline || status == PlanOffline) {
  76 + return errors.New("计划状态有误")
  77 + }
  78 + productPlanDispatchRecord.PlanDispatchStatus = status
  79 + return nil
  80 +}
  81 +
  82 +func NewProductPlanDispatchRecord(productPlan *ProductPlan, workStation *WorkStation) *ProductPlanDispatchRecord {
  83 + return &ProductPlanDispatchRecord{
  84 + CompanyId: productPlan.CompanyId,
  85 + OrgId: productPlan.OrgId,
  86 + BatchNumber: productPlan.BatchNumber,
  87 + ProductDate: productPlan.ProductDate,
  88 + PlanDispatchStatus: PlanOnline,
  89 + WorkStation: workStation,
  90 + CreatedAt: time.Now(),
  91 + UpdatedAt: time.Now(),
  92 + PlanDispatchRecordExt: &PlanDispatchRecordExt{
  93 + ProductPlanId: productPlan.ProductPlanId,
  94 + PlanProductName: productPlan.PlanProductName,
  95 + WorkOn: productPlan.WorkOn,
  96 + Machine: productPlan.Machine,
  97 + Remark: productPlan.Remark,
  98 + },
  99 + Ext: productPlan.Ext,
  100 + }
  101 +}
@@ -37,7 +37,7 @@ func (dao *ProductRecordDao) RecentUnApprovedProductRecord(fromLastHour int, rec @@ -37,7 +37,7 @@ func (dao *ProductRecordDao) RecentUnApprovedProductRecord(fromLastHour int, rec
37 query.Where("created_at <= ?", endTime) 37 query.Where("created_at <= ?", endTime)
38 query.Where("created_at >= ?", beginTime) 38 query.Where("created_at >= ?", beginTime)
39 query.Where("product_record_type & ? >0", recordType) 39 query.Where("product_record_type & ? >0", recordType)
40 - query.Where("product_record_info->>approveStatus = '?'", domain.AttendanceNotApprove) 40 + query.Where("product_record_info->>'approveStatus' = '?'", domain.AttendanceNotApprove)
41 query.Where("product_record_info->>'weighBefore'<>'0'") 41 query.Where("product_record_info->>'weighBefore'<>'0'")
42 query.SetOffsetAndLimit(domain.MaxQueryRow) 42 query.SetOffsetAndLimit(domain.MaxQueryRow)
43 query.SetOrderDirect("product_record_id", "DESC") 43 query.SetOrderDirect("product_record_id", "DESC")
@@ -42,6 +42,7 @@ func init() { @@ -42,6 +42,7 @@ func init() {
42 (*models.EmployeeProductRecord)(nil), 42 (*models.EmployeeProductRecord)(nil),
43 (*models.WorkshopProductRecord)(nil), 43 (*models.WorkshopProductRecord)(nil),
44 (*models.WorkshopWorkTimeRecord)(nil), 44 (*models.WorkshopWorkTimeRecord)(nil),
  45 + (*models.ProductPlanDispatchRecord)(nil),
45 } { 46 } {
46 err := DB.Model(model).CreateTable(&orm.CreateTableOptions{ 47 err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
47 Temp: false, 48 Temp: false,
  1 +package models
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  5 + "time"
  6 +)
  7 +
  8 +type ProductPlanDispatchRecord struct {
  9 + tableName string `comment:"生产计划" pg:"manufacture.product_plan_dispatch_record"`
  10 + // 生产计划调度ID
  11 + ProductPlanDispatchRecordId int `comment:"生产计划调度ID" pg:"pk:product_plan_dispatch_record_id"`
  12 + // 企业id
  13 + CompanyId int `comment:"企业id"`
  14 + // 组织ID
  15 + OrgId int `comment:"组织ID"`
  16 + // 批号
  17 + BatchNumber string `comment:"批号"`
  18 + // 生产日期
  19 + ProductDate time.Time `comment:"生产日期"`
  20 + // 计划状态 1:上线 2:下线
  21 + PlanDispatchStatus int `comment:"计划状态 1:上线 2:下线"`
  22 + // 工作位置
  23 + WorkStation *domain.WorkStation `comment:"工作位置"`
  24 + // 创建时间
  25 + CreatedAt time.Time `comment:"创建时间"`
  26 + // 更新时间
  27 + UpdatedAt time.Time `comment:"更新时间"`
  28 + // 删除时间
  29 + DeletedAt time.Time `comment:"删除时间"`
  30 + // 计划调度记录扩展数据
  31 + PlanDispatchRecordExt *domain.PlanDispatchRecordExt `comment:"计划调度记录扩展数据"`
  32 + // 其他扩展数据
  33 + Ext *domain.Ext `comment:"其他扩展数据"`
  34 +}
  1 +package transform
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
  6 +)
  7 +
  8 +func TransformToProductPlanDispatchRecordDomainModelFromPgModels(productPlanDispatchRecordModel *models.ProductPlanDispatchRecord) (*domain.ProductPlanDispatchRecord, error) {
  9 + return &domain.ProductPlanDispatchRecord{
  10 + ProductPlanDispatchRecordId: productPlanDispatchRecordModel.ProductPlanDispatchRecordId,
  11 + CompanyId: productPlanDispatchRecordModel.CompanyId,
  12 + OrgId: productPlanDispatchRecordModel.OrgId,
  13 + BatchNumber: productPlanDispatchRecordModel.BatchNumber,
  14 + ProductDate: productPlanDispatchRecordModel.ProductDate,
  15 + PlanDispatchStatus: productPlanDispatchRecordModel.PlanDispatchStatus,
  16 + WorkStation: productPlanDispatchRecordModel.WorkStation,
  17 + CreatedAt: productPlanDispatchRecordModel.CreatedAt,
  18 + UpdatedAt: productPlanDispatchRecordModel.UpdatedAt,
  19 + DeletedAt: productPlanDispatchRecordModel.DeletedAt,
  20 + PlanDispatchRecordExt: productPlanDispatchRecordModel.PlanDispatchRecordExt,
  21 + Ext: productPlanDispatchRecordModel.Ext,
  22 + }, nil
  23 +}
  1 +package repository
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/go-pg/pg/v10"
  6 +
  7 + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
  8 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  9 + "github.com/linmadan/egglib-go/utils/snowflake"
  10 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  11 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
  12 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/transform"
  13 +)
  14 +
  15 +type ProductPlanDispatchRecordRepository struct {
  16 + transactionContext *pgTransaction.TransactionContext
  17 +}
  18 +
  19 +func (repository *ProductPlanDispatchRecordRepository) nextIdentify() (int64, error) {
  20 + IdWorker, err := snowflake.NewIdWorker(1)
  21 + if err != nil {
  22 + return 0, err
  23 + }
  24 + id, err := IdWorker.NextId()
  25 + return id, err
  26 +}
  27 +func (repository *ProductPlanDispatchRecordRepository) Save(productPlanDispatchRecord *domain.ProductPlanDispatchRecord) (*domain.ProductPlanDispatchRecord, error) {
  28 + sqlBuildFields := []string{
  29 + "product_plan_dispatch_record_id",
  30 + "company_id",
  31 + "org_id",
  32 + "batch_number",
  33 + "product_date",
  34 + "plan_dispatch_status",
  35 + "work_station",
  36 + "created_at",
  37 + "updated_at",
  38 + "deleted_at",
  39 + "plan_dispatch_record_ext",
  40 + "ext",
  41 + }
  42 + insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_plan_dispatch_record_id", "deleted_at"))
  43 + insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_plan_dispatch_record_id", "deleted_at"))
  44 + returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
  45 + updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_plan_dispatch_record_id", "deleted_at")
  46 + updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
  47 + tx := repository.transactionContext.PgTx
  48 + if productPlanDispatchRecord.Identify() == nil {
  49 + if _, err := tx.QueryOne(
  50 + pg.Scan(
  51 + &productPlanDispatchRecord.ProductPlanDispatchRecordId,
  52 + &productPlanDispatchRecord.CompanyId,
  53 + &productPlanDispatchRecord.OrgId,
  54 + &productPlanDispatchRecord.BatchNumber,
  55 + &productPlanDispatchRecord.ProductDate,
  56 + &productPlanDispatchRecord.PlanDispatchStatus,
  57 + &productPlanDispatchRecord.WorkStation,
  58 + &productPlanDispatchRecord.CreatedAt,
  59 + &productPlanDispatchRecord.UpdatedAt,
  60 + &productPlanDispatchRecord.DeletedAt,
  61 + &productPlanDispatchRecord.PlanDispatchRecordExt,
  62 + &productPlanDispatchRecord.Ext,
  63 + ),
  64 + fmt.Sprintf("INSERT INTO manufacture.product_plan_dispatch_record (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
  65 + //productPlanDispatchRecord.ProductPlanDispatchRecordId,
  66 + productPlanDispatchRecord.CompanyId,
  67 + productPlanDispatchRecord.OrgId,
  68 + productPlanDispatchRecord.BatchNumber,
  69 + productPlanDispatchRecord.ProductDate,
  70 + productPlanDispatchRecord.PlanDispatchStatus,
  71 + productPlanDispatchRecord.WorkStation,
  72 + productPlanDispatchRecord.CreatedAt,
  73 + productPlanDispatchRecord.UpdatedAt,
  74 + //productPlanDispatchRecord.DeletedAt,
  75 + productPlanDispatchRecord.PlanDispatchRecordExt,
  76 + productPlanDispatchRecord.Ext,
  77 + ); err != nil {
  78 + return productPlanDispatchRecord, err
  79 + }
  80 + } else {
  81 + if _, err := tx.QueryOne(
  82 + pg.Scan(
  83 + &productPlanDispatchRecord.ProductPlanDispatchRecordId,
  84 + &productPlanDispatchRecord.CompanyId,
  85 + &productPlanDispatchRecord.OrgId,
  86 + &productPlanDispatchRecord.BatchNumber,
  87 + &productPlanDispatchRecord.ProductDate,
  88 + &productPlanDispatchRecord.PlanDispatchStatus,
  89 + &productPlanDispatchRecord.WorkStation,
  90 + &productPlanDispatchRecord.CreatedAt,
  91 + &productPlanDispatchRecord.UpdatedAt,
  92 + &productPlanDispatchRecord.DeletedAt,
  93 + &productPlanDispatchRecord.PlanDispatchRecordExt,
  94 + &productPlanDispatchRecord.Ext,
  95 + ),
  96 + fmt.Sprintf("UPDATE manufacture.product_plan_dispatch_record SET %s WHERE product_plan_dispatch_record_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
  97 + //productPlanDispatchRecord.ProductPlanDispatchRecordId,
  98 + productPlanDispatchRecord.CompanyId,
  99 + productPlanDispatchRecord.OrgId,
  100 + productPlanDispatchRecord.BatchNumber,
  101 + productPlanDispatchRecord.ProductDate,
  102 + productPlanDispatchRecord.PlanDispatchStatus,
  103 + productPlanDispatchRecord.WorkStation,
  104 + productPlanDispatchRecord.CreatedAt,
  105 + productPlanDispatchRecord.UpdatedAt,
  106 + //productPlanDispatchRecord.DeletedAt,
  107 + productPlanDispatchRecord.PlanDispatchRecordExt,
  108 + productPlanDispatchRecord.Ext,
  109 + productPlanDispatchRecord.Identify(),
  110 + ); err != nil {
  111 + return productPlanDispatchRecord, err
  112 + }
  113 + }
  114 + return productPlanDispatchRecord, nil
  115 +}
  116 +func (repository *ProductPlanDispatchRecordRepository) Remove(productPlanDispatchRecord *domain.ProductPlanDispatchRecord) (*domain.ProductPlanDispatchRecord, error) {
  117 + tx := repository.transactionContext.PgTx
  118 + productPlanDispatchRecordModel := new(models.ProductPlanDispatchRecord)
  119 + productPlanDispatchRecordModel.ProductPlanDispatchRecordId = productPlanDispatchRecord.Identify().(int)
  120 + if _, err := tx.Model(productPlanDispatchRecordModel).WherePK().Delete(); err != nil {
  121 + return productPlanDispatchRecord, err
  122 + }
  123 + return productPlanDispatchRecord, nil
  124 +}
  125 +func (repository *ProductPlanDispatchRecordRepository) FindOne(queryOptions map[string]interface{}) (*domain.ProductPlanDispatchRecord, error) {
  126 + tx := repository.transactionContext.PgTx
  127 + productPlanDispatchRecordModel := new(models.ProductPlanDispatchRecord)
  128 + query := sqlbuilder.BuildQuery(tx.Model(productPlanDispatchRecordModel), queryOptions)
  129 + query.SetWhereByQueryOption("product_plan_dispatch_record.product_plan_dispatch_record_id = ?", "productPlanDispatchRecordId")
  130 + query.SetWhereByQueryOption("company_id = ?", "companyId")
  131 + query.SetWhereByQueryOption("org_id = ?", "orgId")
  132 + query.SetWhereByQueryOption("product_date = ?", "productDate")
  133 + query.SetWhereByQueryOption("work_station->>'workStationId'='?'", "workStationId")
  134 + query.SetWhereByQueryOption("plan_dispatch_record_ext->>'productPlanId'='?'", "productPlanId")
  135 + if err := query.First(); err != nil {
  136 + if err.Error() == "pg: no rows in result set" {
  137 + return nil, domain.ErrorNotFound
  138 + } else {
  139 + return nil, err
  140 + }
  141 + }
  142 + if productPlanDispatchRecordModel.ProductPlanDispatchRecordId == 0 {
  143 + return nil, nil
  144 + } else {
  145 + return transform.TransformToProductPlanDispatchRecordDomainModelFromPgModels(productPlanDispatchRecordModel)
  146 + }
  147 +}
  148 +func (repository *ProductPlanDispatchRecordRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.ProductPlanDispatchRecord, error) {
  149 + tx := repository.transactionContext.PgTx
  150 + var productPlanDispatchRecordModels []*models.ProductPlanDispatchRecord
  151 + productPlanDispatchRecords := make([]*domain.ProductPlanDispatchRecord, 0)
  152 + query := sqlbuilder.BuildQuery(tx.Model(&productPlanDispatchRecordModels), queryOptions)
  153 + query.SetOffsetAndLimit(domain.MaxQueryRow)
  154 + query.SetOrderDirect("product_plan_dispatch_record_id", "DESC")
  155 + if count, err := query.SelectAndCount(); err != nil {
  156 + return 0, productPlanDispatchRecords, err
  157 + } else {
  158 + for _, productPlanDispatchRecordModel := range productPlanDispatchRecordModels {
  159 + if productPlanDispatchRecord, err := transform.TransformToProductPlanDispatchRecordDomainModelFromPgModels(productPlanDispatchRecordModel); err != nil {
  160 + return 0, productPlanDispatchRecords, err
  161 + } else {
  162 + productPlanDispatchRecords = append(productPlanDispatchRecords, productPlanDispatchRecord)
  163 + }
  164 + }
  165 + return int64(count), productPlanDispatchRecords, nil
  166 + }
  167 +}
  168 +func NewProductPlanDispatchRecordRepository(transactionContext *pgTransaction.TransactionContext) (*ProductPlanDispatchRecordRepository, error) {
  169 + if transactionContext == nil {
  170 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  171 + } else {
  172 + return &ProductPlanDispatchRecordRepository{
  173 + transactionContext: transactionContext,
  174 + }, nil
  175 + }
  176 +}
@@ -130,3 +130,15 @@ func (controller *ProductPlanController) SearchProductPlan() { @@ -130,3 +130,15 @@ func (controller *ProductPlanController) SearchProductPlan() {
130 total, data, err := productPlanService.SearchProductPlan(ParseOperateInfo(controller.BaseController), cmd) 130 total, data, err := productPlanService.SearchProductPlan(ParseOperateInfo(controller.BaseController), cmd)
131 ResponseGrid(controller.BaseController, total, data, err) 131 ResponseGrid(controller.BaseController, total, data, err)
132 } 132 }
  133 +
  134 +func (controller *ProductPlanController) SearchProductPlanOnlineDispatchRecord() {
  135 + productPlanService := service.NewProductPlanService(nil)
  136 + cmd := &query.SearchProductPlanQuery{}
  137 + Must(controller.Unmarshal(cmd))
  138 + operateInfo := ParseOperateInfo(controller.BaseController)
  139 + //cmd.OrgId = operateInfo.OrgId
  140 + cmd.CompanyId = operateInfo.CompanyId
  141 + cmd.InOrgIds = operateInfo.OrgIds
  142 + total, data, err := productPlanService.SearchProductPlanOnlineDispatchRecord(ParseOperateInfo(controller.BaseController), cmd)
  143 + ResponseGrid(controller.BaseController, total, data, err)
  144 +}
@@ -18,4 +18,5 @@ func init() { @@ -18,4 +18,5 @@ func init() {
18 web.Router("/product-plans/exchange", &controllers.ProductPlanController{}, "Post:Switch") 18 web.Router("/product-plans/exchange", &controllers.ProductPlanController{}, "Post:Switch")
19 web.Router("/product-plans/submit-product-record", &controllers.ProductPlanController{}, "Post:SubmitProductRecord") 19 web.Router("/product-plans/submit-product-record", &controllers.ProductPlanController{}, "Post:SubmitProductRecord")
20 web.Router("/product-plans/search", &controllers.ProductPlanController{}, "Post:SearchProductPlan") 20 web.Router("/product-plans/search", &controllers.ProductPlanController{}, "Post:SearchProductPlan")
  21 + web.Router("/product-plans/online-dispatch-record/search", &controllers.ProductPlanController{}, "Post:SearchProductPlanOnlineDispatchRecord")
21 } 22 }