CouponMyLists.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\lists;
  20. use app\common\enum\CouponEnum;
  21. use app\common\lists\ListsExtendInterface;
  22. use app\common\lists\ListsSearchInterface;
  23. use app\common\model\Coupon;
  24. use app\common\model\CouponList;
  25. use app\common\model\Goods;
  26. use app\common\model\GoodsCategory;
  27. class CouponMyLists extends BaseShopDataLists implements ListsExtendInterface
  28. {
  29. /**
  30. * @notes 我的优惠券搜索条件
  31. * @return array
  32. * @author 张无忌
  33. * @date 2021/7/29 18:14
  34. */
  35. public function setSearch(): array
  36. {
  37. return [];
  38. }
  39. /**
  40. * @notes 我的优惠券数量统计
  41. * @return mixed
  42. * @author 张无忌
  43. * @date 2021/7/29 18:14
  44. */
  45. public function extend()
  46. {
  47. $model = new CouponList();
  48. // 可使用的优惠券数量
  49. $detail['normal'] = $model->where([
  50. ['user_id', '=', $this->userId],
  51. ['status', '=', CouponEnum::USE_STATUS_NOT],
  52. ['invalid_time', '>=', time()]
  53. ])->count();
  54. // 已使用的优惠券数量
  55. $detail['use'] = $model->where([
  56. ['user_id', '=', $this->userId],
  57. ['status', '=', CouponEnum::USE_STATUS_OK]
  58. ])->count();
  59. // 已失效的优惠券数量
  60. $map1 = [
  61. ['user_id', '=', $this->userId],
  62. ['status', 'in', [CouponEnum::USE_STATUS_EXPIRE, CouponEnum::USE_STATUS_VOID]],
  63. ];
  64. $map2 = [
  65. ['user_id', '=', $this->userId],
  66. ['invalid_time', '<', time()]
  67. ];
  68. $detail['invalid'] = $model->whereOr([$map1, $map2])->count();
  69. return $detail;
  70. }
  71. /**
  72. * @notes 我的优惠券搜索条件
  73. * @return array
  74. * @author 张无忌
  75. * @date 2021/7/29 18:14
  76. */
  77. public function queryWhere(): array
  78. {
  79. $where = [];
  80. $status = isset($this->params['status']) ? $this->params['status'] : -1;
  81. switch ($status) {
  82. case CouponEnum::USE_STATUS_NOT:
  83. $where[] = [
  84. ['CL.user_id', '=', $this->userId],
  85. ['CL.status', '=', CouponEnum::USE_STATUS_NOT],
  86. ['CL.invalid_time', '>=', time()]
  87. ];
  88. break;
  89. case CouponEnum::USE_STATUS_OK:
  90. $where[] = [
  91. ['CL.user_id', '=', $this->userId],
  92. ['CL.status', '=', CouponEnum::USE_STATUS_OK],
  93. ];
  94. break;
  95. case CouponEnum::USE_STATUS_EXPIRE:
  96. case CouponEnum::USE_STATUS_VOID:
  97. $where[] = [
  98. ['CL.user_id', '=', $this->userId],
  99. ['CL.status', 'in', [CouponEnum::USE_STATUS_EXPIRE, CouponEnum::USE_STATUS_VOID]],
  100. ];
  101. $where[] = [
  102. ['CL.user_id', '=', $this->userId],
  103. ['CL.invalid_time', '<', time()]
  104. ];
  105. break;
  106. }
  107. return $where;
  108. }
  109. /**
  110. * @notes 获取我的优惠券列表
  111. * @return array
  112. * @author 张无忌
  113. * @date 2021/7/29 18:16
  114. */
  115. public function lists(): array
  116. {
  117. $model = new Coupon();
  118. $lists = $model->alias('C')
  119. ->field([
  120. 'C.id,CL.id as CL_id,C.name,C.money,C.condition_type','C.discount_max_money','C.discount_ratio',
  121. 'C.condition_money,C.use_goods_type,C.use_goods_ids,C.use_goods_category_ids,C.use_time_type',
  122. 'C.use_time_start,C.use_time_end,C.use_time,CL.status,CL.invalid_time'
  123. ])
  124. ->append([ 'condition', 'use_time_text', 'use_time_text2', 'use_type', 'tips' ])
  125. ->whereOr($this->queryWhere())
  126. ->join('coupon_list CL', 'C.id = CL.coupon_id')
  127. ->order('id', 'desc')
  128. ->limit($this->limitOffset, $this->limitLength)
  129. ->select()
  130. ->toArray();
  131. foreach ($lists as &$item) {
  132. $item['use_scene'] = $item['use_type'];
  133. $item['effective_time'] = $item['use_time_text'];
  134. unset($item['use_goods_type']);
  135. unset($item['use_goods_ids']);
  136. unset($item['use_goods_category_ids']);
  137. unset($item['use_time_type']);
  138. unset($item['use_time_start']);
  139. unset($item['use_time_end']);
  140. unset($item['use_time']);
  141. }
  142. return $lists;
  143. }
  144. /**
  145. * @notes 我的优惠券数量
  146. * @return int
  147. * @author 张无忌
  148. * @date 2021/7/29 18:16
  149. */
  150. public function count(): int
  151. {
  152. $model = new Coupon();
  153. return $model->alias('C')
  154. ->field([
  155. 'C.id,CL.id as CL_id,C.name,C.money,C.condition_type',
  156. 'C.condition_money,C.use_goods_type,C.use_goods_ids,C.use_time_type',
  157. 'C.use_time_start,C.use_time_end,C.use_time,CL.status'
  158. ])
  159. ->whereOr($this->queryWhere())
  160. ->join('coupon_list CL', 'C.id = CL.coupon_id')
  161. ->count();
  162. }
  163. }