作者 yangfu

refactor: 计划管理、生产班组、物料

package dto
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"strings"
)
// 生产班组员工
type ProductGroupEmployeesDto struct {
// 用户Id 用户唯一标识
UserId int `json:"userId,omitempty"`
// 用户姓名
UserName string `json:"userName,omitempty"`
// 班组名称
GroupName string `json:"groupName,omitempty"`
// 上班班次 1:全天 2:白班 4:中班 8:夜班
WorkOnDescription string `json:"workOn,omitempty"`
// 工作位置键值 (车间ID+'.'+生产线ID+'.'+工段ID)
//WorkStationId string `json:"workStationId,omitempty"`
}
func NewProductGroupEmployeesDto(group *domain.ProductGroup) []*ProductGroupEmployeesDto {
employees := make([]*ProductGroupEmployeesDto, 0)
if group.GroupLeader != nil {
employees = append(employees, NewGroupEmployee(group, group.GroupLeader))
}
for i := range group.GroupMembers {
employees = append(employees, NewGroupEmployee(group, group.GroupMembers[i]))
}
return employees
}
func NewGroupEmployee(group *domain.ProductGroup, u *domain.User) *ProductGroupEmployeesDto {
item := &ProductGroupEmployeesDto{}
item.UserId = u.UserId
item.UserName = u.UserName
item.GroupName = group.GroupName
workOns := domain.WorkOnDescription(group.WorkOn)
item.WorkOnDescription = strings.Join(workOns, ",")
//item.WorkStationId = group.WorkStation.WorkStationId
return item
}
type ProductGroupEmployeesDtos struct {
Result []*ProductGroupEmployeesDto
MapResult map[int]*ProductGroupEmployeesDto
}
func (d *ProductGroupEmployeesDtos) Append(items ...*ProductGroupEmployeesDto) {
for i := range items {
if _, ok := d.MapResult[items[i].UserId]; ok {
continue
}
d.MapResult[items[i].UserId] = items[i]
d.Result = append(d.Result, items[i])
}
}
func (d *ProductGroupEmployeesDtos) LoadDto(groups ...*domain.ProductGroup) {
for i := range groups {
items := NewProductGroupEmployeesDto(groups[i])
d.Append(items...)
}
}
func NewProductGroupEmployeesDtos() *ProductGroupEmployeesDtos {
return &ProductGroupEmployeesDtos{
Result: make([]*ProductGroupEmployeesDto, 0),
MapResult: make(map[int]*ProductGroupEmployeesDto),
}
}
... ...
package query
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
"reflect"
"strings"
)
type SearchProductGroupEmployeesQuery struct {
// 查询偏离量
//Offset int `cname:"查询偏离量" json:"offset"`
// 查询限制
//Limit int `cname:"查询限制" json:"limit"`
// 页码
//PageNumber int `cname:"页码" json:"pageNumber,omitempty"`
// 页数
//PageSize int `cname:"页数" json:"pageSize,omitempty"`
// 当前公司
CompanyId int `cname:"当前公司" json:"companyId,omitempty" valid:"Required"`
// 当前登录的组织
OrgId int `cname:"当前登录的组织" json:"orgId,omitempty"`
// 车间ID
WorkshopId int `cname:"车间ID" json: workshopId,omitempty"`
// 生产线ID
LineId int `cname:"生产线ID" json:"lineId,omitempty"`
// 工段ID
SectionId int `json:"sectionId,omitempty"`
}
func (listProductGroupQuery *SearchProductGroupEmployeesQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (listProductGroupQuery *SearchProductGroupEmployeesQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(listProductGroupQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(listProductGroupQuery).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
... ... @@ -367,6 +367,48 @@ func (productGroupService *ProductGroupService) SearchProductGroup(operateInfo *
return count, results, nil
}
// 返回生产班组服务列表
func (productGroupService *ProductGroupService) SearchProductGroupEmployees(operateInfo *domain.OperateInfo, cmd *query.SearchProductGroupEmployeesQuery) (int64, interface{}, error) {
if err := cmd.ValidateQuery(); err != nil {
return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
//workshops, _ := factory.FastPgWorkshops(transactionContext, operateInfo.CompanyId)
queryOptions := utils.ObjectToMap(cmd)
//queryOptions = workshops.FindByNameWithQuery(queryOptions, cmd.WorkshopName, cmd.LineName, "")
var productGroupRepository domain.ProductGroupRepository
productGroupRepository, _, _ = factory.FastPgProductGroup(transactionContext, 0)
_, productGroups, err := productGroupRepository.Find(queryOptions)
if err != nil {
return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var results = dto.NewProductGroupEmployeesDtos()
for i := range productGroups {
item := productGroups[i]
//workStation := workshops.FindWorkStation(item.WorkStation.WorkshopId, item.WorkStation.LineId, item.WorkStation.SectionId)
//item.WorkStation = workStation
items := dto.NewProductGroupEmployeesDto(item)
results.Append(items...)
}
if err := transactionContext.CommitTransaction(); err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return int64(len(results.Result)), map[string]interface{}{
"employees": results.Result,
}, nil
}
func NewProductGroupService(options map[string]interface{}) *ProductGroupService {
newProductGroupService := &ProductGroupService{}
return newProductGroupService
... ...
... ... @@ -19,7 +19,7 @@ type SearchProductPlanQuery struct {
// 当前登录的组织
OrgId int `cname:"当前登录的组织" json:"orgId,omitempty"`
// 匹配多个组织
InOrgIds []int `cname:"匹配多个组织" json:"inOrgIds,omitempty" valid:"Required"`
InOrgIds []int `cname:"匹配多个组织" json:"inOrgIds,omitempty"`
// 页码
PageNumber int `cname:"页码" json:"pageNumber,omitempty"`
// 页数
... ...
package dto
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
type MaterialUnitConversionDto struct {
// 单位换算ID
UnitConversionId int `json:"unitConversionId,omitempty"`
// 物料名称
MaterialName string `json:"materialName,omitempty"`
// 转换前单位数量
FromUnitQuantity *domain.UnitQuantity `json:"fromUnitQuantity,omitempty"`
// 转换后单位数量
ToUnitQuantity *domain.UnitQuantity `json:"toUnitQuantity,omitempty"`
}
func (d *MaterialUnitConversionDto) LoadDto(m *domain.UnitConversion) *MaterialUnitConversionDto {
d.UnitConversionId = m.UnitConversionId
d.MaterialName = m.Material.MaterialName
d.FromUnitQuantity = m.FromUnitQuantity
d.ToUnitQuantity = m.ToUnitQuantity
return d
}
... ...
package query
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
"reflect"
"strings"
)
type SearchMaterialsQuery struct {
// 当前公司
CompanyId int `cname:"当前公司" json:"companyId,omitempty" valid:"Required"`
// 当前登录的组织
OrgId int `cname:"当前登录的组织" json:"orgId,omitempty"`
// 车间ID
WorkshopId int `cname:"车间ID" json: workshopId,omitempty"`
// 生产线ID
LineId int `cname:"生产线ID" json:"lineId,omitempty"`
// 工段ID
SectionId int `json:"sectionId,omitempty"`
}
func (listProductGroupQuery *SearchMaterialsQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (listProductGroupQuery *SearchMaterialsQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(listProductGroupQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(listProductGroupQuery).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
... ... @@ -307,6 +307,42 @@ func (unitConversionService *UnitConversionService) SearchUnitConversion(operate
return count, result, nil
}
// 返回单位换算服务列表
func (unitConversionService *UnitConversionService) SearchMaterial(operateInfo *domain.OperateInfo, listUnitConversionQuery *query.SearchMaterialsQuery) (int64, interface{}, error) {
if err := listUnitConversionQuery.ValidateQuery(); err != nil {
return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
unitConversionRepository, _, _ := factory.FastPgUnitConversion(transactionContext, 0)
count, unitConversions, err := unitConversionRepository.Find(utils.ObjectToMap(listUnitConversionQuery))
if err != nil {
return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
var result = make([]*dto.MaterialUnitConversionDto, 0)
for i := range unitConversions {
item := unitConversions[i]
newItem := &dto.MaterialUnitConversionDto{}
newItem.LoadDto(item)
result = append(result, newItem)
}
return count, map[string]interface{}{
"materials": result,
}, nil
}
func NewUnitConversionService(options map[string]interface{}) *UnitConversionService {
newUnitConversionService := &UnitConversionService{}
return newUnitConversionService
... ...
... ... @@ -166,7 +166,10 @@ func (repository *ProductGroupRepository) Find(queryOptions map[string]interface
if v, ok := queryOptions["groupName"]; ok && len(v.(string)) > 0 {
query.Where(fmt.Sprintf(`group_name like '%%%v%%'`, v))
}
query.SetOffsetAndLimit(20)
query.SetWhereByQueryOption("work_station->>'workshopId'='?'", "workshopId")
query.SetWhereByQueryOption("work_station->>'lineId'='?'", "lineId")
query.SetWhereByQueryOption("work_station->>'sectionId'='?'", "sectionId")
query.SetOffsetAndLimit(domain.MaxQueryRow)
query.SetOrderDirect("product_group_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, productGroups, err
... ...
... ... @@ -183,7 +183,7 @@ func (repository *ProductPlanRepository) Find(queryOptions map[string]interface{
query.Where(fmt.Sprintf(`workshop->>'workshopName' like '%%%v%%'`, v))
}
query.SetWhereByQueryOption("plan_status=?", "planStatus")
query.SetOffsetAndLimit(20)
query.SetOffsetAndLimit(domain.MaxQueryRow)
query.SetOrderDirect("product_plan_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, productPlans, err
... ...
... ... @@ -149,7 +149,7 @@ func (repository *ProductRecordRepository) Find(queryOptions map[string]interfac
var productRecordModels []*models.ProductRecord
productRecords := make([]*domain.ProductRecord, 0)
query := sqlbuilder.BuildQuery(tx.Model(&productRecordModels), queryOptions)
query.SetOffsetAndLimit(20)
query.SetOffsetAndLimit(domain.MaxQueryRow)
query.SetOrderDirect("product_record_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, productRecords, err
... ...
... ... @@ -157,7 +157,7 @@ func (repository *ProductRepository) Find(queryOptions map[string]interface{}) (
if v, ok := queryOptions["productCategory"]; ok && len(v.(string)) > 0 {
query.Where(fmt.Sprintf(`product_category like '%%%v%%'`, v))
}
query.SetOffsetAndLimit(20)
query.SetOffsetAndLimit(domain.MaxQueryRow)
query.SetOrderDirect("product_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, products, err
... ...
... ... @@ -146,6 +146,9 @@ func (repository *UnitConversionRepository) Find(queryOptions map[string]interfa
query := sqlbuilder.BuildQuery(tx.Model(&unitConversionModels), queryOptions)
query.SetWhereByQueryOption("company_id = ?", "companyId")
query.SetWhereByQueryOption("org_id = ?", "orgId")
query.SetWhereByQueryOption("work_station->>'workshopId'='?'", "workshopId")
query.SetWhereByQueryOption("work_station->>'lineId'='?'", "lineId")
query.SetWhereByQueryOption("work_station->>'sectionId'='?'", "sectionId")
if v, ok := queryOptions["inOrgIds"]; ok && len(v.([]int)) > 0 {
query.Where(`org_id in (?)`, pg.In(v))
}
... ...
... ... @@ -78,3 +78,14 @@ func (controller *ProductGroupController) SearchProductGroup() {
total, data, err := productGroupService.SearchProductGroup(ParseOperateInfo(controller.BaseController), cmd)
ResponseGrid(controller.BaseController, total, data, err)
}
func (controller *ProductGroupController) SearchProductGroupEmployees() {
productGroupService := service.NewProductGroupService(nil)
cmd := &query.SearchProductGroupEmployeesQuery{}
Must(controller.Unmarshal(cmd))
operateInfo := ParseOperateInfo(controller.BaseController)
cmd.OrgId = operateInfo.OrgId
cmd.CompanyId = operateInfo.CompanyId
_, data, err := productGroupService.SearchProductGroupEmployees(operateInfo, cmd)
controller.Response(data, err)
}
... ...
... ... @@ -78,3 +78,14 @@ func (controller *UnitConversionController) SearchUnitConversion() {
total, data, err := unitConversionService.SearchUnitConversion(ParseOperateInfo(controller.BaseController), cmd)
ResponseGrid(controller.BaseController, total, data, err)
}
func (controller *UnitConversionController) SearchMaterial() {
unitConversionService := service.NewUnitConversionService(nil)
cmd := &query.SearchMaterialsQuery{}
Must(controller.Unmarshal(cmd))
operateInfo := ParseOperateInfo(controller.BaseController)
cmd.OrgId = operateInfo.OrgId
cmd.CompanyId = operateInfo.CompanyId
_, data, err := unitConversionService.SearchMaterial(ParseOperateInfo(controller.BaseController), cmd)
controller.Response(data, err)
}
... ...
... ... @@ -13,4 +13,5 @@ func init() {
web.Router("/product-groups/batch-remove", &controllers.ProductGroupController{}, "Post:BatchRemoveProductGroup")
web.Router("/product-groups/", &controllers.ProductGroupController{}, "Get:ListProductGroup")
web.Router("/product-groups/search", &controllers.ProductGroupController{}, "Post:SearchProductGroup")
web.Router("/product-groups/employees", &controllers.ProductGroupController{}, "Post:SearchProductGroupEmployees")
}
... ...
... ... @@ -13,4 +13,6 @@ func init() {
web.Router("/unit-conversions/batch-remove", &controllers.UnitConversionController{}, "Post:BatchRemoveUnitConversion")
web.Router("/unit-conversions/", &controllers.UnitConversionController{}, "Get:ListUnitConversion")
web.Router("/unit-conversions/search", &controllers.UnitConversionController{}, "Post:SearchUnitConversion")
web.Router("/unit-conversions/materials", &controllers.UnitConversionController{}, "Post:SearchMaterial")
}
... ...