Coupon.php 3.4 KB

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