isAjax()){
//分页参数
$length = input('rows',10,'intval'); //每页条数
$page = input('page',1,'intval'); //第几页
$start = ($page - 1) * $length; //分页开始位置
//排序
$sortRow = input('sidx','sort','trim'); //排序列
$sort = input('sord','asc','trim'); //排序方式
$order = $sortRow.' '.$sort.' ,id desc';
$month= input('month','','trim');
if($month != ''){
$month = date('Ym',strtotime($month.'-01'));
$map[] = ['create_yyyymm','=',$month];
}
$title = input('title','','trim');
if($title){
$map[] = ['title','like','%'.$title.'%'];
}
$status = input('status','','trim');
if($status != ''){
$map[] = ['status','=',$status];
}
$is_exception = input('is_exception','','trim');
if($is_exception != ''){
$map[] = ['is_exception','=',$is_exception];
}
$plan_id = input('plan_id','','trim');
if($plan_id != ''){
$map[] = ['plan_id','=',$plan_id];
}
$map[] = ['org_id','=',$this->orgId];
$map[] =['del','=',0];
$map[] =['patrol_mode','=',$mode];
$map= empty($map) ? true: $map;
//数据查询
$lists = Db::name('patrol_task')->where($map)
->limit($start,$length)->order($order)->select();
foreach ($lists as $k=>$v){
$lists[$k]['start_time'] = date('Y-m-d H:i',strtotime($v['start_time']));
$lists[$k]['end_time'] = date('Y-m-d H:i',strtotime($v['end_time']));
$taskUser=Db::name('patrol_task_user')
->alias('ptu')
->field('u.real_name')
->join('user u','u.id=ptu.user_id')
->where('ptu.patrol_task_id',$v['id'])
->select();
// foreach ($taskUser as $kk=>$vv){
// $userName[$kk]=$vv['real_name'];
// }
$lists[$k]['task_user']=implode(',',array_column($taskUser,'real_name'));
}
//数据返回
$totalCount = Db::name('patrol_task')->where($map)->count();
$totalPage = ceil($totalCount/$length);
$result['page'] = $page;
$result['total'] = $totalPage;
$result['records'] = $totalCount;
$result['rows'] = $lists;
return json($result);
}else{
$plans = model('PatrolPlan')->getListByMode($this->orgId,$mode);
$this->assign('plans',$plans);
$this->assign('mode',$mode);
$modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
$this->assign('meta_title',$modeName.'任务列表');
return $this->fetch();
}
}
/**
* 新增/编辑
*/
public function add($id=0,$mode){
$type = input('type');
$isCalendar = input('isCalendar');
if(request()->isPost()){
$model = new \app\common\model\PatrolTask();
$res = $model->updates($mode);
if($res){
if($type==1){
if($isCalendar==1){
$this->success('操作成功',url('calendar',['mode'=>$mode]));
}else{
$this->success('操作成功');
}
}else{
$this->success('操作成功',url('index',['mode'=>$mode]));
}
}else{
$this->error($model->getError());
}
}else{
$modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
$meta_title = '新增'.$modeName.'计划';
if($id){
$info = Db::name('patrol_task')->where('id',$id)->find();
$info['start_time']=date('Y-m-d H:i',strtotime($info['start_time']));
$info['end_time']=date('Y-m-d H:i',strtotime($info['end_time']));
$info['patrol_form'] =\db('patrol_task_addr')
->where('patrol_task_id',$id)
->column('patrol_form_id');
$info['task_user']=\db('patrol_task_user')
->where('patrol_task_id',$id)
->column('user_id');
// $addrForms = Db::name('patrol_task_addr')
// ->alias('a')
// ->join('patrol_addr_form b','a.patrol_form_id = b.id')
// ->join('address c','c.id = a.address_id')
// ->where('a.patrol_task_id',$info['id'])
// ->field('a.address_id as addrId,a.patrol_form_id as formId,b.title as formTitle,c.title as addrTitle')
// ->select();
$addrForms = Db::name('patrol_task_addr')
->alias('a')
->join('address c','c.id = a.address_id')
->where('a.patrol_task_id',$info['id'])
->field('a.address_id as addrId,c.title as addrTitle,a.patrol_forms')
->order(['a.id'=>'desc'])
->select();
$addrForms = $addrForms?$addrForms:[];
foreach ($addrForms as $k=>$v){
$forms = [];
if($v['patrol_forms']){
$forms = Db::name('patrol_addr_form')->where('id','in',$v['patrol_forms'])->field('id,title')->select();
}
$addrForms[$k]['forms'] = $forms?$forms:[];
}
$info['addrForms'] = $addrForms?$addrForms:[];
$this->assign('info',$info);
$meta_title = '编辑'.$modeName.'计划';;
}
//获取任务内容
$patrolAddrForm=(new \app\common\model\PatrolAddrForm())->getByModeList($mode);
//获取用户
switch ($mode){
case 1:
$pType=5;
break;
case 2:
$pType=6;
break;
case 3:
$pType=7;
break;
case 4:
$pType=8;
break;
}
$user=(new User())->getPatrolWorker($pType);
$start = input('start','');
$end = input('end','');
$this->assign('patrolForm',$patrolAddrForm);
$this->assign('user',$user);
$this->assign('mode',$mode);
$this->assign('start',$start?date('Y-m-d H:i',strtotime($start)):'');
$this->assign('end',$end?date('Y-m-d H:i',strtotime($end)):'');
$this->assign('meta_title',$meta_title);
$this->assign('type',$type);
$this->assign('isCalendar',$isCalendar);
$this->assign('address',(new \app\common\model\Address())->getListByType($mode+2));
return $this->fetch();
}
}
/**
* 删除记录
*/
public function del($id=0){
if(!$id){
$this->error('参数错误');
}
$info = Db::name('patrol_task')->where('org_id',$this->orgId)->where('id',$id)->where('del',0)->find();
if(!$info){
$this->error('任务不存在');
}
if($info['status'] != 6){
$this->error('任务未关闭不可删除');
}
$res = Db::name('patrol_task')->where('id',$id)->update([
'del'=>1,
'del_user_id'=>is_login(),
'del_time'=>getTime(),
]);
if(!$res){
$this->error('删除失败');
}
// 如果在任务栏删除任务
Db::name('task')->where('bus_id',$id)->where('type',2)->delete();
$title = db('patrol_mode')->where('id',$info['patrol_mode'])->value('name');
$content = $title.'任务管理:删除';
// model('ActionLog')->addlog(is_login(),44,$content,['id' => $id],cur_org_id());
$this->success('删除成功');
}
/**
* 关闭记录
*/
public function close($id=0){
if(!$id){
$this->error('参数错误');
}
$info = Db::name('patrol_task')->where('org_id',$this->orgId)->where('id',$id)->where('del',0)->find();
if(!$info){
$this->error('任务不存在');
}
if($info['status'] == 6){
$this->error('任务已关闭');
}
Db::startTrans();
try{
$res = Db::name('patrol_task')->where('id',$id)->update([
'status'=>6,
'update_time'=>getTime(),
]);
if(!$res){
\exception('操作失败');
}
// 如果在任务栏删除任务
Db::name('task')->where('bus_id',$id)->where('type',2)->delete();
$title = Db::name('patrol_mode')->where('id',$info['patrol_mode'])->value('name');
$content = $title.'任务管理:关闭';
// model('ActionLog')->addlog(is_login(),44,$content,['id' => $id],cur_org_id());
Db::commit();
$this->success('操作成功');
}catch (Exception $e){
Db::rollback();
$this->error('操作失败');
}
}
public function info($id=0,$mode){
$modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
$meta_title=$modeName.'巡视轨迹';
if(!$id){
$this->error('参数错误');
}
$info=Db::name('patrol_task')
->field('id,start_time,end_time,status,title,interrupt_img,interrupt_reson')
->where('id',$id)->find();
if(!$info){
$this->error('记录不存在');
}
$info['start_time']=date('Y-m-d H:i',strtotime($info['start_time']));
$info['end_time']=date('Y-m-d H:i',strtotime($info['end_time']));
//获取执行用户
$task_user=Db::name('patrol_task_user')
->alias('ptu')
->join('user u','u.id=ptu.user_id')
->where('ptu.patrol_task_id',$info['id'])
->column('u.real_name');
$info['task_user']=implode(',',$task_user);
$addrForms = Db::name('patrol_task_addr')
->alias('a')
// ->join('patrol_addr_form b','a.patrol_form_id = b.id')
->join('address c','c.id = a.address_id')
->where('a.patrol_task_id',$info['id'])
->field('a.*,c.title as addrTitle')
->select();
$addrForms = $addrForms?$addrForms:[];
foreach ($addrForms as $k=>$v){
$btitles = Db::name('patrol_addr_form')->where('id','in',$v['patrol_forms'])->column('title');
$addrForms[$k]['formTitle'] = $btitles?implode(',',$btitles):'';
$record = Db::name('patrol_record')
->alias('a')
->join('user b','a.user_id = b.id')
->where('a.patrol_task_id',$v['patrol_task_id'])
->where('a.patrol_addr_id',$v['address_id'])
->field('b.real_name as uname,a.*')
->find();
if($record){
$cj = $record['check_json']?json_decode($record['check_json'],true):[];
$record['check_json'] = $cj;
}
$addrForms[$k]['record'] = $record?$record:[];
}
$info['addrForms'] = $addrForms;
$this->assign('meta_title',$meta_title);
$this->assign('info',$info);
$this->assign('mode',$mode);
$this->assign('modeName',$modeName);
return $this->fetch();
}
public function calendar($mode){
$this->assign('mode',$mode);
return $this->fetch();
}
//获取正月数据
public function taskjson(){
$mode = (int)input('mode');
$start = input('start');
$end = input('end');
$data = array();
if(!$start||!$end||$start>$end){
header('Content-Type:application/json; charset=utf-8');
exit(json_encode($data));
}
$start = date('Y-m-d H:i:s',strtotime($start));
$end = date('Y-m-d H:i:s',strtotime($end));
$map[] = [''];
$list = (new \app\common\model\PatrolTask())->get_list_by_time($this->orgId,$mode,$start,$end);
foreach ($list as $k=>$v){
$arr = array(
'taskid' => $v['id'],
'title' => "{$v['title']}
执行人:{$v['users']}
开始时间:{$v['start_time']}
结束时间:{$v['end_time']}
",
'status' => $v['status'],
'start' => $v['start_time'],
'end' => $v['end_time']
);
if($v['status'] == 0){
$arr['color'] = '#777777';
}else if($v['status'] == 1){
$arr['color'] = '#478fca';
}else if($v['status'] == 2){
$arr['color'] = '#69aa46';
}else if($v['status'] == 3){
$arr['color'] = '#dd5a43';
}else if($v['status'] == 5){
$arr['color'] = '#53a2a7';
}else{
$arr['color'] = '#5b53a7';
}
$data[] = $arr;
}
header('Content-Type:application/json; charset=utf-8');
exit(json_encode($data));
}
//复制月计划
public function plan($mode){
$type = input('type');
if(request()->isPost()){
$data = request()->post();
if($data['from'] == $data['to']){
$this->error('不能复制同月的任务');
}
$ret = (new \app\common\model\PatrolTask())->plan_data($data,$this->orgId);
if($ret){
if($type==1){
$this->success('操作成功',url('index',['mode'=>$mode]));
}else{
$this->success('操作成功',url('calendar',['mode'=>$mode]));
}
}else{
$this->error((new \app\common\model\PatrolTask())->getError());
}
}else{
$from = (new \app\common\model\PatrolTask())->get_task_month($this->orgId,$mode);
//间隔1月往后数11次
$timenow = new \Datetime();
$datetin = \DateInterval::createFromDateString('1 month');
$datePer = new \DatePeriod($timenow, $datetin, 11);
$to = [];
foreach ($datePer as $day) {
$m = $day->format('Y-m');
$to[]= $m;
}
$this->assign('from',$from);
$this->assign('to',$to);
$this->assign('mode',$mode);
$this->assign('type',$type);
return $this->fetch();
}
}
public function batch($mode){
$type = input('type');
if(request()->isPost()){
$data = request()->post();
$minmonth = date('Y-m');
if($minmonth >= $data['from']){
$this->error('参数错误');
}
$where = array(
'org_id' => $this->orgId,
'patrol_mode' => $data['mode'],
'del' => 0,
'create_yyyymm' => date('Ym',strtotime($data['from'].'-01'))
);
$default['del'] = 1;
$default['del_user_id'] = $this->userId;
$default['del_time'] = date('Y-m-d H:i:s');
$result = Db::name('patrol_task')
->where($where)
->update($default);
$type = (new Task())::TASK_TYPE_PATROL;
// 检查是否存在任务栏数据中
$dailyIds = Db::name('patrol_task_task')
->alias('dt')
->join('task t','t.bus_id = dt.id')
->where('t.type',$type)
->where('dt.org_id',$this->orgId)
->where('dt.del',0)
->where('dt.create_yyyymm',date('Ym',strtotime($data['from'].'-01')))
->column('dt.id');
if($result){
if($dailyIds){
Db::name('task')
->where('bus_id', 'in',implode(',',$dailyIds))
->where('type', $type)
->delete();
}
if($type==1){
$this->success('操作成功',url('index',['mode'=>$mode]));
}else{
$this->success('操作成功',url('calendar',['mode'=>$mode]));
}
}else{
$this->error('删除失败');
}
}else{
$from = (new \app\common\model\PatrolTask())->get_task_month($this->orgId,$mode);
$minmonth = date('Y-m');
$aa = [];
foreach ($from as $k=>$v){
if($minmonth < $v['create_yyyymm']){
$aa[] = $v['create_yyyymm'];
}
}
$this->assign('from',$aa);
$this->assign('mode',$mode);
$this->assign('type',$type);
return $this->fetch();
}
}
public function tj($mode) {
$cur = date('Y-m-d');
$start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
$end = input('end', date('Y-m-d'));
$start1 = $start . ' 00:00:00';
$end1 = $end . ' 23:59:59';
if(request()->isAjax()){
//分页参数
$length = input('rows',10,'intval'); //每页条数
$page = input('page',1,'intval'); //第几页
$start = ($page - 1) * $length; //分页开始位置
//排序
$sortRow = input('sidx','sort','trim'); //排序列
$sort = input('sord','asc','trim'); //排序方式
$order = $sortRow.' '.$sort.' ,id desc';
$map1[] = ['a.start_time', '>=', $start1];
$map1[] = ['a.start_time', '<=', $end1];
$map1[] = ['a.org_id', '=', $this->orgId];
$map1[] = ['a.patrol_mode', '=', $mode];
$map1[] = ['a.del', '=', 0];
$map2 = [];
$title = input('title','','trim');
if ($title !='') {
$map2[] = ['u.real_name','like','%'.$title.'%'];
}
$list = Db::name('patrol_task_user')
->alias('b')
->join('patrol_task a','a.id=b.patrol_task_id')
->join('user u','b.user_id=u.id')
->where($map1)
->where($map2)
->group('b.user_id')
->distinct(true)
->field('b.user_id,u.real_name')
->limit($start,$length)
->order('a.id desc')
->select();
foreach ($list as $k => $v) {
$list[$k]['title'] = $v['real_name'];
$list[$k]['c1'] = Db::name('patrol_task')
->alias('a')
->join('patrol_task_user b','a.id=b.patrol_task_id')
->where($map1)
->where('b.user_id',$v['user_id'])
->where('a.status',0)
->count();
$list[$k]['c2'] = Db::name('patrol_task')
->alias('a')
->join('patrol_task_user b','a.id=b.patrol_task_id')
->where($map1)
->where('b.user_id',$v['user_id'])
->where('a.status',1)
->count();
$list[$k]['c3'] = Db::name('patrol_task')
->alias('a')
->join('patrol_task_user b','a.id=b.patrol_task_id')
->where($map1)
->where('b.user_id',$v['user_id'])
->where('a.status',2)
->count();
$list[$k]['c4'] = Db::name('patrol_task')
->alias('a')
->join('patrol_task_user b','a.id=b.patrol_task_id')
->where($map1)
->where('b.user_id',$v['user_id'])
->where('a.status',3)
->count();
$list[$k]['c5'] = Db::name('patrol_task')
->alias('a')
->join('patrol_task_user b','a.id=b.patrol_task_id')
->where($map1)
->where('b.user_id',$v['user_id'])
->where('a.status',5)
->count();
$list[$k]['c6'] = Db::name('patrol_task')
->alias('a')
->join('patrol_task_user b','a.id=b.patrol_task_id')
->where($map1)
->where('b.user_id',$v['user_id'])
->where('a.status',6)
->count();
$list[$k]['st'] = $start1;
$list[$k]['et'] = $end1;
}
//数据返回
$totalCount = Db::name('patrol_task_user')
->alias('b')
->join('patrol_task a','a.id=b.patrol_task_id')
->join('user u','b.user_id=u.id')
->where($map1)
->where($map2)
->group('b.user_id')
->distinct(true)
->count();
$totalPage = ceil($totalCount/$length);
$result['page'] = $page;
$result['total'] = $totalPage;
$result['records'] = $totalCount;
$result['rows'] = $list;
return json($result);
}else{
$this->assign('start', $start);
$this->assign('end', $end);
$this->assign('mode', $mode);
$modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
$this->assign('meta_title',$modeName);
return $this->fetch();
}
}
public function tjData($start1, $end1, $title,$mode) {
$map1[] = ['a.start_time', '>=', $start1];
$map1[] = ['a.start_time', '<=', $end1];
$map1[] = ['a.org_id', '=', $this->orgId];
$map1[] = ['a.patrol_mode', '=', $mode];
$map1[] = ['a.del', '=', 0];
$map2 = [];
if (!empty($title)) {
$map2[] = ['u.real_name','like','%'.$title.'%'];
}
$list = Db::name('patrol_task_user')
->alias('b')
->join('patrol_task a','a.id=b.patrol_task_id')
->join('user u','b.user_id=u.id')
->where($map1)
->where($map2)
->group('b.user_id')
->distinct(true)
->field('b.user_id,u.real_name')
->select();
foreach ($list as $k => $v) {
$list[$k]['title'] = $v['real_name'];
$list[$k]['c1'] = Db::name('patrol_task')
->alias('a')
->join('patrol_task_user b','a.id=b.patrol_task_id')
->where($map1)
->where('b.user_id',$v['user_id'])
->where('a.status',0)
->count();
$list[$k]['c2'] = Db::name('patrol_task')
->alias('a')
->join('patrol_task_user b','a.id=b.patrol_task_id')
->where($map1)
->where('b.user_id',$v['user_id'])
->where('a.status',1)
->count();
$list[$k]['c3'] = Db::name('patrol_task')
->alias('a')
->join('patrol_task_user b','a.id=b.patrol_task_id')
->where($map1)
->where('b.user_id',$v['user_id'])
->where('a.status',2)
->count();
$list[$k]['c4'] = Db::name('patrol_task')
->alias('a')
->join('patrol_task_user b','a.id=b.patrol_task_id')
->where($map1)
->where('b.user_id',$v['user_id'])
->where('a.status',3)
->count();
$list[$k]['c5'] = Db::name('patrol_task')
->alias('a')
->join('patrol_task_user b','a.id=b.patrol_task_id')
->where($map1)
->where('b.user_id',$v['user_id'])
->where('a.status',5)
->count();
$list[$k]['c6'] = Db::name('patrol_task')
->alias('a')
->join('patrol_task_user b','a.id=b.patrol_task_id')
->where($map1)
->where('b.user_id',$v['user_id'])
->where('a.status',6)
->count();
}
return $list;
}
public function tjExport() {
$cur = date('Y-m-d');
$start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
$end = input('end', date('Y-m-d'));
$start1 = $start . ' 00:00:00';
$end1 = $end . ' 23:59:59';
$type = input('title');
$mode = input('mode');
$ret = $this->tjData($start1, $end1, $type,$mode);
$modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
$filename = $modeName.'任务统计_' . date('YmdHis', time()) . '.xls';
$header = [
['title' => '编号', 'name' => 'user_id','width'=>'30'],
['title' => '姓名', 'name' => 'real_name','width'=>'10'],
['title' => '未执行', 'name' => 'c1','width'=>'10'],
['title' => '执行中', 'name' => 'c2','width'=>'10'],
['title' => '已完成', 'name' => 'c3','width'=>'10'],
['title' => '未完成', 'name' => 'c4','width'=>'10'],
['title' => '中断', 'name' => 'c5','width'=>'10'],
['title' => '已关闭', 'name' => 'c6','width'=>'10'],
];
ExcelUtil::export($filename,$header,$ret);
}
public function tjList(){
$userId = input('userId', 0);
$status = input('status');
$start = input('start','','trim');
$end = input('end', '','trim');
$start1 = $start . ' 00:00:00';
$end1 = $end . ' 23:59:59';
$patrol_mode = input('patrol_mode/d', 1);
if(request()->isAjax()) {
$month= input('month','','trim');
if($month != ''){
$map[] = ['a.create_yyyymm','=',$month];
}
$title = input('title','','trim');
if($title){
$map[] = ['a.title','like','%'.$title.'%'];
}
$is_exception = input('is_exception','','trim');
if($is_exception != ''){
$map[] = ['a.is_exception','=',$is_exception];
}
$plan_id = input('plan_id','','trim');
if($plan_id != ''){
$map[] = ['a.plan_id','=',$plan_id];
}
$map1[] = ['a.start_time', '>=', $start1];
$map1[] = ['a.start_time', '<=', $end1];
$map1[] = ['a.org_id', '=', $this->orgId];
$map1[] = ['a.patrol_mode', '=', $patrol_mode];
$map1[] = ['a.status', '=', $status];
$map1[] = ['a.del', '=', 0];
$length = input('rows', 10, 'intval'); //每页条数
$page = input('page', 1, 'intval'); //第几页
$start = ($page - 1) * $length; //分页开始位置
$lists = Db::name('patrol_task')
->alias('a')
->join('patrol_task_user b', 'a.id=b.patrol_task_id')
->where($map1)
->where('b.user_id', $userId)
->field('a.*')
->limit($start, $length)
->order(['a.id' => 'desc'])
->select();
foreach ($lists as $k => $v) {
$lists[$k]['start_time'] = date('Y-m-d H:i', strtotime($v['start_time']));
$lists[$k]['end_time'] = date('Y-m-d H:i', strtotime($v['end_time']));
$taskUser = Db::name('patrol_task_user')
->alias('ptu')
->field('u.real_name')
->join('user u', 'u.id=ptu.user_id')
->where('ptu.patrol_task_id', $v['id'])
->where('ptu.user_id', $userId)
->find();
$lists[$k]['task_user'] = $taskUser['real_name'];
}
$totalCount = Db::name('patrol_task')
->alias('a')
->join('patrol_task_user b', 'a.id=b.patrol_task_id')
->where($map1)
->where('b.user_id', $userId)
->count();
$totalPage = ceil($totalCount / $length);
$result['page'] = $page;
$result['total'] = $totalPage;
$result['records'] = $totalCount;
$result['rows'] = $lists;
return json($result);
}else{
$plans = model('PatrolPlan')->getListByMode($this->orgId,$patrol_mode);
$this->assign('plans',$plans);
$this->assign('userId',$userId);
$this->assign('status',$status);
$this->assign('start',$start);
$this->assign('end',$end);
$this->assign('patrol_mode',$patrol_mode);
$user = Db::name('user')
->where('id', $userId)
->value('real_name');
$this->assign('meta_title','['.$user.']任务列表');
return $this->fetch();
}
}
public function resultRecord($mode){
if(request()->isAjax()){
//分页参数
$length = input('rows',10,'intval'); //每页条数
$page = input('page',1,'intval'); //第几页
$start = ($page - 1) * $length; //分页开始位置
//排序
$sortRow = input('sidx','sort','trim'); //排序列
$sort = input('sord','asc','trim'); //排序方式
$order = $sortRow.' '.$sort.' ,id desc';
$addrTitle = input('addrTitle','','trim');
if($addrTitle !=''){
$addrIds = Db::name('address')->whereLike('title','%'.$addrTitle.'%')->column('id');
$map[] = ['pta.address_id','in',$addrIds];
}
$map[] = ['pt.org_id','=',$this->orgId];
$map[] =['pt.patrol_mode','=',$mode];
$map= empty($map) ? true: $map;
//数据查询
// $lists = Db::name('patrol_record')
// ->where($map)
// ->limit($start,$length)
// ->order('id desc')
// ->group('patrol_addr_id')
// ->select();
$lists = Db::name('patrol_task_addr')
->alias('pta')
->join('patrol_task pt','pt.id=pta.patrol_task_id')
->where($map)
->field('pta.*,pt.patrol_mode')
->order('pta.id desc')
->limit($start,$length)
->group('pta.address_id')
->select();
foreach ($lists as $k=>$v){
$lists[$k]['addr'] = Db::name('address')->where('id',$v['address_id'])->value('title');
$lists[$k]['num'] = Db::name('patrol_record')
->where('patrol_addr_id',$v['address_id'])
->where('patrol_mode',$mode)
->count();
}
$totalCount = Db::name('patrol_task_addr')
->alias('pta')
->join('patrol_task pt','pt.id=pta.patrol_task_id')
->where($map)
->field('pta.*')
->order('pta.id desc')
->group('pta.address_id')
->count();
//数据返回
// $totalCount = Db::name('patrol_record')
// ->where($map)
// ->limit($start,$length)
// ->order('id desc')
// ->group('patrol_addr_id')
// ->count();
$totalPage = ceil($totalCount/$length);
$result['page'] = $page;
$result['total'] = $totalPage;
$result['records'] = $totalCount;
$result['rows'] = $lists;
return json($result);
}else{
$modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
$this->assign('mode',$mode);
$this->assign('meta_title',$modeName.'结果查看');
return $this->fetch();
}
}
public function resultCalendar(){
$mode = input('mode');
$addrId = input('addrId');
$addrName = Db::name('address')->where('id',$addrId)->value('title');
$this->assign('mode',$mode);
$this->assign('addrId',$addrId);
$this->assign('addrName',$addrName);
return $this->fetch();
}
public function taskjsonResult(){
$mode = input('mode');
$addrId = input('addrId');
$start = input('start');
$end = input('end');
$data = array();
if(!$start||!$end||$start>$end){
header('Content-Type:application/json; charset=utf-8');
exit(json_encode($data));
}
$start = date('Y-m-d H:i:s',strtotime($start));
$end = date('Y-m-d H:i:s',strtotime($end));
$list = Db::name('patrol_record')
->where('patrol_addr_id',$addrId)
->where('patrol_mode',$mode)
->where('org_id',$this->orgId)
->where('create_time','>=',$start)
->where('create_time','<=',$end)
->select();
foreach ($list as $k=>$v){
$name = DB::name('user')->where('id',$v['user_id'])->value('real_name');
$time = date('H:i',strtotime($v['create_time']));
$arr = array(
'id'=>$v['id'],
'title'=> $time.' ! '.$name,
'start'=>date('Y-m-d H:i',strtotime($v['create_time'])),
);
$data[] = $arr;
}
header('Content-Type:application/json; charset=utf-8');
exit(json_encode($data));
}
public function addrWork($mode){
$day = input('day',date('Y-m-d'),'trim');
if(request()->isAjax()){
//分页参数
$length = input('rows',10,'intval'); //每页条数
$page = input('page',1,'intval'); //第几页
$start = ($page - 1) * $length; //分页开始位置
//排序
$sortRow = input('sidx','sort','trim'); //排序列
$sort = input('sord','asc','trim'); //排序方式
$order = $sortRow.' '.$sort.' ,id desc';
$first = 1;
$w = date('w',strtotime($day));
$wst = date('Ymd',strtotime("$day -" . ($w ? $w - $first : 6) . ' days'));
$wet = date('Ymd',strtotime("$day +6 days"));
$zuo = date('Ymd',strtotime($day)-86400);
$you = date('Ymd',strtotime($day)+86400);
$st = $day.' 00:00:00';
$et = $day.' 23:59:59';
$map[] = ['pt.org_id','=',$this->orgId];
$map[] =['pt.patrol_mode','=',$mode];
$map[] =['pt.start_time','>=',$st];
$map[] =['pt.start_time','<=',$et];
$map= empty($map) ? true: $map;
$lists = Db::name('patrol_task_addr')
->alias('pta')
->join('patrol_task pt','pt.id=pta.patrol_task_id')
->where($map)
->field('pta.*,pt.patrol_mode')
->limit($start,$length)
->order('pta.id desc')
->group('pta.address_id')
->select();
foreach ($lists as $k=>$v){
$lists[$k]['addr'] = Db::name('address')->where('id',$v['address_id'])->value('title');
$taskIds = Db::name('patrol_task_addr')->where('address_id',$v['address_id'])->column('patrol_task_id');
$lists[$k]['count'] = Db::name('patrol_task')
->whereIn('id',$taskIds)
->where('patrol_mode',$v['patrol_mode'])
->count();
$lists[$k]['num'] = Db::name('patrol_task')
->whereIn('id',$taskIds)
->where('patrol_mode',$v['patrol_mode'])
->where('create_yyyymm',date('Ym',strtotime($day)))
->count();
$lists[$k]['num1'] = Db::name('patrol_task')
->whereIn('id',$taskIds)
->where('patrol_mode',$v['patrol_mode'])
->where('create_yyyymmdd','>=',$wst)
->where('create_yyyymmdd','<=',$wet)
->count();
$lists[$k]['num2'] = Db::name('patrol_task')
->whereIn('id',$taskIds)
->where('patrol_mode',$v['patrol_mode'])
->where('create_yyyymmdd','=',date('Ymd',strtotime($day)))
->count();
$lists[$k]['num3'] = Db::name('patrol_task')
->whereIn('id',$taskIds)
->where('patrol_mode',$v['patrol_mode'])
->where('create_yyyymmdd','=',$zuo)
->where('status','in',[0,1,3,5,6])
->count();
$lists[$k]['num4'] = Db::name('patrol_task')
->whereIn('id',$taskIds)
->where('patrol_mode',$v['patrol_mode'])
->where('create_yyyymmdd','=',date('Ymd',strtotime($day)))
->where('status','in',[0,1,3,5,6])
->count();
}
$totalCount = Db::name('patrol_task_addr')
->alias('pta')
->join('patrol_task pt','pt.id=pta.patrol_task_id')
->where($map)
->field('pta.*,pt.patrol_mode')
->order('pta.id desc')
->group('pta.address_id')
->count();
$totalPage = ceil($totalCount/$length);
$result['page'] = $page;
$result['total'] = $totalPage;
$result['records'] = $totalCount;
$result['rows'] = $lists;
return json($result);
}else{
$modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
$this->assign('mode',$mode);
$this->assign('day',$day);
$this->assign('meta_title',$modeName.'地点工作量统计');
return $this->fetch();
}
}
public function addrWorkExport(){
$day = input('day',date('Y-m-d'),'trim');
$mode = input('mode');
$first = 1;
$w = date('w',strtotime($day));
$wst = date('Ymd',strtotime("$day -" . ($w ? $w - $first : 6) . ' days'));
$wet = date('Ymd',strtotime("$day +6 days"));
$zuo = date('Ymd',strtotime($day)-86400);
$you = date('Ymd',strtotime($day)+86400);
$st = $day.' 00:00:00';
$et = $day.' 23:59:59';
$map[] = ['pt.org_id','=',$this->orgId];
$map[] =['pt.patrol_mode','=',$mode];
$map[] =['pt.start_time','>=',$st];
$map[] =['pt.start_time','<=',$et];
$map= empty($map) ? true: $map;
$lists = Db::name('patrol_task_addr')
->alias('pta')
->join('patrol_task pt','pt.id=pta.patrol_task_id')
->where($map)
->field('pta.*,pt.patrol_mode')
->order('pta.id desc')
->group('pta.address_id')
->select();
foreach ($lists as $k=>$v){
$lists[$k]['addr'] = Db::name('address')->where('id',$v['address_id'])->value('title');
$lists[$k]['count'] = Db::name('patrol_task')
->where('id',$v['patrol_task_id'])
->where('patrol_mode',$v['patrol_mode'])
->count();
$lists[$k]['num'] = Db::name('patrol_task')
->where('id',$v['patrol_task_id'])
->where('patrol_mode',$v['patrol_mode'])
->where('create_yyyymm',date('Ym',strtotime($day)))
->count();
$lists[$k]['num1'] = Db::name('patrol_task')
->where('id',$v['patrol_task_id'])
->where('patrol_mode',$v['patrol_mode'])
->where('create_yyyymmdd','>=',$wst)
->where('create_yyyymmdd','<=',$wet)
->count();
$lists[$k]['num2'] = Db::name('patrol_task')
->where('id',$v['patrol_task_id'])
->where('patrol_mode',$v['patrol_mode'])
->where('create_yyyymmdd','=',date('Ymd',strtotime($day)))
->count();
$lists[$k]['num3'] = Db::name('patrol_task')
->where('id',$v['patrol_task_id'])
->where('patrol_mode',$v['patrol_mode'])
->where('create_yyyymmdd','=',$zuo)
->where('status','in',[0,1,3,5,6])
->count();
$lists[$k]['num4'] = Db::name('patrol_task')
->where('id',$v['patrol_task_id'])
->where('patrol_mode',$v['patrol_mode'])
->where('create_yyyymmdd','=',date('Ymd',strtotime($day)))
->where('status','in',[0,1,3,5,6])
->count();
}
$modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
$filename = $modeName.'地点工作量统计_' . date('YmdHis', time()) . '.xls';
$header = [
['title' => '地点', 'name' => 'addr','width'=>'10'],
['title' => '总数', 'name' => 'count','width'=>'10'],
['title' => '本月', 'name' => 'num','width'=>'10'],
['title' => '本周', 'name' => 'num1','width'=>'10'],
['title' => '今天', 'name' => 'num2','width'=>'10'],
['title' => '昨日未完成', 'name' => 'num3','width'=>'10'],
['title' => '今日未完成', 'name' => 'num4','width'=>'10'],
];
ExcelUtil::export($filename,$header,$lists);
}
}