Finance.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\service\users;
  10. use app\admin\service\Service;
  11. use app\common\models\users\Users as UsersModel;
  12. use app\admin\model\users\UsersLog as UsersLogModel;
  13. use app\admin\model\users\UsersWithdrawLog as UsersWithdrawLogModel;
  14. class Finance extends Service {
  15. private static $type = ["1"=>"银行卡","2"=>"支付宝","3"=>"微信"];
  16. private static $status = ["0"=>"审核中","1"=>"已提现","2"=>"未通过"];
  17. /**
  18. * 获取列表数据
  19. * @param $data
  20. * @param array $condition
  21. * @return array
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\DbException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. */
  26. public static function getList($data,$condition=[]){
  27. $count = UsersLogModel::withJoin("users")->where($condition)->count();
  28. $result = UsersLogModel::withJoin("users")->where($condition)->page($data["page"]??1,$data["limit"]??10)->order("users_log.id","desc")->select()->toArray();
  29. return [ "count"=>$count, "data"=> array_map(function ($res){
  30. $res["username"] = getUserName($res);
  31. return $res;
  32. },$result) ];
  33. }
  34. /**
  35. * 提现申请
  36. * @param $data
  37. * @return array
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public static function apply($data){
  43. $key = $data["key"]??[];
  44. $condition = [];
  45. if(isset($key["status"])){
  46. if(in_array($key["status"],[1,2,3])){
  47. $condition[] = ["users_withdraw_log.status","=",$key["status"]-1];
  48. }
  49. }
  50. $count = UsersWithdrawLogModel::withJoin("users")->where($condition)->count();
  51. $result = UsersWithdrawLogModel::withJoin("users")->where($condition)->page($data["page"]??1,$data["limit"]??10)->order("id","desc")->select()->toArray();
  52. return [ "count"=>$count, "data"=>$data = array_map(function ($res){
  53. $res["username"] = getUserName($res);
  54. $res["type_name"] = self::getType($res["type"]);
  55. $res["status_name"] = self::getStatus($res["status"]);
  56. $str = '';
  57. if($res["type"] == 1){
  58. $str .= "<p>类型:" . ($res["withdraw_type"]==0?"佣金":"余额") . '</p>';
  59. $str .= "<p>卡号:" . $res["code"] . '</p>';
  60. $str .= "<p>开户地址:" . $res["address"] . '</p>';
  61. $str .= "<p>银行:" . $res["bank_name"] . '</p>';
  62. }else if($res["type"] == 2){
  63. $str .= "<p>用户名:" . $res["username"] . '</p>';
  64. $str .= "<p>支付宝:" . $res["account"] . '</p>';
  65. }else if($res["type"] == 3){
  66. $str .= "<p>用户名:" . $res["username"] . '</p>';
  67. $str .= "<p>微信:" . $res["account"] . '</p>';
  68. }
  69. $res["string"] = $str;
  70. return $res;
  71. },$result) ];
  72. }
  73. public static function detail($id){
  74. $row = UsersWithdrawLogModel::where(["id"=>$id])->find();
  75. if(empty($row)){
  76. throw new \Exception("您要访问的内容不存在",0);
  77. }
  78. $str = '&nbsp;&nbsp;';
  79. if($row["type"] == 1){
  80. $str .= "<span>卡号:" . $row["code"] . '</span>&nbsp;&nbsp;';
  81. $str .= "<span>开户地址:" . $row["address"] . '</span>&nbsp;&nbsp;';
  82. $str .= "<span>银行:" . $row["bank_name"] . '</span>&nbsp;&nbsp;';
  83. }else if($row["type"] == 2){
  84. $str .= "<span>用户名:" . $row["username"] . '</span>&nbsp;&nbsp;';
  85. $str .= "<span>支付宝:" . $row["account"] . '</span>&nbsp;&nbsp;';
  86. }else if($row["type"] == 3){
  87. $str .= "<span>用户名:" . $row["username"] . '</span>&nbsp;&nbsp;';
  88. $str .= "<span>微信:" . $row["account"] . '</span>&nbsp;&nbsp;';
  89. }
  90. $row["string"] = $str;
  91. $user = UsersModel::where(["id"=>$row["user_id"]])->find();
  92. $user["username"] = getUserName($user);
  93. return [ "id"=>$id,"user"=>$user,"row"=>$row ];
  94. }
  95. /**
  96. * 保存数据
  97. * @param $data
  98. * @return bool
  99. * @throws \think\db\exception\DataNotFoundException
  100. * @throws \think\db\exception\DbException
  101. * @throws \think\db\exception\ModelNotFoundException
  102. */
  103. public static function save($data){
  104. $row = UsersWithdrawLogModel::where(["id"=>$data["id"]])->find();
  105. if(empty($row)){
  106. throw new \Exception("您要访问的内容不存在",0);
  107. }
  108. $u = UsersModel::where(["id"=>$row["user_id"]])->find();
  109. if($row["withdraw_type"] == 1){
  110. if($u["amount"] < $row["price"]){
  111. throw new \Exception("操作失败,余额不足!",0);
  112. }
  113. }else{
  114. if($u["spread_amount"] < $row["price"]){
  115. throw new \Exception("操作失败,余额不足!",0);
  116. }
  117. }
  118. try{
  119. UsersWithdrawLogModel::startTrans();
  120. UsersWithdrawLogModel::where(["id"=>$data["id"]])->save([ "msg"=>$data["msg"], "status"=>$data["status"], "update_time"=>time() ]);
  121. if($data["status"] == 1) {
  122. if ($row["withdraw_type"] == 1) {
  123. UsersModel::where(["id" => $row["user_id"]])->dec("amount", $row["price"])->update();
  124. } else {
  125. UsersModel::where(["id" => $row["user_id"]])->dec("spread_amount", $row["price"])->update();
  126. }
  127. }
  128. UsersWithdrawLogModel::commit();
  129. return true;
  130. }catch (\Exception $ex){
  131. UsersWithdrawLogModel::rollback();
  132. throw new \Exception($ex->getMessage(),$ex->getCode());
  133. }
  134. }
  135. /**
  136. * 获取支付方式
  137. * @param $type
  138. * @return string
  139. */
  140. public static function getType($type){
  141. return self::$type[$type];
  142. }
  143. /**
  144. * 获取状态
  145. * @param $status
  146. * @return string
  147. */
  148. public static function getStatus($status){
  149. return self::$status[$status];
  150. }
  151. }