AccountLogService.php 14 KB

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