GreenAddr.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace app\common\model;
  3. use app\common\util\AppAuth;
  4. use app\hander\HelpHander;
  5. use think\Db;
  6. class GreenAddr extends Base {
  7. protected $createTime = 'create_time';
  8. protected $updateTime = 'update_time';
  9. public $table = 'green_addr';
  10. protected $validateName = 'greenAddr';
  11. public function updates(){
  12. $data = request()->post();
  13. $data['org_id'] =cur_org_id();
  14. $result = validate($this->validateName)->check($data,[],'');
  15. if(true !== $result){
  16. $this->error = validate($this->validateName)->getError();
  17. return false;
  18. }
  19. if(!$data['user_ids']){
  20. $this->error='执行人员不能为空';
  21. return false;
  22. }
  23. $post['user_ids'] = explode(',',$data['user_ids']);
  24. $id = $data['id'];
  25. unset($data['id']);
  26. unset($data['user_ids']);
  27. $this->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. if(!$ret){
  33. exception('保存失败');
  34. }
  35. Db::name('green_user')
  36. ->where('green_addr_id',$id)->delete();
  37. //添加检查人员
  38. $user = array();
  39. $post['user_ids'] = array_unique($post['user_ids']);
  40. foreach ($post['user_ids'] as $k=>$v){
  41. $user[] = array(
  42. 'green_addr_id' => $id,
  43. 'user_id' => $v
  44. );
  45. }
  46. $taskUser=Db::name('green_user')->insertAll($user);
  47. if(!$taskUser){
  48. exception('工作人员保存失败');
  49. }
  50. }else{
  51. $data['create_time'] = date('Y-m-d H:i:s');
  52. $ret = $this->insertGetId($data);
  53. $id = $ret;
  54. if(!$id){
  55. exception('保存失败');
  56. }
  57. //添加检查人员
  58. $user = array();
  59. $post['user_ids'] = array_unique($post['user_ids']);
  60. foreach ($post['user_ids'] as $k=>$v){
  61. $user[] = array(
  62. 'green_addr_id' => $id,
  63. 'user_id' => $v
  64. );
  65. }
  66. $taskUser=Db::name('green_user')->insertAll($user);
  67. if(!$taskUser){
  68. exception('工作人员保存失败');
  69. }
  70. }
  71. $this->commit();
  72. return true;
  73. }catch (\Exception $e){
  74. // 回滚事务
  75. $this->error = $e->getMessage();
  76. $this->rollback();
  77. return false;
  78. }
  79. }
  80. public function getRoles($roles){
  81. $list = Db::name('roles')
  82. ->where('id','in',explode(',',$roles))
  83. ->column('name');
  84. return $list;
  85. }
  86. public function getRolesList(){
  87. $list = (new WorkTypeMode())->getRoles(9);
  88. $arr = [];
  89. if(!empty($list)){
  90. foreach ($list as $k=>$v){
  91. $arr[] = [
  92. 'id'=>$v['id'],
  93. 'title'=>$v['name'],
  94. ];
  95. }
  96. }
  97. return $arr;
  98. }
  99. public function list(){
  100. $list = $this
  101. ->where('del',0)
  102. ->where('enable',1)
  103. ->where('org_id',cur_org_id())
  104. ->select()
  105. ->toArray();
  106. return $list;
  107. }
  108. public function sweepCode($code,$orgId,$userId,$taskId=0,$greenAddrId=0)
  109. {
  110. $qrcode_arr = get_qrcode_arr($code);
  111. if(!$qrcode_arr){
  112. $this->error = '该地点不存在';
  113. return false;
  114. }
  115. if($qrcode_arr['type'] != 'green'){
  116. $this->error = '该任务不存在';
  117. return false;
  118. }
  119. if($taskId > 0 ){
  120. if($qrcode_arr['id'] != $greenAddrId){
  121. $this->error = '地点不正确';
  122. return false;
  123. }
  124. $task = Db::name('green_task')
  125. ->where('id',$taskId)
  126. ->where('del',0)
  127. ->where('org_id',$orgId)
  128. ->find();
  129. if($task['end_time'] < date('Y-m-d H:i:s') || $task['start_time'] > date('Y-m-d H:i:s')){
  130. $this->error = '不在时间范围内';
  131. return false;
  132. }
  133. $workerAuth = 1;
  134. }else{
  135. $green =Db::name('green_user')
  136. ->where('green_addr_id',$qrcode_arr['id'])
  137. ->where('user_id',$userId)
  138. ->find();
  139. $workerAuth = $green?1:0;
  140. }
  141. $green = $this->where([
  142. 'org_id' => $orgId,
  143. 'id' => $qrcode_arr['id'],
  144. 'del' => 0,
  145. 'enable' => 1
  146. ])->find();
  147. if (!$green) {
  148. $this->error = '该任务不存在';
  149. return false;
  150. }
  151. $lookAllAuth = (new Roles())->getAppAuth($userId,AppAuth::GREEN_LOOK_ALL_AUTH);
  152. $ret = [
  153. 'green_addr_id' => $qrcode_arr['id'],
  154. 'worker_auth' => $workerAuth,
  155. 'look_all_auth' => $lookAllAuth?1:0,
  156. ];
  157. return $ret;
  158. }
  159. public function taskAddrAll($taskId){
  160. $info = Db::name('green_task')
  161. ->field('id,start_time,end_time,title,status,create_time')
  162. ->where('id',$taskId)
  163. ->where('del',0)
  164. ->find();
  165. $info['users'] = Db::name('user')
  166. ->alias('u')
  167. ->field('u.real_name')
  168. ->join('green_task_user dtu','dtu.user_id=u.id')
  169. ->where('dtu.task_id',$info['id'])
  170. ->where('u.del',0)
  171. ->where('u.enable',1)
  172. ->select();
  173. $info['green'] = Db::name('green')
  174. ->alias('d')
  175. ->field('d.id,d.title')
  176. ->join('green_task_addr dta','dta.green_id=d.id')
  177. ->where('dta.task_id',$info['id'])
  178. ->where('d.del',0)
  179. ->where('d.enable',1)
  180. ->select();
  181. foreach ($info['green'] as $k=>$v){
  182. $record = Db::name('green_record')
  183. ->where('task_id',$taskId)
  184. ->where('green_id',$v['id'])
  185. ->find();
  186. $info['green'][$k]['status'] = $record ? 1 : 0;
  187. $info['green'][$k]['green_record_id'] = $record ? $record['id'] : 0;
  188. }
  189. return $info;
  190. }
  191. public function myTaskRecord($page,$size,$userId,$orgId){
  192. $record = Db::name('green_task_user')
  193. ->alias('a')
  194. ->join('green_task b','a.task_id = b.id')
  195. ->where('b.del',0)
  196. ->whereIn('b.status',[2,3])
  197. ->where('a.user_id',$userId)
  198. ->where('b.org_id',$orgId)
  199. ->field('b.id,b.title,b.start_time,b.end_time,b.status')
  200. ->order('b.start_time desc,id desc')
  201. ->page($page,$size)
  202. ->select();
  203. return $record?$record:[];
  204. }
  205. /**
  206. *日常工作量统计
  207. *
  208. * @author wst
  209. * @date 2021/4/13 8:38
  210. */
  211. public function DailyWork($orgId){
  212. $list = $this
  213. ->field('id,title')
  214. ->where('enable',1)
  215. ->where('del',0)
  216. ->where('org_id',$orgId)
  217. ->select();
  218. $list = $list?$list:array();
  219. $count = $count1 = $count2 =$count3 = 0;
  220. foreach ($list as $k=>$v){
  221. $query = Db::name('green_record')
  222. ->where('org_id',$orgId)
  223. ->where('green_id',$v['id']);
  224. $list[$k]['count'] = $query
  225. ->count();
  226. $count += $list[$k]['count'];
  227. //本月
  228. $list[$k]['count1'] = $query
  229. ->where('create_yyyymm',date('Ym'))
  230. ->count();
  231. $count1 += $list[$k]['count1'];
  232. //本周
  233. $list[$k]['count2'] = $query
  234. ->whereIn('create_yyyymmdd',get_week_date())
  235. ->count();
  236. $count2 += $list[$k]['count2'];
  237. //今天
  238. $list[$k]['count3'] = $query
  239. ->where('create_yyyymmdd',date('Ymd'))
  240. ->count();
  241. $count3 += $list[$k]['count3'];
  242. }
  243. return array('list'=>$list,'count'=>$count,'count1'=>$count1,'count2'=>$count2,'count3'=>$count3);
  244. }
  245. // 获取网格化时间段
  246. public function getTimeDaily($orgId){
  247. $hour = model('Config')->getConfig('org_green_hour',$orgId);
  248. $hour = $hour?$hour:24; // 默认24小时刷新一次
  249. $cur = time();
  250. $s = strtotime(date('Y-m-d'));
  251. $n = $s + 86400;
  252. $start = 0;
  253. $end = 0;
  254. while (true){
  255. if($s > $cur){
  256. $start = $s - $hour*60*60;
  257. $end = $s;
  258. if($s > $n){
  259. $end = $n;
  260. }
  261. break;
  262. }
  263. $s = $s + $hour*60*60;
  264. }
  265. return ['start' => date('Y-m-d H:i:s',$start),'end' => date('Y-m-d H:i:s',$end)];
  266. }
  267. }