AccountService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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\Api;
  12. use App\Models\AccountLogModel;
  13. use App\Models\PayMealsModel;
  14. use App\Models\PayOrdersModel;
  15. use App\Services\BaseService;
  16. use App\Services\PaymentService;
  17. use App\Services\RedisService;
  18. use Illuminate\Support\Facades\DB;
  19. use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions\F;
  20. /**
  21. * 交易管理-服务类
  22. * @author laravel开发员
  23. * @since 2020/11/11
  24. * Class AccountService
  25. */
  26. class AccountService extends BaseService
  27. {
  28. public static $instance = null;
  29. /**
  30. * 构造函数
  31. * @author laravel开发员
  32. * @since 2020/11/11
  33. * AccountService constructor.
  34. */
  35. public function __construct()
  36. {
  37. $this->model = new AccountLogModel();
  38. }
  39. /**
  40. * 静态入口
  41. * @return static|null
  42. */
  43. public static function make()
  44. {
  45. if (!self::$instance) {
  46. self::$instance = (new static());
  47. }
  48. return self::$instance;
  49. }
  50. /**
  51. * @param $params
  52. * @param int $pageSize
  53. * @return array
  54. */
  55. public function getDataList($params, $pageSize = 15)
  56. {
  57. $query = $this->getQuery($params);
  58. $list = $query->select(['a.*'])
  59. ->orderBy('a.create_time','desc')
  60. ->orderBy('a.id','desc')
  61. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  62. $list = $list? $list->toArray() :[];
  63. if($list){
  64. $accountTypes = config('payment.accountTypes');
  65. foreach($list['data'] as &$item){
  66. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  67. $item['time_text'] = $item['create_time']? dateFormat($item['create_time'],'Y年m月d日') : '';
  68. $type = isset($item['type'])? intval($item['type']) : 0;
  69. $item['type_text'] = isset($item['remark'])? trim($item['remark']) : '';
  70. if(empty($item['type_text'])){
  71. $item['type_text'] = isset($accountTypes[$type])? $accountTypes[$type] : '收支明细';
  72. }
  73. $item['change_type'] = 1;
  74. if(in_array($type,[1,2,4])){
  75. $item['change_type'] = 2;
  76. }
  77. }
  78. }
  79. return [
  80. 'pageSize'=> $pageSize,
  81. 'total'=>isset($list['total'])? $list['total'] : 0,
  82. 'list'=> isset($list['data'])? $list['data'] : []
  83. ];
  84. }
  85. public function getQuery($params)
  86. {
  87. $where = ['a.mark' => 1];
  88. $type = isset($params['type'])? $params['type'] : 0;
  89. if($type>0){
  90. $where['a.type'] = $type;
  91. }
  92. return $this->model->with(['member'])->from("account_logs as a")
  93. ->leftJoin('member as b','b.id','=','a.user_id')
  94. ->where($where)
  95. ->where(function ($query) use($params) {
  96. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  97. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  98. if($userId){
  99. $query->where('a.user_id',$userId);
  100. }
  101. $status = isset($params['status'])? $params['status'] : 0;
  102. if($status){
  103. $query->where('a.status',$status);
  104. }else{
  105. $query->whereNotIn('a.status',[2]);
  106. }
  107. if ($keyword) {
  108. $query->where(function($query) use($keyword){
  109. $query->where('b.nickname','like',"%{$keyword}%")
  110. ->orWhere('b.mobile','like',"%{$keyword}%")
  111. ->orWhere('b.realname','like',"%{$keyword}%");
  112. });
  113. }
  114. $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
  115. if($orderNo){
  116. $query->where(function($query) use($orderNo){
  117. $query->where('a.source_order_no','like',"%{$orderNo}%");
  118. });
  119. }
  120. })
  121. ->where(function ($query) use($params){
  122. // 日期
  123. $date = isset($params['date']) ? $params['date'] : [];
  124. $start = isset($date[0])? $date[0] : '';
  125. $end = isset($date[1])? $date[1] : '';
  126. $end = $start>=$end? '' : $end;
  127. if ($start) {
  128. $query->where('a.create_time','>=', strtotime($start));
  129. }
  130. if($end){
  131. $query->where('a.create_time','<=', strtotime($end));
  132. }
  133. });
  134. }
  135. /**
  136. * 今日金额统计数据
  137. * @param $userId
  138. * @param int $type
  139. * @return array|mixed
  140. */
  141. public function getTotalByDay($userId, $type=1)
  142. {
  143. $cacheKey = "caches:accounts:total_{$userId}_{$type}";
  144. $data = RedisService::get($cacheKey);
  145. if($data){
  146. return $data;
  147. }
  148. $data = $this->model->where(['user_id'=> $userId,'type'=>$type,'status'=>1,'mark'=>1])
  149. ->where('create_time','>=', strtotime(date('Y-m-d')))
  150. ->sum('money');
  151. if($data){
  152. RedisService::set($cacheKey, $data, rand(5,10));
  153. }
  154. return $data;
  155. }
  156. /**
  157. * 充值套餐
  158. * @param $type
  159. * @return array|mixed
  160. */
  161. public function getPayMealList($type)
  162. {
  163. $cacheKey = "caches:accounts:mealList_{$type}";
  164. $datas = RedisService::get($cacheKey);
  165. if($datas){
  166. return $datas;
  167. }
  168. $datas = PayMealsModel::where(['type'=>$type,'status'=>1,'mark'=>1])
  169. ->orderBy('sort','desc')
  170. ->orderBy('id','asc')
  171. ->get();
  172. $datas = $datas? $datas->toAray() :[];
  173. if($datas){
  174. RedisService::set($cacheKey, $datas, rand(300,600));
  175. }
  176. return $datas;
  177. }
  178. /**
  179. * 生活充值
  180. * @param $userId 用户ID
  181. * @param $params
  182. * @return array|false
  183. */
  184. public function recharge($userId, $params)
  185. {
  186. $mealId = isset($params['id']) ? $params['id'] : 0; // 套餐ID
  187. $account = isset($params['account']) ? $params['account'] : ''; // 充值账号/手机号
  188. $cacheKey = "caches:members:pay:{$userId}_{$mealId}";
  189. if (RedisService::get($cacheKey . '_lock')) {
  190. $this->error = '请不要频繁提交~';
  191. return false;
  192. }
  193. if(empty($account)){
  194. $this->error = '请输入充值账号';
  195. return false;
  196. }
  197. RedisService::set($cacheKey, ['date' => date('Y-m-d H:i:s')], rand(2, 3));
  198. $mealInfo = PayMealsModel::where(['id' => $mealId, 'status' => 1, 'mark' => 1])
  199. ->select(['id','product_id', 'money', 'type', 'discount', 'remark', 'status'])
  200. ->first();
  201. $money = isset($mealInfo['money']) ? floatval($mealInfo['money']) : 0;
  202. $discount = isset($mealInfo['discount']) ? intval($mealInfo['discount']) : 0;
  203. $mealType = isset($mealInfo['type']) && $mealInfo['type'] ? intval($mealInfo['type']) : 1;
  204. $productId= isset($mealInfo['product_id']) ? $mealInfo['product_id'] : 0;
  205. $remark= isset($mealInfo['remark']) ? $mealInfo['remark'] : '';
  206. $price = $discount?round($money*$discount/100,2): $money;
  207. if (empty($mealInfo)) {
  208. $this->error = '该套餐不存在';
  209. RedisService::clear($cacheKey . '_lock');
  210. return false;
  211. }
  212. if ($price <= 0 || $money <= 0 || $productId<=0) {
  213. $this->error = '该套餐参数错误';
  214. RedisService::clear($cacheKey . '_lock');
  215. return false;
  216. }
  217. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  218. ->select(['id', 'openid', 'mobile', 'status'])
  219. ->first();
  220. $openid = isset($info['openid']) ? $info['openid'] : '';
  221. if (!$info || $info['status'] != 1) {
  222. $this->error = 1045;
  223. RedisService::clear($cacheKey . '_lock');
  224. return false;
  225. }
  226. if (empty($openid)) {
  227. $this->error = '用户参数错误,请重新授权登录后尝试~';
  228. RedisService::clear($cacheKey . '_lock');
  229. return false;
  230. }
  231. // 创建订单
  232. $orderNo = get_order_num('PR');
  233. $types = ['','话费充值','电费充值','燃气充值'];
  234. $remark = isset($types[$mealType])? $types[$mealType] : '生活充值';
  235. $order = [
  236. 'order_no' => $orderNo,
  237. 'user_id' => $userId,
  238. 'meal_id' => $mealId,
  239. 'product_id' => $productId,
  240. 'total' => $price,
  241. 'type' => $mealType,
  242. 'pay_total' => $price,
  243. 'account' => $account,
  244. 'discount' => $discount,
  245. 'create_time' => time(),
  246. 'remark' => $remark,
  247. 'status' => 1,
  248. 'mark' => 1
  249. ];
  250. DB::beginTransaction();
  251. if (!$orderId = PayOrdersModel::insertGetId($order)) {
  252. $this->error = '创建充值订单失败';
  253. return false;
  254. }
  255. /* TODO 支付处理 */
  256. $payOrder = [
  257. 'type' => 1,
  258. 'order_no' => $orderNo,
  259. 'pay_money' => $price,
  260. 'body' => $remark,
  261. 'openid' => $openid
  262. ];
  263. // 调起支付
  264. $payment = PaymentService::make()->minPay($info, $payOrder, 'pay');
  265. if (empty($payment)) {
  266. DB::rollBack();
  267. RedisService::clear($cacheKey . '_lock');
  268. $this->error = PaymentService::make()->getError();
  269. return false;
  270. }
  271. // 用户操作记录
  272. DB::commit();
  273. $this->error = '创建充值订单成功,请前往支付~';
  274. RedisService::clear($cacheKey . '_lock');
  275. return [
  276. 'order_id' => $orderId,
  277. 'payment' => $payment,
  278. 'total' => $payOrder['pay_money'],
  279. 'pay_type' => 10,
  280. ];
  281. }
  282. }