BalanceRechargeResultService.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /****************************************************************
  3. * Author: libaojia
  4. * Date: 2017/7/13 下午2:52
  5. * Email: livsyitian@163.com
  6. * QQ: 995265288
  7. * User: 芸众商城 www.yunzshop.com
  8. ****************************************************************/
  9. namespace app\frontend\modules\finance\services;
  10. use app\common\events\finance\BalanceRechargedEvent;
  11. use app\common\exceptions\ShopException;
  12. use app\common\models\finance\BalanceRechargeActivity;
  13. use app\common\services\credit\ConstService;
  14. use app\common\services\finance\BalanceChange;
  15. use \app\common\models\finance\BalanceRecharge;
  16. use EasyWeChat\Support\Log;
  17. use Illuminate\Support\Facades\DB;
  18. class BalanceRechargeResultService
  19. {
  20. /**
  21. * @var BalanceService
  22. */
  23. private $balanceSet;
  24. /**
  25. * @var BalanceRecharge
  26. */
  27. private $rechargeModel;
  28. private $array;
  29. private $enough;
  30. private $give;
  31. public function __construct(BalanceRecharge $balanceRecharge)
  32. {
  33. $this->balanceSet = new BalanceService();
  34. $this->rechargeModel = $balanceRecharge;
  35. }
  36. public function confirm()
  37. {
  38. DB::transaction(function () {
  39. $this->_confirm();
  40. });
  41. }
  42. private function _confirm()
  43. {
  44. $this->updateRechargeStatus();
  45. $this->updateMemberBalance();
  46. $is_award = $this->rechargeActivity();
  47. if ($is_award) {
  48. $this->rechargeEnoughGive();
  49. }
  50. event(new BalanceRechargedEvent($this->rechargeModel));
  51. }
  52. private function rechargeActivity()
  53. {
  54. //是否增加充值活动限制
  55. if ($this->balanceSet->rechargeActivityStatus()) {
  56. //是否在活动时间
  57. $start_time = $this->balanceSet->rechargeActivityStartTime();
  58. $end_time = $this->balanceSet->rechargeActivityEndTime();
  59. $recharge_time = $this->rechargeModel->created_at->timestamp;
  60. if (!($start_time <= $recharge_time) || !($end_time >= $recharge_time)) {
  61. \Log::debug("余额充值:订单号{$this->rechargeModel->ordersn}充值时间未在充值活动时间之内,取消赠送");
  62. return false;
  63. }
  64. //参与次数检测
  65. $rechargeActivity = BalanceRechargeActivity::where('member_id', $this->rechargeModel->member_id)
  66. ->where('activity_id', $this->balanceSet->rechargeActivityCount())
  67. ->first();
  68. $fetter = $this->balanceSet->rechargeActivityFetter();
  69. if ($fetter && $fetter >= 1 && $rechargeActivity && $rechargeActivity->partake_count >= $fetter) {
  70. \Log::debug("余额充值:订单号{$this->rechargeModel->ordersn}会员参与次数已达到上限");
  71. return false;
  72. }
  73. //更新会员参与活动次数
  74. if ($rechargeActivity) {
  75. $rechargeActivity->partake_count += 1;
  76. } else {
  77. $rechargeActivity = new BalanceRechargeActivity();
  78. $rechargeActivity->uniacid = $this->rechargeModel->uniacid;
  79. $rechargeActivity->member_id = $this->rechargeModel->member_id;
  80. $rechargeActivity->partake_count += 1;
  81. $rechargeActivity->activity_id = $this->balanceSet->rechargeActivityCount();
  82. }
  83. $rechargeActivity->save();
  84. return true;
  85. }
  86. return false;
  87. }
  88. /**
  89. * 余额充值支付回调
  90. *
  91. * @throws ShopException
  92. */
  93. public function rechargeEnoughGive()
  94. {
  95. $result = $this->_rechargeEnoughGive();
  96. if ($result !== true) {
  97. throw new ShopException("余额充值:订单号{$this->rechargeModel->ordersn}充值满奖失败");
  98. }
  99. }
  100. /**
  101. * 修改充值状态
  102. *
  103. * @throws ShopException
  104. */
  105. private function updateRechargeStatus()
  106. {
  107. $this->rechargeModel->status = ConstService::STATUS_SUCCESS;
  108. $result = $this->rechargeModel->save();
  109. if (!$result) {
  110. throw new ShopException("余额充值:订单号{$this->rechargeModel->ordersn}修改充值状态失败");
  111. }
  112. }
  113. /**
  114. * 修改会员余额
  115. *
  116. * @throws ShopException
  117. */
  118. private function updateMemberBalance()
  119. {
  120. $result = (new BalanceChange())->recharge($this->getBalanceChangeData());
  121. if (!$result) {
  122. throw new ShopException("余额充值:订单号{$this->rechargeModel->ordersn}修改会员余额失败");
  123. }
  124. }
  125. /**
  126. * 获取余额变动明细记录 data 数组
  127. * @return array
  128. */
  129. private function getBalanceChangeData()
  130. {
  131. return [
  132. 'member_id' => $this->rechargeModel->member_id,
  133. 'remark' => '会员充值' . $this->rechargeModel->money . '元,支付单号:' . $this->array['pay_sn'],
  134. 'source' => ConstService::SOURCE_RECHARGE,
  135. 'relation' => $this->rechargeModel->ordersn,
  136. 'operator' => ConstService::OPERATOR_MEMBER,
  137. 'operator_id' => $this->rechargeModel->member_id,
  138. 'change_value' => $this->rechargeModel->money,
  139. ];
  140. }
  141. /**
  142. * 余额充值奖励
  143. * @return bool|string
  144. */
  145. private function _rechargeEnoughGive()
  146. {
  147. if ($this->getGiveMoney()) {
  148. return (new BalanceChange())->award($this->getBalanceAwardData());
  149. }
  150. return true;
  151. }
  152. /**
  153. * 获取充值奖励金额
  154. * @return string
  155. */
  156. private function getGiveMoney()
  157. {
  158. $sale = $this->getRechargeSale();
  159. $money = $this->rechargeModel->money;
  160. rsort($sale);
  161. $result = '';
  162. foreach ($sale as $key) {
  163. if (empty($key['enough']) || empty($key['give'])) {
  164. continue;
  165. }
  166. if (bccomp($money, $key['enough'], 2) != -1) {
  167. if ($this->getProportionStatus()) {
  168. $result = bcdiv(bcmul($money, $key['give'], 2), 100, 2);
  169. } else {
  170. $result = bcmul($key['give'], 1, 2);
  171. }
  172. $this->enough = floatval($key['enough']);
  173. $this->give = $key['give'];
  174. break;
  175. }
  176. }
  177. return $result;
  178. }
  179. /**
  180. * 获取充值奖励营销设置数组
  181. * @return array
  182. */
  183. private function getRechargeSale()
  184. {
  185. $sale = $this->balanceSet->rechargeSale();
  186. $sale = array_values(array_sort($sale, function ($value) {
  187. return $value['enough'];
  188. }));
  189. return $sale;
  190. }
  191. /**
  192. * 获取余额充值奖励变动 data 数组
  193. * @return array
  194. */
  195. private function getBalanceAwardData()
  196. {
  197. return [
  198. 'member_id' => $this->rechargeModel->member_id,
  199. 'remark' => $this->getBalanceAwardRemark(),
  200. 'source' => ConstService::SOURCE_AWARD,
  201. 'relation' => $this->array['order_sn'],
  202. 'operator' => ConstService::OPERATOR_MEMBER,
  203. 'operator_id' => $this->rechargeModel->member_id,
  204. 'change_value' => $this->getGiveMoney(),
  205. ];
  206. }
  207. /**
  208. * 获取余额奖励日志
  209. * @return string
  210. */
  211. private function getBalanceAwardRemark()
  212. {
  213. if ($this->getProportionStatus()) {
  214. return '充值满' . $this->enough . '元赠送' . $this->give . '%,(充值金额:' . $this->rechargeModel->money . '元)';
  215. }
  216. return '充值满' . $this->enough . '元赠送' . $this->give . '元,(充值金额:' . $this->rechargeModel->money . '元)';
  217. }
  218. /**
  219. * 获取余额奖励设置,比例 或 固定金额
  220. * @return string
  221. */
  222. private function getProportionStatus()
  223. {
  224. return $this->balanceSet->proportionStatus();
  225. }
  226. }