ProductAssistSkuDao.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\store\product;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\store\product\ProductAssistSku;
  14. use think\facade\Db;
  15. class ProductAssistSkuDao extends BaseDao
  16. {
  17. protected function getModel(): string
  18. {
  19. return ProductAssistSku::class;
  20. }
  21. /**
  22. * 清除关联数据
  23. *
  24. * 本函数用于根据指定的ID删除数据库中与产品辅助表相关的记录。
  25. * 它不返回任何值,而是通过调用数据库操作来直接影响数据库。
  26. *
  27. * @param int $id 需要删除的数据的ID。这个ID用于在数据库查询中定位特定的记录。
  28. */
  29. public function clear($id)
  30. {
  31. // 通过模型获取数据库实例,并使用where子句指定product_assist_id为$id,然后执行删除操作。
  32. $this->getModel()::getDB()->where('product_assist_id',$id)->delete();
  33. }
  34. /**
  35. * 减少商品辅助项的库存
  36. *
  37. * 本函数用于更新数据库中特定商品辅助项的库存数量。
  38. * 它通过传入的商品辅助项ID、唯一标识和减少的数量来定位特定的库存记录,
  39. * 并将库存数量减少指定的数值。
  40. *
  41. * @param int $product_assist_id 商品辅助项ID,用于定位特定的商品辅助项。
  42. * @param string $unique 唯一标识,与商品辅助项ID配合使用,确保更新操作的准确性。
  43. * @param int $desc 库存减少的数量,一个正整数,表示库存将减少的量。
  44. * @return bool 更新操作的结果,成功返回true,失败返回false。
  45. */
  46. public function descStock(int $product_assist_id, string $unique, int $desc)
  47. {
  48. // 使用模型的数据库操作方法,通过指定的条件更新库存字段
  49. // 使用Db::raw处理库存的数学运算,确保库存是减去指定的数量
  50. return $this->getModel()::getDB()->where('product_assist_id', $product_assist_id)->where('unique', $unique)->update([
  51. 'stock' => Db::raw('stock-' . $desc)
  52. ]);
  53. }
  54. /**
  55. * 增加辅助商品库存
  56. *
  57. * 本函数用于更新指定辅助商品的库存数量。通过传入辅助商品ID和唯一标识符,
  58. * 以及要增加的库存数量,来实现库存的动态调整。此功能特别适用于需要对商品
  59. * 库存进行精确控制的场景,例如库存管理系统的后台操作。
  60. *
  61. * @param int $product_assist_id 辅助商品的唯一标识ID,用于定位特定的商品。
  62. * @param string $unique 商品的唯一标识字符串,用于进一步确保操作的准确性。
  63. * @param int $desc 要增加的库存数量,以整数形式表示。
  64. * @return bool 更新操作的结果,成功返回true,失败返回false。
  65. */
  66. public function incStock(int $product_assist_id, string $unique, int $desc)
  67. {
  68. // 使用模型获取数据库实例,并通过where子句指定更新条件,然后更新库存字段
  69. return $this->getModel()::getDB()->where('product_assist_id', $product_assist_id)->where('unique', $unique)->update([
  70. 'stock' => Db::raw('stock+' . $desc) // 直接对库存字段进行数学运算,以增加库存
  71. ]);
  72. }
  73. }