supplier.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. if cate, ok := query["category"]; ok {
  75. delete(query, "category")
  76. query["categorys"] = bson.M{"$in": []string{cate.(string)}}
  77. }
  78. option := &repo.PageSearchOptions{
  79. CollectName: repo.CollectionSupplier,
  80. Query: query,
  81. Page: page,
  82. Size: size,
  83. Sort: bson.M{"createTime": -1},
  84. }
  85. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  86. }
  87. func GetPlanSuppliers(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  88. page, size, query := UtilQueryPageSize(c)
  89. //category =>根据内容查询 供应对应内容的供应商
  90. emtyPage := &repo.PageResult{
  91. Total: 0,
  92. Size: size,
  93. Page: page,
  94. List: []map[string]interface{}{},
  95. }
  96. listOut := []map[string]interface{}{}
  97. if query["matId"] != nil {
  98. matId := query["matId"].(string)
  99. if len(matId) < 1 {
  100. return nil, fmt.Errorf("matId(string)为空")
  101. }
  102. id, _ := primitive.ObjectIDFromHex(matId)
  103. ok, list := repo.RepoSeachDocsMap(apictx.CreateRepoCtx(), &repo.DocsSearchOptions{
  104. CollectName: repo.CollectionSupplierMatprice,
  105. Query: repo.Map{"productId": id},
  106. Project: []string{"supplierId"},
  107. })
  108. if !ok {
  109. return emtyPage, nil
  110. }
  111. listOut = list
  112. }
  113. if query["craftId"] != nil {
  114. // if query["craftId"] == nil {
  115. // return nil, fmt.Errorf("参数错误 craftId 或matId不能为空")
  116. // }
  117. cratf := &model.Craft{}
  118. ok, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  119. Query: repo.Map{"_id": query["craftId"].(string)},
  120. CollectName: repo.CollectionCraft,
  121. }, cratf)
  122. if !ok {
  123. return nil, fmt.Errorf("没有对应的工艺信息")
  124. }
  125. //查询工艺分类
  126. pipleLine := []bson.M{
  127. {
  128. "$lookup": bson.M{
  129. "from": repo.CollectionCraft,
  130. "localField": "productId",
  131. "foreignField": "_id",
  132. "as": "craft_docs",
  133. },
  134. },
  135. {
  136. "$match": bson.M{
  137. "craft_docs.0.category": cratf.Category,
  138. },
  139. },
  140. {
  141. "$project": bson.M{
  142. "craft_docs": 0,
  143. "createTime": 0,
  144. "price": 0,
  145. "productId": 0,
  146. "updateTime": 0,
  147. },
  148. },
  149. }
  150. ctx := apictx.CreateRepoCtx()
  151. colls := ctx.Client.GetCollection(repo.CollectionSupplierCraftprice)
  152. findoptions := &options.AggregateOptions{}
  153. cur, err := colls.Aggregate(ctx.Ctx, pipleLine, findoptions)
  154. if err != nil {
  155. return nil, err
  156. }
  157. defer cur.Close(ctx.Ctx)
  158. err = cur.All(ctx.Ctx, &listOut)
  159. if err != nil {
  160. return nil, err
  161. }
  162. if len(listOut) < 1 {
  163. return emtyPage, nil
  164. }
  165. }
  166. if query["productId"] != nil {
  167. processId := query["processId"].(string)
  168. if len(processId) < 1 {
  169. return nil, fmt.Errorf("productId(string)为空")
  170. }
  171. id, _ := primitive.ObjectIDFromHex(processId)
  172. ok, list := repo.RepoSeachDocsMap(apictx.CreateRepoCtx(), &repo.DocsSearchOptions{
  173. CollectName: repo.CollectionSupplierProductprice,
  174. Query: repo.Map{"productId": id},
  175. Project: []string{"supplierId"},
  176. })
  177. if !ok {
  178. return emtyPage, nil
  179. }
  180. listOut = list
  181. }
  182. //获取供应商列表
  183. suppliers := []primitive.ObjectID{}
  184. suppliersMap := map[string]bool{}
  185. for _, item := range listOut {
  186. if item["supplierId"] != nil {
  187. id, ok := item["supplierId"].(primitive.ObjectID)
  188. if !ok {
  189. continue
  190. }
  191. if !suppliersMap[id.Hex()] {
  192. suppliers = append(suppliers, id)
  193. suppliersMap[id.Hex()] = true
  194. }
  195. }
  196. }
  197. if len(suppliers) < 1 {
  198. return emtyPage, nil
  199. }
  200. option := &repo.PageSearchOptions{
  201. CollectName: repo.CollectionSupplier,
  202. Query: repo.Map{"_id": bson.M{"$in": suppliers}},
  203. Page: page,
  204. Size: size,
  205. Sort: bson.M{"createTime": -1},
  206. }
  207. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  208. }
  209. // 更新供应商
  210. func UpdateSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  211. var supplier model.Supplier
  212. err := c.ShouldBindJSON(&supplier)
  213. if err != nil {
  214. return nil, errors.New("参数错误")
  215. }
  216. if supplier.Id.Hex() == "" {
  217. return nil, errors.New("id的为空")
  218. }
  219. supplier.UpdateTime = time.Now()
  220. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionSupplier, supplier.Id.Hex(), &supplier)
  221. }
  222. // 删除供应商
  223. func DelSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  224. supplierId := c.Param("id")
  225. if supplierId == "" {
  226. return nil, errors.New("id为空")
  227. }
  228. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionSupplier, supplierId)
  229. }