作者 yangfu

feat: 计划管理

... ... @@ -14,7 +14,7 @@ type SetOnlineCommand struct {
// 车间ID
WorkshopId int `cname:"车间ID" json:"workshopId" valid:"Required"`
// 生产线ID
LineId int `cname:"生产线ID" json:"lineId" valid:"Required"`
//LineId int `cname:"生产线ID" json:"lineId" valid:"Required"`
// 工段ID
SectionId int `cname:"工段ID" json:"sectionId" valid:"Required"`
}
... ...
... ... @@ -39,7 +39,7 @@ type UpdateProductPlanCommand struct {
// 备注
Remark string `cname:"备注" json:"remark" valid:"Required"`
// 生产日期
ProductDateTime time.Time `cname:"生产日期" json:"-" `
ProductDateTime time.Time `cname:"生产日期" json:"productDateTime" `
}
func (updateProductPlanCommand *UpdateProductPlanCommand) Valid(validation *validation.Validation) {
... ...
... ... @@ -256,15 +256,28 @@ func (productPlanService *ProductPlanService) SetOffline(setOfflineCommand *comm
defer func() {
transactionContext.RollbackTransaction()
}()
var productPlanRepository domain.ProductPlanRepository
var productPlan *domain.ProductPlan
productPlanRepository, productPlan, _ = factory.FastPgProductPlan(transactionContext, setOfflineCommand.ProductPlanId)
if err = productPlan.ChangeStatus(domain.PlanOffline); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if productPlan, err = productPlanRepository.Save(productPlan); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
return productPlan, nil
}
// 计划上线
func (productPlanService *ProductPlanService) SetOnline(setOnlineCommand *command.SetOnlineCommand) (interface{}, error) {
if err := setOnlineCommand.ValidateCommand(); err != nil {
func (productPlanService *ProductPlanService) SetOnline(cmd *command.SetOnlineCommand) (interface{}, error) {
if err := cmd.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
... ... @@ -277,10 +290,45 @@ func (productPlanService *ProductPlanService) SetOnline(setOnlineCommand *comman
defer func() {
transactionContext.RollbackTransaction()
}()
var productPlanRepository domain.ProductPlanRepository
var productPlan *domain.ProductPlan
productPlanRepository, productPlan, err = factory.FastPgProductPlan(transactionContext, cmd.ProductPlanId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err = productPlan.ChangeStatus(domain.PlanOnline); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var workshop *domain.Workshop
_, workshop, err = factory.FastPgWorkshop(transactionContext, cmd.WorkshopId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
line, section, err := workshop.FindSectionById(cmd.SectionId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var workStation *domain.WorkStation
workStation, err = workshop.FindWorkStation(workshop.WorkshopId, line.LineId, section.SectionId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
productPlan.WorkStation = workStation
if productPlan, err = productPlanRepository.Save(productPlan); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
return productPlan, nil
}
// 提交成品记录 (成品 二级品)
... ... @@ -326,8 +374,8 @@ func (productPlanService *ProductPlanService) Exchange(switchCommand *command.Sw
}
// 更新生产计划服务
func (productPlanService *ProductPlanService) UpdateProductPlan(updateProductPlanCommand *command.UpdateProductPlanCommand) (interface{}, error) {
if err := updateProductPlanCommand.ValidateCommand(); err != nil {
func (productPlanService *ProductPlanService) UpdateProductPlan(cmd *command.UpdateProductPlanCommand) (interface{}, error) {
if err := cmd.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
... ... @@ -341,31 +389,38 @@ func (productPlanService *ProductPlanService) UpdateProductPlan(updateProductPla
transactionContext.RollbackTransaction()
}()
var productPlanRepository domain.ProductPlanRepository
if value, err := factory.CreateProductPlanRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
var productPlan *domain.ProductPlan
productPlanRepository, productPlan, err = factory.FastPgProductPlan(transactionContext, cmd.ProductPlanId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
productPlanRepository = value
}
productPlan, err := productPlanRepository.FindOne(map[string]interface{}{"productPlanId": updateProductPlanCommand.ProductPlanId})
//if productPlan.Workshop.WorkshopId != cmd.WorkshopId{
// // 检查批次号是否有重复的
// if item, err := productPlanRepository.FindOne(map[string]interface{}{"companyId": cmd.CompanyId, "orgId": cmd.OrgId, "batchNumber": cmd.BatchNumber}); err == nil && item != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "批次号重复")
// }
//}
_, workshop, err := factory.FastPgWorkshop(transactionContext, cmd.WorkshopId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if productPlan == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateProductPlanCommand.ProductPlanId)))
}
if err := productPlan.Update(tool_funs.SimpleStructToMap(updateProductPlanCommand)); err != nil {
productPlan.Workshop = workshop.CloneSample()
if err := productPlan.Update(tool_funs.SimpleStructToMap(cmd)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if productPlan, err := productPlanRepository.Save(productPlan); err != nil {
if productPlan, err = productPlanRepository.Save(productPlan); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return productPlan, nil
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
result := &dto.ProductPlanDto{}
return result.LoadDto(productPlan, cmd.OrgId), nil
}
// 搜索生产计划服务列表
... ...
... ... @@ -326,6 +326,38 @@ func (workshopService *WorkshopService) SelectorWorkshop(operateInfo *domain.Ope
}
// 车间工段列表
func (workshopService *WorkshopService) SelectorWorkshopSection(operateInfo *domain.OperateInfo, getWorkshopQuery *query.GetWorkshopQuery) (interface{}, error) {
if err := getWorkshopQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, 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()
}()
var workshop *domain.Workshop
_, workshop, err = factory.FastPgWorkshop(transactionContext, getWorkshopQuery.WorkshopId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
sections := workshop.AllProductSections(domain.All)
return map[string]interface{}{
"count": len(sections),
"sections": sections,
}, nil
}
func NewWorkshopService(options map[string]interface{}) *WorkshopService {
newWorkshopService := &WorkshopService{}
return newWorkshopService
... ...
package domain
import "fmt"
// 生产线
type ProductLine struct {
// 生产线ID
... ... @@ -12,6 +14,17 @@ type ProductLine struct {
Removed int `json:"removed,omitempty"`
}
// 查询生产线
func (productLine *ProductLine) FindSection(sectionId int) (*ProductSection, error) {
for i := range productLine.ProductSections {
item := productLine.ProductSections[i]
if item.SectionId == sectionId {
return item, nil
}
}
return nil, fmt.Errorf("工段不存在")
}
func (productLine *ProductLine) GetProductSections(removed int) []*ProductSection {
var result = make([]*ProductSection, 0)
for i := range productLine.ProductSections {
... ...
package domain
import "time"
import (
"errors"
"time"
)
const (
PlanOnline = 1 // 计划上线
... ... @@ -60,119 +63,47 @@ func (productPlan *ProductPlan) Identify() interface{} {
}
func (productPlan *ProductPlan) Update(data map[string]interface{}) error {
//if productPlanId, ok := data["productPlanId"]; ok {
// productPlan.ProductPlanId = productPlanId.(int)
//}
//if companyId, ok := data["companyId"]; ok {
// productPlan.CompanyId = companyId.(int)
//}
//if orgId, ok := data["orgId"]; ok {
// productPlan.OrgId = orgId.(int)
//}
//if batchNumber, ok := data["batchNumber"]; ok {
// productPlan.BatchNumber = batchNumber.(string)
//}
//if productDate, ok := data["productDate"]; ok {
// productPlan.ProductDate = productDate.(time.Time)
//}
//if companyId, ok := data["companyId"]; ok {
// productPlan.Workshop.CompanyId = companyId.(int)
//}
//if orgId, ok := data["orgId"]; ok {
// productPlan.Workshop.OrgId = orgId.(int)
//}
//if workshopId, ok := data["workshopId"]; ok {
// productPlan.Workshop.WorkshopId = workshopId.(int)
//}
//if workshopName, ok := data["workshopName"]; ok {
// productPlan.Workshop.WorkshopName = workshopName.(string)
//}
//if userId, ok := data["userId"]; ok {
// productPlan.Workshop.Principal.UserId = userId.(int)
//}
//if userName, ok := data["userName"]; ok {
// productPlan.Workshop.Principal.UserName = userName.(string)
//}
//if employeeType, ok := data["employeeType"]; ok {
// productPlan.Workshop.Principal.EmployeeType = employeeType.(int)
//}
//if icCardNumber, ok := data["icCardNumber"]; ok {
// productPlan.Workshop.Principal.IcCardNumber = icCardNumber.(string)
//}
//if avatar, ok := data["avatar"]; ok {
// productPlan.Workshop.Principal.Avatar = avatar.(string)
//}
//if phone, ok := data["phone"]; ok {
// productPlan.Workshop.Principal.Phone = phone.(string)
//}
//if productLines, ok := data["productLines"]; ok {
// productPlan.Workshop.ProductLines = productLines.(array)
//}
//if createdAt, ok := data["createdAt"]; ok {
// productPlan.Workshop.CreatedAt = createdAt.(time.Time)
//}
//if updatedAt, ok := data["updatedAt"]; ok {
// productPlan.Workshop.UpdatedAt = updatedAt.(time.Time)
//}
//if deletedAt, ok := data["deletedAt"]; ok {
// productPlan.Workshop.DeletedAt = deletedAt.(time.Time)
//}
//if workOn, ok := data["workOn"]; ok {
// productPlan.WorkOn = workOn.(int)
//}
//if machine, ok := data["machine"]; ok {
// productPlan.Machine = machine.(string)
//}
//if planProductName, ok := data["planProductName"]; ok {
// productPlan.PlanProductName = planProductName.(string)
//}
//if quantity, ok := data["quantity"]; ok {
// productPlan.PlanDevoted.Quantity = quantity.(float64)
//}
//if unit, ok := data["unit"]; ok {
// productPlan.PlanDevoted.Unit = unit.(string)
//}
//if unitWeight, ok := data["unitWeight"]; ok {
// productPlan.PlanDevoted.UnitWeight = unitWeight.(float64)
//}
//if weight, ok := data["weight"]; ok {
// productPlan.PlanDevoted.Weight = weight.(float64)
//}
//if planStatus, ok := data["planStatus"]; ok {
// productPlan.PlanStatus = planStatus.(int)
//}
//if workStationId, ok := data["workStationId"]; ok {
// productPlan.WorkStation.WorkStationId = workStationId.(string)
//}
//if workshopId, ok := data["workshopId"]; ok {
// productPlan.WorkStation.WorkshopId = workshopId.(int)
//}
//if workshopName, ok := data["workshopName"]; ok {
// productPlan.WorkStation.WorkshopName = workshopName.(string)
//}
//if lineId, ok := data["lineId"]; ok {
// productPlan.WorkStation.LineId = lineId.(int)
//}
//if lineName, ok := data["lineName"]; ok {
// productPlan.WorkStation.LineName = lineName.(string)
//}
//if sectionId, ok := data["sectionId"]; ok {
// productPlan.WorkStation.SectionId = sectionId.(int)
//}
//if sectionName, ok := data["sectionName"]; ok {
// productPlan.WorkStation.SectionName = sectionName.(string)
//}
//if remark, ok := data["remark"]; ok {
// productPlan.Remark = remark.(string)
//}
//if createdAt, ok := data["createdAt"]; ok {
// productPlan.CreatedAt = createdAt.(time.Time)
//}
//if updatedAt, ok := data["updatedAt"]; ok {
// productPlan.UpdatedAt = updatedAt.(time.Time)
//}
//if deletedAt, ok := data["deletedAt"]; ok {
// productPlan.DeletedAt = deletedAt.(time.Time)
//}
if productDate, ok := data["productDateTime"]; ok {
productPlan.ProductDate = productDate.(time.Time)
}
if workOn, ok := data["workOn"]; ok {
productPlan.WorkOn = workOn.(int)
}
if machine, ok := data["machine"]; ok {
productPlan.Machine = machine.(string)
}
if planProductName, ok := data["planProductName"]; ok {
productPlan.PlanProductName = planProductName.(string)
}
if quantity, ok := data["quantity"]; ok {
productPlan.PlanDevoted.Quantity = quantity.(float64)
}
if unit, ok := data["unit"]; ok {
productPlan.PlanDevoted.Unit = unit.(string)
}
if weight, ok := data["weight"]; ok {
productPlan.PlanDevoted.Weight = weight.(float64)
}
if remark, ok := data["remark"]; ok {
productPlan.Remark = remark.(string)
}
productPlan.UpdatedAt = time.Now()
return nil
}
func (productPlan *ProductPlan) ChangeStatus(status int) error {
if productPlan.PlanStatus == status && productPlan.PlanStatus == PlanOnline {
return errors.New("计划已经上线")
}
if productPlan.PlanStatus == status && productPlan.PlanStatus == PlanOffline {
return errors.New("计划已经下线")
}
if !(productPlan.PlanStatus == PlanOnline || productPlan.PlanStatus == PlanOffline) {
return errors.New("计划状态有误")
}
productPlan.PlanStatus = status
return nil
}
... ...
... ... @@ -9,3 +9,16 @@ type ProductSection struct {
// 已删除标识 1:正常 2:已删除
Removed int `json:"removed,omitempty"`
}
func (m *ProductSection) CloneSample() *ProductSection {
return &ProductSection{
SectionId: m.SectionId,
SectionName: m.SectionName,
}
}
type ProductSections []*ProductSection
func (ms ProductSections) Len() int { return len(ms) }
func (ms ProductSections) Less(i, j int) bool { return ms[i].SectionId < ms[j].SectionId }
func (ms ProductSections) Swap(i, j int) { ms[i], ms[j] = ms[j], ms[i] }
... ...
... ... @@ -31,6 +31,6 @@ func NewWorkStation(w *Workshop, l *ProductLine, s *ProductSection) *WorkStation
LineName: l.LineName,
SectionId: s.SectionId,
SectionName: s.SectionName,
Principal: w.Principal,
//Principal: w.Principal,
}
}
... ...
... ... @@ -2,10 +2,13 @@ package domain
import (
"fmt"
"sort"
"time"
)
const (
// 所有
All = 0
// 未删除
NotDeleted = 1
// 已删除
... ... @@ -220,6 +223,36 @@ func (workshop *Workshop) GetProductLines(removed int) []*ProductLine {
return result
}
func (workshop *Workshop) AllProductSections(removed int) []*ProductSection {
var result ProductSections = make([]*ProductSection, 0)
lines := workshop.GetProductLines(removed)
for i := range lines {
for j := range lines[i].ProductSections {
section := lines[i].ProductSections[j]
if removed > 0 && section.Removed != removed {
continue
}
result = append(result, section.CloneSample())
}
}
sort.Stable(result)
return result
}
// 查询生产线
func (workshop *Workshop) FindSectionById(sectionId int) (*ProductLine, *ProductSection, error) {
lines := workshop.GetProductLines(0)
for k := range lines {
line := lines[k]
section, _ := line.FindSection(sectionId)
if section != nil {
return line, section, nil
}
}
return nil, nil, fmt.Errorf("工段不存在")
}
func (workshop *Workshop) CloneSample() *Workshop {
return &Workshop{
WorkshopId: workshop.WorkshopId,
... ...
... ... @@ -172,6 +172,17 @@ func (repository *ProductPlanRepository) Find(queryOptions map[string]interface{
var productPlanModels []*models.ProductPlan
productPlans := make([]*domain.ProductPlan, 0)
query := sqlbuilder.BuildQuery(tx.Model(&productPlanModels), queryOptions)
query.SetWhereByQueryOption("company_id = ?", "companyId")
query.SetWhereByQueryOption("org_id = ?", "orgId")
query.SetWhereByQueryOption("batch_number=?", "batchNumber")
if v, ok := queryOptions["batchNumber"]; ok && len(v.(string)) > 0 {
query.Where(fmt.Sprintf(`batch_number like '%%%v%%'`, v))
}
if v, ok := queryOptions["workshopName"]; ok && len(v.(string)) > 0 {
query.Where(fmt.Sprintf(`workshop->>'workshopName' like '%%%v%%'`, v))
}
query.SetWhereByQueryOption("plan_status=?", "planStatus")
query.SetOffsetAndLimit(20)
query.SetOrderDirect("product_plan_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
... ...
... ... @@ -86,6 +86,14 @@ func (controller *ProductPlanController) SetOnline() {
controller.Response(data, err)
}
func (controller *ProductPlanController) SetOffline() {
productPlanService := service.NewProductPlanService(nil)
setOnlineCommand := &command.SetOfflineCommand{}
Must(controller.Unmarshal(setOnlineCommand))
data, err := productPlanService.SetOffline(setOnlineCommand)
controller.Response(data, err)
}
func (controller *ProductPlanController) Switch() {
productPlanService := service.NewProductPlanService(nil)
switchCommand := &command.SwitchCommand{}
... ...
... ... @@ -74,3 +74,11 @@ func (controller *WorkshopController) SelectorWorkshop() {
data, err := workshopService.SearchWorkshop(ParseOperateInfo(controller.BaseController), listWorkshopQuery)
controller.Response(data, err)
}
func (controller *WorkshopController) SelectorWorkshopSection() {
workshopService := service.NewWorkshopService(nil)
getWorkshopQuery := &query.GetWorkshopQuery{}
Must(controller.Unmarshal(getWorkshopQuery))
data, err := workshopService.SelectorWorkshopSection(ParseOperateInfo(controller.BaseController), getWorkshopQuery)
controller.Response(data, err)
}
... ...
... ... @@ -14,6 +14,7 @@ func init() {
web.Router("/product-plans/receive-material", &controllers.ProductPlanController{}, "Post:ReceiveMaterial")
web.Router("/product-plans/return-material", &controllers.ProductPlanController{}, "Post:ReturnMaterial")
web.Router("/product-plans/set-online", &controllers.ProductPlanController{}, "Post:SetOnline")
web.Router("/product-plans/set-offline", &controllers.ProductPlanController{}, "Post:SetOffline")
web.Router("/product-plans/switch", &controllers.ProductPlanController{}, "Post:Switch")
web.Router("/product-plans/submit-product-record", &controllers.ProductPlanController{}, "Post:SubmitProductRecord")
web.Router("/product-plans/search", &controllers.ProductPlanController{}, "Post:SearchProductPlan")
... ...
... ... @@ -13,4 +13,5 @@ func init() {
web.Router("/workshops/", &controllers.WorkshopController{}, "Get:ListWorkshop")
web.Router("/workshops/search", &controllers.WorkshopController{}, "Post:SearchWorkshop")
web.Router("/workshops/selector", &controllers.WorkshopController{}, "Post:SelectorWorkshop")
web.Router("/workshops/sections-selector", &controllers.WorkshopController{}, "Post:SelectorWorkshopSection")
}
... ...