181 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			PHP
		
	
	
			
		
		
	
	
			181 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			PHP
		
	
	
| <?php
 | |
| 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;
 | |
| use think\facade\Db;
 | |
| use think\facade\Log;
 | |
| 
 | |
| /**
 | |
|  * 优惠卷相关
 | |
|  * Class Coupon
 | |
|  * @package app\controller\api
 | |
|  */
 | |
| class Coupon extends Base
 | |
| {
 | |
|     protected $noNeedLogin = [
 | |
| 
 | |
|     ];
 | |
| 
 | |
|     /**
 | |
|      * 我的优惠卷列表
 | |
|      *
 | |
|      * type in ['', 'notUsed', 'normal', 'used']
 | |
|      */
 | |
|     public function getCouponList()
 | |
|     {
 | |
|         $page       = $this->request->param('page/d', 1);
 | |
|         $size       = $this->request->param('size/d', 10);
 | |
|         $type       = $this->request->param('type', '');
 | |
| 
 | |
|         $page       = $page < 1 ? 1 : $page;
 | |
|         $size       = $size < 1 ? 10 : $size;
 | |
|         $accountCode    = $this->request->user['user_code'] ?? '';
 | |
| 
 | |
| 
 | |
|         try {
 | |
|             $whereMap   = [];
 | |
|             $sortOrder  = ['received_time' => 'desc'];
 | |
|             $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];
 | |
|                     $sortOrder  = ['sort_weight' => 'desc', 'end_time' => 'asc'];
 | |
|                     break;
 | |
|                 case 'normal':
 | |
|                     // 未使用且未过期
 | |
|                     $whereMap[] = ['is_verificated', '=', self::BOOL_FALSE];
 | |
|                     $whereMap[] = ['end_time', '< TIME', date('Y-m-d H:i:s')];
 | |
|                     break;
 | |
|                 case 'used':
 | |
|                     // 已使用
 | |
|                     $whereMap[] = ['is_verificated', '=', self::BOOL_TRUE];
 | |
|                     $sortOrder  = ['verificate_time' => 'desc'];
 | |
|                     break;
 | |
|             }
 | |
| 
 | |
|             $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, '优惠卷查询失败!');
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 领取优惠券
 | |
|      * */
 | |
|     public function receiveCoupon()
 | |
|     {
 | |
|         $accountId = $this->request->user['user_id'] ?? 0;
 | |
|         $lat = input("lat/f",0);
 | |
|         $lng =  input("lng/f",0);
 | |
|         $account = AccountRepository::getInstance()->findById($accountId, [], function ($q) {
 | |
|             return $q->with(['business', 'parent']);
 | |
|         });
 | |
|         if(empty($account)){
 | |
|             return $this->json(6001,"无效的用户");
 | |
|         }
 | |
| 
 | |
|         if ($lat <= 0 || $lng <= 0) {
 | |
|             return $this->json(4001, "请授权定位");
 | |
|         }
 | |
| 
 | |
|         $couponMainId = input("couponId/d", 0);
 | |
|         $couponMain = CouponMain::findOne(["id" => $couponMainId],[],function ($q){
 | |
|             //执行领取 开启锁
 | |
|             return $q->with("business")->lock(true);
 | |
|         });
 | |
| 
 | |
|         //检查优惠券状态
 | |
|         $checkCouponMainReceiveStatus = CouponRepository::getInstance()->checkCouponMainReceiveStatus($couponMain);
 | |
|         if( $checkCouponMainReceiveStatus !== true ){
 | |
|             return $checkCouponMainReceiveStatus;
 | |
|         }
 | |
| 
 | |
|         try {
 | |
|             //检查是否可以领取  0可领取 1已领取
 | |
|            AccountRepository::getInstance()->getCouponReceiveStatusText($account->user_code,$couponMain);//领取状态
 | |
|         }catch (RepositoryException $e){
 | |
|             return $this->json(4001,$e->getMessage());
 | |
|         }
 | |
| 
 | |
| 
 | |
| 
 | |
|         //检查通过 执行领取
 | |
|         $time = time();
 | |
|         Db::startTrans();
 | |
|         try {
 | |
|             //写入领取记录
 | |
|             $data = [
 | |
|                 "coupon_id"     =>$couponMain->id,
 | |
|                 "name"          =>$couponMain->name,
 | |
|                 "type_id"          =>$couponMain->type,
 | |
|                 "type_name"          =>$couponMain->type_name,
 | |
|                 "business_code"          =>$couponMain->business_code,
 | |
|                 "business_name"          =>$couponMain->business?$couponMain->business->business_name:'',
 | |
|                 "consumer_code"          =>$account->user_code,
 | |
|                 "consumer_name"          =>$account->nick_name,
 | |
|                 "money"          => $couponMain->money,
 | |
|                 "content"          => createUuid(),//未知作用
 | |
|                 "received_time"          => date("Y-m-d H:i:s",$time),
 | |
|                 "lat"          => $lat,
 | |
|                 "lng"          => $lng,
 | |
|                 "end_time"          => date($couponMain->end_time . " 00:00:00"),
 | |
|                 "edition"          => couponMain::COMMON_ON,//版本  未知作用
 | |
|                 "is_verificated"          => couponMain::COMMON_OFF,//版本  未知作用
 | |
|             ];
 | |
|             CouponRepository::getInstance()->receiveCoupon($data);
 | |
|             Db::commit();
 | |
|             return $this->json();
 | |
|         }catch (RepositoryException $e){
 | |
|             Log::error("优惠券领取失败RepositoryException:".$e->getMessage());
 | |
|             Db::rollback();
 | |
|             return $this->json(5001,"领取失败");
 | |
|         }catch (Exception $e){
 | |
|             Log::error("优惠券领取失败:".$e->getMessage());
 | |
|             Db::rollback();
 | |
|             return $this->json(5001,"领取失败");
 | |
|         }
 | |
| 
 | |
|     }
 | |
| } |