Commit c993c3ab by 朱继来

统计用户实付总额

parent 84030e52
......@@ -10,10 +10,12 @@ use App\Model\PayLogModel;
use App\Model\ErpPayLogModel;
use App\Model\RemovalModel;
use App\Model\UserMainModel;
use App\Model\UserAmountModel;
use App\Model\RedisModel;
class CronController extends Controller
{
// 统计 2019-8-5 到 2019-10-31 期间的用户总额
// 统计 2019-8-5 到 2019-10-31 期间的用户总额 定时任务下午6点 (0 18 * * * /usr/bin/curl http://order.ichunt.net/act/useramoumt)
public function userAmount()
{
// $start_time = strtotime('2019-8-5');
......@@ -22,51 +24,73 @@ class CronController extends Controller
$start_time = strtotime('2018-8-5');
$end_time = strtotime('2018-10-31 23:59:59');
$PayLogModel = new PayLogModel();
$ErpPayLogModel = new ErpPayLogModel();
$RemovalModel = new RemovalModel();
$PayLogModel = new PayLogModel();
$ErpPayLogModel = new ErpPayLogModel();
$RemovalModel = new RemovalModel();
$UserMainModel = new UserMainModel();
$UserAmountModel = new UserAmountModel();
$RedisModel = new RedisModel();
// 查询联营、自营线上支付记录
$pay_log_field = ['pay_log_id', 'order_id', 'order_sn', 'user_id', 'pay_type', 'is_paid', 'pay_amount', 'pay_time'];
$pay_log_list = $PayLogModel->where('is_paid', 1)
->whereBetween('pay_time', [$start_time, $end_time])
->select($pay_log_field)
// ->select('user_id', DB::raw('sum(pay_amount) as pay_amount'))
->orderBy('pay_time', 'desc')
// ->groupBy('user_id')
->get()
->toArray();
$pay_log_list = $PayLogModel->getPayLog($start_time, $end_time);
$pay_log = $this->filterFake($pay_log_list); // 过滤数据
$pay_log_amount = $this->countByCurrency($pay_log); // 统计金额
// 联营线下支付记录
$joint_offline_field = ['log_id', 'order_id', 'order_sn', 'user_id', 'receipt_sn', 'receipt_amount', 'currency', 'status', 'receipt_time', 'create_time'];
$joint_offline_list = $ErpPayLogModel->where('status', 1)
->whereBetween('create_time', [$start_time, $end_time])
->select($joint_offline_field)
->orderBy('create_time', 'desc')
->get()
->toArray();
$joint_offline_list = $ErpPayLogModel->getErpPayLog($start_time, $end_time);
$joint_offline = $this->filterData($joint_offline_list); // 过滤数据
$joint_offline_amount = $this->countByCurrency($joint_offline, 'receipt_amount'); // 统计金额
dd($joint_offline_amount);
// 自营账期支付记录
$self_accounts_field = ['removal_id', 'order_id', 'order_sn', 'user_id', 'checkout_paid', 'checkout_paid_time', 'checkout_paid_amount'];
$self_accounts = $RemovalModel->where('checkout_paid', 1)
->whereBetween('checkout_paid_time', [$start_time, $end_time])
->select($self_accounts_field)
->orderBy('checkout_paid_time', 'desc')
->get();
$self_accounts_list = $RemovalModel->getSelfAccounts($start_time, $end_time);
$self_accounts = $this->filterFake($self_accounts_list); // 过滤数据
$self_accounts_amount = $this->countByCurrency($self_accounts, 'checkout_paid_amount'); // 统计金额
$resp = [];
// 合并用户
$resp = $this->mergeUser($pay_log_amount, $joint_offline_amount);
$resp = $this->mergeUser($resp, $self_accounts_amount);
// 保存用户金额数据
if (!empty($resp)) {
foreach ($resp as $k=>$v) {
$data = [];
$user = $UserMainModel->getUserAccount($k); // 获取用户账号
if ($user) {
$data['account'] = isset($user['mobile']) ? $user['mobile'] : $user['email'];
}
$data['rmb_amount'] = isset($v['rmb']) ? $v['rmb'] : 0;
$data['usd_amount'] = isset($v['usd']) ? $v['usd'] : 0;
$data['cur_rate'] = $this->getRate();
$data['amount'] = $data['rmb_amount'] + $data['usd_amount'] * $data['cur_rate'];
$res = $UserAmountModel->updateOrCreate(['user_id' => $k], $data);
if ($res === false) {
echo '新增或更新失败,用户ID:'.$k; die;
}
}
}
// 获取排名前十的用户,存入缓存
$rank = $UserAmountModel->getUserAmount();
$RedisModel->set('api_lx_activity_user_rank', json_encode($rank));
return $resp;
}
// 过滤尽调数据、添加币种
public function filterFake($data)
{
if (!$data) return false;
$OrderModel = new OrderModel();
foreach ($data as $k=>&$v) {
......@@ -86,6 +110,8 @@ dd($joint_offline_amount);
// 过滤联营线下支付数据
public function filterData($data)
{
if (!$data) return false;
$PayLogModel = new PayLogModel();
foreach ($data as $k=>$v) {
......@@ -125,7 +151,7 @@ dd($joint_offline_amount);
$val['rmb'] = array_sum($val['rmb']);
}
if (isset($v['usd'])) {
if (isset($val['usd'])) {
$val['usd'] = array_sum($val['usd']);
}
}
......@@ -134,4 +160,43 @@ dd($joint_offline_amount);
return $amount;
}
// 合并用户
public function mergeUser($amount_1, $amount_2)
{
if (empty($amount_1)) return $amount_2;
if (empty($amount_2)) return $amount_1;
$keys = array_keys($amount_2); // 获取keys
foreach ($amount_1 as $k1=>&$v1) {
if (in_array($k1, $keys)) {
if (isset($v1['rmb'])) $v1['rmb'] = $v1['rmb'] + $amount_2[$k1]['rmb'];
if (isset($v1['usd'])) $v1['usd'] = $v1['usd'] + $amount_2[$k1]['usd'];
unset($amount_2[$k1]); // 删除amount_2中已合并金额的用户
}
}
if (!empty($amount_2)) {
foreach ($amount_2 as $k2=>$v2) {
$amount_1[$k2] = $v2;
}
}
return $amount_1;
}
// 获取汇率
public function getRate()
{
$url = Config('website.api_domain').'order/getrate';
$check['k1'] = time();
$check['k2'] = md5(md5($check['k1']).'fh6y5t4rr351d2c3bryi');
$temp = json_decode(curlApi($url, $check, "POST"), true);
return isset($temp['data']) ? $temp['data'] : 6.9;
}
}
\ No newline at end of file
......@@ -13,6 +13,18 @@ class ErpPayLogModel extends Model
protected $primaryKey = 'log_id';
public $timestamps = false;
// 联营线下支付记录
public function getErpPayLog($start_time, $end_time)
{
$field = ['log_id', 'order_id', 'order_sn', 'user_id', 'receipt_sn', 'receipt_amount', 'currency', 'status', 'receipt_time', 'create_time'];
$data = $this->where('status', 1)
->whereBetween('create_time', [$start_time, $end_time])
->select($field)
->orderBy('create_time', 'desc')
->get()
->toArray();
return $data;
}
}
\ No newline at end of file
......@@ -13,6 +13,20 @@ class PayLogModel extends Model
protected $primaryKey = 'pay_log_id';
public $timestamps = false;
// 获取联营、自营线上支付记录
public function getPayLog($start_time, $end_time)
{
$field = ['pay_log_id', 'order_id', 'order_sn', 'user_id', 'pay_type', 'is_paid', 'pay_amount', 'pay_time'];
$data = $this->where('is_paid', 1)
->whereBetween('pay_time', [$start_time, $end_time])
->select($field)
// ->select('user_id', DB::raw('sum(pay_amount) as pay_amount'))
->orderBy('pay_time', 'desc')
// ->groupBy('user_id')
->get()
->toArray();
return $data;
}
}
\ No newline at end of file
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;
class RedisModel extends Model
{
const WRITE_CONNECT_METHOD = ['set', 'del', 'rpush','lpush', 'expire', 'hset', 'hmset', 'hdel','hsetnx'];
private $read = [];
private $write = [];
public function __construct($ConnectWrite = 'default', $ConnectRead = 'read')
{
parent::__construct();
$this->read = Redis::connection($ConnectRead);
$this->write = Redis::connection($ConnectWrite);
}
public function __call($method, $args)
{
if (in_array($method, self::WRITE_CONNECT_METHOD)) {
return $this->write->$method(...$args);
} else {
try {
return $this->read->$method(...$args);
} catch (ConnectionException $e) {
return $this->write->$method(...$args);
}
}
}
//管道
public function pipeline_to_hset($data){
return $this->write->pipeline(function ($pipe) use ($data) {
foreach ($data['data'] as $k => $v) {
$pipe->hset($data['key'],$k,$v);
}
});
}
}
......@@ -13,5 +13,18 @@ class RemovalModel extends Model
protected $primaryKey = 'removal_id';
public $timestamps = false;
// 自营账期支付记录
public function getSelfAccounts($start_time, $end_time)
{
$field = ['removal_id', 'order_id', 'order_sn', 'user_id', 'checkout_paid', 'checkout_paid_time', 'checkout_paid_amount'];
$data = $this->where('checkout_paid', 1)
->whereBetween('checkout_paid_time', [$start_time, $end_time])
->select($field)
->orderBy('checkout_paid_time', 'desc')
->get()
->toArray();
return $data;
}
}
\ No newline at end of file
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Request;
use Excel;
use DB;
class UserAmountModel extends Model
{
protected $connection = 'order';
protected $table = 'lie_user_amount';
protected $guarded = ['amount_id'];
protected $primaryKey = 'amount_id';
public $timestamps = true;
const CREATED_AT = 'create_time';
const UPDATED_AT = 'update_time';
public function fromDateTime($value)
{
return strtotime(parent::fromDateTime($value));
}
// 获取排名前十的用户总额
public function getUserAmount()
{
return $this->select('amount_id', 'user_id', 'account', 'amount')->orderBy('amount', 'desc')->take(10)->get()->toArray();
}
}
\ No newline at end of file
......@@ -27,4 +27,10 @@ class UserMainModel extends Model
return $testId;
}
// 获取用户账号
public function getUserAccount($user_id)
{
return $this->where('user_id', $user_id)->select('user_id', 'mobile', 'email')->first();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment