Repair_model.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. <?php
  2. if (!defined('BASEPATH'))exit('No direct script access allowed');
  3. include_once(FCPATH . 'application/models/Base_model.php');
  4. class Repair_model extends Base_model {
  5. protected $table = 'repair_info';
  6. public function __construct() {
  7. parent::__construct();
  8. }
  9. // 根据条件获取维修信息列表
  10. public function getList($filter,$page = null,$limit = null){
  11. $where_sql = "";
  12. $arrProjectTpl = array('company','province','zone');
  13. foreach ($arrProjectTpl as $key){
  14. if (isset($filter[$key]) || !empty($filter[$key])){
  15. $projectsql = "SELECT * FROM project WHERE $key='".$filter[$key]."'";
  16. $query = $this->db->query($projectsql);
  17. $projectList = $query->result_array();
  18. $projectIds = array();
  19. foreach ($projectList as $p){
  20. $projectIds[] = $p['id'];
  21. }
  22. if (empty($projectIds)){
  23. $projectIds[] = 0;
  24. }
  25. $where_sql.= " AND RI.projectid IN(".implode(',', $projectIds).")";
  26. }
  27. }
  28. if (isset($filter['projectid']) || !empty($filter['projectid'])){
  29. $where_sql.= " AND RI.projectid='".$filter['projectid']."'";
  30. }
  31. if (isset($filter['networkid']) || !empty($filter['networkid'])){
  32. $where_sql.= " AND RI.networkid='".$filter['networkid']."'";
  33. }
  34. if (isset($filter['projectname']) || !empty($filter['projectname'])){
  35. $projectsql = "SELECT * FROM project WHERE projectname LIKE '%".$filter['projectname']."%'";
  36. $query = $this->db->query($projectsql);
  37. $projectList = $query->result_array();
  38. $projectIds = array();
  39. foreach ($projectList as $p){
  40. $projectIds[] = $p['id'];
  41. }
  42. if (empty($projectIds)){
  43. $projectIds[] = 0;
  44. }
  45. $where_sql.= " AND RI.projectid IN(".implode(',', $projectIds).")";
  46. }
  47. if (isset($filter['networkname']) || !empty($filter['networkname'])){
  48. $networksql = "SELECT * FROM network WHERE networkname LIKE '%".$filter['networkname']."%'";
  49. $query = $this->db->query($networksql);
  50. $networkList = $query->result_array();
  51. $networkIds = array();
  52. foreach ($networkList as $n){
  53. $networkIds[] = $n['id'];
  54. }
  55. if (empty($networkIds)){
  56. $networkIds[] = 0;
  57. }
  58. $where_sql.= " AND RI.networkid IN(".implode(',', $networkIds).")";
  59. }
  60. if (isset($filter['lampnumber']) || !empty($filter['lampnumber'])){
  61. $lampsql = "SELECT * FROM lampinfo WHERE number LIKE '%".$filter['lampnumber']."%'";
  62. $query = $this->db->query($lampsql);
  63. $lampList = $query->result_array();
  64. $lampIds = array();
  65. foreach ($lampList as $l){
  66. $lampIds[] = $l['id'];
  67. }
  68. if (empty($lampIds)){
  69. $lampIds[] = 0;
  70. }
  71. $where_sql.= " AND RI.lampid IN(".implode(',', $lampIds).")";
  72. }
  73. if (isset($filter['repair_path']) || !empty($filter['repair_path'])){
  74. $where_sql.= " AND RI.repair_path LIKE'%".$filter['repair_path']."%'";
  75. }
  76. if (isset($filter['repair_usernumber']) || !empty($filter['repair_usernumber'])){
  77. $repairusersql = "SELECT * FROM repair_user WHERE number LIKE '%".$filter['repair_usernumber']."%'";
  78. $query = $this->db->query($repairusersql);
  79. $repairuserList = $query->result_array();
  80. $repairuserIds = array();
  81. foreach ($repairuserList as $l){
  82. $repairuserIds[] = $l['id'];
  83. }
  84. if (empty($repairuserIds)){
  85. $repairuserIds[] = 0;
  86. }
  87. $where_sql.= " AND RI.repair_userid IN(".implode(',', $repairuserIds).")";
  88. }
  89. if (isset($filter['repair_username']) || !empty($filter['repair_username'])){
  90. $repairusersql = "SELECT * FROM repair_user WHERE name LIKE '%".$filter['repair_username']."%'";
  91. $query = $this->db->query($repairusersql);
  92. $repairuserList = $query->result_array();
  93. $repairuserIds = array();
  94. foreach ($repairuserList as $l){
  95. $repairuserIds[] = $l['id'];
  96. }
  97. if (empty($repairuserIds)){
  98. $repairuserIds[] = 0;
  99. }
  100. $where_sql.= " AND RI.repair_userid IN(".implode(',', $repairuserIds).")";
  101. }
  102. if (isset($filter['id'])){
  103. $where_sql = " AND RI.id=".intval($filter['id'])." ";
  104. }
  105. if (isset($filter['userid'])){
  106. $where_sql = " AND RI.repair_userid=".intval($filter['userid'])." ";
  107. }
  108. $sql = "SELECT RI.id AS id,
  109. RI.repair_userid AS repair_userid,
  110. RI.projectid AS projectid,
  111. RI.networkid AS networkid,
  112. RI.lampid AS lampid,
  113. RI.repair_hitch AS repair_hitch,
  114. RI.repair_solution AS repair_solution,
  115. RI.repair_time AS repair_time,
  116. RI.repair_path AS repair_path,
  117. RI.created AS created,
  118. P.projectname AS projectname,
  119. N.networkname AS networkname,
  120. RU.number AS repair_usernumber,
  121. L.number AS lampnumber,
  122. RU.name AS repair_username
  123. FROM repair_info AS RI LEFT JOIN
  124. project AS P ON P.id=RI.projectid LEFT JOIN
  125. network AS N ON N.id=RI.networkid LEFT JOIN
  126. lampinfo AS L ON L.id=RI.lampid LEFT JOIN
  127. repair_user AS RU ON RU.id=RI.repair_userid
  128. WHERE 1=1";
  129. $limit_sql = "";
  130. if ($page != null && $limit != null){
  131. $limit_sql = " LIMIT ".($page-1)*$limit.",".$limit;
  132. }
  133. $query = $sql.$where_sql.$limit_sql;
  134. $query = $this->db->query($query);
  135. return $query->result_array();
  136. }
  137. public function getDispatchList($filter,$page = null,$limit = null){
  138. $where_sql = "";
  139. if (isset($filter['user_id'])){
  140. $repairuserIds = $this->get_repair_user_list($filter['user_id']);
  141. if (empty($repairuserIds)){
  142. $repairuserIds[] = 0;
  143. }
  144. $where_sql.= " AND RD.repair_userid IN(".implode(',', $repairuserIds).")";
  145. }
  146. if (isset($filter['repair_usernumber']) || !empty($filter['repair_usernumber'])){
  147. $repairusersql = "SELECT * FROM repair_user WHERE number LIKE '%".$filter['repair_usernumber']."%'";
  148. $query = $this->db->query($repairusersql);
  149. $repairuserList = $query->result_array();
  150. $repairuserIds = array();
  151. foreach ($repairuserList as $l){
  152. $repairuserIds[] = $l['id'];
  153. }
  154. if (empty($repairuserIds)){
  155. $repairuserIds[] = 0;
  156. }
  157. $where_sql.= " AND RD.repair_userid IN(".implode(',', $repairuserIds).")";
  158. }
  159. if (isset($filter['repair_username']) || !empty($filter['repair_username'])){
  160. $repairusersql = "SELECT * FROM repair_user WHERE name LIKE '%".$filter['repair_username']."%'";
  161. $query = $this->db->query($repairusersql);
  162. $repairuserList = $query->result_array();
  163. $repairuserIds = array();
  164. foreach ($repairuserList as $l){
  165. $repairuserIds[] = $l['id'];
  166. }
  167. if (empty($repairuserIds)){
  168. $repairuserIds[] = 0;
  169. }
  170. $where_sql.= " AND RD.repair_userid IN(".implode(',', $repairuserIds).")";
  171. }
  172. if (isset($filter['id'])){
  173. $where_sql = " AND RD.id=".intval($filter['id'])." ";
  174. }
  175. $sql = "SELECT RD.id AS id,
  176. RD.repair_userid AS repair_userid,
  177. RD.projectid AS projectid,
  178. RD.networkid AS networkid,
  179. RD.starttime AS starttime,
  180. RD.plantime AS plantime,
  181. RD.finishtime AS finishtime,
  182. RD.created AS created,
  183. RD.address AS address,
  184. P.projectname AS projectname,
  185. N.networkname AS networkname,
  186. RU.number AS repair_usernumber,
  187. RU.name AS repair_username
  188. FROM repair_dispatch AS RD LEFT JOIN
  189. project AS P ON P.id=RD.projectid LEFT JOIN
  190. network AS N ON N.id=RD.networkid LEFT JOIN
  191. repair_user AS RU ON RU.id=RD.repair_userid
  192. WHERE 1=1";
  193. $limit_sql = "";
  194. if ($page != null && $limit != null){
  195. $limit_sql = " LIMIT ".($page-1)*$limit.",".$limit;
  196. }
  197. $query = $sql.$where_sql.$limit_sql;
  198. $query = $this->db->query($query);
  199. return $query->result_array();
  200. }
  201. // 通过权限获取故障总数
  202. public function get_total_by_role($filter,$role,$userid,$companyid,$projectid=0){
  203. $data = $this->get_list_by_role($filter,$role,$userid,$companyid,$projectid,"count(*) as total");
  204. return $data[0]['total'];
  205. }
  206. // 通过权限获取维修信息列表
  207. public function get_list_by_role($filter,$role,$userid,$companyid,$projectid=0,$field='*',$page=null,$limit=null){
  208. // 设置筛选条件
  209. $keyword = '';
  210. if (isset($filter['keyword'])) {
  211. $keyword = $filter['keyword'];
  212. unset($filter['keyword']);
  213. }
  214. $temp = array();
  215. foreach ($filter as $key => $value) {
  216. $temp[] = "{$key} = '{$value}'";
  217. }
  218. // 获取用户查看权限
  219. if ($projectid > 0) {
  220. $projectIds = $projectid;
  221. }else{
  222. $projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
  223. }
  224. $where = 'role_'.$role.'='.$userid;
  225. if (empty($projectIds)) {
  226. $where .= " and P.id in (0)";
  227. }else{
  228. $where .= " and P.id in ({$projectIds})";
  229. }
  230. if (!empty($temp)) {
  231. $where .= implode(' AND ', $temp);
  232. }
  233. if (!empty($keyword)) {
  234. $where .= " and (N.networkname LIKE '%{$keyword}%'";
  235. $where .= " or L.number LIKE '%{$keyword}%') ";
  236. }
  237. // 设置分页
  238. $limit = '';
  239. if ($page != null && $limit != null) {
  240. $limit = " LIMIT {($page-1)*$limit},{$limit}";
  241. }
  242. $sql = "SELECT {$field} FROM {$this->table} as R
  243. left join lampinfo as L on L.id = R.lampid
  244. left join project as P on P.id = R.projectid
  245. left join network as N on N.id = R.networkid
  246. left join repair_user as RU on RU.id = R.repair_userid where {$where} {$limit} order by R.created";
  247. $data = $this->db->query($sql)->result_array();
  248. return $data;
  249. }
  250. public function get_list_by_filter($filter,$field='*'){
  251. $this->db->select($field);
  252. if (!empty($filter)){
  253. foreach ($filter as $k => $v) {
  254. if (is_array($v)) {
  255. if (!empty($v)) {
  256. $this->db->where_in($k,$v);
  257. }else{
  258. $this->db->where_in($k,array(0));
  259. }
  260. }else{
  261. $this->db->where($k,$v);
  262. }
  263. }
  264. }
  265. return $this->db->get($this->table)->result_array();
  266. }
  267. // 获取维修信息列表
  268. public function get_repair_info_list($filter,$field='*'){
  269. $this->db->select($field);
  270. if(!empty($filter['page']) && !empty($filter['count'])){
  271. $this->db->limit($filter['count'],($filter['page']-1)*$filter['count']);
  272. unset($filter['count']);
  273. unset($filter['page']);
  274. }
  275. if (!empty($filter)){
  276. foreach ($filter as $k => $v) {
  277. if (is_array($v)) {
  278. if (!empty($v)) {
  279. $this->db->where_in($k,$v);
  280. }else{
  281. $this->db->where_in($k,array(0));
  282. }
  283. }else{
  284. $this->db->where($k,$v);
  285. }
  286. }
  287. }
  288. $this->db->join('repair_user as RU','RU.id = RI.repair_userid','left');
  289. return $this->db->get($this->table.' as RI')->result_array();
  290. }
  291. public function get_repair_total($filter){
  292. $this->db->select('count(*) as total');
  293. if (!empty($filter)){
  294. foreach ($filter as $k => $v) {
  295. if (is_array($v)) {
  296. if (!empty($v)) {
  297. $this->db->where_in($k,$v);
  298. }else{
  299. $this->db->where_in($k,array(0));
  300. }
  301. }else{
  302. $this->db->where($k,$v);
  303. }
  304. }
  305. }
  306. $this->db->join('repair_user as RU','RU.id = RI.repair_userid','left');
  307. $data = $this->db->get($this->table.' as RI')->row_array();
  308. return $data['total'];
  309. }
  310. // 获取维修人员id
  311. public function get_userid_by_alarm($alarmid){
  312. $sql = "SELECT RD.repair_userid as userid from alarm_info_log AS AI left join repair_dispatch AS RD on AI.lampid = RD.lampid where RD.alarmid = {$alarmid}";
  313. $data = $this->db->query($sql)->row_array();
  314. return $data['userid'];
  315. }
  316. public function getTotal($filter){
  317. $where_sql = "";
  318. $arrProjectTpl = array('company','province','zone');
  319. foreach ($arrProjectTpl as $key){
  320. if (isset($filter[$key]) || !empty($filter[$key])){
  321. $projectsql = "SELECT * FROM project WHERE $key='".$filter[$key]."'";
  322. $query = $this->db->query($projectsql);
  323. $projectList = $query->result_array();
  324. $projectIds = array();
  325. foreach ($projectList as $p){
  326. $projectIds[] = $p['id'];
  327. }
  328. if (empty($projectIds)){
  329. $projectIds[] = 0;
  330. }
  331. $where_sql.= " AND RI.projectid IN(".implode(',', $projectIds).")";
  332. }
  333. }
  334. if (isset($filter['projectid']) || !empty($filter['projectid'])){
  335. $where_sql.= " AND RI.projectid='".$filter['projectid']."'";
  336. }
  337. if (isset($filter['networkid']) || !empty($filter['networkid'])){
  338. $where_sql.= " AND RI.networkid='".$filter['networkid']."'";
  339. }
  340. if (isset($filter['projectname']) || !empty($filter['projectname'])){
  341. $projectsql = "SELECT * FROM project WHERE projectname LIKE '%".$filter['projectname']."%'";
  342. $query = $this->db->query($projectsql);
  343. $projectList = $query->result_array();
  344. $projectIds = array();
  345. foreach ($projectList as $p){
  346. $projectIds[] = $p['id'];
  347. }
  348. if (empty($projectIds)){
  349. $projectIds[] = 0;
  350. }
  351. $where_sql.= " AND RI.projectid IN(".implode(',', $projectIds).")";
  352. }
  353. if (isset($filter['networkname']) || !empty($filter['networkname'])){
  354. $networksql = "SELECT * FROM network WHERE networkname LIKE '%".$filter['networkname']."%'";
  355. $query = $this->db->query($networksql);
  356. $networkList = $query->result_array();
  357. $networkIds = array();
  358. foreach ($networkList as $n){
  359. $networkIds[] = $n['id'];
  360. }
  361. if (empty($networkIds)){
  362. $networkIds[] = 0;
  363. }
  364. $where_sql.= " AND RI.networkid IN(".implode(',', $networkIds).")";
  365. }
  366. if (isset($filter['lampnumber']) || !empty($filter['lampnumber'])){
  367. $lampsql = "SELECT * FROM lampinfo WHERE number LIKE '%".$filter['lampnumber']."%'";
  368. $query = $this->db->query($lampsql);
  369. $lampList = $query->result_array();
  370. $lampIds = array();
  371. foreach ($lampList as $l){
  372. $lampIds[] = $l['id'];
  373. }
  374. if (empty($lampIds)){
  375. $lampIds[] = 0;
  376. }
  377. $where_sql.= " AND RI.lampid IN(".implode(',', $lampIds).")";
  378. }
  379. if (isset($filter['repair_path']) || !empty($filter['repair_path'])){
  380. $where_sql.= " AND RI.repair_path LIKE'%".$filter['repair_path']."%'";
  381. }
  382. if (isset($filter['repair_usernumber']) || !empty($filter['repair_usernumber'])){
  383. $repairusersql = "SELECT * FROM repair_user WHERE number LIKE '%".$filter['repair_usernumber']."%'";
  384. $query = $this->db->query($repairusersql);
  385. $repairuserList = $query->result_array();
  386. $repairuserIds = array();
  387. foreach ($repairuserList as $l){
  388. $repairuserIds[] = $l['id'];
  389. }
  390. if (empty($repairuserIds)){
  391. $repairuserIds[] = 0;
  392. }
  393. $where_sql.= " AND RI.repair_userid IN(".implode(',', $repairuserIds).")";
  394. }
  395. if (isset($filter['repair_username']) || !empty($filter['repair_username'])){
  396. $repairusersql = "SELECT * FROM repair_user WHERE name LIKE '%".$filter['repair_username']."%'";
  397. $query = $this->db->query($repairusersql);
  398. $repairuserList = $query->result_array();
  399. $repairuserIds = array();
  400. foreach ($repairuserList as $l){
  401. $repairuserIds[] = $l['id'];
  402. }
  403. if (empty($repairuserIds)){
  404. $repairuserIds[] = 0;
  405. }
  406. $where_sql.= " AND RI.repair_userid IN(".implode(',', $repairuserIds).")";
  407. }
  408. if (isset($filter['userid'])){
  409. $where_sql = " AND RI.repair_userid=".intval($filter['userid'])." ";
  410. }
  411. $sql = "SELECT COUNT(RI.id) AS total
  412. FROM repair_info AS RI LEFT JOIN
  413. project AS P ON P.id=RI.projectid LEFT JOIN
  414. network AS N ON N.id=RI.networkid LEFT JOIN
  415. lampinfo AS L ON L.id=RI.lampid LEFT JOIN
  416. repair_user AS RU ON RU.id=RI.repair_userid
  417. WHERE 1=1";
  418. $limit_sql = "";
  419. $query = $sql.$where_sql.$limit_sql;
  420. $query = $this->db->query($query);
  421. $row = $query->row_array();
  422. return $row['total'];
  423. }
  424. public function getDispatchTotal($filter){
  425. $where_sql = "";
  426. if (isset($filter['user_id'])){
  427. $repairuserIds = $this->get_repair_user_list($filter['user_id']);
  428. if (empty($repairuserIds)){
  429. $repairuserIds[] = 0;
  430. }
  431. $where_sql.= " AND RD.repair_userid IN(".implode(',', $repairuserIds).")";
  432. }
  433. if (isset($filter['repair_usernumber']) || !empty($filter['repair_usernumber'])){
  434. $repairusersql = "SELECT * FROM repair_user WHERE number LIKE '%".$filter['repair_usernumber']."%'";
  435. $query = $this->db->query($repairusersql);
  436. $repairuserList = $query->result_array();
  437. $repairuserIds = array();
  438. foreach ($repairuserList as $l){
  439. $repairuserIds[] = $l['id'];
  440. }
  441. if (empty($repairuserIds)){
  442. $repairuserIds[] = 0;
  443. }
  444. $where_sql.= " AND RD.repair_userid IN(".implode(',', $repairuserIds).")";
  445. }
  446. if (isset($filter['repair_username']) || !empty($filter['repair_username'])){
  447. $repairusersql = "SELECT * FROM repair_user WHERE name LIKE '%".$filter['repair_username']."%'";
  448. $query = $this->db->query($repairusersql);
  449. $repairuserList = $query->result_array();
  450. $repairuserIds = array();
  451. foreach ($repairuserList as $l){
  452. $repairuserIds[] = $l['id'];
  453. }
  454. if (empty($repairuserIds)){
  455. $repairuserIds[] = 0;
  456. }
  457. $where_sql.= " AND RD.repair_userid IN(".implode(',', $repairuserIds).")";
  458. }
  459. if (isset($filter['id'])){
  460. $where_sql = " AND RD.id=".intval($filter['id'])." ";
  461. }
  462. $sql = "SELECT COUNT(RD.id) AS total
  463. FROM repair_dispatch AS RD LEFT JOIN
  464. project AS P ON P.id=RD.projectid LEFT JOIN
  465. network AS N ON N.id=RD.networkid LEFT JOIN
  466. repair_user AS RU ON RU.id=RD.repair_userid
  467. WHERE 1=1";
  468. $limit_sql = "";
  469. $query = $sql.$where_sql.$limit_sql;
  470. $query = $this->db->query($query);
  471. $row = $query->row_array();
  472. return $row['total'];
  473. }
  474. public function insertDispatch($data){
  475. $this->db->insert('repair_dispatch', $data);
  476. return $this->db->insert_id();
  477. }
  478. public function updateDispatch($filter,$data){
  479. if (empty($filter)){
  480. return false;
  481. }
  482. foreach ($data as $key => $value) {
  483. if ($value === '') {
  484. $data[$key] = NULL;
  485. }
  486. }
  487. $sql = $this->db->update_string('repair_dispatch',$data,$filter);
  488. return $this->db->query($sql);
  489. }
  490. public function delData($condition) {
  491. if (!empty($condition)){
  492. foreach ($condition as $k => $v) {
  493. if (is_array($v)) {
  494. if (!empty($v)) {
  495. $this->db->where_in($k,$v);
  496. }else{
  497. $this->db->where_in($k,array(0));
  498. }
  499. }else{
  500. $this->db->where($k,$v);
  501. }
  502. }
  503. }else{
  504. return false;
  505. }
  506. $this->db->delete($this->table);
  507. if ($this->db->affected_rows() > 0) {
  508. return true;
  509. } else {
  510. return false;
  511. }
  512. }
  513. public function delDispatchData($condition) {
  514. if (!empty($condition)){
  515. foreach ($condition as $k => $v) {
  516. $this->db->where($k,$v);
  517. }
  518. }
  519. $this->db->delete('repair_dispatch');
  520. if ($this->db->affected_rows() > 0) {
  521. return true;
  522. } else {
  523. return false;
  524. }
  525. }
  526. public function getDispatchOne($id){
  527. $this->db->where('id',$id);
  528. $this->db->limit('1');
  529. $query = $this->db->get('repair_dispatch');
  530. return $query->row_array();
  531. }
  532. // 获取调度信息
  533. public function getDispatchData($filter,$field = '*'){
  534. if (empty($filter)) {
  535. return array();
  536. }
  537. $this->db->select($field);
  538. foreach ($filter as $key => $value) {
  539. if (is_array($value)) {
  540. if (!empty($value)) {
  541. $this->db->where_in($key,$value);
  542. }else{
  543. $this->db->where_in($key,array(0));
  544. }
  545. }else{
  546. $this->db->where($key,$value);
  547. }
  548. }
  549. $this->db->order_by('created','desc');
  550. $query = $this->db->get('repair_dispatch');
  551. return $query->row_array();
  552. }
  553. public function delDispatchOne($id) {
  554. $this->db->where('id', $id);
  555. $this->db->delete('repair_dispatch');
  556. if ($this->db->affected_rows() == 1) {
  557. return true;
  558. } else {
  559. return false;
  560. }
  561. }
  562. // 获取维修人员信息列表
  563. public function get_repair_user_list($user_id){
  564. $query = "SELECT * FROM user WHERE id={$user_id}";
  565. $query = $this->db->query($query);
  566. $user = $query->row_array();
  567. if (empty($user)) {
  568. return array();
  569. }
  570. $colum = 'role_'.$user['role'];
  571. $query = "SELECT id AS id
  572. FROM repair_user
  573. WHERE {$colum}={$user_id}";
  574. $query = $this->db->query($query);
  575. $result = $query->result_array();
  576. $userIds = array();
  577. foreach ($result as $v) {
  578. $userIds[] = $v['id'];
  579. }
  580. return $userIds;
  581. }
  582. // 获取维修人员总数
  583. public function get_repair_user_total($userinfo){
  584. $colum = 'role_'.$userinfo['role'];
  585. $sql = "SELECT count(*) as total from repair_user where {$colum}={$userinfo['id']}";
  586. $data = $this->db->query($sql)->row_array();
  587. return $data['total'];
  588. }
  589. // 删除维修人员
  590. public function del_repair_user($filter){
  591. foreach ($filter as $key => $value) {
  592. $this->db->where($key,$value);
  593. }
  594. if (!empty($filter)) {
  595. $this->db->delete('repair_user');
  596. }
  597. }
  598. // 添加维修人员维修数
  599. public function add_repaircount($userid){
  600. $sql = "update repair_user set repaircount = repaircount + 1 where id={$userid}";
  601. $this->db->query($sql);
  602. }
  603. // 减少维修人员维修数
  604. public function minus_repaircount($userid){
  605. $sql = "update repair_user set repaircount = repaircount - 1 where id={$userid} and repaircount >= 1";
  606. $this->db->query($sql);
  607. }
  608. public function get_repair_user_id($filter,$id=0){
  609. $temp = array();
  610. foreach ($filter as $key => $value) {
  611. $temp[] = "{$key}='{$value}'";
  612. }
  613. $where = '';
  614. if (!empty($temp)) {
  615. $where = "where ".implode(' AND ', $temp);
  616. }
  617. $sql = "SELECT id from repair_user ".$where;
  618. $data = $this->db->query($sql)->row_array();
  619. if (empty($data['id'])) {
  620. return 0;
  621. }else{
  622. return $id == $data['id'] ? 0 : 1;
  623. }
  624. }
  625. public function get_repair_user_by_filter($filter,$field='*'){
  626. $this->db->select($field);
  627. if(!empty($filter['page']) && !empty($filter['count'])){
  628. $this->db->limit($filter['count'],($filter['page']-1)*$filter['count']);
  629. unset($filter['count']);
  630. unset($filter['page']);
  631. }
  632. if (!empty($filter)){
  633. foreach ($filter as $k => $v) {
  634. if (is_array($v)) {
  635. if (!empty($v)) {
  636. $this->db->where_in($k,$v);
  637. }else{
  638. $this->db->where_in($k,array(0));
  639. }
  640. }else{
  641. $this->db->where($k,$v);
  642. }
  643. }
  644. }
  645. return $this->db->get('repair_user')->result_array();
  646. }
  647. }