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