Index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use think\Request;
  5. use think\Db;
  6. use think\Session;
  7. class Index extends controller
  8. {
  9. /**
  10. * 通配符转换
  11. * @param $str
  12. * @return string
  13. */
  14. public function wildcard($str)
  15. {
  16. $_str = '';
  17. for($i=0; $i<strlen($str); $i++){
  18. if($str{$i} == '*'){
  19. $str{$i} = '%';
  20. }elseif ($str{$i} == "?"){
  21. $str{$i} = '_';
  22. }
  23. $_str .= $str{$i}; //后面一个字符的选择
  24. }
  25. return $_str;
  26. }
  27. /**
  28. * \斜线转义便于MySQL like查询
  29. * @param $str
  30. * @return string
  31. */
  32. public function slashEscap($str)
  33. {
  34. $_str = "";
  35. for ($i = 0; $i < strlen($str); $i++)
  36. {
  37. if($str{$i} == "\\"){
  38. $_str .= "\\\\";
  39. }else {
  40. $_str .= $str{$i};
  41. }
  42. }
  43. return $_str;
  44. }
  45. //获取用户的函数 (根据条件筛选出来)
  46. public function getuser(Request $request){
  47. if ($request->isPost()){
  48. $data = input();
  49. $group = isset($data['group']) ? $data['group']:''; //组
  50. $user = isset($data['user']) ? $data['user']:''; //用户
  51. $order = isset($data['order']) ? $data['order']:''; //用户
  52. $groupArr = explode(",", $group); //使用字符串切割
  53. $user = explode(",", $user); //使用字符串切割
  54. $where1=[]; //通配查询
  55. $whereuser['Login']=['in',$user]; //精准选择(用户成员)
  56. $str = "";
  57. $likeData = [];
  58. foreach ($groupArr as $key=>$value){
  59. if(strpos($value,'*') !== false || strpos($value,'?') !== false){//判断是否存在通配查询
  60. $wildcardStr = $this->wildcard($value);
  61. $str = $this->slashEscap($wildcardStr);
  62. $likeData[] = ["like",$str];
  63. $where1['group'] = $likeData;
  64. unset($groupArr[$key]);
  65. }
  66. }
  67. if(count($likeData) == 1){
  68. $where1['group'] = ["like",$str];
  69. }
  70. $where['Group'] = ['in',$groupArr]; //精准选择(组的成员);
  71. $result = Db::connect("db_con2")->name('mt5_users')->where($where)->whereOr($where1)->whereOr($whereuser)->distinct(true)->select();
  72. $result1 = Db::name('order_progress')->where(['orig_order'=>$order])->select(); //筛选函数(当前跟踪的订单)
  73. //外层筛选出来之前做过的订单
  74. foreach ($result as $key => $value) {
  75. //自己选择的用户
  76. $result[$key]['LOGIN'] = $value["Login"];
  77. $result[$key]['NAME'] = $value['Name'];
  78. $result[$key]['GROUP'] = $value['Group'];
  79. $result[$key]['BALANCE'] = $value['Balance'];
  80. foreach ($result1 as $k => $v) {
  81. if($value['Login']==$v['dest_login']){
  82. unset($result[$key]);
  83. }
  84. }
  85. }
  86. $result = array_values($result); //重新组织数组的键值对
  87. if($result){
  88. return json(['code'=>'00','user'=>$result,'msg'=>'获得数据成功']);
  89. }else{
  90. return json(['code'=>'01','msg'=>'没有对应的数据']);
  91. }
  92. }else{
  93. return json(['code'=>'01','msg'=>'请使用post方式访问']);
  94. }
  95. }
  96. //点击订单后获取到的信息
  97. public function getorder(Request $request){
  98. if ($request->isPost()){
  99. $data = input(); //获取订单的信息
  100. //获取deals表中的信息 判断订单是否平仓 entry为0是正 entry为1时是负
  101. $deals = Db::connect("db_con2")->query("select count(*) as 'num' from mt5_deals WHERE PositionID = {$data['orderid']} group by entry ");
  102. $deal = array_column($deals,'num');
  103. if(!$deals || $deal['0'] != $deal['1']){
  104. return json(['code'=>'01','msg'=>'没有找到匹配的订单']);
  105. }
  106. //查询开仓信息
  107. $openResult = Db::connect("db_con2")->query("SELECT PriceCurrent,TimeSetup FROM mt5_orders_history WHERE PositionID={$data['orderid']} and TimeSetup = (SELECT MIN(TimeSetup) FROM mt5_orders_history WHERE PositionID={$data['orderid']})")[0];
  108. //通过deals表查询盈利
  109. $earnings = Db::connect("db_con2")->query("SELECT SUM(Commission) as commission,SUM(Profit) as profit FROM mt5_deals WHERE PositionID={$data['orderid']}")[0];
  110. //查询关仓信息
  111. $closeResult = Db::connect("db_con2")->query("SELECT PriceCurrent,TimeDone,Login,PositionID,Symbol,VolumeInitial,PriceTP,PriceSL FROM mt5_orders_history WHERE PositionID={$data['orderid']} and TimeDone = (SELECT MAX(TimeDone) FROM mt5_orders_history WHERE PositionID={$data['orderid']})")[0];
  112. //获取订单的所有deal
  113. $getDeal = Db::connect("db_con2")->query("select Deal as 'deal' from mt5_deals WHERE PositionID = {$data['orderid']}");
  114. //查询该positionID的所有order
  115. $result = Db::connect("db_con2")->query("SELECT `Order` FROM mt5_orders_history WHERE PositionID = {$data['orderid']}");
  116. $getDeals = array_column($getDeal,'deal');
  117. $order = array_column($result,'Order');
  118. if($closeResult && $result && $openResult){
  119. //发送set_current_postion 数据
  120. $send = ['orders'=>$order,'from'=>strtotime($openResult['TimeSetup'])-1,'to'=>strtotime($closeResult['TimeDone'])+1,'login'=>$closeResult['Login'],'deals'=>$getDeals];
  121. $data = [
  122. 'OPEN_PRICE'=>$openResult["PriceCurrent"],
  123. 'OPEN_TIME'=>$openResult["TimeSetup"],
  124. 'CLOSE_TIME'=>$closeResult['TimeDone'],
  125. 'CLOSE_PRICE'=> $closeResult['PriceCurrent'],
  126. 'LOGIN'=>$closeResult['Login'],
  127. 'TICKET' => $closeResult['PositionID'],
  128. 'SYMBOL' => $closeResult['Symbol'],
  129. 'VOLUME' => $closeResult['VolumeInitial'],
  130. 'TP' => $closeResult['PriceTP'],
  131. 'SL' => $closeResult['PriceSL'],
  132. 'PROFIT' => $earnings['profit'],
  133. 'COMMISSION' => $earnings['commission'],
  134. 'send' => $send
  135. ];
  136. return json(['code'=>'00','data'=>$data,'msg'=>'获得数据成功']);
  137. }else{
  138. return json(['code'=>'01','msg'=>'没有找到匹配的订单']);
  139. }
  140. }else{
  141. return json(['code'=>'01','msg'=>'请使用post方式访问']);
  142. }
  143. }
  144. // 点击订单后查询函数(rollback)
  145. public function rollback_order(Request $request){
  146. if ($request->isPost()){
  147. $data = input(); //获取订单的信息
  148. $result = Db::name('order_progress')->where(['type'=>"insert","orig_order"=>$data['orderid'],'error_code'=>0])->select(); //insert成功后插入的订单
  149. $result1 = Db::name('order_progress')->where(['type'=>"rollback","orig_order"=>$data['orderid']])->where('error_code','neq',0)->select(); //rollback失败后的订单
  150. $result2 = Db::name('order_progress')->where(['type'=>"retry","orig_order"=>$data['orderid']])->where('error_code','eq',0)->select(); //retry成功的订单
  151. $result = array_merge($result,$result1,$result2); //对数据进行合并
  152. if($result){
  153. return json(['code'=>'00','data'=>$result,'msg'=>'获得数据成功']);
  154. }else{
  155. return json(['code'=>'01','msg'=>'暂无数据']);
  156. }
  157. }else{
  158. return json(['code'=>'01','msg'=>'请使用post方式访问']);
  159. }
  160. }
  161. // 点击订单后查询函数(retry)
  162. public function retry_order(Request $request){
  163. if ($request->isPost()){
  164. $data = input(); //获取订单的信息
  165. $result = Db::name('order_progress')->where(['type'=>"insert","orig_order"=>$data['orderid']])->where('error_code','neq',0)->select(); //insert失败的订单
  166. $result1 = Db::name('order_progress')->where(['type'=>"retry","orig_order"=>$data['orderid']])->where('error_code','neq',0)->select(); //retry失败的订单
  167. $result2 = Db::name('order_progress')->where(['type'=>"except_rollback","orig_order"=>$data['orderid']])->where('error_code','neq',0)->select(); //except_rollback失败的订单
  168. $result = array_merge($result,$result1,$result2); //对数据进行合并
  169. if($result){
  170. return json(['code'=>'00','data'=>$result,'msg'=>'获得数据成功']);
  171. }else{
  172. return json(['code'=>'01','msg'=>'暂无数据']);
  173. }
  174. }else{
  175. return json(['code'=>'01','msg'=>'请使用post方式访问']);
  176. }
  177. }
  178. //确认密码函数(批量操作的时候需要)
  179. public function querypassword(Request $request){
  180. if ($request->isPost()){
  181. $data = input(); //获取传递的信息
  182. $password = md5($data['password']); //获取加密后的密码
  183. //根据用户来选择数据
  184. if($data['nickname']=="普通管理员"){
  185. $result = Db::name('admin')->where(['id'=>1])->find(); //找到用户(获取原始的密码)
  186. }else{
  187. $result = Db::name('admin')->where(['id'=>2])->find(); //找到用户(获取原始的密码)
  188. }
  189. // 筛选出来的数据进行密码的匹配
  190. if($password==$result['password']){
  191. return json(['code'=>'00','msg'=>'请求数据成功']);
  192. }else{
  193. return json(['code'=>'01','msg'=>'密码填写有误']);
  194. }
  195. }else{
  196. return json(['code'=>'01','msg'=>'请使用post方式访问']);
  197. }
  198. }
  199. //更新梯度函数(retry的时候进行查询)
  200. public function update_percentage(Request $request){
  201. if ($request->isPost()){
  202. $data = input(); //获取传递的信息
  203. $update_array =[];
  204. foreach ($data as $key => $value) {
  205. $result = Db::connect("db_con2")->name('mt5_users')->where(['Login'=>$value])->find();
  206. $update_array[$value]= $result['Balance'];
  207. }
  208. if($update_array){
  209. return json(['code'=>'00','data'=>$update_array,'msg'=>'请求数据成功']);
  210. }else{
  211. return json(['code'=>'01','data'=>$update_array,'msg'=>'请求数据有误']);
  212. }
  213. }else{
  214. return json(['code'=>'01','msg'=>'请使用post方式访问']);
  215. }
  216. }
  217. }