Files
zzp-server/app/api/logic/RobotLogic.php
2026-01-19 14:19:22 +08:00

519 lines
22 KiB
PHP

// <?php
// namespace app\api\logic;
// use app\common\logic\BaseLogic;
// use app\common\model\goods\{Goods,GoodsRecord};
// use app\common\service\{UtilsService,ConfigService,FileService};
// use app\common\model\member\UserMember;
// use app\common\model\finance\{UserFinance};
// use app\common\model\user\{User,UserRelation,UserKadan,UserGroupRule,UserGroupRecord};
// use app\common\model\lh\{LhCoin,LhRecord};
// use app\common\model\setting\Language;
// use think\facade\{Db};
// /**
// * 抢单逻辑
// * Class RobotLogic
// * @package app\api\logic
// */
// class RobotLogic extends BaseLogic
// {
// /**
// * @notes 首页数据
// * @param $params
// * @return array
// * @throws \think\db\exception\DataNotFoundException
// * @throws \think\db\exception\DbException
// * @throws \think\db\exception\ModelNotFoundException
// * @author BD
// * @date 2024/02/22 10:54
// */
// public static function getIndexData(array $params)
// {
// // 获取今天0点的时间戳
// $todayStart = strtotime(date('Y-m-d 00:00:00'));
// $user = User::where(['id' => $params['user_id']])
// ->field('id,user_money as balance,total_income as totalIncome,is_lh')
// ->findOrEmpty();
// //查询会员等级
// $member_id = UtilsService::get_user_member_id($params['user_id']);
// $userMember = UserMember::where(['id' => $member_id])
// ->field('id, name, logo, bg_img, text_color, money, level1_num, lh_min, lh_max, lh_num')
// ->findOrEmpty();
// $today_income = UserFinance::where("create_time > $todayStart")
// ->where(['user_id' => $params['user_id'],'change_type' => 13])
// ->sum('change_amount');
// $today_order = LhRecord::where("create_time > $todayStart")
// ->where(['user_id' => $params['user_id']])
// ->count();
// $user['member'] = $userMember;
// $user['todayIncome'] = $today_income;
// $user['totalIncome'] = $user['totalIncome'];
// $user['todayOrder'] = $today_order;
// $user['todayTotalOrder'] = $userMember['lh_num'];
// $today_money = LhRecord::where("create_time > $todayStart")
// ->where(['user_id' => $params['user_id']])
// ->sum('money');
// $user['todayMoney'] = $today_money;
// //今日量化过
// if($user['todayMoney'] > 0){
// //可量化总额金额 = 今日最后一次量化金额 * 可量化次数
// $today_last_money = LhRecord::where("create_time > $todayStart")
// ->where(['user_id' => $params['user_id']])
// ->order('create_time desc')
// ->findOrEmpty();
// $user['totalyMoney'] = $today_last_money['money'] * $userMember['lh_num'];
// }else{
// //查询今日量化情况,已量化、总可量化(根据会员等级最高量化金额,用户余额小于最高,则根据用户余额,用户余额小于最低,则根据最低)
// //大于最大量化金额
// if($user['balance'] >= $userMember['lh_max']){
// $user['totalyMoney'] = $userMember['lh_max'];
// //大于最小量化金额 小于最大量化金额
// }elseif($user['balance'] > $userMember['lh_min'] && $user['balance'] < $userMember['lh_max']){
// $user['totalyMoney'] = $user['balance'];
// //小于最小量化金额
// }else{
// $user['totalyMoney'] = $userMember['lh_min'];
// }
// }
// return [
// 'user' => $user,
// // 服务器时间
// 'server_date' => date('Y/m/d H:i:s'),
// ];
// }
// /**
// * @notes 量化
// * @param $params
// * @return array|false
// * @throws \think\db\exception\DataNotFoundException
// * @throws \think\db\exception\DbException
// * @throws \think\db\exception\ModelNotFoundException
// * @author BD
// * @date 2024/02/22 10:54
// */
// public static function buy(array $params)
// {
// Db::startTrans();
// try {
// // 获取今天0点的时间戳
// $todayStart = strtotime(date('Y-m-d 00:00:00'));
// $user = User::where(['id' => $params['user_id']]) -> findOrEmpty();
// $coin = LhCoin::where(['is_show' => 1]) ->orderRaw(" RAND() ") -> findOrEmpty();
// //查询会员等级
// $member_id = UtilsService::get_user_member_id($params['user_id']);
// $userMember = UserMember::where(['id' => $member_id])->findOrEmpty();
// //计算买入金额
// $money = 0;
// //查询今日量化情况,总可量化(根据会员等级最高量化金额,用户余额小于最高,则根据用户余额,用户余额小于最低,则根据最低)
// $today_money = LhRecord::where("create_time > $todayStart")
// ->where(['user_id' => $params['user_id']])
// ->sum('money');
// $today_order = LhRecord::where("create_time > $todayStart")
// ->where(['user_id' => $params['user_id']])
// ->count();
// //今日量化
// if($today_order > 0){
// //买入金额为 今日最后一次量化金额
// $today_last_money = LhRecord::where("create_time > $todayStart")
// ->where(['user_id' => $params['user_id']])
// ->order('create_time desc')
// ->findOrEmpty();
// $money = $today_last_money['money'];
// }else{
// //查询可量化总额
// $totalyMoney = 0;//总可量化金额
// //大于最大量化金额
// if($user['user_money'] >= $userMember['lh_max']){
// $totalyMoney = $userMember['lh_max'];
// //大于最小量化金额 小于最大量化金额
// }elseif($user['user_money'] > $userMember['lh_min'] && $user['user_money'] < $userMember['lh_max']){
// $totalyMoney = $user['user_money'];
// //小于最小量化金额
// }else{
// $totalyMoney = $userMember['lh_min'];
// }
// $money = $totalyMoney / $userMember['lh_num'];
// //最后一次,量化金额 = 可量化金额 - 已量化金额
// if($userMember['lh_num'] - $today_order == 1){
// $money = $totalyMoney - $today_money;
// }
// }
// if($money <= 0){
// throw new \Exception('network.parameterAbnormality');
// }
// // //如果用户余额低于最大量化金额,则根据量化金额取值,否则根据用户余额取值
// // if($userMember['lh_max'] > $user['user_money']){
// // $userMember['lh_max'] = $user['user_money'];
// // }
// // $money = mt_rand($userMember['lh_min'], $userMember['lh_max']);
// $rate = round($userMember['rate_min'] + mt_rand() / mt_getrandmax() * ($userMember['rate_max'] - $userMember['rate_min']), 2);
// //每次量化收益 = 最小-最大量化金额随机值 * 最小-最大量化收益率随机值 / 每日量化次数
// $income = round(($money * $rate / 100), 2);
// $money_sale = round($coin['price'] + ($coin['price'] * $rate ) / 100, 4);
// $data = [
// 'sn' => generate_sn(LhRecord::class, 'sn'),
// 'user_id' => $params['user_id'],
// 'coin_id' => $coin['id'],
// 'coin_name' => $coin['name'],
// 'coin_logo' => $coin['logo'],
// 'coin_symbol' => $coin['symbol'],
// 'coin_buy_name' => $coin['buy_name'],
// 'coin_sale_name' => $coin['sale_name'],
// 'money' => $money,
// 'income' => $income,
// 'money_buy' => $coin['price'],
// 'money_sale' => $money_sale
// ];
// $order = LhRecord::create($data);
// //记录日志
// UtilsService::user_finance_add(
// $data['user_id'],
// 13,
// 1,
// $data['income'],
// $data['sn'],
// '',
// 1 //冻结
// );
// //用户资金修改
// UtilsService::user_money_change($data['user_id'], 1, $data['income'],'user_money');
// UtilsService::user_money_change($data['user_id'], 1, $data['income'],'total_income');
// //团队收益奖励
// UtilsService::team_reward_add($data['user_id'],$data['income'],2);
// Db::commit();
// return [];
// } catch (\Exception $e) {
// Db::rollback();
// self::setError($e->getMessage());
// return false;
// }
// }
// /**
// * @notes 抢单
// * @param $params
// * @return array|false
// * @throws \think\db\exception\DataNotFoundException
// * @throws \think\db\exception\DbException
// * @throws \think\db\exception\ModelNotFoundException
// * @author BD
// * @date 2024/02/22 10:54
// */
// public static function grab(array $params)
// {
// Db::startTrans();
// try {
// $user = User::where(['id' => $params['user_id']]) -> findOrEmpty();
// $time24Hours = time() - 24 * 60 * 60;//24小时前
// //查看是否存在进行中的订单
// $orderStatus3 = GoodsRecord::where(['user_id' => $params['user_id'],'status' => 3])->findOrEmpty();
// if (!$orderStatus3->isEmpty()) {
// throw new \Exception('network.parameterAbnormality');//派单中,请稍后重试
// }
// $orderStatus4 = GoodsRecord::where(['user_id' => $params['user_id'],'status' => 4])->findOrEmpty();
// if (!$orderStatus4->isEmpty()) {
// throw new \Exception('network.parameterAbnormality');//订单未支付,请先完成支付
// }
// //判断24小时抢单次数
// //分组模式下,抢单次数=分组订单,未分组,抢单次数=会员等级抢单次数
// $today_order = GoodsRecord::where("create_time > $time24Hours")->where(['user_id' => $params['user_id']])->count();
// //判断用户分组
// $userGroupRecord = UserGroupRecord::where(['user_id' => $params['user_id']])
// ->order('id', 'desc')
// ->findOrEmpty();
// if(!$userGroupRecord->isEmpty()) {
// $userGroupRuleCount = UserGroupRule::where(['group_id' => $userGroupRecord['group_id']])->count();
// if ($today_order >= $userGroupRuleCount) {
// throw new \Exception('network.parameterAbnormality');//今日抢单次数已用完
// }
// }else{
// //查询会员等级
// $member_id = UtilsService::get_user_member_id($params['user_id']);
// $userMember = UserMember::where(['id' => $member_id])->findOrEmpty();
// if ($today_order >= $userMember['order']) {
// throw new \Exception('network.parameterAbnormality');//今日抢单次数已用完
// }
// }
// $language = Language::where(['symbol' => $params['lang']])->findOrEmpty();
// if ($language->isEmpty()) {
// throw new \Exception('network.parameterAbnormality');
// }
// $data = [
// 'sn' => generate_sn(GoodsRecord::class, 'sn'),
// 'user_id' => $params['user_id'],
// 'lang' => $params['lang'],
// 'status' => 5,
// ];
// //判断派单模式 1手动2自动
// $config = ConfigService::get('website', 'trade');
// //自动----------------------------------------------------------------------------------------
// if($config['robot_model'] -2 == 0){
// //查询会员等级
// $member_id = UtilsService::get_user_member_id($data['user_id']);
// $member = UserMember::where(['id' => $member_id])->findOrEmpty();
// $auto_status = false;
// $total_money = 0;
// $commission_cust = 0;//自定义佣金标识
// //判断是否有卡单订单
// $total_order = GoodsRecord::where(['user_id' => $params['user_id']])->count();
// $kadan = UserKadan::where(['user_id' => $params['user_id'],'num' => $total_order + 1,'status' => 0])->findOrEmpty();
// if (!$kadan->isEmpty()) {
// //计算匹配金额
// $total_money = $kadan['money'];
// $commission_cust = $kadan['commission'];
// UserKadan::update([
// 'id' => $kadan['id'],
// 'status' => 1,
// ]);
// $data['type'] = 3;
// $auto_status = true;
// //没有卡单订单
// }else{
// //判断用户分组
// $userGroupRecord = UserGroupRecord::where(['user_id' => $params['user_id']])
// ->order('id', 'desc')
// ->findOrEmpty();
// if(!$userGroupRecord->isEmpty()) {
// $userGroupRule = UserGroupRule::where(['group_id' => $userGroupRecord['group_id'],'num' => $today_order + 1])->findOrEmpty();
// //判断是否存在分组规则,不存在则进入手动派单
// if(!$userGroupRule->isEmpty()){
// //计算订单金额 金额类型1固定值2百分比
// if($userGroupRule['money_type'] == 1){
// $total_money = $userGroupRule['money'];
// }else{
// $total_money = round($user['user_money'] * $userGroupRule['money_percentage'] / 100,0);
// }
// //计算订单佣金 佣金类型1固定值2百分比
// if($userGroupRule['commission_type'] == 1){
// $commission_cust = $userGroupRule['commission'];
// }else{
// $commission_cust = round($total_money * $userGroupRule['commission_percentage'] / 100,0);
// }
// $data['type'] = 2;
// $auto_status = true;
// //用户分组规则金额百分比模式下,判断用户余额跟自动派单最低余额
// if(($userGroupRule['money_type'] == 2) && ($user['user_money'] - $config['robot_range_min_money'] < 0)){
// $auto_status = false;
// }
// }else{
// $auto_status = false;
// }
// }else{
// //会员自动派单订单
// if($user['user_money'] - $config['robot_range_min_money'] >= 0){
// //计算匹配金额
// $total_money = round($user['user_money'] * rand($config['robot_range'][0],$config['robot_range'][1]) / 100,0);
// $auto_status = true;
// }
// }
// }
// if($auto_status){
// $goods = Goods::where("money <= $total_money")->group(" RAND() ")->findOrEmpty();
// if (!$goods->isEmpty()) {
// //多语言替换
// $data_langs = UtilsService::get_langs_data($goods['langs'],$params['lang']);
// $goods['title'] = $data_langs['title'];
// //数量
// $num = floor($total_money / $goods['money']);
// //计算佣金
// $commission = $commission_cust > 0 ? $commission_cust : round($goods['money'] * $num * $member['commission']/100,2);
// if($commission > 0.01){
// $data['goods_id'] = $goods['id'];
// $data['goods_title'] = $goods['title'];
// $data['goods_image'] = FileService::setFileUrl($goods['image']);
// $data['unit_price'] = $goods['money'];
// $data['num'] = $num;
// $data['money'] = $goods['money'] * $num;
// $data['commission'] = $commission;
// $data['status'] = 4;
// }
// }
// }
// }
// $order = GoodsRecord::create($data);
// Db::commit();
// return [
// 'order_id' => $data['sn'],
// 'status' => 5,
// ];
// } catch (\Exception $e) {
// Db::rollback();
// self::setError($e->getMessage());
// return false;
// }
// }
// /**
// * @notes 获取进行中订单
// * @param $params
// * @return array
// * @throws \think\db\exception\DataNotFoundException
// * @throws \think\db\exception\DbException
// * @throws \think\db\exception\ModelNotFoundException
// * @author BD
// * @date 2024/02/22 10:54
// */
// public static function grabIngRecord(array $params)
// {
// try {
// $field = ['id','sn','goods_title as title','goods_image as image','unit_price','num','money','commission','status'];
// $record = GoodsRecord::field($field)
// ->where(['user_id' => $params['user_id']])
// ->where('status = 5 OR status = 4')
// ->order(['id' => 'desc'])
// ->findOrEmpty()
// ->toArray();
// return $record;
// } catch (\Exception $e) {
// self::setError($e->getMessage());
// return false;
// }
// }
// /**
// * @notes 订单支付
// * @param array $params
// * @return array|false
// * @author BD
// * @date 2024/02/22 10:54
// */
// public static function grabPay(array $params)
// {
// Db::startTrans();
// try {
// $order = GoodsRecord::where(['id' => $params['id'],'user_id' => $params['user_id']])->findOrEmpty();
// GoodsRecord::update([
// 'id' => $order['id'],
// 'status' => 1,
// 'pay_time' => time()
// ]);
// //支付扣除金额---------------------------------------------------------------------------------------------
// //记录日志
// UtilsService::user_finance_add(
// $order['user_id'],
// 7,
// 2,
// $order['money'],
// $order['sn']
// );
// //用户资金修改
// UtilsService::user_money_change($params['user_id'], 2, $order['money'],'user_money');
// $commission = $order['commission'];
// //收益增加金额---------------------------------------------------------------------------------------------
// //记录日志
// UtilsService::user_finance_add(
// $order['user_id'],
// 9,
// 1,
// $commission,
// $order['sn'],
// '',
// 1 //冻结
// );
// //用户资金修改
// UtilsService::user_money_change($params['user_id'], 1, $commission,'user_money');
// UtilsService::user_money_change($params['user_id'], 1, $commission,'total_income');
// //团队收益奖励
// UtilsService::team_reward_add($params['user_id'],$commission,2);
// //订单本金返还---------------------------------------------------------------------------------------------
// //记录日志
// UtilsService::user_finance_add(
// $order['user_id'],
// 8,
// 1,
// $order['money'],
// $order['sn']
// );
// //用户资金修改
// UtilsService::user_money_change($params['user_id'], 1, $order['money'],'user_money');
// Db::commit();
// return [
// 'order_id' => (int)$order['id'],
// 'from' => 'order'
// ];
// } catch (\Exception $e) {
// Db::rollback();
// self::setError($e->getMessage());
// return false;
// }
// }
// }