| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace common\services;
- use common\components\dict;
- use common\components\wxUtil;
- use Yii;
- use common\components\stringUtil;
- use common\models\xhCoupon;
- class xhCouponService {
- public static function getUserValidCoupon($userId,$consumePrice)
- {
- $coupon = self::getUserCoupon($userId);
- if(!empty($coupon)){
- foreach($coupon as $key => $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);
- */
- }
- }
|