Commit f725ddbe by 朱继来

Merge branch 'master' of http://119.23.72.7/zhujilai/Order into zjl_iteration_20190723

parents 6d5e60c4 c993c3ab
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use App\Model\OrderModel;
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 期间的用户总额 定时任务下午6点 (0 18 * * * /usr/bin/curl http://order.ichunt.net/act/useramoumt)
public function userAmount()
{
// $start_time = strtotime('2019-8-5');
// $end_time = strtotime('2019-10-31 23:59:59');
$start_time = strtotime('2018-8-5');
$end_time = strtotime('2018-10-31 23:59:59');
$PayLogModel = new PayLogModel();
$ErpPayLogModel = new ErpPayLogModel();
$RemovalModel = new RemovalModel();
$UserMainModel = new UserMainModel();
$UserAmountModel = new UserAmountModel();
$RedisModel = new RedisModel();
// 查询联营、自营线上支付记录
$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_list = $ErpPayLogModel->getErpPayLog($start_time, $end_time);
$joint_offline = $this->filterData($joint_offline_list); // 过滤数据
$joint_offline_amount = $this->countByCurrency($joint_offline, 'receipt_amount'); // 统计金额
// 自营账期支付记录
$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) {
$order = $OrderModel->where(['order_id'=>$v['order_id']])->select('order_id', 'user_id', 'currency', 'is_type')->first();
if ($order['is_type']) {
unset($data[$k]);
continue;
}
$v['currency'] = $order['currency'];
}
return $data;
}
// 过滤联营线下支付数据
public function filterData($data)
{
if (!$data) return false;
$PayLogModel = new PayLogModel();
foreach ($data as $k=>$v) {
$pay_log = $PayLogModel->where(['order_id'=>$v['order_id']])->select('pay_log_id')->first();
// 若pay_log表里已存在订单支付记录,则说明该订单已收款完成,需要清除
if ($pay_log) {
unset($data[$k]);
continue;
}
}
return $data;
}
// 根据币种统计用户金额
public function countByCurrency($data, $field='pay_amount')
{
if (!$data) return false;
$amount = [];
// 获取用户对应的金额
foreach ($data as $k=>$v) {
if ($v['currency'] == 1) { // RMB
$amount[$v['user_id']]['rmb'][$k] = $v[$field];
} else { // USD
$amount[$v['user_id']]['usd'][$k] = $v[$field];
}
}
if (!empty($amount)) {
// 统计用户金额
foreach ($amount as &$val)
{
if (isset($val['rmb'])) {
$val['rmb'] = array_sum($val['rmb']);
}
if (isset($val['usd'])) {
$val['usd'] = array_sum($val['usd']);
}
}
}
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
......@@ -142,5 +142,6 @@ Route::group(['middleware' => 'api'], function () {
Route::get ('/api/check/changeprice', 'SpecialController@changeOrderPrice');
Route::get ('/api/check/exportdimission', 'SpecialController@exportDimission');
Route::match (['get', 'post'], '/api/check/importdimission', 'SpecialController@importDimission');
Route::get('/act/useramoumt', 'CronController@userAmount'); // 活动统计用户实付金额
});
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Request;
use Excel;
use DB;
class ErpPayLogModel extends Model
{
protected $connection = 'order';
protected $table = 'lie_erp_pay_log';
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
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Request;
use Excel;
use DB;
class PayLogModel extends Model
{
protected $connection = 'order';
protected $table = 'lie_pay_log';
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);
}
});
}
}
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Request;
use Excel;
use DB;
class RemovalModel extends Model
{
protected $connection = 'order';
protected $table = 'lie_removal';
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