CouponRightService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace bizHd\promote\services;
  3. use bizHd\base\services\BaseService;
  4. use biz\sj\services\MerchantService;
  5. use bizHd\user\services\UserAssetService;
  6. use bizHd\user\services\UserService;
  7. use common\components\error;
  8. use common\components\validate;
  9. use common\components\wxUtil;
  10. use common\components\util;
  11. use common\services\xhTMessageService;
  12. use Yii;
  13. class CouponRightService extends BaseService
  14. {
  15. public static $baseFile = '\bizHd\promote\classes\CouponRightClass';
  16. //给用户增加发卷的权力 ssh
  17. public static function addRight($order)
  18. {
  19. }
  20. //老带新详情和优惠卷详情 ssh 2019.8.25
  21. public static function getRightDetail($id)
  22. {
  23. $right = self::getById($id);
  24. if (empty($right)) {
  25. util::fail('没有老带新优惠卷');
  26. }
  27. $modelId = $right['modelId'];
  28. $modeInfo = CouponModelService::getById($modelId);
  29. $right['modelInfo'] = $modeInfo;
  30. return $right;
  31. }
  32. //我的可以分享的优惠卷 ssh
  33. public static function myShare()
  34. {
  35. }
  36. //给予老带新优惠卷发放权 ssh
  37. public static function grantRight($order, $merchant)
  38. {
  39. $categoryId = $order['categoryId'];
  40. $usageId = $order['usageId'];
  41. $rightList = CouponModelService::getAvailableCouponListById($categoryId, $usageId);
  42. if (empty($rightList)) {
  43. return false;
  44. }
  45. $theRight = current($rightList);
  46. $id = $theRight['id'];
  47. $modelInfo = CouponModelService::getById($id, true);
  48. if (empty($modelInfo)) {
  49. util::fail('优惠券不存在');
  50. }
  51. /**优惠券停用**/
  52. if ($modelInfo->status == 0) {
  53. return false;
  54. }
  55. $userId = $order['userId'];
  56. $hasRight = self::getByCondition(['userId' => $userId, 'modelId' => $id]);
  57. //已经有这个权利不再进行发放
  58. if ($hasRight) {
  59. return false;
  60. }
  61. $orderId = $order['id'];
  62. $sjId = $order['sjId'];
  63. $time = time();
  64. $data = ['userId' => $userId, 'modelId' => $id, 'orderId' => $orderId, 'sjId' => $sjId, 'type' => $theRight['type'], 'targetId' => $theRight['targetId'],
  65. 'amount' => $theRight['amount'], 'meetAmount' => $theRight['meetAmount'], 'addTime' => $time];
  66. self::add($data);
  67. $modelInfo->rightNum += 1;
  68. $modelInfo->save();
  69. //消息通知
  70. $allTM = xhTMessageService::getList($sjId);
  71. $user = UserService::getById($userId);
  72. if ($user['subscribe'] == 1) {
  73. $shortTempId = 'OPENTM207422813';//收入提醒
  74. if (isset($allTM[$shortTempId])) {
  75. $tempId = $allTM[$shortTempId];
  76. $openId = $user['openId'];
  77. $data = ["touser" => $openId, "template_id" => $tempId,
  78. "url" => Yii::$app->params['mobileUrl'] . '/center/my-right?account=' . $merchant['id'],
  79. "data" => [
  80. "first" => ["value" => '您获得一张老带新优惠卷', "color" => "#173177"],
  81. "keyword1" => ["value" => '点击查看', "color" => "#173177"],
  82. "keyword2" => ["value" => '老带新活动', "color" => "#173177"],
  83. "keyword3" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
  84. "remark" => ["value" => "点击查看明细", "color" => "#173177"]]];
  85. wxUtil::sendTaskInform($data, $merchant);
  86. }
  87. }
  88. }
  89. //分享出去之后领取优惠卷 ssh
  90. public static function gainCoupon($data,$userId)
  91. {
  92. validate::easyCheck(['rightId'], $data);
  93. $rightId = $data['rightId'];
  94. $right = self::getById($rightId);
  95. if (empty($right)) {
  96. util::fail('没有找到优惠卷');
  97. }
  98. if ($right['userId'] == $userId) {
  99. util::fail('不能领自己发的卷');
  100. }
  101. $sjId = $right['sjId'];
  102. $modelId = $right['modelId'];
  103. $model = CouponModelService::getById($modelId);
  104. if (empty($model)) {
  105. util::fail('没有找到优惠卷!');
  106. }
  107. if ($model['delStatus'] == 1) {
  108. util::fail('优惠卷已经删除!');
  109. }
  110. if ($model['status'] == 0) {
  111. util::fail('优惠卷已经停止领取');
  112. }
  113. $isNew = UserAssetService::isNewCustomer($userId);
  114. if ($isNew == false) {
  115. util::fail('新客才能领取哦');
  116. }
  117. $has = CouponService::hasGained();
  118. if ($has) {
  119. util::fail('您已经领取过优惠卷了');
  120. }
  121. $userInfo = UserService::getById($userId);
  122. $mobile = isset($userInfo['mobile']) ? $userInfo['mobile'] : '';
  123. $data = [];
  124. $time = time();
  125. $date = date("Y-m-d H:i:s");
  126. $data['deadline'] = empty($model['validTime']) ? 4294967295 : ($time + $model['validTime'] * 86400);
  127. $data['name'] = $model['name'];
  128. $data['amount'] = $model['amount'];
  129. $data['meetAmount'] = $model['meetAmount'];
  130. $data['userId'] = $userId;
  131. $data['introUserId'] = $right['userId'];
  132. $data['sjId'] = $model['sjId'];
  133. $data['reward'] = $model['reward'];
  134. $data['type'] = $model['type'];
  135. $data['targetId'] = $model['targetId'];
  136. $data['beginTime'] = $time;
  137. $data['orderId'] = 0;
  138. $data['addTime'] = $time;
  139. $data['mobile'] = $mobile;
  140. $data['createTime'] = $date;
  141. $data['rightId'] = $rightId;
  142. $data['modelId'] = $modelId;
  143. CouponService::add($data);
  144. //领卷数+1
  145. $getNum = $model['getNum'] + 1;
  146. CouponModelService::updateById($modelId, ['getNum' => $getNum]);
  147. $merchant = MerchantService::getById($sjId);
  148. //通知领卷人
  149. if (isset($userInfo['subscribe']) && !empty($userInfo['subscribe'])) {
  150. $openId = $userInfo['openId'];
  151. $allTM = xhTMessageService::getList($sjId);
  152. $shortTempId = 'OPENTM207430125';
  153. if (isset($allTM[$shortTempId])) {
  154. $tempId = $allTM[$shortTempId];
  155. $url = Yii::$app->params['mobileUrl'] . '/center/my-coupon?account=' . $sjId;
  156. $data = [
  157. "touser" => $openId, "template_id" => $tempId, "url" => $url,
  158. "data" => ["first" => ["value" => '恭喜您', "color" => "#173177"],
  159. "keyword1" => ["value" => "领卷成功,点此查看。", "color" => "#173177"],
  160. "keyword2" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
  161. "remark" => ["value" => "", "color" => "#173177"]]];
  162. wxUtil::sendTaskInform($data, $merchant);
  163. }
  164. }
  165. //通知发卷人
  166. $introduceUserId = $right['userId'];
  167. $introduceUserInfo = UserService::getById($introduceUserId);
  168. if (isset($introduceUserInfo['subscribe']) && !empty($introduceUserInfo['subscribe'])) {
  169. $openId = $introduceUserInfo['openId'];
  170. $allTM = xhTMessageService::getList($sjId);
  171. $shortTempId = 'OPENTM207430125';
  172. if (isset($allTM[$shortTempId])) {
  173. $tempId = $allTM[$shortTempId];
  174. $url = Yii::$app->params['mobileUrl'] . '/center/get-coupon?account=' . $sjId;
  175. $data = [
  176. "touser" => $openId, "template_id" => $tempId, "url" => $url,
  177. "data" => ["first" => ["value" => '恭喜您', "color" => "#173177"],
  178. "keyword1" => ["value" => "您的伙伴【{$userInfo['userName']}】已经领取您的优惠券。", "color" => "#173177"],
  179. "keyword2" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
  180. "remark" => ["value" => "点此查看", "color" => "#173177"]]];
  181. wxUtil::sendTaskInform($data, $merchant);
  182. }
  183. }
  184. return true;
  185. }
  186. }