chartPolicy.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import { message } from 'antd'
  2. import * as service from '../services/index'
  3. import URLS from '../constants/url'
  4. import * as moment from 'moment'
  5. function getBodyFilter(modelFilter) {
  6. let { name, label, operator, operatorLabel, type, value1, value2 } = modelFilter;
  7. let bodyFilter = {
  8. columnName: name,
  9. columnLabel: label,
  10. columnType: type,
  11. symbol: operator,
  12. symbolLabel: operatorLabel,
  13. value: value1
  14. };
  15. if(type === 'scale' && operator === 'between') {
  16. bodyFilter['value'] = value1 + ',' + value2;
  17. }else if(type === 'time') {
  18. // let v1 = dateFormat(new Date(value1),'yyyy-MM-dd hh:mm:ss');
  19. // let v2 = dateFormat(new Date(value2),'yyyy-MM-dd hh:mm:ss');
  20. let v1 = moment(value1).format('YYYY-MM-DD HH:mm:ss');
  21. let v2 = moment(value2).format('YYYY-MM-DD HH:mm:ss');
  22. if(operator === 'between') {
  23. bodyFilter['value'] = v1 + ',' + v2;
  24. }else {
  25. bodyFilter['value'] = v1;
  26. }
  27. }
  28. return bodyFilter;
  29. }
  30. function getModelFilter(resFilter) {
  31. let { columnName, columnLabel, columnType, symbol, symbolLabel, value } = resFilter;
  32. let modelFilter = {
  33. name: columnName,
  34. label: columnLabel,
  35. type: columnType,
  36. operator: symbol,
  37. operatorLabel: symbolLabel,
  38. value1: value
  39. };
  40. if(columnType === 'scale' && symbol === 'between') {
  41. modelFilter['value1'] = value.split(',')[0];
  42. modelFilter['value2'] = value.split(',')[1];
  43. }else if(columnType === 'time') {
  44. let value1 = value.split(',')[0];
  45. let value2 = value.split(',')[1];
  46. let v1 = moment(value1);
  47. let v2 = moment(value2);
  48. if(symbol === 'between') {
  49. modelFilter['value1'] = v1;
  50. modelFilter['value2'] = v2;
  51. }else {
  52. modelFilter['value'] = v1;
  53. }
  54. }
  55. return modelFilter;
  56. }
  57. export default {
  58. namespace: 'chartPolicy',
  59. state: {
  60. chartCode: null,
  61. columnData: [],
  62. list: [{
  63. code: '1',
  64. enabled: true,
  65. name: '开放策略1',
  66. targets: [],
  67. filters: []
  68. }]
  69. },
  70. reducers: {
  71. list(state, action) {
  72. const { list } = action;
  73. return { ...state, list };
  74. },
  75. modify(state, action) {
  76. const { policy } = action;
  77. let { list } = state;
  78. for(let i = 0; i < list.length; i++) {
  79. if(list[i].code === policy.code) {
  80. list[i] = policy;
  81. break;
  82. }
  83. }
  84. return { ...state, list };
  85. },
  86. setColumnData(state, action) {
  87. const { columnData } = action;
  88. return { ...state, columnData }
  89. }
  90. },
  91. effects: {
  92. *fetchList(action, { select, call, put }) {
  93. const body = action.chartCode;
  94. try {
  95. const res = yield call(service.fetch, {
  96. url: URLS.CHART_POLICY_LIST,
  97. body
  98. });
  99. console.log('请求图表策略列表', body, res);
  100. if(!res.err && res.data.code > 0) {
  101. let list = (res.data.data && res.data.data.length > 0) ? res.data.data.sort((a, b) => new Date(a.createDate) - new Date(b.createDate)).map(d => ({
  102. code: d.strategies.id,
  103. enabled: d.strategies.isOpen+'' === '1',
  104. name: d.strategies.name,
  105. targets: d.userGroupName.map(g => ({
  106. code: g.ID+'',
  107. isGroup: true,
  108. name: g.NAME
  109. })).concat(d.userName.map(u => ({
  110. code: u.ID+'',
  111. isGroup: false,
  112. name: u.NAME
  113. }))),
  114. filters: d.strategies.ruleStr ? JSON.parse(d.strategies.ruleStr).map(f => getModelFilter(f)) : [],
  115. })) : [];
  116. yield put({ type: 'list', list });
  117. }else {
  118. message.error('请求图表策略列表失败: ' + (res.err || res.data.msg));
  119. }
  120. }catch(e) {
  121. console.log(body, e);
  122. message.error('读取图表策略列表错误');
  123. }
  124. },
  125. *remoteAdd(action, { put, call, select }) {
  126. const chartPolicy = yield select(state => state.present.chartPolicy);
  127. const { chartCode } = action;
  128. const { policy } = action;
  129. const body = {
  130. type: 'chart',
  131. tarId: chartCode,
  132. name: policy.name,
  133. rule: policy.filters.map(f => getBodyFilter(f)),
  134. isOpen: policy.enabled ? '1' : '0',
  135. createBy: 'zhuth'
  136. };
  137. try {
  138. const res = yield call(service.fetch, {
  139. url: URLS.CHART_POLICY_ADD,
  140. body,
  141. });
  142. console.log('添加策略', body, res);
  143. if(!res.err && res.data.code > 0) {
  144. let { list } = chartPolicy;
  145. list.push({
  146. code: res.data.data,
  147. ...policy
  148. });
  149. yield put({ type: 'list', list });
  150. }else {
  151. message.error('添加失败: ' + (res.err || res.data.msg));
  152. }
  153. }catch(e) {
  154. console.log(body, e);
  155. message.error('添加失败');
  156. }
  157. },
  158. *remoteModify(action, { put, call, select }) {
  159. const { policy, chartCode } = action;
  160. console.log(chartCode);
  161. const body = {
  162. id: policy.code,
  163. type: 'chart',
  164. tarId: chartCode,
  165. name: policy.name,
  166. rule: policy.filters.map(f => getBodyFilter(f)),
  167. isOpen: policy.enabled ? '1' : '0',
  168. createBy: 'zhuth'
  169. };
  170. try {
  171. const res = yield call(service.fetch, {
  172. url: URLS.CHART_POLICY_UPDATE,
  173. body
  174. });
  175. console.log('修改图表策略', body, res);
  176. if(!res.err && res.data.code > 0) {
  177. yield put({ type: 'modify', policy });
  178. }else {
  179. message.error('修改失败: ' + (res.err || res.data.msg));
  180. }
  181. }catch(e) {
  182. console.log(body, e);
  183. message.error('修改失败');
  184. }
  185. },
  186. *remoteDelete(action, { put, call, select }) {
  187. const chartPolicy = yield select(state => state.present.chartPolicy);
  188. const { code } = action;
  189. const body = [code];
  190. try {
  191. const res = yield call(service.fetch, {
  192. url: URLS.CHART_POLICY_DELETE,
  193. body
  194. });
  195. console.log('删除策略', body, res);
  196. if(!res.err && res.data.code > 0) {
  197. let { list } = chartPolicy;
  198. for(let i = 0; i < list.length; i++) {
  199. if(list[i].code === code) {
  200. list.splice(i, 1);
  201. break;
  202. }
  203. }
  204. yield put({ type: 'list', list });
  205. }else {
  206. message.error('删除失败: ' + (res.err || res.data.msg));
  207. }
  208. }catch(e) {
  209. console.log(body, e);
  210. message.error('删除失败');
  211. }
  212. },
  213. *remoteSetTarget(action, { put, call, select }) {
  214. const { policy, targets } = action;
  215. const body = {
  216. stId: policy.code+'',
  217. type: 'chart',
  218. obj: targets.map(t => ({
  219. obId: t.code,
  220. objectType: t.isGroup ? '0' : '1'
  221. }))
  222. };
  223. try {
  224. const res = yield call(service.fetch, {
  225. url: URLS.CHART_POLICY_TARGET_UPDATE,
  226. body
  227. });
  228. console.log('设置策略对象', body, res);
  229. if(!res.err && res.data.code > 0) {
  230. yield put({ type: 'modify', policy: {
  231. ...policy,
  232. targets
  233. } });
  234. }else {
  235. message.error('设置对象失败: ' + (res.err || res.data.msg));
  236. }
  237. }catch(e) {
  238. console.log(body, e);
  239. message.error('设置对象失败');
  240. }
  241. },
  242. /**
  243. * 获得列数据
  244. */
  245. *remoteColumnData(action, { put, call, select }) {
  246. const body = action.chartCode;
  247. try {
  248. const res = yield call(service.fetch, {
  249. url: URLS.CHART_QUERY_DATACOLUMNS,
  250. body
  251. });
  252. console.log('获得图表列数据', body, res);
  253. if(!res.err && res.data.code > 0) {
  254. let columnData = res.data.data.map(d => ({
  255. name: d.columnName,
  256. label: d.columnRaname,
  257. type: d.columnType,
  258. }));
  259. yield put({ type: 'setColumnData', columnData });
  260. }else {
  261. message.error('获得图表列数据失败: ' + (res.err || res.data.msg));
  262. }
  263. }catch(e) {
  264. console.log(body, e);
  265. message.error('获得图表列数据失败');
  266. }
  267. }
  268. },
  269. subscriptions: {
  270. setup({ dispatch, history }) {
  271. return history.listen(({ pathname, query }) => {
  272. })
  273. }
  274. }
  275. };