supplier.go 10 KB

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