BalanceLogService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. MessageModel::where(['is_read'=>2,'type'=>6,'mark'=>1])->update(['is_read'=>1,'update_time'=>time()]);
  76. }
  77. return [
  78. 'pageSize'=> $pageSize,
  79. 'total'=>isset($list['total'])? $list['total'] : 0,
  80. 'counts'=>$counts,
  81. 'list'=> isset($list['data'])? $list['data'] : []
  82. ];
  83. }
  84. /**
  85. * 查询
  86. * @param $params
  87. * @return \Illuminate\Database\Eloquent\Builder
  88. */
  89. public function getQuery($params)
  90. {
  91. $where = ['a.mark' => 1];
  92. $type = isset($params['type'])? $params['type'] : 0;
  93. if($type>0){
  94. $where['a.type'] = $type;
  95. }
  96. return $this->model->with(['member'])->from("balance_logs as a")
  97. ->leftJoin('member as b','b.id','=','a.user_id')
  98. ->where($where)
  99. ->where(function ($query) use($params) {
  100. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  101. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  102. if($userId){
  103. $query->where('a.user_id',$userId);
  104. }
  105. if ($keyword) {
  106. $query->where(function($query) use($keyword){
  107. $query->where('b.nickname','like',"%{$keyword}%")
  108. ->orWhere('b.mobile','like',"%{$keyword}%");
  109. });
  110. }
  111. $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
  112. if($orderNo){
  113. $query->where(function($query) use($orderNo){
  114. $query->where('a.order_no','like',"%{$orderNo}%");
  115. });
  116. }
  117. $status = isset($params['status'])? $params['status'] : -1;
  118. if($status==0){
  119. $query->whereIn('a.status',[2,3,4]);
  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. $updateData = ['status'=>$status,'pay_status'=> $payStatus,'pay_img'=> $payImg,'confirm_admin_id'=>$adminId,'update_time'=>time(),'confirm_remark'=>$confirmRemark];
  187. if(!$this->model->where(['id'=> $id])->update($updateData)){
  188. DB::rollBack();
  189. $this->error = '提现审核失败';
  190. return false;
  191. }
  192. // 如果驳回
  193. if($status == 3){
  194. $updateData = ['balance'=>DB::raw("balance + {$money}"),'update_time'=>time()];
  195. if(!MemberModel::where(['id'=> $orderUserId])->update($updateData)){
  196. DB::rollBack();
  197. $this->error = '提现审核处理失败';
  198. return false;
  199. }
  200. $log = [
  201. 'user_id' => $orderUserId,
  202. 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
  203. 'type' => 5,
  204. 'money' => $money,
  205. 'before_money' => $balance,
  206. 'date'=> date('Y-m-d'),
  207. 'create_time' => time(),
  208. 'remark' => '提现驳回',
  209. 'status' => 1,
  210. 'mark' => 1,
  211. ];
  212. if(!AccountLogModel::insertGetId($log)){
  213. DB::rollBack();
  214. $this->error = '提现审核处理失败';
  215. return false;
  216. }
  217. }else if($status == 2){
  218. // 线上直接打款逻辑
  219. }
  220. DB::commit();
  221. $this->error = '提现审核成功';
  222. return ['id'=>$id,'money'=>$money,'status'=>$status];
  223. }
  224. /**
  225. * 打款
  226. * @param $adminId
  227. * @param $params
  228. * @return array|false
  229. */
  230. public function payment($adminId, $params)
  231. {
  232. $id = isset($params['id'])? intval($params['id']) : 0;
  233. $payStatus = isset($params['pay_status'])? intval($params['pay_status']) : 0;
  234. $payImg = isset($params['pay_img'])? trim($params['pay_img']) : '';
  235. $payImg = $payImg? get_image_path($payImg) : '';
  236. $info = $this->model->with(['member'])->where(['id'=> $id,'mark'=>1])->first();
  237. $userInfo = isset($info['member'])? $info['member'] : [];
  238. $orderStatus = isset($info['status'])? $info['status'] : 0;
  239. $orderUserId = isset($info['user_id'])? $info['user_id'] : 0;
  240. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  241. $money = isset($info['money'])? $info['money'] : 0;
  242. if(empty($info) || empty($userInfo) || $orderUserId<=0 || $money<=0){
  243. $this->error = '提现信息不存在或参数错误';
  244. return false;
  245. }
  246. if($orderStatus != 2){
  247. $this->error = '提现订单状态不可操作';
  248. return false;
  249. }
  250. if($payStatus != 2){
  251. $this->error = '请选择已打款';
  252. return false;
  253. }
  254. DB::beginTransaction();
  255. $updateData = ['pay_status'=> $payStatus,'pay_img'=> $payImg,'update_time'=>time()];
  256. if(!$this->model->where(['id'=> $id])->update($updateData)){
  257. DB::rollBack();
  258. $this->error = '提现打款处理失败';
  259. return false;
  260. }
  261. DB::commit();
  262. $this->error = '提现打款状态更新成功';
  263. return ['id'=>$id,'money'=>$money,'status'=>$payStatus];
  264. }
  265. /**
  266. * 删除
  267. * @return array
  268. */
  269. public function delete()
  270. {
  271. // 设置日志标题
  272. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除余额明细", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  273. ActionLogModel::record();
  274. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  275. return parent::delete(); // TODO: Change the autogenerated stub
  276. }
  277. /*\提现统计
  278. * @param int $type
  279. * @return array|mixed
  280. */
  281. public function getTotal($type=1)
  282. {
  283. $cacheKey = "caches:balances:total_{$type}";
  284. $data = RedisService::get($cacheKey);
  285. if($data){
  286. return $data;
  287. }
  288. $data = $this->model->where(['mark'=>1])
  289. ->where(function($query) use($type){
  290. if($type== 1){
  291. $query->where(['type'=>2])->whereIn('status',[2,4]);
  292. }else {
  293. $query->where(['type'=>2]);
  294. }
  295. })->sum('money');
  296. if($data){
  297. RedisService::set($cacheKey, $data, rand(300, 600));
  298. }
  299. return $data;
  300. }
  301. }