作者 yangfu

feat: 增加物料分组编码自动生成

package dto
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
)
type MaterialGroupDto struct {
// 物料分组ID
... ... @@ -26,3 +29,30 @@ func (d *MaterialGroupDto) LoadDto(m *domain.ProductMaterialGroup, orgId int) *M
d.MaterialGroupNumber = m.MaterialGroupNumber
return d
}
func (productMaterialGroup *MaterialGroupDto) PID() string {
return fmt.Sprintf("%d", productMaterialGroup.Pid)
}
func (productMaterialGroup *MaterialGroupDto) ID() string {
return fmt.Sprintf("%d", productMaterialGroup.ProductMaterialGroupId)
}
type MaterialGroupDtos []*MaterialGroupDto
func (tree MaterialGroupDtos) Len() int {
return len(tree)
}
func (tree MaterialGroupDtos) Less(i, j int) bool {
if tree[i].Pid < tree[j].Pid {
return true
}
if tree[i].Pid == tree[j].Pid {
return tree[i].ProductMaterialGroupId < tree[j].ProductMaterialGroupId
}
return false
}
func (tree MaterialGroupDtos) Swap(i, j int) {
tree[i], tree[j] = tree[j], tree[i]
}
... ...
package query
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type SelectorProductMaterialGroupQuery struct {
// 查询偏离量
//Offset int `cname:"查询偏离量" json:"offset"`
// 查询限制
//Limit int `cname:"查询限制" json:"limit"`
// options 'tree' 'list' default tree
Style string `json:"style"`
// 当前公司
CompanyId int `cname:"当前公司" json:"companyId,omitempty" valid:"Required"`
// 页码
//PageNumber int `cname:"页码" json:"pageNumber,omitempty"`
// 页数
//PageSize int `cname:"页数" json:"pageSize,omitempty"`
}
func (cmd *SelectorProductMaterialGroupQuery) Valid(validation *validation.Validation) {
//cmd.Offset, cmd.Limit = domain.Pagination(cmd.PageNumber, cmd.PageSize)
cmd.Style = "tree"
}
func (cmd *SelectorProductMaterialGroupQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(cmd)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(cmd).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
}
... ...
... ... @@ -12,6 +12,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
"sort"
)
// 物料分组服务
... ... @@ -38,6 +39,7 @@ func (productMaterialGroupService *ProductMaterialGroupService) CreateProductMat
MaterialGroupName: createProductMaterialGroupCommand.MaterialGroupName,
MaterialGroupNumber: createProductMaterialGroupCommand.MaterialGroupNumber,
}
var productMaterialGroup *domain.ProductMaterialGroup
materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext))
if productMaterialGroup, err = materialService.AddMaterialGroup(operateInfo, newProductMaterialGroup); err != nil {
... ... @@ -185,7 +187,7 @@ func (productMaterialGroupService *ProductMaterialGroupService) UpdateProductMat
productMaterialGroup := &domain.ProductMaterialGroup{
ProductMaterialGroupId: cmd.ProductMaterialGroupId,
MaterialGroupName: cmd.MaterialGroupName,
MaterialGroupNumber: cmd.MaterialGroupNumber,
//MaterialGroupNumber: cmd.MaterialGroupNumber,
}
materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext))
if productMaterialGroup, err := materialService.UpdateMaterialGroup(operateInfo, productMaterialGroup); err != nil {
... ... @@ -233,6 +235,48 @@ func (productMaterialGroupService *ProductMaterialGroupService) SearchProductMat
return count, results, nil
}
func (productMaterialGroupService *ProductMaterialGroupService) SelectorProductMaterialGroup(operateInfo *domain.OperateInfo, cmd *query.SelectorProductMaterialGroupQuery) (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()
}()
var productMaterialGroupRepository domain.ProductMaterialGroupRepository
productMaterialGroupRepository, _, _ = factory.FastProductMaterialGroup(transactionContext, 0)
queryOptions := utils.ObjectToMap(cmd)
count, productGroups, err := productMaterialGroupRepository.Find(queryOptions)
if err != nil {
return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var results = make([]*dto.MaterialGroupDto, 0)
for i := range productGroups {
newItem := &dto.MaterialGroupDto{}
newItem.LoadDto(productGroups[i], operateInfo.OrgId)
results = append(results, newItem)
}
if err := transactionContext.CommitTransaction(); err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if cmd.Style == "tree" {
sort.Stable(dto.MaterialGroupDtos(results))
nodes := make([]utils.TreeNode, 0)
for i := range results {
nodes = append(nodes, results[i])
}
tree := utils.NewTree(nodes)
return count, tree.Nodes, nil
}
return count, results, nil
}
func NewProductMaterialGroupService(options map[string]interface{}) *ProductMaterialGroupService {
newProductMaterialGroupService := &ProductMaterialGroupService{}
return newProductMaterialGroupService
... ...
... ... @@ -2,6 +2,7 @@ package domain
import (
"fmt"
"strconv"
"time"
)
... ... @@ -49,6 +50,27 @@ func (productMaterialGroup *ProductMaterialGroup) Update(data map[string]interfa
return nil
}
func (productMaterialGroup *ProductMaterialGroup) GroupNumberComposeUp(parent *ProductMaterialGroup, latestNumber string, total int) string {
iNumber, err := strconv.ParseInt(latestNumber, 10, 64)
if err == nil {
iNumber += 1
return formatFunc(iNumber)
}
if parent != nil {
return parent.MaterialGroupNumber + formatFunc(int64(total+1))
}
return formatFunc(int64(total + 1))
}
func formatFunc(iNumber int64) string {
sNumber := fmt.Sprintf("%d", iNumber)
if len(sNumber)%2 == 1 {
return "0" + sNumber
}
return sNumber
}
/***** 1.实现树 *****/
func (productMaterialGroup *ProductMaterialGroup) PID() string {
... ...
package dao
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
)
type ProductMaterialGroupDao struct {
transactionContext *pgTransaction.TransactionContext
}
func (dao *ProductMaterialGroupDao) CurrentGroupLatestNumber(companyId int, productMaterialGroupId int) (string, int, error) {
m := new(models.ProductMaterialGroup)
var result string
var total int
query := dao.transactionContext.PgTx.Model(m).Where("company_id = ?", companyId)
query.Where("pid = ?", productMaterialGroupId)
query.ColumnExpr("max(material_group_number) number")
query.ColumnExpr("count(material_group_number) total")
query.AllWithDeleted()
err := query.Select(&result, &total)
return result, total, err
}
func NewProductMaterialGroupDao(transactionContext *pgTransaction.TransactionContext) (*ProductMaterialGroupDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &ProductMaterialGroupDao{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -123,10 +123,10 @@ func (ptr *PGCommonStatisticsService) HourProductiveStatistics(queryOptions map[
}
type HourProductiveStatisticsRequest struct {
CompanyId int `json:"companyId" valid:"Required"`
OrgId int `json:"orgId" valid:"Required"`
WorkshopId int `json:"workshopId" valid:"Required"`
Date time.Time `json:"date"`
CompanyId int `json:"companyId" valid:"Required"`
OrgId int `json:"orgId" valid:"Required"`
WorkshopId int `json:"workshopId" valid:"Required"`
Date utils.TimeString `json:"date"`
}
func NewXYData(xData []string, values interface{}) interface{} {
... ... @@ -154,8 +154,8 @@ func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map
Total float64 `json:"total"`
}
var date = time.Now()
if !xtime.IsZero(request.Date) {
date = request.Date
if !xtime.IsZero(time.Time(request.Date)) {
date = time.Time(request.Date)
}
workshop, err := workshopRepository.FindOne(map[string]interface{}{"workshopId": request.WorkshopId})
if err != nil || workshop == nil {
... ... @@ -213,8 +213,8 @@ func (ptr *PGCommonStatisticsService) ProportionOfSecondLevelStatistics(queryOpt
}
productRecordDao, _ := dao.NewProductRecordDao(ptr.transactionContext)
var date = time.Now()
if !xtime.IsZero(request.Date) {
date = request.Date
if !xtime.IsZero(time.Time(request.Date)) {
date = time.Time(request.Date)
}
var input = []struct {
name string
... ... @@ -262,8 +262,8 @@ func (ptr *PGCommonStatisticsService) WorkshopProductionEfficiencyStatistics(que
}
productRecordDao, _ := dao.NewDeviceDailyRunningRecordDao(ptr.transactionContext)
var date = time.Now()
if !xtime.IsZero(request.Date) {
date = request.Date
if !xtime.IsZero(time.Time(request.Date)) {
date = time.Time(request.Date)
}
var input = []struct {
name string
... ...
... ... @@ -4,6 +4,7 @@ import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
"sort"
... ... @@ -66,6 +67,10 @@ func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *do
parent *domain.ProductMaterialGroup
newProductMaterialGroup *domain.ProductMaterialGroup
productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
productMaterialGroupDao *dao.ProductMaterialGroupDao
latestNumber string
total int
)
if user, err = NewUserService().User(opt.UserId); err != nil {
return nil, err
... ... @@ -81,6 +86,12 @@ func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *do
if newProductMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialGroupNumber": item.MaterialGroupNumber}); err == nil || newProductMaterialGroup != nil {
return nil, fmt.Errorf("物料分组编码已存在")
}
productMaterialGroupDao, _ = dao.NewProductMaterialGroupDao(ptr.transactionContext)
latestNumber, total, err = productMaterialGroupDao.CurrentGroupLatestNumber(opt.CompanyId, item.Pid)
if err != nil {
return nil, err
}
newProductMaterialGroup = &domain.ProductMaterialGroup{
CompanyId: opt.CompanyId,
OrgId: opt.OrgId,
... ... @@ -92,16 +103,17 @@ func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *do
UpdatedAt: time.Now(),
Ext: domain.NewExt(org.OrgName).WithOperator(user),
}
newProductMaterialGroup.MaterialGroupNumber = newProductMaterialGroup.GroupNumberComposeUp(parent, latestNumber, total)
newProductMaterialGroup, err = productMaterialGroupRepository.Save(newProductMaterialGroup)
return newProductMaterialGroup, err
}
func (ptr *PGMaterialService) UpdateMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) {
var (
user *domain.User
org *domain.Org
err error
parent *domain.ProductMaterialGroup
user *domain.User
org *domain.Org
err error
//parent *domain.ProductMaterialGroup
productMaterialGroup *domain.ProductMaterialGroup
productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
)
... ... @@ -111,24 +123,25 @@ func (ptr *PGMaterialService) UpdateMaterialGroup(opt *domain.OperateInfo, item
if org, err = NewUserService().Organization(opt.OrgId); err != nil {
return nil, err
}
if item.ProductMaterialGroupId == item.Pid {
return nil, fmt.Errorf("当前物料分组不能做为自己上级")
}
//if item.ProductMaterialGroupId == item.Pid {
// return nil, fmt.Errorf("当前物料分组不能做为自己上级")
//}
if productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.ProductMaterialGroupId}); err != nil || productMaterialGroup == nil {
return nil, fmt.Errorf("物料分组不存在")
}
if item.Pid != productMaterialGroup.ProductMaterialGroupId && item.Pid != 0 {
if parent, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.Pid}); err != nil || parent == nil {
return nil, fmt.Errorf("上级物料分组不存在")
}
}
if productMaterialGroup.MaterialGroupNumber != item.MaterialGroupNumber {
if group, err := productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialGroupNumber": item.MaterialGroupNumber}); err == nil || group != nil {
return nil, fmt.Errorf("物料分组编码已存在")
}
}
productMaterialGroup.MaterialGroupNumber = item.MaterialGroupNumber
//if item.Pid != productMaterialGroup.ProductMaterialGroupId && item.Pid != 0 {
// if parent, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.Pid}); err != nil || parent == nil {
// return nil, fmt.Errorf("上级物料分组不存在")
// }
//}
//if productMaterialGroup.MaterialGroupNumber != item.MaterialGroupNumber {
// if group, err := productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialGroupNumber": item.MaterialGroupNumber}); err == nil || group != nil {
// return nil, fmt.Errorf("物料分组编码已存在")
// }
//}
//productMaterialGroup.MaterialGroupNumber = item.MaterialGroupNumber
productMaterialGroup.MaterialGroupName = item.MaterialGroupName
productMaterialGroup.UpdatedAt = item.UpdatedAt
productMaterialGroup.Ext = domain.NewExt(org.OrgName).WithOperator(user)
... ...
package utils
import (
"github.com/linmadan/egglib-go/utils/xtime"
"strconv"
"time"
)
... ... @@ -103,3 +104,32 @@ func TimeBeforeEqual(t1, t2 time.Time) bool {
}
return false
}
type TimeString time.Time
func (timeString *TimeString) UnmarshalJSON(data []byte) error {
if len(data) == 2 {
return nil
}
t, err := xtime.Parse(string(data[1 : len(data)-1]))
if err != nil {
return err
}
*timeString = TimeString(t)
return nil
}
func (timeString TimeString) MarshalJSON() ([]byte, error) {
if xtime.IsZero(time.Time(timeString)) {
return []byte(`""`), nil
}
b := make([]byte, 0)
b = append(b, '"')
b = time.Time(timeString).AppendFormat(b, time.RFC3339)
b = append(b, '"')
return b, nil
}
func (timeString TimeString) Time() time.Time {
return time.Time(timeString)
}
... ...
... ... @@ -78,14 +78,10 @@ func (controller *ProductMaterialGroupController) SearchProductMaterialGroup() {
func (controller *ProductMaterialGroupController) SelectorProductMaterialGroup() {
productMaterialGroupService := service.NewProductMaterialGroupService(nil)
cmd := &query.SearchProductMaterialGroupQuery{}
offset, _ := controller.GetInt("offset")
cmd.Offset = offset
limit, _ := controller.GetInt("limit")
cmd.Limit = limit
cmd := &query.SelectorProductMaterialGroupQuery{}
operateInfo := ParseOperateInfo(controller.BaseController)
cmd.CompanyId = operateInfo.CompanyId
count, data, err := productMaterialGroupService.SearchProductMaterialGroup(ParseOperateInfo(controller.BaseController), cmd)
count, data, err := productMaterialGroupService.SelectorProductMaterialGroup(ParseOperateInfo(controller.BaseController), cmd)
controller.Response(map[string]interface{}{
"groups": data,
"count": count,
... ...