作者 yangfu

增加车间工位管理

@@ -34,10 +34,76 @@ func FastPgWorkshop(transactionContext application.TransactionContext, id int, o @@ -34,10 +34,76 @@ func FastPgWorkshop(transactionContext application.TransactionContext, id int, o
34 return rep, mod, err 34 return rep, mod, err
35 } 35 }
36 36
  37 +// FastPgWorkstation 快速返回工作定位对象
  38 +//
  39 +// transactionContext 事务
  40 +// workshopId 车间ID
  41 +// lineId 生产线ID
  42 +// sectionId 对象ID
  43 +func FastPgWorkstation(transactionContext application.TransactionContext, workshopId, lineId, sectionId int, options ...option) (domain.WorkshopRepository, *domain.WorkStation, error) {
  44 + var rep domain.WorkshopRepository
  45 + var mod *domain.Workshop
  46 + var err error
  47 + if value, err := CreateWorkshopRepository(map[string]interface{}{
  48 + "transactionContext": transactionContext,
  49 + }); err != nil {
  50 + return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  51 + } else {
  52 + rep = value
  53 + }
  54 +
  55 + if mod, err = rep.FindOne(map[string]interface{}{"workshopId": workshopId}); err != nil {
  56 + if err == domain.ErrorNotFound {
  57 + return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该车间不存在")
  58 + }
  59 + return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  60 + }
  61 +
  62 + workStation, err := mod.FindWorkStation(workshopId, lineId, sectionId)
  63 + if err != nil {
  64 + return rep, nil, err
  65 + }
  66 + o := NewFastOptions(options...)
  67 + if o.SetPrincipal {
  68 + workStation.Principal = mod.Principal
  69 + }
  70 + return rep, workStation, err
  71 +}
  72 +
  73 +// FastPgProductJob 快速返回工位对象
  74 +//
  75 +// transactionContext 事务
  76 +// id 对象唯一标识
  77 +func FastPgProductJob(transactionContext application.TransactionContext, id int, options ...option) (domain.ProductJobRepository, *domain.ProductJob, error) {
  78 + var rep domain.ProductJobRepository
  79 + var mod *domain.ProductJob
  80 + var err error
  81 + if value, err := CreateProductJobRepository(map[string]interface{}{
  82 + "transactionContext": transactionContext,
  83 + }); err != nil {
  84 + return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  85 + } else {
  86 + rep = value
  87 + }
  88 + if id > 0 {
  89 + if mod, err = rep.FindOne(map[string]interface{}{"productJobId": id}); err != nil {
  90 + if err == domain.ErrorNotFound {
  91 + return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该车间不存在")
  92 + }
  93 + return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  94 + }
  95 + }
  96 + //if err = fastPgDataAuth(transactionContext, mod, options...); err != nil {
  97 + // return nil, nil, err
  98 + //}
  99 + return rep, mod, err
  100 +}
  101 +
37 /***** 2.配置 *****/ 102 /***** 2.配置 *****/
38 103
39 type FastOptions struct { 104 type FastOptions struct {
40 DataAuthRequired bool 105 DataAuthRequired bool
  106 + SetPrincipal bool
41 OperateInfo *domain.OperateInfo 107 OperateInfo *domain.OperateInfo
42 } 108 }
43 109
@@ -66,3 +132,10 @@ func WithOperator(op *domain.OperateInfo) option { @@ -66,3 +132,10 @@ func WithOperator(op *domain.OperateInfo) option {
66 options.OperateInfo = op 132 options.OperateInfo = op
67 } 133 }
68 } 134 }
  135 +
  136 +// WithOperator 操作人
  137 +func WithSetPrincipal() option {
  138 + return func(options *FastOptions) {
  139 + options.SetPrincipal = true
  140 + }
  141 +}
@@ -10,11 +10,13 @@ import ( @@ -10,11 +10,13 @@ import (
10 10
11 type CreateProductJobCommand struct { 11 type CreateProductJobCommand struct {
12 // 企业id 12 // 企业id
13 - CompanyId int `cname:"企业id" json:"companyId" valid:"Required"` 13 + //CompanyId int `cname:"企业id" json:"companyId" valid:"Required"`
14 // 组织ID 14 // 组织ID
15 - OrgId int `cname:"组织ID" json:"orgId" valid:"Required"` 15 + //OrgId int `cname:"组织ID" json:"orgId" valid:"Required"`
16 // 工位名称 16 // 工位名称
17 JobName string `cname:"工位名称" json:"jobName" valid:"Required"` 17 JobName string `cname:"工位名称" json:"jobName" valid:"Required"`
  18 + // 工位名称
  19 + ProcessName string `cname:"工位名称" json:"processName" valid:"Required"`
18 // 车间ID 20 // 车间ID
19 WorkshopId int `cname:"车间ID" json:"workshopId" valid:"Required"` 21 WorkshopId int `cname:"车间ID" json:"workshopId" valid:"Required"`
20 // 生产线ID 22 // 生产线ID
@@ -26,7 +28,7 @@ type CreateProductJobCommand struct { @@ -26,7 +28,7 @@ type CreateProductJobCommand struct {
26 } 28 }
27 29
28 func (createProductJobCommand *CreateProductJobCommand) Valid(validation *validation.Validation) { 30 func (createProductJobCommand *CreateProductJobCommand) Valid(validation *validation.Validation) {
29 - validation.SetError("CustomValid", "未实现的自定义认证") 31 + //validation.SetError("CustomValid", "未实现的自定义认证")
30 } 32 }
31 33
32 func (createProductJobCommand *CreateProductJobCommand) ValidateCommand() error { 34 func (createProductJobCommand *CreateProductJobCommand) ValidateCommand() error {
@@ -14,7 +14,7 @@ type RemoveProductJobCommand struct { @@ -14,7 +14,7 @@ type RemoveProductJobCommand struct {
14 } 14 }
15 15
16 func (removeProductJobCommand *RemoveProductJobCommand) Valid(validation *validation.Validation) { 16 func (removeProductJobCommand *RemoveProductJobCommand) Valid(validation *validation.Validation) {
17 - validation.SetError("CustomValid", "未实现的自定义认证") 17 + //validation.SetError("CustomValid", "未实现的自定义认证")
18 } 18 }
19 19
20 func (removeProductJobCommand *RemoveProductJobCommand) ValidateCommand() error { 20 func (removeProductJobCommand *RemoveProductJobCommand) ValidateCommand() error {
@@ -24,7 +24,7 @@ type UpdateProductJobCommand struct { @@ -24,7 +24,7 @@ type UpdateProductJobCommand struct {
24 } 24 }
25 25
26 func (updateProductJobCommand *UpdateProductJobCommand) Valid(validation *validation.Validation) { 26 func (updateProductJobCommand *UpdateProductJobCommand) Valid(validation *validation.Validation) {
27 - validation.SetError("CustomValid", "未实现的自定义认证") 27 + //validation.SetError("CustomValid", "未实现的自定义认证")
28 } 28 }
29 29
30 func (updateProductJobCommand *UpdateProductJobCommand) ValidateCommand() error { 30 func (updateProductJobCommand *UpdateProductJobCommand) ValidateCommand() error {
  1 +package dto
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  4 +
  5 +type ProductJobDto struct {
  6 + // 工位ID
  7 + ProductJobId int `json:"productJobId,omitempty"`
  8 + // 工位名称
  9 + JobName string `json:"jobName,omitempty"`
  10 + // 工序名称
  11 + ProcessName string `json:"processName,omitempty"`
  12 + //// 工作位置键值 (车间ID+'.'+生产线ID+'.'+工段ID)
  13 + //WorkStationId string `json:"workStationId,omitempty"`
  14 + //// 车间ID
  15 + //WorkshopId int `json:"workshopId,omitempty"`
  16 + //// 车间名称
  17 + //WorkshopName string `json:"workshopName,omitempty"`
  18 + //// 生产线ID
  19 + //LineId int `json:"lineId,omitempty"`
  20 + //// 生产线名称
  21 + //LineName string `json:"lineName,omitempty"`
  22 + //// 工段ID
  23 + //SectionId int `json:"sectionId,omitempty"`
  24 + //// 工段名称
  25 + //SectionName string `json:"sectionName,omitempty"`
  26 + //// 负责人 (用户对象)
  27 + //Principal *domain.User `json:"principal,omitempty"`
  28 + *domain.WorkStation
  29 +}
  30 +
  31 +func (m *ProductJobDto) LoadDto(job *domain.ProductJob) {
  32 + m.ProductJobId = job.ProductJobId
  33 + m.JobName = job.JobName
  34 + m.ProcessName = job.ProcessName
  35 + m.WorkStation = job.WorkStation
  36 +}
@@ -14,7 +14,7 @@ type GetProductJobQuery struct { @@ -14,7 +14,7 @@ type GetProductJobQuery struct {
14 } 14 }
15 15
16 func (getProductJobQuery *GetProductJobQuery) Valid(validation *validation.Validation) { 16 func (getProductJobQuery *GetProductJobQuery) Valid(validation *validation.Validation) {
17 - validation.SetError("CustomValid", "未实现的自定义认证") 17 + //validation.SetError("CustomValid", "未实现的自定义认证")
18 } 18 }
19 19
20 func (getProductJobQuery *GetProductJobQuery) ValidateQuery() error { 20 func (getProductJobQuery *GetProductJobQuery) ValidateQuery() error {
@@ -2,6 +2,7 @@ package query @@ -2,6 +2,7 @@ package query
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
5 "reflect" 6 "reflect"
6 "strings" 7 "strings"
7 8
@@ -10,23 +11,37 @@ import ( @@ -10,23 +11,37 @@ import (
10 11
11 type ListProductJobQuery struct { 12 type ListProductJobQuery struct {
12 // 查询偏离量 13 // 查询偏离量
13 - Offset int `cname:"查询偏离量" json:"offset" valid:"Required"` 14 + Offset int `cname:"查询偏离量" json:"offset,omitempty"`
14 // 查询限制 15 // 查询限制
15 - Limit int `cname:"查询限制" json:"limit" valid:"Required"` 16 + Limit int `cname:"查询限制" json:"limit,omitempty"`
  17 + // 当前公司
  18 + CompanyId int `json:"-"`
  19 + // 当前登录的组织
  20 + OrgId int `json:"-"`
  21 + // 页码
  22 + PageNumber int `cname:"页码" json:"pageNumber,omitempty"`
  23 + // 页数
  24 + PageSize int `cname:"页数" json:"pageSize,omitempty"`
  25 + // 工位名称
  26 + JobName string `cname:"车间名称" json:"jobName,omitempty"`
  27 + // 车间名称
  28 + WorkshopName string `cname:"车间名称" json:"workshopName,omitempty"`
  29 + // 生产线名称
  30 + LineName string `cname:"生产线名称" json:"lineName,omitempty"`
16 } 31 }
17 32
18 -func (listProductJobQuery *ListProductJobQuery) Valid(validation *validation.Validation) {  
19 - validation.SetError("CustomValid", "未实现的自定义认证") 33 +func (cmd *ListProductJobQuery) Valid(validation *validation.Validation) {
  34 + cmd.Offset, cmd.Limit = domain.Pagination(cmd.PageNumber, cmd.PageSize)
20 } 35 }
21 36
22 -func (listProductJobQuery *ListProductJobQuery) ValidateQuery() error { 37 +func (cmd *ListProductJobQuery) ValidateQuery() error {
23 valid := validation.Validation{} 38 valid := validation.Validation{}
24 - b, err := valid.Valid(listProductJobQuery) 39 + b, err := valid.Valid(cmd)
25 if err != nil { 40 if err != nil {
26 return err 41 return err
27 } 42 }
28 if !b { 43 if !b {
29 - elem := reflect.TypeOf(listProductJobQuery).Elem() 44 + elem := reflect.TypeOf(cmd).Elem()
30 for _, validErr := range valid.Errors { 45 for _, validErr := range valid.Errors {
31 field, isExist := elem.FieldByName(validErr.Field) 46 field, isExist := elem.FieldByName(validErr.Field)
32 if isExist { 47 if isExist {
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  6 + "reflect"
  7 + "strings"
  8 +
  9 + "github.com/beego/beego/v2/core/validation"
  10 +)
  11 +
  12 +type SearchProductJobQuery struct {
  13 + // 查询偏离量
  14 + Offset int `cname:"查询偏离量" json:"offset,omitempty"`
  15 + // 查询限制
  16 + Limit int `cname:"查询限制" json:"limit,omitempty"`
  17 + // 当前公司
  18 + CompanyId int `cname:"当前公司" json:"companyId,omitempty" valid:"Required"`
  19 + // 当前登录的组织
  20 + OrgId int `cname:"当前登录的组织" json:"orgId,omitempty" valid:"Required"`
  21 + // 页码
  22 + PageNumber int `cname:"页码" json:"pageNumber,omitempty"`
  23 + // 页数
  24 + PageSize int `cname:"页数" json:"pageSize,omitempty"`
  25 + // 工位名称
  26 + JobName string `cname:"车间名称" json:"jobName,omitempty"`
  27 + // 车间名称
  28 + WorkshopName string `cname:"车间名称" json:"workshopName,omitempty"`
  29 + // 生产线名称
  30 + LineName string `cname:"生产线名称" json:"lineName,omitempty"`
  31 +}
  32 +
  33 +func (cmd *SearchProductJobQuery) Valid(validation *validation.Validation) {
  34 + cmd.Offset, cmd.Limit = domain.Pagination(cmd.PageNumber, cmd.PageSize)
  35 +}
  36 +
  37 +func (cmd *SearchProductJobQuery) ValidateQuery() error {
  38 + valid := validation.Validation{}
  39 + b, err := valid.Valid(cmd)
  40 + if err != nil {
  41 + return err
  42 + }
  43 + if !b {
  44 + elem := reflect.TypeOf(cmd).Elem()
  45 + for _, validErr := range valid.Errors {
  46 + field, isExist := elem.FieldByName(validErr.Field)
  47 + if isExist {
  48 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  49 + } else {
  50 + return fmt.Errorf(validErr.Message)
  51 + }
  52 + }
  53 + }
  54 + return nil
  55 +}
@@ -6,8 +6,11 @@ import ( @@ -6,8 +6,11 @@ import (
6 "github.com/linmadan/egglib-go/utils/tool_funs" 6 "github.com/linmadan/egglib-go/utils/tool_funs"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productJob/command" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productJob/command"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productJob/dto"
9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productJob/query" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productJob/query"
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  12 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
  13 + "time"
11 ) 14 )
12 15
13 // 工位服务 16 // 工位服务
@@ -15,8 +18,8 @@ type ProductJobService struct { @@ -15,8 +18,8 @@ type ProductJobService struct {
15 } 18 }
16 19
17 // 创建工位服务 20 // 创建工位服务
18 -func (productJobService *ProductJobService) CreateProductJob(createProductJobCommand *command.CreateProductJobCommand) (interface{}, error) {  
19 - if err := createProductJobCommand.ValidateCommand(); err != nil { 21 +func (productJobService *ProductJobService) CreateProductJob(operateInfo *domain.OperateInfo, cmd *command.CreateProductJobCommand) (interface{}, error) {
  22 + if err := cmd.ValidateCommand(); err != nil {
20 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 23 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
21 } 24 }
22 transactionContext, err := factory.CreateTransactionContext(nil) 25 transactionContext, err := factory.CreateTransactionContext(nil)
@@ -29,24 +32,36 @@ func (productJobService *ProductJobService) CreateProductJob(createProductJobCom @@ -29,24 +32,36 @@ func (productJobService *ProductJobService) CreateProductJob(createProductJobCom
29 defer func() { 32 defer func() {
30 transactionContext.RollbackTransaction() 33 transactionContext.RollbackTransaction()
31 }() 34 }()
32 - newProductJob := &domain.ProductJob{  
33 - CompanyId: createProductJobCommand.CompanyId,  
34 - OrgId: createProductJobCommand.OrgId,  
35 - JobName: createProductJobCommand.JobName,  
36 - //WorkshopId: createProductJobCommand.WorkshopId,  
37 - //LineId: createProductJobCommand.LineId,  
38 - //SectionId: createProductJobCommand.SectionId,  
39 - RelatedDevices: createProductJobCommand.RelatedDevices, 35 +
  36 + //var workshopRepository domain.WorkshopRepository
  37 + var workStation *domain.WorkStation
  38 + _, workStation, err = factory.FastPgWorkstation(transactionContext, cmd.WorkshopId, cmd.LineId, cmd.SectionId, factory.WithSetPrincipal())
  39 + if err != nil {
  40 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
40 } 41 }
  42 +
41 var productJobRepository domain.ProductJobRepository 43 var productJobRepository domain.ProductJobRepository
42 - if value, err := factory.CreateProductJobRepository(map[string]interface{}{  
43 - "transactionContext": transactionContext,  
44 - }); err != nil {  
45 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
46 - } else {  
47 - productJobRepository = value 44 + var productJob *domain.ProductJob
  45 + productJobRepository, productJob, _ = factory.FastPgProductJob(transactionContext, 0)
  46 +
  47 + if job, err := productJobRepository.FindOne(map[string]interface{}{
  48 + "jobName": cmd.JobName,
  49 + "workStationId": workStation.WorkStationId,
  50 + }); err == nil && job != nil {
  51 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "有重复的工位")
48 } 52 }
49 - if productJob, err := productJobRepository.Save(newProductJob); err != nil { 53 +
  54 + productJob = &domain.ProductJob{
  55 + CompanyId: operateInfo.CompanyId,
  56 + OrgId: operateInfo.OrgId,
  57 + JobName: cmd.JobName,
  58 + ProcessName: cmd.ProcessName,
  59 + CreatedAt: time.Now(),
  60 + UpdatedAt: time.Now(),
  61 + WorkStation: workStation,
  62 + RelatedDevices: cmd.RelatedDevices,
  63 + }
  64 + if productJob, err := productJobRepository.Save(productJob); err != nil {
50 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 65 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
51 } else { 66 } else {
52 if err := transactionContext.CommitTransaction(); err != nil { 67 if err := transactionContext.CommitTransaction(); err != nil {
@@ -71,26 +86,17 @@ func (productJobService *ProductJobService) GetProductJob(getProductJobQuery *qu @@ -71,26 +86,17 @@ func (productJobService *ProductJobService) GetProductJob(getProductJobQuery *qu
71 defer func() { 86 defer func() {
72 transactionContext.RollbackTransaction() 87 transactionContext.RollbackTransaction()
73 }() 88 }()
74 - var productJobRepository domain.ProductJobRepository  
75 - if value, err := factory.CreateProductJobRepository(map[string]interface{}{  
76 - "transactionContext": transactionContext,  
77 - }); err != nil {  
78 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
79 - } else {  
80 - productJobRepository = value  
81 - }  
82 - productJob, err := productJobRepository.FindOne(map[string]interface{}{"productJobId": getProductJobQuery.ProductJobId}) 89 + //var productJobRepository domain.ProductJobRepository
  90 + _, productJob, err := factory.FastPgProductJob(transactionContext, getProductJobQuery.ProductJobId)
83 if err != nil { 91 if err != nil {
84 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 92 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
85 } 93 }
86 - if productJob == nil {  
87 - return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProductJobQuery.ProductJobId)))  
88 - } else {  
89 - if err := transactionContext.CommitTransaction(); err != nil {  
90 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
91 - }  
92 - return productJob, nil 94 + if err := transactionContext.CommitTransaction(); err != nil {
  95 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
93 } 96 }
  97 + newJobDto := &dto.ProductJobDto{}
  98 + newJobDto.LoadDto(productJob)
  99 + return newJobDto, nil
94 } 100 }
95 101
96 // 返回工位服务列表 102 // 返回工位服务列表
@@ -170,8 +176,8 @@ func (productJobService *ProductJobService) RemoveProductJob(removeProductJobCom @@ -170,8 +176,8 @@ func (productJobService *ProductJobService) RemoveProductJob(removeProductJobCom
170 } 176 }
171 177
172 // 更新工位服务 178 // 更新工位服务
173 -func (productJobService *ProductJobService) UpdateProductJob(updateProductJobCommand *command.UpdateProductJobCommand) (interface{}, error) {  
174 - if err := updateProductJobCommand.ValidateCommand(); err != nil { 179 +func (productJobService *ProductJobService) UpdateProductJob(cmd *command.UpdateProductJobCommand) (interface{}, error) {
  180 + if err := cmd.ValidateCommand(); err != nil {
175 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 181 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
176 } 182 }
177 transactionContext, err := factory.CreateTransactionContext(nil) 183 transactionContext, err := factory.CreateTransactionContext(nil)
@@ -184,24 +190,41 @@ func (productJobService *ProductJobService) UpdateProductJob(updateProductJobCom @@ -184,24 +190,41 @@ func (productJobService *ProductJobService) UpdateProductJob(updateProductJobCom
184 defer func() { 190 defer func() {
185 transactionContext.RollbackTransaction() 191 transactionContext.RollbackTransaction()
186 }() 192 }()
187 - var productJobRepository domain.ProductJobRepository  
188 - if value, err := factory.CreateProductJobRepository(map[string]interface{}{  
189 - "transactionContext": transactionContext,  
190 - }); err != nil {  
191 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
192 - } else {  
193 - productJobRepository = value  
194 - }  
195 - productJob, err := productJobRepository.FindOne(map[string]interface{}{"productJobId": updateProductJobCommand.ProductJobId}) 193 +
  194 + //var workshopRepository domain.WorkshopRepository
  195 + var workStation *domain.WorkStation
  196 + _, workStation, err = factory.FastPgWorkstation(transactionContext, cmd.WorkshopId, cmd.LineId, cmd.SectionId, factory.WithSetPrincipal())
196 if err != nil { 197 if err != nil {
197 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 198 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
198 } 199 }
199 - if productJob == nil {  
200 - return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateProductJobCommand.ProductJobId)))  
201 - }  
202 - if err := productJob.Update(tool_funs.SimpleStructToMap(updateProductJobCommand)); err != nil {  
203 - return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) 200 +
  201 + var productJobRepository domain.ProductJobRepository
  202 + var productJob *domain.ProductJob
  203 + var checkDuplicateJobName = false
  204 + productJobRepository, productJob, _ = factory.FastPgProductJob(transactionContext, cmd.ProductJobId)
  205 +
  206 + if cmd.JobName != productJob.JobName {
  207 + checkDuplicateJobName = true
  208 + }
  209 + if workStation.WorkStationId != productJob.WorkStation.WorkStationId {
  210 + checkDuplicateJobName = true
  211 + }
  212 + if checkDuplicateJobName {
  213 + if job, err := productJobRepository.FindOne(map[string]interface{}{
  214 + "jobName": cmd.JobName,
  215 + "companyId": productJob.CompanyId,
  216 + "orgId": productJob.OrgId,
  217 + "workStationId": workStation.WorkStationId,
  218 + }); err == nil && job != nil {
  219 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "有重复的工位")
  220 + }
204 } 221 }
  222 +
  223 + productJob.WorkStation = workStation
  224 + productJob.JobName = cmd.JobName
  225 + productJob.RelatedDevices = cmd.RelatedDevices
  226 + productJob.UpdatedAt = time.Now()
  227 +
205 if productJob, err := productJobRepository.Save(productJob); err != nil { 228 if productJob, err := productJobRepository.Save(productJob); err != nil {
206 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 229 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
207 } else { 230 } else {
@@ -212,6 +235,46 @@ func (productJobService *ProductJobService) UpdateProductJob(updateProductJobCom @@ -212,6 +235,46 @@ func (productJobService *ProductJobService) UpdateProductJob(updateProductJobCom
212 } 235 }
213 } 236 }
214 237
  238 +// 搜索工位服务列表
  239 +func (productJobService *ProductJobService) SearchProductJob(operateInfo *domain.OperateInfo, listProductJobQuery *query.SearchProductJobQuery) (int64, interface{}, error) {
  240 + listProductJobQuery.OrgId = operateInfo.OrgId
  241 + listProductJobQuery.CompanyId = operateInfo.CompanyId
  242 + if err := listProductJobQuery.ValidateQuery(); err != nil {
  243 + return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
  244 + }
  245 + transactionContext, err := factory.CreateTransactionContext(nil)
  246 + if err != nil {
  247 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  248 + }
  249 + if err := transactionContext.StartTransaction(); err != nil {
  250 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  251 + }
  252 + defer func() {
  253 + transactionContext.RollbackTransaction()
  254 + }()
  255 + var productJobRepository domain.ProductJobRepository
  256 + if value, err := factory.CreateProductJobRepository(map[string]interface{}{
  257 + "transactionContext": transactionContext,
  258 + }); err != nil {
  259 + return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  260 + } else {
  261 + productJobRepository = value
  262 + }
  263 +
  264 + count, productJobs, err := productJobRepository.Find(utils.ObjectToMap(listProductJobQuery))
  265 + if err != nil {
  266 + return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  267 + }
  268 + var result = make([]*dto.ProductJobDto, 0)
  269 + for i := range productJobs {
  270 + item := productJobs[i]
  271 + newJobDto := &dto.ProductJobDto{}
  272 + newJobDto.LoadDto(item)
  273 + result = append(result, newJobDto)
  274 + }
  275 + return count, result, nil
  276 +}
  277 +
215 func NewProductJobService(options map[string]interface{}) *ProductJobService { 278 func NewProductJobService(options map[string]interface{}) *ProductJobService {
216 newProductJobService := &ProductJobService{} 279 newProductJobService := &ProductJobService{}
217 return newProductJobService 280 return newProductJobService
@@ -133,8 +133,8 @@ func (productSectionService *ProductSectionService) ListProductSection(listProdu @@ -133,8 +133,8 @@ func (productSectionService *ProductSectionService) ListProductSection(listProdu
133 } 133 }
134 134
135 // 移除工段服务 135 // 移除工段服务
136 -func (productSectionService *ProductSectionService) RemoveProductSection(removeProductSectionCommand *command.RemoveProductSectionCommand) (interface{}, error) {  
137 - if err := removeProductSectionCommand.ValidateCommand(); err != nil { 136 +func (productSectionService *ProductSectionService) RemoveProductSection(cmd *command.RemoveProductSectionCommand) (interface{}, error) {
  137 + if err := cmd.ValidateCommand(); err != nil {
138 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 138 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
139 } 139 }
140 transactionContext, err := factory.CreateTransactionContext(nil) 140 transactionContext, err := factory.CreateTransactionContext(nil)
@@ -149,13 +149,25 @@ func (productSectionService *ProductSectionService) RemoveProductSection(removeP @@ -149,13 +149,25 @@ func (productSectionService *ProductSectionService) RemoveProductSection(removeP
149 }() 149 }()
150 var workshopRepository domain.WorkshopRepository 150 var workshopRepository domain.WorkshopRepository
151 var workshop *domain.Workshop 151 var workshop *domain.Workshop
152 - workshopRepository, workshop, err = factory.FastPgWorkshop(transactionContext, removeProductSectionCommand.WorkshopId) 152 + workshopRepository, workshop, err = factory.FastPgWorkshop(transactionContext, cmd.WorkshopId)
153 if err != nil { 153 if err != nil {
154 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 154 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
155 } 155 }
156 - if err = workshop.RemoveSection(removeProductSectionCommand.LineId, removeProductSectionCommand.SectionId); err != nil { 156 +
  157 + workStation, err := workshop.FindWorkStation(workshop.WorkshopId, cmd.LineId, cmd.SectionId)
  158 + if err != nil {
157 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 159 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
158 } 160 }
  161 + var productJobRepository domain.ProductJobRepository
  162 + productJobRepository, _, _ = factory.FastPgProductJob(transactionContext, 0)
  163 + if count, _, err := productJobRepository.Find(map[string]interface{}{"workStationId": workStation.WorkStationId, "limit": 1}); err == nil && count > 0 {
  164 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "该工段下存在工位")
  165 + }
  166 +
  167 + if err = workshop.RemoveSection(cmd.LineId, cmd.SectionId); err != nil {
  168 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  169 + }
  170 +
159 if workshop, err = workshopRepository.Save(workshop); err != nil { 171 if workshop, err = workshopRepository.Save(workshop); err != nil {
160 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 172 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
161 } 173 }
@@ -70,3 +70,11 @@ func (info OperateInfo) GetUserId(userId int) int { @@ -70,3 +70,11 @@ func (info OperateInfo) GetUserId(userId int) int {
70 func (info OperateInfo) String() string { 70 func (info OperateInfo) String() string {
71 return fmt.Sprintf("UserId: %v OrgId:%v CompanyId:%v", info.UserId, info.OrgId, info.CompanyId) 71 return fmt.Sprintf("UserId: %v OrgId:%v CompanyId:%v", info.UserId, info.OrgId, info.CompanyId)
72 } 72 }
  73 +
  74 +func Pagination(pageNumber, pageSize int) (offset int, limit int) {
  75 + if pageSize != 0 {
  76 + offset = (pageNumber - 1) * pageSize
  77 + limit = pageSize
  78 + }
  79 + return
  80 +}
@@ -12,6 +12,8 @@ type ProductJob struct { @@ -12,6 +12,8 @@ type ProductJob struct {
12 OrgId int `json:"orgId,omitempty"` 12 OrgId int `json:"orgId,omitempty"`
13 // 工位名称 13 // 工位名称
14 JobName string `json:"jobName,omitempty"` 14 JobName string `json:"jobName,omitempty"`
  15 + // 工序名称
  16 + ProcessName string `json:"processName,omitempty"`
15 // 创建时间 17 // 创建时间
16 CreatedAt time.Time `json:"createdAt,omitempty"` 18 CreatedAt time.Time `json:"createdAt,omitempty"`
17 // 更新时间 19 // 更新时间
@@ -39,50 +41,12 @@ func (productJob *ProductJob) Identify() interface{} { @@ -39,50 +41,12 @@ func (productJob *ProductJob) Identify() interface{} {
39 } 41 }
40 42
41 func (productJob *ProductJob) Update(data map[string]interface{}) error { 43 func (productJob *ProductJob) Update(data map[string]interface{}) error {
42 - if productJobId, ok := data["productJobId"]; ok {  
43 - productJob.ProductJobId = productJobId.(int)  
44 - }  
45 - if companyId, ok := data["companyId"]; ok {  
46 - productJob.CompanyId = companyId.(int)  
47 - }  
48 - if orgId, ok := data["orgId"]; ok {  
49 - productJob.OrgId = orgId.(int)  
50 - }  
51 if jobName, ok := data["jobName"]; ok { 44 if jobName, ok := data["jobName"]; ok {
52 productJob.JobName = jobName.(string) 45 productJob.JobName = jobName.(string)
53 } 46 }
54 - if createdAt, ok := data["createdAt"]; ok {  
55 - productJob.CreatedAt = createdAt.(time.Time)  
56 - }  
57 - if updatedAt, ok := data["updatedAt"]; ok {  
58 - productJob.UpdatedAt = updatedAt.(time.Time)  
59 - }  
60 - if deletedAt, ok := data["deletedAt"]; ok {  
61 - productJob.DeletedAt = deletedAt.(time.Time)  
62 - }  
63 - if workStationId, ok := data["workStationId"]; ok {  
64 - productJob.WorkStation.WorkStationId = workStationId.(string)  
65 - }  
66 - if workshopId, ok := data["workshopId"]; ok {  
67 - productJob.WorkStation.WorkshopId = workshopId.(int)  
68 - }  
69 - if workshopName, ok := data["workshopName"]; ok {  
70 - productJob.WorkStation.WorkshopName = workshopName.(string)  
71 - }  
72 - if lineId, ok := data["lineId"]; ok {  
73 - productJob.WorkStation.LineId = lineId.(int)  
74 - }  
75 - if lineName, ok := data["lineName"]; ok {  
76 - productJob.WorkStation.LineName = lineName.(string)  
77 - }  
78 - if sectionId, ok := data["sectionId"]; ok {  
79 - productJob.WorkStation.SectionId = sectionId.(int)  
80 - }  
81 - if sectionName, ok := data["sectionName"]; ok {  
82 - productJob.WorkStation.SectionName = sectionName.(string) 47 + if relatedDevices, ok := data["relatedDevices"]; ok {
  48 + productJob.RelatedDevices = relatedDevices.([]int)
83 } 49 }
84 - //if relatedDevices, ok := data["relatedDevices"]; ok {  
85 - // productJob.RelatedDevices = relatedDevices.(array)  
86 - //} 50 + productJob.UpdatedAt = time.Now()
87 return nil 51 return nil
88 } 52 }
1 package domain 1 package domain
2 2
  3 +import "fmt"
  4 +
3 // 工作位置(车间、生产线、工段组成),唯一标识工作位置 5 // 工作位置(车间、生产线、工段组成),唯一标识工作位置
4 type WorkStation struct { 6 type WorkStation struct {
5 // 工作位置键值 (车间ID+'.'+生产线ID+'.'+工段ID) 7 // 工作位置键值 (车间ID+'.'+生产线ID+'.'+工段ID)
@@ -16,4 +18,18 @@ type WorkStation struct { @@ -16,4 +18,18 @@ type WorkStation struct {
16 SectionId int `json:"sectionId,omitempty"` 18 SectionId int `json:"sectionId,omitempty"`
17 // 工段名称 19 // 工段名称
18 SectionName string `json:"sectionName,omitempty"` 20 SectionName string `json:"sectionName,omitempty"`
  21 + // 负责人 (用户对象)
  22 + Principal *User `json:"principal,omitempty"`
  23 +}
  24 +
  25 +func NewWorkStation(w *Workshop, l *ProductLine, s *ProductSection) *WorkStation {
  26 + return &WorkStation{
  27 + WorkStationId: fmt.Sprintf("%v.%v.%v", w.WorkshopId, l.LineId, s.SectionId),
  28 + WorkshopId: w.WorkshopId,
  29 + WorkshopName: w.WorkshopName,
  30 + LineId: l.LineId,
  31 + LineName: l.LineName,
  32 + SectionId: s.SectionId,
  33 + SectionName: s.SectionName,
  34 + }
19 } 35 }
@@ -187,3 +187,19 @@ func (workshop *Workshop) FindSection(lineId, sectionId int) (*ProductSection, e @@ -187,3 +187,19 @@ func (workshop *Workshop) FindSection(lineId, sectionId int) (*ProductSection, e
187 } 187 }
188 return nil, fmt.Errorf("工段不存在") 188 return nil, fmt.Errorf("工段不存在")
189 } 189 }
  190 +
  191 +// 查询生产线
  192 +func (workshop *Workshop) FindWorkStation(workshopId, lineId, sectionId int) (*WorkStation, error) {
  193 + if workshop.WorkshopId != workshopId {
  194 + return nil, fmt.Errorf("不存在")
  195 + }
  196 + line, err := workshop.FindLine(lineId)
  197 + if err != nil {
  198 + return nil, err
  199 + }
  200 + section, err := workshop.FindSection(lineId, sectionId)
  201 + if err != nil {
  202 + return nil, err
  203 + }
  204 + return NewWorkStation(workshop, line, section), nil
  205 +}
@@ -20,9 +20,11 @@ type ProductJob struct { @@ -20,9 +20,11 @@ type ProductJob struct {
20 // 更新时间 20 // 更新时间
21 UpdatedAt time.Time `comment:"更新时间"` 21 UpdatedAt time.Time `comment:"更新时间"`
22 // 删除时间 22 // 删除时间
23 - DeletedAt time.Time `comment:"删除时间"` 23 + DeletedAt time.Time `pg:",soft_delete" comment:"删除时间"`
24 // 工作位置 24 // 工作位置
25 WorkStation *domain.WorkStation `comment:"工作位置"` 25 WorkStation *domain.WorkStation `comment:"工作位置"`
26 // 关联设备列表 26 // 关联设备列表
27 - RelatedDevices []int `pg:",soft_delete" comment:"关联设备列表" pg:",array"` 27 + RelatedDevices []int ` comment:"关联设备列表"`
  28 + // 工序名称
  29 + ProcessName string ` comment:"工序名称"`
28 } 30 }
@@ -16,5 +16,6 @@ func TransformToProductJobDomainModelFromPgModels(productJobModel *models.Produc @@ -16,5 +16,6 @@ func TransformToProductJobDomainModelFromPgModels(productJobModel *models.Produc
16 DeletedAt: productJobModel.DeletedAt, 16 DeletedAt: productJobModel.DeletedAt,
17 WorkStation: productJobModel.WorkStation, 17 WorkStation: productJobModel.WorkStation,
18 RelatedDevices: productJobModel.RelatedDevices, 18 RelatedDevices: productJobModel.RelatedDevices,
  19 + ProcessName: productJobModel.ProcessName,
19 }, nil 20 }, nil
20 } 21 }
@@ -35,20 +35,15 @@ func (repository *ProductJobRepository) Save(productJob *domain.ProductJob) (*do @@ -35,20 +35,15 @@ func (repository *ProductJobRepository) Save(productJob *domain.ProductJob) (*do
35 "deleted_at", 35 "deleted_at",
36 "work_station", 36 "work_station",
37 "related_devices", 37 "related_devices",
  38 + "process_name",
38 } 39 }
39 - insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)  
40 - insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields) 40 + insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_job_id", "deleted_at"))
  41 + insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_job_id", "deleted_at"))
41 returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields) 42 returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
42 - updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "productJob_id") 43 + updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_job_id", "deleted_at")
43 updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields) 44 updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
44 tx := repository.transactionContext.PgTx 45 tx := repository.transactionContext.PgTx
45 if productJob.Identify() == nil { 46 if productJob.Identify() == nil {
46 - productJobId, err := repository.nextIdentify()  
47 - if err != nil {  
48 - return productJob, err  
49 - } else {  
50 - productJob.ProductJobId = int(productJobId)  
51 - }  
52 if _, err := tx.QueryOne( 47 if _, err := tx.QueryOne(
53 pg.Scan( 48 pg.Scan(
54 &productJob.ProductJobId, 49 &productJob.ProductJobId,
@@ -59,18 +54,18 @@ func (repository *ProductJobRepository) Save(productJob *domain.ProductJob) (*do @@ -59,18 +54,18 @@ func (repository *ProductJobRepository) Save(productJob *domain.ProductJob) (*do
59 &productJob.UpdatedAt, 54 &productJob.UpdatedAt,
60 &productJob.DeletedAt, 55 &productJob.DeletedAt,
61 &productJob.WorkStation, 56 &productJob.WorkStation,
62 - pg.Array(&productJob.RelatedDevices), 57 + &productJob.RelatedDevices,
  58 + &productJob.ProcessName,
63 ), 59 ),
64 - fmt.Sprintf("INSERT INTO product_jobs (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),  
65 - productJob.ProductJobId, 60 + fmt.Sprintf("INSERT INTO manufacture.product_job (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
66 productJob.CompanyId, 61 productJob.CompanyId,
67 productJob.OrgId, 62 productJob.OrgId,
68 productJob.JobName, 63 productJob.JobName,
69 productJob.CreatedAt, 64 productJob.CreatedAt,
70 productJob.UpdatedAt, 65 productJob.UpdatedAt,
71 - productJob.DeletedAt,  
72 productJob.WorkStation, 66 productJob.WorkStation,
73 - pg.Array(productJob.RelatedDevices), 67 + productJob.RelatedDevices,
  68 + productJob.ProcessName,
74 ); err != nil { 69 ); err != nil {
75 return productJob, err 70 return productJob, err
76 } 71 }
@@ -85,18 +80,18 @@ func (repository *ProductJobRepository) Save(productJob *domain.ProductJob) (*do @@ -85,18 +80,18 @@ func (repository *ProductJobRepository) Save(productJob *domain.ProductJob) (*do
85 &productJob.UpdatedAt, 80 &productJob.UpdatedAt,
86 &productJob.DeletedAt, 81 &productJob.DeletedAt,
87 &productJob.WorkStation, 82 &productJob.WorkStation,
88 - pg.Array(&productJob.RelatedDevices), 83 + &productJob.RelatedDevices,
  84 + &productJob.ProcessName,
89 ), 85 ),
90 - fmt.Sprintf("UPDATE product_jobs SET %s WHERE product_job_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),  
91 - productJob.ProductJobId, 86 + fmt.Sprintf("UPDATE manufacture.product_job SET %s WHERE product_job_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
92 productJob.CompanyId, 87 productJob.CompanyId,
93 productJob.OrgId, 88 productJob.OrgId,
94 productJob.JobName, 89 productJob.JobName,
95 productJob.CreatedAt, 90 productJob.CreatedAt,
96 productJob.UpdatedAt, 91 productJob.UpdatedAt,
97 - productJob.DeletedAt,  
98 productJob.WorkStation, 92 productJob.WorkStation,
99 - pg.Array(productJob.RelatedDevices), 93 + productJob.RelatedDevices,
  94 + productJob.ProcessName,
100 productJob.Identify(), 95 productJob.Identify(),
101 ); err != nil { 96 ); err != nil {
102 return productJob, err 97 return productJob, err
@@ -118,6 +113,10 @@ func (repository *ProductJobRepository) FindOne(queryOptions map[string]interfac @@ -118,6 +113,10 @@ func (repository *ProductJobRepository) FindOne(queryOptions map[string]interfac
118 productJobModel := new(models.ProductJob) 113 productJobModel := new(models.ProductJob)
119 query := sqlbuilder.BuildQuery(tx.Model(productJobModel), queryOptions) 114 query := sqlbuilder.BuildQuery(tx.Model(productJobModel), queryOptions)
120 query.SetWhereByQueryOption("product_job.product_job_id = ?", "productJobId") 115 query.SetWhereByQueryOption("product_job.product_job_id = ?", "productJobId")
  116 + query.SetWhereByQueryOption("company_id = ?", "companyId")
  117 + query.SetWhereByQueryOption("org_id = ?", "orgId")
  118 + query.SetWhereByQueryOption("job_name=?", "jobName")
  119 + query.SetWhereByQueryOption("work_station->>'workStationId'=?", "workStationId")
121 if err := query.First(); err != nil { 120 if err := query.First(); err != nil {
122 if err.Error() == "pg: no rows in result set" { 121 if err.Error() == "pg: no rows in result set" {
123 return nil, fmt.Errorf("没有此资源") 122 return nil, fmt.Errorf("没有此资源")
@@ -136,7 +135,23 @@ func (repository *ProductJobRepository) Find(queryOptions map[string]interface{} @@ -136,7 +135,23 @@ func (repository *ProductJobRepository) Find(queryOptions map[string]interface{}
136 var productJobModels []*models.ProductJob 135 var productJobModels []*models.ProductJob
137 productJobs := make([]*domain.ProductJob, 0) 136 productJobs := make([]*domain.ProductJob, 0)
138 query := sqlbuilder.BuildQuery(tx.Model(&productJobModels), queryOptions) 137 query := sqlbuilder.BuildQuery(tx.Model(&productJobModels), queryOptions)
139 - query.SetOffsetAndLimit(20) 138 +
  139 + query.SetWhereByQueryOption("company_id = ?", "companyId")
  140 + query.SetWhereByQueryOption("org_id = ?", "orgId")
  141 + query.SetWhereByQueryOption("work_station->>'workStationId'=?", "workStationId")
  142 + if v, ok := queryOptions["jobName"]; ok && len(v.(string)) > 0 {
  143 + query.Where(fmt.Sprintf(`job_name like '%%%v%%'`, v))
  144 + }
  145 + if v, ok := queryOptions["workshopName"]; ok && len(v.(string)) > 0 {
  146 + query.Where(fmt.Sprintf(`work_station->>'workshopName' like '%%%v%%'`, v))
  147 + }
  148 + if v, ok := queryOptions["lineName"]; ok && len(v.(string)) > 0 {
  149 + query.Where(fmt.Sprintf(`work_station->>'lineName' like '%%%v%%'`, v))
  150 + }
  151 + if v, ok := queryOptions["sectionName"]; ok && len(v.(string)) > 0 {
  152 + query.Where(fmt.Sprintf(`work_station->>'sectionName' like '%%%v%%'`, v))
  153 + }
  154 + query.SetOffsetAndLimit(domain.MaxQueryRow)
140 query.SetOrderDirect("product_job_id", "DESC") 155 query.SetOrderDirect("product_job_id", "DESC")
141 if count, err := query.SelectAndCount(); err != nil { 156 if count, err := query.SelectAndCount(); err != nil {
142 return 0, productJobs, err 157 return 0, productJobs, err
  1 +package utils
  2 +
  3 +import (
  4 + "bytes"
  5 + "encoding/json"
  6 + "fmt"
  7 + jsonlib "github.com/linmadan/egglib-go/utils/json"
  8 + "io"
  9 + "reflect"
  10 + "strconv"
  11 + "strings"
  12 + "time"
  13 +)
  14 +
  15 +func CamelCase(name string, firstUpper bool) string {
  16 + array := []byte(name)
  17 + if len(array) == 0 {
  18 + return ""
  19 + }
  20 + rspArray := make([]byte, len(array))
  21 + if firstUpper {
  22 + copy(rspArray[:1], strings.ToUpper(string(array[:1])))
  23 + } else {
  24 + copy(rspArray[:1], strings.ToLower(string(array[:1])))
  25 + }
  26 + copy(rspArray[1:], array[1:])
  27 + return string(rspArray)
  28 +}
  29 +
  30 +func ObjectToMap(o interface{}) map[string]interface{} {
  31 + if o == nil {
  32 + return nil
  33 + }
  34 + value := reflect.ValueOf(o)
  35 + if value.Kind() != reflect.Ptr {
  36 + return nil
  37 + }
  38 + elem := value.Elem()
  39 + relType := elem.Type()
  40 + m := make(map[string]interface{})
  41 + for i := 0; i < relType.NumField(); i++ {
  42 + field := relType.Field(i)
  43 + if elem.Field(i).IsZero() {
  44 + continue
  45 + }
  46 + m[CamelCase(field.Name, false)] = elem.Field(i).Interface()
  47 + }
  48 + return m
  49 +}
  50 +
  51 +// AssertString convert v to string value
  52 +func AssertString(v interface{}) string {
  53 + if v == nil {
  54 + return ""
  55 + }
  56 +
  57 + // if func (v *Type) String() string, we can't use Elem()
  58 + switch vt := v.(type) {
  59 + case fmt.Stringer:
  60 + return vt.String()
  61 + }
  62 +
  63 + val := reflect.ValueOf(v)
  64 + if val.Kind() == reflect.Ptr && !val.IsNil() {
  65 + val = val.Elem()
  66 + }
  67 +
  68 + switch vt := val.Interface().(type) {
  69 + case bool:
  70 + return strconv.FormatBool(vt)
  71 + case error:
  72 + return vt.Error()
  73 + case float32:
  74 + return strconv.FormatFloat(float64(vt), 'f', -1, 32)
  75 + case float64:
  76 + return strconv.FormatFloat(vt, 'f', -1, 64)
  77 + case fmt.Stringer:
  78 + return vt.String()
  79 + case int:
  80 + return strconv.Itoa(vt)
  81 + case int8:
  82 + return strconv.Itoa(int(vt))
  83 + case int16:
  84 + return strconv.Itoa(int(vt))
  85 + case int32:
  86 + return strconv.Itoa(int(vt))
  87 + case int64:
  88 + return strconv.FormatInt(vt, 10)
  89 + case string:
  90 + return vt
  91 + case uint:
  92 + return strconv.FormatUint(uint64(vt), 10)
  93 + case uint8:
  94 + return strconv.FormatUint(uint64(vt), 10)
  95 + case uint16:
  96 + return strconv.FormatUint(uint64(vt), 10)
  97 + case uint32:
  98 + return strconv.FormatUint(uint64(vt), 10)
  99 + case uint64:
  100 + return strconv.FormatUint(vt, 10)
  101 + case []byte:
  102 + return string(vt)
  103 + default:
  104 + return fmt.Sprint(val.Interface())
  105 + }
  106 +}
  107 +
  108 +// ValidatePtr validate v is a ptr value
  109 +func ValidatePtr(v *reflect.Value) error {
  110 + // sequence is very important, IsNil must be called after checking Kind() with reflect.Ptr,
  111 + // panic otherwise
  112 + if !v.IsValid() || v.Kind() != reflect.Ptr || v.IsNil() {
  113 + return fmt.Errorf("not a valid pointer: %v", v)
  114 + }
  115 +
  116 + return nil
  117 +}
  118 +
  119 +func LoadCustomFieldToMap(src interface{}, fields ...string) map[string]interface{} {
  120 + rsp := LoadCustomField(src, fields...)
  121 + if rsp == nil {
  122 + return map[string]interface{}{}
  123 + }
  124 + return rsp.(map[string]interface{})
  125 +}
  126 +
  127 +func LoadCustomField(src interface{}, fields ...string) interface{} {
  128 + typeSrc := reflect.TypeOf(src)
  129 + valueSrc := reflect.ValueOf(src)
  130 +
  131 + if v, ok := src.(reflect.Value); ok {
  132 + valueSrc = v
  133 + typeSrc = v.Type()
  134 + }
  135 + if typeSrc.Kind() == reflect.Ptr {
  136 + valueSrc = valueSrc.Elem()
  137 + }
  138 + k := valueSrc.Kind()
  139 + switch k {
  140 + case reflect.Array, reflect.Slice:
  141 + len := valueSrc.Len()
  142 + retSliceMap := make([]map[string]interface{}, 0)
  143 + if len == 0 {
  144 + return retSliceMap
  145 + }
  146 + for i := 0; i < len; i++ {
  147 + v := valueSrc.Index(i)
  148 + retSliceMap = append(retSliceMap, (LoadCustomField(v, fields...)).(map[string]interface{}))
  149 + }
  150 + return retSliceMap
  151 + case reflect.Struct:
  152 + retSliceMap := make(map[string]interface{})
  153 + for _, filed := range fields {
  154 + f := valueSrc.FieldByName(filed)
  155 + if !f.IsValid() {
  156 + continue
  157 + }
  158 + v := f.Interface()
  159 + if t, ok := v.(time.Time); ok {
  160 + v = t.Local().Format("2006-01-02 15:04:05")
  161 + }
  162 + retSliceMap[CamelCase(filed, false)] = v
  163 + }
  164 + return retSliceMap
  165 + default:
  166 + return src
  167 + }
  168 + return src
  169 +}
  170 +
  171 +func AppendCustomField(src interface{}, options map[string]interface{}) interface{} {
  172 + var mapSrc map[string]interface{}
  173 + var ok bool
  174 + mapSrc, ok = src.(map[string]interface{})
  175 + if !ok {
  176 + jsonlib.Unmarshal([]byte(jsonlib.MarshalToString(src)), &mapSrc)
  177 + }
  178 + for field, value := range options {
  179 + mapSrc[CamelCase(field, false)] = value
  180 + }
  181 + return mapSrc
  182 +}
  183 +
  184 +/*
  185 +
  186 +json 格式化
  187 +
  188 +*/
  189 +
  190 +func Marshal(v interface{}) ([]byte, error) {
  191 + return json.Marshal(v)
  192 +}
  193 +
  194 +func Unmarshal(data []byte, v interface{}) error {
  195 + decoder := json.NewDecoder(bytes.NewReader(data))
  196 + if err := unmarshalUseNumber(decoder, v); err != nil {
  197 + return formatError(string(data), err)
  198 + }
  199 +
  200 + return nil
  201 +}
  202 +
  203 +func UnmarshalFromString(str string, v interface{}) error {
  204 + decoder := json.NewDecoder(strings.NewReader(str))
  205 + if err := unmarshalUseNumber(decoder, v); err != nil {
  206 + return formatError(str, err)
  207 + }
  208 +
  209 + return nil
  210 +}
  211 +
  212 +func UnmarshalFromReader(reader io.Reader, v interface{}) error {
  213 + var buf strings.Builder
  214 + teeReader := io.TeeReader(reader, &buf)
  215 + decoder := json.NewDecoder(teeReader)
  216 + if err := unmarshalUseNumber(decoder, v); err != nil {
  217 + return formatError(buf.String(), err)
  218 + }
  219 +
  220 + return nil
  221 +}
  222 +
  223 +func unmarshalUseNumber(decoder *json.Decoder, v interface{}) error {
  224 + decoder.UseNumber()
  225 + return decoder.Decode(v)
  226 +}
  227 +
  228 +func formatError(v string, err error) error {
  229 + return fmt.Errorf("string: `%s`, error: `%s`", v, err.Error())
  230 +}
  231 +
  232 +type ReflectVal struct {
  233 + T reflect.Type
  234 + V reflect.Value
  235 +}
  236 +
  237 +/*
  238 + 拷贝当前对象到目标对象,具有相同属性的值
  239 +*/
  240 +func CopyObject(src, dst interface{}) {
  241 + var srcMap = make(map[string]ReflectVal)
  242 +
  243 + vs := reflect.ValueOf(src)
  244 + ts := reflect.TypeOf(src)
  245 + vd := reflect.ValueOf(dst)
  246 + td := reflect.TypeOf(dst)
  247 +
  248 + ls := vs.Elem().NumField()
  249 + for i := 0; i < ls; i++ {
  250 + srcMap[ts.Elem().Field(i).Name] = ReflectVal{
  251 + T: vs.Elem().Field(i).Type(),
  252 + V: vs.Elem().Field(i),
  253 + }
  254 + }
  255 +
  256 + ld := vd.Elem().NumField()
  257 + for i := 0; i < ld; i++ {
  258 + n := td.Elem().Field(i).Name
  259 + t := vd.Elem().Field(i).Type()
  260 + if v, ok := srcMap[n]; ok && v.T == t && vd.Elem().Field(i).CanSet() {
  261 + vd.Elem().Field(i).Set(v.V)
  262 + }
  263 + }
  264 +}
@@ -10,22 +10,22 @@ import ( @@ -10,22 +10,22 @@ import (
10 "strconv" 10 "strconv"
11 ) 11 )
12 12
13 -func ResponseGrid(c beego.BaseController, data interface{}, err error) { 13 +func ResponseGrid(c beego.BaseController, total int64, data interface{}, err error) {
14 var response utils.JsonResponse 14 var response utils.JsonResponse
15 if err != nil { 15 if err != nil {
16 response = utils.ResponseError(c.Ctx, err) 16 response = utils.ResponseError(c.Ctx, err)
17 } else { 17 } else {
18 - response = ResponseGridData(c.Ctx, data) 18 + response = ResponseGridData(c.Ctx, total, data)
19 } 19 }
20 c.Data["json"] = response 20 c.Data["json"] = response
21 c.ServeJSON() 21 c.ServeJSON()
22 } 22 }
23 23
24 -func ResponseGridData(ctx *context.Context, data interface{}) utils.JsonResponse { 24 +func ResponseGridData(ctx *context.Context, total int64, data interface{}) utils.JsonResponse {
25 jsonResponse := utils.JsonResponse{} 25 jsonResponse := utils.JsonResponse{}
26 jsonResponse["code"] = 0 26 jsonResponse["code"] = 0
27 jsonResponse["msg"] = "ok" 27 jsonResponse["msg"] = "ok"
28 - jsonResponse["data"] = map[string]interface{}{"grid": data} 28 + jsonResponse["data"] = map[string]interface{}{"total": total, "grid": data}
29 ctx.Input.SetData("outputData", jsonResponse) 29 ctx.Input.SetData("outputData", jsonResponse)
30 return jsonResponse 30 return jsonResponse
31 } 31 }
@@ -42,6 +42,13 @@ func ParseOperateInfo(c beego.BaseController) *domain.OperateInfo { @@ -42,6 +42,13 @@ func ParseOperateInfo(c beego.BaseController) *domain.OperateInfo {
42 opt.UserId = header(c, constant.HeaderUserId) 42 opt.UserId = header(c, constant.HeaderUserId)
43 opt.CompanyId = header(c, constant.HeaderCompanyId) 43 opt.CompanyId = header(c, constant.HeaderCompanyId)
44 opt.OrgId = header(c, constant.HeaderOrgId) 44 opt.OrgId = header(c, constant.HeaderOrgId)
  45 + // 默认公司组织
  46 + if opt.CompanyId == 0 {
  47 + opt.CompanyId = 1
  48 + }
  49 + if opt.OrgId == 0 {
  50 + opt.OrgId = 1
  51 + }
45 return opt 52 return opt
46 } 53 }
47 54
@@ -15,7 +15,7 @@ func (controller *ProductJobController) CreateProductJob() { @@ -15,7 +15,7 @@ func (controller *ProductJobController) CreateProductJob() {
15 productJobService := service.NewProductJobService(nil) 15 productJobService := service.NewProductJobService(nil)
16 createProductJobCommand := &command.CreateProductJobCommand{} 16 createProductJobCommand := &command.CreateProductJobCommand{}
17 controller.Unmarshal(createProductJobCommand) 17 controller.Unmarshal(createProductJobCommand)
18 - data, err := productJobService.CreateProductJob(createProductJobCommand) 18 + data, err := productJobService.CreateProductJob(ParseOperateInfo(controller.BaseController), createProductJobCommand)
19 controller.Response(data, err) 19 controller.Response(data, err)
20 } 20 }
21 21
@@ -58,3 +58,11 @@ func (controller *ProductJobController) ListProductJob() { @@ -58,3 +58,11 @@ func (controller *ProductJobController) ListProductJob() {
58 data, err := productJobService.ListProductJob(listProductJobQuery) 58 data, err := productJobService.ListProductJob(listProductJobQuery)
59 controller.Response(data, err) 59 controller.Response(data, err)
60 } 60 }
  61 +
  62 +func (controller *ProductJobController) SearchProductJob() {
  63 + productJobService := service.NewProductJobService(nil)
  64 + listProductJobQuery := &query.SearchProductJobQuery{}
  65 + controller.Unmarshal(listProductJobQuery)
  66 + total, data, err := productJobService.SearchProductJob(ParseOperateInfo(controller.BaseController), listProductJobQuery)
  67 + ResponseGrid(controller.BaseController, total, data, err)
  68 +}
@@ -11,4 +11,5 @@ func init() { @@ -11,4 +11,5 @@ func init() {
11 web.Router("/product-jobs/:productJobId", &controllers.ProductJobController{}, "Get:GetProductJob") 11 web.Router("/product-jobs/:productJobId", &controllers.ProductJobController{}, "Get:GetProductJob")
12 web.Router("/product-jobs/:productJobId", &controllers.ProductJobController{}, "Delete:RemoveProductJob") 12 web.Router("/product-jobs/:productJobId", &controllers.ProductJobController{}, "Delete:RemoveProductJob")
13 web.Router("/product-jobs/", &controllers.ProductJobController{}, "Get:ListProductJob") 13 web.Router("/product-jobs/", &controllers.ProductJobController{}, "Get:ListProductJob")
  14 + web.Router("/product-jobs/search", &controllers.ProductJobController{}, "Post:SearchProductJob")
14 } 15 }