123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?php
- /**
- *
- * User: anyluck
- * Date: 2020/6/4
- * Time: 13:29
- */
- namespace app\common\controller;
- use app\common\model\BondFinance;
- use app\common\model\Finance;
- use app\common\model\Mining;
- use app\common\model\Order;
- use app\common\model\OrderFrequency;
- use app\common\model\User;
- use app\common\model\VendibilityFinance;
- use app\web\model\Msg;
- class Orde
- {
- // 订单处理 前后台通用
- /**卖出处理
- * @param $order_id 订单id
- * @param $push_user_id 购买用户
- * @param $order 交易订单
- * @param $count_order 已购买次数
- */
- public static function push_order($user, $order_id, $push_user_id, $order, $count_order, $sxf_money = 0)
- {
- // 修改订单信息
- //减去账号通证 和可售额度
- if ($count_order + 1 == 5) {
- $money = $user->money - $order->out_game;// 通行证
- } else {
- $money = $user->money - $order->tong_money;// 通行证
- }
- $push_money = $user->push_money - $order->number;// 可售额度
- User::update(["money" => $money, "push_money" => $push_money], ["id" => $push_user_id]);
- $where["id"] = $order_id;
- $update["push_user"] = $push_user_id;
- $update["sxf_money"] = $money;//卖出已交手续费
- $update["status"] = 1;
- $update["stat_time"] = time() + 1 * 3600;
- Order::where($where)->update($update);
- // 添加财务记录
- self::Finance($push_user_id, "卖出BTS", $money, 1);
- // 给买家发短信或邮件
- $buy_user_id = $order->buy_user;// 买入人
- $buy_user = User::where(["id" => $buy_user_id])->field("id,phone,mail")->find();
- // 发送通知用户 短信和邮件
- $content = "其他对您卖出BTS,请登录前去查看";
- if ($buy_user->phone) {
- Msg::phone($content, $user->phone);
- } else {
- Msg::mail($content, $user->mail);
- }
- }
- // /**买入 减去对应的保证金
- // * @param $buy_user_id 用户id
- // * @param $money // 剩余多少钱
- // * @param $jmoney // 减去多少钱
- // * @param $ensure_money // 减去保证金
- // */
- // public static function buy_order($buy_user_id, $ensure_money, $jensure_money)
- // {
- // User::update(["ensure_money" => $ensure_money], ["id" => $buy_user_id]);// 更新用户金额
- // // 添加保证金记录
- // self::bond_finance($buy_user_id, "发起购买BTS交易", $jensure_money, 1);
- // }
- // 转账处理
- public static function transfer($user_id, $puser_id, $money, $jmoney, $name)
- {
- // 给转账人减钱
- User::update(["money" => $jmoney], ["id" => $user_id]);
- // 给接受人加钱
- $where["id"] = $puser_id;
- $user = User::where($where)->field("money,name,phone,mail")->find();
- $zong_money = $user->money + $money;
- User::update(["money" => $zong_money], ["id" => $puser_id]);
- self::Finance($user_id, "您给用户" . $user->name . "转账", $money, 1);// 转账人减钱
- self::Finance($puser_id, "用户" . $name . "给您转账", $money, 0);// 收账人加钱
- // 发送通知用户 短信和邮件
- $content = "用户" . $name . "给你转账" . $money . "BTS";
- if ($user->phone) {
- Msg::phone($content, $user->phone);
- } else {
- Msg::mail($content, $user->mail);
- }
- }
- // 保证金操作
- public static function bond_transfer($user, $user_id, $money, $ensure_money, $type)
- {
- if ($type == 0) {//0 充值 1 提现
- $money = $user->money - $money;// 通证
- $ensure_moneys = $user->ensure_money + $ensure_money;// 加保证金
- User::update(["money" => $money, "ensure_money" => $ensure_moneys], ["id" => $user_id]);
- // 添加财务记录
- self::Finance($user_id, "保证金充值", $money, 1);
- self::bond_finance($user_id, "保证金充值", $money, 0);
- } else {
- $money = $user->money + $money;// 通证
- $ensure_moneys = $user->ensure_money - $ensure_money;// 加保证金
- User::update(["money" => $money, "ensure_money" => $ensure_moneys], ["id" => $user_id]);
- // 添加财务记录
- self::Finance($user_id, "保证金提现", $money, 0);
- self::bond_finance($user_id, "保证金提现", $money, 1);
- }
- }
- //
- //// 后台充值
- // public static function admin_Recharge($user_id, $money)
- // {
- // $where["id"] = $user_id;
- // $user = User::where($where)->field("money,name,phone,mail")->find();
- // $zong_money = $user->money + $money;
- // User::update(["money" => $zong_money], ["id" => $user_id]);// 更新账号金额
- // self::Finance($user_id, "充值成功", $money, 0);
- // // 发送通知用户 短信和邮件
- // $content = "充值" . $money . "BTS已处理";
- // if ($user->phone) {
- // Msg::phone($content, $user->phone);
- // } else {
- // Msg::mail($content, $user->mail);
- // }
- // }
- // 订单失败处理,全局使用
- public static function order_fail($msg, $order)
- {
- // 查找交易区块
- $mining = Mining::where(["id" => $order->mining_id])->find();
- // 扣除买入人的违约金
- $ensure_money = $mining->breach_money;
- $where["id"] = $order->buy_user;
- $user = User::where($where)->find();
- $update["ensure_money"] = $user->ensure_money - $ensure_money;
- // 添加违约金记录
- self::bond_finance($order->buy_user, "交易失败扣除保证金", $ensure_money, 1);
- User::update($update, $where);
- // 给卖出人加钱
- $push_user = User::where(["id" => $order->push_user])->find();
- $push_money = $push_user->push_money + $order->number;// 加回可售额度
- // 加上违约金 加入通证 和手续费
- $add_money = $ensure_money + $order->sxf_money;// 总获得多少钱
- $money = $push_user->money + $add_money;// 账号变动
- User::update(["money" => $money, "push_money" => $push_money], ["id" => $order->push_user]);
- // 添加记录
- self::Finance($order->push_user, "交易失败退回手续费", $order->sxf_money, 0);
- self::Finance($order->push_user, "交易失败获得违约金", $ensure_money, 0);
- // 更新订单状态
- $where_order["id"] = $order->id;
- $update_order["msg"] = $msg;
- $update_order["status"] = 5;
- $update_order["end_time"] = time();
- Order::update($update_order, $where_order);
- return 1;
- }
- /** 交易成功全局使用
- * @param $order 订单数据
- */
- public static function order_success($order)
- {
- // 更新订单状态
- $update_order["status"]=3;
- $update_order["end_time"]=time();
- Order::update($update_order,["id"=>$order->id]);
- $buy_user = $order->buy_user;// 买入人
- $push_user = $order->push_user;// 卖出人
- $mining_id = $order->mining_id;// 交易区
- $mining = Mining::where(["id" => $mining_id])->find();// 交易区
- if (!$mining){
- json_result(400,"价格区已经不存在了,无法计算");
- }
- // 统计交易次数
- // todo 买入人处理
- $buy_order_frequency = OrderFrequency::where(["user_id" => $buy_user, "mining_id" => $mining_id])->find();
- if (!$buy_order_frequency) {//不存在
- $buy_count = 1;
- $buy_number = 1;
- $push_number = 0;
- $add_buy["user_id"] = $buy_user;
- $add_buy["mining_id"] = $mining_id;
- $add_buy["buy"] = 1;
- $add_buy["push"] = 0;
- OrderFrequency::create($add_buy);
- } else {
- $buy_count = $buy_order_frequency->buy + 1;
- $buy_number = $buy_count;
- $push_number = $buy_order_frequency->push;
- OrderFrequency::update(["buy" => $buy_count], ["id" => $buy_order_frequency->id]);// 更新次数
- }
- $buy_user_data = User::where(["id" => $buy_user])->find();
- $keshou_money = $mining->enlarge_quota;// 可售额度
- // 第一次交易给用户加矿池金
- if ($buy_count == 1) {
- $pool_money = $mining->mining;
- $update_buy["pool_money"] = $buy_user_data->pool_money + $pool_money;// 加矿池金
- self::VendonolotyFinance($buy_user, $mining_id, $order->id, $order->number, $mining->mining, $keshou_money, $buy_number, $push_number, 1);
- }
- $enlarge_quota = $keshou_money + $buy_user_data->push_money;//可售额度
- // 给账号加相应数据
- $update_buy["push_money"] = $enlarge_quota;
- User::update($update_buy, ["id" => $buy_user]);
- // TODO 卖出处理
- $push_order_frequency = OrderFrequency::where(["user_id" => $push_user, "mining_id" => $mining_id])->find();
- if (!$push_order_frequency) {
- $push_count = 1;
- $add_push["user_id"] = $push_user;
- $add_push["mining_id"] = $mining_id;
- $add_push["buy"] = 0;
- $add_push["push"] = 1;
- OrderFrequency::create($add_push);
- } else {
- $push_count = $push_order_frequency->push + 1;
- OrderFrequency::update(["push" => $push_count], ["id" => $push_order_frequency->id]);// 更新次数
- }
- // 减少账号的矿金
- $push_user_data = User::where(["id" => $push_user])->find();
- $kuangjine = $push_user_data->pool_money - $mining->num;
- User::update(["pool_money" => $kuangjine], ["id" => $push_user]);
- if ($push_count == 5) {// 第五次 代理结算
- $direct_push = $mining->direct_push;//直推获得
- $second_push = $mining->second_push;//次推获得
- // 查找直接上级
- if ($push_user_data->pid) {// 是否存在直接上级
- $zhitui_push_user = User::where(["id" => $push_user_data->pid])->find();
- if ($zhitui_push_user) {
- // 直推奖励
- if ($direct_push) {// 直推奖励设置不小于0
- // 查看活跃度
- $zhitui_time =strtotime($zhitui_push_user->update_time) + (24 * 3600);
- if ($zhitui_time >= time()) {// 在活跃度里面就计算奖励
- $update_zhitui["money"] = $zhitui_push_user->money + $direct_push;
- User::update($update_zhitui, ["id" => $push_user_data->pid]);
- // 添加财务记录
- self::Finance($push_user_data->pid, "获得直推奖励", $direct_push, 0,1);
- }
- }
- if ($zhitui_push_user->pid) {// 是否存在次级上级
- // 次级上级
- if ($second_push) {// 次级奖励不小于0
- // 查询次级用户
- $ciji_push_user = User::where(["id" => $zhitui_push_user->pid])->find();
- if ($ciji_push_user) {
- $ciji_time = (int)strtotime($ciji_push_user->update_time) + 24 * 3600;
- if ($ciji_time >= time()) {// 给次级上级发奖励
- $update_ciji["money"] = $ciji_push_user->money - $second_push;
- User::update($update_ciji, ["id" => $zhitui_push_user->pid]);
- // 添加财务记录
- self::Finance($zhitui_push_user->pid, "获得次推奖励", $second_push, 0,1);
- }
- }
- }
- }
- }
- }
- }
- }
- //资产明细
- public static function Finance($user_id, $name, $money, $status,$daili=0)
- {
- $add["user_id"] = $user_id;
- $add["name"] = $name;
- $add["money"] = $money;
- $add["status"] = $status;
- $add["daili"] = $daili;
- $add["add_time"] = time();
- Finance::create($add);
- }
- // 保证金处理
- public static function bond_finance($user_id, $name, $money, $status)
- {
- $add["user_id"] = $user_id;
- $add["name"] = $name;
- $add["money"] = $money;
- $add["status"] = $status;
- $add["add_time"] = time();
- BondFinance::create($add);
- }
- /**可售余额 记录
- * @param $user_id 用户id
- * @param $mining_id 区块id
- * @param $order_id 订单id
- * @param $money 获得交易区块
- * @param $pool 矿池数量
- * @param $push_money 可售额度
- * @param $buy 当前买入次数
- * @param $push 当前卖出次数
- * @param $status 0 增加 1 减少
- */
- public static function VendonolotyFinance($user_id, $mining_id, $order_id, $money, $pool, $push_money, $buy, $push, $status)
- {
- $add["user_id"] = $user_id;
- $add["mining_id"] = $mining_id;
- $add["order_id"] = $order_id;
- $add["money"] = $money;
- $add["pool"] = $pool;
- $add["push_money"] = $push_money;
- $add["buy"] = $buy;
- $add["push"] = $push;
- $add["status"] = $status;
- $add["add_time"] = time();
- VendibilityFinance::create($add);
- }
- }
|