xhUserAssetService.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace common\services;
  3. use biz\user\services\UserIntegralService;
  4. use common\models\xhActive;
  5. use Yii;
  6. use common\components\configDict;
  7. use common\components\stringUtil;
  8. use common\components\wxUtil;
  9. use common\models\xhUserAsset;
  10. class xhUserAssetService
  11. {
  12. /**
  13. * 获取用户资产信息
  14. */
  15. public static function getByUserId($userId)
  16. {
  17. return xhUserAsset::find()->where(['userId' => $userId])->asArray()->one();
  18. }
  19. public static function updateByUserId($userId, $data)
  20. {
  21. xhUserAsset::updateByCondition(['userId' => $userId], $data);
  22. }
  23. public static function add($userId, $data)
  24. {
  25. $user = xhUserAsset::add($data);
  26. self::getByUserId($userId);
  27. return $user;
  28. }
  29. /**
  30. * 用户资金变化增加积分
  31. * 充值、微信付款、余额付款等改变用户资产
  32. * 此方法用于资金增减并引起积分增加,会员等级升级
  33. */
  34. public static function ioChangeAsset($user, $userAsset, $upAssetData, $merchant, $merchantExtend, $order, $capitalType)
  35. {
  36. $userId = $user['id'];
  37. $merchantId = $merchant['id'];
  38. $orderId = $order['id'];
  39. $openId = $user['openId'];
  40. $integral = isset($upAssetData['integral']) ? $upAssetData['integral'] : 0;
  41. $preLevel = $userAsset['member'];
  42. $preIntegral = $userAsset['integral'];
  43. $addIntegral = stringUtil::calcSub($integral, $preIntegral);
  44. $allTM = xhTMessageService::getList($merchantId);
  45. //没有积分,后续升级流程不需要再走
  46. if (empty($integral)) {
  47. self::updateByUserId($userId, $upAssetData);
  48. return;
  49. }
  50. $typeList = configDict::getConfig('capitalType');//充值支付涉及的流水类型列表
  51. switch ($capitalType) {//根据流水类型定制通知内容
  52. case $typeList['xhOrder']['id']://购买商品
  53. $goodsNum = $order['goodsNum'];
  54. $totalFee = $order['actPrice'];
  55. $name = $order['orderName'];
  56. $sourceType = $order['sourceType'];
  57. if ($sourceType == 0) {
  58. $shortTempId = 'OPENTM202297555';//下单成功通知
  59. $tempId = isset($allTM[$shortTempId]) ? $allTM[$shortTempId] : 0;
  60. if (!empty($tempId)) {
  61. $data = [
  62. "touser" => $openId,
  63. "template_id" => $tempId,
  64. "url" => Yii::$app->params['frontUrl'] . "/center/order-detail?id=$orderId&account=" . $merchant['id'],
  65. "data" => [
  66. "first" => ["value" => '亲,您已成功下单!', "color" => "#173177"],
  67. "keyword1" => ["value" => (string)$orderId, "color" => "#173177"],
  68. "keyword2" => ["value" => $name, "color" => "#173177"],
  69. "keyword3" => ["value" => $goodsNum . '件', "color" => "#173177"],
  70. "keyword4" => ["value" => number_format($totalFee, 2) . '元', "color" => "#173177"],
  71. "keyword5" => ["value" => '微信支付', "color" => "#173177"],
  72. "remark" => ["value" => "", "color" => "#173177"]]];
  73. wxUtil::sendTaskInform($data, $merchant);
  74. }
  75. } else {
  76. if ($addIntegral > 0) {
  77. $shortTempId = 'TM00230';
  78. $tempId = isset($allTM[$shortTempId]) ? $allTM[$shortTempId] : 0;
  79. if (!empty($tempId)) {
  80. $changeData = ["touser" => $openId, "template_id" => $tempId,
  81. "url" => Yii::$app->params['frontUrl'] . "/center/order-detail?id=$orderId&account=" . $merchant['id'],
  82. "data" => [
  83. "first" => ["value" => "您在本店购物获得积分", "color" => "#173177"],
  84. "FieldName" => ["value" => "会员名称", "color" => "#0F0F0F"],
  85. "Account" => ["value" => $user['userName'], "color" => "#173177"],
  86. "change" => ["value" => "增加", "color" => "#0F0F0F"],
  87. "CreditChange" => ["value" => $addIntegral, "color" => "#173177"],
  88. "CreditTotal" => ["value" => $integral, "color" => "#173177"],
  89. "Remark" => ["value" => "点此查看订单明细、添加编辑收花人信息", "color" => "#173177"],],];
  90. wxUtil::sendTaskInform($changeData, $merchant);
  91. }
  92. }
  93. }
  94. break;
  95. case $typeList['xhActiveOrder']['id']://活动报名
  96. $activeId = $order['activeId'];
  97. $active = xhActiveService::getById($activeId);
  98. $newApplyNum = $active['applyNum'] + 1;//活动报名人数+1
  99. xhActiveService::updateById($activeId, ['applyNum' => $newApplyNum]);
  100. $shortTempId = 'TM00575';//报名成功通知
  101. $tempId = isset($allTM[$shortTempId]) ? $allTM[$shortTempId] : 0;
  102. if (!empty($tempId)) {
  103. $changeData = ["touser" => $openId, "template_id" => $tempId,
  104. "url" => Yii::$app->params['frontUrl'] . '/active/order?id=' . $orderId . '&account=' . $merchant['id'],
  105. "data" => [
  106. "first" => ["value" => "您好,您已报名成功", "color" => "#173177"],
  107. "keynote1" => ["value" => $active['title'], "color" => "#173177"],
  108. "keynote2" => ["value" => date("m-d H:i", $active['startTime']), "color" => "#173177"],
  109. "keynote3" => ["value" => $active['address'], "color" => "#173177"],
  110. "remark" => ["value" => "欢迎您的参加哦!", "color" => "#173177"],],];
  111. wxUtil::sendTaskInform($changeData, $merchant);
  112. }
  113. break;
  114. case $typeList['xhRecharge']['id']://现金充值
  115. $totalBalance = isset($upAssetData['balance']) ? $upAssetData['balance'] : 0;
  116. $rechargeAmount = $order['amount'];
  117. $url = empty($user['payPassword']) ? Yii::$app->params['frontUrl'] . '/center/password?account=' . $merchant['id'] : '';
  118. $remark = empty($user['payPassword']) ? '您还没有设置支付密码!点此进行设置。' : '您当前帐户余额:' . $totalBalance . '元';
  119. $shortTempId = 'TM00006';//充值成功通知用户
  120. $tempId = isset($allTM[$shortTempId]) ? $allTM[$shortTempId] : 0;
  121. if (!empty($tempId)) {
  122. $data = ["touser" => $openId, "template_id" => $tempId, "url" => $url, "data" => [
  123. "first" => ["value" => '亲,您已充值成功。', "color" => "#173177"],
  124. "accountType" => ["value" => '会员类型', "color" => "#000000"],
  125. "account" => ["value" => '普通', "color" => "#173177"],
  126. "amount" => ["value" => $rechargeAmount . '元', "color" => "#173177"],
  127. "result" => ["value" => '充值成功', "color" => "#173177"],
  128. "remark" => ["value" => $remark, "color" => "#173177"]]];
  129. wxUtil::sendTaskInform($data, $merchant);
  130. }
  131. break;
  132. case $typeList['xhUserUnite']['id']:
  133. if ($addIntegral > 0) {
  134. $shortTempId = 'TM00230';
  135. $tempId = isset($allTM[$shortTempId]) ? $allTM[$shortTempId] : 0;
  136. if (!empty($tempId)) {
  137. $changeData = ["touser" => $openId, "template_id" => $tempId,
  138. "url" => Yii::$app->params['frontUrl'] . '/center/right?account=' . $merchant['id'],
  139. "data" => [
  140. "first" => ["value" => "支付宝绑定成功,获得积分:", "color" => "#173177"],
  141. "FieldName" => ["value" => "会员名称", "color" => "#0F0F0F"],
  142. "Account" => ["value" => $user['userName'], "color" => "#173177"],
  143. "change" => ["value" => "增加", "color" => "#0F0F0F"],
  144. "CreditChange" => ["value" => $addIntegral, "color" => "#173177"],
  145. "CreditTotal" => ["value" => $integral, "color" => "#173177"],
  146. "Remark" => ["value" => "点此查看明细!", "color" => "#173177"],],];
  147. wxUtil::sendTaskInform($changeData, $merchant);
  148. }
  149. }
  150. break;
  151. default:
  152. }
  153. //新增积分
  154. UserIntegralService::increaseToUpgrade($preIntegral, $integral, $preLevel, $userId, $merchantId, $merchantExtend);
  155. self::updateByUserId($userId, $upAssetData);
  156. }
  157. }