user-order-goods.service.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { DataSource } from 'typeorm';
  2. import { OrderGoods } from '../entities/order-goods.entity';
  3. import { GenericCrudService } from '@d8d/shared-crud';
  4. export class UserOrderGoodsService extends GenericCrudService<OrderGoods> {
  5. constructor(dataSource: DataSource) {
  6. super(dataSource, OrderGoods, {
  7. userTracking: {
  8. createdByField: 'createdBy',
  9. updatedByField: 'updatedBy'
  10. }
  11. });
  12. }
  13. /**
  14. * 获取当前用户的订单商品列表
  15. */
  16. async getUserOrderGoodsList(
  17. page: number = 1,
  18. pageSize: number = 10,
  19. keyword?: string,
  20. userId?: string | number
  21. ): Promise<[OrderGoods[], number]> {
  22. const skip = (page - 1) * pageSize;
  23. const query = this.repository.createQueryBuilder('orderGoods')
  24. .leftJoinAndSelect('orderGoods.order', 'order')
  25. .leftJoinAndSelect('orderGoods.goods', 'goods')
  26. .leftJoinAndSelect('orderGoods.supplier', 'supplier')
  27. .leftJoinAndSelect('orderGoods.imageFile', 'imageFile');
  28. // 数据权限过滤:只返回当前用户订单的商品
  29. if (userId) {
  30. query.andWhere('order.userId = :userId', { userId });
  31. }
  32. // 关键词搜索
  33. if (keyword) {
  34. query.andWhere('(orderGoods.orderNo LIKE :keyword OR orderGoods.goodsName LIKE :keyword)', {
  35. keyword: `%${keyword}%`
  36. });
  37. }
  38. query.skip(skip).take(pageSize).orderBy('orderGoods.id', 'DESC');
  39. return query.getManyAndCount();
  40. }
  41. /**
  42. * 获取当前用户的订单商品详情
  43. */
  44. async getUserOrderGoodsById(id: number, userId?: string | number): Promise<OrderGoods | null> {
  45. const orderGoods = await this.repository.findOne({
  46. where: { id },
  47. relations: ['order', 'goods', 'supplier', 'imageFile']
  48. });
  49. if (!orderGoods) {
  50. return null;
  51. }
  52. // 数据权限验证:检查订单是否属于当前用户
  53. if (userId && orderGoods.order && orderGoods.order.userId !== userId) {
  54. throw new Error('无权访问该订单商品');
  55. }
  56. return orderGoods;
  57. }
  58. /**
  59. * 为当前用户创建订单商品
  60. */
  61. async createUserOrderGoods(data: Partial<OrderGoods>, userId?: string | number): Promise<OrderGoods> {
  62. // 验证订单是否属于当前用户
  63. if (userId && data.orderId) {
  64. const orderRepository = this.dataSource.getRepository('Order');
  65. const order = await orderRepository.findOne({
  66. where: { id: data.orderId },
  67. select: ['userId']
  68. });
  69. if (!order || order.userId !== userId) {
  70. throw new Error('无权为该订单创建商品');
  71. }
  72. }
  73. return this.create(data, userId);
  74. }
  75. /**
  76. * 更新当前用户的订单商品
  77. */
  78. async updateUserOrderGoods(id: number, data: Partial<OrderGoods>, userId?: string | number): Promise<OrderGoods | null> {
  79. // 先验证权限
  80. const existing = await this.getUserOrderGoodsById(id, userId);
  81. if (!existing) {
  82. return null;
  83. }
  84. return this.update(id, data, userId);
  85. }
  86. /**
  87. * 删除当前用户的订单商品
  88. */
  89. async deleteUserOrderGoods(id: number, userId?: string | number): Promise<boolean> {
  90. // 先验证权限
  91. const existing = await this.getUserOrderGoodsById(id, userId);
  92. if (!existing) {
  93. return false;
  94. }
  95. return this.delete(id, userId);
  96. }
  97. }