AccountLogService.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  110. if ($userId) {
  111. $query->where('a.user_id', $userId);
  112. }
  113. $userType = isset($params['user_type'])? $params['user_type'] : 0;
  114. if ($userType) {
  115. $query->where('a.user_type', $userType);
  116. }
  117. $type = isset($params['type'])? $params['type'] : 0;
  118. if (is_array($type)) {
  119. $query->whereIn('a.type', $type);
  120. } else if($type){
  121. $query->where('a.type', $type);
  122. }
  123. $coinType = isset($params['coin_type'])? $params['coin_type'] : 0;
  124. if (is_array($coinType)) {
  125. $query->whereIn('a.coin_type', $coinType);
  126. } else if($coinType){
  127. $query->where('a.coin_type', $coinType);
  128. }
  129. $status = isset($params['status'])? $params['status'] : 0;
  130. if (is_array($status)) {
  131. $query->whereIn('a.status', $status);
  132. } else if($status){
  133. $query->where('a.status', $status);
  134. }
  135. });
  136. }
  137. /**
  138. * 统计
  139. * @param $params
  140. * @return array
  141. */
  142. public function count($params)
  143. {
  144. $query = $this->getQuery($params);
  145. $count = $query->count('a.id');
  146. $query1 = clone $query;
  147. $query2 = clone $query;
  148. $total1 = $query1->where('a.money','>',0)->sum('a.money');
  149. $total2 = $query2->where('a.money','<=',0)->sum('a.money');
  150. return [
  151. 'count' => $count,
  152. 'total1' => round(abs($total1),2), // 进
  153. 'total2' => round(abs($total2),2), // 出
  154. 'total' => round(abs($total1)+abs($total2),2)
  155. ];
  156. }
  157. /**
  158. * 平台充兑
  159. * @param $adminId
  160. * @param $params
  161. * @return bool
  162. */
  163. public function changeAccount($adminId,$params)
  164. {
  165. $userType = isset($params['user_type'])? intval($params['user_type']) : 1;
  166. $coinType = isset($params['coin_type'])? intval($params['coin_type']) : 1;
  167. $type = isset($params['type'])? intval($params['type']) : 1;
  168. $accountId = isset($params['user_id'])? intval($params['user_id']) : 0;
  169. $amount = isset($params['amount'])? floatval($params['amount']) : 0;
  170. if($amount<=0){
  171. $this->error = 4100;
  172. return false;
  173. }
  174. if(!in_array($type,[1,2])){
  175. $this->error = 4003;
  176. return false;
  177. }
  178. $userTypes = [1=>'会员'];
  179. $fields = [1=>'usdt',2=>'sbt',3=>'profit'];
  180. $coinNames = [1=>'USDT余额',2=>'SBT',3=>'收益/奖励'];
  181. $field = isset($fields[$coinType])? $fields[$coinType] : 'usdt';
  182. $coinName = isset($coinNames[$coinType])? $coinNames[$coinType] : 'USDT余额';
  183. $accountName = isset($userTypes[$userType])? $userTypes[$userType] : '会员';
  184. ActionLogModel::setTitle("{$accountName}[ID:{$accountId}]{$coinName}账户上下分");
  185. ActionLogModel::record();
  186. $userInfo = MemberModel::where(['id'=> $accountId,'mark'=>1])->first();
  187. $userId = isset($userInfo['id'])? $userInfo['id'] : 0;
  188. if(empty($userInfo) || $userId<=0){
  189. $this->error = 4004;
  190. return false;
  191. }
  192. if(!in_array($coinType,[1,2,3])){
  193. $this->error = 4003;
  194. return false;
  195. }
  196. // 账户余额
  197. $userAmount = isset($userInfo[$field])? $userInfo[$field] : 0;
  198. if($type == 2 && $userAmount < $amount){
  199. $this->error = lang(2035,['name'=>$coinName]);
  200. return false;
  201. }
  202. DB::beginTransaction();
  203. $amount = $type == 1? $amount : -$amount;
  204. $updateData = [$field=>DB::raw("{$field} + {$amount}"),'update_time'=>time()];
  205. if(!MemberModel::where(['id'=> $accountId])->update($updateData)){
  206. DB::rollBack();
  207. $this->error = 1003;
  208. return false;
  209. }
  210. // 账户明细
  211. $orderNo = get_order_num('PC');
  212. $log = [
  213. 'user_id' => $accountId,
  214. 'source_uid' => 0,
  215. 'order_no' => $orderNo,
  216. 'type' => $type==1?5:6,
  217. 'coin_type' => $coinType,
  218. 'user_type'=> 1,
  219. 'money' => $amount,
  220. 'before_money' => isset($userInfo[$field])? $userInfo[$field] : 0,
  221. 'create_time' => time(),
  222. 'admin_id' => $adminId,
  223. 'action_ip' => get_client_ip(),
  224. 'remark' => $type==1?"链上充值":"链上扣除",
  225. 'status' => 1,
  226. 'mark' => 1,
  227. ];
  228. if(!AccountLogModel::insertGetId($log)){
  229. DB::rollBack();
  230. $this->error = 2029;
  231. return false;
  232. }
  233. DB::commit();
  234. ActionLogModel::setTitle("{$coinName}账户上分/下分");
  235. ActionLogModel::record();
  236. $this->error = 1002;
  237. return true;
  238. }
  239. }