BalanceRechargeService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2022/2/15
  8. * Time: 9:49
  9. */
  10. namespace app\backend\modules\balance\services;
  11. use app\backend\modules\finance\models\BalanceRechargeRecords;
  12. use app\backend\modules\member\models\Member;
  13. use app\common\facades\Setting;
  14. use app\common\models\finance\BalanceRechargeCheck;
  15. use app\common\services\finance\BalanceChange;
  16. use Illuminate\Support\Facades\DB;
  17. class BalanceRechargeService
  18. {
  19. private $balanceSet;
  20. /**
  21. * @var Member
  22. */
  23. private $member;
  24. private $data;
  25. /**
  26. * @var BalanceRechargeCheck
  27. */
  28. private $rechargeCheckLog;
  29. /**
  30. * @var BalanceRechargeRecords
  31. */
  32. private $rechargeModel;
  33. public function getBalanceSet()
  34. {
  35. if (!isset($this->balanceSet)) {
  36. $this->balanceSet = Setting::get('finance.balance');
  37. }
  38. return $this->balanceSet;
  39. }
  40. public function setMember($member_id)
  41. {
  42. if (!isset($member) || $member_id != $member->uid) {
  43. $this->member = Member::find($member_id);
  44. if (!$this->member) {
  45. throw new \Exception('会员信息未找到');
  46. }
  47. }
  48. }
  49. /**
  50. * @return Member
  51. * @throws \Exception
  52. */
  53. public function getMember()
  54. {
  55. if (!isset($this->member)) {
  56. throw new \Exception('会员模型未设置');
  57. }
  58. return $this->member;
  59. }
  60. /**
  61. * 充值余额审核是否开启
  62. * @return bool
  63. */
  64. public function chargeCheckOpen()
  65. {
  66. return $this->getBalanceSet()['charge_check_swich'] ? true : false;
  67. }
  68. /**
  69. * @param array $rechargeData
  70. * @return bool
  71. * @throws \Exception
  72. */
  73. public function rechargeStart($rechargeData = [])
  74. {
  75. if (!$rechargeData) {
  76. throw new \Exception('参数错误');
  77. }
  78. $this->data = $rechargeData;
  79. $this->fillChargeCheckLog();
  80. if (in_array($this->data['type'],[BalanceRechargeRecords::PAY_TYPE_SHOP])) {
  81. //需要审核,进行保存
  82. if (!$this->rechargeCheckLog->explain) {
  83. throw new \Exception('请填写充值说明');
  84. }
  85. if (mb_strlen($this->rechargeCheckLog->recharge_remark) > 50) {
  86. throw new \Exception('备注不能超过50个字');
  87. }
  88. if (!$this->rechargeCheckLog->save()) {
  89. throw new \Exception('审核数据保存失败');
  90. }
  91. }
  92. // $this->recharge();
  93. return true;
  94. }
  95. /**
  96. * 添加审核数据(还未保存)
  97. */
  98. private function fillChargeCheckLog()
  99. {
  100. $fill = [
  101. 'uniacid' => \YunShop::app()->uniacid,
  102. 'member_id' => $this->data['member_id'], //充值会员ID
  103. 'money' => $this->data['money'], //充值金额
  104. 'type' => $this->data['type'], //充值类型
  105. 'operator_id' => $this->data['operator_id'], //操作者ID
  106. 'operator' => $this->data['operator'], //操作者
  107. 'source' => $this->data['source'], //充值来源
  108. 'remark' => $this->data['remark'] ? : '', //备注
  109. 'explain' => $this->data['explain'] ? : '', //充值说明
  110. 'enclosure' => $this->data['enclosure'] ? : '', //附件
  111. 'recharge_remark' => $this->data['recharge_remark'] ? : '', //充值填写的备注
  112. ];
  113. $this->rechargeCheckLog = new BalanceRechargeCheck();
  114. $this->rechargeCheckLog->fill($fill);
  115. }
  116. /**
  117. * 审核
  118. * @param $id
  119. * @param $status
  120. * @return bool
  121. * @throws \Exception
  122. */
  123. public function verifyChargeLog($id,$status)
  124. {
  125. if (!$id || !in_array($status,[1,2])) {
  126. throw new \Exception('参数错误');
  127. }
  128. $this->rechargeCheckLog = BalanceRechargeCheck::uniacid()->where('id',$id)->first();
  129. if (!$this->rechargeCheckLog) {
  130. throw new \Exception('未找到该审核数据');
  131. }
  132. if ($this->rechargeCheckLog->status != 0) {
  133. throw new \Exception('该记录状态无法审核');
  134. }
  135. $this->rechargeCheckLog->status = $status;
  136. DB::transaction(function () use ($status) {
  137. if (!$this->rechargeCheckLog->save()) {
  138. throw new \Exception('审核失败');
  139. }
  140. if ($this->rechargeCheckLog->status == 1) {//审核通过,进行充值
  141. $this->setMember($this->rechargeCheckLog->member_id);//设置会员模型
  142. $this->recharge();
  143. }
  144. });
  145. return true;
  146. }
  147. /**
  148. * @return \Illuminate\Support\MessageBag|string
  149. * @throws \Exception
  150. */
  151. private function recharge()
  152. {
  153. $this->rechargeModel = new BalanceRechargeRecords();
  154. $this->rechargeModel->fill($this->getRechargeData());
  155. $validator = $this->rechargeModel->validator();
  156. if ($validator->fails()) {
  157. throw new \Exception($validator->messages());
  158. }
  159. if ($this->rechargeModel->save()) {
  160. $data = $this->getChangeBalanceData();
  161. if ($this->rechargeModel->money > 0) {
  162. $data['change_value'] = $this->rechargeModel->money;
  163. $result = (new BalanceChange())->recharge($data);
  164. } else {
  165. $data['change_value'] = -$this->rechargeModel->money;
  166. $result = (new BalanceChange())->rechargeMinus($data);
  167. }
  168. if ($result !== true) {
  169. throw new \Exception($result);
  170. }
  171. }
  172. return true;
  173. }
  174. private function getChangeBalanceData()
  175. {
  176. return array(
  177. 'member_id' => $this->rechargeCheckLog->member_id,
  178. 'remark' => $this->rechargeCheckLog->remark,
  179. 'source' => $this->rechargeCheckLog->source,
  180. 'relation' => $this->rechargeModel->ordersn,
  181. 'operator' => $this->rechargeCheckLog->operator,
  182. 'operator_id' => $this->rechargeCheckLog->operator_id
  183. );
  184. }
  185. /**
  186. * @return array
  187. * @throws \Exception
  188. */
  189. private function getRechargeData()
  190. {
  191. return array(
  192. 'uniacid' => $this->rechargeCheckLog->uniacid,
  193. 'member_id' => $this->rechargeCheckLog->member_id,
  194. 'old_money' => $this->getMember()->credit2,
  195. 'money' => $this->rechargeCheckLog->money,
  196. 'new_money' => $this->getNewMoney(),
  197. 'type' => $this->rechargeCheckLog->type,
  198. 'ordersn' => BalanceRechargeRecords::createOrderSn('RV','ordersn'),
  199. 'status' => BalanceRechargeRecords::PAY_STATUS_SUCCESS,
  200. 'remark' => $this->rechargeCheckLog->recharge_remark
  201. );
  202. }
  203. /**
  204. * @return int|string
  205. * @throws \Exception
  206. */
  207. private function getNewMoney()
  208. {
  209. $new_value = bcadd($this->getMember()->credit2, $this->rechargeCheckLog->money, 2);
  210. return $new_value > 0 ? $new_value : 0;
  211. }
  212. }