Orde.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /**
  3. *
  4. * User: anyluck
  5. * Date: 2020/6/4
  6. * Time: 13:29
  7. */
  8. namespace app\common\controller;
  9. use app\common\model\BondFinance;
  10. use app\common\model\Finance;
  11. use app\common\model\Mining;
  12. use app\common\model\Order;
  13. use app\common\model\OrderFrequency;
  14. use app\common\model\User;
  15. use app\common\model\VendibilityFinance;
  16. use app\web\model\Msg;
  17. class Orde
  18. {
  19. // 订单处理 前后台通用
  20. /**卖出处理
  21. * @param $order_id 订单id
  22. * @param $push_user_id 购买用户
  23. * @param $order 交易订单
  24. * @param $count_order 已购买次数
  25. */
  26. public static function push_order($user, $order_id, $push_user_id, $order, $count_order, $sxf_money = 0)
  27. {
  28. // 修改订单信息
  29. //减去账号通证 和可售额度
  30. if ($count_order + 1 == 5) {
  31. $money = $user->money - $order->out_game;// 通行证
  32. } else {
  33. $money = $user->money - $order->tong_money;// 通行证
  34. }
  35. $push_money = $user->push_money - $order->number;// 可售额度
  36. User::update(["money" => $money, "push_money" => $push_money], ["id" => $push_user_id]);
  37. $where["id"] = $order_id;
  38. $update["push_user"] = $push_user_id;
  39. $update["sxf_money"] = $money;//卖出已交手续费
  40. $update["status"] = 1;
  41. $update["stat_time"] = time() + 1 * 3600;
  42. Order::where($where)->update($update);
  43. // 添加财务记录
  44. self::Finance($push_user_id, "卖出BTS", $money, 1);
  45. // 给买家发短信或邮件
  46. $buy_user_id = $order->buy_user;// 买入人
  47. $buy_user = User::where(["id" => $buy_user_id])->field("id,phone,mail")->find();
  48. // 发送通知用户 短信和邮件
  49. $content = "其他对您卖出BTS,请登录前去查看";
  50. if ($buy_user->phone) {
  51. Msg::phone($content, $user->phone);
  52. } else {
  53. Msg::mail($content, $user->mail);
  54. }
  55. }
  56. // /**买入 减去对应的保证金
  57. // * @param $buy_user_id 用户id
  58. // * @param $money // 剩余多少钱
  59. // * @param $jmoney // 减去多少钱
  60. // * @param $ensure_money // 减去保证金
  61. // */
  62. // public static function buy_order($buy_user_id, $ensure_money, $jensure_money)
  63. // {
  64. // User::update(["ensure_money" => $ensure_money], ["id" => $buy_user_id]);// 更新用户金额
  65. // // 添加保证金记录
  66. // self::bond_finance($buy_user_id, "发起购买BTS交易", $jensure_money, 1);
  67. // }
  68. // 转账处理
  69. public static function transfer($user_id, $puser_id, $money, $jmoney, $name)
  70. {
  71. // 给转账人减钱
  72. User::update(["money" => $jmoney], ["id" => $user_id]);
  73. // 给接受人加钱
  74. $where["id"] = $puser_id;
  75. $user = User::where($where)->field("money,name,phone,mail")->find();
  76. $zong_money = $user->money + $money;
  77. User::update(["money" => $zong_money], ["id" => $puser_id]);
  78. self::Finance($user_id, "您给用户" . $user->name . "转账", $money, 1);// 转账人减钱
  79. self::Finance($puser_id, "用户" . $name . "给您转账", $money, 0);// 收账人加钱
  80. // 发送通知用户 短信和邮件
  81. $content = "用户" . $name . "给你转账" . $money . "BTS";
  82. if ($user->phone) {
  83. Msg::phone($content, $user->phone);
  84. } else {
  85. Msg::mail($content, $user->mail);
  86. }
  87. }
  88. // 保证金操作
  89. public static function bond_transfer($user, $user_id, $money, $ensure_money, $type)
  90. {
  91. if ($type == 0) {//0 充值 1 提现
  92. $money = $user->money - $money;// 通证
  93. $ensure_moneys = $user->ensure_money + $ensure_money;// 加保证金
  94. User::update(["money" => $money, "ensure_money" => $ensure_moneys], ["id" => $user_id]);
  95. // 添加财务记录
  96. self::Finance($user_id, "保证金充值", $money, 1);
  97. self::bond_finance($user_id, "保证金充值", $money, 0);
  98. } else {
  99. $money = $user->money + $money;// 通证
  100. $ensure_moneys = $user->ensure_money - $ensure_money;// 加保证金
  101. User::update(["money" => $money, "ensure_money" => $ensure_moneys], ["id" => $user_id]);
  102. // 添加财务记录
  103. self::Finance($user_id, "保证金提现", $money, 0);
  104. self::bond_finance($user_id, "保证金提现", $money, 1);
  105. }
  106. }
  107. //
  108. //// 后台充值
  109. // public static function admin_Recharge($user_id, $money)
  110. // {
  111. // $where["id"] = $user_id;
  112. // $user = User::where($where)->field("money,name,phone,mail")->find();
  113. // $zong_money = $user->money + $money;
  114. // User::update(["money" => $zong_money], ["id" => $user_id]);// 更新账号金额
  115. // self::Finance($user_id, "充值成功", $money, 0);
  116. // // 发送通知用户 短信和邮件
  117. // $content = "充值" . $money . "BTS已处理";
  118. // if ($user->phone) {
  119. // Msg::phone($content, $user->phone);
  120. // } else {
  121. // Msg::mail($content, $user->mail);
  122. // }
  123. // }
  124. // 订单失败处理,全局使用
  125. public static function order_fail($msg, $order)
  126. {
  127. // 查找交易区块
  128. $mining = Mining::where(["id" => $order->mining_id])->find();
  129. // 扣除买入人的违约金
  130. $ensure_money = $mining->breach_money;
  131. $where["id"] = $order->buy_user;
  132. $user = User::where($where)->find();
  133. $update["ensure_money"] = $user->ensure_money - $ensure_money;
  134. // 添加违约金记录
  135. self::bond_finance($order->buy_user, "交易失败扣除保证金", $ensure_money, 1);
  136. User::update($update, $where);
  137. // 给卖出人加钱
  138. $push_user = User::where(["id" => $order->push_user])->find();
  139. $push_money = $push_user->push_money + $order->number;// 加回可售额度
  140. // 加上违约金 加入通证 和手续费
  141. $add_money = $ensure_money + $order->sxf_money;// 总获得多少钱
  142. $money = $push_user->money + $add_money;// 账号变动
  143. User::update(["money" => $money, "push_money" => $push_money], ["id" => $order->push_user]);
  144. // 添加记录
  145. self::Finance($order->push_user, "交易失败退回手续费", $order->sxf_money, 0);
  146. self::Finance($order->push_user, "交易失败获得违约金", $ensure_money, 0);
  147. // 更新订单状态
  148. $where_order["id"] = $order->id;
  149. $update_order["msg"] = $msg;
  150. $update_order["status"] = 5;
  151. $update_order["end_time"] = time();
  152. Order::update($update_order, $where_order);
  153. return 1;
  154. }
  155. /** 交易成功全局使用
  156. * @param $order 订单数据
  157. */
  158. public static function order_success($order)
  159. {
  160. // 更新订单状态
  161. $update_order["status"]=3;
  162. $update_order["end_time"]=time();
  163. Order::update($update_order,["id"=>$order->id]);
  164. $buy_user = $order->buy_user;// 买入人
  165. $push_user = $order->push_user;// 卖出人
  166. $mining_id = $order->mining_id;// 交易区
  167. $mining = Mining::where(["id" => $mining_id])->find();// 交易区
  168. if (!$mining){
  169. json_result(400,"价格区已经不存在了,无法计算");
  170. }
  171. // 统计交易次数
  172. // todo 买入人处理
  173. $buy_order_frequency = OrderFrequency::where(["user_id" => $buy_user, "mining_id" => $mining_id])->find();
  174. if (!$buy_order_frequency) {//不存在
  175. $buy_count = 1;
  176. $buy_number = 1;
  177. $push_number = 0;
  178. $add_buy["user_id"] = $buy_user;
  179. $add_buy["mining_id"] = $mining_id;
  180. $add_buy["buy"] = 1;
  181. $add_buy["push"] = 0;
  182. OrderFrequency::create($add_buy);
  183. } else {
  184. $buy_count = $buy_order_frequency->buy + 1;
  185. $buy_number = $buy_count;
  186. $push_number = $buy_order_frequency->push;
  187. OrderFrequency::update(["buy" => $buy_count], ["id" => $buy_order_frequency->id]);// 更新次数
  188. }
  189. $buy_user_data = User::where(["id" => $buy_user])->find();
  190. $keshou_money = $mining->enlarge_quota;// 可售额度
  191. // 第一次交易给用户加矿池金
  192. if ($buy_count == 1) {
  193. $pool_money = $mining->mining;
  194. $update_buy["pool_money"] = $buy_user_data->pool_money + $pool_money;// 加矿池金
  195. self::VendonolotyFinance($buy_user, $mining_id, $order->id, $order->number, $mining->mining, $keshou_money, $buy_number, $push_number, 1);
  196. }
  197. $enlarge_quota = $keshou_money + $buy_user_data->push_money;//可售额度
  198. // 给账号加相应数据
  199. $update_buy["push_money"] = $enlarge_quota;
  200. User::update($update_buy, ["id" => $buy_user]);
  201. // TODO 卖出处理
  202. $push_order_frequency = OrderFrequency::where(["user_id" => $push_user, "mining_id" => $mining_id])->find();
  203. if (!$push_order_frequency) {
  204. $push_count = 1;
  205. $add_push["user_id"] = $push_user;
  206. $add_push["mining_id"] = $mining_id;
  207. $add_push["buy"] = 0;
  208. $add_push["push"] = 1;
  209. OrderFrequency::create($add_push);
  210. } else {
  211. $push_count = $push_order_frequency->push + 1;
  212. OrderFrequency::update(["push" => $push_count], ["id" => $push_order_frequency->id]);// 更新次数
  213. }
  214. // 减少账号的矿金
  215. $push_user_data = User::where(["id" => $push_user])->find();
  216. $kuangjine = $push_user_data->pool_money - $mining->num;
  217. User::update(["pool_money" => $kuangjine], ["id" => $push_user]);
  218. if ($push_count == 5) {// 第五次 代理结算
  219. $direct_push = $mining->direct_push;//直推获得
  220. $second_push = $mining->second_push;//次推获得
  221. // 查找直接上级
  222. if ($push_user_data->pid) {// 是否存在直接上级
  223. $zhitui_push_user = User::where(["id" => $push_user_data->pid])->find();
  224. if ($zhitui_push_user) {
  225. // 直推奖励
  226. if ($direct_push) {// 直推奖励设置不小于0
  227. // 查看活跃度
  228. $zhitui_time =strtotime($zhitui_push_user->update_time) + (24 * 3600);
  229. if ($zhitui_time >= time()) {// 在活跃度里面就计算奖励
  230. $update_zhitui["money"] = $zhitui_push_user->money + $direct_push;
  231. User::update($update_zhitui, ["id" => $push_user_data->pid]);
  232. // 添加财务记录
  233. self::Finance($push_user_data->pid, "获得直推奖励", $direct_push, 0,1);
  234. }
  235. }
  236. if ($zhitui_push_user->pid) {// 是否存在次级上级
  237. // 次级上级
  238. if ($second_push) {// 次级奖励不小于0
  239. // 查询次级用户
  240. $ciji_push_user = User::where(["id" => $zhitui_push_user->pid])->find();
  241. if ($ciji_push_user) {
  242. $ciji_time = (int)strtotime($ciji_push_user->update_time) + 24 * 3600;
  243. if ($ciji_time >= time()) {// 给次级上级发奖励
  244. $update_ciji["money"] = $ciji_push_user->money - $second_push;
  245. User::update($update_ciji, ["id" => $zhitui_push_user->pid]);
  246. // 添加财务记录
  247. self::Finance($zhitui_push_user->pid, "获得次推奖励", $second_push, 0,1);
  248. }
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }
  255. }
  256. //资产明细
  257. public static function Finance($user_id, $name, $money, $status,$daili=0)
  258. {
  259. $add["user_id"] = $user_id;
  260. $add["name"] = $name;
  261. $add["money"] = $money;
  262. $add["status"] = $status;
  263. $add["daili"] = $daili;
  264. $add["add_time"] = time();
  265. Finance::create($add);
  266. }
  267. // 保证金处理
  268. public static function bond_finance($user_id, $name, $money, $status)
  269. {
  270. $add["user_id"] = $user_id;
  271. $add["name"] = $name;
  272. $add["money"] = $money;
  273. $add["status"] = $status;
  274. $add["add_time"] = time();
  275. BondFinance::create($add);
  276. }
  277. /**可售余额 记录
  278. * @param $user_id 用户id
  279. * @param $mining_id 区块id
  280. * @param $order_id 订单id
  281. * @param $money 获得交易区块
  282. * @param $pool 矿池数量
  283. * @param $push_money 可售额度
  284. * @param $buy 当前买入次数
  285. * @param $push 当前卖出次数
  286. * @param $status 0 增加 1 减少
  287. */
  288. public static function VendonolotyFinance($user_id, $mining_id, $order_id, $money, $pool, $push_money, $buy, $push, $status)
  289. {
  290. $add["user_id"] = $user_id;
  291. $add["mining_id"] = $mining_id;
  292. $add["order_id"] = $order_id;
  293. $add["money"] = $money;
  294. $add["pool"] = $pool;
  295. $add["push_money"] = $push_money;
  296. $add["buy"] = $buy;
  297. $add["push"] = $push;
  298. $add["status"] = $status;
  299. $add["add_time"] = time();
  300. VendibilityFinance::create($add);
  301. }
  302. }