QueryUtil.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. Ext.define('saas.util.QueryUtil', {
  2. BaseUtil: Ext.create('saas.util.BaseUtil'),
  3. // 请求页面组件接口模板
  4. baseUrl: 'http://192.168.0.181:8560/api/ui/co_view/config?name={xtype}',
  5. // 模板替换正则
  6. urlRe: /(.*){xtype}(.*)/g,
  7. /**
  8. * 获得form的字段配置
  9. * @param form: form组件
  10. */
  11. setItems: function(form) {
  12. var me = this;
  13. debuggre;
  14. Ext.each(form.items.items, function(f){
  15. if(f.xtype=='core-query-queryformpanel'){
  16. }else if(f.xtype=='core-query-gridpanel'){
  17. }
  18. });
  19. // xtype = form.xtype,
  20. // url = me.baseUrl.replace(me.urlRe, '$1' + xtype);
  21. // me.BaseUtil.request({url})
  22. // .then(function(response) {
  23. // var res = Ext.decode(response.responseText);
  24. // if(res.success) {
  25. // var config = res.data, items = [];
  26. // if(config) {
  27. // items = config.items;
  28. // //_baseItems
  29. // // _BaseColumn: '',
  30. // // _RelativeColumn: '',
  31. // form.add(items);
  32. // }
  33. // form.fireEvent('afterSetItems', form, items);
  34. // }
  35. // })
  36. // .catch(function(response) {
  37. // console.error(response);
  38. // });
  39. },
  40. /**
  41. * 获得form的字段查询条件
  42. * @param form: form组件
  43. * @returns Mode: 字段模式,默认MAIN 当f.fieldMode = "DETAIL",Mode->"DETAIL"
  44. */
  45. getStoreMode: function(form){
  46. var Mode = "MAIN";
  47. Ext.each(form.items.items, function(f){
  48. if(!Ext.isEmpty(f.value)&&f.fieldMode == "DETAIL") {
  49. Mode = "DETAIL";
  50. break;
  51. }
  52. });
  53. return Mode;
  54. },
  55. /**
  56. * 获得form的字段查询条件
  57. * @param form: form组件
  58. */
  59. getFormCondition: function(form){
  60. var condition = "",
  61. me = this;
  62. Ext.each(form.items.items, function(f){
  63. if((f.xtype == 'checkbox' || f.xtype == 'radio')&&f.logic){
  64. if(f.value == true) {
  65. if(condition == ''){
  66. condition += "("+f.logic+")";
  67. } else {
  68. condition += ' AND (' + f.logic+')';
  69. }
  70. }
  71. } else if(f.xtype == 'datefield' && f.value != null && f.value != '' ){
  72. var v = Ext.Date.format(new Date(f.value), 'Y-m-d');
  73. if(condition == ''){
  74. condition += "to_char("+f.name+",'yyyy-MM-dd')='"+v+"'";
  75. } else {
  76. condition += " AND to_char("+f.name+",'yyyy-MM-dd')='"+v+"'";
  77. }
  78. } else if(f.xtype == 'datetimefield' && f.value != null){
  79. var v = Ext.Date.format(new Date(f.value), 'Y-m-d H:i:s');
  80. if(condition == ''){
  81. condition += f.name + "=to_date('" + v + "', 'yyyy-MM-dd HH24:mi:ss')";
  82. } else {
  83. condition += ' AND ' + f.name + "=to_date('" + v + "', 'yyyy-MM-dd HH24:mi:ss')";
  84. }
  85. } else if(f.xtype == 'numberfield' && f.value != null && f.value != ''){
  86. var endChar = '=';
  87. if(condition == ''){
  88. condition += f.name + endChar + f.value;
  89. } else {
  90. condition += ' AND ' + f.name + endChar + f.value;
  91. }
  92. } else if(f.xtype == 'combo' && f.value != '$ALL'){
  93. if(f.store.data.length > 1) {
  94. if(condition == ''){
  95. condition += '(';
  96. } else {
  97. condition += ' AND (';
  98. }
  99. var _a = '';
  100. f.store.each(function(d, idx){
  101. if(d.data.value != '$ALL') {
  102. if(_a == ''){
  103. _a += f.name +' '+ d.data.value+' ' ;
  104. } else {
  105. _a += ' OR ' + f.name +' '+ d.data.value +' ';
  106. }
  107. }
  108. });
  109. condition += _a + ')';
  110. }
  111. } else {
  112. if(!Ext.isEmpty(f.value)){
  113. if(me.contains(f.value.toString(), 'BETWEEN', true) && me.contains(f.value.toString(), 'AND', true)){
  114. if(condition == ''){
  115. condition += f.name + " " + f.value;
  116. } else {
  117. condition += ' AND (' + f.name + " " + f.value + ")";
  118. }
  119. } else if(me.contains(f.value.toString(), '||', true)){
  120. var str = '';
  121. Ext.each(f.value.split('||'), function(v){
  122. if(v != null && v != ''){
  123. if(str == ''){
  124. str += f.name + "='" + v + "'";
  125. } else {
  126. str += ' OR ' + f.name + "='" + v + "'";
  127. }
  128. }
  129. });
  130. if(condition == ''){
  131. condition += "(" + str + ")";
  132. } else {
  133. condition += ' AND (' + str + ")";
  134. }
  135. } else if(f.value.toString().charAt(0) == '!'){
  136. if(condition == ''){
  137. condition += 'nvl(' + f.name + ",' ')<>'" + f.value.substr(1) + "'";
  138. } else {
  139. condition += ' AND (nvl(' + f.name + ",' ')<>'" + f.value.substr(1) + "')";
  140. }
  141. } else {
  142. if(f.value.toString().indexOf('%') >= 0) {
  143. if(condition == ''){
  144. condition += f.name + " like '" + f.value + "'";
  145. } else {
  146. condition += ' AND (' + f.name + " like '" + f.value + "')";
  147. }
  148. } else {
  149. if(condition == ''){
  150. condition += '('+f.name + "='" + f.value + "')";
  151. } else {
  152. condition += ' AND (' + f.name + "='" + f.value + "')";
  153. }
  154. }
  155. }
  156. }
  157. }
  158. });
  159. return condition;
  160. },
  161. /**
  162. * string:原始字符串
  163. * substr:子字符串
  164. * isIgnoreCase:忽略大小写
  165. */
  166. contains: function(string, substr, isIgnoreCase){
  167. if (string == null || substr == null) return false;
  168. if (isIgnoreCase === undefined || isIgnoreCase === true) {
  169. string = string.toLowerCase();
  170. substr = substr.toLowerCase();
  171. }
  172. return string.indexOf(substr) > -1;
  173. },
  174. /**
  175. *
  176. */
  177. queryData: function(grid,condition){
  178. var me = this;
  179. if(Ext.isEmpty(condition)){
  180. condition =" 1=1 ";
  181. }
  182. var params = { keyword: condition,number:1,size:15 };
  183. var dataRes = {
  184. url :grid.listUrl,
  185. async:false,
  186. params:params
  187. };
  188. me.BaseUtil.request(dataRes)
  189. .then(function(response) {
  190. var res = Ext.decode(response.responseText);
  191. if(res.success) {
  192. var data = res.data;
  193. console.log(data);
  194. grid.getStore().loadData(data.list);
  195. grid.fireEvent('afterLoadData', grid, data.list);
  196. }
  197. })
  198. .catch(function(response) {
  199. console.log(response);
  200. // something...
  201. });
  202. },
  203. onQuery: function(parentForm,queryMoreForm){
  204. var me = this,
  205. grid = parentForm.down('grid'),
  206. queryForm = parentForm.down('form'),
  207. condition = me.getFormCondition(queryForm),
  208. Mode = me.getStoreMode(queryForm);
  209. if(queryMoreForm){//更多查询
  210. if(!Ext.isEmpty(condition)){
  211. condition = " and ( "+me.getStoreMode(queryMoreForm)+" ) ";
  212. }else{
  213. condition = me.getStoreMode(queryMoreForm);
  214. }
  215. if(Mode=="MAIN"){
  216. me.getStoreMode(queryMoreForm);
  217. }
  218. }
  219. console.log("condition:"+condition);
  220. condition = " 1=1 ";
  221. if(Mode=="MAIN"){
  222. console.log("Mode:"+Mode+",查询当前列表");
  223. grid.loadPage(1);
  224. }else{
  225. console.log("Mode:"+Mode+",查询当前关联列表");
  226. //若明细字段含明细字段注意切换数据源 grid.reconfigure(store, columns);
  227. //关联viewName = 关联viewName+"-RelativeGrid"
  228. grid.loadPage(1);
  229. }
  230. },
  231. add10EmptyRow: function(grid) {
  232. var store = grid.getStore(),
  233. selectedRecord = grid.selModel.lastSelected,
  234. datas = [];
  235. Ext.Array.each(new Array(10), function() {
  236. datas.push({});
  237. })
  238. store.insert(store.indexOf(selectedRecord) + 1, datas);
  239. },
  240. /**
  241. * 获取grid被选数据
  242. * grid:原始字符串
  243. */
  244. getGridSelected:function(grid){
  245. var items = grid.selModel.getSelection(),
  246. data = new Array();
  247. Ext.each(items, function(item, index){
  248. if(!Ext.isEmpty(item.data[grid._idField])&&!Ext.isEmpty(item.data[grid._codeField])){
  249. var o = new Object();
  250. if(grid._idField){
  251. o['id'] = item.data[grid._idField];
  252. }
  253. if(grid._codeField){
  254. o['code'] = item.data[grid._codeField];
  255. }
  256. data.push(o);
  257. }
  258. });
  259. return data;
  260. },
  261. /**
  262. * 发起批量操作请求
  263. * grid:原始字符串
  264. */
  265. vastRes:function(url,params){
  266. var me = this;
  267. me.BaseUtil.request({
  268. url: url,
  269. params: JSON.stringify(params),
  270. method: 'POST',
  271. })
  272. .then(function() {
  273. Ext.Msg.alert('提示','操作成功');
  274. })
  275. .catch(function(response) {
  276. console.log(response);
  277. Ext.Msg.alert('提示','操作失败');
  278. });
  279. }
  280. });