supplier.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "box-cost/log"
  6. "errors"
  7. "fmt"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. "go.mongodb.org/mongo-driver/bson"
  11. "go.mongodb.org/mongo-driver/bson/primitive"
  12. "go.mongodb.org/mongo-driver/mongo/options"
  13. )
  14. // 供应商管理
  15. func Supplier(r *GinRouter) {
  16. // 创建供应商
  17. r.POST("/supplier/create", CreateSupplier)
  18. // 获取供应商详情
  19. r.GET("/supplier/detail/:id", GetSupplier)
  20. // 获取供应商列表
  21. r.GET("/supplier/list", GetSuppliers)
  22. // 更新供应商
  23. r.POST("/supplier/update", UpdateSupplier)
  24. // 删除供应商
  25. r.POST("/supplier/delete/:id", DelSupplier)
  26. // 获取供应商列表
  27. r.GET("/plan/supplier/list", GetPlanSuppliers)
  28. }
  29. // 创建供应商
  30. func CreateSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  31. var supplier model.Supplier
  32. err := c.ShouldBindJSON(&supplier)
  33. if err != nil {
  34. fmt.Println(err)
  35. return nil, errors.New("参数错误!")
  36. }
  37. ctx := apictx.CreateRepoCtx()
  38. if supplier.Name == "" {
  39. return nil, errors.New("供应商名为空")
  40. }
  41. if supplier.Address == "" {
  42. return nil, errors.New("供应商地址为空")
  43. }
  44. if supplier.Phone == "" {
  45. return nil, errors.New("供应商联系电话为空")
  46. }
  47. supplier.CreateTime = time.Now()
  48. supplier.UpdateTime = time.Now()
  49. result, err := repo.RepoAddDoc(ctx, repo.CollectionSupplier, &supplier)
  50. return result, err
  51. }
  52. // 获取供应商信息
  53. func GetSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  54. supplierId := c.Param("id")
  55. id, err := primitive.ObjectIDFromHex(supplierId)
  56. if err != nil {
  57. return nil, errors.New("非法id")
  58. }
  59. var supplier model.Supplier
  60. option := &repo.DocSearchOptions{
  61. CollectName: repo.CollectionSupplier,
  62. Query: repo.Map{"_id": id},
  63. }
  64. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &supplier)
  65. if !found || err != nil {
  66. log.Info(err)
  67. return nil, errors.New("数据未找到")
  68. }
  69. return supplier, nil
  70. }
  71. // 获取供应商列表
  72. func GetSuppliers(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  73. page, size, query := UtilQueryPageSize(c)
  74. option := &repo.PageSearchOptions{
  75. CollectName: repo.CollectionSupplier,
  76. Query: query,
  77. Page: page,
  78. Size: size,
  79. Sort: bson.M{"createTime": -1},
  80. }
  81. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  82. }
  83. func GetPlanSuppliers(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  84. page, size, query := UtilQueryPageSize(c)
  85. //category =>根据内容查询 供应对应内容的供应商
  86. emtyPage := &repo.PageResult{
  87. Total: 0,
  88. Size: size,
  89. Page: page,
  90. List: []map[string]interface{}{},
  91. }
  92. listOut := []map[string]interface{}{}
  93. if query["matId"] != nil {
  94. matId := query["matId"].(string)
  95. if len(matId) < 1 {
  96. return nil, fmt.Errorf("matId(string)为空")
  97. }
  98. id, _ := primitive.ObjectIDFromHex(matId)
  99. ok, list := repo.RepoSeachDocsMap(apictx.CreateRepoCtx(), &repo.DocsSearchOptions{
  100. CollectName: repo.CollectionSupplierMatprice,
  101. Query: repo.Map{"productId": id},
  102. Project: []string{"supplierId"},
  103. })
  104. if !ok {
  105. return emtyPage, nil
  106. }
  107. listOut = list
  108. } else {
  109. if query["craftId"] == nil {
  110. return nil, fmt.Errorf("参数错误 craftId 或matId不能为空")
  111. }
  112. cratf := &model.Craft{}
  113. ok, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  114. Query: repo.Map{"_id": query["craftId"].(string)},
  115. CollectName: repo.CollectionCraft,
  116. }, cratf)
  117. if !ok {
  118. return nil, fmt.Errorf("没有对应的工艺信息")
  119. }
  120. //查询工艺分类
  121. pipleLine := []bson.M{
  122. {
  123. "$lookup": bson.M{
  124. "from": repo.CollectionCraft,
  125. "localField": "productId",
  126. "foreignField": "_id",
  127. "as": "craft_docs",
  128. },
  129. },
  130. {
  131. "$match": bson.M{
  132. "craft_docs.0.category": cratf.Category,
  133. },
  134. },
  135. {
  136. "$project": bson.M{
  137. "craft_docs": 0,
  138. "createTime": 0,
  139. "price": 0,
  140. "productId": 0,
  141. "updateTime": 0,
  142. },
  143. },
  144. }
  145. ctx := apictx.CreateRepoCtx()
  146. colls := ctx.Client.GetCollection(repo.CollectionSupplierCraftprice)
  147. findoptions := &options.AggregateOptions{}
  148. cur, err := colls.Aggregate(ctx.Ctx, pipleLine, findoptions)
  149. if err != nil {
  150. return nil, err
  151. }
  152. defer cur.Close(ctx.Ctx)
  153. err = cur.All(ctx.Ctx, &listOut)
  154. if err != nil {
  155. return nil, err
  156. }
  157. if len(listOut) < 1 {
  158. return emtyPage, nil
  159. }
  160. }
  161. //获取供应商列表
  162. suppliers := []primitive.ObjectID{}
  163. suppliersMap := map[string]bool{}
  164. for _, item := range listOut {
  165. if item["supplierId"] != nil {
  166. id, ok := item["supplierId"].(primitive.ObjectID)
  167. if !ok {
  168. continue
  169. }
  170. if !suppliersMap[id.Hex()] {
  171. suppliers = append(suppliers, id)
  172. suppliersMap[id.Hex()] = true
  173. }
  174. }
  175. }
  176. if len(suppliers) < 1 {
  177. return emtyPage, nil
  178. }
  179. option := &repo.PageSearchOptions{
  180. CollectName: repo.CollectionSupplier,
  181. Query: repo.Map{"_id": bson.M{"$in": suppliers}},
  182. Page: page,
  183. Size: size,
  184. Sort: bson.M{"createTime": -1},
  185. }
  186. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  187. }
  188. // 更新供应商
  189. func UpdateSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  190. var supplier model.Supplier
  191. err := c.ShouldBindJSON(&supplier)
  192. if err != nil {
  193. return nil, errors.New("参数错误")
  194. }
  195. if supplier.Id.Hex() == "" {
  196. return nil, errors.New("id的为空")
  197. }
  198. supplier.UpdateTime = time.Now()
  199. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionSupplier, supplier.Id.Hex(), &supplier)
  200. }
  201. // 删除供应商
  202. func DelSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  203. supplierId := c.Param("id")
  204. if supplierId == "" {
  205. return nil, errors.New("id为空")
  206. }
  207. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionSupplier, supplierId)
  208. }