PageRenderTime 36ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Dao/Td/Rule/Rule.php

https://github.com/polokk/tudu-web-1
PHP | 485 lines | 286 code | 84 blank | 115 comment | 44 complexity | 89a9bf7cd31256932b8a4cfea25f630b MD5 | raw file
  1. <?php
  2. /**
  3. * Tudu Dao
  4. *
  5. * LICENSE
  6. *
  7. *
  8. * @category Dao
  9. * @package Dao_Td
  10. * @subpackage Rule
  11. * @copyright Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
  12. * @link http://www.oray.com/
  13. * @version $Id: Rule.php 2605 2013-01-05 10:01:22Z chenyongfa $
  14. */
  15. /**
  16. * @category Dao
  17. * @package Dao_Td
  18. * @subpackage Rule
  19. * @copyright Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
  20. */
  21. class Dao_Td_Rule_Rule extends Oray_Dao_Abstract
  22. {
  23. public function init()
  24. {
  25. Dao_Td_Rule_Record_Rule::setDao($this);
  26. }
  27. /**
  28. * 读取图度规则
  29. *
  30. * @param $condition
  31. * @param $filter
  32. */
  33. public function getRule(array $condition, $filter = null)
  34. {
  35. $table = 'td_rule';
  36. $columns = 'rule_id AS ruleid, unique_id AS uniqueid, description, operation, mail_remind AS mailremind, value, is_valid AS isvalid';
  37. $where = array();
  38. if (!empty($condition['ruleid'])) {
  39. $where[] = 'rule_id = ' . $this->_db->quote($condition['ruleid']);
  40. }
  41. if (!empty($condition['uniqueid'])) {
  42. $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
  43. }
  44. if (empty($where)) {
  45. return null;
  46. }
  47. if (isset($filter['isvalid'])) {
  48. $where[] = 'is_valid = ' . ($filter['isvalid'] ? 1 : 0);
  49. }
  50. $where = implode(' AND ' , $where);
  51. $sql = "SELECT {$columns} FROM {$table} WHERE {$where} LIMIT 1";
  52. try {
  53. $record = $this->_db->fetchRow($sql);
  54. if (!$record) {
  55. return null;
  56. }
  57. return Oray_Dao::record('Dao_Td_Rule_Record_Rule', $record);
  58. } catch (Zend_Db_Exception $e) {
  59. $this->_catchException($e, __METHOD__);
  60. return null;
  61. }
  62. }
  63. /**
  64. * 读取图度规则(多条)
  65. *
  66. * @param array $condition
  67. * @param array $filter
  68. * @param mixed $sort
  69. * @param int $maxCount
  70. * @return Oray_Dao_Recordset
  71. */
  72. public function getRules(array $condition, $filter = null, $sort = null, $maxCount = null)
  73. {
  74. $table = 'td_rule';
  75. $columns = 'rule_id AS ruleid, unique_id AS uniqueid, description, operation, mail_remind AS mailremind, value, is_valid AS isvalid';
  76. $where = array();
  77. $order = array();
  78. $limit = '';
  79. if (!empty($condition['uniqueid'])) {
  80. $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
  81. }
  82. if (!$where) {
  83. return new Oray_Dao_Recordset();
  84. }
  85. if (isset($filter['isvalid'])) {
  86. $where[] = 'is_valid = ' . ($filter['isvalid'] ? 1 : 0);
  87. }
  88. $where = implode(' AND ', $where);
  89. if ($where) {
  90. $where = 'WHERE ' . $where;
  91. }
  92. $sort = $this->_formatSort($sort);
  93. foreach ($sort as $key => $val) {
  94. switch ($key) {
  95. default:
  96. continue 2;
  97. }
  98. $order[] = $key . ' ' . $val;
  99. }
  100. // ORDER
  101. $order = implode(', ', $order);
  102. if ($order) {
  103. $order = 'ORDER BY ' . $order;
  104. }
  105. // LIMIT
  106. if (is_int($maxCount) && $maxCount > 0) {
  107. $limit = 'LIMIT ' . $maxCount;
  108. }
  109. $sql = "SELECT {$columns} FROM {$table} {$where} {$order} {$limit}";
  110. try {
  111. $records = $this->_db->fetchAll($sql);
  112. return new Oray_Dao_Recordset($records, 'Dao_Td_Rule_Record_Rule');
  113. } catch (Zend_Db_Exception $e) {
  114. $this->_catchException($e, __METHOD__);
  115. return new Oray_Dao_Recordset();
  116. }
  117. }
  118. /**
  119. * 创建图度规则
  120. *
  121. * @param array $params
  122. * @return boolean
  123. */
  124. public function createRule(array $params)
  125. {
  126. if (empty($params['ruleid']) || empty($params['uniqueid'])) {
  127. return false;
  128. }
  129. $table = 'td_rule';
  130. $bind = array();
  131. $bind['rule_id'] = $params['ruleid'];
  132. $bind['unique_id'] = $params['uniqueid'];
  133. if (!empty($params['description'])) {
  134. $bind['description'] = $params['description'];
  135. }
  136. if (!empty($params['operation'])) {
  137. $bind['operation'] = $params['operation'];
  138. }
  139. if (!empty($params['mailremind'])) {
  140. $bind['mail_remind'] = $params['mailremind'];
  141. }
  142. if (!empty($params['value'])) {
  143. $bind['value'] = $params['value'];
  144. }
  145. if (isset($params['isvalid'])) {
  146. $bind['is_valid'] = ($params['isvalid'] ? 1 : 0);
  147. }
  148. try {
  149. $this->_db->insert($table, $bind);
  150. } catch (Zend_Db_Exception $e) {
  151. $this->_catchException($e, __METHOD__);
  152. return false;
  153. }
  154. return $bind['rule_id'];
  155. }
  156. /**
  157. * 更新图度规则
  158. *
  159. * @param string $ruleId
  160. * @param string $params
  161. * @return boolean
  162. */
  163. public function updateRule($ruleId, array $params)
  164. {
  165. if (empty($ruleId)) {
  166. return false;
  167. }
  168. $table = 'td_rule';
  169. $where = 'rule_id = ' . $this->_db->quote($ruleId);
  170. $bind = array();
  171. if (!empty($params['description'])) {
  172. $bind['description'] = $params['description'];
  173. }
  174. if (!empty($params['operation'])) {
  175. $bind['operation'] = $params['operation'];
  176. }
  177. if (isset($params['mailremind'])) {
  178. $bind['mail_remind'] = $params['mailremind'];
  179. }
  180. if (isset($params['value'])) {
  181. $bind['value'] = $params['value'];
  182. }
  183. if (isset($params['isvalid'])) {
  184. $bind['is_valid'] = ($params['isvalid'] ? 1 : 0);
  185. }
  186. try {
  187. $this->_db->update($table, $bind, $where);
  188. } catch (Zend_Db_Exception $e) {
  189. $this->_catchException($e, __METHOD__);
  190. return false;
  191. }
  192. return true;
  193. }
  194. /**
  195. * 删除图度规则
  196. *
  197. * @param $ruleId
  198. * @return boolean
  199. */
  200. public function deleteRule($ruleId)
  201. {
  202. $ruleId = $this->_db->quote($ruleId);
  203. $sqls = array();
  204. $sqls[] = "DELETE FROM td_rule_filter WHERE rule_id = {$ruleId}";
  205. $sqls[] = "DELETE FROM td_rule WHERE rule_id = {$ruleId}";
  206. try {
  207. foreach ($sqls as $sql) {
  208. $this->_db->query($sql);
  209. }
  210. } catch (Zend_Db_Exception $e) {
  211. $this->_catchException($e, __METHOD__);
  212. return false;
  213. }
  214. return true;
  215. }
  216. /**
  217. * 读取图度规则过滤条件
  218. *
  219. * @param $condition
  220. * @param $filter
  221. * @param $sort
  222. * @param $maxCount
  223. * @return array
  224. */
  225. public function getFilters(array $condition, $filter = null, $sort = null, $maxCount = null)
  226. {
  227. $table = 'td_rule_filter';
  228. $columns = 'filter_id AS filterid, rule_id AS ruleid, what, type, value, is_valid AS isvalid';
  229. $where = array();
  230. $order = array();
  231. $limit = '';
  232. if (!empty($condition['ruleid'])) {
  233. $where[] = 'rule_id = ' . $this->_db->quote($condition['ruleid']);
  234. }
  235. if (!$where) {
  236. return new Oray_Dao_Recordset();
  237. }
  238. if (isset($filter['isvalid'])) {
  239. $where[] = 'is_valid = ' . ($filter['isvalid'] ? 1 : 0);
  240. }
  241. // WHERE
  242. $where = implode(' AND ', $where);
  243. if ($where) {
  244. $where = 'WHERE ' . $where;
  245. }
  246. // ORDER
  247. $order = implode(', ', $order);
  248. if ($order) {
  249. $order = 'ORDER BY ' . $order;
  250. }
  251. // LIMIT
  252. if (is_int($maxCount) && $maxCount > 0) {
  253. $limit = 'LIMIT ' . $maxCount;
  254. }
  255. $sql = "SELECT {$columns} FROM {$table} {$where} {$order} {$limit}";
  256. try {
  257. $records = $this->_db->fetchAll($sql);
  258. return new Oray_Dao_Recordset($records, 'Dao_Td_Rule_Record_Filter');
  259. } catch (Zend_Db_Exception $e) {
  260. $this->_catchException($e, __METHOD__);
  261. return new Oray_Dao_Recordset();
  262. }
  263. }
  264. /**
  265. * 创建图度规则过滤条件
  266. *
  267. * @param array $params
  268. * @return boolean|Ambigous <>
  269. */
  270. public function addFilter(array $params)
  271. {
  272. if (empty($params['ruleid']) || empty($params['filterid']) || empty($params['value'])) {
  273. return false;
  274. }
  275. $table = 'td_rule_filter';
  276. $bind = array();
  277. $bind['rule_id'] = $params['ruleid'];
  278. $bind['filter_id'] = $params['filterid'];
  279. $bind['value'] = $params['value'];
  280. if (!empty($params['what'])) {
  281. $bind['what'] = $params['what'];
  282. }
  283. if (!empty($params['type'])) {
  284. $bind['type'] = $params['type'];
  285. }
  286. if (isset($params['isvalid'])) {
  287. $bind['is_valid'] = ($params['isvalid'] ? 1 : 0);
  288. }
  289. try {
  290. $this->_db->insert($table, $bind);
  291. } catch (Zend_Db_Exception $e) {
  292. $this->_catchException($e, __METHOD__);
  293. return false;
  294. }
  295. return $params['filterid'];
  296. }
  297. /**
  298. * 更新图度规则过滤条件
  299. *
  300. * @param string $filterId
  301. * @param array $params
  302. * @return boolean
  303. */
  304. public function updateFilter($filterId, array $params)
  305. {
  306. if (!$filterId) {
  307. return false;
  308. }
  309. $table = 'td_rule_filter';
  310. $bind = array();
  311. $where = 'filter_id = ' . $this->_db->quote($filterId);
  312. if (!empty($params['what'])) {
  313. $bind['what'] = $params['what'];
  314. }
  315. if (!empty($params['type'])) {
  316. $bind['type'] = $params['type'];
  317. }
  318. if (isset($params['value'])) {
  319. $bind['value'] = $params['value'];
  320. }
  321. if (isset($params['isvalid'])) {
  322. $bind['is_valid'] = ($params['isvalid'] ? 1 : 0);
  323. }
  324. try {
  325. $this->_db->update($table, $bind, $where);
  326. } catch (Zend_Db_Exception $e) {
  327. $this->_catchException($e, __METHOD__);
  328. return false;
  329. }
  330. return true;
  331. }
  332. /**
  333. * 删除图度规则过滤条件
  334. *
  335. * @param string $ruleId
  336. * @param string $filterId
  337. * @return boolean
  338. */
  339. public function removeFilter($ruleId, $filterId)
  340. {
  341. $sql = 'DELETE FROM td_rule_filter WHERE rule_id = ' . $this->_db->quote($ruleId) . ' AND '
  342. . 'filter_id = ' . $this->_db->quote($filterId);
  343. try {
  344. $this->_db->query($sql);
  345. } catch (Zend_Db_Exception $e) {
  346. $this->_catchException($e, __METHOD__);
  347. return false;
  348. }
  349. return true;
  350. }
  351. /**
  352. *
  353. * @param $ruleId
  354. * @param $filter
  355. */
  356. public function getRuleById($ruleId, $filter = null)
  357. {
  358. return $this->getRule(array('ruleid' => $ruleId), $filter);
  359. }
  360. /**
  361. *
  362. * @param $ruleId
  363. * @param $filter
  364. * @param $sort
  365. * @param $maxCount
  366. */
  367. public function getFiltersByRuleId($ruleId, $filter = null, $sort = null, $maxCount = null)
  368. {
  369. return $this->getFilters(array('ruleid' => $ruleId), $filter, $sort, $maxCount);
  370. }
  371. /**
  372. *
  373. * @param $uniqueId
  374. * @param $filter
  375. * @param $sort
  376. * @param $maxCount
  377. */
  378. public function getRulesByUniqueId($uniqueId, $filter = null, $sort = null, $maxCount = null)
  379. {
  380. return $this->getRules(array('uniqueid' => $uniqueId), $filter, $sort, $maxCount);
  381. }
  382. /**
  383. * 生成规则ID
  384. *
  385. * @return string
  386. */
  387. public static function getRuleId()
  388. {
  389. $ruleId = base_convert(substr(microtime(true) * 10000, 0, -1), 10, 16) . str_pad(dechex(mt_rand(0, 0xfffff)), 5, '0', STR_PAD_LEFT);
  390. return $ruleId;
  391. }
  392. /**
  393. *
  394. * @return string
  395. */
  396. public static function getFilterId()
  397. {
  398. $filterId = base_convert(substr(microtime(true) * 10000, 0, -1), 10, 16) . str_pad(dechex(mt_rand(0, 0xfffff)), 5, '0', STR_PAD_LEFT);
  399. return $filterId;
  400. }
  401. }