BalanceLogService.php 11 KB

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