supplier.go 6.3 KB

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