DepositService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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\DepositModel;
  14. use App\Models\MemberModel;
  15. use App\Models\MessageModel;
  16. use App\Models\ActionLogModel;
  17. use App\Services\BaseService;
  18. use App\Services\ConfigService;
  19. use App\Services\PaymentService;
  20. use App\Services\RedisService;
  21. use App\Services\SmsService;
  22. use Illuminate\Support\Facades\DB;
  23. /**
  24. * 保证金订单管理-服务类
  25. * @author laravel开发员
  26. * @since 2020/11/11
  27. * @package App\Services\Api
  28. */
  29. class DepositService extends BaseService
  30. {
  31. // 静态对象
  32. protected static $instance = null;
  33. /**
  34. * 构造函数
  35. * @author laravel开发员
  36. * @since 2020/11/11
  37. */
  38. public function __construct()
  39. {
  40. $this->model = new DepositModel();
  41. }
  42. /**
  43. * 静态入口
  44. */
  45. public static function make()
  46. {
  47. if (!self::$instance) {
  48. self::$instance = new static();
  49. }
  50. return self::$instance;
  51. }
  52. /**
  53. * 列表数据
  54. * @param $params
  55. * @param int $pageSize
  56. * @return array
  57. */
  58. public function getDataList($params, $pageSize = 15)
  59. {
  60. $query = $this->getQuery($params);
  61. $refundStatus = isset($params['refund_status']) ? $params['refund_status'] : -1;
  62. $model = clone $query;
  63. $counts = [
  64. 'count'=> $model->count('a.id'),
  65. 'total'=> $model->sum($refundStatus>=0?'a.refund_money':'a.money'),
  66. ];
  67. if($refundStatus==0){
  68. $query->orderBy('a.refund_status', 'asc')
  69. ->orderBy('a.create_time', 'desc')
  70. ->orderBy('a.id', 'desc');
  71. }else if($refundStatus>0){
  72. $query->orderBy('a.create_time', 'desc')
  73. ->orderBy('a.id', 'desc');
  74. }else{
  75. $query->orderBy('a.create_time', 'desc')
  76. ->orderBy('a.id', 'desc');
  77. }
  78. $field = ["a.*"];
  79. $list = $query->select($field)
  80. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  81. $list = $list ? $list->toArray() : [];
  82. if ($list) {
  83. foreach ($list['data'] as &$item){
  84. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  85. $item['time_text'] = $item['create_time']? datetime($item['create_time'],'Y年m月d日') : '';
  86. }
  87. $datas = [
  88. 'pageSize' => $pageSize,
  89. 'total' => isset($list['total']) ? $list['total'] : 0,
  90. 'list' => isset($list['data']) ? $list['data'] : [],
  91. 'counts' => $counts,
  92. ];
  93. // 消息已读
  94. MessageModel::where(['is_read'=>2,'type'=>$refundStatus>=0?4:3,'mark'=>1])->update(['is_read'=>1,'update_time'=>time()]);
  95. }
  96. return $datas;
  97. }
  98. /**
  99. * 查询条件
  100. * @param $params
  101. * @return mixed
  102. */
  103. public function getQuery($params)
  104. {
  105. $where = ['a.status' => 0, 'a.mark' => 1];
  106. $status = isset($params['status']) ? $params['status'] : 0;
  107. if ($status > 0) {
  108. $where['a.status'] = $status;
  109. } else {
  110. unset($where['a.status']);
  111. }
  112. $model = $this->model->with(['user'])->from('deposit_orders as a')
  113. ->leftJoin('member as b','b.id','=','a.user_id')
  114. ->where($where)
  115. ->where(function ($query) use ($params) {
  116. $refundNo= isset($params['refund_no']) ? trim($params['refund_no']) : '';
  117. $orderNo = isset($params['order_no']) ? trim($params['order_no']) : '';
  118. $refundStatus = isset($params['refund_status']) ? $params['refund_status'] : -1;
  119. if($refundNo){
  120. $query->where(function($query) use($refundNo){
  121. $query->where('a.refund_no', 'like', "%{$refundNo}%")
  122. ->orWhere('a.refund_transaction_id', 'like', "%{$refundNo}%");
  123. });
  124. }else if($orderNo){
  125. $query->where(function($query) use($orderNo){
  126. $query->where('a.order_no', 'like', "%{$orderNo}%")
  127. ->orWhere('a.transaction_id', 'like', "%{$orderNo}%");
  128. });
  129. }
  130. if($refundStatus==0){
  131. $query->where(['a.status'=>3])->where('a.refund_status','>',1);
  132. }else if($refundStatus>0){
  133. $query->where(['a.status'=>3])->where('a.refund_status', $refundStatus);
  134. }
  135. // 用户
  136. $account = isset($params['account']) ? trim($params['account']) : '';
  137. if ($account) {
  138. $query->where(function ($query) use ($account) {
  139. $query->where('b.mobile', 'like', "%{$account}%")
  140. ->orWhere('b.nickname', 'like', "%{$account}%");
  141. });
  142. }
  143. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  144. if ($userId>0) {
  145. $query->where('a.user_id', $userId);
  146. }
  147. });
  148. return $model;
  149. }
  150. /**
  151. * 详情信息
  152. * @param $id
  153. * @return mixed
  154. */
  155. public function getInfo($id)
  156. {
  157. $cacheKey = "caches:deposit:info_{$id}";
  158. $info = RedisService::get($cacheKey);
  159. if($info){
  160. return $info;
  161. }
  162. $info = $this->model->with(['user'])->where(['order_id' => $id])->first();
  163. $info = $info? $info->toArray() :[];
  164. if($info){
  165. RedisService::set($cacheKey, $info, rand(5,10));
  166. }
  167. return $info;
  168. }
  169. /**
  170. * 审核
  171. * @param $adminId
  172. * @param $params
  173. * @return array|false
  174. */
  175. public function confirm($adminId, $params)
  176. {
  177. $id = isset($params['id'])? intval($params['id']) : 0;
  178. $status = isset($params['refund_status'])? intval($params['refund_status']) : 0;
  179. $confirmRemark = isset($params['refund_confirm_remark'])? trim($params['refund_confirm_remark']) : '';
  180. $info = $this->model->with(['user'])->where(['id'=> $id,'mark'=>1])->first();
  181. $userInfo = isset($info['user'])? $info['user'] : [];
  182. $orderStatus = isset($info['refund_status'])? $info['refund_status'] : 0;
  183. $orderUserId = isset($info['user_id'])? $info['user_id'] : 0;
  184. $refundMoney = isset($info['refund_money'])? $info['refund_money'] : 0;
  185. if(empty($info) || empty($userInfo) || $orderUserId<=0 || $refundMoney<=0){
  186. $this->error = '退保申请信息不存在或参数错误';
  187. return false;
  188. }
  189. if($orderStatus != 1){
  190. $this->error = '退保订单状态不可操作';
  191. return false;
  192. }
  193. if(!in_array($status,[2,3])){
  194. $this->error = '退保审核状态错误';
  195. return false;
  196. }
  197. if($status == 3 && empty($confirmRemark)){
  198. $this->error = '请填写审核驳回备注';
  199. return false;
  200. }
  201. DB::beginTransaction();
  202. $updateOrder = ['refund_status'=>$status,'confirm_admin_id'=>$adminId,'update_time'=>time(),'refund_confirm_remark'=>$confirmRemark];
  203. // 如果驳回
  204. if($status == 3){
  205. $updateData = ['deposit'=> $refundMoney,'update_time'=>time()];
  206. if(!MemberModel::where(['id'=> $orderUserId])->update($updateData)){
  207. DB::rollBack();
  208. $this->error = '退保审核处理失败';
  209. return false;
  210. }
  211. }else if($status == 2){
  212. // 线上直接退款逻辑
  213. $payType = isset($info['pay_type'])? $info['pay_type'] : 0;
  214. $order = [
  215. 'money' => $refundMoney,
  216. 'pay_type' => $payType,
  217. 'order_no' => isset($info['order_no'])? $info['order_no'] : '',
  218. 'out_trade_no' => isset($info['refund_no'])? $info['refund_no'] : '',
  219. 'transaction_id' => isset($info['transaction_id'])? $info['transaction_id'] : '',
  220. 'remark'=> '退保'
  221. ];
  222. if(!PaymentService::make()->refund($order)){
  223. DB::rollBack();
  224. $this->error = '退保退款处理失败';
  225. return false;
  226. }
  227. // 支付宝调用成功直接完成
  228. if($payType == 20){
  229. $updateOrder['refund_status'] = 4;
  230. $updateOrder['remark'] = '已退保';
  231. }
  232. }
  233. if(!$this->model->where(['id'=> $id])->update($updateOrder)){
  234. DB::rollBack();
  235. $this->error = '退款审核处理失败';
  236. return false;
  237. }
  238. DB::commit();
  239. $this->error = '退款审核成功';
  240. return ['id'=>$id,'money'=>$refundMoney,'refund_status'=>$status];
  241. }
  242. /**
  243. * 打款
  244. * @param $adminId
  245. * @param $params
  246. * @return array|false
  247. */
  248. public function payment($adminId, $params)
  249. {
  250. $id = isset($params['id'])? intval($params['id']) : 0;
  251. $info = $this->model->with(['user'])->where(['id'=> $id,'mark'=>1])->first();
  252. $userInfo = isset($info['user'])? $info['user'] : [];
  253. $orderStatus = isset($info['refund_status'])? $info['refund_status'] : 0;
  254. $orderUserId = isset($info['user_id'])? $info['user_id'] : 0;
  255. $refundMoney = isset($info['refund_money'])? $info['refund_money'] : 0;
  256. if(empty($info) || empty($userInfo) || $orderUserId<=0 || $refundMoney<=0){
  257. $this->error = '退保申请信息不存在或参数错误';
  258. return false;
  259. }
  260. if($orderStatus == 4){
  261. $this->error = '退保订单已打款';
  262. return false;
  263. }
  264. if($orderStatus != 2){
  265. $this->error = '退保订单状态不可操作';
  266. return false;
  267. }
  268. // 线上直接退款逻辑
  269. $payType = isset($info['pay_type'])? $info['pay_type'] : 0;
  270. $order = [
  271. 'money' => $refundMoney,
  272. 'pay_type' => $payType,
  273. 'order_no' => isset($info['order_no'])? $info['order_no'] : '',
  274. 'out_trade_no' => isset($info['refund_no'])? $info['refund_no'] : '',
  275. 'transaction_id' => isset($info['transaction_id'])? $info['transaction_id'] : '',
  276. 'remark'=> '退保'
  277. ];
  278. DB::beginTransaction();
  279. if(!PaymentService::make()->refund($order)){
  280. DB::rollBack();
  281. $this->error = '退保退款处理请求失败';
  282. return false;
  283. }
  284. if($payType == 20 && !$this->model->where(['id'=> $id])->update(['refund_status'=>4,'remark'=>'已退保','update_time'=>time()])){
  285. DB::rollBack();
  286. $this->error = '退保退款处理失败';
  287. return false;
  288. }
  289. DB::commit();
  290. $this->error = '退保打款请求成功';
  291. return ['id'=>$id,'money'=>$refundMoney,'status'=>$orderStatus];
  292. }
  293. /**
  294. * 统计
  295. * @param int $type
  296. * @return array|mixed
  297. */
  298. public function getTotal($type=0)
  299. {
  300. $cacheKey = "caches:deposit:total_{$type}";
  301. $data = RedisService::get($cacheKey);
  302. if($data){
  303. return $data;
  304. }
  305. $data = $this->model->where(['mark'=>1])
  306. ->where(function($query) use($type){
  307. if($type== 1){
  308. $query->where(['status'=>3])->whereIn('refund_status',[2,4]);
  309. }else {
  310. $query->where(['status'=>3]);
  311. }
  312. })->sum('money');
  313. if($data){
  314. RedisService::set($cacheKey, $data, rand(300, 600));
  315. }
  316. return $data;
  317. }
  318. /**
  319. * 删除七天之前标记软删除的数据
  320. */
  321. public function delete()
  322. {
  323. // 设置日志标题
  324. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除充值记录信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  325. ActionLogModel::record();
  326. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  327. return parent::delete();
  328. }
  329. }