Daily.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace app\common\model;
  3. use app\common\util\AppAuth;
  4. use app\hander\HelpHander;
  5. use think\Db;
  6. class Daily extends Base {
  7. protected $createTime = 'create_time';
  8. protected $updateTime = 'update_time';
  9. public $table = 'daily';
  10. protected $validateName = 'Daily';
  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('daily_user')
  36. ->where('daily_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. 'daily_id' => $id,
  43. 'user_id' => $v
  44. );
  45. }
  46. $taskUser=Db::name('daily_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. 'daily_id' => $id,
  63. 'user_id' => $v
  64. );
  65. }
  66. $taskUser=Db::name('daily_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($orgId,$code, $userId,$taskId=0,$dailyId=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'] != 'daily'){
  116. $this->error = '该任务不存在';
  117. return false;
  118. }
  119. if($qrcode_arr['ucode'] != config('app.ucode')){
  120. $this->error = '该任务不存在';
  121. return false;
  122. }
  123. if($taskId > 0 ){
  124. if($qrcode_arr['id'] != $dailyId){
  125. $this->error = '地点不正确';
  126. return false;
  127. }
  128. $task = Db::name('daily_task')
  129. ->where('id',$taskId)
  130. ->where('del',0)
  131. ->where('org_id',$orgId)
  132. ->find();
  133. if($task['end_time'] < date('Y-m-d H:i:s') || $task['start_time'] > date('Y-m-d H:i:s')){
  134. $this->error = '不在时间范围内';
  135. return false;
  136. }
  137. // $taskAddr = Db::name('daily_task_addr')
  138. // ->where('task_id',$task['id'])
  139. // ->column('daily_id');
  140. // if(!in_array($qrcode_arr['id'],$taskAddr)){
  141. // $this->error = '此任务下没有该地点';
  142. // return false;
  143. // }
  144. $workerAuth = 1;
  145. }else{
  146. $daily =Db::name('daily_user')
  147. ->where('daily_id',$qrcode_arr['id'])
  148. ->where('user_id',$userId)
  149. ->find();
  150. $workerAuth = $daily?1:0;
  151. }
  152. $daily = $this->where([
  153. 'org_id' => $orgId,
  154. 'id' => $qrcode_arr['id'],
  155. 'del' => 0,
  156. 'enable' => 1
  157. ])->find();
  158. if (!$daily) {
  159. $this->error = '该任务不存在';
  160. return false;
  161. }
  162. $lookAllAuth = (new Roles())->getAppAuth($userId,AppAuth::DAILY_LOOK_ALL_AUTH);
  163. $ret = [
  164. 'daily_id' => $qrcode_arr['id'],
  165. 'worker_auth' => $workerAuth,
  166. 'look_all_auth' => $lookAllAuth?1:0
  167. ];
  168. return $ret;
  169. }
  170. //日常工作任务检查项
  171. public function dailyInfo($id,$orgId){
  172. $daily = $this
  173. ->where([
  174. 'org_id' => $orgId,
  175. 'id' => $id,
  176. 'del' => 0,
  177. 'enable' => 1,
  178. ])->find();
  179. if (!$daily) {
  180. $this->error = '该任务不存在';
  181. return false;
  182. }
  183. $formId = empty($daily['daily_form']) ? [] : explode(',', $daily['daily_form']);
  184. $dailyForm = Db::name('daily_form')
  185. ->where(['del' => 0, 'enable' => 1])
  186. ->where('id','in', $formId)
  187. ->select();
  188. $daily['daily_form'] = $dailyForm;
  189. return $daily;
  190. }
  191. public function taskAddrAll($taskId){
  192. $info = Db::name('daily_task')
  193. ->field('id,start_time,end_time,title,status,create_time')
  194. ->where('id',$taskId)
  195. ->where('del',0)
  196. ->find();
  197. $info['users'] = Db::name('user')
  198. ->alias('u')
  199. ->field('u.real_name')
  200. ->join('daily_task_user dtu','dtu.user_id=u.id')
  201. ->where('dtu.task_id',$info['id'])
  202. ->where('u.del',0)
  203. ->where('u.enable',1)
  204. ->select();
  205. $info['daily'] = Db::name('daily')
  206. ->alias('d')
  207. ->field('d.id,d.title')
  208. ->join('daily_task_addr dta','dta.daily_id=d.id')
  209. ->where('dta.task_id',$info['id'])
  210. ->where('d.del',0)
  211. ->where('d.enable',1)
  212. ->select();
  213. foreach ($info['daily'] as $k=>$v){
  214. $record = Db::name('daily_record')
  215. ->where('task_id',$taskId)
  216. ->where('daily_id',$v['id'])
  217. ->find();
  218. $info['daily'][$k]['status'] = $record ? 1 : 0;
  219. $info['daily'][$k]['daily_record_id'] = $record ? $record['id'] : 0;
  220. }
  221. return $info;
  222. }
  223. public function myTaskRecord($page,$size,$userId,$orgId){
  224. $record = Db::name('daily_task_user')
  225. ->alias('a')
  226. ->join('daily_task b','a.task_id = b.id')
  227. ->where('b.del',0)
  228. ->whereIn('b.status',[2,3])
  229. ->where('a.user_id',$userId)
  230. ->where('b.org_id',$orgId)
  231. ->field('b.id,b.title,b.start_time,b.end_time,b.status')
  232. ->order('b.start_time desc,id desc')
  233. ->page($page,$size)
  234. ->select();
  235. return $record?$record:[];
  236. }
  237. /**
  238. *日常工作量统计
  239. *
  240. * @author wst
  241. * @date 2021/4/13 8:38
  242. */
  243. public function DailyWork($orgId){
  244. $list = $this
  245. ->field('id,title')
  246. ->where('enable',1)
  247. ->where('del',0)
  248. ->where('org_id',$orgId)
  249. ->select();
  250. $list = $list?$list:array();
  251. $count = $count1 = $count2 =$count3 = 0;
  252. foreach ($list as $k=>$v){
  253. $query = Db::name('daily_record')
  254. ->where('org_id',$orgId)
  255. ->where('daily_id',$v['id']);
  256. $list[$k]['count'] = $query
  257. ->count();
  258. $count += $list[$k]['count'];
  259. //本月
  260. $list[$k]['count1'] = $query
  261. ->where('create_yyyymm',date('Ym'))
  262. ->count();
  263. $count1 += $list[$k]['count1'];
  264. //本周
  265. $list[$k]['count2'] = $query
  266. ->whereIn('create_yyyymmdd',get_week_date())
  267. ->count();
  268. $count2 += $list[$k]['count2'];
  269. //今天
  270. $list[$k]['count3'] = $query
  271. ->where('create_yyyymmdd',date('Ymd'))
  272. ->count();
  273. $count3 += $list[$k]['count3'];
  274. }
  275. return array('list'=>$list,'count'=>$count,'count1'=>$count1,'count2'=>$count2,'count3'=>$count3);
  276. }
  277. // 获取网格化时间段
  278. public function getTimeDaily($orgId){
  279. $hour = model('Config')->getConfig('org_daily_hour',$orgId);
  280. $hour = $hour?$hour:24; // 默认24小时刷新一次
  281. $cur = time();
  282. $s = strtotime(date('Y-m-d'));
  283. $n = $s + 86400;
  284. $start = 0;
  285. $end = 0;
  286. while (true){
  287. if($s > $cur){
  288. $start = $s - $hour*60*60;
  289. $end = $s;
  290. if($s > $n){
  291. $end = $n;
  292. }
  293. break;
  294. }
  295. $s = $s + $hour*60*60;
  296. }
  297. return ['start' => date('Y-m-d H:i:s',$start),'end' => date('Y-m-d H:i:s',$end)];
  298. }
  299. }