BalanceLogService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\AccountLogModel;
  13. use App\Models\ActionLogModel;
  14. use App\Models\AgentModel;
  15. use App\Models\BalanceLogModel;
  16. use App\Models\MemberModel;
  17. use App\Services\BaseService;
  18. use App\Services\ConfigService;
  19. use App\Services\RedisService;
  20. use Illuminate\Support\Facades\DB;
  21. /**
  22. * 余额管理-服务类
  23. * @author laravel开发员
  24. * @since 2020/11/11
  25. */
  26. class BalanceLogService extends BaseService
  27. {
  28. public static $instance = null;
  29. /**
  30. * 构造函数
  31. * @author laravel开发员
  32. * @since 2020/11/11
  33. * AccountService constructor.
  34. */
  35. public function __construct()
  36. {
  37. $this->model = new BalanceLogModel();
  38. }
  39. /**
  40. * 静态入口
  41. * @return static|null
  42. */
  43. public static function make()
  44. {
  45. if (!self::$instance) {
  46. self::$instance = (new static());
  47. }
  48. return self::$instance;
  49. }
  50. /**
  51. * @param $params
  52. * @param int $pageSize
  53. * @return array
  54. */
  55. public function getDataList($params, $pageSize = 15)
  56. {
  57. $query = $this->getQuery($params);
  58. $list = $query->select(['a.*'])
  59. ->orderBy('a.status','asc')
  60. ->orderBy('a.create_time','desc')
  61. ->orderBy('a.id','desc')
  62. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  63. $list = $list? $list->toArray() :[];
  64. if($list){
  65. foreach($list['data'] as &$item){
  66. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  67. $item['time_text'] = $item['create_time']? datetime($item['create_time'],'Y年m月d日') : '';
  68. }
  69. }
  70. return [
  71. 'pageSize'=> $pageSize,
  72. 'total'=>isset($list['total'])? $list['total'] : 0,
  73. 'list'=> isset($list['data'])? $list['data'] : []
  74. ];
  75. }
  76. public function getQuery($params)
  77. {
  78. $where = ['a.mark' => 1];
  79. $status = isset($params['status'])? $params['status'] : 0;
  80. $type = isset($params['type'])? $params['type'] : 0;
  81. if($status>0){
  82. $where['a.status'] = $status;
  83. }
  84. if($type>0){
  85. $where['a.type'] = $type;
  86. }
  87. return $this->model->with(['member'])->from("balance_logs as a")
  88. ->leftJoin('member as b','b.id','=','a.user_id')
  89. ->where($where)
  90. ->where(function ($query) use($params) {
  91. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  92. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  93. if($userId){
  94. $query->where('a.user_id',$userId);
  95. }
  96. if ($keyword) {
  97. $query->where(function($query) use($keyword){
  98. $query->where('b.nickname','like',"%{$keyword}%")
  99. ->orWhere('b.mobile','like',"%{$keyword}%")
  100. ->orWhere('b.realname','like',"%{$keyword}%");
  101. });
  102. }
  103. $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
  104. if($orderNo){
  105. $query->where(function($query) use($orderNo){
  106. $query->where('a.order_no','like',"%{$orderNo}%");
  107. });
  108. }
  109. $account = isset($params['account'])? trim($params['account']) : '';
  110. if($account){
  111. $query->where(function($query) use($account){
  112. $query->where('a.account','like',"%{$account}%");
  113. });
  114. }
  115. })
  116. ->where(function ($query) use($params){
  117. // 日期
  118. $date = isset($params['date']) ? $params['date'] : [];
  119. $start = isset($date[0])? $date[0] : '';
  120. $end = isset($date[1])? $date[1] : '';
  121. $end = $start>=$end? '' : $end;
  122. if ($start) {
  123. $query->where('a.create_time','>=', strtotime($start));
  124. }
  125. if($end){
  126. $query->where('a.create_time','<=', strtotime($end));
  127. }
  128. });
  129. }
  130. /**
  131. * 收入提现
  132. * @param $userId
  133. * @param $params
  134. * @return array|false
  135. */
  136. public function withdraw($userId, $params)
  137. {
  138. // 参数验证
  139. $accountType = isset($params['account_type']) && $params['account_type']? intval($params['account_type']) : 2;
  140. $payType = isset($params['pay_type']) && $params['pay_type']? intval($params['pay_type']) : 10;
  141. $money = isset($params['money'])? floatval($params['money']) : 0;
  142. $accountRemark = isset($params['account_remark'])? trim($params['account_remark']) : '';
  143. $account = isset($params['account'])? trim($params['account']) : '';
  144. $realname = isset($params['realname'])? trim($params['realname']) : '';
  145. if($money<=0){
  146. $this->error = 2301;
  147. return false;
  148. }
  149. if($payType != 10 && (empty($realname) || empty($account))){
  150. $this->error = '收款账号不存在';
  151. return false;
  152. }
  153. $openWithdraw = ConfigService::make()->getConfigByCode('withdraw_open',1);
  154. if(!$openWithdraw){
  155. $this->error = 2304;
  156. return false;
  157. }
  158. $withdrawMin = ConfigService::make()->getConfigByCode('withdraw_min',0.1);
  159. if($withdrawMin>0 && $money < $withdrawMin){
  160. $this->error = lang(2305,['money'=>$withdrawMin]);
  161. return false;
  162. }
  163. // 锁
  164. $cacheLockKey = "caches:members:withdraw:{$userId}";
  165. if(RedisService::get($cacheLockKey)){
  166. $this->error = 1034;
  167. return false;
  168. }
  169. // 判断用户账号状态
  170. RedisService::set($cacheLockKey, ['user_id'=>$userId,'params'=>$params], rand(10,20));
  171. $userInfo = MemberService::make()->getInfo($userId,[], true);
  172. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  173. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  174. $agent = isset($userInfo['agent'])? $userInfo['agent'] : [];
  175. $agentBalance = isset($agent['balance'])? $agent['balance'] : 0;
  176. $nickname = isset($userInfo['nickname'])? $userInfo['nickname'] : '';
  177. $balance = $accountType==2?$agentBalance:$balance;
  178. if(empty($userInfo) || $status != 1){
  179. $this->error = 1045;
  180. RedisService::clear($cacheLockKey);
  181. return false;
  182. }
  183. if($accountType==2 && empty($agent)){
  184. $this->error = '代理账号或已被冻结,请联系客服';
  185. RedisService::clear($cacheLockKey);
  186. return false;
  187. }
  188. if($money > $balance){
  189. $this->error = '可提现金额不足';
  190. RedisService::clear($cacheLockKey);
  191. return false;
  192. }
  193. // 提现处理
  194. DB::beginTransaction();
  195. $updateData = ['balance'=>DB::raw("balance - {$money}"),'update_time'=>time()];
  196. // 会员账户
  197. if($accountType==1 && !MemberModel::where(['id'=> $userId])->update($updateData)){
  198. DB::rollBack();
  199. $this->error = '提现处理失败';
  200. RedisService::clear($cacheLockKey);
  201. return false;
  202. }
  203. // 代理账户
  204. else if($accountType==2 && !AgentModel::where(['user_id'=> $userId])->update($updateData)){
  205. DB::rollBack();
  206. $this->error = '提现处理失败';
  207. RedisService::clear($cacheLockKey);
  208. return false;
  209. }
  210. $system = isset($params['system']) && $params['system'] ? $params['system'] : [];
  211. $platform = isset($system['platform']) && $system['platform']? $system['platform'] : 'mp';
  212. $orderNo = get_order_num('LW');
  213. $order = [
  214. 'user_id'=> $userId,
  215. 'order_no'=> $orderNo,
  216. 'money'=> $money,
  217. 'type'=>2,
  218. 'account_type'=> $accountType,
  219. 'pay_type'=> $payType, // 收款方式
  220. 'source'=> $platform,// 提现渠道来源
  221. 'account_remark'=> $accountRemark,
  222. 'realname'=> $payType==10?'微信零钱' : $realname,
  223. 'account'=> $account,
  224. 'create_time'=> time(),
  225. 'status'=>1,
  226. 'mark'=>1
  227. ];
  228. if(!$orderId = $this->model::insertGetId($order)){
  229. DB::rollBack();
  230. $this->error = '提现处理失败';
  231. RedisService::clear($cacheLockKey);
  232. return false;
  233. }
  234. $log = [
  235. 'user_id' => $userId,
  236. 'source_order_no' => $orderNo,
  237. 'type' => 4,
  238. 'money' => $money,
  239. 'before_money' => $balance,
  240. 'date'=> date('Y-m-d'),
  241. 'create_time' => time(),
  242. 'remark' => '收入提现'.($payType==10?'到微信零钱':''),
  243. 'status' => 1,
  244. 'mark' => 1,
  245. ];
  246. if(!AccountLogModel::insertGetId($log)){
  247. DB::rollBack();
  248. $this->error = '提现处理失败';
  249. RedisService::clear($cacheLockKey);
  250. return false;
  251. }
  252. DB::commit();
  253. // 操作日志
  254. ActionLogModel::setRecord($userId,['type'=>2,'title'=>'收入提现','content'=>"姓名:{$realname},账号:{$account},提现{$money}元,单号:{$orderNo}",'module'=>'balanceLog']);
  255. ActionLogModel::record();
  256. RedisService::clear($cacheLockKey);
  257. $this->error = '提现申请成功,请耐心等候审核~';
  258. return ['id'=>$orderId,'money'=>$money];
  259. }
  260. public function confirm($userId, $params)
  261. {
  262. $id = isset($params['id'])?$params['id']: 0;
  263. $userInfo = MemberService::make()->getInfo($userId,[], true);
  264. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  265. if(empty($userInfo) || $status != 1){
  266. $this->error = 1045;
  267. return false;
  268. }
  269. $withdrawLog = BalanceLogModel::where(['id'=> $id,'mark'=>1])
  270. ->first();
  271. if(empty($withdrawLog)){
  272. $this->error = '提现数据不存在';
  273. return false;
  274. }
  275. }
  276. }