Example #1
0
 public static function Re_invest()
 {
     //遍历thirdproduct表获取到昨天到期的thirdproduct项目的id
     $max_time = strtotime(date('Y-m-d'));
     $min_time = strtotime(date('Y-m-d', strtotime('-1 day')));
     $thirdproduct = (new \yii\db\Query())->select(['id'])->from('fund_thirdproduct')->andWhere(['>=', 'end_at', $min_time])->andWhere(['<', 'end_at', $max_time])->all();
     $thirdorderArr = array();
     //根据thirdproduct_id在thirdorder表中获取到order_id
     foreach ($thirdproduct as $key => $value) {
         $thirdorder = (new \yii\db\Query())->groupBy('order_id')->select(['order_id'])->from('fund_thirdorder')->where(['thirdproduct_id' => $value['id']])->all();
         foreach ($thirdorder as $k => $v) {
             $thirdorderArr[$key . 'O' . $k]['order_id'] = $v['order_id'];
         }
     }
     $thirdorderArr = self::a_array_unique($thirdorderArr);
     //根据order_id在order表中获取到member_id,money,
     foreach ($thirdorderArr as $k => $v) {
         $order = (new \yii\db\Query())->select(['id', 'member_id', 'money'])->from('fund_orders')->where(['id' => $v['order_id']])->one();
         $order_id = $order['id'];
         $member_id = $order['member_id'];
         $money = $order['money'];
         //判断已购买且未被使用且未过期的债权项目中的可投资额度是否大于订单金额,若小于则不进行在投资,若大于则进行再投资
         $amount = Thirdproduct::find()->andWhere(['process_status' => 0, 'status' => Thirdproduct::STATUS_ACTIVE, 'intent' => Thirdproduct::INTENT_CHECK])->andWhere(['>=', 'end_at', time()])->asArray()->sum('amount');
         $invest_sum = Thirdproduct::find()->andWhere(['process_status' => 0, 'status' => Thirdproduct::STATUS_ACTIVE, 'intent' => Thirdproduct::INTENT_CHECK])->andWhere(['>=', 'end_at', time()])->asArray()->sum('invest_sum');
         $cha = $amount - $invest_sum;
         if ($cha > $money) {
             //对thirdorder中order_id等于id的订单进行软删除处理
             //Thirdorder::deleteAll(['order_id' => $order_id, 'member_id' => $member_id]);
             Thirdorder::updateAll(['status' => \common\models\base\fund\Thirdorder::STATUS_DELETED], ['order_id' => $order_id, 'member_id' => $member_id]);
             //根据member_id,money,以及order表的id重新生成thirdorder订单
             //获取用户再次投资生成的债权字典
             $thirdArr = self::redoc($money);
             //按照生成的债权字典,将钱分配给债权表
             $setthird = self::reset_Third($thirdArr, $member_id, $order_id);
             if (!$setthird) {
                 throw new ErrorException('资金分配到债权失败', 6002);
             }
         }
     }
 }