TranferMoneyLogic.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/4/18
  6. * Time: 15:16
  7. */
  8. namespace Biz\Game;
  9. use App\Commons\Model\Money_transfer;
  10. use App\Commons\Model\Wagent_log as Wagent_logModel ;
  11. use App\Commons\Model\Money_details as Money_detailsModel ;
  12. //转账逻辑详细实现
  13. class TranferMoneyLogic
  14. {
  15. private $accountModel = null ;
  16. private $wagentModel = null ;
  17. private $transferModel = null ;
  18. private $accountDetailModel = null ;
  19. private $paras = null ;
  20. private $type = null ;
  21. private $money = 0 ;
  22. public function __construct( $accountModel,$parasArray,$wagetnModel )
  23. {
  24. $this->accountModel = $accountModel ;
  25. $this->wagentModel = $wagetnModel ;
  26. $this->paras = $parasArray ;
  27. }
  28. public function dorun(){
  29. $ret0 = $this->parentCheck() ;
  30. if ( $ret0 !== true){
  31. return $ret0 ;
  32. }
  33. try {
  34. _beginTransaction();
  35. $ret1 = $this->wagentLogRecord();
  36. if (!$ret1){ _rollBack(); return $this->makeret(0,'false_1',0); }
  37. $ret2 = $this->wagentRecord();
  38. if (!$ret2){ _rollBack(); return $this->makeret(0,'false_2',0); }
  39. $ret3 = $this->accountDetailUpdate();
  40. if (!$ret3){ _rollBack(); return $this->makeret(0,'false_3',0); }
  41. $ret4 = $this->moneyTranferRecord();
  42. if (!$ret4){ _rollBack(); return $this->makeret(0,'false_4',0); }
  43. $ret5 = $this->moneyDetailInsrt();
  44. if (!$ret5){ _rollBack(); return $this->makeret(0,'false_5',0); }
  45. _commit();
  46. return $this->makeret(1,'success',1);
  47. }catch (\Exception $e){
  48. _rollBack();
  49. return $this->makeret(0,'false_0'.$e->getMessage(), 0 ) ;
  50. }
  51. }
  52. //返回数据
  53. private function makeret($status=1,$msg='success',$datas=[]){
  54. return [
  55. 'status'=>$status,
  56. 'msg'=>$msg,
  57. 'datas'=>$datas
  58. ];
  59. }
  60. //参数检测
  61. private function parentCheck(){
  62. //IN代表代理账户转入到用户账号 OUT代表用户账号转入到代理账号上
  63. $type = isset($this->paras['type']) ? strtoupper(trim($this->paras['type'])) : '';
  64. if (!in_array( $type , ['IN','OUT'])){ return $this->makeret(0,'type error!','type error'); }
  65. $this->type = $type ;
  66. //转账金额不能小于等于0
  67. $credit = isset($this->paras['credit']) ? floatval(trim($this->paras['credit'])) : 0;
  68. if ( $credit <=0 ){ return $this->makeret(0,'credit error','credit error'); }
  69. $this->money = $credit ;
  70. //代理上传的订单号不能为空
  71. $billno = isset($this->paras['billno']) ? trim($this->paras['billno']) : '';
  72. if (empty($billno)){ return $this->makeret(0,'billno empty','billno empty'); }
  73. //代理上传的订单号不能重复
  74. $transferModel = new Money_transfer();
  75. $hasexit = $transferModel->findOneByAttr('billno',$billno);
  76. if ( $hasexit ){ return $this->makeret(0,'billno exists','billno exists'); }
  77. $this->transferModel = $transferModel ;
  78. //用户详情数据,内有用户余额信息
  79. $parentinfo = lm('account_detailed', 'commons')->where('account_identity', $this->accountModel->identity)->first();
  80. if (empty($parentinfo)) { return $this->makeret(1,'account_detailed not exists','0'); }
  81. $cash = $parentinfo->cash ;
  82. if ( $type == 'OUT' && $cash<$credit ){
  83. return $this->makeret(0,'no_enough_credit_user','no_enough_credit');
  84. }
  85. if ( $type == 'IN' && $this->wagentModel->money <$credit ){
  86. return $this->makeret(0,'no_enough_credit_agent','no_enough_credit');
  87. }
  88. $this->accountDetailModel = $parentinfo ;
  89. return true;
  90. }
  91. //代理钱变化日志记录 wageng_log增加
  92. private function wagentLogRecord(){
  93. $beforeMoney = intval(trim($this->wagentModel->money)*100);
  94. $afterMoney = ( $this->type == 'IN') ? (($beforeMoney - $this->money*100)/100) : (($beforeMoney + $this->money*100)/100) ;
  95. $model = new Wagent_logModel();
  96. $model->agent_name = $this->wagentModel->agent_name ;
  97. $model->account_identity = $this->accountModel->identity;
  98. $model->ordernumber = $this->paras['billno'];
  99. $model->credit = $this->money;
  100. $model->agent_money_before = $beforeMoney/100;
  101. $model->agent_money_after = sprintf("%.2f",$afterMoney) ;
  102. $ret = $model->save();
  103. return $ret;
  104. }
  105. //代理钱更新 wagent表更行
  106. private function wagentRecord(){
  107. $beforeMoney = intval(trim($this->wagentModel->money)*100);
  108. $afterMoney = ( $this->type == 'IN') ? (($beforeMoney - $this->money*100)/100) : (($beforeMoney + $this->money*100)/100) ;
  109. $this->wagentModel->money = sprintf("%.2f",$afterMoney) ;
  110. $ret = $this->wagentModel->save();
  111. return $ret;
  112. }
  113. //用户余额更新
  114. private function accountDetailUpdate(){
  115. $beforeMoney = intval(trim($this->accountDetailModel->cash)*100 ) ;
  116. $afterMoney = ( $this->type == 'IN') ? (($beforeMoney + $this->money*100)/100) : (($beforeMoney - $this->money*100)/100) ;
  117. $this->accountDetailModel->cash = sprintf("%.2f",$afterMoney) ;
  118. $beforeMoney = intval(trim($this->accountDetailModel->available_cash)*100 ) ;
  119. $afterMoney = ( $this->type == 'IN') ? (($beforeMoney + $this->money*100)/100) : (($beforeMoney - $this->money*100)/100) ;
  120. $this->accountDetailModel->available_cash = sprintf("%.2f",$afterMoney) ;
  121. $ret = $this->accountDetailModel->save();
  122. return $ret ;
  123. }
  124. //代理转账记录 money_tranfer表增加
  125. private function moneyTranferRecord(){
  126. $this->transferModel->account_indent = $this->accountModel->identity;
  127. $this->transferModel->ordernumber = date("YmdHis").uniqid();
  128. $this->transferModel->billno = $this->paras['billno'];
  129. $this->transferModel->tradeamount = sprintf("%.2f", $this->money ) ;
  130. $this->transferModel->blance = $this->accountDetailModel->cash ;
  131. $this->transferModel->type = $this->type ;
  132. $this->transferModel->tradtype = ($this->type == 'IN') ? 8 : 9 ;
  133. $this->transferModel->tfrom = 0 ;
  134. $this->transferModel->tto = 0 ;
  135. $this->transferModel->addtime = date("Y-m-d H:i:s") ;
  136. $this->transferModel->agent_name = $this->wagentModel->agent_name ;
  137. $ret = $this->transferModel->save();
  138. return $ret ;
  139. }
  140. //用户流水记录
  141. private function moneyDetailInsrt(){
  142. $model = new Money_detailsModel();
  143. $model->info_identity = UUID();
  144. $model->trade_id = $this->paras['billno'] ;
  145. $model->account_name = $this->accountModel->account ;
  146. $model->account_identity = $this->accountModel->identity ;
  147. $model->money = sprintf("%.2f", $this->money ) ; ;
  148. $model->money_time = date("Y-m-d H:i:s") ;
  149. $model->money_type = ($this->type == 'IN') ? 1 : 2 ;
  150. $model->money_cash = $this->accountDetailModel->cash ;
  151. $model->trade_type = ($this->type == 'IN') ? 23 : 24 ;
  152. $model->trade_desc = ($this->type == 'IN') ? '从代理转入': '转出到代理';
  153. $model->reason = '代理接口操作';
  154. $model->sysetem_user = 1 ;
  155. $model->status = 1 ;
  156. $model->out_order_id = $this->transferModel->ordernumber ;
  157. $model->remark = '代理转入转出记录';
  158. $ret = $model->save();
  159. return $ret ;
  160. }
  161. }