CapitalLogService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\CapitalLogModel;
  13. use App\Models\MemberModel;
  14. use App\Models\UserModel;
  15. use App\Services\BaseService;
  16. /**
  17. * 资产交易明细-服务类
  18. * Class CapitalLogService
  19. * @package App\Services\Common
  20. */
  21. class CapitalLogService extends BaseService
  22. {
  23. // 静态对象
  24. protected static $instance = null;
  25. /**
  26. * 构造函数
  27. * @since 2020/11/10
  28. * CapitalLogService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new CapitalLogModel();
  33. $this->userModel = new UserModel();
  34. $this->memberModel = new MemberModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. // 保证金缴纳/退保
  48. public function edit()
  49. {
  50. $data = request()->all();
  51. $id = isset($data['id'])? $data['id'] : 0;
  52. $userId = isset($data['user_id'])? $data['user_id'] : 0;
  53. $money = isset($data['num'])? floatval($data['num']) : 0;
  54. if($userId<=0){
  55. return returnJson('承兑商用户参数错误,请刷新后重试');
  56. }
  57. if($money<=0){
  58. return returnJson('请输入正确的金额');
  59. }
  60. $info = $this->userModel->getInfo($id);
  61. if(empty($info) || empty($info['user_id'])){
  62. return returnJson('用户不存在');
  63. }
  64. if($info['user_type'] != 2){
  65. returnJson('用户类型错误,非承兑商用户');
  66. }
  67. $changeType = isset($data['change_type'])? $data['change_type'] : 0;
  68. $changeType = $changeType==1? $changeType : 2;
  69. if($changeType==2 && $info['bond'] < $money){
  70. return returnJson('退保金额超出账户保证金余额:'.$info['bond']);
  71. }
  72. $data = [
  73. 'order_no'=> get_order_num('CT'),
  74. 'user_id'=> $info['user_id'],
  75. 'num'=> $money,
  76. 'type'=> 8,
  77. 'change_type'=> $changeType,
  78. 'trade_type'=> 4,
  79. 'total'=> $money,
  80. 'create_time'=> isset($data['datetime'])&&$data['datetime']? strtotime($data['datetime']) : time(),
  81. 'balance'=> $changeType==2? ($info['bond'] - $money) : ($info['bond']+ $money),
  82. 'remark'=> isset($data['remark'])? $data['remark'] : '',
  83. ];
  84. $result = parent::edit($data);
  85. $success = isset($result['success'])? $result['success'] : false;
  86. if($success){
  87. if($changeType == 1){
  88. $this->userModel->where(['id'=> $id])->increment('bond', $money);
  89. }else{
  90. $this->userModel->where(['id'=> $id])->decrement('bond', $money);
  91. }
  92. }
  93. return $result;
  94. }
  95. /**
  96. * 获取列表
  97. * @param $params 参数
  98. * @param int $pageSize 分页大小:默认 15
  99. * @return array
  100. */
  101. public function getDataList($params, $pageSize = 15)
  102. {
  103. $where = ['a.mark' => 1];
  104. $type = isset($params['type'])? $params['type'] : 1;
  105. $changeType = isset($params['change_type'])? $params['change_type'] : 1;
  106. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  107. if($type>0){
  108. $where['a.type'] = $type;
  109. }
  110. if($changeType>0){
  111. $where['a.change_type'] = $changeType;
  112. }
  113. if($userId>0){
  114. $where['a.user_id'] = $userId;
  115. }
  116. $list = $this->model->from('capital_logs as a')
  117. ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
  118. ->where($where)
  119. ->where(function ($query) use($params){
  120. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  121. if($keyword){
  122. $query->where('a.order_no','like',"%{$keyword}%");
  123. }
  124. // 日期
  125. $date = isset($params['date']) ? $params['date'] : [];
  126. $start = isset($date[0])? $date[0] : '';
  127. $end = isset($date[1])? $date[1] : '';
  128. $end = $start>=$end? '' : $end;
  129. if ($start) {
  130. $query->where('a.create_time','>=', strtotime($start));
  131. }
  132. if($end){
  133. $query->where('a.create_time','<', strtotime($end));
  134. }
  135. })
  136. ->select(['a.*', 'm.username'])
  137. ->orderBy('a.create_time','desc')
  138. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  139. $list = $list? $list->toArray() :[];
  140. if($list){
  141. foreach($list['data'] as &$item){
  142. $item['time_text'] = $item['create_time']? datetime(strtotime($item['create_time']), 'm-d H:i'):'';
  143. }
  144. }
  145. return [
  146. 'pageSize'=> $pageSize,
  147. 'total'=>isset($list['total'])? $list['total'] : 0,
  148. 'list'=> isset($list['data'])? $list['data'] : []
  149. ];
  150. }
  151. }