CouponRulesLists.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 app\common\model\goods\Goods;
  20. use think\db\Query;
  21. /**
  22. * CouponRules列表
  23. * Class CouponRulesLists
  24. * @package app\adminapi\listscoupon
  25. */
  26. class CouponRulesLists extends BaseAdminDataLists implements ListsSearchInterface
  27. {
  28. /**
  29. * @notes 设置搜索条件
  30. * @return \string[][]
  31. * @author likeadmin
  32. * @date 2024/07/18 10:11
  33. */
  34. public function setSearch(): array
  35. {
  36. return [
  37. '=' => ['amount', 'amount_require', 'max_deductible_price', 'mold_type', 'server_category_name', 'coupon_type','voucher_status','coupon_target'],
  38. '%like%' => ['event_name','code'],
  39. ];
  40. }
  41. public function queryWhere()
  42. {
  43. $where = [];
  44. if(isset($this->params['min_expire_time']) && is_numeric($this->params['min_expire_time'])){
  45. $where[] = ['expire_time', '>=', $this->params['min_expire_time']];
  46. }
  47. if(isset($this->params['max_expire_time']) && is_numeric($this->params['min_expire_time'])){
  48. $where[] = ['expire_time', '<=', $this->params['min_expire_time']];
  49. }
  50. if(isset($this->params['min_expire_time']) && is_numeric($this->params['min_expire_time'])){
  51. $where[] = ['expire_time', '>=', $this->params['min_expire_time']];
  52. }
  53. if(isset($this->params['min_voucher_count']) && is_numeric($this->params['min_voucher_count'])){
  54. $where[] = ['voucher_count', '>=', $this->params['min_voucher_count']];
  55. }
  56. if (isset($this->params['labels']) && !empty($this->params['labels'])) {
  57. $sqls = [];
  58. foreach ($this->params['labels'] as $item) {
  59. $sqls[] = "FIND_IN_SET({$item}, labels) > 0";
  60. }
  61. $query_sql = implode(' OR ', $sqls);
  62. $period_ids = CouponRules::where('labels','<>', '')->whereRaw($query_sql)->column('id');
  63. $where[] = [ 'id','IN',$period_ids?:[0]];
  64. }
  65. if(isset($this->params['max_voucher_count']) && is_numeric($this->params['max_voucher_count'])){
  66. $where[] = ['voucher_count', '<=', $this->params['max_voucher_count']];
  67. }
  68. if(isset($this->params['min_remaining_count']) && is_numeric($this->params['min_remaining_count'])){
  69. $where[] = ['remaining_count', '>=', $this->params['min_remaining_count']];
  70. }
  71. if(isset($this->params['max_remaining_count']) && is_numeric($this->params['max_remaining_count'])){
  72. $where[] = ['remaining_count', '<=', $this->params['max_remaining_count']];
  73. }
  74. if(isset($this->params['goods_category_ids']) && !empty($this->params['goods_category_ids'])){
  75. $categoryIds =[];
  76. foreach($this->params['goods_category_ids'] as $v){
  77. $v = json_decode($v,true)?? (array)$v;
  78. $categoryIds[] = end($v);
  79. }
  80. $ids = CouponRules::alias('cr')
  81. ->join('coupon_category cc','cr.id = cc.coupon_id')
  82. ->whereIn('cc.goods_category_id',$categoryIds)->distinct(true)->field('cc.coupon_id')->column('cc.coupon_id');
  83. if(empty($ids)){
  84. $ids = [0];
  85. }
  86. $where[] = ['id','IN',$ids];
  87. }
  88. if (isset($this->params['property_activity_id']) && !empty($this->params['property_activity_id'])) {
  89. // 筛选已选过的
  90. $where[] = ['id','>' ,0];//100000
  91. }
  92. return $where;
  93. }
  94. /**
  95. * @notes 获取列表
  96. * @return array
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\DbException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. * @author likeadmin
  101. * @date 2024/07/18 10:11
  102. */
  103. public function lists(): array
  104. {
  105. $data = CouponRules::with(['couponWithCategory'=>function(Query $query){
  106. $query->field('id,name');
  107. },'couponWithGoods'=>function(Query $query){
  108. $query->field('id,goods_name');
  109. }])->where($this->searchWhere)
  110. ->where($this->queryWhere())
  111. ->limit($this->limitOffset, $this->limitLength)
  112. ->order(['id' => 'desc'])
  113. ->select()
  114. ->toArray();
  115. foreach($data as $k => $v){
  116. $v['goods_category_ids'] = array_column($v['couponWithCategory'],'id');
  117. $v['goods'] = $v['couponWithGoods'];
  118. $v['goods_id'] = array_column($v['couponWithGoods'],'id');
  119. $data[$k] = $v;
  120. }
  121. return $data;
  122. }
  123. /**
  124. * @notes 获取数量
  125. * @return int
  126. * @author likeadmin
  127. * @date 2024/07/18 10:11
  128. */
  129. public function count(): int
  130. {
  131. return CouponRules::where($this->searchWhere)->where($this->queryWhere())->count();
  132. }
  133. }