CdkeyLibraryDao.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\CdkeyLibrary;
  14. use think\facade\Db;
  15. /**
  16. * Class StoreServiceDao
  17. * @package app\common\dao\store\service
  18. * @author xaboy
  19. * 卡密库
  20. */
  21. class CdkeyLibraryDao extends BaseDao
  22. {
  23. /**
  24. * @return string
  25. * @author xaboy
  26. * @day 2020/5/29
  27. */
  28. protected function getModel(): string
  29. {
  30. return CdkeyLibrary::class;
  31. }
  32. /**
  33. * 根据条件搜索数据。
  34. *
  35. * 本函数用于根据提供的条件数组搜索数据库中的记录。它支持多个条件,
  36. * 包括mer_id(商户ID)、product_id(产品ID)和product_attr_value_id(产品属性值ID)。
  37. * 条件是可选的,只有在数组中存在且不为空时才会被应用。
  38. *
  39. * @param array $where 包含搜索条件的数组。
  40. * @param int $is_del 删除状态标志,默认为0表示未删除。
  41. * @return \Illuminate\Database\Query\Builder|static 返回查询构建器实例或静态调用的结果。
  42. */
  43. public function search(array $where, $is_del = 0)
  44. {
  45. // 获取数据库实例
  46. return $this->getModel()::getDB()->alias('CdkeyLibrary')
  47. // 当'mer_id'字段存在且不为空时,添加where条件
  48. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  49. $query->where('mer_id', $where['mer_id']);
  50. })
  51. // 当'product_id'字段存在且不为空时,添加where条件
  52. ->when(isset($where['product_id']) && $where['product_id'] !== '', function ($query) use ($where) {
  53. $query->where('product_id', $where['product_id']);
  54. })
  55. // 当'product_attr_value_id'字段存在且不为空时,添加where条件
  56. ->when(isset($where['product_attr_value_id']) && $where['product_attr_value_id'] !== '', function ($query) use ($where) {
  57. $query->where('product_attr_value_id', $where['product_attr_value_id']);
  58. })
  59. ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  60. $query->where('name', 'like', '%' . $where['keyword'] . '%');
  61. })
  62. ->when(isset($where['name']) && $where['name'] !== '', function ($query) use ($where) {
  63. $query->where('name', 'like', '%' . $where['name'] . '%');
  64. })
  65. ->when(isset($where['productName']) && $where['productName'] !== '', function ($query) use ($where) {
  66. $query->hasWhere('product', function ($query) use ($where) {
  67. $query->where('store_name', 'like', '%' . $where['productName'] . '%');
  68. });
  69. })
  70. // 指定删除状态条件
  71. ->where('CdkeyLibrary.is_del', $is_del);
  72. }
  73. /**
  74. * 统计总 cdkey 数量
  75. * @param $id
  76. * @param $num
  77. * @return void
  78. * @author Qinii
  79. */
  80. public function incTotalNum($id, $num = 1)
  81. {
  82. $this->getModel()::getDB()->where('id', $id)->update([
  83. 'total_num' => Db::raw('total_num+' . $num),
  84. ]);
  85. }
  86. /**
  87. * 减少指定ID的商品或资源的总数
  88. *
  89. * 此方法用于更新数据库中特定ID记录的总数量字段,将其减少指定的数量。
  90. * 主要应用于商品库存管理、资源下载计数等领域,通过减少总量来反映消耗或使用情况。
  91. *
  92. * @param int $id 需要更新数量的记录的ID
  93. * @param int $num 需要减少的数量,默认为1,表示减少一个单位
  94. */
  95. public function decTotalNum($id, $num = 1)
  96. {
  97. // 使用模型的数据库操作方法,根据ID更新记录的total_num字段
  98. // 通过Db::raw直接执行SQL表达式,实现数量的减少
  99. $this->getModel()::getDb()->where('id', $id)->update([
  100. 'total_num' => Db::raw('total_num-' . $num),
  101. ]);
  102. }
  103. public function incUsedNum($id, $num = 1)
  104. {
  105. $this->getModel()::getDb()->where('id', $id)->update([
  106. 'used_num' => Db::raw('used_num+' . $num),
  107. ]);
  108. }
  109. }