supplier-price.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. )
  13. // 供应商价格管理
  14. func SupplierPrice(r *GinRouter) {
  15. // // 创建供应商价格
  16. // r.POST("/supplier/mat/create", CreateSupplierPrice)
  17. // // 获取供应商价格列表
  18. // r.GET("/supplier/mat/list", GetSupplierPrices)
  19. // // 更新供应商价格
  20. // r.POST("/supplier/mat/update", UpdateSupplierPrice)
  21. // // 删除供应商价格
  22. // r.POST("/supplier/mat/delete/:id", DelSupplierPrice)
  23. //材料供应
  24. CreateCRUD(r, "/supplier/mat", &CRUDOption{
  25. Collection: repo.CollectionSupplierMatprice,
  26. NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  27. entity := &model.SupplierPrice{}
  28. c.ShouldBindJSON(entity)
  29. if entity.ProductId == primitive.NilObjectID || entity.SupplierId == primitive.NilObjectID {
  30. return nil, fmt.Errorf("产品ID或供应商Id不能为空")
  31. }
  32. curr := &model.SupplierPrice{}
  33. ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  34. CollectName: repo.CollectionSupplierMatprice,
  35. Query: repo.Map{"productId": entity.ProductId, "supplierId": entity.SupplierId},
  36. Project: []string{"_id"},
  37. }, curr)
  38. if err != nil {
  39. return nil, err
  40. }
  41. if ok {
  42. return nil, fmt.Errorf("该材料已经在供应列表里面了")
  43. }
  44. entity.CreateTime = time.Now()
  45. entity.UpdateTime = time.Now()
  46. return entity, nil
  47. },
  48. EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
  49. return &model.SupplierPrice{}
  50. },
  51. JWT: true,
  52. OnUpdate: func(c *gin.Context, apictx *ApiSession, entity interface{}) {
  53. calc := entity.(*model.SupplierPrice)
  54. calc.UpdateTime = time.Now()
  55. },
  56. SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
  57. if query["supplierId"] != nil {
  58. query["supplierId"], _ = primitive.ObjectIDFromHex(query["supplierId"].(string))
  59. }
  60. if query["productId"] != nil {
  61. query["productId"], _ = primitive.ObjectIDFromHex(query["productId"].(string))
  62. }
  63. return query
  64. },
  65. SearchPostProcess: func(page *repo.PageResult, c *gin.Context, apictx *ApiSession, query map[string]interface{}) (interface{}, error) {
  66. //查询材料对应的材料信息
  67. for _, mat := range page.List {
  68. _id, _ := mat["productId"].(primitive.ObjectID)
  69. matInfo := &model.Material{}
  70. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  71. CollectName: repo.CollectionMaterial,
  72. Query: repo.Map{"_id": _id},
  73. }, matInfo)
  74. mat["matInfo"] = matInfo
  75. }
  76. return page, nil
  77. },
  78. SearchProject: []string{"price", "supplierId", "productId", "updateTime", "_id"},
  79. })
  80. CreateCRUD(r, "/supplier/craft", &CRUDOption{
  81. Collection: repo.CollectionSupplierCraftprice,
  82. NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  83. entity := &model.SupplierPrice{}
  84. c.ShouldBindJSON(entity)
  85. if entity.ProductId == primitive.NilObjectID || entity.SupplierId == primitive.NilObjectID {
  86. return nil, fmt.Errorf("产品ID或供应商Id不能为空")
  87. }
  88. curr := &model.SupplierPrice{}
  89. ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  90. CollectName: repo.CollectionSupplierCraftprice,
  91. Query: repo.Map{"productId": entity.ProductId, "supplierId": entity.SupplierId},
  92. Project: []string{"_id"},
  93. }, curr)
  94. if err != nil {
  95. return nil, err
  96. }
  97. if ok {
  98. return nil, fmt.Errorf("该工艺已经在供应列表里面了")
  99. }
  100. entity.CreateTime = time.Now()
  101. entity.UpdateTime = time.Now()
  102. return entity, nil
  103. },
  104. EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
  105. return &model.SupplierPrice{}
  106. },
  107. JWT: true,
  108. OnUpdate: func(c *gin.Context, apictx *ApiSession, entity interface{}) {
  109. calc := entity.(*model.SupplierPrice)
  110. calc.UpdateTime = time.Now()
  111. },
  112. SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
  113. if query["supplierId"] != nil {
  114. query["supplierId"], _ = primitive.ObjectIDFromHex(query["supplierId"].(string))
  115. }
  116. if query["productId"] != nil {
  117. query["productId"], _ = primitive.ObjectIDFromHex(query["productId"].(string))
  118. }
  119. return query
  120. },
  121. SearchPostProcess: func(page *repo.PageResult, c *gin.Context, apictx *ApiSession, query map[string]interface{}) (interface{}, error) {
  122. //查询材料对应的材料信息
  123. for _, mat := range page.List {
  124. _id, _ := mat["productId"].(primitive.ObjectID)
  125. craft := &model.Craft{}
  126. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  127. CollectName: repo.CollectionCraft,
  128. Query: repo.Map{"_id": _id},
  129. }, craft)
  130. mat["craftInfo"] = craft
  131. }
  132. return page, nil
  133. },
  134. SearchProject: []string{"price", "supplierId", "productId", "updateTime", "_id"},
  135. })
  136. CreateCRUD(r, "/supplier/product", &CRUDOption{
  137. Collection: repo.CollectionSupplierProductprice,
  138. NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  139. entity := &model.SupplierPrice{}
  140. c.ShouldBindJSON(entity)
  141. if entity.ProductId == primitive.NilObjectID || entity.SupplierId == primitive.NilObjectID {
  142. return nil, fmt.Errorf("产品ID或供应商Id不能为空")
  143. }
  144. curr := &model.SupplierPrice{}
  145. ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  146. CollectName: repo.CollectionSupplierProductprice,
  147. Query: repo.Map{"productId": entity.ProductId, "supplierId": entity.SupplierId},
  148. Project: []string{"_id"},
  149. }, curr)
  150. if err != nil {
  151. return nil, err
  152. }
  153. if ok {
  154. return nil, fmt.Errorf("该工序已经在供应列表里面了")
  155. }
  156. entity.CreateTime = time.Now()
  157. entity.UpdateTime = time.Now()
  158. return entity, nil
  159. },
  160. EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
  161. return &model.SupplierPrice{}
  162. },
  163. JWT: true,
  164. OnUpdate: func(c *gin.Context, apictx *ApiSession, entity interface{}) {
  165. calc := entity.(*model.SupplierPrice)
  166. calc.UpdateTime = time.Now()
  167. },
  168. SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
  169. if query["supplierId"] != nil {
  170. query["supplierId"], _ = primitive.ObjectIDFromHex(query["supplierId"].(string))
  171. }
  172. if query["productId"] != nil {
  173. query["productId"], _ = primitive.ObjectIDFromHex(query["productId"].(string))
  174. }
  175. return query
  176. },
  177. SearchPostProcess: func(page *repo.PageResult, c *gin.Context, apictx *ApiSession, query map[string]interface{}) (interface{}, error) {
  178. //查询材料对应的材料信息
  179. for _, ps := range page.List {
  180. _id, _ := ps["productId"].(primitive.ObjectID)
  181. process := &model.Product{}
  182. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  183. CollectName: repo.CollectionProduct,
  184. Query: repo.Map{"_id": _id},
  185. }, process)
  186. ps["processInfo"] = process
  187. }
  188. return page, nil
  189. },
  190. SearchProject: []string{"price", "supplierId", "productId", "updateTime", "_id"},
  191. })
  192. //添加计价方案
  193. SupplierCalcPriceColl := "supplier-calcprice"
  194. CreateCRUD(r, "/supplier/calc", &CRUDOption{
  195. Collection: SupplierCalcPriceColl,
  196. NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  197. entity := &model.SupplierCalcPrice{}
  198. c.ShouldBindJSON(entity)
  199. if entity.SupplierId == primitive.NilObjectID {
  200. return nil, fmt.Errorf("供应商Id不能为空")
  201. }
  202. if entity.Calc == nil || len(entity.Calc.Category) < 1 {
  203. return nil, fmt.Errorf("计价方案参数错误")
  204. }
  205. calc := &model.SupplierCalcPrice{}
  206. ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(),
  207. &repo.DocSearchOptions{CollectName: SupplierCalcPriceColl,
  208. Query: repo.Map{"calc.category": entity.Calc.Category, "supplierId": entity.SupplierId}}, calc)
  209. if err != nil {
  210. return nil, err
  211. }
  212. if ok {
  213. return nil, fmt.Errorf("相同的分类计价方案只能有一个")
  214. }
  215. entity.CreateTime = time.Now()
  216. entity.UpdateTime = time.Now()
  217. return entity, nil
  218. },
  219. EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
  220. return &model.SupplierCalcPrice{}
  221. },
  222. JWT: true,
  223. OnUpdate: func(c *gin.Context, apictx *ApiSession, entity interface{}) {
  224. calc := entity.(*model.SupplierCalcPrice)
  225. calc.UpdateTime = time.Now()
  226. },
  227. SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
  228. if query["supplierId"] != nil {
  229. query["supplierId"], _ = primitive.ObjectIDFromHex(query["supplierId"].(string))
  230. }
  231. return query
  232. },
  233. SearchProject: []string{"calc", "supplierId", "updateTime", "_id"},
  234. })
  235. }
  236. // 创建供应商价格
  237. func CreateSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  238. var supplierprice model.SupplierPrice
  239. err := c.ShouldBindJSON(&supplierprice)
  240. if err != nil {
  241. fmt.Println(err)
  242. return nil, errors.New("参数错误!")
  243. }
  244. ctx := apictx.CreateRepoCtx()
  245. if supplierprice.SupplierId.Hex() == "" {
  246. return nil, errors.New("供应商id为空")
  247. }
  248. // if supplierprice.ProductId.Hex() == "" {
  249. // return nil, errors.New("供应产品id为空")
  250. // }
  251. // if supplierprice.PriceStrategy == nil {
  252. // return nil, errors.New("供应商价格策略为空")
  253. // }
  254. supplierprice.CreateTime = time.Now()
  255. supplierprice.UpdateTime = time.Now()
  256. result, err := repo.RepoAddDoc(ctx, repo.CollectionSupplierPrice, &supplierprice)
  257. return result, err
  258. }
  259. // 获取供应商价格信息
  260. func GetSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  261. supplierpriceId := c.Param("id")
  262. id, err := primitive.ObjectIDFromHex(supplierpriceId)
  263. if err != nil {
  264. return nil, errors.New("非法id")
  265. }
  266. var supplierprice model.SupplierPrice
  267. option := &repo.DocSearchOptions{
  268. CollectName: repo.CollectionSupplierPrice,
  269. Query: repo.Map{"_id": id},
  270. }
  271. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &supplierprice)
  272. if !found || err != nil {
  273. log.Info(err)
  274. return nil, errors.New("数据未找到")
  275. }
  276. return supplierprice, nil
  277. }
  278. // 获取供应商价格列表
  279. func GetSupplierPrices(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  280. page, size, query := UtilQueryPageSize(c)
  281. option := &repo.PageSearchOptions{
  282. CollectName: repo.CollectionSupplierPrice,
  283. Query: query,
  284. Page: page,
  285. Size: size,
  286. Sort: bson.M{"createTime": -1},
  287. }
  288. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  289. }
  290. // 更新供应商价格
  291. func UpdateSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  292. var supplierprice model.SupplierPrice
  293. err := c.ShouldBindJSON(&supplierprice)
  294. if err != nil {
  295. return nil, errors.New("参数错误")
  296. }
  297. if supplierprice.Id.Hex() == "" {
  298. return nil, errors.New("id的为空")
  299. }
  300. supplierprice.UpdateTime = time.Now()
  301. // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionSupplierPrice, supplierprice.Id.Hex(), &supplierprice)
  302. return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionSupplierPrice, supplierprice.Id.Hex(), &supplierprice, &repo.RecordLogReq{
  303. Path: c.Request.URL.Path,
  304. TargetId: supplierprice.Id.Hex(),
  305. })
  306. }
  307. // 删除供应商价格
  308. func DelSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  309. supplierpriceId := c.Param("id")
  310. if supplierpriceId == "" {
  311. return nil, errors.New("id为空")
  312. }
  313. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionSupplierPrice, supplierpriceId)
  314. }