123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- <?php
- namespace app\common\model;
- use app\common\util\AppAuth;
- use app\hander\HelpHander;
- use think\Db;
- class Daily extends Base {
- protected $createTime = 'create_time';
- protected $updateTime = 'update_time';
- public $table = 'daily';
- protected $validateName = 'Daily';
- public function updates(){
- $data = request()->post();
- $data['org_id'] =cur_org_id();
- $result = validate($this->validateName)->check($data,[],'');
- if(true !== $result){
- $this->error = validate($this->validateName)->getError();
- return false;
- }
- if(!$data['user_ids']){
- $this->error='检查人员不能为空';
- return false;
- }
- $post['user_ids'] = explode(',',$data['user_ids']);
- $id = $data['id'];
- unset($data['id']);
- unset($data['user_ids']);
- $this->startTrans();
- try{
- if($id > 0){
- $data['update_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- if(!$ret){
- exception('保存失败');
- }
- Db::name('daily_user')
- ->where('daily_id',$id)->delete();
- //添加检查人员
- $user = array();
- $post['user_ids'] = array_unique($post['user_ids']);
- foreach ($post['user_ids'] as $k=>$v){
- $user[] = array(
- 'daily_id' => $id,
- 'user_id' => $v
- );
- }
- $taskUser=Db::name('daily_user')->insertAll($user);
- if(!$taskUser){
- exception('工作人员保存失败');
- }
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- $ret = $this->insertGetId($data);
- $id = $ret;
- if(!$id){
- exception('保存失败');
- }
- //添加检查人员
- $user = array();
- $post['user_ids'] = array_unique($post['user_ids']);
- foreach ($post['user_ids'] as $k=>$v){
- $user[] = array(
- 'daily_id' => $id,
- 'user_id' => $v
- );
- }
- $taskUser=Db::name('daily_user')->insertAll($user);
- if(!$taskUser){
- exception('工作人员保存失败');
- }
- }
- $this->commit();
- return true;
- }catch (\Exception $e){
- // 回滚事务
- $this->error = $e->getMessage();
- $this->rollback();
- return false;
- }
- }
- public function getRoles($roles){
- $list = Db::name('roles')
- ->where('id','in',explode(',',$roles))
- ->column('name');
- return $list;
- }
- public function getRolesList($orgId){
- $list = (new WorkTypeMode())->getRoles(9,$orgId);
- $arr = [];
- if(!empty($list)){
- foreach ($list as $k=>$v){
- $arr[] = [
- 'id'=>$v['id'],
- 'title'=>$v['name'],
- ];
- }
- }
- return $arr;
- }
- public function list(){
- $list = $this
- ->where('del',0)
- ->where('enable',1)
- ->where('org_id',cur_org_id())
- ->select()
- ->toArray();
- return $list;
- }
- public function sweepCode($orgId,$code, $userId,$taskId=0,$dailyId=0)
- {
- $qrcode_arr = get_qrcode_arr($code);
- if(!$qrcode_arr){
- $this->error = '该任务不存在';
- return false;
- }
- if(!in_array($qrcode_arr['type'],['address','daily'])){
- $this->error = '二维码不正确';
- return false;
- }
- // if($qrcode_arr['type'] != 'daily'){
- // $this->error = '该任务不存在';
- // return false;
- // }
- if($qrcode_arr['ucode'] != config('app.ucode')){
- $this->error = '该任务不存在';
- return false;
- }
- if($qrcode_arr['type']=='address'){
- $addressId = $qrcode_arr['id'];
- $qrcode_arr['id'] =Db::name('daily')
- ->where('address_id',$addressId)
- ->where('del',0)
- ->value('id');
- if(empty($qrcode_arr['id'])){
- $this->error = '当前地点未绑定日常工作';
- return false;
- }
- }
- if($taskId > 0 ){
- if($qrcode_arr['id'] != $dailyId){
- $this->error = '地点不正确';
- return false;
- }
- $task = Db::name('daily_task')
- ->where('id',$taskId)
- ->where('del',0)
- ->where('org_id',$orgId)
- ->find();
- if($task['end_time'] < date('Y-m-d H:i:s') || $task['start_time'] > date('Y-m-d H:i:s')){
- $this->error = '不在时间范围内';
- return false;
- }
- // $taskAddr = Db::name('daily_task_addr')
- // ->where('task_id',$task['id'])
- // ->column('daily_id');
- // if(!in_array($qrcode_arr['id'],$taskAddr)){
- // $this->error = '此任务下没有该地点';
- // return false;
- // }
- $workerAuth = 1;
- }else{
- $daily =Db::name('daily_user')
- ->where('daily_id',$qrcode_arr['id'])
- ->where('user_id',$userId)
- ->find();
- $workerAuth = $daily?1:0;
- }
- if($qrcode_arr['type']=='address'){
- $map[] = ['org_id','=',$orgId];
- $map[] = ['id','=',$addressId];
- $map[] = ['del','=',0];
- $map[] = ['enable','=',1];
- // if($addressId != $ysid){
- // $map[] = ['del','=',$addressId];
- // $map[] = ['enable','=',$addressId];
- // }
- $daily =Db::name('address')->where($map)->find();
- }else{
- $daily =Db::name('daily')->where([
- 'org_id' => $orgId,
- 'id' => $qrcode_arr['id'],
- 'del' => 0,
- 'enable' => 1
- ])->find();
- }
- if (!$daily) {
- $this->error = '该任务不存在';
- return false;
- }
- $lookAllAuth = (new Roles())->getAppAuth($userId,AppAuth::DAILY_LOOK_ALL_AUTH);
- $ret = [
- 'daily_id' => $qrcode_arr['id'],
- 'worker_auth' => $workerAuth,
- 'look_all_auth' => $lookAllAuth?1:0
- ];
- return $ret;
- }
- //日常工作任务检查项
- public function dailyInfo($id,$orgId){
- $daily = $this
- ->where([
- 'org_id' => $orgId,
- 'id' => $id,
- 'del' => 0,
- 'enable' => 1,
- ])->find();
- if (!$daily) {
- $this->error = '该任务不存在';
- return false;
- }
- $formId = empty($daily['daily_form']) ? [] : explode(',', $daily['daily_form']);
- $dailyForm = Db::name('daily_form')
- ->where(['del' => 0, 'enable' => 1])
- ->where('id','in', $formId)
- ->select();
- $daily['title'] = Db::name('address')
- ->where('id',$daily['address_id'])
- ->value('title');
- $daily['daily_form'] = $dailyForm;
- return $daily;
- }
- public function taskAddrAll($taskId){
- $info = Db::name('daily_task')
- ->field('id,start_time,end_time,title,status,create_time')
- ->where('id',$taskId)
- ->where('del',0)
- ->find();
- $info['users'] = Db::name('user')
- ->alias('u')
- ->field('u.real_name')
- ->join('daily_task_user dtu','dtu.user_id=u.id')
- ->where('dtu.task_id',$info['id'])
- ->where('u.del',0)
- ->where('u.enable',1)
- ->select();
- $info['daily'] = Db::name('daily')
- ->alias('d')
- ->field('d.id,d.title,d.address_id')
- ->join('daily_task_addr dta','dta.daily_id=d.id')
- ->where('dta.task_id',$info['id'])
- ->where('d.del',0)
- ->where('d.enable',1)
- ->select();
- foreach ($info['daily'] as $k=>$v){
- $record = Db::name('daily_record')
- ->where('task_id',$taskId)
- ->where('daily_id',$v['id'])
- ->find();
- $info['daily'][$k]['title'] = Db::name('address')
- ->where('id',$v['address_id'])
- ->value('title');
- $info['daily'][$k]['status'] = $record ? 1 : 0;
- $info['daily'][$k]['daily_record_id'] = $record ? $record['id'] : 0;
- }
- return $info;
- }
- public function myTaskRecord($page,$size,$userId,$orgId){
- $record = Db::name('daily_task_user')
- ->alias('a')
- ->join('daily_task b','a.task_id = b.id')
- ->where('b.del',0)
- ->whereIn('b.status',[2,3])
- ->where('a.user_id',$userId)
- ->where('b.org_id',$orgId)
- ->field('b.id,b.title,b.start_time,b.end_time,b.status')
- ->order('b.start_time desc,id desc')
- ->page($page,$size)
- ->select();
- return $record?$record:[];
- }
- /**
- *日常工作量统计
- *
- * @author wst
- * @date 2021/4/13 8:38
- */
- public function DailyWork($orgId){
- $list = $this
- ->field('id,title,address_id')
- ->where('enable',1)
- ->where('del',0)
- ->where('org_id',$orgId)
- ->select();
- $list = $list?$list:array();
- $count = $count1 = $count2 =$count3 = 0;
- foreach ($list as $k=>$v){
- $list[$k]['title'] = '';
- if($v['address_id'] >0){
- $list[$k]['title'] = Db::name('address')
- ->where('id',$v['address_id'])
- ->value('title');
- }
- $query = Db::name('daily_record')
- ->where('org_id',$orgId)
- ->where('daily_id',$v['id']);
- $list[$k]['count'] = $query
- ->count();
- $count += $list[$k]['count'];
- //本月
- $list[$k]['count1'] = $query
- ->where('create_yyyymm',date('Ym'))
- ->count();
- $count1 += $list[$k]['count1'];
- //本周
- $list[$k]['count2'] = $query
- ->whereIn('create_yyyymmdd',get_week_date())
- ->count();
- $count2 += $list[$k]['count2'];
- //今天
- $list[$k]['count3'] = $query
- ->where('create_yyyymmdd',date('Ymd'))
- ->count();
- $count3 += $list[$k]['count3'];
- }
- return array('list'=>$list,'count'=>$count,'count1'=>$count1,'count2'=>$count2,'count3'=>$count3);
- }
- // 获取网格化时间段
- public function getTimeDaily($orgId){
- $hour = model('Config')->getConfig('org_daily_hour',$orgId);
- $hour = $hour?$hour:24; // 默认24小时刷新一次
- $cur = time();
- $s = strtotime(date('Y-m-d'));
- $n = $s + 86400;
- $start = 0;
- $end = 0;
- while (true){
- if($s > $cur){
- $start = $s - $hour*60*60;
- $end = $s;
- if($s > $n){
- $end = $n;
- }
- break;
- }
- $s = $s + $hour*60*60;
- }
- return ['start' => date('Y-m-d H:i:s',$start),'end' => date('Y-m-d H:i:s',$end)];
- }
- }
|