$price) { util::fail('优惠券要求最低消费' . $coupon['miniCost'] . '元'); } return $coupon['amount']; } //销卷并分成 ssh 2019.8.25 public static function destroyToGetReward($data, $sjId) { validate::easyCheck(['id'], $data); $couponId = $data['id']; if (empty($couponId)) { return false; } $coupon = self::getById($couponId, true); if (empty($coupon)) { util::fail('找不到优惠卷'); } if ($coupon['sjId'] != $sjId) { util::fail('你无法操作'); } $time = time(); if ($coupon->status != 0) { util::fail('优惠卷已使用或过期'); } if (!empty($coupon->deadline) && $coupon->deadline < $time) { $coupon->status = 2; $coupon->save(); util::fail('优惠卷已过期失效'); } $coupon->status = 1; $coupon->save(); $userInfo = UserService::getById($coupon['introUserId']); $mobile = isset($userInfo['mobile']) ? $userInfo['mobile'] : ''; /**老带新优惠券发放奖励**/ if ($coupon->property == 0) { $rewardData = []; $createTime = date("Y-m-d H:i:s"); $addTime = time(); $rewardData['reward'] = $coupon['reward']; $rewardData['sjId'] = $coupon['sjId']; $rewardData['couponId'] = $couponId; $rewardData['userId'] = $coupon['introUserId']; $rewardData['mobile'] = $mobile; $rewardData['invitedUserId'] = $coupon['userId']; $rewardData['status'] = 1; $rewardData['addTime'] = $addTime; $rewardData['createTime'] = $createTime; CouponRewardService::add($rewardData); /**通知受益人**/ if ($userInfo['subscribe']) { $inviteUserInfo = UserService::getById($coupon['userId']); $allTM = xhTMessageService::getList($coupon['sjId']); $shortTempId = 'OPENTM207422813';//收入提醒 if (isset($allTM[$shortTempId])) { $tempId = $allTM[$shortTempId]; $openId = $userInfo['openId']; $merchant = MerchantService::getById($coupon['sjId']); $data = ["touser" => $openId, "template_id" => $tempId, "url" => Yii::$app->params['mobile'] . '/center/myReward?account=' . $coupon['sjId'], "data" => [ "first" => ["value" => $inviteUserInfo['userName'] . '使用了您的优惠卷,您获得:' . $rewardData['reward'], "color" => "#173177"], "keyword1" => ["value" => '点击查看', "color" => "#173177"], "keyword2" => ["value" => '老带新活动', "color" => "#173177"], "keyword3" => ["value" => date("Y-m-d H:i"), "color" => "#173177"], "remark" => ["value" => "点击查看明细", "color" => "#173177"]]]; $return = wxUtil::sendTaskInform($data, $merchant); } Yii::info('销卷分成通知收益人结果:' . json_encode($return)); } } } //是否领取过老带新的卷 ssh 2019.8.25 public static function hasGained($userId, $sjId) { $return = self::getByCondition(['sjId' => $sjId, 'userId' => $userId]); return empty($return) ? false : true; } //获取我的优惠卷 $status null全部 0未使用 1使用 2过期 public static function getMyCoupon($userId, $status = null) { $where = ['userId' => $userId]; if (isset($status) != false) { $where['status'] = $status; } return self::getAllByCondition($where, 'addTime desc', '*'); } //取出部分可用的优惠卷 ssh 2019.8.28 public static function getAvailableCouponList($data, $userId, $sjId) { validate::easyCheck(['categoryId', 'usageId', 'amount'], $data); $amount = $data['amount']; $categoryId = $data['categoryId']; $usageId = $data['usageId']; $respond = self::getListData('*', ['sjId' => $sjId, 'userId' => $userId], 1, 20, 'id desc'); $list = isset($respond['list']) && !empty($respond['list']) ? $respond['list'] : []; $time = time(); if (!empty($list)) { foreach ($list as $key => $val) { if ($val['deadline'] <= $time) { unset($list[$key]); } if ($val['meetAmount'] != 0 && $val['meetAmount'] > $amount) { unset($list[$key]); } if ($val['status'] != 0) { unset($list[$key]); } if ($val['type'] != 0) { if ($val['type'] == 1 && $val['targetId'] != $categoryId) { unset($list[$key]); } if ($val['type'] == 2 && $val['targetId'] != $usageId) { unset($list[$key]); } } } } return $list; } //判断用户是否有卷 public static function hasCoupon($userId, $sjId) { $list = self::getAllByCondition(['sjId' => $sjId, 'userId' => $userId], 'id desc', '*'); $hasCoupon = false; $time = time(); foreach ($list as $val) { if ($val['deadline'] > $time && $val['status'] == 0) { $hasCoupon = true; } } return $hasCoupon; } //查询优惠券将过期的状态变为过期 ssh 2019.9.3 public static function setDeadline($list) { if (empty($list)) { return false; } $ids = []; $time = time(); foreach ($list as $key => $val) { if ($val['status'] == 0 && $val['deadline'] < $time) { $ids[] = $val['id']; } } if (!empty($ids)) { self::updateByIds($ids, ['status' => 2]); } } //付款成功后领取优惠券 ssh 2019.9.17 public static function paySuccessGetCoupon($userId, $sjId) { /**确认是否可领卷**/ $flag = UserAssetService::couldGetCoupon($userId); if ($flag == -1) { return false; } /**列出首次、二次和三次的卷**/ $commonCoupon = CouponModelService::getCommonCoupon(); if (count($commonCoupon) != 3) { Yii::info('sjId:' . $sjId . '还没有设置通用卷(首次卷、二次卷、三次卷)'); return false; } $userInfo = UserService::getById($userId); $mobile = $userInfo['mobile']; /**领卷**/ $model = isset($commonCoupon[$flag]) ? $commonCoupon[$flag] : []; if (empty($model)) { return false; } $modelId = $model['id']; $data = []; $time = time(); $date = date("Y-m-d H:i:s"); $data['deadline'] = empty($model['validTime']) ? 4294967295 : ($time + $model['validTime'] * 86400); $data['name'] = $model['name']; $data['amount'] = $model['amount']; $data['meetAmount'] = $model['meetAmount']; $data['userId'] = $userId; $data['introUserId'] = 0; $data['sjId'] = $model['sjId']; $data['reward'] = ''; $data['type'] = $model['type']; $data['targetId'] = $model['targetId']; $data['beginTime'] = $time; $data['orderId'] = 0; $data['addTime'] = $time; $data['mobile'] = $mobile; $data['createTime'] = $date; $data['rightId'] = 0; $data['modelId'] = $modelId; CouponService::add($data); //通知领卷人 if (isset($userInfo['subscribe']) && !empty($userInfo['subscribe'])) { $merchant = MerchantService::getById($sjId); $openId = $userInfo['openId']; $allTM = xhTMessageService::getList($sjId); $shortTempId = 'OPENTM207430125'; if (isset($allTM[$shortTempId])) { $tempId = $allTM[$shortTempId]; $url = Yii::$app->params['mobileUrl'] . '/center/my-coupon?account=' . $sjId; $data = [ "touser" => $openId, "template_id" => $tempId, "url" => $url, "data" => ["first" => ["value" => '恭喜您', "color" => "#173177"], "keyword1" => ["value" => "领卷成功,点此查看。", "color" => "#173177"], "keyword2" => ["value" => date("Y-m-d H:i"), "color" => "#173177"], "remark" => ["value" => "", "color" => "#173177"]]]; wxUtil::sendTaskInform($data, $merchant); } } return true; } }