Money.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. namespace Biz\Money;
  3. /**
  4. * 资金操作相关类
  5. */
  6. class Money {
  7. /**
  8. * 资金明细表
  9. * @var string
  10. */
  11. private $money_table = 'fund_detailed';
  12. /**
  13. * 用户详情表
  14. * @var string
  15. */
  16. private $account_detailed = 'account_detailed';
  17. /**
  18. * 用户银行绑定记录表
  19. * @var string
  20. */
  21. private $bank_table = 'account_bank';
  22. /**
  23. * 用户表
  24. * @var string
  25. */
  26. private $account_table = 'account';
  27. /**
  28. * 交易密码表
  29. * @var string
  30. */
  31. private $pay_password = 'pay_password';
  32. /**
  33. * 资金明细表
  34. * @var string
  35. */
  36. private $money_details = 'money_details';
  37. /**
  38. * 资金记录修改
  39. * @param
  40. * string $account_identity 用户id
  41. * @param string $money 金额
  42. * @param 订单号 $order_id 订单号
  43. * @param int $type 交易类型
  44. * @return [type] [description]
  45. */
  46. public function insertMoney($account_identity, $money, $order_id, $type, $game_no = '',$UUID,$account=array())
  47. {
  48. //_beginTransaction();
  49. //获取用户信息
  50. if(empty($account)){
  51. return '-4020';
  52. }
  53. //查询余额
  54. $getAccount = $this->getAccount($account_identity);
  55. //验证流水金额与余额是否正常
  56. $checkWater = $this->checkWater($account_identity,$getAccount['available_cash']);
  57. if($checkWater != 1){
  58. return $checkWater;
  59. }
  60. //获取money_type
  61. $m_type = $this->getMtype($type);
  62. //判断加减金额
  63. if ($m_type == 1) {
  64. $nowMoney = $getAccount['available_cash'] + $money;
  65. $nowCash = $getAccount['cash'] + $money;
  66. } else {
  67. $nowMoney = $getAccount['available_cash'] - $money;
  68. $nowCash = $getAccount['cash'] - $money;
  69. }
  70. //更新用户余额
  71. $updateAccountMoney = $this->updateAccountMoney($account_identity,$nowMoney, $nowCash);
  72. dd($updateAccountMoney);
  73. if($updateAccountMoney != 1){
  74. return $updateAccountMoney;
  75. }else {
  76. //插入资金详情
  77. $insertDetail = $this->insertDetail($account_identity, $order_id, $money, $nowMoney, $type,$UUID,$account);
  78. if($insertDetail !=1){
  79. return $insertDetail;
  80. }
  81. }
  82. return '1';
  83. }
  84. /**
  85. * 更新用户金额
  86. * @param [type] $account_identity 用户唯一ID
  87. * @param [type] $nowMoney 操作后可提现金额
  88. * @param [type] $nowCash 操作后总金额
  89. */
  90. public function updateAccountMoney($account_identity, $nowMoney,$nowCash)
  91. {
  92. echo '</br>';
  93. var_dump($account_identity);
  94. echo '</br>';
  95. var_dump($nowMoney);
  96. echo '</br>';
  97. var_dump($nowCash);
  98. if(empty($account_identity)||empty($nowMoney)||empty($nowCash)){
  99. return '-2255';
  100. }
  101. //更新用户余额
  102. $res = lm($this->account_detailed, 'Commons')
  103. ->where('account_identity',$account_identity)
  104. ->update([
  105. 'cash'=> $nowCash,
  106. 'available_cash' => $nowMoney
  107. ]);
  108. if(!$res){
  109. return '-7024';
  110. }
  111. return '1';
  112. }
  113. /**
  114. * 插入资金详情
  115. * @param [type] $account_identity 用户唯一id
  116. * @param [type] $order_id 交易id
  117. * @param [type] $money 交易金额
  118. * @param [type] $nowMoney 剩余金额
  119. * @param [type] $type 交易类型的数字表示
  120. * @return [type] [description]
  121. */
  122. public function insertDetail($account_identity, $order_id, $money, $nowMoney, $type,$UUID,$account)
  123. {
  124. //获取用户信息
  125. if(empty($account)){
  126. return '-4020';
  127. }
  128. //获取money_type
  129. $m_type = $this->getMtype($type);
  130. $trade_desc = $this->getDesc($account['account'], $money, $order_id, $type);
  131. $data2 = array(
  132. 'info_identity' => $UUID,
  133. 'trade_id' => $order_id,
  134. 'account_name' => $account['account'],
  135. 'account_identity' => $account_identity,
  136. 'money' => $money,
  137. 'money_time' => date('Y-m-d H:i:s', time()),
  138. 'money_type' => $m_type,
  139. 'money_cash' => $nowMoney,
  140. //'status' => $status,
  141. 'trade_type' => $type,
  142. 'trade_desc' => $trade_desc,
  143. );
  144. $res3 = lm($this->money_details,'commons')->insert($data2);
  145. if ($res3) {
  146. return '1';
  147. }
  148. return '-7025';
  149. }
  150. /**
  151. * 获取用户余额信息
  152. * @param [type] $account_identity 用户唯一id
  153. * @return [type] 用户资金相关数组
  154. */
  155. public function getAccount($account_identity) {
  156. $res = lm($this->account_detailed, 'Commons')
  157. ->select('available_cash', 'cash', 'frozen_cash', 'account_identity')
  158. ->where('account_identity', $account_identity)
  159. ->first();
  160. if(!$res){
  161. Render('', '2115',lang('Errors','Api')->get('error-2115'));
  162. }
  163. $res->toarray();
  164. return $res;
  165. }
  166. /**
  167. * 验证账户金额是否足够
  168. * @param [type] $money 操作所需金额
  169. * @param [type] $available_cash 账户可用金额
  170. * @return [type] [description]
  171. */
  172. public function verifyMoney($money, $available_cash) {
  173. if ($available_cash > $money) {
  174. return true;
  175. } else {
  176. return false;
  177. }
  178. }
  179. /**
  180. * 检测是否设置交易密码
  181. * @param [type] $account_identity 用户id
  182. */
  183. public function checkPayPwd($account_identity='')
  184. {
  185. //$account_identity = isset($_SESSION['uinfo']['account_identity']) ? $_SESSION['uinfo']['account_identity'] : '';
  186. if (empty($account_identity)) {
  187. Render('', '-2001', lang()->get('user not login'));
  188. }
  189. //检查用户是否已经设置密码
  190. $judge = lm($this->pay_password, 'commons')->where(['account_identity' => $account_identity])->first();
  191. if ($judge) {
  192. return '1';
  193. } else {
  194. return '-1024';
  195. }
  196. }
  197. /**
  198. * 检测资金流水情况
  199. * @param $account_identity
  200. * @param $cash
  201. */
  202. public function checkWater($account_identity, $NowCash)
  203. {
  204. //查询最后一条资金余额
  205. $last_data = lm($this->money_details, 'commons')->where('account_identity', $account_identity)->orderBy('id', 'desc')->first();
  206. if (empty($last_data)) {
  207. return -500147;
  208. }
  209. $last_data = $last_data->toArray();
  210. if ($last_data['money_cash'] != $NowCash) {
  211. return -2255;
  212. }
  213. return 1;
  214. }
  215. /**
  216. * 验证交易密码
  217. * @param [type] $account_identity 用户唯一id
  218. * @param [type] $pwd 密码
  219. * @return [type] true or false
  220. */
  221. public function verifyPayPwd($account_identity,$pwd) {
  222. $enc = lm($this->pay_password,'commons')->select('encryption')->where('account_identity', $account_identity)->first()->toArray();
  223. $VerPwd = md5(md5($enc["encryption"] . $pwd));
  224. $res = lm($this->pay_password,'commons')->where(['account_identity' => $account_identity, 'pay_password' => $VerPwd])->first();
  225. if ($res) {
  226. return true;
  227. } else {
  228. return false;
  229. }
  230. }
  231. /**
  232. * 反水
  233. * @param string $account_identity 用户唯一ID
  234. * @param string $account 用户名
  235. * @param string $bet_money 投注总金额
  236. */
  237. public function fanshui($account_identity,$account,$bet_money,$order_id){
  238. //查询是否可以反水
  239. $account_info = lm('Account','Api')->where([['identity',$account_identity],['account',$account]])->first();
  240. if(!$account_info){
  241. Render('', '51030',lang('Errors','Api')->get('error-51030'));
  242. }
  243. //查询反水比例
  244. $Setinfo = lm('SetInfo','Api')->where([['infoname','fanshui'],['status',1]])->first();
  245. if(empty($Setinfo)){
  246. Render('', '51034',lang('Errors','Api')->get('error-51034'));
  247. }
  248. $account_info->toArray();
  249. $Setinfo->toArray();
  250. if($account_info['fanshui'] == 1){
  251. //执行插入金额详情与用户余额加反水
  252. // $order_id = OrderID(); //订单ID
  253. $UUID = UUID(); //信息ID
  254. $type = 7; //反水
  255. $bet_money = $bet_money * $Setinfo['infocontent']; //计算反水额
  256. $res = $this->insertMoney($account_identity, $bet_money, $order_id, $type, $game_no = '',$UUID,$account_info);
  257. if($res != 1){
  258. Render('', $res,lang('Errors','Api')->get('error'.$res));
  259. }
  260. }
  261. }
  262. /**
  263. * 获取描述信息
  264. * @param string $account_name 用户名
  265. * @param string $money 金额
  266. * @param string $order_id 订单号
  267. * @param int $type 操作类型
  268. * @return [type] [description]
  269. */
  270. public function getDesc($account_name, $money, $order_id, $type)
  271. {
  272. switch ($type) {
  273. case 1:
  274. return $account_name . '投注' . $money . '元。' . '订单号' . $order_id;
  275. break;
  276. case 2:
  277. return $account_name . '追号' . $money . '元。' . '订单号' . $order_id;
  278. break;
  279. case 3:
  280. return $account_name . '撤单' . '订单号' . $order_id;
  281. break;
  282. case 4:
  283. return $account_name . '中奖' . $money . '元。' . '订单号' . $order_id;
  284. break;
  285. case 5:
  286. return $account_name . '申请提现' . $money . '元。' . '订单号' . $order_id;
  287. break;
  288. case 6:
  289. return $account_name . '充值' . $money . '元。' . '订单号' . $order_id;
  290. break;
  291. case 7:
  292. return $account_name . '反水' . $money . '元。' . '订单号' . $order_id;
  293. break;
  294. case 8:
  295. return $account_name . '回水' . $money . '元。' . '订单号' . $order_id;
  296. break;
  297. case 9:
  298. return $account_name . '管理员扣款' . $money . '元。' . '订单号' . $order_id;
  299. break;
  300. case 10:
  301. return $account_name . '佣金提成' . $money . '元。' . '订单号' . $order_id;
  302. break;
  303. default:
  304. return '';
  305. break;
  306. }
  307. }
  308. /**
  309. * 返回money_type
  310. * @param [type] $type 类型
  311. * @return [type] [description]
  312. */
  313. public function getMtype($type)
  314. {
  315. $arr = array(
  316. '1' => 2,
  317. '2' => 2,
  318. '3' => 1,
  319. '4' => 1,
  320. '5' => 2,
  321. '6' => 1,
  322. '7' => 1,
  323. '8' => 1,
  324. '9' => 2,
  325. '10' => 1,
  326. '11' => 2,
  327. '15' => 2,
  328. );
  329. return $arr[$type];
  330. }
  331. }
  332. ?>