BalanceLogService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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\Common;
  12. use App\Models\AccountLogModel;
  13. use App\Models\ActionLogModel;
  14. use App\Models\BalanceLogModel;
  15. use App\Models\MemberModel;
  16. use App\Models\MessageModel;
  17. use App\Services\BaseService;
  18. use App\Services\RedisService;
  19. use Illuminate\Support\Facades\DB;
  20. /**
  21. * 余额管理-服务类
  22. * @author laravel开发员
  23. * @since 2020/11/11
  24. * @package App\Services\Common
  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. $model = clone $query;
  59. $counts = [
  60. 'count'=> $model->count('a.id'),
  61. 'total'=> $model->sum('a.money'),
  62. ];
  63. $list = $query->select(['a.*'])
  64. ->orderBy('a.create_time','desc')
  65. ->orderBy('a.id','desc')
  66. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  67. $list = $list? $list->toArray() :[];
  68. if($list){
  69. foreach($list['data'] as &$item){
  70. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  71. $item['pay_img'] = $item['pay_img']? get_image_url($item['pay_img']) : '';
  72. $item['member'] = isset($item['member'])? $item['member'] : [];
  73. }
  74. }
  75. return [
  76. 'pageSize'=> $pageSize,
  77. 'total'=>isset($list['total'])? $list['total'] : 0,
  78. 'counts'=>$counts,
  79. 'list'=> isset($list['data'])? $list['data'] : []
  80. ];
  81. }
  82. /**
  83. * 查询
  84. * @param $params
  85. * @return \Illuminate\Database\Eloquent\Builder
  86. */
  87. public function getQuery($params)
  88. {
  89. $where = ['a.mark' => 1];
  90. $type = isset($params['type'])? $params['type'] : 0;
  91. if($type>0){
  92. $where['a.type'] = $type;
  93. }
  94. $accountType = isset($params['account_type'])? $params['account_type'] : 0;
  95. if($accountType>0){
  96. $where['a.account_type'] = $accountType;
  97. }
  98. return $this->model->with(['member'])->from("balance_logs as a")
  99. ->leftJoin('member as b','b.id','=','a.user_id')
  100. ->where($where)
  101. ->where(function ($query) use($params) {
  102. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  103. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  104. if($userId){
  105. $query->where('a.user_id',$userId);
  106. }
  107. if ($keyword) {
  108. $query->where(function($query) use($keyword){
  109. $query->where('b.nickname','like',"%{$keyword}%")
  110. ->orWhere('b.mobile','like',"%{$keyword}%");
  111. });
  112. }
  113. $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
  114. if($orderNo){
  115. $query->where(function($query) use($orderNo){
  116. $query->where('a.order_no','like',"%{$orderNo}%");
  117. });
  118. }
  119. $status = isset($params['status'])? $params['status'] : 0;
  120. if($status==0){
  121. $query->whereIn('a.status',[2,4]);
  122. }else if($status<0){
  123. $query->whereIn('a.status', [1,3]);
  124. }else if($status>0){
  125. $query->where('a.status', $status);
  126. }
  127. })
  128. ->where(function ($query) use($params){
  129. // 日期
  130. $date = isset($params['date']) ? $params['date'] : [];
  131. $start = isset($date[0])? $date[0] : '';
  132. $end = isset($date[1])? $date[1] : '';
  133. $end = $start>=$end? '' : $end;
  134. if ($start) {
  135. $query->where('a.create_time','>=', strtotime($start));
  136. }
  137. if($end){
  138. $query->where('a.create_time','<=', strtotime($end));
  139. }
  140. });
  141. }
  142. /**
  143. * 添加或编辑
  144. * @return array
  145. * @since 2020/11/11
  146. * @author laravel开发员
  147. */
  148. public function edit()
  149. {
  150. $data = request()->all();
  151. return parent::edit($data); // TODO: Change the autogenerated stub
  152. }
  153. /**
  154. * 审核
  155. * @param $adminId
  156. * @param $params
  157. * @return array|false
  158. */
  159. public function confirm($adminId, $params)
  160. {
  161. $id = isset($params['id'])? intval($params['id']) : 0;
  162. $status = isset($params['status'])? intval($params['status']) : 0;
  163. $payStatus = isset($params['pay_status'])? intval($params['pay_status']) : 0;
  164. $confirmRemark = isset($params['confirm_remark'])? trim($params['confirm_remark']) : '';
  165. $payImg = isset($params['pay_img'])? trim($params['pay_img']) : '';
  166. $payImg = $payImg? get_image_path($payImg) : '';
  167. $info = $this->model->with(['member'])->where(['id'=> $id,'mark'=>1])->first();
  168. $userInfo = isset($info['member'])? $info['member'] : [];
  169. $orderStatus = isset($info['status'])? $info['status'] : 0;
  170. $orderUserId = isset($info['user_id'])? $info['user_id'] : 0;
  171. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  172. $money = isset($info['money'])? $info['money'] : 0;
  173. if(empty($info) || empty($userInfo) || $orderUserId<=0 || $money<=0){
  174. $this->error = '提现信息不存在或参数错误';
  175. return false;
  176. }
  177. if($orderStatus != 1){
  178. $this->error = '提现订单状态不可操作';
  179. return false;
  180. }
  181. if(!in_array($status,[2,3])){
  182. $this->error = '审核状态错误';
  183. return false;
  184. }
  185. if($status == 3 && empty($confirmRemark)){
  186. $this->error = '请填写审核驳回备注';
  187. return false;
  188. }
  189. DB::beginTransaction();
  190. $status = $payStatus==20 && $status==2?4:$status;
  191. $updateData = ['status'=>$status,'pay_status'=> $payStatus,'pay_img'=> $payImg,'confirm_admin_id'=>$adminId,'update_time'=>time(),'confirm_remark'=>$confirmRemark];
  192. if(!$this->model->where(['id'=> $id])->update($updateData)){
  193. DB::rollBack();
  194. $this->error = '提现审核失败';
  195. return false;
  196. }
  197. // 如果驳回
  198. if($status == 3){
  199. $updateData = ['balance'=>DB::raw("balance + {$money}"),'update_time'=>time()];
  200. if(!MemberModel::where(['id'=> $orderUserId])->update($updateData)){
  201. DB::rollBack();
  202. $this->error = '提现审核处理失败';
  203. return false;
  204. }
  205. $log = [
  206. 'user_id' => $orderUserId,
  207. 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
  208. 'type' => 5,
  209. 'money' => $money,
  210. 'after_money' => moneyFormat($balance+$money,2),
  211. 'date'=> date('Y-m-d'),
  212. 'create_time' => time(),
  213. 'remark' => '提现驳回',
  214. 'status' => 1,
  215. 'mark' => 1,
  216. ];
  217. if(!AccountLogModel::insertGetId($log)){
  218. DB::rollBack();
  219. $this->error = '提现审核处理失败';
  220. return false;
  221. }
  222. }else if($status == 2){
  223. // 线上直接打款逻辑
  224. }
  225. DB::commit();
  226. $this->error = '提现审核成功';
  227. return ['id'=>$id,'money'=>$money,'status'=>$status];
  228. }
  229. /**
  230. * 打款
  231. * @param $adminId
  232. * @param $params
  233. * @return array|false
  234. */
  235. public function payment($adminId, $params)
  236. {
  237. $id = isset($params['id'])? intval($params['id']) : 0;
  238. $payStatus = isset($params['pay_status'])? intval($params['pay_status']) : 0;
  239. $payImg = isset($params['pay_img'])? trim($params['pay_img']) : '';
  240. $payImg = $payImg? get_image_path($payImg) : '';
  241. $info = $this->model->with(['member'])->where(['id'=> $id,'mark'=>1])->first();
  242. $userInfo = isset($info['member'])? $info['member'] : [];
  243. $orderStatus = isset($info['status'])? $info['status'] : 0;
  244. $orderUserId = isset($info['user_id'])? $info['user_id'] : 0;
  245. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  246. $money = isset($info['money'])? $info['money'] : 0;
  247. if(empty($info) || empty($userInfo) || $orderUserId<=0 || $money<=0){
  248. $this->error = '提现信息不存在或参数错误';
  249. return false;
  250. }
  251. if($orderStatus != 2){
  252. $this->error = '提现订单状态不可操作';
  253. return false;
  254. }
  255. if($payStatus != 20){
  256. $this->error = '请选择已打款';
  257. return false;
  258. }
  259. DB::beginTransaction();
  260. $updateData = ['pay_status'=> $payStatus,'pay_img'=> $payImg,'update_time'=>time()];
  261. if($payStatus == 20){
  262. $updateData['status'] = 4;
  263. }
  264. if(!$this->model->where(['id'=> $id])->update($updateData)){
  265. DB::rollBack();
  266. $this->error = '提现打款处理失败';
  267. return false;
  268. }
  269. DB::commit();
  270. $this->error = '提现打款状态更新成功';
  271. return ['id'=>$id,'money'=>$money,'status'=>$payStatus];
  272. }
  273. /**
  274. * 删除
  275. * @return array
  276. */
  277. public function delete()
  278. {
  279. // 设置日志标题
  280. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除余额明细", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  281. ActionLogModel::record();
  282. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  283. return parent::delete(); // TODO: Change the autogenerated stub
  284. }
  285. /*\提现统计
  286. * @param int $type
  287. * @return array|mixed
  288. */
  289. public function getTotal($type=1)
  290. {
  291. $cacheKey = "caches:balances:total_{$type}";
  292. $data = RedisService::get($cacheKey);
  293. if($data){
  294. return $data;
  295. }
  296. $data = $this->model->where(['mark'=>1])
  297. ->where(function($query) use($type){
  298. if($type== 1){
  299. $query->where(['type'=>2])->whereIn('status',[2,4]);
  300. }else {
  301. $query->where(['type'=>2]);
  302. }
  303. })->sum('money');
  304. if($data){
  305. RedisService::set($cacheKey, $data, rand(300, 600));
  306. }
  307. return $data;
  308. }
  309. }