setName('SettleCs')->setDescription('结算占股'); } protected function execute(Input $input, Output $output) { $this->settleCs($input, $output); } // 结算占股 private function settleCs($input, $output) { $settleTime = time() - 86400; $where = []; $where['c.is_checkout'] = 0; $where['c.create_time'] = array('<=',$settleTime); $where['u.agent_parent_id'] = array('>',0); $where['u.agent'] = 1; $list = Db::name('cs')->alias('c') ->join('cg_user u','u.id=c.user_id') ->field('c.user_id,sum(c.share_amount) as share_amount, sum(c.share_maliang) as share_maliang') ->where($where) ->group('c.user_id') ->select(); foreach($list as $v){ $userId = $v['user_id']; $shareAmount = $v['share_amount']; $shareMaliang = $v['share_maliang']; $amount = $shareAmount - $shareMaliang; try{ Db::startTrans(); // 用户信息 总代不结算 $userInfo = Db::name('user')->where('id',$userId)->find(); if($userInfo['agent_parent_id'] == 0){ throw new Exception("结算占股:{$userInfo['username']} 为总代"); } if($userInfo['agent'] == 0){ throw new Exception("结算占股:{$userInfo['username']} 为会员"); } // 上级信息 $parentId = $userInfo['agent_parent_id']; $parent = Db::name('user')->where('id',$parentId)->lock(true)->find(); if(empty($parent)){ throw new Exception("结算占股:{$userInfo['username']} 上级不存在"); } // 下级余额判断 $userMoney = $userInfo['money'] + $shareAmount - $shareMaliang; if(SETTLE_MONEY_EXCEED_PARENT_MONTY != 1 && $userMoney < 0){ throw new Exception("结算占股:{$userInfo['username']} 余额不足"); } // 上级余额判断 $parentMoney = $parent['money'] - $shareAmount + $shareMaliang; if(SETTLE_MONEY_EXCEED_PARENT_MONTY != 1 && $parentMoney < 0){ throw new Exception("结算占股:{$userInfo['username']} 上级余额不足"); } // 扣除上级余额 $result = Db::name('user')->where(['id'=>$parent['id']])->update(['money'=>$parentMoney]); if(!$result){ throw new Exception("结算占股:{$userInfo['username']} 操作失败"); } // 增加下级余额 $result = Db::name('user')->where(['id'=>$userInfo['id']])->update(['money'=>$userMoney,'last_cs_time'=>time()]); if(!$result){ throw new Exception("结算占股:{$userInfo['username']} 操作失败"); } // 更新洗码表状态 $csWhere = []; $csWhere['is_checkout'] = 0; $csWhere['user_id'] = $userId; $csWhere['create_time'] = array('<=',$settleTime); $result = Db::name('cs')->where($csWhere)->update(['is_checkout'=>1,'checkout_time'=>time()]); if(!$result){ throw new Exception("结算占股:{$userInfo['username']} 操作失败"); } // 结算占股记录 $dataCs = array(); $dataCs['user_id'] = $userInfo['id']; $dataCs['username'] = $userInfo['username']; $dataCs['admin_or_agent'] = 5; $dataCs['share_amount'] = $shareAmount; $dataCs['share_maliang'] = $shareMaliang; $dataCs['agent_cs'] = $userInfo['agent_cs']; $dataCs['create_time'] = time(); $dataCs['old_money'] = $userInfo['money']; $dataCs['new_money'] = $userMoney; $dataCs['connection_old_money'] = $parent['money']; $dataCs['connection_new_money'] = $parentMoney; $dataCs['type'] = 1; $result = Db::name('cs_log')->insert($dataCs); if(!$result){ throw new Exception("结算占股:{$userInfo['username']} 操作失败"); } // 上下分金额 $scoreAmount = 0; if($shareAmount > 0){ $mode = 1; $scoreAmount = $shareAmount - $shareMaliang; }elseif($shareAmount < 0){ $mode = 2; $scoreAmount = to_number($shareAmount - $shareMaliang); } // 处理上分(下级) $dataRecharge = array(); $dataRecharge['type'] = 3; $dataRecharge['amount'] = $scoreAmount; $dataRecharge['mode'] = $mode; $dataRecharge['agent_or_admin'] = 5; $dataRecharge['controller_type'] = '定时结算系统操作'; $dataRecharge['user_id'] = $userInfo['id']; $dataRecharge['user_type'] = $userInfo['agent']; $dataRecharge['user_agent_level'] = $userInfo['agent_level']; $dataRecharge['username_for'] = $userInfo['username']; $dataRecharge['nickname_for'] = $userInfo['nickname']; $dataRecharge['user_parent_id'] = $userInfo['agent_parent_id']; $dataRecharge['create_time'] = time(); $dataRecharge['old_money'] = $userInfo['money']; $dataRecharge['new_money'] = $userMoney; $dataRecharge['controller_old_money'] = $parent['money']; $dataRecharge['controller_new_money'] = $parentMoney; $dataRecharge['controller_system'] = 1; $dataRecharge['remake'] = '结算占股,超过24小时未结占股自动结算'; $result = Db::name('recharge')->insert($dataRecharge); if(!$result){ throw new Exception("结算占股:{$userInfo['username']} 操作失败"); } // 处理上分(上级) $dataRecharge = array(); $dataRecharge['type'] = 3; $dataRecharge['amount'] = $scoreAmount; $dataRecharge['mode'] = $mode == 1 ? 2 : 1; $dataRecharge['agent_or_admin'] = 5; $dataRecharge['controller_type'] = '定时结算系统操作'; $dataRecharge['user_id'] = $parent['id']; $dataRecharge['user_type'] = $parent['agent']; $dataRecharge['user_agent_level'] = $parent['agent_level']; $dataRecharge['username_for'] = $parent['username']; $dataRecharge['nickname_for'] = $parent['nickname']; $dataRecharge['user_parent_id'] = $parent['agent_parent_id']; $dataRecharge['create_time'] = time(); $dataRecharge['old_money'] = $parent['money']; $dataRecharge['new_money'] = $parentMoney; $dataRecharge['controller_old_money'] = $parent['money']; $dataRecharge['controller_new_money'] = $parentMoney; $dataRecharge['controller_system'] = 1; $dataRecharge['remake'] = '下级结算占股,超过24小时未结占股自动结算'; $result = Db::name('recharge')->insert($dataRecharge); if(!$result){ throw new Exception("结算占股:{$userInfo['username']} 操作失败"); } Db::commit(); $output->writeln("{$userInfo['username']} 成功结算占股:{$amount}元"); }catch (Exception $e){ $output->writeln($e->getMessage().',不参与自动结算'); Db::rollback(); } } } }