CouponRulesLists.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\lists\coupon;
  15. use app\adminapi\lists\BaseAdminDataLists;
  16. use app\common\model\coupon\CouponCategory;
  17. use app\common\model\coupon\CouponRules;
  18. use app\common\lists\ListsSearchInterface;
  19. use think\db\Query;
  20. /**
  21. * CouponRules列表
  22. * Class CouponRulesLists
  23. * @package app\adminapi\listscoupon
  24. */
  25. class CouponRulesLists extends BaseAdminDataLists implements ListsSearchInterface
  26. {
  27. /**
  28. * @notes 设置搜索条件
  29. * @return \string[][]
  30. * @author likeadmin
  31. * @date 2024/07/18 10:11
  32. */
  33. public function setSearch(): array
  34. {
  35. return [
  36. '=' => ['amount', 'amount_require', 'max_deductible_price', 'mold_type', 'server_category_name', 'coupon_type','voucher_status'],
  37. '%like%' => ['event_name','code'],
  38. ];
  39. }
  40. public function queryWhere()
  41. {
  42. $where = [];
  43. if(isset($this->params['min_expire_time']) && is_numeric($this->params['min_expire_time'])){
  44. $where[] = ['expire_time', '>=', $this->params['min_expire_time']];
  45. }
  46. if(isset($this->params['max_expire_time']) && is_numeric($this->params['min_expire_time'])){
  47. $where[] = ['expire_time', '<=', $this->params['min_expire_time']];
  48. }
  49. if(isset($this->params['min_expire_time']) && is_numeric($this->params['min_expire_time'])){
  50. $where[] = ['expire_time', '>=', $this->params['min_expire_time']];
  51. }
  52. if(isset($this->params['min_voucher_count']) && is_numeric($this->params['min_voucher_count'])){
  53. $where[] = ['voucher_count', '>=', $this->params['min_voucher_count']];
  54. }
  55. if(isset($this->params['max_voucher_count']) && is_numeric($this->params['max_voucher_count'])){
  56. $where[] = ['voucher_count', '<=', $this->params['max_voucher_count']];
  57. }
  58. if(isset($this->params['min_remaining_count']) && is_numeric($this->params['min_remaining_count'])){
  59. $where[] = ['remaining_count', '>=', $this->params['min_remaining_count']];
  60. }
  61. if(isset($this->params['max_remaining_count']) && is_numeric($this->params['max_remaining_count'])){
  62. $where[] = ['remaining_count', '<=', $this->params['max_remaining_count']];
  63. }
  64. if(isset($this->params['goods_category_ids']) && !empty($this->params['goods_category_ids'])){
  65. $categoryIds =[];
  66. foreach($this->params['goods_category_ids'] as $v){
  67. $v = json_decode($v,true)?? (array)$v;
  68. $categoryIds[] = end($v);
  69. }
  70. $ids = CouponRules::alias('cr')
  71. ->join('coupon_category cc','cr.id = cc.coupon_id')
  72. ->whereIn('cc.goods_category_id',$categoryIds)->distinct(true)->field('cc.coupon_id')->column('cc.coupon_id');
  73. if(empty($ids)){
  74. $ids = [0];
  75. }
  76. $where[] = ['id','IN',$ids];
  77. }
  78. if (isset($this->params['property_activity_id']) && !empty($this->params['property_activity_id'])) {
  79. // 筛选已选过的
  80. $where[] = ['id','>' ,0];//100000
  81. }
  82. return $where;
  83. }
  84. /**
  85. * @notes 获取列表
  86. * @return array
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\DbException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. * @author likeadmin
  91. * @date 2024/07/18 10:11
  92. */
  93. public function lists(): array
  94. {
  95. $data = CouponRules::with(['couponWithCategory'=>function(Query $query){
  96. $query->field('id,name');
  97. }])->where($this->searchWhere)
  98. ->where($this->queryWhere())
  99. ->field(['id','code', 'amount', 'coupon_type','amount_require', 'discount_ratio', 'event_name', 'expire_time', 'max_deductible_price', 'mold_type', 'server_category_name', 'voucher_status', 'voucher_count','remaining_count','property_activity_id'])
  100. ->limit($this->limitOffset, $this->limitLength)
  101. ->order(['id' => 'desc'])
  102. ->select()
  103. ->toArray();
  104. foreach($data as $k => $v){
  105. $v['goods_category_ids'] = array_column($v['couponWithCategory'],'id');
  106. $data[$k] = $v;
  107. }
  108. return $data;
  109. }
  110. /**
  111. * @notes 获取数量
  112. * @return int
  113. * @author likeadmin
  114. * @date 2024/07/18 10:11
  115. */
  116. public function count(): int
  117. {
  118. return CouponRules::where($this->searchWhere)->where($this->queryWhere())->count();
  119. }
  120. }