supplier.go 6.3 KB

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