xhUserAssetService.php 6.8 KB

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