sun-pc 2 months ago
parent
commit
38a2a3b14f
5 changed files with 276 additions and 106 deletions
  1. 193 5
      boxcost/api/stages.go
  2. 0 89
      boxcost/db/model/pack.go
  3. 83 0
      boxcost/db/model/plan.go
  4. 0 4
      boxcost/go.mod
  5. 0 8
      boxcost/go.sum

+ 193 - 5
boxcost/api/stages.go

@@ -1,23 +1,211 @@
 package api
 
 import (
+	"box-cost/db/model"
+	"box-cost/db/repo"
+	"context"
+
 	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+	"go.mongodb.org/mongo-driver/mongo"
+	"go.mongodb.org/mongo-driver/mongo/options"
 )
 
+// StageRequest 用于添加和更新stage的请求结构
+type StageRequest struct {
+	PlanId  string                `json:"planId"`
+	PackId  string                `json:"packId"`
+	CompId  string                `json:"compId"`
+	StageId string                `json:"stageId,omitempty"`
+	Stage   *model.ComponentStage `json:"stage"`
+}
+
+// 更新计划和部件的价格
+func updatePrices(ctx context.Context, db *mongo.Collection, planId primitive.ObjectID, compId string) error {
+	// 1. 获取计划详情
+	var plan model.ProductPlan
+	err := db.FindOne(ctx, bson.M{"_id": planId}).Decode(&plan)
+	if err != nil {
+		return err
+	}
+
+	// 2. 遍历所有组件,找到目标组件并重新计算价格
+	var totalPrice float64 = 0
+	for _, comp := range plan.Pack.Components {
+		compPrice := 0.0
+		// 计算组件的所有stage总价
+		for _, stage := range comp.Stages {
+			stagePrice := stage.OrderPrice * float64(stage.OrderCount)
+			compPrice += stagePrice
+		}
+		// 更新组件总价
+		if comp.Id == compId {
+			_, err = db.UpdateOne(ctx,
+				bson.M{"_id": planId, "pack.components.id": compId},
+				bson.M{"$set": bson.M{"pack.components.$.totalPrice": compPrice}})
+			if err != nil {
+				return err
+			}
+		}
+		totalPrice += compPrice
+	}
+
+	// 3. 更新计划总价
+	_, err = db.UpdateOne(ctx,
+		bson.M{"_id": planId},
+		bson.M{"$set": bson.M{"totalPrice": &totalPrice}})
+	return err
+}
+
 // 增加stage
-// planId packId compentId
-// 计划价格 组件价格
+// 根据planId packId compentId定位到到计划中的compent
+// 注意更新计划价格 组件价格
 func AddStage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var req StageRequest
+	if err := c.ShouldBindJSON(&req); err != nil {
+		return nil, err
+	}
+
+	planId, err := primitive.ObjectIDFromHex(req.PlanId)
+	if err != nil {
+		return nil, err
+	}
+
+	// 创建新的stage,使用请求中的stage数据
+	newStage := req.Stage
+	newStage.Id = primitive.NewObjectID().Hex() // 确保生成新的ID
+
+	// 更新数据库
+	db := apictx.Svc.Mongo.GetCollection(repo.CollectionProductPlan)
+	update := bson.M{
+		"$push": bson.M{
+			"pack.components.$[components].stages": newStage,
+		},
+	}
+	arrayFilters := []interface{}{
+		bson.M{"components.id": req.CompId},
+	}
+
+	opts := options.Update().SetArrayFilters(options.ArrayFilters{
+		Filters: arrayFilters,
+	})
+
+	result, err := db.UpdateOne(apictx.CreateRepoCtx().Ctx,
+		bson.M{"_id": planId},
+		update,
+		opts,
+	)
+
+	if err != nil {
+		return nil, err
+	}
+
+	// 更新价格
+	err = updatePrices(apictx.CreateRepoCtx().Ctx, db, planId, req.CompId)
+	if err != nil {
+		return nil, err
+	}
 
-	return result, err
+	return result, nil
 }
 
+// 删除stage
+// 根据planId packId compentId stageId定位到到计划中的stage
+// 注意更新计划价格 组件价格
 func DeleteStage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var req StageRequest
+	if err := c.ShouldBindJSON(&req); err != nil {
+		return nil, err
+	}
 
-	return result, err
+	planId, err := primitive.ObjectIDFromHex(req.PlanId)
+	if err != nil {
+		return nil, err
+	}
+
+	// 更新数据库
+	db := apictx.Svc.Mongo.GetCollection(repo.CollectionProductPlan)
+	update := bson.M{
+		"$pull": bson.M{
+			"pack.components.$[components].stages": bson.M{
+				"id": req.StageId,
+			},
+		},
+	}
+	arrayFilters := []interface{}{
+		bson.M{"components.id": req.CompId},
+	}
+
+	opts := options.Update().SetArrayFilters(options.ArrayFilters{
+		Filters: arrayFilters,
+	})
+
+	result, err := db.UpdateOne(apictx.CreateRepoCtx().Ctx,
+		bson.M{"_id": planId},
+		update,
+		opts,
+	)
+
+	if err != nil {
+		return nil, err
+	}
+
+	// 更新价格
+	err = updatePrices(apictx.CreateRepoCtx().Ctx, db, planId, req.CompId)
+	if err != nil {
+		return nil, err
+	}
+
+	return result, nil
 }
 
+// 更新stage
+// 根据planId packId compentId stageId定位到到计划中的stage
+// 注意更新计划价格 组件价格
 func UpdateStage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var req StageRequest
+	if err := c.ShouldBindJSON(&req); err != nil {
+		return nil, err
+	}
+
+	planId, err := primitive.ObjectIDFromHex(req.PlanId)
+	if err != nil {
+		return nil, err
+	}
+
+	// 更新数据库
+	db := apictx.Svc.Mongo.GetCollection(repo.CollectionProductPlan)
+	update := bson.M{
+		"$set": bson.M{
+			"pack.components.$[components].stages.$[stage]": req.Stage,
+		},
+	}
+
+	arrayFilters := []interface{}{
+		bson.M{"components.id": req.CompId},
+		bson.M{"stage.id": req.StageId},
+	}
+
+	opts := options.Update().SetArrayFilters(options.ArrayFilters{
+		Filters: arrayFilters,
+	})
+
+	result, err := db.UpdateOne(apictx.CreateRepoCtx().Ctx,
+		bson.M{"_id": planId},
+		update,
+		opts,
+	)
+
+	if err != nil {
+		return nil, err
+	}
+
+	// 更新价格
+	err = updatePrices(apictx.CreateRepoCtx().Ctx, db, planId, req.CompId)
+	if err != nil {
+		return nil, err
+	}
 
-	return result, err
+	return result, nil
 }

+ 0 - 89
boxcost/db/model/pack.go

@@ -1,90 +1 @@
 package model
-
-import (
-	"time"
-
-	"go.mongodb.org/mongo-driver/bson/primitive"
-)
-
-// 包装
-type Pack struct {
-	Id        primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
-	Name      string             `bson:"name" json:"name"`
-	Thumbnail string             `bson:"thumbnail" json:"thumbnail"`
-	// 部件数量
-	CompCounts int `bson:"compCounts" json:"compCounts"`
-	// 设计师
-	Designer string `bson:"designer" json:"designer"`
-	// 组成包装的部件
-	Components []*PackComponent `bson:"components" json:"components"`
-	CreateTime time.Time        `bson:"createTime" json:"createTime"`
-	UpdateTime time.Time        `bson:"updateTime" json:"updateTime"`
-}
-
-type PackComponent struct {
-	Id        string `bson:"id" json:"id"`
-	Name      string `bson:"name" json:"name"`
-	Thumbnail string `bson:"thumbnail" json:"thumbnail"`
-	//数量
-	Count int `bson:"count" json:"count"`
-
-	//刀版图
-	Uv string `bson:"uv" json:"uv"`
-
-	//部件尺寸
-	UvSize string `bson:"uvSize" json:"uvSize"`
-
-	//所有材料
-	Stages []*ComponentStage `bson:"stages" json:"stages"`
-
-	Remark string `bson:"remark" json:"remark"`
-
-	//部件总价
-	TotalPrice float64 `bson:"totalPrice" json:"totalPrice"`
-}
-
-type ComponentStage struct {
-	Id     string             `bson:"id" json:"id"`
-	TypeId primitive.ObjectID `bson:"typeId" json:"typeId"`
-
-	// 下单单价
-	OrderPrice float64 `bson:"orderPrice" json:"orderPrice"`
-
-	// 下单数量
-	OrderCount int `bson:"orderCount" json:"orderCount"`
-
-	// 是否固定价格 如果是:为预算总价,不是:为实际总价
-	IsFix *bool `bson:"isFix" json:"isFix"`
-
-	//实际价格 OrderPrice*ConfirmCount
-	//预算价格 OrderPrice*OrderCount
-
-	//确认收货数量
-	ConfirmCount int `bson:"confirmCount" json:"confirmCount"`
-
-	// 供应相关信息
-	// 交货时间
-	DeliveryTime time.Time `bson:"deliveryTime" json:"deliveryTime"`
-
-	// 供应商信息
-	SupplierInfo *Supplier `bson:"supplierInfo" json:"supplierInfo"`
-
-	BatchCount      *float64 `bson:"batchCount" json:"batchCount"`           //拼版数量
-	BatchSizeWidth  int      `bson:"batchSizeWidth" json:"batchSizeWidth"`   //平板尺寸
-	BatchSizeHeight int      `bson:"batchSizeHeight" json:"batchSizeHeight"` //平板尺寸
-
-	Remark     string    `bson:"remark" json:"remark"`     //备注
-	Size       string    `bson:"size" json:"size"`         //工艺尺寸
-	Name       string    `bson:"name" json:"name"`         //名称
-	Category   string    `bson:"category" json:"category"` //分类
-	Price      float64   `bson:"price" json:"price"`       //单价
-	Unit       string    `bson:"unit" json:"unit"`         //单位
-	Norm       string    `bson:"norm" json:"norm"`         // 规格
-	CreateTime time.Time `bson:"createTime" json:"createTime"`
-	UpdateTime time.Time `bson:"updateTime" json:"updateTime"`
-	Type       int       `bson:"type" json:"type"`         //1-材料 2-工艺 3-成品
-	Group      string    `bson:"group" json:"group"`       //工序合标记,相同Group的数据合并成一个单据
-	BillId     string    `bson:"billId" json:"billId"`     //订单Id
-	BillType   int       `bson:"billType" json:"billType"` //订单type // 1,2,3
-	Sort       int       `bson:"sort" json:"sort"`
-}

+ 83 - 0
boxcost/db/model/plan.go

@@ -28,3 +28,86 @@ type ProductPlan struct {
 
 	CreateTime time.Time `bson:"createTime,omitempty" json:"createTime"`
 }
+
+// 包装
+type Pack struct {
+	Id        primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
+	Name      string             `bson:"name" json:"name"`
+	Thumbnail string             `bson:"thumbnail" json:"thumbnail"`
+	// 部件数量
+	CompCounts int `bson:"compCounts" json:"compCounts"`
+	// 设计师
+	Designer string `bson:"designer" json:"designer"`
+	// 组成包装的部件
+	Components []*PackComponent `bson:"components" json:"components"`
+	CreateTime time.Time        `bson:"createTime" json:"createTime"`
+	UpdateTime time.Time        `bson:"updateTime" json:"updateTime"`
+}
+
+type PackComponent struct {
+	Id        string `bson:"id" json:"id"`
+	Name      string `bson:"name" json:"name"`
+	Thumbnail string `bson:"thumbnail" json:"thumbnail"`
+	//数量
+	Count int `bson:"count" json:"count"`
+
+	//刀版图
+	Uv string `bson:"uv" json:"uv"`
+
+	//部件尺寸
+	UvSize string `bson:"uvSize" json:"uvSize"`
+
+	//所有材料
+	Stages []*ComponentStage `bson:"stages" json:"stages"`
+
+	Remark string `bson:"remark" json:"remark"`
+
+	//部件总价
+	TotalPrice float64 `bson:"totalPrice" json:"totalPrice"`
+}
+
+type ComponentStage struct {
+	Id     string             `bson:"id" json:"id"`
+	TypeId primitive.ObjectID `bson:"typeId" json:"typeId"`
+
+	// 下单单价
+	OrderPrice float64 `bson:"orderPrice" json:"orderPrice"`
+
+	// 下单数量
+	OrderCount int `bson:"orderCount" json:"orderCount"`
+
+	// 是否固定价格 如果是:为预算总价,不是:为实际总价
+	IsFix *bool `bson:"isFix" json:"isFix"`
+
+	//实际价格 OrderPrice*ConfirmCount
+	//预算价格 OrderPrice*OrderCount
+
+	//确认收货数量
+	ConfirmCount int `bson:"confirmCount" json:"confirmCount"`
+
+	// 供应相关信息
+	// 交货时间
+	DeliveryTime time.Time `bson:"deliveryTime" json:"deliveryTime"`
+
+	// 供应商信息
+	SupplierInfo *Supplier `bson:"supplierInfo" json:"supplierInfo"`
+
+	BatchCount      *float64 `bson:"batchCount" json:"batchCount"`           //拼版数量
+	BatchSizeWidth  int      `bson:"batchSizeWidth" json:"batchSizeWidth"`   //平板尺寸
+	BatchSizeHeight int      `bson:"batchSizeHeight" json:"batchSizeHeight"` //平板尺寸
+
+	Remark     string    `bson:"remark" json:"remark"`     //备注
+	Size       string    `bson:"size" json:"size"`         //工艺尺寸
+	Name       string    `bson:"name" json:"name"`         //名称
+	Category   string    `bson:"category" json:"category"` //分类
+	Price      float64   `bson:"price" json:"price"`       //单价
+	Unit       string    `bson:"unit" json:"unit"`         //单位
+	Norm       string    `bson:"norm" json:"norm"`         // 规格
+	CreateTime time.Time `bson:"createTime" json:"createTime"`
+	UpdateTime time.Time `bson:"updateTime" json:"updateTime"`
+	Type       int       `bson:"type" json:"type"`         //1-材料 2-工艺 3-成品
+	Group      string    `bson:"group" json:"group"`       //工序合标记,相同Group的数据合并成一个单据
+	BillId     string    `bson:"billId" json:"billId"`     //订单Id
+	BillType   int       `bson:"billType" json:"billType"` //订单type // 1,2,3
+	Sort       int       `bson:"sort" json:"sort"`
+}

+ 0 - 4
boxcost/go.mod

@@ -21,7 +21,6 @@ require (
 )
 
 require (
-	github.com/StackExchange/wmi v1.2.1 // indirect
 	github.com/alibabacloud-go/alibabacloud-gateway-spi v0.0.4 // indirect
 	github.com/alibabacloud-go/debug v0.0.0-20190504072949-9472017b5c68 // indirect
 	github.com/alibabacloud-go/endpoint-util v1.1.0 // indirect
@@ -30,9 +29,6 @@ require (
 	github.com/alibabacloud-go/tea-xml v1.1.2 // indirect
 	github.com/aliyun/credentials-go v1.1.2 // indirect
 	github.com/clbanning/mxj/v2 v2.5.6 // indirect
-	github.com/go-ole/go-ole v1.2.6 // indirect
-	github.com/jessevdk/go-flags v1.5.0 // indirect
-	github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
 	github.com/tjfoc/gmsm v1.3.2 // indirect
 	golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
 	google.golang.org/appengine v1.6.7 // indirect

+ 0 - 8
boxcost/go.sum

@@ -109,8 +109,6 @@ github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbt
 github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
 github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
 github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ=
-github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
-github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
 github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
 github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
 github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
@@ -493,9 +491,6 @@ github.com/go-logr/logr v1.2.1/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbV
 github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
 github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI=
 github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
-github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
-github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
-github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
 github.com/go-openapi/jsonpointer v0.0.0-20160704185906-46af16f9f7b1/go.mod h1:+35s3my2LFTysnkMfxsJBAMHj/DoqoB9knIWoYG/Vk0=
 github.com/go-openapi/jsonpointer v0.19.2/go.mod h1:3akKfEdA7DF1sugOqz1dVQHBcuDBPKZGEoHC/NkiQRg=
 github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
@@ -791,8 +786,6 @@ github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0f
 github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
 github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
 github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
-github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
-github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
 github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
 github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
 github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
@@ -824,7 +817,6 @@ github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+
 github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
 github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
 github.com/k0kubun/pp v2.3.0+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
-github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA=
 github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
 github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4=
 github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA=