AccountLogService.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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\MemberModel;
  15. use App\Services\BaseService;
  16. use App\Services\RedisService;
  17. use Illuminate\Support\Facades\DB;
  18. /**
  19. * 承兑商管理-服务类
  20. * @author laravel开发员
  21. * @since 2020/11/11
  22. * @package App\Services\Common
  23. */
  24. class AccountLogService extends BaseService
  25. {
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new AccountLogModel();
  34. }
  35. /**
  36. * 获取列表
  37. * @param $params 参数
  38. * @param int $pageSize 分页大小:默认 15
  39. * @return array
  40. */
  41. public function getDataList($params, $pageSize = 10, $field = [])
  42. {
  43. $query = $this->getQuery($params);
  44. $list = $query->select($field ? $field : ['a.*', 'b.nickname','b.wallet_url'])
  45. ->orderBy('a.create_time','desc')
  46. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  47. $list = $list ? $list->toArray() : [];
  48. if ($list) {
  49. foreach ($list['data'] as &$item) {
  50. $item['create_time'] = datetime($item['create_time'],'Y-m-d H:i:s');
  51. if($item['user_type'] == 1){
  52. $item['uid'] = $item['user_id'];
  53. $item['account'] = isset($item['nickname'])&& $item['nickname']?$item['nickname']: $item['user_id'];
  54. }
  55. }
  56. }
  57. return [
  58. 'pageSize' => $pageSize,
  59. 'total' => isset($list['total']) ? $list['total'] : 0,
  60. 'list' => isset($list['data']) ? $list['data'] : []
  61. ];
  62. }
  63. /**
  64. * 查询构造
  65. * @param $params
  66. * @return \Illuminate\Database\Eloquent\Builder
  67. */
  68. public function getQuery($params)
  69. {
  70. $where = ['a.mark' => 1];
  71. return $this->model->with(['member'])
  72. ->from('account_log as a')
  73. ->leftJoin('member as b', function($join) {
  74. $join->on('b.id','=', 'a.user_id')->where('a.user_type',1);
  75. })
  76. ->where($where)
  77. ->where(function ($query) use ($params) {
  78. $kw = isset($params['keyword']) ? trim($params['keyword']) : '';
  79. if ($kw) {
  80. $query->where('b.nickname', 'like', "%{$params['keyword']}%")
  81. ->orWhere('b.wallet_url', '=', trim($params['keyword']))
  82. ->orWhere('b.id', '=', $params['keyword']);
  83. }
  84. })
  85. ->where(function ($query) use($params){
  86. // 日期
  87. $date = isset($params['date']) ? $params['date'] : [];
  88. $start = isset($date[0])? $date[0] : '';
  89. $end = isset($date[1])? $date[1] : '';
  90. $end = $start>=$end? '' : $end;
  91. if ($start) {
  92. $query->where('a.create_time','>=', strtotime($start));
  93. }
  94. if($end){
  95. $query->where('a.create_time','<=', strtotime($end));
  96. }
  97. $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
  98. if($orderNo){
  99. $query->where('a.order_no', 'like', "%{$orderNo}%");
  100. }
  101. $hash = isset($params['hash'])? trim($params['hash']) : '';
  102. if($hash){
  103. $query->where('a.hash', '=', trim($hash));
  104. }
  105. $walletUrl = isset($params['wallet_url']) ? trim($params['wallet_url']) : '';
  106. if ($walletUrl) {
  107. $query->where('b.wallet_url', $walletUrl);
  108. }
  109. $userType = isset($params['user_type'])? $params['user_type'] : 0;
  110. if ($userType) {
  111. $query->where('a.user_type', $userType);
  112. }
  113. $type = isset($params['type'])? $params['type'] : 0;
  114. if (is_array($type)) {
  115. $query->whereIn('a.type', $type);
  116. } else if($type){
  117. $query->where('a.type', $type);
  118. }
  119. $coinType = isset($params['coin_type'])? $params['coin_type'] : 0;
  120. if (is_array($coinType)) {
  121. $query->whereIn('a.coin_type', $coinType);
  122. } else if($coinType){
  123. $query->where('a.coin_type', $coinType);
  124. }
  125. $status = isset($params['status'])? $params['status'] : 0;
  126. if (is_array($status)) {
  127. $query->whereIn('a.status', $status);
  128. } else if($status){
  129. $query->where('a.status', $status);
  130. }
  131. });
  132. }
  133. /**
  134. * 统计
  135. * @param $params
  136. * @return array
  137. */
  138. public function count($params)
  139. {
  140. $query = $this->getQuery($params);
  141. $count = $query->count('a.id');
  142. $query1 = clone $query;
  143. $query2 = clone $query;
  144. $total1 = $query1->where('a.money','>',0)->sum('a.money');
  145. $total2 = $query2->where('a.money','<=',0)->sum('a.money');
  146. return [
  147. 'count' => $count,
  148. 'total1' => round(abs($total1),2), // 进
  149. 'total2' => round(abs($total2),2), // 出
  150. 'total' => round(abs($total1)+abs($total2),2)
  151. ];
  152. }
  153. /**
  154. * 平台充兑
  155. * @param $adminId
  156. * @param $params
  157. * @return bool
  158. */
  159. public function changeAccount($adminId,$params)
  160. {
  161. $userType = isset($params['user_type'])? intval($params['user_type']) : 1;
  162. $coinType = isset($params['coin_type'])? intval($params['coin_type']) : 1;
  163. $type = isset($params['type'])? intval($params['type']) : 1;
  164. $accountId = isset($params['user_id'])? intval($params['user_id']) : 0;
  165. $amount = isset($params['amount'])? floatval($params['amount']) : 0;
  166. if($amount<=0){
  167. $this->error = 4100;
  168. return false;
  169. }
  170. if(!in_array($type,[1,2])){
  171. $this->error = 4003;
  172. return false;
  173. }
  174. $userTypes = [1=>'会员'];
  175. $fields = [1=>'usdt',2=>'sbt',3=>'profit'];
  176. $coinNames = [1=>'USDT余额',2=>'SBT',3=>'收益/奖励'];
  177. $field = isset($fields[$coinType])? $fields[$coinType] : 'usdt';
  178. $coinName = isset($coinNames[$coinType])? $coinNames[$coinType] : 'USDT余额';
  179. $accountName = isset($userTypes[$userType])? $userTypes[$userType] : '会员';
  180. ActionLogModel::setTitle("{$accountName}[ID:{$accountId}]{$coinName}账户上下分");
  181. ActionLogModel::record();
  182. $userInfo = MemberModel::where(['id'=> $accountId,'mark'=>1])->first();
  183. $userId = isset($userInfo['id'])? $userInfo['id'] : 0;
  184. if(empty($userInfo) || $userId<=0){
  185. $this->error = 4004;
  186. return false;
  187. }
  188. if(!in_array($coinType,[1,2,3])){
  189. $this->error = 4003;
  190. return false;
  191. }
  192. // 账户余额
  193. $userAmount = isset($userInfo[$field])? $userInfo[$field] : 0;
  194. if($userAmount < $amount){
  195. $this->error = lang(2035,['name'=>$coinName]);
  196. return false;
  197. }
  198. DB::beginTransaction();
  199. $amount = $type == 1? $amount : -$amount;
  200. $updateData = [$field=>DB::raw("{$field} + {$amount}"),'update_time'=>time()];
  201. if(!MemberModel::where(['id'=> $accountId])->update($updateData)){
  202. DB::rollBack();
  203. $this->error = 1003;
  204. return false;
  205. }
  206. // 账户明细
  207. $orderNo = get_order_num('PC');
  208. $log = [
  209. 'user_id' => $accountId,
  210. 'source_uid' => 0,
  211. 'order_no' => $orderNo,
  212. 'type' => $type==1?5:6,
  213. 'coin_type' => $coinType,
  214. 'user_type'=> 1,
  215. 'money' => $amount,
  216. 'before_money' => isset($userInfo[$field])? $userInfo[$field] : 0,
  217. 'create_time' => time(),
  218. 'adnin_id' => $adminId,
  219. 'action_ip' => get_client_ip(),
  220. 'remark' => $type==1?"链上充值":"链上扣除",
  221. 'status' => 1,
  222. 'mark' => 1,
  223. ];
  224. if(!AccountLogModel::insertGetId($log)){
  225. DB::rollBack();
  226. $this->error = 2029;
  227. return false;
  228. }
  229. DB::commit();
  230. ActionLogModel::setTitle("{$coinName}账户上分/下分");
  231. ActionLogModel::record();
  232. $this->error = 1002;
  233. return true;
  234. }
  235. }