UserCouponLists.php 2.3 KB

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