0
0

Daily.php 12 KB

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