/** * Auther:langxi * 根据赎回债权字典进行赎回操作 * @param $Redeem */ private static function Set_redeem($Redeem) { foreach ($Redeem as $k => $v) { //order表金额赎回 $order = Order::findOne($v['order_id']); $order->money = $order['money'] - $v['thirdmoney']; if ($order['money'] == '0') { $order->status = Order::STATUS_DELETE; } $order = $order->save(); if (!$order) { $result = array('errorNum' => '1', 'errorMsg' => '转让债权赎回失败', 'data' => null); return $result; } //thirdorder表金额赎回 $thirdorder = Thirdorder::findOne($v['thirdorder_id']); $thirdorder->money = $thirdorder['money'] - $v['thirdmoney']; if ($thirdorder['money'] == '0') { $thirdorder->status = Thirdorder::STATUS_DELETED; } $thirdorder->mcmoney = $thirdorder['mcmoney'] + $v['thirdmoney']; //记录最大债权增加 $thirdorder->ocmoney = $thirdorder['ocmoney'] - $v['thirdmoney']; //记录原始债权金额减少 $thirdorder = $thirdorder->save(); if (!$thirdorder) { $result = array('errorNum' => '1', 'errorMsg' => '第三方转让债权失败', 'data' => null); return $result; } //thirdproduct第三方债权已投金额减少 $thirdproduct = Thirdproduct::findOne($v['thirdproduct_id']); $thirdproduct->invest_sum = $thirdproduct['invest_sum'] - $v['thirdmoney']; $thirdproduct->mcmoney = $thirdproduct['mcmoney'] + $v['thirdmoney']; //赎回导致最大债权人金额增加 $thirdproduct = $thirdproduct->save(); if (!$thirdproduct) { $result = array('errorNum' => '1', 'errorMsg' => '赎回第三方债权已投金额失败', 'data' => null); return $result; } //获取债权的最大债权人id,将用户赎回的钱从最大债权人的账户中减去 $maxcreditor = Thirdproduct::find()->select(['maxcreditor'])->where(['id' => $v['thirdproduct_id']])->asArray()->one(); $maxcreditor = $maxcreditor['maxcreditor']; $is_max = Catmiddle::find()->where(['cid' => '1', 'uid' => $maxcreditor])->asArray()->one(); if (!$is_max) { $result = array('errorNum' => '1', 'errorMsg' => '最大债权人异常', 'data' => null); return $result; } $max_info = Info::findOne($maxcreditor); $max_info->balance = $max_info['balance'] - $v['thirdmoney']; $max_info = $max_info->save(); if (!$max_info) { $result = array('errorNum' => '1', 'errorMsg' => '消减最大债权人账户金额失败', 'data' => null); return $result; } //写入职员账户记录表中 $member_id = Order::find()->select(['member_id'])->where(['id' => $v['order_id']])->asArray()->one(); $member_id = $member_id['member_id']; $clerk = new ClerkLog(); $clerk->member_id = $member_id; $clerk->clerk_id = $maxcreditor; $clerk->behav = ClerkLog::CLERK_BEHAV_TWO; $clerk->step = $v['thirdmoney']; $clerk->remark = '最大债权人'; $clerk = $clerk->save(); if (!$clerk) { throw new ErrorException('写入职员账户记录失败'); } } return true; }
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); } } } }