supplier.go 7.4 KB

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