CouponRulesLists.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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', 'voucher_count'],
  37. '%like%' => ['event_name'],
  38. ];
  39. }
  40. public function queryWhere()
  41. {
  42. $where = [];
  43. if(isset($this->params['begin_use']) && !empty($this->params['begin_use'])){
  44. $time = [strtotime($this->params['begin_use'][0]), strtotime($this->params['begin_use'][1])];
  45. $where[] = ['begin_use', 'between', $time];
  46. }
  47. if(isset($this->params['expire_time']) && !empty($this->params['expire_time'])){
  48. $time = [strtotime($this->params['expire_time'][0]), strtotime($this->params['expire_time'][1])];
  49. $where[] = ['expire_time', 'between', $time];
  50. }
  51. if(isset($this->params['goods_category_ids']) && !empty($this->params['goods_category_ids'])){
  52. $categoryIds =[];
  53. foreach($this->params['goods_category_ids'] as $v){
  54. $v = json_decode($v,true)?? (array)$v;
  55. $categoryIds[] = end($v);
  56. }
  57. $ids = CouponRules::alias('cr')
  58. ->join('coupon_category cc','cr.id = cc.coupon_id')
  59. ->whereIn('cc.goods_category_id',$categoryIds)->distinct(true)->field('cc.coupon_id')->column('cc.coupon_id');
  60. if(empty($ids)){
  61. $ids = [0];
  62. }
  63. $where[] = ['id','IN',$ids];
  64. }
  65. return $where;
  66. }
  67. /**
  68. * @notes 获取列表
  69. * @return array
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. * @author likeadmin
  74. * @date 2024/07/18 10:11
  75. */
  76. public function lists(): array
  77. {
  78. $data = CouponRules::with(['couponWithCategory'=>function(Query $query){
  79. $query->field('id,name');
  80. }])->where($this->searchWhere)
  81. ->where($this->queryWhere())
  82. ->field(['id', 'amount', 'coupon_type','amount_require', 'begin_use', 'discount_ratio', 'event_name', 'expire_time', 'max_deductible_price', 'mold_type', 'server_category_name', 'voucher_status', 'voucher_count'])
  83. ->limit($this->limitOffset, $this->limitLength)
  84. ->order(['id' => 'desc'])
  85. ->select()
  86. ->toArray();
  87. foreach($data as $k => $v){
  88. $v['goods_category_ids'] = array_column($v['couponWithCategory'],'id');
  89. $v['begin_use'] = $v['begin_use'] ? date('Y-m-d H:i:s',$v['begin_use']) :null;
  90. $v['expire_time'] = $v['begin_use'] ?date('Y-m-d H:i:s',$v['expire_time']) :null;
  91. $data[$k] = $v;
  92. }
  93. return $data;
  94. }
  95. /**
  96. * @notes 获取数量
  97. * @return int
  98. * @author likeadmin
  99. * @date 2024/07/18 10:11
  100. */
  101. public function count(): int
  102. {
  103. return CouponRules::where($this->searchWhere)->where($this->queryWhere())->count();
  104. }
  105. }