123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class AttendanceAddr extends Model
- {
- public function add(){
- $data = [
- 'id' => input('id/d',0),
- 'title' => input('title','','trim'),
- 'distance' => input('distance/d',0),
- 'lat' => input('lat','','trim'),
- 'lng' => input('lng','','trim'),
- 'org_id' => input('orgId/d',0),
- // 'auths' => input('auths','','trim'),
- ];
- $result = validate('AttendanceAddr')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('AttendanceAddr')->getError());
- }
- $id = $data['id'];
- unset($data['id']);
- // $auths = json_decode($data['auths'],true);
- // unset($data['auths']);
- Db::startTrans();
- try{
- if($id > 0){
- $data['update_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- Db::name('attendance_addr_auth')->where('addr_id',$id)->delete();
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data);
- $id = $this->id;
- }
- if(!$ret){
- \exception('操作失败');
- }
- // $arr = [];
- // foreach ($auths as $k=>$v){
- // $arr[] = [
- // 'bus_id' => $v['busId'],
- // 'org_id' => $data['org_id'],
- // 'type' => $v['type'],
- // 'addr_id' => $id
- // ];
- // }
- // $ret = Db::name('attendance_addr_auth')->insertAll($arr);
- // if($ret != count($arr)){
- // \exception('操作失败');
- // }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- public function info($id){
- $info = $this->where('id',$id)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- $data = $info->toArray();
- $data['addr_id'] = $data['id'];
- unset($data['id']);
- // $auths = Db::name('attendance_addr_auth')->where('addr_id',$id)->field('id as auth_id,bus_id,type')->select();
- // $data['auths'] = $auths?$auths:[];
- return $data;
- }
- public function lists($page,$size,$title,$orgId){
- $map[] = ['del','=',0];
- $map[] = ['org_id','=',$orgId];
- if($title != ''){
- $map[] = ['title','like','%'.$title.'%'];
- }
- $lists = $this
- ->where($map)
- ->page($page,$size)
- ->order('id desc')
- ->select();
- $total = $this->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists->toArray():[]
- ];
- return $data;
- }
- public function del($id){
- $ret = $this->where('id',$id)->setField('del',1);
- if(!$ret){
- HelpHander::error('删除失败');
- }
- return true;
- }
- public function modifyStatus($id){
- $info = Db::name('attendance_addr')->where('del',0)->where('id',$id)->find();
- if(!$info){
- $this->error('记录不存在');
- }
- $enable = $info['enable']?0:1;
- $ret = Db::name('attendance_addr')->where('id',$id)->setField('enable',$enable);
- if(!$ret){
- \exception('操作失败');
- }
- return true;
- }
- public function all($orgId){
- $map[] = ['del','=',0];
- $map[] = ['org_id','=',$orgId];
- $lists = $this
- ->where($map)
- ->order('id desc')
- ->select();
- $lists = $lists?$lists->toArray():[];
- return $lists;
- }
- }
|