Commit 3e5d4e09 authored by 杨培's avatar 杨培

结算计划任务开发中

parent b8eb654f
......@@ -147,4 +147,22 @@ class Income extends \yii\db\ActiveRecord
->sum('o.credit');
}
/**
* 获取按用户的需要结算数据列表
*
* @return array
*/
public static function fetchSettleData()
{
$todayStart = strtotime('Today');
$query = Income::find()->alias('i')
->select('i.user_id,SUM(o.credit) AS amount, MIN(i.id) AS income_id_min, MAX(i.id) AS income_id_max')->leftJoin(Orders::tableName().' o', 'i.orders_id=o.id AND o.status=1')
->leftJoin(SettleAccountWeixin::tableName().' w', 'w.user_id=i.user_id')
->where('i.status=0 AND i.updated_at<:start AND w.user_id IS NOT NULL', [':start'=>$todayStart])
->groupBy('i.user_id')->having('amount>0');
return $query->asArray()->all();
}
}
<?php
namespace common\models;
use yii\behaviors\AttributeBehavior;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
/**
* This is the model class for table "settle_orders".
......@@ -17,8 +20,16 @@ namespace common\models;
* @property integer $created_at
* @property integer $updated_at
*/
class SettleOrders extends \yii\db\ActiveRecord
class SettleOrders extends ActiveRecord
{
const TYPE_WEIXIN = 'weixin';
const TYPE_ALIPAY = 'alipay';
const STATUS_UNPAID = 0;
const STATUS_PAIED = 1;
const STATUS_FAILED = 2;
const STATUS_ONPAY = 3;
/**
* @inheritdoc
*/
......@@ -26,6 +37,10 @@ class SettleOrders extends \yii\db\ActiveRecord
{
return 'settle_orders';
}
public function formName() {
return '';
}
/**
* @inheritdoc
......@@ -33,12 +48,15 @@ class SettleOrders extends \yii\db\ActiveRecord
public function rules()
{
return [
[['user_id', 'type', 'out_trade_no', 'trade_no', 'amount', 'income_id_min', 'income_id_max', 'created_at', 'updated_at'], 'required'],
[['user_id', 'amount', 'income_id_min', 'income_id_max'], 'required'],
[['user_id', 'income_id_min', 'income_id_max', 'status', 'created_at', 'updated_at'], 'integer'],
[['type'], 'string'],
[['type', 'out_trade_no', 'trade_no'], 'string'],
[['type'], 'default', 'value'=>self::TYPE_WEIXIN],
[['amount'], 'number'],
[['out_trade_no', 'trade_no'], 'string', 'max' => 50],
[['out_trade_no'], 'unique'],
[['trade_no'], 'default', 'value'=>''],
[['status'], 'default', 'value'=>self::STATUS_UNPAID]
];
}
......@@ -61,4 +79,74 @@ class SettleOrders extends \yii\db\ActiveRecord
'updated_at' => 'Updated At',
];
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
]
],
[
'class' => AttributeBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'out_trade_no'
],
'value' => function ($event) {
/* @var $event \yii\base\ModelEvent */
return $this->getOutTradeNo();
}
]
];
}
/**
* 生成或获取订单号
*
* @return string
*/
public function getOutTradeNo()
{
if (!$this->out_trade_no) {
$this->out_trade_no = self::generateOutTradeNo($this->user_id);
}
return $this->out_trade_no;
}
public static function generateOutTradeNo($userId)
{
$userSeg = sprintf('%08d', $userId);
if (strlen($userSeg) > 8) {
$userSeg = substr($userSeg, -8);
}
return date('YmdHis').$userSeg.sprintf(rand(0, 9999));
}
/**
* 找到类似的订单
*
* @param int $userId
* @param int $incomeIdMin
* @param int $incomeIdMax
* @return array|null
*/
public static function findSameOrder($userId, $incomeIdMin, $incomeIdMax)
{
return self::find()->where(['user_id'=>$userId, 'income_id_min'=>$incomeIdMin, 'income_id_max'=>$incomeIdMax])->asArray()->one();
}
public static function getAllUnpaidOrders()
{
return self::find()->where(['status'=>self::STATUS_UNPAID])->asArray()->all();
}
}
......@@ -16,7 +16,7 @@ return [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
'levels' => ['error', 'warning', 'info'],
],
],
],
......
<?php
/**
* Created by PhpStorm.
* User: pader
* Date: 2016/11/1
* Time: 16:06
*/
namespace console\controllers;
use common\libs\Utils;
use common\models\Income;
use common\models\Orders;
use common\models\SettleAccountWeixin;
use common\models\SettleOrders;
use yii\console\Controller;
/**
* 提现与结算
* @package console\controllers
*/
class WithdrawController extends Controller
{
public function actionSettle()
{
$this->actionCreateSettleOrders();
$this->actionPaySettleOrders();
}
/**
* 结算用户资金到绑定的微信账户
*/
public function actionCreateSettleOrders()
{
//$todayStart = strtotime('Today');
$incomeData = Income::fetchSettleData();
$count = 0;
foreach ($incomeData as $row) {
$order = new SettleOrders();
$order->load($row);
//是否有相同订单
$sameOrder = SettleOrders::findSameOrder($order->user_id, $order->income_id_min, $order->income_id_max);
if ($sameOrder) {
\Yii::warning("Create settle order canceled because found same order: {id={$sameOrder['id']}, out_trade_no={$sameOrder['out_trade_no']}}.");
continue;
}
if (!$order->save()) {
\Yii::error('Create settle order failed:' . Utils::fetchErrorFirst($order->errors));
continue;
}
++$count;
}
$info = "Create settle orders finish, count=$count.";
\Yii::info($info);
echo $info."\n";
}
public function actionPaySettleOrders()
{
$unpaidOrders = SettleOrders::getAllUnpaidOrders();
print_r($unpaidOrders);
}
}
\ No newline at end of file
......@@ -15,16 +15,19 @@ class m160927_122057_income_init extends Migration
}
$this->createTable($this->table, [
'id' => $this->primaryKey()->unsigned(),
'user_id' => $this->integer()->notNull()->unsigned(),
'orders_id' => $this->integer()->notNull()->unsigned(),
'status' => $this->smallInteger()->notNull()->defaultValue(0)->comment('0 未结算 1 结算 '),
'created_at' => $this->integer()->notNull(),
'updated_at' => $this->integer()->notNull(),
'id' => $this->primaryKey(10)->unsigned(),
'user_id' => $this->integer(10)->notNull()->unsigned(),
'orders_id' => $this->integer(10)->notNull()->unsigned(),
'status' => $this->smallInteger(5)->notNull()->defaultValue(0)->comment('0 未结算 1 结算 '),
'created_at' => $this->integer(10)->notNull(),
'updated_at' => $this->integer(10)->notNull(),
], $tableOptions);
$this->addCommentOnTable($this->table, '用户收入表');
$this->createIndex('index-user_id-status', $this->table, ['user_id', 'status']);
$this->createIndex('index-orders_id', $this->table, 'orders_id');
$this->createIndex('userid-status', $this->table, ['user_id', 'status']);
$this->createIndex('orders_id', $this->table, 'orders_id');
$this->createIndex('status_createdat', $this->table, ['status', 'created_at']);
}
public function down()
......
......@@ -15,20 +15,20 @@ class m160927_122231_orders_init extends Migration
}
$this->createTable($this->table, [
'id' => $this->primaryKey()->unsigned(),
'user_id' => $this->integer()->notNull()->unsigned(),
'course_id' => $this->integer()->notNull()->unsigned(),
'id' => $this->primaryKey(10)->unsigned(),
'user_id' => $this->integer(10)->notNull()->unsigned(),
'course_id' => $this->integer(10)->notNull()->unsigned(),
'number' => $this->string(32)->notNull(),
'credit' => $this->decimal(6,2)->unsigned()->notNull()->comment('消费'),
'status' => $this->smallInteger()->notNull()->defaultValue(0)->unsigned()->comment('0 未支付 1 已支付'),
'pay_order' => $this->string(64)->notNull()->comment('第三方支付订单'),
'created_at' => $this->integer()->notNull(),
'updated_at' => $this->integer()->notNull(),
'created_at' => $this->integer(10)->unsigned()->notNull(),
'updated_at' => $this->integer(10)->unsigned()->notNull(),
], $tableOptions);
$this->addCommentOnTable($this->table, '用户订单表');
$this->createIndex('index-user_id', $this->table, 'user_id');
$this->createIndex('index-user_id-status', $this->table, ['user_id', 'status']);
$this->createIndex('userid_status', $this->table, ['user_id', 'status']);
}
public function down()
......
......@@ -17,7 +17,7 @@ class m161020_033521_settle_orders_init extends Migration
'amount' => $this->decimal(8, 2)->unsigned()->comment('结算金额'),
'income_id_min' => $this->integer(10)->unsigned()->comment('结算的收记录范围最小id'),
'income_id_max' => $this->integer(10)->unsigned()->comment('结算的收记录范围最大id'),
'`status` TINYINT(1) NOT NULL DEFAULT \'0\' COMMENT \'订单状态:0 未支付,1 已支付, 2 支付失败\'',
'`status` TINYINT(1) NOT NULL DEFAULT \'0\' COMMENT \'订单状态:0 未支付,1 已支付, 2 支付失败,3支付中\'',
'created_at' => $this->integer(10)->unsigned()->notNull(),
'updated_at' => $this->integer(10)->unsigned()->notNull(),
]);
......
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 to comment