supplier.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. r.GETJWT("/supplier/bill/list", SupplierBillList)
  30. // 单据分配给供应商
  31. r.GET("/supplier/bill/alloc", SupplierBillAlloc)
  32. }
  33. // 把订单分配给供应商
  34. // purchase produce product
  35. // id为订单id
  36. // /supplier/bill/alloc?id=xxx&type=purchase
  37. func SupplierBillAlloc(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  38. billId, _ := primitive.ObjectIDFromHex(c.Query("id"))
  39. if billId.IsZero() {
  40. return nil, errors.New("订单id不正确")
  41. }
  42. billType := c.Query("type")
  43. billTypes := []string{"purchase", "produce", "product"}
  44. flagType := false
  45. for _, bt := range billTypes {
  46. if bt == billType {
  47. flagType = true
  48. break
  49. }
  50. }
  51. if !flagType {
  52. return nil, errors.New("订单类型错误")
  53. }
  54. switch billType {
  55. case "purchase":
  56. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, billId.Hex(), &model.PurchaseBill{IsSend: true, SendTime: time.Now()})
  57. case "produce":
  58. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, billId.Hex(), &model.ProduceBill{IsSend: true, SendTime: time.Now()})
  59. case "product":
  60. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, billId.Hex(), &model.ProductBill{IsSend: true, SendTime: time.Now()})
  61. default:
  62. return true, nil
  63. }
  64. }
  65. // 供应商-订单列表
  66. // purchase produce product
  67. // /supplier/bill/list?type=purchase&query={"status":"created"}
  68. func SupplierBillList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  69. userId, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
  70. billType := c.Query("type")
  71. page, size, query := UtilQueryPageSize(c)
  72. if userId.IsZero() {
  73. return nil, errors.New("非法用户")
  74. }
  75. // purchase produce product
  76. billTypes := []string{"purchase", "produce", "product"}
  77. flagType := false
  78. for _, bt := range billTypes {
  79. if bt == billType {
  80. flagType = true
  81. break
  82. }
  83. }
  84. if !flagType {
  85. return nil, errors.New("订单类型错误")
  86. }
  87. query["supplierId"] = userId
  88. query["isSend"] = true
  89. collectName := ""
  90. switch billType {
  91. case "purchase":
  92. collectName = repo.CollectionBillPurchase
  93. case "produce":
  94. collectName = repo.CollectionBillProduce
  95. case "product":
  96. collectName = repo.CollectionBillProduct
  97. default:
  98. return []map[string]interface{}{}, nil
  99. }
  100. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  101. CollectName: collectName,
  102. Page: page,
  103. Size: size,
  104. Query: query,
  105. Sort: repo.Map{"sendTime": 1},
  106. })
  107. }
  108. // 创建供应商
  109. func CreateSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  110. var supplier model.Supplier
  111. err := c.ShouldBindJSON(&supplier)
  112. if err != nil {
  113. fmt.Println(err)
  114. return nil, errors.New("参数错误!")
  115. }
  116. ctx := apictx.CreateRepoCtx()
  117. if supplier.Name == "" {
  118. return nil, errors.New("供应商名为空")
  119. }
  120. if supplier.Address == "" {
  121. return nil, errors.New("供应商地址为空")
  122. }
  123. if supplier.Phone == "" {
  124. return nil, errors.New("供应商联系电话为空")
  125. }
  126. supplier.CreateTime = time.Now()
  127. supplier.UpdateTime = time.Now()
  128. result, err := repo.RepoAddDoc(ctx, repo.CollectionSupplier, &supplier)
  129. return result, err
  130. }
  131. // 获取供应商信息
  132. func GetSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  133. supplierId := c.Param("id")
  134. id, err := primitive.ObjectIDFromHex(supplierId)
  135. if err != nil {
  136. return nil, errors.New("非法id")
  137. }
  138. var supplier model.Supplier
  139. option := &repo.DocSearchOptions{
  140. CollectName: repo.CollectionSupplier,
  141. Query: repo.Map{"_id": id},
  142. }
  143. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &supplier)
  144. if !found || err != nil {
  145. log.Info(err)
  146. return nil, errors.New("数据未找到")
  147. }
  148. return supplier, nil
  149. }
  150. // 获取供应商列表
  151. func GetSuppliers(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  152. page, size, query := UtilQueryPageSize(c)
  153. if _name, ok := query["name"]; ok {
  154. delete(query, "name")
  155. query["name"] = bson.M{"$regex": _name.(string)}
  156. }
  157. if cate, ok := query["category"]; ok {
  158. delete(query, "category")
  159. query["categorys"] = bson.M{"$in": []string{cate.(string)}}
  160. }
  161. option := &repo.PageSearchOptions{
  162. CollectName: repo.CollectionSupplier,
  163. Query: query,
  164. Page: page,
  165. Size: size,
  166. Sort: bson.M{"createTime": -1},
  167. }
  168. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  169. }
  170. // !暂时弃用
  171. func GetPlanSuppliers(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  172. page, size, query := UtilQueryPageSize(c)
  173. emtyPage := &repo.PageResult{
  174. Total: 0,
  175. Size: size,
  176. Page: page,
  177. List: []map[string]interface{}{},
  178. }
  179. listOut := []map[string]interface{}{}
  180. flag := false
  181. if query["matId"] != nil || query["craftId"] != nil || query["productId"] != nil {
  182. flag = true
  183. }
  184. filtter := repo.Map{}
  185. if _name, ok := query["name"]; ok {
  186. filtter["name"] = bson.M{"$regex": _name.(string)}
  187. }
  188. if cate, ok := query["category"]; ok {
  189. filtter["categorys"] = bson.M{"$in": []string{cate.(string)}}
  190. }
  191. if !flag {
  192. option := &repo.PageSearchOptions{
  193. CollectName: repo.CollectionSupplier,
  194. // Query: repo.Map{"categorys": bson.M{"$in": []string{cate.(string)}}},
  195. Query: filtter,
  196. Page: page,
  197. Size: size,
  198. Sort: bson.M{"createTime": -1},
  199. }
  200. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  201. }
  202. //category =>根据内容查询 供应对应内容的供应商
  203. if query["matId"] != nil {
  204. matId := query["matId"].(string)
  205. if len(matId) < 1 {
  206. return nil, fmt.Errorf("matId(string)为空")
  207. }
  208. id, _ := primitive.ObjectIDFromHex(matId)
  209. ok, list := repo.RepoSeachDocsMap(apictx.CreateRepoCtx(), &repo.DocsSearchOptions{
  210. CollectName: repo.CollectionSupplierMatprice,
  211. Query: repo.Map{"productId": id},
  212. Project: []string{"supplierId"},
  213. })
  214. if !ok {
  215. return emtyPage, nil
  216. }
  217. listOut = list
  218. }
  219. if query["craftId"] != nil {
  220. cratf := &model.Craft{}
  221. ok, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  222. Query: repo.Map{"_id": query["craftId"].(string)},
  223. CollectName: repo.CollectionCraft,
  224. }, cratf)
  225. if !ok {
  226. return nil, fmt.Errorf("没有对应的工艺信息")
  227. }
  228. //查询工艺分类
  229. pipleLine := []bson.M{
  230. {
  231. "$lookup": bson.M{
  232. "from": repo.CollectionCraft,
  233. "localField": "productId",
  234. "foreignField": "_id",
  235. "as": "craft_docs",
  236. },
  237. },
  238. {
  239. "$match": bson.M{
  240. "craft_docs.0.category": cratf.Category,
  241. },
  242. },
  243. {
  244. "$project": bson.M{
  245. "craft_docs": 0,
  246. "createTime": 0,
  247. "price": 0,
  248. "productId": 0,
  249. "updateTime": 0,
  250. },
  251. },
  252. }
  253. ctx := apictx.CreateRepoCtx()
  254. colls := ctx.Client.GetCollection(repo.CollectionSupplierCraftprice)
  255. findoptions := &options.AggregateOptions{}
  256. cur, err := colls.Aggregate(ctx.Ctx, pipleLine, findoptions)
  257. if err != nil {
  258. return nil, err
  259. }
  260. defer cur.Close(ctx.Ctx)
  261. err = cur.All(ctx.Ctx, &listOut)
  262. if err != nil {
  263. return nil, err
  264. }
  265. if len(listOut) < 1 {
  266. return emtyPage, nil
  267. }
  268. return listOut, nil
  269. // cratfId := query["craftId"].(string)
  270. // if len(cratfId) < 1 {
  271. // return nil, fmt.Errorf("cratfId(string)为空")
  272. // }
  273. // id, _ := primitive.ObjectIDFromHex(cratfId)
  274. // ok, list := repo.RepoSeachDocsMap(apictx.CreateRepoCtx(), &repo.DocsSearchOptions{
  275. // CollectName: repo.CollectionSupplierCraftprice,
  276. // Query: repo.Map{"craftId": id},
  277. // Project: []string{"supplierId"},
  278. // })
  279. // if !ok {
  280. // return emtyPage, nil
  281. // }
  282. // listOut = list
  283. }
  284. if query["productId"] != nil {
  285. productId := query["productId"].(string)
  286. if len(productId) < 1 {
  287. return nil, fmt.Errorf("productId(string)为空")
  288. }
  289. id, _ := primitive.ObjectIDFromHex(productId)
  290. ok, list := repo.RepoSeachDocsMap(apictx.CreateRepoCtx(), &repo.DocsSearchOptions{
  291. CollectName: repo.CollectionSupplierProductprice,
  292. Query: repo.Map{"productId": id},
  293. Project: []string{"supplierId"},
  294. })
  295. if !ok {
  296. return emtyPage, nil
  297. }
  298. listOut = list
  299. }
  300. //获取供应商列表
  301. suppliers := []primitive.ObjectID{}
  302. suppliersMap := map[string]bool{}
  303. for _, item := range listOut {
  304. if item["supplierId"] != nil {
  305. id, ok := item["supplierId"].(primitive.ObjectID)
  306. if !ok {
  307. continue
  308. }
  309. if !suppliersMap[id.Hex()] {
  310. suppliers = append(suppliers, id)
  311. suppliersMap[id.Hex()] = true
  312. }
  313. }
  314. }
  315. if len(suppliers) < 1 {
  316. return emtyPage, nil
  317. }
  318. filtter["_id"] = bson.M{"$in": suppliers}
  319. option := &repo.PageSearchOptions{
  320. CollectName: repo.CollectionSupplier,
  321. Query: filtter,
  322. // Query: repo.Map{"_id": bson.M{"$in": suppliers}},
  323. Page: page,
  324. Size: size,
  325. Sort: bson.M{"createTime": -1},
  326. }
  327. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  328. }
  329. // 更新供应商
  330. func UpdateSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  331. var supplier model.Supplier
  332. err := c.ShouldBindJSON(&supplier)
  333. if err != nil {
  334. return nil, errors.New("参数错误")
  335. }
  336. if supplier.Id.Hex() == "" {
  337. return nil, errors.New("id的为空")
  338. }
  339. supplier.UpdateTime = time.Now()
  340. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionSupplier, supplier.Id.Hex(), &supplier)
  341. }
  342. // 删除供应商
  343. func DelSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  344. supplierId := c.Param("id")
  345. if supplierId == "" {
  346. return nil, errors.New("id为空")
  347. }
  348. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionSupplier, supplierId)
  349. }