UserCouponLists.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace app\api\lists;
  3. use app\common\lists\ListsSearchInterface;
  4. use app\common\model\coupon\UserCoupon;
  5. use think\db\Query;
  6. class UserCouponLists extends BaseApiDataLists implements ListsSearchInterface
  7. {
  8. protected $count = 0;
  9. public function setSearch(): array
  10. {
  11. return [
  12. '=' => ['user_id'],
  13. ];
  14. }
  15. public function queryWhere(){
  16. $where = [];
  17. if(isset($this->params['voucher_status']) && is_array($this->params['voucher_status'])){
  18. $where[] = ['voucher_status','IN',$this->params['voucher_status']];
  19. }
  20. $where[] = ['user_id','=',$this->userId];
  21. return $where;
  22. }
  23. public function lists(): array
  24. {
  25. $data = UserCoupon::with(['couponRules'=>function(Query $query){
  26. $query->withoutField(['voucher_status','voucher_count']);
  27. $query->with(['couponWithCategory'=>function(Query $query){
  28. $query->field('id,name');
  29. }]);
  30. }])
  31. ->where($this->searchWhere)
  32. ->where($this->queryWhere())
  33. ->field(['id', 'user_id', 'coupon_id', 'voucher_status','voucher_count'])
  34. ->limit($this->limitOffset, $this->limitLength)
  35. ->order(['id' => 'desc'])
  36. ->select()
  37. ->toArray();
  38. $couponIds = [];
  39. foreach($data as $k => $v){
  40. if($v['couponRules']){
  41. if($v['voucher_status'] !=2 &&($v['couponRules']['begin_use'] <time() || $data['couponRules']['expire_time'] > time())){
  42. $couponIds[] = $v['coupon_id'];
  43. $v['voucher_status'] = 2;
  44. }
  45. $v['couponRules']['begin_use'] = date("Y-m-d H:i:s",$v['couponRules']['begin_use'] );
  46. $v['couponRules']['expire_time'] = date("Y-m-d H:i:s",$v['couponRules']['expire_time'] );
  47. $v['couponRules']['goods_category_ids'] = array_column($v['couponRules']['couponWithCategory'],'id');
  48. $data[$k] = $v;
  49. }
  50. }
  51. if(!empty($couponIds)){
  52. UserCoupon::whereIn('coupon_id',$couponIds)->update(['voucher_status'=>2]);
  53. }
  54. $this->count = count($data);
  55. return $data;
  56. }
  57. public function count(): int
  58. {
  59. return $this->count;
  60. }
  61. }