| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- namespace bizHd\promote\services;
- use bizHd\base\services\BaseService;
- use biz\sj\services\MerchantService;
- use bizHd\user\services\UserAssetService;
- use bizHd\user\services\UserService;
- use common\components\error;
- use common\components\validate;
- use common\components\wxUtil;
- use common\components\util;
- use common\services\xhTMessageService;
- use Yii;
- class CouponRightService extends BaseService
- {
-
- public static $baseFile = '\bizHd\promote\classes\CouponRightClass';
-
- //给用户增加发卷的权力 ssh
- public static function addRight($order)
- {
- }
-
- //老带新详情和优惠卷详情 ssh 2019.8.25
- public static function getRightDetail($id)
- {
- $right = self::getById($id);
- if (empty($right)) {
- util::fail('没有老带新优惠卷');
- }
- $modelId = $right['modelId'];
- $modeInfo = CouponModelService::getById($modelId);
- $right['modelInfo'] = $modeInfo;
- return $right;
- }
-
- //我的可以分享的优惠卷 ssh
- public static function myShare()
- {
- }
-
- //给予老带新优惠卷发放权 ssh
- public static function grantRight($order, $merchant)
- {
- $categoryId = $order['categoryId'];
- $usageId = $order['usageId'];
- $rightList = CouponModelService::getAvailableCouponListById($categoryId, $usageId);
- if (empty($rightList)) {
- return false;
- }
- $theRight = current($rightList);
- $id = $theRight['id'];
- $modelInfo = CouponModelService::getById($id, true);
- if (empty($modelInfo)) {
- util::fail('优惠券不存在');
- }
- /**优惠券停用**/
- if ($modelInfo->status == 0) {
- return false;
- }
- $userId = $order['userId'];
- $hasRight = self::getByCondition(['userId' => $userId, 'modelId' => $id]);
- //已经有这个权利不再进行发放
- if ($hasRight) {
- return false;
- }
- $orderId = $order['id'];
- $sjId = $order['sjId'];
- $time = time();
- $data = ['userId' => $userId, 'modelId' => $id, 'orderId' => $orderId, 'sjId' => $sjId, 'type' => $theRight['type'], 'targetId' => $theRight['targetId'],
- 'amount' => $theRight['amount'], 'meetAmount' => $theRight['meetAmount'], 'addTime' => $time];
- self::add($data);
- $modelInfo->rightNum += 1;
- $modelInfo->save();
-
- //消息通知
- $allTM = xhTMessageService::getList($sjId);
- $user = UserService::getById($userId);
-
- if ($user['subscribe'] == 1) {
- $shortTempId = 'OPENTM207422813';//收入提醒
- if (isset($allTM[$shortTempId])) {
- $tempId = $allTM[$shortTempId];
- $openId = $user['openId'];
- $data = ["touser" => $openId, "template_id" => $tempId,
- "url" => Yii::$app->params['mobileUrl'] . '/center/my-right?account=' . $merchant['id'],
- "data" => [
- "first" => ["value" => '您获得一张老带新优惠卷', "color" => "#173177"],
- "keyword1" => ["value" => '点击查看', "color" => "#173177"],
- "keyword2" => ["value" => '老带新活动', "color" => "#173177"],
- "keyword3" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
- "remark" => ["value" => "点击查看明细", "color" => "#173177"]]];
- wxUtil::sendTaskInform($data, $merchant);
- }
- }
- }
-
- //分享出去之后领取优惠卷 ssh
- public static function gainCoupon($data,$userId)
- {
- validate::easyCheck(['rightId'], $data);
- $rightId = $data['rightId'];
- $right = self::getById($rightId);
- if (empty($right)) {
- util::fail('没有找到优惠卷');
- }
- if ($right['userId'] == $userId) {
- util::fail('不能领自己发的卷');
- }
- $sjId = $right['sjId'];
- $modelId = $right['modelId'];
- $model = CouponModelService::getById($modelId);
- if (empty($model)) {
- util::fail('没有找到优惠卷!');
- }
- if ($model['delStatus'] == 1) {
- util::fail('优惠卷已经删除!');
- }
- if ($model['status'] == 0) {
- util::fail('优惠卷已经停止领取');
- }
- $isNew = UserAssetService::isNewCustomer($userId);
- if ($isNew == false) {
- util::fail('新客才能领取哦');
- }
- $has = CouponService::hasGained();
- if ($has) {
- util::fail('您已经领取过优惠卷了');
- }
- $userInfo = UserService::getById($userId);
- $mobile = isset($userInfo['mobile']) ? $userInfo['mobile'] : '';
- $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'] = $right['userId'];
- $data['sjId'] = $model['sjId'];
- $data['reward'] = $model['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'] = $rightId;
- $data['modelId'] = $modelId;
- CouponService::add($data);
-
- //领卷数+1
- $getNum = $model['getNum'] + 1;
- CouponModelService::updateById($modelId, ['getNum' => $getNum]);
-
- $merchant = MerchantService::getById($sjId);
-
- //通知领卷人
- if (isset($userInfo['subscribe']) && !empty($userInfo['subscribe'])) {
- $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);
- }
- }
-
- //通知发卷人
- $introduceUserId = $right['userId'];
- $introduceUserInfo = UserService::getById($introduceUserId);
- if (isset($introduceUserInfo['subscribe']) && !empty($introduceUserInfo['subscribe'])) {
- $openId = $introduceUserInfo['openId'];
- $allTM = xhTMessageService::getList($sjId);
- $shortTempId = 'OPENTM207430125';
- if (isset($allTM[$shortTempId])) {
- $tempId = $allTM[$shortTempId];
- $url = Yii::$app->params['mobileUrl'] . '/center/get-coupon?account=' . $sjId;
- $data = [
- "touser" => $openId, "template_id" => $tempId, "url" => $url,
- "data" => ["first" => ["value" => '恭喜您', "color" => "#173177"],
- "keyword1" => ["value" => "您的伙伴【{$userInfo['userName']}】已经领取您的优惠券。", "color" => "#173177"],
- "keyword2" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
- "remark" => ["value" => "点此查看", "color" => "#173177"]]];
- wxUtil::sendTaskInform($data, $merchant);
- }
- }
- return true;
- }
-
- }
|