AttendanceAddr.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class AttendanceAddr extends Model
  8. {
  9. public function add(){
  10. $data = [
  11. 'id' => input('id/d',0),
  12. 'title' => input('title','','trim'),
  13. 'distance' => input('distance/d',0),
  14. 'lat' => input('lat','','trim'),
  15. 'lng' => input('lng','','trim'),
  16. 'org_id' => input('orgId/d',0),
  17. // 'auths' => input('auths','','trim'),
  18. ];
  19. $result = validate('AttendanceAddr')->check($data,[],'');
  20. if(true !== $result){
  21. HelpHander::error(validate('AttendanceAddr')->getError());
  22. }
  23. $id = $data['id'];
  24. unset($data['id']);
  25. // $auths = json_decode($data['auths'],true);
  26. // unset($data['auths']);
  27. Db::startTrans();
  28. try{
  29. if($id > 0){
  30. $data['update_time'] = date('Y-m-d H:i:s');
  31. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  32. Db::name('attendance_addr_auth')->where('addr_id',$id)->delete();
  33. }else{
  34. $data['create_time'] = date('Y-m-d H:i:s');
  35. $ret = $this->allowField(true)->save($data);
  36. $id = $this->id;
  37. }
  38. if(!$ret){
  39. \exception('操作失败');
  40. }
  41. // $arr = [];
  42. // foreach ($auths as $k=>$v){
  43. // $arr[] = [
  44. // 'bus_id' => $v['busId'],
  45. // 'org_id' => $data['org_id'],
  46. // 'type' => $v['type'],
  47. // 'addr_id' => $id
  48. // ];
  49. // }
  50. // $ret = Db::name('attendance_addr_auth')->insertAll($arr);
  51. // if($ret != count($arr)){
  52. // \exception('操作失败');
  53. // }
  54. Db::commit();
  55. }catch (Exception $e){
  56. Db::rollback();
  57. HelpHander::error($e->getMessage());
  58. }
  59. return true;
  60. }
  61. public function info($id){
  62. $info = $this->where('id',$id)->find();
  63. if(!$info){
  64. HelpHander::error('数据不存在');
  65. }
  66. $data = $info->toArray();
  67. $data['addr_id'] = $data['id'];
  68. unset($data['id']);
  69. // $auths = Db::name('attendance_addr_auth')->where('addr_id',$id)->field('id as auth_id,bus_id,type')->select();
  70. // $data['auths'] = $auths?$auths:[];
  71. return $data;
  72. }
  73. public function lists($page,$size,$title,$orgId){
  74. $map[] = ['del','=',0];
  75. $map[] = ['org_id','=',$orgId];
  76. if($title != ''){
  77. $map[] = ['title','like','%'.$title.'%'];
  78. }
  79. $lists = $this
  80. ->where($map)
  81. ->page($page,$size)
  82. ->order('id desc')
  83. ->select();
  84. $total = $this->where($map)->count();
  85. $data = [
  86. 'total' => $total,
  87. 'list' => $lists?$lists->toArray():[]
  88. ];
  89. return $data;
  90. }
  91. public function del($id){
  92. $ret = $this->where('id',$id)->setField('del',1);
  93. if(!$ret){
  94. HelpHander::error('删除失败');
  95. }
  96. return true;
  97. }
  98. public function modifyStatus($id){
  99. $info = Db::name('attendance_addr')->where('del',0)->where('id',$id)->find();
  100. if(!$info){
  101. $this->error('记录不存在');
  102. }
  103. $enable = $info['enable']?0:1;
  104. $ret = Db::name('attendance_addr')->where('id',$id)->setField('enable',$enable);
  105. if(!$ret){
  106. \exception('操作失败');
  107. }
  108. return true;
  109. }
  110. public function all($orgId){
  111. $map[] = ['del','=',0];
  112. $map[] = ['org_id','=',$orgId];
  113. $lists = $this
  114. ->where($map)
  115. ->order('id desc')
  116. ->select();
  117. $lists = $lists?$lists->toArray():[];
  118. return $lists;
  119. }
  120. }