AccountLogService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. }
  51. }
  52. return [
  53. 'pageSize' => $pageSize,
  54. 'total' => isset($list['total']) ? $list['total'] : 0,
  55. 'list' => isset($list['data']) ? $list['data'] : []
  56. ];
  57. }
  58. /**
  59. * 查询构造
  60. * @param $params
  61. * @return \Illuminate\Database\Eloquent\Builder
  62. */
  63. public function getQuery($params)
  64. {
  65. $where = ['a.mark' => 1];
  66. return $this->model->with(['member'])
  67. ->from('account_log as a')
  68. ->leftJoin('member as b', function($join) {
  69. $join->on('b.id','=', 'a.user_id')->where('a.user_type',1);
  70. })
  71. ->where($where)
  72. ->where(function ($query) use ($params) {
  73. $kw = isset($params['keyword']) ? trim($params['keyword']) : '';
  74. if ($kw) {
  75. $query->where('b.nickname', 'like', "%{$params['keyword']}%")
  76. ->orWhere('c.id', '=', "{$params['keyword']}");
  77. }
  78. })
  79. ->where(function ($query) use($params){
  80. // 日期
  81. $date = isset($params['date']) ? $params['date'] : [];
  82. $start = isset($date[0])? $date[0] : '';
  83. $end = isset($date[1])? $date[1] : '';
  84. $end = $start>=$end? '' : $end;
  85. if ($start) {
  86. $query->where('a.create_time','>=', strtotime($start));
  87. }
  88. if($end){
  89. $query->where('a.create_time','<=', strtotime($end));
  90. }
  91. $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
  92. if($orderNo){
  93. $query->where('a.order_no', 'like', "%{$orderNo}%");
  94. }
  95. $walletUrl = isset($params['wallet_url']) ? trim($params['wallet_url']) : '';
  96. if ($walletUrl) {
  97. $query->where('b.wallet_url', $walletUrl);
  98. }
  99. $userType = isset($params['user_type'])? $params['user_type'] : 0;
  100. if ($userType) {
  101. $query->where('a.user_type', $userType);
  102. }
  103. $type = isset($params['type'])? $params['type'] : 0;
  104. if (is_array($type)) {
  105. $query->whereIn('a.type', $type);
  106. } else if($type){
  107. $query->where('a.type', $type);
  108. }
  109. $coinType = isset($params['coin_type'])? $params['coin_type'] : 0;
  110. if (is_array($coinType)) {
  111. $query->whereIn('a.coin_type', $coinType);
  112. } else if($coinType){
  113. $query->where('a.coin_type', $coinType);
  114. }
  115. $status = isset($params['status'])? $params['status'] : 0;
  116. if (is_array($status)) {
  117. $query->whereIn('a.status', $status);
  118. } else if($status){
  119. $query->where('a.status', $status);
  120. }
  121. });
  122. }
  123. /**
  124. * 统计
  125. * @param $params
  126. * @return array
  127. */
  128. public function count($params)
  129. {
  130. $query = $this->getQuery($params);
  131. $count = $query->count('a.id');
  132. $total = $query->sum('a.money');
  133. $query1 = clone $query;
  134. $query2 = clone $query;
  135. $total1 = $query1->where('a.money','>',0)->sum('a.money');
  136. $total2 = $query2->where('a.money','<=',0)->sum('a.money');
  137. return [
  138. 'count' => $count,
  139. 'total1' => round($total1,2), // 进
  140. 'total2' => round($total2,2), // 出
  141. 'total' => round($total,2)
  142. ];
  143. }
  144. /**
  145. * 平台充兑
  146. * @param $adminId
  147. * @param $params
  148. * @return bool
  149. */
  150. public function changeAccount($adminId,$params)
  151. {
  152. $userType = isset($params['user_type'])? intval($params['user_type']) : 1;
  153. $coinType = isset($params['coin_type'])? intval($params['coin_type']) : 1;
  154. $type = isset($params['type'])? intval($params['type']) : 1;
  155. $accountId = isset($params['user_id'])? intval($params['user_id']) : 0;
  156. $amount = isset($params['amount'])? floatval($params['amount']) : 0;
  157. if($amount<=0){
  158. $this->error = 4100;
  159. return false;
  160. }
  161. if(!in_array($type,[1,2])){
  162. $this->error = 4003;
  163. return false;
  164. }
  165. $userTypes = [1=>'会员',2=>'商家',3=>'承兑商'];
  166. $fields = [1=>'usdt',2=>'balance',3=>'power_num',4=>'score',5=>'wait_score',6=>'quota'];
  167. $coinNames = [1=>'USDT余额',2=>'星豆余额',3=>'算力',4=>'积分',5=>'待返积分',6=>'交易额度'];
  168. $field = isset($fields[$coinType])? $fields[$coinType] : 'usdt';
  169. $coinName = isset($coinNames[$coinType])? $coinNames[$coinType] : 'USDT 余额';
  170. $accountName = isset($userTypes[$userType])? $userTypes[$userType] : '会员';
  171. ActionLogModel::setTitle("{$accountName}[ID:{$accountId}]{$coinName}账户调整");
  172. ActionLogModel::record();
  173. $amount = $type == 1? $amount : -$amount;
  174. if($userType == 3){
  175. $userInfo = AcceptorModel::where(['id'=> $accountId,'mark'=>1])->first();
  176. $userId = isset($userInfo['user_id'])? $userInfo['user_id'] : 0;
  177. if(empty($userInfo) || $userId<=0){
  178. $this->error = 4004;
  179. return false;
  180. }
  181. if(!in_array($coinType,[1,6])){
  182. $this->error = 4003;
  183. return false;
  184. }
  185. DB::beginTransaction();
  186. $updateData = [$field=>DB::raw("{$field} + {$amount}"),'update_time'=>time()];
  187. if(!AcceptorModel::where(['id'=> $accountId])->update($updateData)){
  188. DB::rollBack();
  189. $this->error = 1003;
  190. return false;
  191. }
  192. // 平台明细
  193. $orderNo = get_order_num('PS');
  194. $log = [
  195. 'user_id' => $accountId,
  196. 'source_id' => $adminId,
  197. 'source_order_no' => $orderNo,
  198. 'type' => 8,
  199. 'coin_type' => $coinType,
  200. 'user_type'=> $userType,
  201. 'money' => $amount,
  202. 'actual_money' => $amount,
  203. 'balance' => isset($userInfo[$field])? $userInfo[$field] : 0,
  204. 'create_time' => time(),
  205. 'update_time' => time(),
  206. 'remark' => $type==1?"平台上分":"平台下分",
  207. 'status' => 1,
  208. 'mark' => 1,
  209. ];
  210. if(!AccountLogModel::insertGetId($log)){
  211. DB::rollBack();
  212. $this->error = 2029;
  213. return false;
  214. }
  215. DB::commit();
  216. $dateTime = date('Y-m-d H:i:s');
  217. $actionName = $type==1?"平台上分":"平台下分";
  218. MessageService::make()->pushMessage($userId,$type==1?"平台上分成功通知":"平台下分成功通知","您在{$dateTime}(UTC+8)收到{$actionName}[{$amount}]{$coinName}账户调整成功,请及时查看对应账户进行查收",3);
  219. $this->error = 1002;
  220. return true;
  221. }else{
  222. $userInfo = MemberModel::where(['id'=> $accountId,'mark'=>1])->first();
  223. $userId = isset($userInfo['id'])? $userInfo['id'] : 0;
  224. if(empty($userInfo) || $userId<=0){
  225. $this->error = 4004;
  226. return false;
  227. }
  228. if(!in_array($coinType,[1,2,3,4])){
  229. $this->error = 4003;
  230. return false;
  231. }
  232. DB::beginTransaction();
  233. $updateData = [$field=>DB::raw("{$field} + {$amount}"),'update_time'=>time()];
  234. if(!MemberModel::where(['id'=> $accountId])->update($updateData)){
  235. DB::rollBack();
  236. $this->error = 1003;
  237. return false;
  238. }
  239. // 平台明细
  240. $orderNo = get_order_num('PS');
  241. $log = [
  242. 'user_id' => $accountId,
  243. 'source_id' => $adminId,
  244. 'source_order_no' => $orderNo,
  245. 'type' => 8,
  246. 'coin_type' => $coinType,
  247. 'user_type'=> $userType,
  248. 'money' => $amount,
  249. 'actual_money' => $amount,
  250. 'balance' => isset($userInfo[$field])? $userInfo[$field] : 0,
  251. 'create_time' => time(),
  252. 'update_time' => time(),
  253. 'remark' => $type==1?"平台上分":"平台下分",
  254. 'status' => 1,
  255. 'mark' => 1,
  256. ];
  257. if(!AccountLogModel::insertGetId($log)){
  258. DB::rollBack();
  259. $this->error = 2029;
  260. return false;
  261. }
  262. DB::commit();
  263. $dateTime = date('Y-m-d H:i:s');
  264. $actionName = $type==1?"平台上分":"平台下分";
  265. MessageService::make()->pushMessage($userId,$type==1?"平台上分成功通知":"平台下分成功通知","您在{$dateTime}(UTC+8)收到{$actionName}[{$amount}]{$coinName}账户调整成功,请及时查看对应账户进行查收",3);
  266. $this->error = 1002;
  267. return true;
  268. }
  269. $this->error = 1003;
  270. return false;
  271. }
  272. }