chartPolicy.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import { message } from 'antd'
  2. import * as service from '../services/index'
  3. import URLS from '../constants/url'
  4. import 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 = moment(value1).format('YYYY-MM-DD');
  19. let v2 = moment(value2).format('YYYY-MM-DD');
  20. if(operator === 'between') {
  21. bodyFilter['value'] = v1 + ',' + v2;
  22. }else {
  23. bodyFilter['value'] = v1;
  24. }
  25. }else if(type === 'categorical' && (operator === 'contain' || operator === 'notContain')) {
  26. bodyFilter['value'] = JSON.stringify(value1);
  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['value1'] = v1;
  53. }
  54. }else if(columnType === 'categorical' && (symbol === 'contain' || symbol === 'notContain')) {
  55. modelFilter['value1'] = JSON.parse(value);
  56. }
  57. return modelFilter;
  58. }
  59. export default {
  60. namespace: 'chartPolicy',
  61. state: {
  62. chartCode: null,
  63. columnData: [],
  64. list: [{
  65. code: '1',
  66. enabled: true,
  67. name: '开放策略1',
  68. targets: [],
  69. filters: []
  70. }]
  71. },
  72. reducers: {
  73. list(state, action) {
  74. const { list } = action;
  75. return { ...state, list };
  76. },
  77. modify(state, action) {
  78. const { policy } = action;
  79. let { list } = state;
  80. for(let i = 0; i < list.length; i++) {
  81. if(list[i].code === policy.code) {
  82. list[i] = policy;
  83. break;
  84. }
  85. }
  86. return { ...state, list };
  87. },
  88. setColumnData(state, action) {
  89. const { columnData } = action;
  90. return { ...state, columnData }
  91. }
  92. },
  93. effects: {
  94. *fetchList(action, { select, call, put }) {
  95. const body = action.chartCode;
  96. try {
  97. const res = yield call(service.fetch, {
  98. url: URLS.CHART_POLICY_LIST,
  99. body
  100. });
  101. console.log('请求图表策略列表', body, res);
  102. if(!res.err && res.data.code > 0) {
  103. 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 => ({
  104. code: d.strategies.id,
  105. enabled: d.strategies.isOpen+'' === '1',
  106. name: d.strategies.name,
  107. targets: d.userGroupName.map(g => ({
  108. code: g.id+'',
  109. isGroup: true,
  110. name: g.name
  111. })).concat(d.userName.map(u => ({
  112. code: u.id+'',
  113. isGroup: false,
  114. name: u.name
  115. }))),
  116. filters: d.strategies.ruleStr ? JSON.parse(d.strategies.ruleStr).map(f => getModelFilter(f)) : [],
  117. })) : [];
  118. yield put({ type: 'list', list });
  119. }else {
  120. message.error('请求图表策略列表失败: ' + (res.err || res.data.msg));
  121. }
  122. }catch(e) {
  123. console.log(body, e);
  124. message.error('读取图表策略列表错误: ' + e);
  125. }
  126. },
  127. *remoteAdd(action, { put, call, select }) {
  128. const chartPolicy = yield select(state => state.present.chartPolicy);
  129. const { chartCode, policy } = action;
  130. const body = {
  131. type: 'chart',
  132. tarId: chartCode,
  133. name: policy.name,
  134. rule: policy.filters.map(f => getBodyFilter(f)),
  135. isOpen: policy.enabled ? '1' : '0',
  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('添加失败: ' + e);
  156. }
  157. },
  158. *remoteModify(action, { put, call, select }) {
  159. const { policy, chartCode } = action;
  160. const body = {
  161. id: policy.code,
  162. type: 'chart',
  163. tarId: chartCode,
  164. name: policy.name,
  165. rule: policy.filters.map(f => getBodyFilter(f)),
  166. isOpen: policy.enabled ? '1' : '0',
  167. };
  168. try {
  169. const res = yield call(service.fetch, {
  170. url: URLS.CHART_POLICY_UPDATE,
  171. body
  172. });
  173. console.log('修改图表策略', body, res);
  174. if(!res.err && res.data.code > 0) {
  175. yield put({ type: 'modify', policy });
  176. }else {
  177. message.error('修改失败: ' + (res.err || res.data.msg));
  178. }
  179. }catch(e) {
  180. console.log(body, e);
  181. message.error('修改失败: ' + e);
  182. }
  183. },
  184. *remoteDelete(action, { put, call, select }) {
  185. const chartPolicy = yield select(state => state.present.chartPolicy);
  186. const { code } = action;
  187. const body = code;
  188. try {
  189. const res = yield call(service.fetch, {
  190. url: URLS.CHART_POLICY_DELETE,
  191. body
  192. });
  193. console.log('删除策略', body, res);
  194. if(!res.err && res.data.code > 0) {
  195. let { list } = chartPolicy;
  196. for(let i = 0; i < list.length; i++) {
  197. if(list[i].code === code) {
  198. list.splice(i, 1);
  199. break;
  200. }
  201. }
  202. yield put({ type: 'list', list });
  203. }else {
  204. message.error('删除失败: ' + (res.err || res.data.msg));
  205. }
  206. }catch(e) {
  207. console.log(body, e);
  208. message.error('删除失败: ' + e);
  209. }
  210. },
  211. *remoteSetTarget(action, { put, call, select }) {
  212. const { policy, targets } = action;
  213. const body = {
  214. stId: policy.code+'',
  215. type: 'chart',
  216. obj: targets.map(t => ({
  217. obId: t.code,
  218. objectType: t.isGroup ? '0' : '1'
  219. }))
  220. };
  221. try {
  222. const res = yield call(service.fetch, {
  223. url: URLS.CHART_POLICY_TARGET_UPDATE,
  224. body
  225. });
  226. console.log('设置策略对象', body, res);
  227. if(!res.err && res.data.code > 0) {
  228. yield put({ type: 'modify', policy: {
  229. ...policy,
  230. targets
  231. } });
  232. }else {
  233. message.error('设置对象失败: ' + (res.err || res.data.msg));
  234. }
  235. }catch(e) {
  236. console.log(body, e);
  237. message.error('设置对象失败: ' + e);
  238. }
  239. },
  240. /**
  241. * 获得列数据
  242. */
  243. *remoteColumnData(action, { put, call, select }) {
  244. const body = action.chartCode;
  245. try {
  246. const res = yield call(service.fetch, {
  247. url: URLS.CHART_QUERY_DATACOLUMNS,
  248. body
  249. });
  250. console.log('获得图表列数据', body, res);
  251. if(!res.err && res.data.code > 0) {
  252. let columnData = res.data.data.map(d => ({
  253. name: d.columnName,
  254. label: d.columnRaname,
  255. type: d.columnType,
  256. }));
  257. yield put({ type: 'setColumnData', columnData });
  258. }else {
  259. message.error('获得图表列数据失败: ' + (res.err || res.data.msg));
  260. }
  261. }catch(e) {
  262. console.log(body, e);
  263. message.error('获得图表列数据失败: ' + e);
  264. }
  265. }
  266. }
  267. };