supplier.go 7.0 KB

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