StoreActivityDao.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\store\StoreActivity;
  15. use app\common\repositories\system\RelevanceRepository;
  16. use think\exception\ValidateException;
  17. /**
  18. *
  19. * Class StoreActivityDao
  20. * @package app\common\dao\system\merchant
  21. */
  22. class StoreActivityDao extends BaseDao
  23. {
  24. protected function getModel(): string
  25. {
  26. return StoreActivity::class;
  27. }
  28. public function search(array $where = [], array $with = [])
  29. {
  30. $where['is_del'] = 0;
  31. return $this->getSearch($where)->when(!empty($with), function ($query) use ($with) {
  32. $query->with($with);
  33. });
  34. }
  35. /**
  36. * 增加指定ID的数据的total字段值。
  37. * 此函数主要用于对数据库中的某些记录进行总量控制和更新,确保数据的正确性和一致性。
  38. *
  39. * @param int $id 需要更新的记录的ID。
  40. * @param int $inc 需要增加的值,默认为1,表示增加1。
  41. * @throws ValidateException 如果找不到相关数据或更新后的总量超过了预设的限制,则抛出异常。
  42. */
  43. public function incTotal($id, $inc = 1)
  44. {
  45. // 根据ID查询数据库记录。
  46. $res = $this->getModel()::getDb()->where($this->getPk(), $id)->find();
  47. // 如果查询结果为空,则抛出异常,表示数据异常。
  48. if (empty($res)) {
  49. throw new ValidateException('活动数据异常');
  50. }
  51. // 计算更新后的total值。
  52. $total = $res['total'] + $inc;
  53. // 如果当前记录的count值存在且小于更新后的total值,则抛出异常,表示超出总数限制。
  54. if ($res['count'] && $res['count'] < $total) {
  55. throw new ValidateException('超出总数限制');
  56. }
  57. // 更新记录的total值,并保存到数据库。
  58. $res->total = $total;
  59. $res->save();
  60. }
  61. }