AccountLogService.php 11 KB

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