BalanceLogService.php 11 KB

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