CleanPlan.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\util\ExcelUtil;
  4. use think\App;
  5. use think\Db;
  6. use think\Exception;
  7. class CleanPlan extends Auth
  8. {
  9. public function __construct(App $app = null) {
  10. parent::__construct($app);
  11. $this->model= new \app\common\model\CleanPlan();
  12. $this->table= $this->model->table;
  13. }
  14. public function index(){
  15. if(request()->isAjax()){
  16. //分页参数
  17. $length = input('rows',10,'intval'); //每页条数
  18. $page = input('page',1,'intval'); //第几页
  19. $start = ($page - 1) * $length; //分页开始位置
  20. //排序
  21. $sortRow = input('sidx','id','trim'); //排序列
  22. $sort = input('sord','desc','trim'); //排序方式
  23. $order = $sortRow.' '.$sort;
  24. $title = input('title','','trim');
  25. if($title){
  26. $map[] = ['title','like','%'.$title.'%'];
  27. }
  28. $enable = input('enable','','trim');
  29. if($enable != ''){
  30. $map[] = ['enable','=',$enable];
  31. }
  32. $map[] = ['del','=',0];
  33. $map[] = ['org_id','=',$this->orgId];
  34. $map[] = ['pid','=',0];
  35. $map= empty($map) ? true: $map;
  36. //数据查询
  37. $lists = Db::name($this->table)->where($map)->limit($start,$length)->order($order)->select();
  38. foreach ($lists as $k=>$v){
  39. $ids = Db::name($this->table)->where('del',0)->where('pid',$v['id'])->column('id');
  40. $lists[$k]['count'] = 0;
  41. $lists[$k]['finish'] = 0;
  42. $lists[$k]['notFinish'] = 0;
  43. if($ids){
  44. $lists[$k]['count'] = Db::name('clean_plan_record')->where('del',0)->where('plan_id','in',$ids)->count();
  45. $lists[$k]['finish'] = Db::name('clean_plan_record')->where('del',0)->where('enable',1)->where('plan_id','in',$ids)->count();
  46. $lists[$k]['notFinish'] = Db::name('clean_plan_record')->where('del',0)->where('enable',0)->where('plan_id','in',$ids)->count();
  47. }
  48. }
  49. //数据返回
  50. $totalCount = Db::name($this->table)->where($map)->count();
  51. $totalPage = ceil($totalCount/$length);
  52. $result['page'] = $page;
  53. $result['total'] = $totalPage;
  54. $result['records'] = $totalCount;
  55. $result['rows'] = $lists;
  56. return json($result);
  57. }else{
  58. $this->assign('m_name','专项年计划');
  59. return $this->fetch();
  60. }
  61. }
  62. /**
  63. * 新增/编辑
  64. */
  65. public function add($id=0){
  66. if(request()->isPost()){
  67. $res = $this->model->updates();
  68. if($res){
  69. $this->success('操作成功',url('index'));
  70. }else{
  71. $this->error($this->model->getError());
  72. }
  73. }else{
  74. if($id > 0){
  75. $info = Db::name($this->table)->where('id',$id)->find();
  76. if($info){
  77. $info['forms'] = $info['forms']?explode(',',$info['forms']):[];
  78. $info['addrs'] = $info['addrs']?explode(',',$info['addrs']):[];
  79. $info['user'] = $info['user']?explode(',',$info['user']):[];
  80. }
  81. $this->assign('info',$info);
  82. }
  83. $users = (new \app\common\model\User())->getCleanWorker();
  84. $this->assign('user',$users);
  85. $this->assign('type',(new \app\common\model\CleanType())->getList());
  86. $this->assign('address',(new \app\common\model\Address())->getListByType(8));
  87. return $this->fetch();
  88. }
  89. }
  90. /**
  91. * 删除记录
  92. * @param int $id
  93. */
  94. public function del($id=0){
  95. if(!$id){
  96. $this->error('参数错误');
  97. }
  98. $info = Db::name($this->table)->where('id',$id)->where('del',0)->find();
  99. if(!$info){
  100. $this->error('记录不存在或已删除');
  101. }
  102. if($info['pid'] == 0){ // 年计划
  103. $planIds = Db::name($this->table)->where('pid',$id)->where('del',0)->column('id');
  104. //删除年下面的月计划
  105. Db::name($this->table)->where('pid',$id)->where('del',0)->setField('del',1);
  106. //删除日计划
  107. if(!empty($planIds)){
  108. Db::name('clean_plan_record')->whereIn('plan_id',$planIds)->setField('del',1);
  109. }
  110. }else{
  111. Db::name('clean_plan_record')->where('plan_id',$id)->setField('del',1);
  112. }
  113. $res = db($this->table)->where('id',$id)->setField('del',1);
  114. if($res){
  115. $this->success('删除成功');
  116. }else{
  117. $this->error('删除失败');
  118. }
  119. }
  120. /**
  121. * 改变字段值
  122. * @param int $fv
  123. * @param string $fn
  124. * @param int $fv
  125. */
  126. public function changeField($id=0,$fn='',$fv=0){
  127. if(!$fn||!$id){
  128. $this->error('参数错误');
  129. }
  130. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  131. if($res){
  132. $this->success('操作成功');
  133. }else{
  134. $this->error('操作失败');
  135. }
  136. }
  137. public function month($id=0){
  138. if(request()->isAjax()){
  139. //分页参数
  140. $length = input('rows',10,'intval'); //每页条数
  141. $page = input('page',1,'intval'); //第几页
  142. $start = ($page - 1) * $length; //分页开始位置
  143. //排序
  144. $sortRow = input('sidx','id','trim'); //排序列
  145. $sort = input('sord','desc','trim'); //排序方式
  146. $order = $sortRow.' '.$sort;
  147. $title = input('title','','trim');
  148. if($title){
  149. $map[] = ['title','like','%'.$title.'%'];
  150. }
  151. $enable = input('enable','','trim');
  152. if($enable != ''){
  153. $map[] = ['enable','=',$enable];
  154. }
  155. $map[] = ['pid','=',$id];
  156. $map[] = ['del','=',0];
  157. $map[] = ['org_id','=',$this->orgId];
  158. $map= empty($map) ? true: $map;
  159. //数据查询
  160. $lists = db($this->table)->where($map)->limit($start,$length)->order($order)->select();
  161. foreach ($lists as $k=>$v){
  162. $lists[$k]['count'] = Db::name('clean_plan_record')->where('plan_id',$v['id'])->where('del',0)->count();
  163. $lists[$k]['finish'] = Db::name('clean_plan_record')->where('plan_id',$v['id'])->where('del',0)->where('enable',1)->count();
  164. $lists[$k]['notFinish'] = Db::name('clean_plan_record')->where('plan_id',$v['id'])->where('del',0)->where('enable',0)->count();
  165. }
  166. //数据返回
  167. $totalCount = db($this->table)->where($map)->count();
  168. $totalPage = ceil($totalCount/$length);
  169. $result['page'] = $page;
  170. $result['total'] = $totalPage;
  171. $result['records'] = $totalCount;
  172. $result['rows'] = $lists;
  173. return json($result);
  174. }else{
  175. $this->assign('id',$id);
  176. $info = Db::name('clean_plan')->where('id',$id)->find();
  177. $this->assign('m_name','['.$info['title'].']月计划');
  178. return $this->fetch();
  179. }
  180. }
  181. public function madd($id=0,$pid=0){
  182. if(request()->isPost()){
  183. $res = $this->model->mupdates();
  184. if($res){
  185. $this->success('操作成功',url('index'));
  186. }else{
  187. $this->error($this->model->getError());
  188. }
  189. }else{
  190. if($id > 0){
  191. $info = Db::name($this->table)->where('id',$id)->find();
  192. if($info){
  193. $info['forms'] = $info['forms']?explode(',',$info['forms']):[];
  194. $info['addrs'] = $info['addrs']?explode(',',$info['addrs']):[];
  195. $pid = $info['pid'];
  196. $info['user'] = $info['user']?explode(',',$info['user']):[];
  197. }
  198. $this->assign('info',$info);
  199. }
  200. $pinfo = Db::name($this->table)->where('id',$pid)->find();
  201. $months = [];
  202. for ($i=1;$i<=12;$i++){
  203. $months[] = [
  204. 'id' => $i>9?$pinfo['year'].'-'.$i:$pinfo['year'].'-0'.$i,
  205. 'title' => $i.'月'
  206. ];
  207. }
  208. $this->assign('months',$months);
  209. $forms = $pinfo['forms']?explode(',',$pinfo['forms']):[];
  210. $addrs = $pinfo['addrs']?explode(',',$pinfo['addrs']):[];
  211. $uids = $pinfo['user']?explode(',',$pinfo['user']):[];
  212. $typs = (new \app\common\model\CleanType())->getList();
  213. $address = (new \app\common\model\Address())->getListByType(8);
  214. foreach ($typs as $k=>$v){
  215. if(!in_array($v['id'],$forms)){
  216. unset($typs[$k]);
  217. }
  218. }
  219. foreach ($address as $k=>$v){
  220. if(!in_array($v['id'],$addrs)){
  221. unset($address[$k]);
  222. }
  223. }
  224. $users = (new \app\common\model\User())->getCleanWorker();
  225. $nuser = [];
  226. foreach ($users as $k=>$v){
  227. if(in_array($v['id'],$uids)){
  228. $nuser[] = $v;
  229. }
  230. }
  231. $this->assign('user',$nuser);
  232. $this->assign('type',$typs?$typs:[]);
  233. $this->assign('address',$address?$address:[]);
  234. $this->assign('pid',$pid);
  235. return $this->fetch();
  236. }
  237. }
  238. public function export($id = 0){
  239. set_time_limit(0);
  240. ini_set("memory_limit","3000M");
  241. $info = Db::name('clean_plan')->where('id',$id)->where('pid',0)->where('del',0)->find();
  242. if(!$info){
  243. $this->error('记录不存在');
  244. }
  245. $lists = Db::name('clean_plan')->where('pid',$id)->where('del',0)->order('month asc,id asc')->select();
  246. if(!$lists){
  247. $this->error('未设置月计划');
  248. }
  249. $sheets = [];
  250. $data = [];
  251. foreach ($lists as $k=>$v){
  252. $sheets[] = $v['month'];
  253. $record = Db::name('clean_plan_record')
  254. ->alias('a')
  255. ->join('clean_type b','b.id = a.form_id')
  256. ->join('address c','c.id = a.address_id')
  257. ->where('a.plan_id',$v['id'])
  258. ->where('a.del',0)
  259. ->field('a.start,a.end,a.remark,b.title as btitle,c.title as atitle,a.create_time,a.enable,a.finish_user_id,a.finish_time,a.id,a.content')
  260. ->order('a.start asc,a.id asc')
  261. ->select();
  262. $record = $record?$record:[];
  263. foreach ($record as $kk=>$vv){
  264. $users = Db::name('clean_plan_user')
  265. ->alias('a')
  266. ->join('user b','a.user_id = b.id')
  267. ->where('a.record_id',$vv['id'])
  268. ->column('b.real_name');
  269. $record[$kk]['users'] = $users?implode(',',$users):'';
  270. $record[$kk]['finish_user'] = '';
  271. if($vv['finish_user_id'] > 0){
  272. $record[$kk]['finish_user'] = Db::name('user')->where('id',$vv['finish_user_id'])->value('real_name');
  273. }
  274. if($vv['enable'] == 0){
  275. $record[$kk]['enable_text'] = '未完成';
  276. }else{
  277. $record[$kk]['enable_text'] = '已完成';
  278. }
  279. }
  280. $data[] = $record;
  281. }
  282. $filename = $info['year'].'_'.$info['title'];
  283. $header = [ // header格式,title和name必须存在
  284. ['title' => '任务项', 'name' => 'btitle','width'=>'20'],
  285. ['title' => '地点', 'name' => 'atitle','width'=>'20'],
  286. ['title' => '开始时间', 'name' => 'start','width'=>'20'],
  287. ['title' => '结束时间', 'name' => 'end','width'=>'20'],
  288. ['title' => '备注', 'name' => 'remark','width'=>'20'],
  289. ['title' => '人员', 'name' => 'users','width'=>'20'],
  290. ['title' => '状态', 'name' => 'enable_text','width'=>'20'],
  291. ['title' => '汇报人', 'name' => 'finish_user','width'=>'20'],
  292. ['title' => '汇报内容', 'name' => 'content','width'=>'20'],
  293. ['title' => '汇报时间', 'name' => 'finish_time','width'=>'20'],
  294. ];
  295. ExcelUtil::exportBySheet($filename,$header,$sheets,$data);
  296. }
  297. }