xhUserAssetService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace common\services;
  3. use bizHd\user\services\UserIntegralService;
  4. use common\models\xhActive;
  5. use Yii;
  6. use common\components\dict;
  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. $sjId = $merchant['id'];
  38. $orderId = isset($order['id']) ? $order['id'] : '';
  39. $orderSn = isset($order['orderSn']) ? $order['orderSn'] : '';
  40. $openId = $user['openId'];
  41. $miniOpenId = isset($user['miniOpenId']) ? $user['miniOpenId'] : '';
  42. $growth = isset($upAssetData['growth']) ? $upAssetData['growth'] : 0;
  43. $preLevel = $userAsset['member'];
  44. $preGrowth = $userAsset['growth'];
  45. $addGrowth = stringUtil::calcSub($growth, $preGrowth);
  46. $allTM = xhTMessageService::getList($sjId);
  47. //没有成长值,后续升级流程不需要再走
  48. if (empty($growth)) {
  49. self::updateByUserId($userId, $upAssetData);
  50. return;
  51. }
  52. switch ($capitalType) {//根据流水类型定制通知内容
  53. case dict::getDict('capitalType', 'xhOrder', 'id'):
  54. //购买商品
  55. $goodsNum = $order['goodsNum'];
  56. $totalFee = $order['actPrice'];
  57. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '门店商品';
  58. $sourceType = $order['sourceType'];
  59. if ($sourceType == 0) {
  60. $shortTempId = 'OPENTM202297555';//下单成功通知
  61. $tempId = isset($allTM[$shortTempId]) ? $allTM[$shortTempId] : 0;
  62. if (!empty($tempId)) {
  63. $data = [
  64. "touser" => $openId,
  65. "template_id" => $tempId,
  66. "url" => Yii::$app->params['mallDomain'] . "/#/pages/order/detail?account=" . $merchant['id'] . "&shopId=" . $order['shopId'] . "&id=" . $orderId,
  67. "data" => [
  68. "first" => ["value" => '亲,您已成功下单!', "color" => "#173177"],
  69. "keyword1" => ["value" => (string)$orderSn, "color" => "#173177"],
  70. "keyword2" => ["value" => $name, "color" => "#173177"],
  71. "keyword3" => ["value" => $goodsNum . '件', "color" => "#173177"],
  72. "keyword4" => ["value" => number_format($totalFee, 2) . '元', "color" => "#173177"],
  73. "keyword5" => ["value" => '微信支付', "color" => "#173177"],
  74. "remark" => ["value" => "", "color" => "#173177"]
  75. ]
  76. ];
  77. if (!empty($miniOpenId)) {
  78. $data['miniprogram']['appid'] = $merchantExtend['miniAppId'];
  79. $data['miniprogram']['pagepath'] = 'pages/order/detail?id=' . $orderId;
  80. }
  81. wxUtil::sendTaskInform($data, $merchant);
  82. }
  83. } else {
  84. if ($addGrowth > 0) {
  85. $shortTempId = 'TM00230';
  86. $tempId = isset($allTM[$shortTempId]) ? $allTM[$shortTempId] : 0;
  87. if (!empty($tempId)) {
  88. $changeData = [
  89. "touser" => $openId,
  90. "template_id" => $tempId,
  91. "url" => Yii::$app->params['mallDomain'] . "/center/order-detail?id=$orderId&account=" . $merchant['id'],
  92. "data" => [
  93. "first" => ["value" => "您在本店购物获得成长值", "color" => "#173177"],
  94. "FieldName" => ["value" => "会员名称", "color" => "#0F0F0F"],
  95. "Account" => ["value" => $user['userName'], "color" => "#173177"],
  96. "change" => ["value" => "增加", "color" => "#0F0F0F"],
  97. "CreditChange" => ["value" => $addGrowth, "color" => "#173177"],
  98. "CreditTotal" => ["value" => $growth, "color" => "#173177"],
  99. "Remark" => ["value" => "点此查看订单明细、添加编辑收花人信息", "color" => "#173177"],
  100. ],
  101. ];
  102. wxUtil::sendTaskInform($changeData, $merchant);
  103. }
  104. }
  105. }
  106. break;
  107. case dict::getDict('capitalType', 'xhActiveOrder', 'id'):
  108. $activeId = $order['activeId'];
  109. $active = xhActiveService::getById($activeId);
  110. $newApplyNum = $active['applyNum'] + 1;//活动报名人数+1
  111. xhActiveService::updateById($activeId, ['applyNum' => $newApplyNum]);
  112. $shortTempId = 'TM00575';//报名成功通知
  113. $tempId = isset($allTM[$shortTempId]) ? $allTM[$shortTempId] : 0;
  114. if (!empty($tempId)) {
  115. $changeData = [
  116. "touser" => $openId,
  117. "template_id" => $tempId,
  118. "url" => Yii::$app->params['mallDomain'] . '/active/order?id=' . $orderId . '&account=' . $merchant['id'],
  119. "data" => [
  120. "first" => ["value" => "您好,您已报名成功", "color" => "#173177"],
  121. "keynote1" => ["value" => $active['title'], "color" => "#173177"],
  122. "keynote2" => ["value" => date("m-d H:i", $active['startTime']), "color" => "#173177"],
  123. "keynote3" => ["value" => $active['address'], "color" => "#173177"],
  124. "remark" => ["value" => "欢迎您的参加哦!", "color" => "#173177"],
  125. ],
  126. ];
  127. wxUtil::sendTaskInform($changeData, $merchant);
  128. }
  129. break;
  130. case dict::getDict('capitalType', 'xhRecharge', 'id'):
  131. $totalBalance = isset($upAssetData['balance']) ? $upAssetData['balance'] : 0;
  132. $rechargeAmount = $order['amount'];
  133. $url = Yii::$app->params['mallDomain'] . "/#/pages/home/user?account=" . $merchant['id'];
  134. $remark = '您当前帐户余额:' . $totalBalance . '元';
  135. $shortTempId = 'TM00006';
  136. $tempId = isset($allTM[$shortTempId]) ? $allTM[$shortTempId] : 0;
  137. if (!empty($tempId)) {
  138. $data = [
  139. "touser" => $openId,
  140. "template_id" => $tempId,
  141. "url" => $url,
  142. "data" => [
  143. "first" => ["value" => $remark, "color" => "#173177"],
  144. "accountType" => ["value" => '会员类型', "color" => "#000000"],
  145. "account" => ["value" => '点击查看', "color" => "#173177"],
  146. "amount" => ["value" => $rechargeAmount . '元', "color" => "#173177"],
  147. "result" => ["value" => '充值成功', "color" => "#173177"],
  148. "remark" => ["value" => '', "color" => "#173177"]]
  149. ];
  150. if (!empty($miniOpenId)) {
  151. $data['miniprogram']['appid'] = $merchantExtend['miniAppId'];
  152. $data['miniprogram']['pagepath'] = 'pages/home/user';
  153. }
  154. wxUtil::sendTaskInform($data, $merchant);
  155. }
  156. break;
  157. case dict::getDict('capitalType', 'xhUserUnite', 'id'):
  158. $addIntegral = 0;
  159. if ($addIntegral > 0) {
  160. $shortTempId = 'TM00230';
  161. $tempId = isset($allTM[$shortTempId]) ? $allTM[$shortTempId] : 0;
  162. if (!empty($tempId)) {
  163. $changeData = [
  164. "touser" => $openId,
  165. "template_id" => $tempId,
  166. "url" => Yii::$app->params['mallDomain'] . '/center/right?account=' . $merchant['id'],
  167. "data" => [
  168. "first" => ["value" => "支付宝绑定成功,获得积分:", "color" => "#173177"],
  169. "FieldName" => ["value" => "会员名称", "color" => "#0F0F0F"],
  170. "Account" => ["value" => $user['userName'], "color" => "#173177"],
  171. "change" => ["value" => "增加", "color" => "#0F0F0F"],
  172. "CreditChange" => ["value" => $addIntegral, "color" => "#173177"],
  173. "CreditTotal" => ["value" => $growth, "color" => "#173177"],
  174. "Remark" => ["value" => "点此查看明细!", "color" => "#173177"],
  175. ],
  176. ];
  177. wxUtil::sendTaskInform($changeData, $merchant);
  178. }
  179. }
  180. break;
  181. default:
  182. }
  183. //新增积分
  184. UserIntegralService::increaseToUpgrade($preGrowth, $growth, $preLevel, $userId, $sjId, $merchantExtend);
  185. self::updateByUserId($userId, $upAssetData);
  186. }
  187. }