Opt.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace Manage\Src;
  3. use Dever;
  4. use Manage\Src\Lib\Input as Html;
  5. class Opt
  6. {
  7. # 建立的索引名
  8. const INDEX = 'i1';
  9. public function push($param = false)
  10. {
  11. $param = $param ? $param : json_decode(base64_decode(Dever::input('param')), true);
  12. if ($param) {
  13. foreach ($param as $k => $v) {
  14. $k = explode('.', $k);
  15. $time = $v['time'];
  16. unset($v['time']);
  17. $this->update($k[0], $k[1], implode(',', $v), false, $time);
  18. }
  19. }
  20. return 1;
  21. }
  22. public function get($id)
  23. {
  24. $opt = is_array($id) ? $id : Dever::db('manage/opt')->one($id);
  25. $db = Dever::db($opt['project'] . '/' . $opt['table']);
  26. if (empty($db->config['lang'])) {
  27. return;
  28. }
  29. $result['table'] = $db->config['lang'] . '<br />' . $opt['table'];
  30. $result['total'] = Dever::load($opt['project'] . '/' . $opt['table'] . '-total');
  31. $table = '<b>[表结构]</b>:';
  32. if ($result['total'] > 0) {
  33. $table .= '数据总数('.$result['total'].')';
  34. }
  35. $table .= '<table border=1 width=100% height=100%>';
  36. $table .= Dever::tbody(array('字段', '类型-长度', '名称'));
  37. foreach ($db->config['struct'] as $k => $v) {
  38. if (isset($v['type'])) {
  39. $temp = Html::cue(array('name' => $v['name']));
  40. $v['name'] = $temp['name'];
  41. $table .= Dever::tbody(array($k, $v['type'], $v['name']));
  42. }
  43. }
  44. $table .= '</table>';
  45. $table .= '<br /><b>[常用字段]</b>:<table border=1 width=100% height=100%>';
  46. $table .= Dever::tbody(array('字段', '说明'));
  47. $col = explode(',', $opt['col']);
  48. $index = array();
  49. foreach ($col as $k => $v) {
  50. if (isset($db->config['struct'][$v])) {
  51. $struct = $db->config['struct'][$v];
  52. if ($v == 'id') {
  53. $struct['name'] = '<font style="color:red">主键</font>,无需索引';
  54. } elseif ((isset($struct['option']) && is_array($struct['option']) && count($struct['option']) <= 2) || strpos($struct['type'], 'tinyint') !== false) {
  55. $struct['name'] = '<font style="color:red">不同值较少</font>,无需索引';
  56. } else {
  57. $index[$v] = $v;
  58. }
  59. $table .= Dever::tbody(array($v, $struct['name']));
  60. }
  61. }
  62. $table .= '</table>';
  63. if ($opt['col_index']) {
  64. $table .= '<br /><b style="color:red">已为('.$opt['col_index'].')建立索引,版本号:<input type="text" id="version_'.$opt['id'].'" class="form-control layui-input" style="width:300px;display: inline;" value="'.$opt['version'].'"></b>';
  65. } else {
  66. $table .= '<input type="hidden" id="version_'.$opt['id'].'" class="form-control " value="0">';
  67. }
  68. if ($index) {
  69. $index = implode(',', $index);
  70. $result['index'] = $index;
  71. $table .= '<br /><br /><b style="color:blue">建议索引:'.self::INDEX.'-<input type="text" style="width:300px;display: inline;" id="index_'.$opt['id'].'" class="form-control layui-input" value="'.$index.'"></b>';
  72. $url = Dever::url('opt.index?id=' . $opt['id']);
  73. $table .= '&nbsp;&nbsp;&nbsp;&nbsp;<a class="btn btn-default layui-btn layuiadmin-btn-list" onclick="load(\''.$url.'\' + \'&version=\' + $(\'#version_'.$opt['id'].'\').val() + \'&index=\' + $(\'#index_'.$opt['id'].'\').val())">点此更新索引</a>';
  74. }
  75. $result['text'] = $table;
  76. return $result;
  77. }
  78. public function index_api()
  79. {
  80. $id = Dever::input('id');
  81. $config = Dever::load('manage/opt-one', $id);
  82. if ($config) {
  83. $info = $this->get($config);
  84. if (isset($info['index'])) {
  85. $index = array();
  86. $info['index'] = Dever::input('index', $info['index']);
  87. $index['version'] = Dever::input('version', 0);
  88. $index['version'] += 1;
  89. $index[$index['version']] = array
  90. (
  91. self::INDEX => $info['index']
  92. );
  93. Dever::load($config['project'] . '/' . $config['table'] . '-index', $index);
  94. Dever::load('manage/opt-update', array('set_status' => 2, 'set_version' => $index['version'], 'set_col_index' => $info['index'], 'where_id' => $id));
  95. }
  96. }
  97. Dever::out('yes');
  98. }
  99. private function update($project, $table, $col, $method = false, $time = 0)
  100. {
  101. if ($project == 'manage') {
  102. return;
  103. }
  104. $param['option_project'] = $project;
  105. $param['option_table'] = $table;
  106. if ($method) $param['option_method'] = $method;
  107. $id = -1;
  108. if ($col) {
  109. $info = Dever::db('manage/opt')->one($param);
  110. if ($info) {
  111. $param['where_id'] = $info['id'];
  112. $param['set_col'] = array();
  113. $col = explode(',', $col);
  114. $info['col'] = explode(',', $info['col']);
  115. foreach ($info['col'] as $v) {
  116. $param['set_col'][$v] = $v;
  117. }
  118. foreach ($col as $v) {
  119. $param['set_col'][$v] = $v;
  120. }
  121. $param['set_col'] = implode(',', $param['set_col']);
  122. $param['set_time'] = $time;
  123. $param['set_num'] = 1;
  124. Dever::db('manage/opt')->updateNum($param);
  125. $id = $info['id'];
  126. } else {
  127. $param['add_project'] = $param['option_project'];
  128. $param['add_table'] = $param['option_table'];
  129. if($method) $param['add_method'] = $param['option_method'];
  130. $param['add_col'] = $col;
  131. $param['add_time'] = $time;
  132. $param['add_num'] = 1;
  133. $param['add_status'] = 1;
  134. $id = Dever::db('manage/opt')->insert($param);
  135. }
  136. }
  137. return $id;
  138. }
  139. }