supplier.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. )
  13. // 供应商管理
  14. func Supplier(r *GinRouter) {
  15. // 创建供应商
  16. r.POST("/supplier/create", CreateSupplier)
  17. // 获取供应商详情
  18. r.GET("/supplier/detail/:id", GetSupplier)
  19. // 获取供应商列表
  20. r.GET("/supplier/list", GetSuppliers)
  21. // 更新供应商
  22. r.POST("/supplier/update", UpdateSupplier)
  23. // 删除供应商
  24. r.POST("/supplier/delete/:id", DelSupplier)
  25. // 获取供应商列表
  26. r.GET("/plan/supplier/list", GetPlanSuppliers)
  27. }
  28. // 创建供应商
  29. func CreateSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  30. var supplier model.Supplier
  31. err := c.ShouldBindJSON(&supplier)
  32. if err != nil {
  33. fmt.Println(err)
  34. return nil, errors.New("参数错误!")
  35. }
  36. ctx := apictx.CreateRepoCtx()
  37. if supplier.Name == "" {
  38. return nil, errors.New("供应商名为空")
  39. }
  40. if supplier.Address == "" {
  41. return nil, errors.New("供应商地址为空")
  42. }
  43. if supplier.Phone == "" {
  44. return nil, errors.New("供应商联系电话为空")
  45. }
  46. supplier.CreateTime = time.Now()
  47. supplier.UpdateTime = time.Now()
  48. result, err := repo.RepoAddDoc(ctx, repo.CollectionSupplier, &supplier)
  49. return result, err
  50. }
  51. // 获取供应商信息
  52. func GetSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  53. supplierId := c.Param("id")
  54. id, err := primitive.ObjectIDFromHex(supplierId)
  55. if err != nil {
  56. return nil, errors.New("非法id")
  57. }
  58. var supplier model.Supplier
  59. option := &repo.DocSearchOptions{
  60. CollectName: repo.CollectionSupplier,
  61. Query: repo.Map{"_id": id},
  62. }
  63. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &supplier)
  64. if !found || err != nil {
  65. log.Info(err)
  66. return nil, errors.New("数据未找到")
  67. }
  68. return supplier, nil
  69. }
  70. // 获取供应商列表
  71. func GetSuppliers(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  72. page, size, query := UtilQueryPageSize(c)
  73. if _name, ok := query["name"]; ok {
  74. delete(query, "name")
  75. query["name"] = bson.M{"$regex": _name.(string)}
  76. }
  77. if cate, ok := query["category"]; ok {
  78. delete(query, "category")
  79. query["categorys"] = bson.M{"$in": []string{cate.(string)}}
  80. }
  81. option := &repo.PageSearchOptions{
  82. CollectName: repo.CollectionSupplier,
  83. Query: query,
  84. Page: page,
  85. Size: size,
  86. Sort: bson.M{"createTime": -1},
  87. }
  88. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  89. }
  90. func GetPlanSuppliers(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  91. page, size, query := UtilQueryPageSize(c)
  92. emtyPage := &repo.PageResult{
  93. Total: 0,
  94. Size: size,
  95. Page: page,
  96. List: []map[string]interface{}{},
  97. }
  98. listOut := []map[string]interface{}{}
  99. flag := false
  100. if query["matId"] != nil || query["craftId"] != nil || query["productId"] != nil {
  101. flag = true
  102. }
  103. filtter := repo.Map{}
  104. if _name, ok := query["name"]; ok {
  105. filtter["name"] = bson.M{"$regex": _name.(string)}
  106. }
  107. if cate, ok := query["category"]; ok {
  108. filtter["categorys"] = bson.M{"$in": []string{cate.(string)}}
  109. }
  110. if !flag {
  111. option := &repo.PageSearchOptions{
  112. CollectName: repo.CollectionSupplier,
  113. // Query: repo.Map{"categorys": bson.M{"$in": []string{cate.(string)}}},
  114. Query: filtter,
  115. Page: page,
  116. Size: size,
  117. Sort: bson.M{"createTime": -1},
  118. }
  119. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  120. }
  121. //category =>根据内容查询 供应对应内容的供应商
  122. if query["matId"] != nil {
  123. matId := query["matId"].(string)
  124. if len(matId) < 1 {
  125. return nil, fmt.Errorf("matId(string)为空")
  126. }
  127. id, _ := primitive.ObjectIDFromHex(matId)
  128. ok, list := repo.RepoSeachDocsMap(apictx.CreateRepoCtx(), &repo.DocsSearchOptions{
  129. CollectName: repo.CollectionSupplierMatprice,
  130. Query: repo.Map{"productId": id},
  131. Project: []string{"supplierId"},
  132. })
  133. if !ok {
  134. return emtyPage, nil
  135. }
  136. listOut = list
  137. }
  138. if query["craftId"] != nil {
  139. // cratf := &model.Craft{}
  140. // ok, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  141. // Query: repo.Map{"_id": query["craftId"].(string)},
  142. // CollectName: repo.CollectionCraft,
  143. // }, cratf)
  144. // if !ok {
  145. // return nil, fmt.Errorf("没有对应的工艺信息")
  146. // }
  147. // //查询工艺分类
  148. // pipleLine := []bson.M{
  149. // {
  150. // "$lookup": bson.M{
  151. // "from": repo.CollectionCraft,
  152. // "localField": "productId",
  153. // "foreignField": "_id",
  154. // "as": "craft_docs",
  155. // },
  156. // },
  157. // {
  158. // "$match": bson.M{
  159. // "craft_docs.0.category": cratf.Category,
  160. // },
  161. // },
  162. // {
  163. // "$project": bson.M{
  164. // "craft_docs": 0,
  165. // "createTime": 0,
  166. // "price": 0,
  167. // "productId": 0,
  168. // "updateTime": 0,
  169. // },
  170. // },
  171. // }
  172. // ctx := apictx.CreateRepoCtx()
  173. // colls := ctx.Client.GetCollection(repo.CollectionSupplierCraftprice)
  174. // findoptions := &options.AggregateOptions{}
  175. // cur, err := colls.Aggregate(ctx.Ctx, pipleLine, findoptions)
  176. // if err != nil {
  177. // return nil, err
  178. // }
  179. // defer cur.Close(ctx.Ctx)
  180. // err = cur.All(ctx.Ctx, &listOut)
  181. // if err != nil {
  182. // return nil, err
  183. // }
  184. // if len(listOut) < 1 {
  185. // return emtyPage, nil
  186. // }
  187. // return listOut, nil
  188. cratfId := query["cratfId"].(string)
  189. if len(cratfId) < 1 {
  190. return nil, fmt.Errorf("cratfId(string)为空")
  191. }
  192. id, _ := primitive.ObjectIDFromHex(cratfId)
  193. ok, list := repo.RepoSeachDocsMap(apictx.CreateRepoCtx(), &repo.DocsSearchOptions{
  194. CollectName: repo.CollectionSupplierCraftprice,
  195. Query: repo.Map{"cratfId": id},
  196. Project: []string{"supplierId"},
  197. })
  198. if !ok {
  199. return emtyPage, nil
  200. }
  201. listOut = list
  202. }
  203. if query["productId"] != nil {
  204. productId := query["productId"].(string)
  205. if len(productId) < 1 {
  206. return nil, fmt.Errorf("productId(string)为空")
  207. }
  208. id, _ := primitive.ObjectIDFromHex(productId)
  209. ok, list := repo.RepoSeachDocsMap(apictx.CreateRepoCtx(), &repo.DocsSearchOptions{
  210. CollectName: repo.CollectionSupplierProductprice,
  211. Query: repo.Map{"productId": id},
  212. Project: []string{"supplierId"},
  213. })
  214. if !ok {
  215. return emtyPage, nil
  216. }
  217. listOut = list
  218. }
  219. //获取供应商列表
  220. suppliers := []primitive.ObjectID{}
  221. suppliersMap := map[string]bool{}
  222. for _, item := range listOut {
  223. if item["supplierId"] != nil {
  224. id, ok := item["supplierId"].(primitive.ObjectID)
  225. if !ok {
  226. continue
  227. }
  228. if !suppliersMap[id.Hex()] {
  229. suppliers = append(suppliers, id)
  230. suppliersMap[id.Hex()] = true
  231. }
  232. }
  233. }
  234. if len(suppliers) < 1 {
  235. return emtyPage, nil
  236. }
  237. filtter["_id"] = bson.M{"$in": suppliers}
  238. option := &repo.PageSearchOptions{
  239. CollectName: repo.CollectionSupplier,
  240. Query: filtter,
  241. // Query: repo.Map{"_id": bson.M{"$in": suppliers}},
  242. Page: page,
  243. Size: size,
  244. Sort: bson.M{"createTime": -1},
  245. }
  246. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  247. }
  248. // 更新供应商
  249. func UpdateSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  250. var supplier model.Supplier
  251. err := c.ShouldBindJSON(&supplier)
  252. if err != nil {
  253. return nil, errors.New("参数错误")
  254. }
  255. if supplier.Id.Hex() == "" {
  256. return nil, errors.New("id的为空")
  257. }
  258. supplier.UpdateTime = time.Now()
  259. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionSupplier, supplier.Id.Hex(), &supplier)
  260. }
  261. // 删除供应商
  262. func DelSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  263. supplierId := c.Param("id")
  264. if supplierId == "" {
  265. return nil, errors.New("id为空")
  266. }
  267. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionSupplier, supplierId)
  268. }