Coupon.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\store\model;
  13. use app\common\model\Coupon as CouponModel;
  14. use app\common\enum\coupon\ExpireType as ExpireTypeEnum;
  15. use app\common\enum\coupon\ApplyRange as ApplyRangeEnum;
  16. /**
  17. * 优惠券模型
  18. * Class Coupon
  19. * @package app\store\model
  20. */
  21. class Coupon extends CouponModel
  22. {
  23. /**
  24. * 获取列表记录
  25. * @param array $param
  26. * @return \think\Paginator
  27. * @throws \think\db\exception\DbException
  28. */
  29. public function getList(array $param = []): \think\Paginator
  30. {
  31. // 检索查询条件
  32. $filter = $this->getFilter($param);
  33. // 查询列表数据
  34. return $this->where($filter)
  35. ->where('is_delete', '=', 0)
  36. ->order(['sort', 'create_time' => 'desc'])
  37. ->paginate(15);
  38. }
  39. /**
  40. * 检索查询条件
  41. * @param array $param
  42. * @return array
  43. */
  44. private function getFilter(array $param = []): array
  45. {
  46. // 默认查询条件
  47. $param = $this->setQueryDefaultValue($param, ['search' => '']);
  48. // 检索查询条件
  49. $filter = [];
  50. !empty($param['search']) && $filter[] = ['name', 'like', "%{$param['search']}%"];
  51. return $filter;
  52. }
  53. /**
  54. * 添加新记录
  55. * @param array $data
  56. * @return bool|false
  57. */
  58. public function add(array $data): bool
  59. {
  60. $data['store_id'] = self::$storeId;
  61. return $this->save($this->createData($data));
  62. }
  63. /**
  64. * 更新记录
  65. * @param array $data
  66. * @return bool
  67. */
  68. public function edit(array $data): bool
  69. {
  70. return $this->save($this->createData($data)) !== false;
  71. }
  72. /**
  73. * 创建数据
  74. * @param array $data
  75. * @return array
  76. */
  77. private function createData(array $data): array
  78. {
  79. // 折扣券记录有效期
  80. // 领取后生效
  81. if ($data['expire_type'] == ExpireTypeEnum::RECEIVE) {
  82. $data['start_time'] = $data['end_time'] = 0;
  83. } // 固定时间
  84. elseif ($data['expire_type'] == ExpireTypeEnum::FIXED_TIME) {
  85. $times = between_time($data['betweenTime']);
  86. $data['start_time'] = $times['start_time'];
  87. $data['end_time'] = $times['end_time'] + (86400 - 1);
  88. $data['expire_day'] = 1;
  89. }
  90. // 适用范围
  91. if ($data['apply_range'] == ApplyRangeEnum::ALL) {
  92. $data['apply_range_config'] = [];
  93. }
  94. return $data;
  95. }
  96. /**
  97. * 删除记录 (软删除)
  98. * @return bool
  99. */
  100. public function setDelete(): bool
  101. {
  102. return $this->save(['is_delete' => 1]) !== false;
  103. }
  104. }