|
@@ -19,28 +19,41 @@ class UserCouponLogic extends BaseLogic
|
|
|
Db::startTrans();
|
|
Db::startTrans();
|
|
|
try{
|
|
try{
|
|
|
$errMsgArr = [];
|
|
$errMsgArr = [];
|
|
|
- $rules = CouponRules::whereIn('id',$params['coupon_ids'])->lock(true)->select();
|
|
|
|
|
|
|
+ $rules = CouponRules::whereIn('code',$params['codes'])->where('voucher_status',1)->lock(true)->select();
|
|
|
$userCoupon = UserCoupon::where('user_id',$params['user_id'])
|
|
$userCoupon = UserCoupon::where('user_id',$params['user_id'])
|
|
|
- ->whereIn('coupon_id',$params['coupon_ids'])
|
|
|
|
|
|
|
+ ->whereIn('code',$params['codes'])
|
|
|
->where('voucher_count','>',0)
|
|
->where('voucher_count','>',0)
|
|
|
->select();
|
|
->select();
|
|
|
$updateData = [];
|
|
$updateData = [];
|
|
|
$createData = [];
|
|
$createData = [];
|
|
|
foreach($rules as $v) {
|
|
foreach($rules as $v) {
|
|
|
- if ($v->expire_time < time()) {
|
|
|
|
|
- $errMsgArr[] = $v->event_name .'已过期';
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
if($v->voucher_status == 2){
|
|
if($v->voucher_status == 2){
|
|
|
$errMsgArr[] = $v->event_name .'已设置为过期';
|
|
$errMsgArr[] = $v->event_name .'已设置为过期';
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
+ if($v->remaining_count <= 0 ){
|
|
|
|
|
+ $errMsgArr[] = $v->event_name .'数量不足';
|
|
|
|
|
+ }
|
|
|
if($userCoupon->where('coupon_id',$v->id)->count()){
|
|
if($userCoupon->where('coupon_id',$v->id)->count()){
|
|
|
$errMsgArr[] = $v->event_name . '已领取';
|
|
$errMsgArr[] = $v->event_name . '已领取';
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
- $updateData[$v->id] = ['voucher_count' => --$v->voucher_count];
|
|
|
|
|
- $createData[] = ['user_id'=>$params['user_id'],'coupon_id' => $v->id,'voucher_count' =>1];
|
|
|
|
|
|
|
+ $updateData[$v->id] = ['remaining_count' => --$v->remaining_count];
|
|
|
|
|
+ $createData[] = [
|
|
|
|
|
+ 'user_id'=>$params['user_id'],
|
|
|
|
|
+ 'code' => $v->code,
|
|
|
|
|
+ 'voucher_status' => 0,
|
|
|
|
|
+ 'voucher_count' =>1,
|
|
|
|
|
+ 'amount' => $v->mold_type != 1 ? $v->amount:null,
|
|
|
|
|
+ 'amount_require' => $v->amount_require,
|
|
|
|
|
+ 'begin_use' => time(),
|
|
|
|
|
+ 'discount_ratio' => $v->mold_type == 1 ? $v->discount_ratio:null,
|
|
|
|
|
+ 'event_name' => $v->event_name,
|
|
|
|
|
+ 'expire_time' => time()+$v->expire_time,
|
|
|
|
|
+ 'max_deductible_price' => $v->mold_type == 1 ? $v->max_deductible_price:null,
|
|
|
|
|
+ 'mold_type' => $v->mold_type,
|
|
|
|
|
+ 'server_category_name' => $v->server_category_name
|
|
|
|
|
+ ];
|
|
|
}
|
|
}
|
|
|
CouponRules::updateWhenCase($updateData);
|
|
CouponRules::updateWhenCase($updateData);
|
|
|
if(!empty($createData)){
|
|
if(!empty($createData)){
|
|
@@ -69,36 +82,39 @@ class UserCouponLogic extends BaseLogic
|
|
|
$where[] = ['voucher_status','IN',$params['voucher_status']];
|
|
$where[] = ['voucher_status','IN',$params['voucher_status']];
|
|
|
}
|
|
}
|
|
|
$where[] = ['user_id','=',$params['user_id']];
|
|
$where[] = ['user_id','=',$params['user_id']];
|
|
|
-
|
|
|
|
|
$data = UserCoupon::with(['couponRules'=>function(Query $query){
|
|
$data = UserCoupon::with(['couponRules'=>function(Query $query){
|
|
|
- $query->withoutField(['voucher_status','voucher_count']);
|
|
|
|
|
|
|
+ $query->field(['id','code']);
|
|
|
$query->with(['couponWithCategory'=>function(Query $query){
|
|
$query->with(['couponWithCategory'=>function(Query $query){
|
|
|
$query->field('id,name');
|
|
$query->field('id,name');
|
|
|
}]);
|
|
}]);
|
|
|
}])
|
|
}])
|
|
|
->where($where)
|
|
->where($where)
|
|
|
- ->field(['id', 'user_id', 'coupon_id', 'voucher_status','voucher_count'])
|
|
|
|
|
|
|
+ ->where(function(Query $query){
|
|
|
|
|
+ $query->where(' expire_time >'. (time()-86400*7));
|
|
|
|
|
+ })
|
|
|
|
|
+ ->field(['id', 'user_id', 'code','begin_use','expire_time', 'voucher_status','voucher_count'])
|
|
|
|
|
+ ->append(['voucher_status_text'])
|
|
|
->order(['id' => 'desc'])
|
|
->order(['id' => 'desc'])
|
|
|
->select()
|
|
->select()
|
|
|
->toArray();
|
|
->toArray();
|
|
|
- $couponIds = [];
|
|
|
|
|
|
|
+ $couponCodes = [];
|
|
|
foreach($data as $k => $v){
|
|
foreach($data as $k => $v){
|
|
|
if($v['couponRules']){
|
|
if($v['couponRules']){
|
|
|
- if($v['voucher_status'] !=2 &&($v['couponRules']['begin_use'] <time() || $data['couponRules']['expire_time'] > time())){
|
|
|
|
|
- $couponIds[] = $v['coupon_id'];
|
|
|
|
|
- $v['voucher_status'] = 2;
|
|
|
|
|
- }
|
|
|
|
|
- $v['couponRules']['begin_use'] = date("Y-m-d H:i:s",$v['couponRules']['begin_use'] );
|
|
|
|
|
- $v['couponRules']['expire_time'] = date("Y-m-d H:i:s",$v['couponRules']['expire_time'] );
|
|
|
|
|
$v['couponRules']['goods_category_ids'] = array_column($v['couponRules']['couponWithCategory'],'id');
|
|
$v['couponRules']['goods_category_ids'] = array_column($v['couponRules']['couponWithCategory'],'id');
|
|
|
$data[$k] = $v;
|
|
$data[$k] = $v;
|
|
|
}
|
|
}
|
|
|
|
|
+ if($v['expire_time'] < time()){
|
|
|
|
|
+ $couponCodes[] = $v['code'];
|
|
|
|
|
+ $data[$k]['voucher_status_text'] = '已过期';
|
|
|
|
|
+ }
|
|
|
|
|
+ $data[$k]['begin_use'] = date("Y-m-d H:i:s",$v['begin_use'] );
|
|
|
|
|
+ $data[$k]['expire_time'] = date("Y-m-d H:i:s",$v['expire_time'] );
|
|
|
}
|
|
}
|
|
|
- if(!empty($couponIds)){
|
|
|
|
|
- UserCoupon::whereIn('coupon_id',$couponIds)->update(['voucher_status'=>2]);
|
|
|
|
|
|
|
+ if(!empty($couponCodes)){
|
|
|
|
|
+ UserCoupon::whereIn('code',$couponCodes)->update(['voucher_status'=>2]);
|
|
|
}
|
|
}
|
|
|
return $data;
|
|
return $data;
|
|
|
- }catch(\Exception $e){
|
|
|
|
|
|
|
+ } catch (\Exception $e){
|
|
|
Db::rollback();
|
|
Db::rollback();
|
|
|
self::setError($e->getMessage());
|
|
self::setError($e->getMessage());
|
|
|
return false;
|
|
return false;
|
|
@@ -113,15 +129,24 @@ class UserCouponLogic extends BaseLogic
|
|
|
public static function categoryCouponList($params)
|
|
public static function categoryCouponList($params)
|
|
|
{
|
|
{
|
|
|
try{
|
|
try{
|
|
|
- $data = CouponRules::with(['couponWithCategory'=>function(Query $query) use($params){
|
|
|
|
|
- $query->where('goods_category_id',$params['goods_category_id']);
|
|
|
|
|
- }])->field(['id', 'amount', 'coupon_type','amount_require', 'discount_ratio', 'event_name', 'expire_time', 'max_deductible_price', 'mold_type', 'server_category_name', 'voucher_count'])
|
|
|
|
|
- ->order(['id' => 'desc'])
|
|
|
|
|
- ->select()
|
|
|
|
|
- ->toArray();
|
|
|
|
|
|
|
+ $data = CouponRules::alias('cr')
|
|
|
|
|
+ ->leftJoin('coupon_category cc','cc.coupon_id = cr.id')
|
|
|
|
|
+ ->where('cc.goods_category_id',$params['goods_category_id'])
|
|
|
|
|
+ ->where('cr.remaining_count','>',0)
|
|
|
|
|
+ ->where('cr.voucher_status',1)
|
|
|
|
|
+ ->order(['cr.id' => 'desc'])
|
|
|
|
|
+ ->field(['cr.id','cr.code','cr.amount','cr.amount_require','cr.discount_ratio','cr.server_category_name',
|
|
|
|
|
+ 'cr.event_name','cr.expire_time','cr.max_deductible_price','cr.mold_type',])
|
|
|
|
|
+ ->select();
|
|
|
|
|
+ $codes = $data->column('code');
|
|
|
|
|
+ $userCouponCodes = [];
|
|
|
|
|
+ if(!empty($code)){
|
|
|
|
|
+ $userCouponCodes = UserCoupon::where('code',$codes)
|
|
|
|
|
+ ->where('user_id',$params['user_id'])->column('code');
|
|
|
|
|
+ }
|
|
|
|
|
+ $data = $data->whereNotIn('code',$userCouponCodes)->toArray();
|
|
|
return $data;
|
|
return $data;
|
|
|
}catch(\Exception $e){
|
|
}catch(\Exception $e){
|
|
|
- Db::rollback();
|
|
|
|
|
self::setError($e->getMessage());
|
|
self::setError($e->getMessage());
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|