supplier.go 7.3 KB

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