PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Dao/App/Attend/Checkin.php

https://github.com/polokk/tudu-web-1
PHP | 394 lines | 260 code | 60 blank | 74 comment | 41 complexity | aa846cca336f16a1942ceb462ee02b3b MD5 | raw file
  1. <?php
  2. /**
  3. * Attend_Checkin
  4. * 签到登记
  5. *
  6. * LICENSE
  7. *
  8. *
  9. * @category Dao
  10. * @package Dao_App
  11. * @subpackage App
  12. * @copyright Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
  13. * @link http://www.oray.com/
  14. * @version $Id: Checkin.php 2766 2013-03-05 10:16:20Z chenyongfa $
  15. */
  16. /**
  17. * @category Dao
  18. * @package Dao_App
  19. * @subpackage App
  20. * @copyright Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
  21. */
  22. class Dao_App_Attend_Checkin extends Oray_Dao_Abstract
  23. {
  24. const TYPE_CHECKIN = 0;// 上班签到
  25. const TYPE_CHECKOUT = 1;// 下班签退
  26. /**
  27. * 签到登记类型
  28. *
  29. * @var array
  30. */
  31. protected $_supportCheckinType = array(
  32. self::TYPE_CHECKIN,
  33. self::TYPE_CHECKOUT
  34. );
  35. const STATUS_NORMAL = 0;// 正常
  36. const STATUS_LATE = 1;// 迟到
  37. const STATUS_LEAVE = 2;// 早退
  38. const STATUS_WORK = 3;// 旷工
  39. /**
  40. * 考勤状况
  41. *
  42. * @var array
  43. */
  44. protected $_supportCheckinStatus = array(
  45. self::STATUS_NORMAL,
  46. self::STATUS_LATE,
  47. self::STATUS_LEAVE,
  48. self::STATUS_WORK
  49. );
  50. /**
  51. * 获取多条签到记录
  52. *
  53. * @param array $condition
  54. * @param array filter
  55. * @return Dao_App_Attend_Record_Checkin
  56. */
  57. public function getCheckin(array $condition, $filter = null)
  58. {
  59. $table = 'attend_checkin';
  60. $columns = 'checkin_id AS checkinid, org_id AS orgid, unique_id AS uniqueid, date, type, status, ip, '
  61. . 'address, create_time AS createtime';
  62. $recordClass = 'Dao_App_Attend_Record_Checkin';
  63. $where = array();
  64. $bind = array();
  65. $where[] = 'org_id = :orgid';
  66. $bind['orgid'] = $condition['orgid'];
  67. $where[] = 'unique_id = :uniqueid';
  68. $bind['uniqueid'] = $condition['uniqueid'];
  69. $where[] = 'date = :date';
  70. $bind['date'] = $condition['date'];
  71. $where[] = 'type = :type';
  72. $bind['type'] = $condition['type'];
  73. $where = implode(' AND ', $where);
  74. if ($where) {
  75. $where = 'WHERE ' . $where;
  76. }
  77. $sql = "SELECT $columns FROM $table $where LIMIT 1";
  78. try {
  79. $record = $this->_db->fetchRow($sql, $bind);
  80. if (!$record) {
  81. return null;
  82. }
  83. return Oray_Dao::record($recordClass, $record);
  84. } catch (Zend_Db_Exception $e) {
  85. $this->_catchException($e, __METHOD__);
  86. return new Oray_Dao_Recordset();
  87. }
  88. }
  89. /**
  90. * 获取多条签到记录
  91. *
  92. * @param array $condition
  93. * @param array filter
  94. * @param mixed $sort
  95. * @param int $maxCount
  96. * @return Oray_Dao_Recordset
  97. */
  98. public function getCheckins(array $condition, $filter = null, $sort = null, $maxCount = null)
  99. {
  100. $table = 'attend_checkin';
  101. $columns = 'checkin_id AS checkinid, org_id AS orgid, unique_id AS uniqueid, date, type, status, ip, '
  102. . 'address, create_time AS createtime';
  103. $recordClass = 'Dao_App_Attend_Record_Checkin';
  104. $where = array();
  105. $order = array();
  106. $bind = array();
  107. $limit = '';
  108. if (isset($condition['orgid'])) {
  109. $where[] = 'org_id = :orgid';
  110. $bind['orgid'] = $condition['orgid'];
  111. }
  112. if (isset($condition['uniqueid'])) {
  113. $where[] = 'unique_id = :uniqueid';
  114. $bind['uniqueid'] = $condition['uniqueid'];
  115. }
  116. if (isset($condition['date'])) {
  117. if (is_array($condition['date'])) {
  118. $where[] = 'date >= :start';
  119. $where[] = 'date < :end';
  120. $bind['start'] = $condition['date']['start'];
  121. $bind['end'] = $condition['date']['end'];
  122. } else {
  123. $where[] = 'date = :date';
  124. $bind['date'] = $condition['date'];
  125. }
  126. }
  127. if (empty($where)) {
  128. return new Oray_Dao_Recordset();
  129. }
  130. $where = implode(' AND ', $where);
  131. if ($where) {
  132. $where = 'WHERE ' . $where;
  133. }
  134. $sort = $this->_formatSort($sort);
  135. foreach ($sort as $key => $val) {
  136. switch ($key) {
  137. case 'createtime':
  138. $key = 'create_time';
  139. break;
  140. case 'type':
  141. $key = 'type';
  142. break;
  143. default:
  144. continue 2;
  145. }
  146. $order[] = $key . ' ' . $val;
  147. }
  148. $order = implode(', ', $order);
  149. if ($order) {
  150. $order = 'ORDER BY ' . $order;
  151. }
  152. if (is_int($maxCount) && $maxCount > 0) {
  153. $limit = 'LIMIT ' . $maxCount;
  154. }
  155. $sql = "SELECT $columns FROM $table $where $order $limit";
  156. try {
  157. $records = $this->_db->fetchAll($sql, $bind);
  158. return new Oray_Dao_Recordset($records, $recordClass);
  159. } catch (Zend_Db_Exception $e) {
  160. $this->_catchException($e, __METHOD__);
  161. return new Oray_Dao_Recordset();
  162. }
  163. }
  164. /**
  165. * 获取迟到或旷工或早退记录
  166. *
  167. * @param array $condition
  168. * @param array $filter
  169. * @return array
  170. */
  171. public function getViolationRecords(array $condition, $filter = null)
  172. {
  173. $table = 'attend_date AS d '
  174. . 'LEFT JOIN attend_checkin AS c ON c.unique_id = d.unique_id AND c.org_id = d.org_id AND c.date = d.date ';
  175. $columns = 'c.checkin_id AS checkinid, d.org_id AS orgid, d.unique_id AS uniqueid, d.date, c.type, c.status, c.ip, c.address, '
  176. . 'd.is_late AS islate, d.is_leave AS isleave, d.is_work AS iswork, c.create_time AS createtime';
  177. $bind = array();
  178. $where = array();
  179. $order = 'ORDER BY c.date ASC';
  180. if (!empty($condition['orgid'])) {
  181. $where[] = 'd.org_id = ?';
  182. $bind[] = $condition['orgid'];
  183. }
  184. if (!empty($condition['uniqueid'])) {
  185. $where[] = 'd.unique_id = ?';
  186. $bind[] = $condition['uniqueid'];
  187. }
  188. if (!empty($condition['islate']) && is_int($condition['islate'])) {
  189. $where[] = 'd.is_late = ?';
  190. $bind[] = $condition['islate'];
  191. }
  192. if (!empty($condition['isleave']) && is_int($condition['isleave'])) {
  193. $where[] = 'd.is_leave = ?';
  194. $bind[] = $condition['isleave'];
  195. }
  196. if (!empty($condition['iswork']) && is_int($condition['iswork'])) {
  197. $where[] = 'd.is_work = ?';
  198. $bind[] = $condition['iswork'];
  199. }
  200. if (isset($condition['date']) && is_array($condition['date'])) {
  201. $where[] = 'd.date >= ?';
  202. $where[] = 'd.date < ?';
  203. $bind[] = $condition['date']['start'];
  204. $bind[] = $condition['date']['end'];
  205. }
  206. if (empty($where)) {
  207. return array();
  208. }
  209. $where = implode(' AND ', $where);
  210. if ($where) {
  211. $where = 'WHERE ' . $where;
  212. }
  213. $sql = "SELECT {$columns} FROM {$table} {$where} {$order}";
  214. try {
  215. $records = $this->_db->fetchAll($sql, $bind);
  216. $records = self::formatRecords($records);
  217. return $records;
  218. } catch (Zend_Db_Exception $e) {
  219. $this->_catchException($e);
  220. return array();
  221. }
  222. }
  223. /**
  224. * 签到、签退登记
  225. *
  226. * @param array $params
  227. * return boolean|string
  228. */
  229. public function createCheckin(array $params)
  230. {
  231. if (empty($params['checkinid'])
  232. || empty($params['orgid'])
  233. || empty($params['uniqueid'])
  234. || empty($params['date']))
  235. {
  236. return false;
  237. }
  238. $table = 'attend_checkin';
  239. $bind = array(
  240. 'checkin_id' => $params['checkinid'],
  241. 'org_id' => $params['orgid'],
  242. 'unique_id' => $params['uniqueid'],
  243. 'date' => (int) $params['date'],
  244. 'create_time' => !empty($params['createtime']) ? (int) $params['createtime'] : time()
  245. );
  246. if (isset($params['type']) && in_array($params['type'], $this->_supportCheckinType)) {
  247. $bind['type'] = (int) $params['type'];
  248. }
  249. if (isset($params['status']) && in_array($params['status'], $this->_supportCheckinStatus)) {
  250. $bind['status'] = (int) $params['status'];
  251. }
  252. if (!empty($params['ip'])) {
  253. $bind['ip'] = $params['ip'];
  254. }
  255. if (!empty($params['address'])) {
  256. $bind['address'] = $params['address'];
  257. }
  258. try {
  259. $this->_db->insert($table, $bind);
  260. } catch (Zend_Db_Exception $e) {
  261. $this->_catchException($e, __METHOD__);
  262. return false;
  263. }
  264. return $params['checkinid'];
  265. }
  266. /**
  267. * 更新签到、签退登记
  268. *
  269. * @param array $params
  270. * return boolean|string
  271. */
  272. public function updateCheckin($checkinId, array $params)
  273. {
  274. if (empty($checkinId)) {
  275. return false;
  276. }
  277. $table = 'attend_checkin';
  278. $bind = array();
  279. if (isset($params['status']) && in_array($params['status'], $this->_supportCheckinStatus)) {
  280. $bind['status'] = (int) $params['status'];
  281. }
  282. if (!empty($params['ip'])) {
  283. $bind['ip'] = $params['ip'];
  284. }
  285. if (!empty($params['address'])) {
  286. $bind['address'] = $params['address'];
  287. }
  288. if (!empty($params['createtime'])) {
  289. $bind['create_time'] = (int) $params['createtime'];
  290. }
  291. try {
  292. $where = 'checkin_id = ' . $this->_db->quote($checkinId);
  293. $this->_db->update($table, $bind, $where);
  294. } catch (Zend_Db_Exception $e) {
  295. $this->_catchException($e, __METHOD__);
  296. return false;
  297. }
  298. return $checkinId;
  299. }
  300. /**
  301. *
  302. * @param array $records
  303. */
  304. public static function formatRecords($records)
  305. {
  306. $ret = array();
  307. foreach ($records as $record) {
  308. $ret[$record['date']][] = array(
  309. 'checkinid' => $record['checkinid'],
  310. 'orgid' => $record['orgid'],
  311. 'uniqueid' => $record['uniqueid'],
  312. 'type' => (int) $record['type'],
  313. 'status' => (int) $record['status'],
  314. 'ip' => long2ip($record['ip']),
  315. 'address' => $record['address'],
  316. 'islate' => (int) $record['islate'],
  317. 'isleave' => (int) $record['isleave'],
  318. 'iswork' => (int) $record['iswork'],
  319. 'createtime' => (int) $record['createtime']
  320. );
  321. }
  322. ksort($ret);
  323. return $ret;
  324. }
  325. /**
  326. * 获取签到ID
  327. *
  328. * return string
  329. */
  330. public static function getCheckinId()
  331. {
  332. return base_convert(substr(microtime(true) * 10000, 0, -1), 10, 16) . str_pad(dechex(mt_rand(0, 0xfffff)), 5, '0', STR_PAD_LEFT);
  333. }
  334. }