DailyTask.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\Task;
  4. use think\Db;
  5. class DailyTask extends Auth
  6. {
  7. public function index(){
  8. if(request()->isAjax()){
  9. //分页参数
  10. $length = input('rows',10,'intval'); //每页条数
  11. $page = input('page',1,'intval'); //第几页
  12. $start = ($page - 1) * $length; //分页开始位置
  13. //排序
  14. $sortRow = input('sidx','sort','trim'); //排序列
  15. $sort = input('sord','asc','trim'); //排序方式
  16. $order = $sortRow.' '.$sort.' ,id desc';
  17. $month= input('month','','trim');
  18. if($month != ''){
  19. $map[] = ['create_yyyymm','=',$month];
  20. }
  21. $title = input('title','','trim');
  22. if($title){
  23. $map[] = ['title','like','%'.$title.'%'];
  24. }
  25. $status = input('status','','trim');
  26. if($status != ''){
  27. $map[] = ['status','=',$status];
  28. }
  29. $map[] = ['org_id','=',$this->orgId];
  30. $map[] =['del','=',0];
  31. $map= empty($map) ? true: $map;
  32. //数据查询
  33. $lists = Db::name('daily_task')->where($map)
  34. ->limit($start,$length)->order($order)->select();
  35. foreach ($lists as $k=>$v){
  36. $lists[$k]['start_time'] = date('Y-m-d H:i',strtotime($v['start_time']));
  37. $lists[$k]['end_time'] = date('Y-m-d H:i',strtotime($v['end_time']));
  38. $taskUser=Db::name('daily_task_user')
  39. ->alias('dtu')
  40. ->field('u.real_name')
  41. ->join('user u','u.id=dtu.user_id')
  42. ->where('dtu.task_id',$v['id'])
  43. ->select();
  44. $userName = [];
  45. foreach ($taskUser as $kk=>$vv){
  46. $userName[$kk]=$vv['real_name'];
  47. }
  48. $lists[$k]['task_user']=implode(',',$userName);
  49. }
  50. //数据返回
  51. $totalCount = Db::name('daily_task')->where($map)->count();
  52. $totalPage = ceil($totalCount/$length);
  53. $result['page'] = $page;
  54. $result['total'] = $totalPage;
  55. $result['records'] = $totalCount;
  56. $result['rows'] = $lists;
  57. return json($result);
  58. }else{
  59. return $this->fetch();
  60. }
  61. }
  62. /**
  63. * 新增/编辑
  64. */
  65. public function add($id=0){
  66. if(request()->isPost()){
  67. $res = model('DailyTask')->updates();
  68. if($res){
  69. $this->success('操作成功',url('index'));
  70. }else{
  71. $this->error(model('DailyTask')->getError());
  72. }
  73. }else{
  74. $title = '新增';
  75. if($id){
  76. $title = '编辑';
  77. $info = Db::name('DailyTask')->where('id',$id)->find();
  78. $info['daily_addr'] = Db::name('daily_task_addr')->where('task_id',$info['id'])->column('daily_id');
  79. $daily_user = Db::name('daily_task_user')->where('task_id',$info['id'])->column('user_id');
  80. $info['daily_user'] = isset($daily_user)?implode(',',$daily_user):'';
  81. $this->assign('info',$info);
  82. }
  83. $dailyAddr= Db::name('Daily')->where('org_id',cur_org_id())->where('del',0)->where('enable',1)->select();
  84. $dailyUser = model('WorkTypeMode')->getRolesUser(9,cur_org_id());
  85. $this->assign('dailyAddr',$dailyAddr);
  86. $this->assign('dailyUser',$dailyUser);
  87. $this->assign('send_user_num',0);
  88. $this->assign('title',$title);
  89. return $this->fetch();
  90. }
  91. }
  92. /**
  93. * 删除记录
  94. * @param int $id
  95. */
  96. public function del($id=0){
  97. if(!$id){
  98. $this->error('参数错误');
  99. }
  100. $res = model('DailyTask')->del($id,$this->userId);
  101. if($res){
  102. $this->success('删除成功');
  103. }else{
  104. $this->error('删除失败');
  105. }
  106. }
  107. public function info($id=0){
  108. if(!$id){
  109. $this->error('参数错误');
  110. }
  111. $info=Db::name('daily_task')
  112. ->field('id,start_time,end_time,status,title')
  113. ->where('id',$id)
  114. ->find();
  115. $info['start_time']=date('Y-m-d H:i',strtotime($info['start_time']));
  116. $info['end_time']=date('Y-m-d H:i',strtotime($info['end_time']));
  117. //获取执行用户
  118. $task_user=Db::name('daily_task_user')
  119. ->alias('dtu')
  120. ->join('user u','u.id=dtu.user_id')
  121. ->where('dtu.task_id',$info['id'])
  122. ->column('u.real_name');
  123. $info['task_user']=implode(',',$task_user);
  124. $task_addr = Db::name('daily_task_addr')
  125. ->alias('dta')
  126. ->join('daily d','d.id=dta.daily_id')
  127. ->where('dta.task_id',$info['id'])
  128. ->column('d.title');
  129. $info['task_addr']=implode(',',$task_addr);
  130. $map[] = ['dr.task_id', '=', $info['id']];
  131. $map[] = ['dr.org_id', '=', $this->orgId];
  132. //数据查询
  133. $lists = Db::name('daily_record')
  134. ->alias('dr')
  135. ->field('dr.*,d.title')
  136. ->Leftjoin('daily d', 'd.id=dr.daily_id')
  137. ->where($map)
  138. ->order('dr.id','desc')
  139. ->select();
  140. foreach ($lists as $k => $v) {
  141. $lists[$k]['task_title'] = Db::name('daily_task')
  142. ->where('id', $v['task_id'])->value('title');
  143. $lists[$k]['task_user'] = Db::name('user')
  144. ->where('id', $v['user_id'])->value('real_name');
  145. }
  146. $this->assign('info',$info);
  147. $this->assign('recordList',$lists);
  148. return $this->fetch();
  149. }
  150. public function calendar(){
  151. return $this->fetch();
  152. }
  153. //获取正月数据
  154. public function taskjson(){
  155. $start = input('start');
  156. $end = input('end');
  157. $data = array();
  158. if(!$start||!$end||$start>$end){
  159. header('Content-Type:application/json; charset=utf-8');
  160. exit(json_encode($data));
  161. }
  162. $start = date('Y-m-d H:i:s',strtotime($start));
  163. $end = date('Y-m-d H:i:s',strtotime($end));
  164. $map[] = [''];
  165. $list = model('DailyTask')->get_list_by_time($this->orgId,$start,$end);
  166. foreach ($list as $k=>$v){
  167. $arr = array(
  168. 'taskid' => $v['id'],
  169. 'title' => "{$v['title']}<br>执行人:{$v['users']}<br>开始时间:{$v['start_time']}<br>结束时间:{$v['end_time']}<br>",
  170. 'status' => $v['status'],
  171. 'start' => $v['start_time'],
  172. 'end' => $v['end_time']
  173. );
  174. if($v['status'] == 0){
  175. $arr['color'] = '#777777';
  176. }else if($v['status'] == 1){
  177. $arr['color'] = '#478fca';
  178. }else if($v['status'] == 2){
  179. $arr['color'] = '#69aa46';
  180. }else{
  181. $arr['color'] = '#dd5a43';
  182. }
  183. $data[] = $arr;
  184. }
  185. header('Content-Type:application/json; charset=utf-8');
  186. exit(json_encode($data));
  187. }
  188. //复制月计划
  189. public function plan(){
  190. $type = input('type');
  191. if(request()->isPost()){
  192. $data = request()->post();
  193. if($data['from'] == $data['to']){
  194. $this->error('不能复制同月的任务');
  195. }
  196. $ret = model('DailyTask')->plan_data($data,$this->orgId);
  197. if($ret){
  198. if($type==1){
  199. $this->success('操作成功',url('index'));
  200. }else{
  201. $this->success('操作成功',url('calendar'));
  202. }
  203. }else{
  204. $this->error(model('DailyTask')->getError());
  205. }
  206. }else{
  207. $from = model('DailyTask')->get_task_month($this->orgId);
  208. //间隔1月往后数11次
  209. $timenow = new \Datetime();
  210. $datetin = \DateInterval::createFromDateString('1 month');
  211. $datePer = new \DatePeriod($timenow, $datetin, 11);
  212. $to = [];
  213. foreach ($datePer as $day) {
  214. $m = $day->format('Y-m');
  215. $to[]= $m;
  216. }
  217. $this->assign('from',$from);
  218. $this->assign('to',$to);
  219. $this->assign('type',$type);
  220. return $this->fetch();
  221. }
  222. }
  223. public function batch(){
  224. $type = input('type');
  225. if(request()->isPost()){
  226. $result = model('DailyTask')->batchDel($this->orgId,$this->userId);
  227. if($result){
  228. if($type==1){
  229. $this->success('操作成功',url('index'));
  230. }else{
  231. $this->success('操作成功',url('calendar'));
  232. }
  233. }else{
  234. $this->error('删除失败');
  235. }
  236. }else{
  237. $from = model('DailyTask')->get_task_month($this->orgId);
  238. $minmonth = date('Y-m');
  239. $aa = [];
  240. foreach ($from as $k=>$v){
  241. if($minmonth < $v['create_yyyymm']){
  242. $aa[] = $v['create_yyyymm'];
  243. }
  244. }
  245. $this->assign('from',$aa);
  246. $this->assign('type',$type);
  247. return $this->fetch();
  248. }
  249. }
  250. }