supplier.go 11 KB

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