$val){ $meetAmount = $val['meetAmount']; if($consumePrice < $meetAmount){//订单价格达到使用代金劵的金额要求 unset($coupon[$key]); } } } return $coupon; } /** * 获取用户的代金劵 */ public static function getUserCoupon($userId) { $cacheKey = dict::getCacheKey('userCoupon').$userId; $exists = Yii::$app->redis->executeCommand('EXISTS', [$cacheKey]); if(empty($exists)){ $coupon = xhCoupon::getAllByCondition(['userId' => $userId,'status' => 0]); $string = !empty($coupon) ? json_encode($coupon) : ''; Yii::$app->redis->executeCommand('SET', [$cacheKey,$string]); }else{ $cacheData = Yii::$app->redis->executeCommand('GET', [$cacheKey]); $coupon = !empty($cacheData) ? json_decode($cacheData,true) : []; if(!empty($coupon)){ $now = time(); foreach($coupon as $key => $val){ if($now > $val['deadline'] || $val['useStatus'] == 1){//代金劵已经过期 $couponId = $val['id']; $userId = $val['userId']; self::delById($couponId,$userId);//删除已经过期的代金劵 unset($coupon[$key]); } } } } return $coupon; } /** * 刷新缓存的用户代金劵 */ public static function refreshUserCoupon($userId) { $cacheKey = dict::getCacheKey('userCoupon').$userId; Yii::$app->redis->executeCommand('DEL', [$cacheKey]); self::getUserCoupon($userId); } public static function add($data,$userId) { xhCoupon::add($data); self::refreshUserCoupon($userId); } public static function delById($id,$userId) { xhCoupon::deleteById($id); $cacheKey = dict::getCacheKey('coupon').$id; Yii::$app->redis->executeCommand('DEL', [$cacheKey]); self::refreshUserCoupon($userId); } public static function getById($id) { $cacheKey = dict::getCacheKey('coupon').$id; $coupon = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]); if(!empty($coupon)){ $arr = []; $i = 0; $x = 0; foreach($coupon as $key => $val){ $i++; if($i%2 == 0){ $arr[$x]['value'] = $val; $x++; }else{ $arr[$x]['key'] = $val; } } $couponArr = []; foreach($arr as $key => $val){ $couponArr[$val['key']] = $val['value']; } unset($couponArr['info_already_get_by_sql']); return $couponArr; } $coupon = xhCoupon::find()->where(['id' => $id])->asArray()->one(); $params = [$cacheKey]; $params[] = 'info_already_get_by_sql'; $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过 if(!empty($coupon)){ foreach($coupon as $key => $val){ $params[] = $key; $params[] = $val; } } Yii::$app->redis->executeCommand('HMSET',$params);//将xhGoods信息放入缓存 return $coupon; } public static function updateById($id,$userId,$data) { xhCoupon::updateById($id, $data); $cacheKey = dict::getCacheKey('coupon').$id; $couponParams = [$cacheKey]; foreach($data as $key => $val){ $couponParams[] = $key; $couponParams[] = $val; } Yii::$app->redis->executeCommand('HMSET',$couponParams);//更新缓存信息 self::refreshUserCoupon($userId); } //关注公众号时,判断商家是否开启新客注册送券,并且客户提供了手机号,是则送券 ssh 2020.5.5 public static function focusGiveCoupon($userId,$merchant,$merchantExtend) { $giveCouponPrice = $merchantExtend['giveCouponPrice'];//代金劵金额 $couponMeetPrice = $merchantExtend['couponMeetPrice'];//消费额 $sjId = $merchant['id']; $userInfo = xhUserService::getById($userId); $duration = $merchantExtend['couponValidity'];//有效期 $amount = $giveCouponPrice; $meetAmount = $couponMeetPrice; $addData['beginTime'] = time(); $addData['deadline'] = $addData['beginTime'] + $duration * 86400; $addData['amount'] = $amount; $addData['meetAmount'] = $meetAmount; $addData['createTime'] = date("Y-m-d H:i:s"); $addData['sjId'] = $sjId; $addData['userId'] = $userId; $addData['number'] = 'CO'.stringUtil::buildOrderNo($userId);//代金劵编号 xhCouponService::add($addData,$userId); /* $allTM = xhTMessageService::getList($sjId); $shortTempId = 'OPENTM207112032';//收入提醒 $tempId = $allTM[$shortTempId]; $openId = $userInfo['openId']; $data = ["touser" => $openId,"template_id" => $tempId, "url" => Yii::$app->params['frontUrl'].'/center/coupon?account='.$merchant['id'], "data" => [ "first" => ["value" => "送您一张代金劵。","color" => "#173177"], "keyword1" => ["value" => '代金劵',"color" => "#173177"], "keyword2" => ["value" => $amount.'元',"color" => "#173177"], "keyword3" => ["value" => date("Y-m-d H:i"),"color" => "#173177"], "remark" => ["value" => "点击查看详情","color" => "#173177"]]]; wxUtil::sendTaskInform($data,$merchant); */ } }