Daily.php 12 KB

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