| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- namespace bizHd\promote\services;
- use bizHd\base\services\BaseService;
- use biz\sj\services\MerchantService;
- use bizHd\promote\classes\CouponClass;
- use bizHd\user\services\UserAssetService;
- use bizHd\user\services\UserService;
- use common\components\validate;
- use common\components\wxUtil;
- use common\components\util;
- use common\services\xhTMessageService;
- use Yii;
- class CouponService extends BaseService
- {
-
- public static $baseFile = '\bizHd\promote\classes\CouponClass';
-
- //优惠券列表 ssh 2019.12.11
- public static function getCouponList($where)
- {
- $data = CouponClass::getList('*', $where, 'addTime DESC');
- $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
- if (empty($list)) {
- return $data;
- }
- $data['list'] = CouponClass::groupCouponBaseInfo($list);
- return $data;
- }
-
- //验证使用的优惠券是否有效
- public static function checkBeforeUse($couponId, $price, $userId)
- {
- $coupon = CouponClass::getById($couponId);
- if (empty($coupon)) {
- util::fail('优惠券无效');
- }
- if ($coupon['userId'] != $userId) {
- util::fail('您没有权限使用');
- }
- $time = time();
- if ($coupon['deadline'] < $time) {
- CouponClass::setExpire($coupon);
- util::fail('优惠券已过期');
- }
- if ($coupon['status'] != 0) {
- util::fail('优惠券不可用');
- }
- if ($coupon['miniCost'] > $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;
- }
-
- }
|