/** * Auther:langxi * * 将投资的金额按照生成的债权数据字典,分散加入债权表 */ private static function set_Third($thirdArr, $member_id) { //债权表中无需写入购买人数,没有意义 foreach ($thirdArr as $vo) { //获取thirdproduct中的原始债权人id,最大债权人id,并检查其是否为公司员工,并判断资金应支付给债权人还是原始债权人, $thirdproduct = Thirdproduct::find()->select(['creditor', 'maxcreditor'])->where(['id' => $vo['id']])->asArray()->one(); if ($vo['ocmoney'] > 0) { //将钱支付给原始债权人(网站) $creditor = $thirdproduct['creditor']; $is_yuan = Catmiddle::find()->where(['cid' => '2', 'uid' => $creditor])->asArray()->one(); if (!$is_yuan) { $result = array('errorNum' => '1', 'errorMsg' => '原始债权人异常', 'data' => null); return $result; } $yuan_info = Info::findOne($creditor); $yuan_info->balance = $yuan_info['balance'] + $vo['ocmoney']; $yuan_info = $yuan_info->save(); if (!$yuan_info) { $result = array('errorNum' => '1', 'errorMsg' => '资金放入原始债权人账户失败', 'data' => null); return $result; } //写入职员账户记录表中 $clerk = new ClerkLog(); $clerk->member_id = $member_id; $clerk->clerk_id = $creditor; $clerk->behav = ClerkLog::CLERK_BEHAV_ONE; $clerk->step = $vo['ocmoney']; $clerk->remark = '原始债权人'; $clerk = $clerk->save(); if (!$clerk) { $result = array('errorNum' => '1', 'errorMsg' => '写入职员账户记录失败', 'data' => null); return $result; } } if ($vo['mcmoney'] > 0) { //将钱支付给最大债权人(网站) $maxcreditor = $thirdproduct['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'] + $vo['mcmoney']; $max_info = $max_info->save(); if (!$max_info) { $result = array('errorNum' => '1', 'errorMsg' => '资金放入最大债权人账户失败', 'data' => null); return $result; } //写入职员账户记录表中 $clerk = new ClerkLog(); $clerk->member_id = $member_id; $clerk->clerk_id = $maxcreditor; $clerk->behav = ClerkLog::CLERK_BEHAV_ONE; $clerk->step = $vo['mcmoney']; $clerk->remark = '最大债权人'; $clerk = $clerk->save(); if (!$clerk) { $result = array('errorNum' => '1', 'errorMsg' => '写入职员账户记录失败', 'data' => null); return $result; } } //按用户id和订单时间获取上面生成的订单id值 $order = Order::find()->where(['member_id' => $member_id])->orderBy('start_at desc')->asArray()->one(); $thirdorder = new Thirdorder(); $thirdorder->member_id = $member_id; $thirdorder->thirdproduct_id = $vo['id']; $thirdorder->order_id = $order['id']; $thirdorder->money = $vo['mcmoney'] + $vo['ocmoney']; $thirdorder->ocmoney = $vo['ocmoney']; $thirdorder->mcmoney = $vo['mcmoney']; $thirdorder->start_money = $vo['mcmoney'] + $vo['ocmoney']; $thirdorder->status = Thirdorder::STATUS_ACTIVE; $thirdorder->start_at = time(); $thirdorder->end_at = $order['end_at']; $thirdorder = $thirdorder->save(); if (!$thirdorder) { $result = array('errorNum' => '1', 'errorMsg' => '债权数据字典写入债权订单表失败', 'data' => null); return $result; } //将资金分配给债权表 $thirdproduct = Thirdproduct::findOne($vo['id']); $thirdproduct->ocmoney = $thirdproduct['ocmoney'] - $vo['ocmoney']; $thirdproduct->mcmoney = $thirdproduct['mcmoney'] - $vo['mcmoney']; $thirdproduct->invest_sum = $thirdproduct['invest_sum'] + $vo['ocmoney'] + $vo['mcmoney']; $thirdproduct = $thirdproduct->save(); if (!$thirdproduct) { $result = array('errorNum' => '1', 'errorMsg' => '资金写入债权表失败', 'data' => null); return $result; } } return false; }
/** * 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; }
/** * Auther:langxi * * 将再投资的金额按照生成的在投资债权数据字典,分散加入债权表 */ private static function reset_Third($thirdArr, $member_id, $order_id) { //将软删除的订单的金额,从thirdproduct表中的已投金额中减去 $thirdorder = (new \yii\db\Query())->select(['thirdproduct_id', 'money'])->from('fund_thirdorder')->where(['member_id' => $member_id, 'order_id' => $order_id])->all(); foreach ($thirdorder as $key => $val) { $thirdproduct = Thirdproduct::findOne($val['thirdproduct_id']); $thirdproduct->invest_sum = $thirdproduct['invest_sum'] - $val['money']; $thirdproduct = $thirdproduct->save(); } //债权表中无需写入购买人数,没有意义 foreach ($thirdArr as $vo) { //将债权数据字典连同订单号写入到thirdorder中 //按用户id和订单时间获取上面生成的订单id值 $order = Order::find()->where(['member_id' => $member_id, 'id' => $order_id])->asArray()->one(); $thirdorder = new Thirdorder(); $thirdorder->member_id = $member_id; $thirdorder->thirdproduct_id = $vo['id']; $thirdorder->order_id = $order['id']; $thirdorder->money = $vo['t_money']; $thirdorder->start_money = $vo['t_money']; $thirdorder->status = Thirdorder::STATUS_ACTIVE; $thirdorder->start_at = time(); $thirdorder->end_at = $order['end_at']; $thirdorder = $thirdorder->save(); if (!$thirdorder) { throw new ErrorException('债权数据字典写入债权订单表失败', 6002); } //将资金分配给债权表 $thirdproduct = Thirdproduct::findOne($vo['id']); $thirdproduct->invest_sum = $thirdproduct['invest_sum'] + $vo['t_money']; $thirdproduct = $thirdproduct->save(); if (!$thirdproduct) { throw new ErrorException('资金写入债权表失败', 6002); } } return true; }