stages.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "context"
  6. "github.com/gin-gonic/gin"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. "go.mongodb.org/mongo-driver/mongo"
  10. "go.mongodb.org/mongo-driver/mongo/options"
  11. )
  12. // StageRequest 用于添加和更新stage的请求结构
  13. type StageRequest struct {
  14. PlanId string `json:"planId"`
  15. PackId string `json:"packId"`
  16. CompId string `json:"compId"`
  17. StageId string `json:"stageId,omitempty"`
  18. Stage *model.ComponentStage `json:"stage"`
  19. }
  20. // 更新计划和部件的价格
  21. func updatePrices(ctx context.Context, db *mongo.Collection, planId primitive.ObjectID, compId string) error {
  22. // 1. 获取计划详情
  23. var plan model.ProductPlan
  24. err := db.FindOne(ctx, bson.M{"_id": planId}).Decode(&plan)
  25. if err != nil {
  26. return err
  27. }
  28. // 2. 遍历所有组件,找到目标组件并重新计算价格
  29. var totalPrice float64 = 0
  30. for _, comp := range plan.Pack.Components {
  31. compPrice := 0.0
  32. // 计算组件的所有stage总价
  33. for _, stage := range comp.Stages {
  34. stagePrice := stage.OrderPrice * float64(stage.OrderCount)
  35. compPrice += stagePrice
  36. }
  37. // 更新组件总价
  38. if comp.Id == compId {
  39. _, err = db.UpdateOne(ctx,
  40. bson.M{"_id": planId, "pack.components.id": compId},
  41. bson.M{"$set": bson.M{"pack.components.$.totalPrice": compPrice}})
  42. if err != nil {
  43. return err
  44. }
  45. }
  46. totalPrice += compPrice
  47. }
  48. // 3. 更新计划总价
  49. _, err = db.UpdateOne(ctx,
  50. bson.M{"_id": planId},
  51. bson.M{"$set": bson.M{"totalPrice": &totalPrice}})
  52. return err
  53. }
  54. // 增加stage
  55. // 根据planId packId compentId定位到到计划中的compent
  56. // 注意更新计划价格 组件价格
  57. func AddStage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  58. var req StageRequest
  59. if err := c.ShouldBindJSON(&req); err != nil {
  60. return nil, err
  61. }
  62. planId, err := primitive.ObjectIDFromHex(req.PlanId)
  63. if err != nil {
  64. return nil, err
  65. }
  66. // 创建新的stage,使用请求中的stage数据
  67. newStage := req.Stage
  68. newStage.Id = primitive.NewObjectID().Hex() // 确保生成新的ID
  69. // 更新数据库
  70. db := apictx.Svc.Mongo.GetCollection(repo.CollectionProductPlan)
  71. update := bson.M{
  72. "$push": bson.M{
  73. "pack.components.$[components].stages": newStage,
  74. },
  75. }
  76. arrayFilters := []interface{}{
  77. bson.M{"components.id": req.CompId},
  78. }
  79. opts := options.Update().SetArrayFilters(options.ArrayFilters{
  80. Filters: arrayFilters,
  81. })
  82. result, err := db.UpdateOne(apictx.CreateRepoCtx().Ctx,
  83. bson.M{"_id": planId},
  84. update,
  85. opts,
  86. )
  87. if err != nil {
  88. return nil, err
  89. }
  90. // 更新价格
  91. err = updatePrices(apictx.CreateRepoCtx().Ctx, db, planId, req.CompId)
  92. if err != nil {
  93. return nil, err
  94. }
  95. return result, nil
  96. }
  97. // 删除stage
  98. // 根据planId packId compentId stageId定位到到计划中的stage
  99. // 注意更新计划价格 组件价格
  100. func DeleteStage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  101. var req StageRequest
  102. if err := c.ShouldBindJSON(&req); err != nil {
  103. return nil, err
  104. }
  105. planId, err := primitive.ObjectIDFromHex(req.PlanId)
  106. if err != nil {
  107. return nil, err
  108. }
  109. // 更新数据库
  110. db := apictx.Svc.Mongo.GetCollection(repo.CollectionProductPlan)
  111. update := bson.M{
  112. "$pull": bson.M{
  113. "pack.components.$[components].stages": bson.M{
  114. "id": req.StageId,
  115. },
  116. },
  117. }
  118. arrayFilters := []interface{}{
  119. bson.M{"components.id": req.CompId},
  120. }
  121. opts := options.Update().SetArrayFilters(options.ArrayFilters{
  122. Filters: arrayFilters,
  123. })
  124. result, err := db.UpdateOne(apictx.CreateRepoCtx().Ctx,
  125. bson.M{"_id": planId},
  126. update,
  127. opts,
  128. )
  129. if err != nil {
  130. return nil, err
  131. }
  132. // 更新价格
  133. err = updatePrices(apictx.CreateRepoCtx().Ctx, db, planId, req.CompId)
  134. if err != nil {
  135. return nil, err
  136. }
  137. return result, nil
  138. }
  139. // 更新stage
  140. // 根据planId packId compentId stageId定位到到计划中的stage
  141. // 注意更新计划价格 组件价格
  142. func UpdateStage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  143. var req StageRequest
  144. if err := c.ShouldBindJSON(&req); err != nil {
  145. return nil, err
  146. }
  147. planId, err := primitive.ObjectIDFromHex(req.PlanId)
  148. if err != nil {
  149. return nil, err
  150. }
  151. // 更新数据库
  152. db := apictx.Svc.Mongo.GetCollection(repo.CollectionProductPlan)
  153. update := bson.M{
  154. "$set": bson.M{
  155. "pack.components.$[components].stages.$[stage]": req.Stage,
  156. },
  157. }
  158. arrayFilters := []interface{}{
  159. bson.M{"components.id": req.CompId},
  160. bson.M{"stage.id": req.StageId},
  161. }
  162. opts := options.Update().SetArrayFilters(options.ArrayFilters{
  163. Filters: arrayFilters,
  164. })
  165. result, err := db.UpdateOne(apictx.CreateRepoCtx().Ctx,
  166. bson.M{"_id": planId},
  167. update,
  168. opts,
  169. )
  170. if err != nil {
  171. return nil, err
  172. }
  173. // 更新价格
  174. err = updatePrices(apictx.CreateRepoCtx().Ctx, db, planId, req.CompId)
  175. if err != nil {
  176. return nil, err
  177. }
  178. return result, nil
  179. }
  180. // 单独更新计划字段
  181. func UpdatePlanfileds(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  182. return nil, nil
  183. }
  184. // 新增组件
  185. func AddCompnonet(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  186. return nil, nil
  187. }
  188. func DeleteCompnonet(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  189. return nil, nil
  190. }
  191. func UpdateCompnonetName(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  192. return nil, nil
  193. }