ProductPresellSkuDao.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\ProductPresellSku;
  14. use think\facade\Db;
  15. class ProductPresellSkuDao extends BaseDao
  16. {
  17. protected function getModel(): string
  18. {
  19. return ProductPresellSku::class;
  20. }
  21. /**
  22. * 清除预售产品的相关数据
  23. *
  24. * 本函数用于根据给定的预售产品ID,从数据库中删除与该预售产品相关的所有数据。
  25. * 这里的相关数据指的是通过预售产品ID可以唯一标识的数据记录。
  26. *
  27. * @param int $id 预售产品的唯一标识ID
  28. */
  29. public function clear($id)
  30. {
  31. // 通过模型获取数据库实例,并基于给定的预售产品ID删除相关数据
  32. $this->getModel()::getDB()->where('product_presell_id', $id)->delete();
  33. }
  34. /**
  35. * 减少指定预售产品的库存并增加销量
  36. *
  37. * 本函数用于处理预售产品的库存逻辑。通过传入预售产品ID和唯一标识符,
  38. * 对对应产品的库存进行减少,同时增加该产品的销量。
  39. * 这种设计适用于预售后期对已预订产品的库存和销量进行精确调整的场景。
  40. *
  41. * @param int $product_presell_id 预售产品的ID,用于定位特定的预售产品。
  42. * @param string $unique 唯一标识符,用于确保操作的准确性,防止重复操作。
  43. * @param int $desc 需要减少的库存数量,同时也是增加的销量数量。
  44. * @return bool 返回操作结果,成功为true,失败为false。
  45. */
  46. public function descStock(int $product_presell_id, string $unique, int $desc)
  47. {
  48. // 使用模型的数据库操作方法,根据预售产品ID和唯一标识符定位到特定的数据行。
  49. // 然后更新该行的库存和销量数据,库存减少,销量增加。
  50. return $this->getModel()::getDB()->where('product_presell_id', $product_presell_id)->where('unique', $unique)->update([
  51. 'stock' => Db::raw('stock-' . $desc), // 直接通过数据库原生表达式减少库存
  52. 'seles' => Db::raw('seles+' . $desc), // 直接通过数据库原生表达式增加销量
  53. ]);
  54. }
  55. /**
  56. * 增加预售产品的库存并减少销量
  57. *
  58. * 本函数用于在数据库中更新指定预售产品的库存和销量。它通过增加库存量和减少销售量来调整产品的状态。
  59. * 主要用于处理预售结束后的库存和销量结算。
  60. *
  61. * @param int $product_presell_id 预售产品的ID,用于定位特定的预售产品
  62. * @param string $unique 预售产品的唯一标识,用于确保操作的准确性
  63. * @param int $desc 库存增加的数量,同时也是销量减少的数量,表示此次操作的规模
  64. * @return bool 更新操作的结果,成功返回true,失败返回false
  65. */
  66. public function incStock(int $product_presell_id, string $unique, int $desc)
  67. {
  68. // 使用模型的数据库操作方法,根据预售产品ID和唯一标识定位到特定的预售产品记录
  69. // 然后更新该记录的库存和销量字段,库存增加,销量减少
  70. $info = $this->getModel()::getDB()->where('product_presell_id', $product_presell_id)->where('unique',$unique)->find();
  71. $info->stock = bcadd($info['stock'], $desc);
  72. $info->seles = max(bcsub($info['seles'], $desc), 0);
  73. return $info->save();
  74. }
  75. /**
  76. * 增加 参与或支付成功 人数
  77. * @param int $product_presell_id
  78. * @param string $unique
  79. * @param string $field
  80. * @return mixed
  81. * @author Qinii
  82. * @day 2020-11-27
  83. */
  84. public function incCount(int $product_presell_id,string $unique,string $field,$inc = 1)
  85. {
  86. return $this->getModel()::getDB()->where('product_presell_id', $product_presell_id)->where('unique', $unique)
  87. ->update([
  88. $field => Db::raw($field.'+' . $inc)
  89. ]);
  90. }
  91. /**
  92. * 减少 参与或支付成功 人数
  93. * @param int $product_presell_id
  94. * @param string $unique
  95. * @param string $field
  96. * @return mixed
  97. * @author Qinii
  98. * @day 2020-11-27
  99. */
  100. public function desCount(int $product_presell_id,string $unique,$inc = 1)
  101. {
  102. $res = $this->getModel()::getDB()->where('product_presell_id', $product_presell_id)->where('unique',$unique)->find();
  103. if($res->presell->presell_type == 1 ){
  104. $res->one_pay = ($res->one_pay > 0) ? $res->one_pay - $inc : 0;
  105. }else{
  106. $res->two_pay = ($res->two_pay > 0) ? $res->two_pay - $inc : 0;
  107. }
  108. return $res->save();
  109. }
  110. }