stages.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "math"
  9. "time"
  10. "github.com/gin-gonic/gin"
  11. "go.mongodb.org/mongo-driver/bson"
  12. "go.mongodb.org/mongo-driver/bson/primitive"
  13. "go.mongodb.org/mongo-driver/mongo"
  14. "go.mongodb.org/mongo-driver/mongo/options"
  15. )
  16. // StageRequest 用于添加和更新stage的请求结构
  17. type StageRequest struct {
  18. PlanId string `json:"planId"`
  19. // PackId string `json:"packId"`
  20. CompId string `json:"componentId"`
  21. StageId string `json:"stageId,omitempty"`
  22. Stages []*model.ComponentStage `json:"stages"`
  23. }
  24. // 更新计划和部件的价格
  25. func updatePrices(c *gin.Context, db *mongo.Collection, planId primitive.ObjectID, compId string) error {
  26. // 1. 获取计划详情
  27. var plan model.ProductPlan
  28. err := db.FindOne(c.Request.Context(), bson.M{"_id": planId}).Decode(&plan)
  29. if err != nil {
  30. return err
  31. }
  32. // 2. 遍历所有组件,找到目标组件并重新计算价格
  33. var totalPrice float64 = 0
  34. for _, comp := range plan.Pack.Components {
  35. compPrice := 0.0
  36. // 计算组件的所有stage总价
  37. for _, stage := range comp.Stages {
  38. stagePrice := stage.OrderPrice * float64(stage.OrderCount)
  39. compPrice += stagePrice
  40. }
  41. // 截断组件总价到两位小数
  42. _compPrice := math.Trunc(compPrice*1000) / 1000
  43. // 更新组件总价
  44. if comp.Id == compId {
  45. _, err = db.UpdateOne(c.Request.Context(),
  46. bson.M{"_id": planId, "pack.components.id": compId},
  47. bson.M{"$set": bson.M{"pack.components.$.totalPrice": _compPrice}})
  48. if err != nil {
  49. return err
  50. }
  51. }
  52. totalPrice += compPrice
  53. }
  54. // 3. 更新计划总价(截断到两位小数)
  55. totalPrice = math.Trunc(totalPrice*1000) / 1000
  56. _, err = db.UpdateOne(c.Request.Context(),
  57. bson.M{"_id": planId},
  58. bson.M{"$set": bson.M{"totalPrice": &totalPrice}})
  59. return err
  60. }
  61. // 增加stage
  62. // 根据planId packId compentId定位到到计划中的compent
  63. // 注意更新计划价格 组件价格
  64. func AddStage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  65. var req StageRequest
  66. if err := c.ShouldBindJSON(&req); err != nil {
  67. return nil, err
  68. }
  69. planId, err := primitive.ObjectIDFromHex(req.PlanId)
  70. if err != nil {
  71. return nil, err
  72. }
  73. // 更新数据库,添加多个stage
  74. update := bson.M{
  75. "$push": bson.M{
  76. "pack.components.$[components].stages": bson.M{
  77. "$each": req.Stages,
  78. },
  79. },
  80. "$set": bson.M{
  81. "updateTime": time.Now(),
  82. },
  83. }
  84. arrayFilters := []interface{}{
  85. bson.M{"components.id": req.CompId},
  86. }
  87. opts := options.Update().SetArrayFilters(options.ArrayFilters{
  88. Filters: arrayFilters,
  89. })
  90. db := apictx.Svc.Mongo.GetCollection(repo.CollectionProductPlan)
  91. result, err := db.UpdateOne(c.Request.Context(),
  92. bson.M{"_id": planId},
  93. update,
  94. opts,
  95. )
  96. if err != nil {
  97. return nil, err
  98. }
  99. // 更新价格
  100. err = updatePrices(c, db, planId, req.CompId)
  101. if err != nil {
  102. return nil, err
  103. }
  104. _, err = AddPlanHistory(c, apictx, req.PlanId)
  105. if err != nil {
  106. fmt.Println("记录历史失败:", err)
  107. }
  108. return result, nil
  109. }
  110. // 删除stage
  111. // 根据planId packId compentId stageId定位到到计划中的stage
  112. // 注意更新计划价格 组件价格
  113. func DeleteStage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  114. var req StageRequest
  115. if err := c.ShouldBindJSON(&req); err != nil {
  116. return nil, err
  117. }
  118. planId, err := primitive.ObjectIDFromHex(req.PlanId)
  119. if err != nil {
  120. return nil, err
  121. }
  122. if req.StageId == "" || req.CompId == "" {
  123. return nil, errors.New("stageId and compId are required")
  124. }
  125. // 更新数据库
  126. db := apictx.Svc.Mongo.GetCollection(repo.CollectionProductPlan)
  127. update := bson.M{
  128. "$pull": bson.M{
  129. "pack.components.$[components].stages": bson.M{
  130. "id": req.StageId,
  131. },
  132. },
  133. }
  134. arrayFilters := []interface{}{
  135. bson.M{"components.id": req.CompId},
  136. }
  137. opts := options.Update().SetArrayFilters(options.ArrayFilters{
  138. Filters: arrayFilters,
  139. })
  140. result, err := db.UpdateOne(c.Request.Context(),
  141. bson.M{"_id": planId},
  142. update,
  143. opts,
  144. )
  145. if err != nil {
  146. return nil, err
  147. }
  148. // 更新价格
  149. err = updatePrices(c, db, planId, req.CompId)
  150. if err != nil {
  151. return nil, err
  152. }
  153. _, err = AddPlanHistory(c, apictx, req.PlanId)
  154. if err != nil {
  155. fmt.Println("记录历史失败:", err)
  156. }
  157. return result, nil
  158. }
  159. // 根据planId compentId stageId定位到到计划中的stage
  160. // 注意更新计划价格 组件价格
  161. // 根据planId compentId stageId定位到到计划中的stage
  162. // 注意更新计划价格 组件价格
  163. func updateStage(c *gin.Context, db *mongo.Collection, req *StageRequest) (interface{}, error) {
  164. if len(req.PlanId) == 0 || len(req.CompId) == 0 || len(req.StageId) == 0 {
  165. return nil, fmt.Errorf("planId, componentId and stageId are required")
  166. }
  167. planId, err := primitive.ObjectIDFromHex(req.PlanId)
  168. if err != nil {
  169. return nil, fmt.Errorf("invalid planId: %v", err)
  170. }
  171. if len(req.Stages) == 0 {
  172. return nil, fmt.Errorf("stages array cannot be empty")
  173. }
  174. // 构建更新操作
  175. update := bson.M{
  176. "$set": bson.M{
  177. "pack.components.$[comp].stages.$[stg]": req.Stages[0],
  178. "updateTime": time.Now(),
  179. },
  180. }
  181. // 使用正确的数组过滤器
  182. arrayFilters := options.ArrayFilters{
  183. Filters: []interface{}{
  184. bson.M{"comp.id": req.CompId},
  185. bson.M{"stg.id": req.StageId},
  186. },
  187. }
  188. // 执行更新
  189. opts := options.FindOneAndUpdate().
  190. SetArrayFilters(arrayFilters).
  191. SetReturnDocument(options.After)
  192. var updatedDoc bson.M
  193. err = db.FindOneAndUpdate(
  194. c.Request.Context(),
  195. bson.M{"_id": planId},
  196. update,
  197. opts,
  198. ).Decode(&updatedDoc)
  199. if err != nil {
  200. if err == mongo.ErrNoDocuments {
  201. // 打印更详细的错误信息
  202. fmt.Printf("Debug - Failed to update. PlanId: %s, CompId: %s, StageId: %s\n", planId.Hex(), req.CompId, req.StageId)
  203. return nil, fmt.Errorf("plan or stage not found, planId: %s, compId: %s, stageId: %s", req.PlanId, req.CompId, req.StageId)
  204. }
  205. return nil, fmt.Errorf("failed to update stage: %v", err)
  206. }
  207. fmt.Printf("Debug - Update successful\n")
  208. // 更新价格
  209. err = updatePrices(c, db, planId, req.CompId)
  210. if err != nil {
  211. return nil, fmt.Errorf("failed to update prices: %v", err)
  212. }
  213. return true, nil
  214. }
  215. // 更新stages
  216. // 根据planId packId compentId stageId定位到到计划中的stage
  217. // 注意更新计划价格 组件价格
  218. func UpdateStages(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  219. var req StageRequest
  220. if err := c.ShouldBindJSON(&req); err != nil {
  221. return nil, err
  222. }
  223. // 参数检查
  224. if len(req.PlanId) == 0 || len(req.CompId) == 0 || len(req.Stages) == 0 {
  225. return nil, fmt.Errorf("invalid parameters: planId, componentId and stages are required")
  226. }
  227. planId, err := primitive.ObjectIDFromHex(req.PlanId)
  228. if err != nil {
  229. return nil, fmt.Errorf("invalid planId: %v", err)
  230. }
  231. // 获取更新前的数据用于验证
  232. db := apictx.Svc.Mongo.GetCollection(repo.CollectionProductPlan)
  233. ctx := c.Request.Context()
  234. var plan model.ProductPlan
  235. err = db.FindOne(ctx, bson.M{"_id": planId}).Decode(&plan)
  236. if err != nil {
  237. return nil, fmt.Errorf("failed to find plan: %v", err)
  238. }
  239. // 验证组件是否存在
  240. componentExists := false
  241. if plan.Pack != nil {
  242. for _, comp := range plan.Pack.Components {
  243. if comp.Id == req.CompId {
  244. componentExists = true
  245. break
  246. }
  247. }
  248. }
  249. if !componentExists {
  250. return nil, fmt.Errorf("component %s not found in plan", req.CompId)
  251. }
  252. // 准备批量更新操作
  253. var models []mongo.WriteModel
  254. now := time.Now()
  255. for _, stage := range req.Stages {
  256. if stage.Id == "" {
  257. continue // 跳过没有ID的stage
  258. }
  259. update := bson.M{
  260. "$set": bson.M{
  261. "pack.components.$[components].stages.$[stage]": stage,
  262. "updateTime": now,
  263. },
  264. }
  265. arrayFilters := options.ArrayFilters{
  266. Filters: []interface{}{
  267. bson.M{"components.id": req.CompId},
  268. bson.M{"stage.id": stage.Id},
  269. },
  270. }
  271. model := mongo.NewUpdateOneModel().
  272. SetFilter(bson.M{"_id": planId}).
  273. SetUpdate(update).
  274. SetArrayFilters(arrayFilters)
  275. models = append(models, model)
  276. }
  277. if len(models) == 0 {
  278. return nil, fmt.Errorf("no valid stages to update")
  279. }
  280. // 执行批量更新
  281. opts := options.BulkWrite().SetOrdered(false) // 允许并行执行
  282. result, err := db.BulkWrite(c.Request.Context(), models, opts)
  283. if err != nil {
  284. return nil, fmt.Errorf("failed to update stages: %v", err)
  285. }
  286. // 更新价格
  287. err = updatePrices(c, db, planId, req.CompId)
  288. if err != nil {
  289. return nil, fmt.Errorf("failed to update prices: %v", err)
  290. }
  291. // 记录历史
  292. _, err = AddPlanHistory(c, apictx, req.PlanId)
  293. if err != nil {
  294. fmt.Println("记录历史失败:", err)
  295. }
  296. return result, nil
  297. }
  298. type UpdatePlanFieldsReq struct {
  299. PlanId string `json:"planId"`
  300. Total int `json:"total,omitempty"`
  301. CreateUser string `json:"createUser,omitempty"`
  302. Name string `json:"name,omitempty"`
  303. }
  304. // 单独更新计划字段
  305. func UpdatePlanfileds(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  306. var req UpdatePlanFieldsReq
  307. if err := c.ShouldBindJSON(&req); err != nil {
  308. return nil, err
  309. }
  310. planId, _ := primitive.ObjectIDFromHex(req.PlanId)
  311. if planId.IsZero() {
  312. return nil, fmt.Errorf("invalid planId")
  313. }
  314. result, err := repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, req.PlanId, &model.ProductPlan{
  315. Total: req.Total,
  316. CreateUser: req.CreateUser,
  317. Name: req.Name,
  318. UpdateTime: time.Now(),
  319. })
  320. if err != nil {
  321. return nil, err
  322. }
  323. // 记录历史
  324. _, err = AddPlanHistory(c, apictx, req.PlanId)
  325. if err != nil {
  326. fmt.Println("记录历史失败:", err)
  327. }
  328. return result, nil
  329. }
  330. // 新增组件
  331. func AddCompnonet(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  332. _planId := c.Param("id")
  333. planId, _ := primitive.ObjectIDFromHex(_planId)
  334. if planId.IsZero() {
  335. return nil, fmt.Errorf("planId is invalid")
  336. }
  337. var component model.PackComponent
  338. if err := c.ShouldBindJSON(&component); err != nil {
  339. return nil, err
  340. }
  341. // 计算新的总价
  342. newTotalPrice := component.TotalPrice
  343. oldPlan := &model.ProductPlan{}
  344. db := apictx.Svc.Mongo.GetCollection(repo.CollectionProductPlan)
  345. if err := db.FindOne(c.Request.Context(), bson.M{"_id": planId}).Decode(&oldPlan); err != nil {
  346. return nil, err
  347. } else if oldPlan.TotalPrice != nil {
  348. newTotalPrice += *oldPlan.TotalPrice
  349. }
  350. _newTotalPrice := math.Trunc(newTotalPrice*1000) / 1000
  351. update := bson.M{
  352. "$push": bson.M{
  353. "pack.components": component,
  354. },
  355. "$set": bson.M{
  356. "updateTime": time.Now(),
  357. "totalPrice": _newTotalPrice,
  358. },
  359. "$inc": bson.M{
  360. "pack.compCounts": 1,
  361. },
  362. }
  363. filter := bson.M{"_id": planId}
  364. result := db.FindOneAndUpdate(c.Request.Context(), filter, update)
  365. if result.Err() != nil {
  366. return nil, result.Err()
  367. }
  368. _, err := AddPlanHistory(c, apictx, _planId)
  369. if err != nil {
  370. fmt.Println("记录历史失败:", err)
  371. }
  372. return true, nil
  373. }
  374. func DeleteCompnonet(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  375. _planId := c.Param("id")
  376. planId, _ := primitive.ObjectIDFromHex(_planId)
  377. if planId.IsZero() {
  378. return nil, fmt.Errorf("planId is invalid")
  379. }
  380. componentId := c.Param("componentId")
  381. if len(componentId) == 0 {
  382. return nil, fmt.Errorf("componentId is invalid")
  383. }
  384. oldPlan := &model.ProductPlan{}
  385. // Find the component to be deleted and its price
  386. db := apictx.Svc.Mongo.GetCollection(repo.CollectionProductPlan)
  387. var componentPrice float64
  388. if err := db.FindOne(c.Request.Context(), bson.M{"_id": planId}).Decode(&oldPlan); err != nil {
  389. return nil, err
  390. } else if oldPlan.Pack != nil {
  391. for _, comp := range oldPlan.Pack.Components {
  392. if comp.Id == componentId {
  393. componentPrice = comp.TotalPrice
  394. break
  395. }
  396. }
  397. }
  398. var newTotalPrice float64
  399. if oldPlan.TotalPrice != nil {
  400. newTotalPrice = *oldPlan.TotalPrice - componentPrice
  401. if newTotalPrice < 0 {
  402. newTotalPrice = 0
  403. }
  404. }
  405. _newTotalPrice := math.Trunc(newTotalPrice*1000) / 1000
  406. update := bson.M{
  407. "$pull": bson.M{
  408. "pack.components": bson.M{"id": componentId},
  409. },
  410. "$set": bson.M{
  411. "updateTime": time.Now(),
  412. "totalPrice": _newTotalPrice,
  413. },
  414. "$inc": bson.M{
  415. "pack.compCounts": -1,
  416. },
  417. }
  418. filter := bson.M{"_id": planId}
  419. result := db.FindOneAndUpdate(c.Request.Context(), filter, update)
  420. if result.Err() != nil {
  421. return nil, result.Err()
  422. }
  423. _, err := AddPlanHistory(c, apictx, _planId)
  424. if err != nil {
  425. fmt.Println("记录历史失败:", err)
  426. }
  427. return true, nil
  428. }
  429. type UpdateCompnonetReq struct {
  430. Name string `json:"name,omitempty"`
  431. Stages []*model.ComponentStage `json:"stages,omitempty"`
  432. }
  433. func UpdateCompnonet(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  434. _planId := c.Param("id")
  435. planId, _ := primitive.ObjectIDFromHex(_planId)
  436. if planId.IsZero() {
  437. return nil, fmt.Errorf("planId is invalid")
  438. }
  439. componentId := c.Param("componentId")
  440. if len(componentId) == 0 {
  441. return nil, fmt.Errorf("componentId is invalid")
  442. }
  443. var nameUpdate UpdateCompnonetReq
  444. if err := c.ShouldBindJSON(&nameUpdate); err != nil {
  445. return nil, err
  446. }
  447. // 检查是否有需要更新的字段
  448. if len(nameUpdate.Name) == 0 && len(nameUpdate.Stages) == 0 {
  449. return nil, fmt.Errorf("no fields to update")
  450. }
  451. db := apictx.Svc.Mongo.GetCollection(repo.CollectionProductPlan)
  452. update := bson.M{
  453. "$set": bson.M{
  454. "updateTime": time.Now(),
  455. },
  456. }
  457. if len(nameUpdate.Name) > 0 {
  458. update["$set"].(bson.M)["pack.components.$.name"] = nameUpdate.Name
  459. }
  460. if len(nameUpdate.Stages) > 0 {
  461. update["$set"].(bson.M)["pack.components.$.stages"] = nameUpdate.Stages
  462. }
  463. filter := bson.M{
  464. "_id": planId,
  465. "pack.components.id": componentId,
  466. }
  467. result := db.FindOneAndUpdate(c.Request.Context(), filter, update)
  468. if result.Err() != nil {
  469. return nil, result.Err()
  470. }
  471. // 如果更新了stages,需要更新价格
  472. if len(nameUpdate.Stages) > 0 {
  473. err := updatePrices(c, db, planId, componentId)
  474. if err != nil {
  475. return nil, fmt.Errorf("failed to update prices: %v", err)
  476. }
  477. }
  478. // 记录历史
  479. _, err := AddPlanHistory(c, apictx, _planId)
  480. if err != nil {
  481. fmt.Println("记录历史失败:", err)
  482. }
  483. return true, nil
  484. }
  485. // 记录历史
  486. func AddPlanHistory(c *gin.Context, apictx *ApiSession, planId string, ht ...string) (interface{}, error) {
  487. htype := "update"
  488. if len(ht) > 0 {
  489. htype = ht[0]
  490. }
  491. // 获取更新后的数据并记录历史
  492. newPlan := &model.ProductPlan{}
  493. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  494. CollectName: repo.CollectionProductPlan,
  495. Query: repo.Map{"_id": planId},
  496. }, newPlan)
  497. if !found {
  498. return false, errors.New("数据未找到")
  499. }
  500. if err != nil {
  501. return false, err
  502. }
  503. newPlanBytes, _ := json.Marshal(newPlan)
  504. userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
  505. userInfo, err := getUserById(apictx, userId)
  506. if err != nil {
  507. return false, err
  508. }
  509. history := &model.History{
  510. Userinfo: userInfo,
  511. TargetId: planId,
  512. Path: c.Request.URL.Path,
  513. Collection: repo.CollectionProductPlan,
  514. Type: htype,
  515. Content: string(newPlanBytes),
  516. CreateTime: time.Now(),
  517. }
  518. _, err = repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionPlanHistory, history)
  519. if err != nil {
  520. return false, err
  521. }
  522. return true, nil
  523. }