|
|
@@ -7,6 +7,8 @@ use think\Env;
|
|
|
use think\Cache;
|
|
|
use think\Loader ;
|
|
|
use fast\Asset;
|
|
|
+use app\common\model\LedgerTokenChangeModel;
|
|
|
+use app\common\model\LedgerWalletModel;
|
|
|
use app\common\model\ProductOrder;
|
|
|
use app\common\model\LedgerTeacChangeModel;
|
|
|
use app\common\model\ProductPledges;
|
|
|
@@ -38,34 +40,30 @@ class PledgeLogic
|
|
|
}
|
|
|
|
|
|
//添加质抵押订单
|
|
|
- public static function setPledgeOrder(object $pledge, array $order_no, int $user_id)
|
|
|
+ public static function setPledgeOrder(object $pledge, array $order_no, int $user_id, int $count, string $pay_type, float $price)
|
|
|
{
|
|
|
- $model = Loader::model('ProductOrder');
|
|
|
- $count = count($order_no);
|
|
|
- $product= $model::alias('a')
|
|
|
- ->join('product_list b', 'a.product_id = b.id', 'left')
|
|
|
- ->where('a.user_id', $user_id)
|
|
|
- ->where('a.status', $model::Paid)
|
|
|
- ->whereIn('a.order_no', $order_no)->field('a.id,b.zh_name,b.thum,a.order_no')->limit($count)->select();
|
|
|
-
|
|
|
+ $product= self::getOrderProductList($user_id, $order_no, $count);
|
|
|
+
|
|
|
if(empty($product) || count($product) < $count) throw new Exception('订单不存在');
|
|
|
$pledge_num = 1;
|
|
|
- if($pledge->type_id == ProductPledges::Single){
|
|
|
- $pledge_num = $count;
|
|
|
- }
|
|
|
-
|
|
|
+ if($pledge->type_id == ProductPledges::Single) $pledge_num = $count;
|
|
|
+
|
|
|
//添加订单
|
|
|
Loader::model('UserPledge')::setPledgeData($user_id, $pledge->id, $pledge->product_id,
|
|
|
$product, //订单信息
|
|
|
$pledge->day_num, $pledge_num);
|
|
|
|
|
|
+ //扣除支付金额
|
|
|
+ $action = ($pay_type == Asset::TEAC)? LedgerTeacChangeModel::PledgeFee: LedgerTokenChangeModel::Pledge; //类型
|
|
|
+ (new LedgerWalletModel())->changeWalletAccount($user_id, $pay_type, -$price, $action, 0);
|
|
|
+
|
|
|
//修改状态
|
|
|
- return $model->whereIn('id', array_column($product, 'id'))->setField('status', $model::Freeze);
|
|
|
+ $model = Loader::model('ProductOrder');
|
|
|
+ return $model->whereIn('order_no', $order_no)->setField('status', $model::Freeze);
|
|
|
}
|
|
|
|
|
|
|
|
|
//获取质抵押订单列表
|
|
|
- //(60*60*24)*(当前时间-最后一次收取时间)
|
|
|
public static function getPledgeOrderList(int $user_id)//: array
|
|
|
{
|
|
|
$model = Loader::model('UserPledge');
|
|
|
@@ -73,7 +71,7 @@ class PledgeLogic
|
|
|
->join('product_pledge b', 'a.pledge_id = b.id', 'left')
|
|
|
->where('a.user_id', $user_id)
|
|
|
->where('a.status', '>', $model::Close)
|
|
|
- ->field('a.*,b.title,b.day_num')
|
|
|
+ ->field('a.*,b.title,b.type_id,b.day_num,b.token,b.teac,b.is_renew')
|
|
|
->select();
|
|
|
if(empty($list)) throw new Exception('暂无质押订单');
|
|
|
$day = 86400;
|
|
|
@@ -96,52 +94,69 @@ class PledgeLogic
|
|
|
}
|
|
|
|
|
|
//解除质抵押订单
|
|
|
- public static function setPledgeRemove(int $pledge_id, int $user_id)
|
|
|
+ public static function setPledgeRemove(int $pledge_id, string $order_id, int $user_id)
|
|
|
{
|
|
|
$model = Loader::model('UserPledge');
|
|
|
+ $time = time();
|
|
|
$rows = $model::alias('a')
|
|
|
->join('product_pledge b', 'a.pledge_id = b.id', 'left')
|
|
|
->where('a.user_id', $user_id)
|
|
|
- ->where('a.id', $pledge_id)->where('a.status', $model::Ongoing)
|
|
|
+ ->where('a.id', $pledge_id)
|
|
|
+ ->where('a.status', $model::Ongoing)
|
|
|
+ ->where('a.end_time', '>', $time)
|
|
|
->field('a.*,b.day_num')
|
|
|
->find();
|
|
|
if(empty($rows)) throw new Exception('暂无质押订单');
|
|
|
+
|
|
|
+ $orderId = explode(',', $order_id);
|
|
|
+ $detail = json_decode($rows->details, true);
|
|
|
+ $count = count($orderId);
|
|
|
+ $num = $rows->num;
|
|
|
+ if(count($orderId) == count($detail)) {
|
|
|
+ $rows->status = $model::Remove;
|
|
|
+ }else{
|
|
|
+
|
|
|
+ $result = [];
|
|
|
+ foreach ($detail as &$item)
|
|
|
+ if(!in_array($item['id'], $orderId)) $result[] = $item;
|
|
|
+ $num = $count;
|
|
|
+ $rows->num = count($result);
|
|
|
+ $rows->details = json_encode($result, JSON_UNESCAPED_UNICODE);
|
|
|
+ }
|
|
|
+
|
|
|
$day = 86400;
|
|
|
$total = 0; //当前累计收益
|
|
|
- $time = time();
|
|
|
$reta = bcdiv($rows->day_num, $day, 6); //天数
|
|
|
$inter = ($rows->last_time == 0) ? $time - $rows->create_time: $time - $rows->last_time; //最后收取时间
|
|
|
- $total = bcmul($reta, $inter, 6) * $rows->num; //累计收益
|
|
|
+ $total = bcmul($reta, $inter, 6) * $num; //累计收益
|
|
|
$rows->total_self= bcadd($total, $rows->total_self, 6);
|
|
|
|
|
|
//修改状态
|
|
|
- $detail =json_decode($rows->details, true);
|
|
|
- Loader::model('ProductOrder')::where('user_id', $user_id)->whereIn('id', array_column($detail, 'id'))->setField('status', ProductOrder::Paid);
|
|
|
+ Loader::model('ProductOrder')::where('user_id', $user_id)->whereIn('id', $orderId)->setField('status', ProductOrder::Paid);
|
|
|
|
|
|
- $rows->status = $model::Remove;
|
|
|
+ //更新领取状态
|
|
|
$rows->last_time = $time;
|
|
|
return $rows->save();
|
|
|
}
|
|
|
|
|
|
|
|
|
- //收取质抵押订单列表
|
|
|
- //(60*60*24)*(当前时间-最后一次收取时间)
|
|
|
+ //收取质抵押订单列表:(60*60*24)*(当前时间-最后一次收取时间)
|
|
|
public static function getPledgeCollect(int $user_id)
|
|
|
{
|
|
|
$model = Loader::model('UserPledge');
|
|
|
+ $time = time();
|
|
|
$list = $model::alias('a')
|
|
|
->join('product_pledge b', 'a.pledge_id = b.id', 'left')
|
|
|
->where('a.user_id', $user_id)
|
|
|
->where('a.status', '>', $model::Close)
|
|
|
+ ->where('a.end_time', '>', $time)
|
|
|
->field('a.*,b.day_num')
|
|
|
->select();
|
|
|
|
|
|
if(empty($list)) throw new Exception('暂无质押订单');
|
|
|
$day = 86400;
|
|
|
$total = 0; //当前累计收益
|
|
|
- $time = time();
|
|
|
- foreach ($list as $item)
|
|
|
- {
|
|
|
+ foreach ($list as $item) {
|
|
|
//解冻
|
|
|
if($item->status == $model::Remove) {
|
|
|
$total += $item->total_self;
|
|
|
@@ -161,5 +176,42 @@ class PledgeLogic
|
|
|
return Loader::model('LedgerWalletModel')->changeWalletAccount($user_id, Asset::TEAC, $total, LedgerTeacChangeModel::Pledge, 0);
|
|
|
}
|
|
|
|
|
|
+ //存储订单续费
|
|
|
+ public static function setPledgeOrderRenew(object $pledge, int $user_id, string $pay_type)
|
|
|
+ {
|
|
|
+ //扣除支付金额
|
|
|
+ $action = ($pay_type == Asset::TEAC)? LedgerTeacChangeModel::PledgeFee: LedgerTokenChangeModel::Pledge; //类型
|
|
|
+ (new LedgerWalletModel())->changeWalletAccount($user_id, $pay_type, -$pledge[$pay_type], $action, 0);
|
|
|
+
|
|
|
+ $pledge->end_time = $pledge->end_time + config('pledge_end_time');
|
|
|
+ return $pledge->save();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //添加用户存储产品
|
|
|
+ public static function setPledgeProductAdd(object $pledge, int $user_id, int $count, array $order_no)
|
|
|
+ {
|
|
|
+ $product = self::getOrderProductList($user_id, $order_no, $count);
|
|
|
+ if(empty($product) || count($product) < $count) throw new Exception('订单不存在');
|
|
|
+
|
|
|
+ //修改订单状态
|
|
|
+ Loader::model('ProductOrder')::where('user_id', $user_id)->whereIn('order_no', $order_no)->setField('status', ProductOrder::Freeze);
|
|
|
+
|
|
|
+ //添加产品
|
|
|
+ $detail = json_decode($pledge->details, true);
|
|
|
+ $pledge->details = json_encode(array_merge($detail, $product), JSON_UNESCAPED_UNICODE);
|
|
|
+ $pledge->num = $pledge->num + $count ;
|
|
|
+ return $pledge->save();
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
+ //获取订单产品
|
|
|
+ private static function getOrderProductList(int $user_id, array $order_no, int $count)
|
|
|
+ {
|
|
|
+ return Loader::model('ProductOrder')::alias('a')
|
|
|
+ ->join('product_list b', 'a.product_id = b.id', 'left')
|
|
|
+ ->where('a.user_id', $user_id)
|
|
|
+ ->where('a.status', ProductOrder::Paid)
|
|
|
+ ->whereIn('a.order_no', $order_no)->field('a.id,b.zh_name,b.thum,a.order_no')->limit($count)->select();
|
|
|
+ }
|
|
|
}
|