xhUserAssetService.php 7.1 KB

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