supplier.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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"
  13. "go.mongodb.org/mongo-driver/mongo/options"
  14. )
  15. // 供应商管理
  16. func Supplier(r *GinRouter) {
  17. // 创建供应商
  18. r.POST("/supplier/create", CreateSupplier)
  19. // 获取供应商详情
  20. r.GET("/supplier/detail/:id", GetSupplier)
  21. // 获取供应商列表
  22. r.GET("/supplier/list", GetSuppliers)
  23. // 更新供应商
  24. r.POST("/supplier/update", UpdateSupplier)
  25. // 删除供应商
  26. r.POST("/supplier/delete/:id", DelSupplier)
  27. // 获取供应商列表
  28. r.GET("/plan/supplier/list", GetPlanSuppliers)
  29. // 供应商获取自己的单据列表
  30. r.GETJWT("/supplier/bill/list", SupplierBillList)
  31. // 供应商接单
  32. r.POSTJWT("/supplier/bill/ack", SupplierBillAck)
  33. // 单据分配给供应商
  34. r.GET("/supplier/bill/alloc", SupplierBillAlloc)
  35. }
  36. const (
  37. PURCHASE_BILL_TYPE = "purchase"
  38. PRODUCE_BILL_TYPE = "produce"
  39. PRODUCT_BILL_TYPE = "product"
  40. )
  41. type SupplierSmsTempInfo struct {
  42. Product string // 产品名+数量
  43. SerialNumber string
  44. Phone string
  45. }
  46. func genSupplierSmsTemp(billId primitive.ObjectID, billType string, apictx *ApiSession) (*SupplierSmsTempInfo, error) {
  47. productName := ""
  48. supplierId := primitive.NilObjectID
  49. serialNumber := ""
  50. if billType == PURCHASE_BILL_TYPE {
  51. purchase := &model.PurchaseBill{}
  52. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  53. CollectName: repo.CollectionBillPurchase,
  54. Query: repo.Map{"_id": billId},
  55. Project: []string{"productName", "supplierId", "serialNumber", "isAck"},
  56. }, purchase)
  57. if !found || err != nil {
  58. return nil, errors.New("未找到该订单")
  59. }
  60. // 已经接单不发送提醒
  61. if *purchase.IsAck {
  62. return nil, errors.New("该供应商已经接单")
  63. }
  64. serialNumber = purchase.SerialNumber
  65. productName = purchase.ProductName
  66. supplierId = purchase.SupplierId
  67. }
  68. if billType == PRODUCE_BILL_TYPE {
  69. produce := &model.ProduceBill{}
  70. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  71. CollectName: repo.CollectionBillProduce,
  72. Query: repo.Map{"_id": billId},
  73. Project: []string{"productName", "supplierId", "serialNumber", "isAck"},
  74. }, produce)
  75. if !found || err != nil {
  76. return nil, errors.New("未找到该订单")
  77. }
  78. // 已经接单不发送提醒
  79. if *produce.IsAck {
  80. return nil, errors.New("该供应商已经接单")
  81. }
  82. serialNumber = produce.SerialNumber
  83. productName = produce.ProductName
  84. supplierId = produce.SupplierId
  85. }
  86. if billType == PRODUCT_BILL_TYPE {
  87. product := &model.ProductBill{}
  88. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  89. CollectName: repo.CollectionBillProduct,
  90. Query: repo.Map{"_id": billId},
  91. Project: []string{"productName", "supplierId", "serialNumber", "isAck"},
  92. }, product)
  93. if !found || err != nil {
  94. return nil, errors.New("未找到该订单")
  95. }
  96. // 已经接单不发送提醒
  97. if *product.IsAck {
  98. return nil, errors.New("该供应商已经接单")
  99. }
  100. serialNumber = product.SerialNumber
  101. productName = product.ProductName
  102. supplierId = product.SupplierId
  103. }
  104. // 查询供应商信息
  105. user, err := getUserById(apictx, supplierId)
  106. if user == nil || err != nil {
  107. return nil, errors.New("未找到该供应商信息")
  108. }
  109. if len(user.Phone) != 11 {
  110. return nil, errors.New("手机号信息错误")
  111. }
  112. return &SupplierSmsTempInfo{
  113. Product: productName,
  114. SerialNumber: serialNumber,
  115. Phone: user.Phone,
  116. }, nil
  117. }
  118. // 把订单分配给供应商
  119. // purchase produce product
  120. // id为订单id
  121. // /supplier/bill/alloc?id=xxx&type=purchase
  122. func SupplierBillAlloc(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  123. billId, _ := primitive.ObjectIDFromHex(c.Query("id"))
  124. if billId.IsZero() {
  125. return nil, errors.New("订单id不正确")
  126. }
  127. billType := c.Query("type")
  128. billTypes := []string{"purchase", "produce", "product"}
  129. flagType := false
  130. for _, bt := range billTypes {
  131. if bt == billType {
  132. flagType = true
  133. break
  134. }
  135. }
  136. if !flagType {
  137. return nil, errors.New("订单类型错误")
  138. }
  139. result := &mongo.UpdateResult{}
  140. var err error
  141. switch billType {
  142. case PURCHASE_BILL_TYPE:
  143. result, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, billId.Hex(), &model.PurchaseBill{IsSend: true, SendTime: time.Now()})
  144. case PRODUCE_BILL_TYPE:
  145. result, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, billId.Hex(), &model.ProduceBill{IsSend: true, SendTime: time.Now()})
  146. case PRODUCT_BILL_TYPE:
  147. result, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, billId.Hex(), &model.ProductBill{IsSend: true, SendTime: time.Now()})
  148. default:
  149. return result, nil
  150. }
  151. if err != nil {
  152. // 给供应商发送通知短信
  153. smsInfo, err := genSupplierSmsTemp(billId, billType, apictx)
  154. fmt.Println(err)
  155. if err != nil {
  156. err = SendSmsNotify(smsInfo.Phone, &SupplierSmsReq{smsInfo.Product, smsInfo.SerialNumber})
  157. fmt.Println(err)
  158. log.Error(err)
  159. }
  160. }
  161. return result, err
  162. }
  163. // 供应商-接单
  164. // purchase produce product
  165. // POST /supplier/bill/ack
  166. // {"id":xxxx,"type":"purchase"}
  167. type SupplierBillAckReq struct {
  168. Type string
  169. Id primitive.ObjectID
  170. }
  171. func SupplierBillAck(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  172. userId, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
  173. form := SupplierBillAckReq{}
  174. err := c.ShouldBindJSON(&form)
  175. if err != nil {
  176. return nil, errors.New("参数错误")
  177. }
  178. billType := form.Type
  179. id := form.Id
  180. _id := form.Id.Hex()
  181. if id.IsZero() {
  182. return nil, errors.New("id为空")
  183. }
  184. if userId.IsZero() {
  185. return nil, errors.New("非法用户")
  186. }
  187. // purchase produce product
  188. billTypes := []string{"purchase", "produce", "product"}
  189. flagType := false
  190. for _, bt := range billTypes {
  191. if bt == billType {
  192. flagType = true
  193. break
  194. }
  195. }
  196. if !flagType {
  197. return nil, errors.New("订单类型错误")
  198. }
  199. isAck := true
  200. switch billType {
  201. case "purchase":
  202. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, _id, &model.PurchaseBill{IsAck: &isAck, AckTime: time.Now()})
  203. case "produce":
  204. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, _id, &model.ProduceBill{IsAck: &isAck, AckTime: time.Now()})
  205. case "product":
  206. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, _id, &model.ProductBill{IsAck: &isAck, AckTime: time.Now()})
  207. default:
  208. return nil, errors.New("更新类型错误")
  209. }
  210. }
  211. // 供应商-订单列表
  212. // purchase produce product
  213. // /supplier/bill/list?type=purchase&query={"status":"created"}
  214. func SupplierBillList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  215. userId, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
  216. billType := c.Query("type")
  217. page, size, query := UtilQueryPageSize(c)
  218. if userId.IsZero() {
  219. return nil, errors.New("非法用户")
  220. }
  221. // purchase produce product
  222. billTypes := []string{"purchase", "produce", "product"}
  223. flagType := false
  224. for _, bt := range billTypes {
  225. if bt == billType {
  226. flagType = true
  227. break
  228. }
  229. }
  230. if !flagType {
  231. return nil, errors.New("订单类型错误")
  232. }
  233. query["supplierId"] = userId
  234. query["isSend"] = true
  235. collectName := ""
  236. switch billType {
  237. case "purchase":
  238. collectName = repo.CollectionBillPurchase
  239. case "produce":
  240. collectName = repo.CollectionBillProduce
  241. case "product":
  242. collectName = repo.CollectionBillProduct
  243. default:
  244. return []map[string]interface{}{}, nil
  245. }
  246. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  247. CollectName: collectName,
  248. Page: page,
  249. Size: size,
  250. Query: query,
  251. Sort: bson.M{"sendTime": -1},
  252. })
  253. }
  254. // 创建供应商
  255. func CreateSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  256. var supplier model.Supplier
  257. err := c.ShouldBindJSON(&supplier)
  258. if err != nil {
  259. fmt.Println(err)
  260. return nil, errors.New("参数错误!")
  261. }
  262. ctx := apictx.CreateRepoCtx()
  263. if supplier.Name == "" {
  264. return nil, errors.New("供应商名为空")
  265. }
  266. if supplier.Address == "" {
  267. return nil, errors.New("供应商地址为空")
  268. }
  269. if supplier.Phone == "" {
  270. return nil, errors.New("供应商联系电话为空")
  271. }
  272. supplier.CreateTime = time.Now()
  273. supplier.UpdateTime = time.Now()
  274. result, err := repo.RepoAddDoc(ctx, repo.CollectionSupplier, &supplier)
  275. return result, err
  276. }
  277. // 获取供应商信息
  278. func GetSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  279. supplierId := c.Param("id")
  280. id, err := primitive.ObjectIDFromHex(supplierId)
  281. if err != nil {
  282. return nil, errors.New("非法id")
  283. }
  284. var supplier model.Supplier
  285. option := &repo.DocSearchOptions{
  286. CollectName: repo.CollectionSupplier,
  287. Query: repo.Map{"_id": id},
  288. }
  289. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &supplier)
  290. if !found || err != nil {
  291. log.Info(err)
  292. return nil, errors.New("数据未找到")
  293. }
  294. return supplier, nil
  295. }
  296. // 获取供应商列表
  297. func GetSuppliers(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  298. page, size, query := UtilQueryPageSize(c)
  299. if _name, ok := query["name"]; ok {
  300. delete(query, "name")
  301. query["name"] = bson.M{"$regex": _name.(string)}
  302. }
  303. if cate, ok := query["category"]; ok {
  304. delete(query, "category")
  305. query["categorys"] = bson.M{"$in": []string{cate.(string)}}
  306. }
  307. option := &repo.PageSearchOptions{
  308. CollectName: repo.CollectionSupplier,
  309. Query: query,
  310. Page: page,
  311. Size: size,
  312. Sort: bson.M{"createTime": -1},
  313. }
  314. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  315. }
  316. // !暂时弃用
  317. func GetPlanSuppliers(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  318. page, size, query := UtilQueryPageSize(c)
  319. emtyPage := &repo.PageResult{
  320. Total: 0,
  321. Size: size,
  322. Page: page,
  323. List: []map[string]interface{}{},
  324. }
  325. listOut := []map[string]interface{}{}
  326. flag := false
  327. if query["matId"] != nil || query["craftId"] != nil || query["productId"] != nil {
  328. flag = true
  329. }
  330. filtter := repo.Map{}
  331. if _name, ok := query["name"]; ok {
  332. filtter["name"] = bson.M{"$regex": _name.(string)}
  333. }
  334. if cate, ok := query["category"]; ok {
  335. filtter["categorys"] = bson.M{"$in": []string{cate.(string)}}
  336. }
  337. if !flag {
  338. option := &repo.PageSearchOptions{
  339. CollectName: repo.CollectionSupplier,
  340. // Query: repo.Map{"categorys": bson.M{"$in": []string{cate.(string)}}},
  341. Query: filtter,
  342. Page: page,
  343. Size: size,
  344. Sort: bson.M{"createTime": -1},
  345. }
  346. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  347. }
  348. //category =>根据内容查询 供应对应内容的供应商
  349. if query["matId"] != nil {
  350. matId := query["matId"].(string)
  351. if len(matId) < 1 {
  352. return nil, fmt.Errorf("matId(string)为空")
  353. }
  354. id, _ := primitive.ObjectIDFromHex(matId)
  355. ok, list := repo.RepoSeachDocsMap(apictx.CreateRepoCtx(), &repo.DocsSearchOptions{
  356. CollectName: repo.CollectionSupplierMatprice,
  357. Query: repo.Map{"productId": id},
  358. Project: []string{"supplierId"},
  359. })
  360. if !ok {
  361. return emtyPage, nil
  362. }
  363. listOut = list
  364. }
  365. if query["craftId"] != nil {
  366. cratf := &model.Craft{}
  367. ok, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  368. Query: repo.Map{"_id": query["craftId"].(string)},
  369. CollectName: repo.CollectionCraft,
  370. }, cratf)
  371. if !ok {
  372. return nil, fmt.Errorf("没有对应的工艺信息")
  373. }
  374. //查询工艺分类
  375. pipleLine := []bson.M{
  376. {
  377. "$lookup": bson.M{
  378. "from": repo.CollectionCraft,
  379. "localField": "productId",
  380. "foreignField": "_id",
  381. "as": "craft_docs",
  382. },
  383. },
  384. {
  385. "$match": bson.M{
  386. "craft_docs.0.category": cratf.Category,
  387. },
  388. },
  389. {
  390. "$project": bson.M{
  391. "craft_docs": 0,
  392. "createTime": 0,
  393. "price": 0,
  394. "productId": 0,
  395. "updateTime": 0,
  396. },
  397. },
  398. }
  399. ctx := apictx.CreateRepoCtx()
  400. colls := ctx.Client.GetCollection(repo.CollectionSupplierCraftprice)
  401. findoptions := &options.AggregateOptions{}
  402. cur, err := colls.Aggregate(ctx.Ctx, pipleLine, findoptions)
  403. if err != nil {
  404. return nil, err
  405. }
  406. defer cur.Close(ctx.Ctx)
  407. err = cur.All(ctx.Ctx, &listOut)
  408. if err != nil {
  409. return nil, err
  410. }
  411. if len(listOut) < 1 {
  412. return emtyPage, nil
  413. }
  414. return listOut, nil
  415. // cratfId := query["craftId"].(string)
  416. // if len(cratfId) < 1 {
  417. // return nil, fmt.Errorf("cratfId(string)为空")
  418. // }
  419. // id, _ := primitive.ObjectIDFromHex(cratfId)
  420. // ok, list := repo.RepoSeachDocsMap(apictx.CreateRepoCtx(), &repo.DocsSearchOptions{
  421. // CollectName: repo.CollectionSupplierCraftprice,
  422. // Query: repo.Map{"craftId": id},
  423. // Project: []string{"supplierId"},
  424. // })
  425. // if !ok {
  426. // return emtyPage, nil
  427. // }
  428. // listOut = list
  429. }
  430. if query["productId"] != nil {
  431. productId := query["productId"].(string)
  432. if len(productId) < 1 {
  433. return nil, fmt.Errorf("productId(string)为空")
  434. }
  435. id, _ := primitive.ObjectIDFromHex(productId)
  436. ok, list := repo.RepoSeachDocsMap(apictx.CreateRepoCtx(), &repo.DocsSearchOptions{
  437. CollectName: repo.CollectionSupplierProductprice,
  438. Query: repo.Map{"productId": id},
  439. Project: []string{"supplierId"},
  440. })
  441. if !ok {
  442. return emtyPage, nil
  443. }
  444. listOut = list
  445. }
  446. //获取供应商列表
  447. suppliers := []primitive.ObjectID{}
  448. suppliersMap := map[string]bool{}
  449. for _, item := range listOut {
  450. if item["supplierId"] != nil {
  451. id, ok := item["supplierId"].(primitive.ObjectID)
  452. if !ok {
  453. continue
  454. }
  455. if !suppliersMap[id.Hex()] {
  456. suppliers = append(suppliers, id)
  457. suppliersMap[id.Hex()] = true
  458. }
  459. }
  460. }
  461. if len(suppliers) < 1 {
  462. return emtyPage, nil
  463. }
  464. filtter["_id"] = bson.M{"$in": suppliers}
  465. option := &repo.PageSearchOptions{
  466. CollectName: repo.CollectionSupplier,
  467. Query: filtter,
  468. // Query: repo.Map{"_id": bson.M{"$in": suppliers}},
  469. Page: page,
  470. Size: size,
  471. Sort: bson.M{"createTime": -1},
  472. }
  473. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  474. }
  475. // 更新供应商
  476. func UpdateSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  477. var supplier model.Supplier
  478. err := c.ShouldBindJSON(&supplier)
  479. if err != nil {
  480. return nil, errors.New("参数错误")
  481. }
  482. if supplier.Id.Hex() == "" {
  483. return nil, errors.New("id的为空")
  484. }
  485. supplier.UpdateTime = time.Now()
  486. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionSupplier, supplier.Id.Hex(), &supplier)
  487. }
  488. // 删除供应商
  489. func DelSupplier(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  490. supplierId := c.Param("id")
  491. if supplierId == "" {
  492. return nil, errors.New("id为空")
  493. }
  494. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionSupplier, supplierId)
  495. }