CouponRulesLists.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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['max_voucher_count']) && is_numeric($this->params['max_voucher_count'])){
  57. $where[] = ['voucher_count', '<=', $this->params['max_voucher_count']];
  58. }
  59. if(isset($this->params['min_remaining_count']) && is_numeric($this->params['min_remaining_count'])){
  60. $where[] = ['remaining_count', '>=', $this->params['min_remaining_count']];
  61. }
  62. if(isset($this->params['max_remaining_count']) && is_numeric($this->params['max_remaining_count'])){
  63. $where[] = ['remaining_count', '<=', $this->params['max_remaining_count']];
  64. }
  65. if(isset($this->params['goods_category_ids']) && !empty($this->params['goods_category_ids'])){
  66. $categoryIds =[];
  67. foreach($this->params['goods_category_ids'] as $v){
  68. $v = json_decode($v,true)?? (array)$v;
  69. $categoryIds[] = end($v);
  70. }
  71. $ids = CouponRules::alias('cr')
  72. ->join('coupon_category cc','cr.id = cc.coupon_id')
  73. ->whereIn('cc.goods_category_id',$categoryIds)->distinct(true)->field('cc.coupon_id')->column('cc.coupon_id');
  74. if(empty($ids)){
  75. $ids = [0];
  76. }
  77. $where[] = ['id','IN',$ids];
  78. }
  79. if (isset($this->params['property_activity_id']) && !empty($this->params['property_activity_id'])) {
  80. // 筛选已选过的
  81. $where[] = ['id','>' ,0];//100000
  82. }
  83. return $where;
  84. }
  85. /**
  86. * @notes 获取列表
  87. * @return array
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. * @author likeadmin
  92. * @date 2024/07/18 10:11
  93. */
  94. public function lists(): array
  95. {
  96. $data = CouponRules::with(['couponWithCategory'=>function(Query $query){
  97. $query->field('id,name');
  98. },'couponWithGoods'=>function(Query $query){
  99. $query->field('id,goods_name');
  100. }])->where($this->searchWhere)
  101. ->where($this->queryWhere())
  102. ->field(['id','code', 'amount', 'coupon_target', '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'])
  103. ->limit($this->limitOffset, $this->limitLength)
  104. ->order(['id' => 'desc'])
  105. ->select()
  106. ->toArray();
  107. foreach($data as $k => $v){
  108. $v['goods_category_ids'] = array_column($v['couponWithCategory'],'id');
  109. $v['goods'] = $v['couponWithGoods'];
  110. $v['goods_id'] = array_column($v['couponWithGoods'],'id');
  111. $data[$k] = $v;
  112. }
  113. return $data;
  114. }
  115. /**
  116. * @notes 获取数量
  117. * @return int
  118. * @author likeadmin
  119. * @date 2024/07/18 10:11
  120. */
  121. public function count(): int
  122. {
  123. return CouponRules::where($this->searchWhere)->where($this->queryWhere())->count();
  124. }
  125. }