supplier.go 7.6 KB

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