| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace app\api\logic;
- use Exception;
- use think\Env;
- use think\Cache;
- use think\Loader ;
- use fast\Asset;
- use app\common\model\ProductOrder;
- use app\common\model\LedgerTeacChangeModel;
- use app\common\model\ProductPledges;
- //质押抵扣
- class PledgeLogic
- {
-
-
- //获取产品信息
- public static function getByProductIdList(object $list, string $lan = 'zh')
- {
- $model = Loader::model('ProductLists');
- foreach ($list as &$item) {
- $item['product_list'] = $model->whereIn('id', $item->product_id);
- if($item->type_id == ProductPledges::Combin) $item['product_list'] = $model->orderRaw('field(id,'. $item->product_id.')');
- $item['product_list'] = $model->field('id,thum,'.$lan.'_name as name')->select();
- }
- return $list;
- }
- /*
- * 获取产品信息
- */
- public static function getHoldProductList($user_id, $product_id)
- {
- $model = Loader::model('ProductOrder');
- return $model::where('user_id', $user_id)->where('product_id', $product_id)->where('status', $model::Paid)->field('id,order_no')->select();
- }
- //添加质抵押订单
- public static function setPledgeOrder(object $pledge, array $order_no, int $user_id)
- {
- $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();
-
- if(empty($product) || count($product) < $count) throw new Exception('订单不存在');
- $pledge_num = 1;
- 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);
- //修改状态
- return $model->whereIn('id', array_column($product, 'id'))->setField('status', $model::Freeze);
- }
- //获取质抵押订单列表
- //(60*60*24)*(当前时间-最后一次收取时间)
- public static function getPledgeOrderList(int $user_id)//: array
- {
- $model = Loader::model('UserPledge');
- $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)
- ->field('a.*,b.title,b.day_num')
- ->select();
- if(empty($list)) throw new Exception('暂无质押订单');
- $day = 86400;
- $total = 0; //当前累计收益
- $growth= 0; //增长累计收益
- $time = time();
- $result = [];
- foreach ($list as $item) {
- if($item->status == $model::Remove) {
- $total += $item->total_self;
- }else{
- $reta = bcdiv($item->day_num, $day, 6); //天
- $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
- $total += bcmul($reta, $inter, 6)* $item->num; //累计收益
- $growth += $reta* $item->num; //增长收益
- $result[] = $item;
- }
- }
- return ['total' => $total, 'growth' => $growth, 'list' => $result];
- }
- //解除质抵押订单
- public static function setPledgeRemove(int $pledge_id, int $user_id)
- {
- $model = Loader::model('UserPledge');
- $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)
- ->field('a.*,b.day_num')
- ->find();
- if(empty($rows)) throw new Exception('暂无质押订单');
- $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; //累计收益
- $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);
- $rows->status = $model::Remove;
- $rows->last_time = $time;
- return $rows->save();
- }
-
- //收取质抵押订单列表
- //(60*60*24)*(当前时间-最后一次收取时间)
- public static function getPledgeCollect(int $user_id)
- {
- $model = Loader::model('UserPledge');
- $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)
- ->field('a.*,b.day_num')
- ->select();
- if(empty($list)) throw new Exception('暂无质押订单');
- $day = 86400;
- $total = 0; //当前累计收益
- $time = time();
- foreach ($list as $item)
- {
- //解冻
- if($item->status == $model::Remove) {
- $total += $item->total_self;
- $item->status = $model::Close; //关闭
- }else{
- $reta = bcdiv($item->day_num, $day, 6); //天数
- $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
- $total_inco = bcmul($reta, $inter, 6) * $item->num; //累计收益
- $item->last_time = $time; //收取时间
- $item->total_self= bcadd($total_inco, $item->total_self, 2); //累计收益
- $total += $total_inco; //累计收益
- }
- //更新领取状态
- $item->save();
- }
- //更新用户资产
- return Loader::model('LedgerWalletModel')->changeWalletAccount($user_id, Asset::TEAC, $total, LedgerTeacChangeModel::Pledge, 0);
- }
- }
|