CouponService.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace bizHd\promote\services;
  3. use bizHd\base\services\BaseService;
  4. use biz\sj\services\MerchantService;
  5. use bizHd\promote\classes\CouponClass;
  6. use bizHd\user\services\UserAssetService;
  7. use bizHd\user\services\UserService;
  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 CouponService extends BaseService
  14. {
  15. public static $baseFile = '\bizHd\promote\classes\CouponClass';
  16. //优惠券列表 ssh 2019.12.11
  17. public static function getCouponList($where)
  18. {
  19. $data = CouponClass::getList('*', $where, 'addTime DESC');
  20. $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
  21. if (empty($list)) {
  22. return $data;
  23. }
  24. $data['list'] = CouponClass::groupCouponBaseInfo($list);
  25. return $data;
  26. }
  27. //验证使用的优惠券是否有效
  28. public static function checkBeforeUse($couponId, $price, $userId)
  29. {
  30. $coupon = CouponClass::getById($couponId);
  31. if (empty($coupon)) {
  32. util::fail('优惠券无效');
  33. }
  34. if ($coupon['userId'] != $userId) {
  35. util::fail('您没有权限使用');
  36. }
  37. $time = time();
  38. if ($coupon['deadline'] < $time) {
  39. CouponClass::setExpire($coupon);
  40. util::fail('优惠券已过期');
  41. }
  42. if ($coupon['status'] != 0) {
  43. util::fail('优惠券不可用');
  44. }
  45. if ($coupon['miniCost'] > $price) {
  46. util::fail('优惠券要求最低消费' . $coupon['miniCost'] . '元');
  47. }
  48. return $coupon['amount'];
  49. }
  50. //销卷并分成 ssh 2019.8.25
  51. public static function destroyToGetReward($data, $sjId)
  52. {
  53. validate::easyCheck(['id'], $data);
  54. $couponId = $data['id'];
  55. if (empty($couponId)) {
  56. return false;
  57. }
  58. $coupon = self::getById($couponId, true);
  59. if (empty($coupon)) {
  60. util::fail('找不到优惠卷');
  61. }
  62. if ($coupon['sjId'] != $sjId) {
  63. util::fail('你无法操作');
  64. }
  65. $time = time();
  66. if ($coupon->status != 0) {
  67. util::fail('优惠卷已使用或过期');
  68. }
  69. if (!empty($coupon->deadline) && $coupon->deadline < $time) {
  70. $coupon->status = 2;
  71. $coupon->save();
  72. util::fail('优惠卷已过期失效');
  73. }
  74. $coupon->status = 1;
  75. $coupon->save();
  76. $userInfo = UserService::getById($coupon['introUserId']);
  77. $mobile = isset($userInfo['mobile']) ? $userInfo['mobile'] : '';
  78. /**老带新优惠券发放奖励**/
  79. if ($coupon->property == 0) {
  80. $rewardData = [];
  81. $createTime = date("Y-m-d H:i:s");
  82. $addTime = time();
  83. $rewardData['reward'] = $coupon['reward'];
  84. $rewardData['sjId'] = $coupon['sjId'];
  85. $rewardData['couponId'] = $couponId;
  86. $rewardData['userId'] = $coupon['introUserId'];
  87. $rewardData['mobile'] = $mobile;
  88. $rewardData['invitedUserId'] = $coupon['userId'];
  89. $rewardData['status'] = 1;
  90. $rewardData['addTime'] = $addTime;
  91. $rewardData['createTime'] = $createTime;
  92. CouponRewardService::add($rewardData);
  93. /**通知受益人**/
  94. if ($userInfo['subscribe']) {
  95. $inviteUserInfo = UserService::getById($coupon['userId']);
  96. $allTM = xhTMessageService::getList($coupon['sjId']);
  97. $shortTempId = 'OPENTM207422813';//收入提醒
  98. if (isset($allTM[$shortTempId])) {
  99. $tempId = $allTM[$shortTempId];
  100. $openId = $userInfo['openId'];
  101. $merchant = MerchantService::getById($coupon['sjId']);
  102. $data = ["touser" => $openId, "template_id" => $tempId,
  103. "url" => Yii::$app->params['mobile'] . '/center/myReward?account=' . $coupon['sjId'], "data" => [
  104. "first" => ["value" => $inviteUserInfo['userName'] . '使用了您的优惠卷,您获得:' . $rewardData['reward'], "color" => "#173177"],
  105. "keyword1" => ["value" => '点击查看', "color" => "#173177"],
  106. "keyword2" => ["value" => '老带新活动', "color" => "#173177"],
  107. "keyword3" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
  108. "remark" => ["value" => "点击查看明细", "color" => "#173177"]]];
  109. $return = wxUtil::sendTaskInform($data, $merchant);
  110. }
  111. Yii::info('销卷分成通知收益人结果:' . json_encode($return));
  112. }
  113. }
  114. }
  115. //是否领取过老带新的卷 ssh 2019.8.25
  116. public static function hasGained($userId, $sjId)
  117. {
  118. $return = self::getByCondition(['sjId' => $sjId, 'userId' => $userId]);
  119. return empty($return) ? false : true;
  120. }
  121. //获取我的优惠卷 $status null全部 0未使用 1使用 2过期
  122. public static function getMyCoupon($userId, $status = null)
  123. {
  124. $where = ['userId' => $userId];
  125. if (isset($status) != false) {
  126. $where['status'] = $status;
  127. }
  128. return self::getAllByCondition($where, 'addTime desc', '*');
  129. }
  130. //取出部分可用的优惠卷 ssh 2019.8.28
  131. public static function getAvailableCouponList($data, $userId, $sjId)
  132. {
  133. validate::easyCheck(['categoryId', 'usageId', 'amount'], $data);
  134. $amount = $data['amount'];
  135. $categoryId = $data['categoryId'];
  136. $usageId = $data['usageId'];
  137. $respond = self::getListData('*', ['sjId' => $sjId, 'userId' => $userId], 1, 20, 'id desc');
  138. $list = isset($respond['list']) && !empty($respond['list']) ? $respond['list'] : [];
  139. $time = time();
  140. if (!empty($list)) {
  141. foreach ($list as $key => $val) {
  142. if ($val['deadline'] <= $time) {
  143. unset($list[$key]);
  144. }
  145. if ($val['meetAmount'] != 0 && $val['meetAmount'] > $amount) {
  146. unset($list[$key]);
  147. }
  148. if ($val['status'] != 0) {
  149. unset($list[$key]);
  150. }
  151. if ($val['type'] != 0) {
  152. if ($val['type'] == 1 && $val['targetId'] != $categoryId) {
  153. unset($list[$key]);
  154. }
  155. if ($val['type'] == 2 && $val['targetId'] != $usageId) {
  156. unset($list[$key]);
  157. }
  158. }
  159. }
  160. }
  161. return $list;
  162. }
  163. //判断用户是否有卷
  164. public static function hasCoupon($userId, $sjId)
  165. {
  166. $list = self::getAllByCondition(['sjId' => $sjId, 'userId' => $userId], 'id desc', '*');
  167. $hasCoupon = false;
  168. $time = time();
  169. foreach ($list as $val) {
  170. if ($val['deadline'] > $time && $val['status'] == 0) {
  171. $hasCoupon = true;
  172. }
  173. }
  174. return $hasCoupon;
  175. }
  176. //查询优惠券将过期的状态变为过期 ssh 2019.9.3
  177. public static function setDeadline($list)
  178. {
  179. if (empty($list)) {
  180. return false;
  181. }
  182. $ids = [];
  183. $time = time();
  184. foreach ($list as $key => $val) {
  185. if ($val['status'] == 0 && $val['deadline'] < $time) {
  186. $ids[] = $val['id'];
  187. }
  188. }
  189. if (!empty($ids)) {
  190. self::updateByIds($ids, ['status' => 2]);
  191. }
  192. }
  193. //付款成功后领取优惠券 ssh 2019.9.17
  194. public static function paySuccessGetCoupon($userId, $sjId)
  195. {
  196. /**确认是否可领卷**/
  197. $flag = UserAssetService::couldGetCoupon($userId);
  198. if ($flag == -1) {
  199. return false;
  200. }
  201. /**列出首次、二次和三次的卷**/
  202. $commonCoupon = CouponModelService::getCommonCoupon();
  203. if (count($commonCoupon) != 3) {
  204. Yii::info('sjId:' . $sjId . '还没有设置通用卷(首次卷、二次卷、三次卷)');
  205. return false;
  206. }
  207. $userInfo = UserService::getById($userId);
  208. $mobile = $userInfo['mobile'];
  209. /**领卷**/
  210. $model = isset($commonCoupon[$flag]) ? $commonCoupon[$flag] : [];
  211. if (empty($model)) {
  212. return false;
  213. }
  214. $modelId = $model['id'];
  215. $data = [];
  216. $time = time();
  217. $date = date("Y-m-d H:i:s");
  218. $data['deadline'] = empty($model['validTime']) ? 4294967295 : ($time + $model['validTime'] * 86400);
  219. $data['name'] = $model['name'];
  220. $data['amount'] = $model['amount'];
  221. $data['meetAmount'] = $model['meetAmount'];
  222. $data['userId'] = $userId;
  223. $data['introUserId'] = 0;
  224. $data['sjId'] = $model['sjId'];
  225. $data['reward'] = '';
  226. $data['type'] = $model['type'];
  227. $data['targetId'] = $model['targetId'];
  228. $data['beginTime'] = $time;
  229. $data['orderId'] = 0;
  230. $data['addTime'] = $time;
  231. $data['mobile'] = $mobile;
  232. $data['createTime'] = $date;
  233. $data['rightId'] = 0;
  234. $data['modelId'] = $modelId;
  235. CouponService::add($data);
  236. //通知领卷人
  237. if (isset($userInfo['subscribe']) && !empty($userInfo['subscribe'])) {
  238. $merchant = MerchantService::getById($sjId);
  239. $openId = $userInfo['openId'];
  240. $allTM = xhTMessageService::getList($sjId);
  241. $shortTempId = 'OPENTM207430125';
  242. if (isset($allTM[$shortTempId])) {
  243. $tempId = $allTM[$shortTempId];
  244. $url = Yii::$app->params['mobileUrl'] . '/center/my-coupon?account=' . $sjId;
  245. $data = [
  246. "touser" => $openId, "template_id" => $tempId, "url" => $url,
  247. "data" => ["first" => ["value" => '恭喜您', "color" => "#173177"],
  248. "keyword1" => ["value" => "领卷成功,点此查看。", "color" => "#173177"],
  249. "keyword2" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
  250. "remark" => ["value" => "", "color" => "#173177"]]];
  251. wxUtil::sendTaskInform($data, $merchant);
  252. }
  253. }
  254. return true;
  255. }
  256. }