diff --git a/app/controller/api/Coupon.php b/app/controller/api/Coupon.php index 1d6b4c0..3fd5243 100644 --- a/app/controller/api/Coupon.php +++ b/app/controller/api/Coupon.php @@ -3,6 +3,7 @@ namespace app\controller\api; use app\exception\RepositoryException; use app\model\CouponMain; +use app\model\Score; use app\repository\AccountRepository; use app\repository\CouponRepository; use think\Exception; @@ -25,7 +26,7 @@ class Coupon extends Base * * type in ['', 'notUsed', 'normal', 'used'] */ - public function getMyCouponList() + public function getCouponList() { $page = $this->request->param('page/d', 1); $size = $this->request->param('size/d', 10); @@ -39,10 +40,23 @@ class Coupon extends Base try { $whereMap = []; $sortOrder = ['received_time' => 'desc']; - $fields = ['*', '(end_time > NOW()) as sort_weight']; + $fields = [ + 'id', + 'is_verificated as isVerificated', + 'money', + 'name as couponName', + 'business_code as businessCode', + 'end_time as endTime', + 'consumer_name as consumerName', + 'verificate_time as verificateTime', + '(end_time > NOW()) as sort_weight']; $whereMap[] = ['consumer_code', '=', $accountCode]; switch ($type) { + case 'all': + // 全部持有优惠券 + $sortOrder = ['sort_weight' => 'desc', 'end_time' => 'asc']; + break; case 'notUsed': // 未使用(包含已过期) $whereMap[] = ['is_verificated', '=', self::BOOL_FALSE]; @@ -52,19 +66,35 @@ class Coupon extends Base // 未使用且未过期 $whereMap[] = ['is_verificated', '=', self::BOOL_FALSE]; $whereMap[] = ['end_time', '< TIME', date('Y-m-d H:i:s')]; - $fields = []; break; case 'used': // 已使用 $whereMap[] = ['is_verificated', '=', self::BOOL_TRUE]; $sortOrder = ['verificate_time' => 'desc']; - $fields = []; break; } - $res = CouponRepository::getInstance()->findList($whereMap, $fields, $page, $size, null, $sortOrder); - $res['list'] = multiTwoArrayKeysExcludeFilter($res['list']->toArray(), ['sort_weight']); + $res = CouponRepository::getInstance()->findList($whereMap, $fields, $page, $size,function ($q){ + return $q->with(["couponMain","scoreModel"]); + }, $sortOrder); + $res['list'] ->each(function ($item){ + //重置优惠券名称 + if(isset($item->couponMain) && $item->couponMain){ + $item->couponName = $item->couponMain->name; + } + //是否已经打分过了 + if(isset($item->scoreModel) && $item->scoreModel){ + $item->scored = true; + $item->score = $item->scoreModel->score; + }else{ + $item->scored = false; + $item->score = Score::COMMON_OFF; + } + + }); + + $res['list'] = multiTwoArrayKeysExcludeFilter($res['list']->toArray(), ['sort_weight']); return $this->json(0, 'success', $res); } catch (RepositoryException | \Exception $e) { return $this->json(5001, '优惠卷查询失败!'); diff --git a/app/model/Coupon.php b/app/model/Coupon.php index f781bcd..d9dd88f 100644 --- a/app/model/Coupon.php +++ b/app/model/Coupon.php @@ -37,4 +37,8 @@ class Coupon extends Base { return $this->hasOne(CouponMain::class, 'id', 'coupon_id'); } + public function scoreModel() + { + return $this->hasOne(Score::class, 'coupon_id', 'id'); + } } \ No newline at end of file diff --git a/app/model/Score.php b/app/model/Score.php new file mode 100644 index 0000000..7c7ad23 --- /dev/null +++ b/app/model/Score.php @@ -0,0 +1,12 @@ + -