BalanceLogService.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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\BalanceLogModel;
  15. use App\Models\MemberModel;
  16. use App\Services\BaseService;
  17. use App\Services\ConfigService;
  18. use App\Services\RedisService;
  19. use Illuminate\Support\Facades\DB;
  20. /**
  21. * 余额管理-服务类
  22. * @author laravel开发员
  23. * @since 2020/11/11
  24. */
  25. class BalanceLogService extends BaseService
  26. {
  27. public static $instance = null;
  28. /**
  29. * 构造函数
  30. * @author laravel开发员
  31. * @since 2020/11/11
  32. * AccountService constructor.
  33. */
  34. public function __construct()
  35. {
  36. $this->model = new BalanceLogModel();
  37. }
  38. /**
  39. * 静态入口
  40. * @return static|null
  41. */
  42. public static function make()
  43. {
  44. if (!self::$instance) {
  45. self::$instance = (new static());
  46. }
  47. return self::$instance;
  48. }
  49. /**
  50. * @param $params
  51. * @param int $pageSize
  52. * @return array
  53. */
  54. public function getDataList($params, $pageSize = 15)
  55. {
  56. $query = $this->getQuery($params);
  57. $list = $query->select(['a.*'])
  58. ->orderBy('a.status','asc')
  59. ->orderBy('a.create_time','desc')
  60. ->orderBy('a.id','desc')
  61. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  62. $list = $list? $list->toArray() :[];
  63. if($list){
  64. foreach($list['data'] as &$item){
  65. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  66. $item['time_text'] = $item['create_time']? datetime($item['create_time'],'Y年m月d日') : '';
  67. }
  68. }
  69. return [
  70. 'pageSize'=> $pageSize,
  71. 'total'=>isset($list['total'])? $list['total'] : 0,
  72. 'list'=> isset($list['data'])? $list['data'] : []
  73. ];
  74. }
  75. public function getQuery($params)
  76. {
  77. $where = ['a.mark' => 1];
  78. $status = isset($params['status'])? $params['status'] : 0;
  79. $type = isset($params['type'])? $params['type'] : 0;
  80. if($status>0){
  81. $where['a.status'] = $status;
  82. }
  83. if($type>0){
  84. $where['a.type'] = $type;
  85. }
  86. return $this->model->with(['member'])->from("balance_logs as a")
  87. ->leftJoin('member as b','b.id','=','a.user_id')
  88. ->where($where)
  89. ->where(function ($query) use($params) {
  90. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  91. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  92. if($userId){
  93. $query->where('a.user_id',$userId);
  94. }
  95. if ($keyword) {
  96. $query->where(function($query) use($keyword){
  97. $query->where('b.nickname','like',"%{$keyword}%")
  98. ->orWhere('b.mobile','like',"%{$keyword}%")
  99. ->orWhere('b.realname','like',"%{$keyword}%");
  100. });
  101. }
  102. $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
  103. if($orderNo){
  104. $query->where(function($query) use($orderNo){
  105. $query->where('a.order_no','like',"%{$orderNo}%");
  106. });
  107. }
  108. $account = isset($params['account'])? trim($params['account']) : '';
  109. if($account){
  110. $query->where(function($query) use($account){
  111. $query->where('a.account','like',"%{$account}%");
  112. });
  113. }
  114. })
  115. ->where(function ($query) use($params){
  116. // 日期
  117. $date = isset($params['date']) ? $params['date'] : [];
  118. $start = isset($date[0])? $date[0] : '';
  119. $end = isset($date[1])? $date[1] : '';
  120. $end = $start>=$end? '' : $end;
  121. if ($start) {
  122. $query->where('a.create_time','>=', strtotime($start));
  123. }
  124. if($end){
  125. $query->where('a.create_time','<=', strtotime($end));
  126. }
  127. });
  128. }
  129. /**
  130. * 收入提现
  131. * @param $userId
  132. * @param $params
  133. * @return array|false
  134. */
  135. public function withdraw($userId, $params)
  136. {
  137. // 参数验证
  138. $money = isset($params['money'])? floatval($params['money']) : 0;
  139. $remark = isset($params['remark'])? trim($params['remark']) : '';
  140. $account = isset($params['account'])? trim($params['account']) : '';
  141. $realname = isset($params['realname'])? trim($params['realname']) : '';
  142. if($money<=0){
  143. $this->error = 2301;
  144. return false;
  145. }
  146. if(empty($realname) || empty($account)){
  147. $this->error = 2302;
  148. return false;
  149. }
  150. $openWithdraw = ConfigService::make()->getConfigByCode('withdraw_open',1);
  151. if(!$openWithdraw){
  152. $this->error = 2304;
  153. return false;
  154. }
  155. $withdrawMin = ConfigService::make()->getConfigByCode('withdraw_min',0.1);
  156. if($withdrawMin>0 && $money < $withdrawMin){
  157. $this->error = lang(2305,['money'=>$withdrawMin]);
  158. return false;
  159. }
  160. // 锁
  161. $cacheLockKey = "caches:members:withdraw:{$userId}";
  162. if(RedisService::get($cacheLockKey)){
  163. $this->error = 1034;
  164. return false;
  165. }
  166. // 判断用户账号状态
  167. RedisService::set($cacheLockKey, ['user_id'=>$userId,'params'=>$params], rand(10,20));
  168. $userInfo = MemberService::make()->getInfo($userId,[], true);
  169. $confirmStatus = isset($userInfo['confirm_status'])? $userInfo['confirm_status'] : 0;
  170. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  171. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  172. $nickname = isset($userInfo['nickname'])? $userInfo['nickname'] : '';
  173. if(empty($userInfo) || $status != 1){
  174. $this->error = 2016;
  175. RedisService::clear($cacheLockKey);
  176. return false;
  177. }
  178. // 账号审核情况
  179. if($confirmStatus != 1){
  180. $this->error = 2042;
  181. RedisService::clear($cacheLockKey);
  182. return false;
  183. }
  184. if($money> $balance){
  185. $this->error = 2303;
  186. RedisService::clear($cacheLockKey);
  187. return false;
  188. }
  189. // 提现处理
  190. DB::beginTransaction();
  191. $updateData = ['balance'=>DB::raw("balance - {$money}"),'update_time'=>time()];
  192. if(!MemberModel::where(['id'=> $userId])->update($updateData)){
  193. DB::rollBack();
  194. $this->error = 2306;
  195. RedisService::clear($cacheLockKey);
  196. return false;
  197. }
  198. $orderNo = get_order_num('JW');
  199. $order = [
  200. 'user_id'=> $userId,
  201. 'order_no'=> $orderNo,
  202. 'money'=> $money,
  203. 'before_money'=> $balance,
  204. 'type'=>2,
  205. 'pay_type'=>0,
  206. 'remark'=> $remark,
  207. 'realname'=> $realname,
  208. 'account'=> $account,
  209. 'create_time'=> time(),
  210. 'status'=>1,
  211. 'mark'=>1
  212. ];
  213. if(!$orderId = $this->model::insertGetId($order)){
  214. DB::rollBack();
  215. $this->error = 2307;
  216. RedisService::clear($cacheLockKey);
  217. return false;
  218. }
  219. $log = [
  220. 'user_id' => $userId,
  221. 'source_order_no' => $orderNo,
  222. 'type' => 4,
  223. 'money' => $money,
  224. 'before_money' => $balance,
  225. 'date'=> date('Y-m-d'),
  226. 'create_time' => time(),
  227. 'remark' => '收入提现',
  228. 'status' => 1,
  229. 'mark' => 1,
  230. ];
  231. if(!AccountLogModel::insertGetId($log)){
  232. DB::rollBack();
  233. $this->error = 2307;
  234. RedisService::clear($cacheLockKey);
  235. return false;
  236. }
  237. DB::commit();
  238. $title = "用户[{$nickname}]申请了提现";
  239. $message = [
  240. 'from_uid'=> $userId,
  241. 'to_uid'=> 1,
  242. 'op'=> 'notice',
  243. 'scene'=> 'withdraw',
  244. 'message'=> '提现消息',
  245. 'type'=> 6,
  246. 'order'=>[
  247. 'title'=> $title.'<span class="ele-text-primary">查看订单</span>',
  248. 'order_no'=> $orderNo,
  249. 'money'=> $money,
  250. 'date'=> date('Y-m-d H:i:s'),
  251. 'user_id'=> $userId,
  252. 'type'=> 'withdraw',
  253. 'remark'=> '提现消息',
  254. ]
  255. ];
  256. // 操作日志
  257. ActionLogModel::setRecord($userId,['type'=>2,'title'=>'收入提现','content'=>"姓名:{$realname},账号:{$account},提现{$money}元,单号:{$orderNo}",'module'=>'balanceLog']);
  258. ActionLogModel::record();
  259. $this->error = 2308;
  260. return ['id'=>$orderId,'message'=>$message,'money'=>$money];
  261. }
  262. }