supplier.go 12 KB

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