Daily.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. class Daily extends Auth {
  6. public function __construct(App $app = null) {
  7. parent::__construct($app);
  8. $this->model = new \app\common\model\Daily();
  9. }
  10. public function index() {
  11. if (request()->isAjax()) {
  12. //分页参数
  13. $length = input('rows', 10, 'intval'); //每页条数
  14. $page = input('page', 1, 'intval'); //第几页
  15. $start = ($page - 1) * $length; //分页开始位置
  16. //排序
  17. $sortRow = input('sidx', 'sort', 'trim'); //排序列
  18. $sort = input('sord', 'asc', 'trim'); //排序方式
  19. $order = $sortRow . ' ' . $sort . ' ,id desc';
  20. $title = input('title', '', 'trim');
  21. if ($title) {
  22. $map[] = ['title', 'like', '%' . $title . '%'];
  23. }
  24. $enable = input('enable', '', 'trim');
  25. if ($enable != '') {
  26. $map[] = ['enable', '=', $enable];
  27. }
  28. $map[] = ['org_id', '=', $this->orgId];
  29. $map[] = ['del', '=', 0];
  30. $map = empty($map) ? true : $map;
  31. //数据查询
  32. $lists = Db::name('daily')->where($map)->limit($start, $length)->order($order)->select();
  33. // foreach ($lists as $k => $v) {
  34. // $rolesName = $this->model->getRoles($v['roles']);
  35. // $lists[$k]['user_name'] = implode(',', $rolesName);
  36. // }
  37. //数据返回
  38. $totalCount = Db::name('daily')->where($map)->count();
  39. $totalPage = ceil($totalCount / $length);
  40. $result['page'] = $page;
  41. $result['total'] = $totalPage;
  42. $result['records'] = $totalCount;
  43. $result['rows'] = $lists;
  44. return json($result);
  45. }
  46. else {
  47. $this->assign('meta_title', '任务列表');
  48. return $this->fetch();
  49. }
  50. }
  51. /**
  52. * 新增/编辑
  53. */
  54. public function add($id = 0) {
  55. if (request()->isPost()) {
  56. $model = $this->model;
  57. $res = $model->updates();
  58. if ($res) {
  59. $this->success('操作成功', url('index'));
  60. }
  61. else {
  62. $this->error($model->getError());
  63. }
  64. }
  65. else {
  66. $meta_title = '新增工作地点';
  67. $formModel = new \app\common\model\DailyForm();
  68. if ($id) {
  69. $info = Db::name('daily')->where('id', $id)->find();
  70. $info['daily_form'] = $formModel->getByIdList($info['daily_form']);
  71. // $info['roles'] = explode(',', $info['roles']);
  72. $this->assign('info', $info);
  73. $daily_user = Db::name('daily_user')
  74. ->where('daily_id',$info['id'])->column('user_id');
  75. $info['daily_user'] = isset($daily_user)?implode(',',$daily_user):'';
  76. $this->assign('info',$info);
  77. $meta_title = '编辑工作地点';
  78. }
  79. //获取检查项
  80. $dailyForm = $formModel->list();
  81. //角色
  82. $roles = $this->model->getRolesList();
  83. $dailyUser = model('WorkTypeMode')->getRolesUser(9,cur_org_id());
  84. $this->assign('daily_form', $dailyForm);
  85. $this->assign('dailyUser',$dailyUser);
  86. $this->assign('meta_title', $meta_title);
  87. $this->assign('send_user_num',0);
  88. return $this->fetch();
  89. }
  90. }
  91. /**
  92. * 删除记录
  93. * @param int $id
  94. */
  95. public function del($id = 0) {
  96. if (!$id) {
  97. $this->error('参数错误');
  98. }
  99. $res = Db::name('daily')->where('id', $id)->update(['del' => 1]);
  100. if ($res) {
  101. $this->success('删除成功');
  102. }
  103. else {
  104. $this->error('删除失败');
  105. }
  106. }
  107. /**
  108. * 改变字段值
  109. * @param int $fv
  110. * @param string $fn
  111. * @param int $fv
  112. */
  113. public function changeField($id = 0, $fn = '', $fv = 0) {
  114. if (!$fn || !$id) {
  115. $this->error('参数错误');
  116. }
  117. $res = Db::name('daily')->where('id', $id)->update([$fn => $fv]);
  118. if ($res) {
  119. $this->success('操作成功');
  120. }
  121. else {
  122. $this->error('操作失败');
  123. }
  124. }
  125. //二维码
  126. public function qrcode($id = 0) {
  127. $info = Db::name('daily')->where('id', $id)->find();
  128. if (!$info) {
  129. exit('数据不存在');
  130. }
  131. $code = get_qrcode_str('daily', $id);
  132. $this->assign('code', $code);
  133. $this->assign('info', $info);
  134. return $this->fetch();
  135. }
  136. public function allPrint(){
  137. $ids = input('ids');
  138. if(!$ids){
  139. $list = Db::name('daily')
  140. ->where('org_id',cur_org_id())
  141. ->where('enable',1)
  142. ->where('del',0)
  143. ->select();
  144. }else{
  145. $ids = explode(',',$ids);
  146. $list = Db::name('daily')
  147. ->where('org_id',cur_org_id())
  148. ->where('enable',1)
  149. ->where('id','in',$ids)
  150. ->where('del',0)
  151. ->select();
  152. }
  153. foreach ($list as $k=>$v){
  154. $list[$k]['qCode'] = get_qrcode_str('daily',$v['id']);
  155. }
  156. $this->assign('list',$list);
  157. return $this->fetch();
  158. }
  159. public function group() {
  160. $title = input('title', '', 'trim');
  161. if ($title) {
  162. $map[] = ['title', 'like', '%' . $title . '%'];
  163. }
  164. $map[] = ['enable', '=', 1];
  165. $map[] = ['org_id', '=', $this->orgId];
  166. $map[] = ['del', '=', 0];
  167. $lists = Db::name('daily')->where($map)->field('id,title')->order('id desc')->select();
  168. $hours = model('Daily')->getTimeDaily($this->orgId);
  169. foreach ($lists as $k=>$v){
  170. $users = Db::name('daily_user')
  171. ->alias('du')
  172. ->join('user u','u.id = du.user_id')
  173. ->where('du.daily_id',$v['id'])
  174. ->column('real_name');
  175. $users = $users?$users:[];
  176. $lists[$k]['users'] = $users?implode(',',$users):'';
  177. $m = [];
  178. $m[] = ['org_id','=',$this->orgId];
  179. $m[] = ['daily_id','=',$v['id']];
  180. $m[] = ['create_time','>=',$hours['start']];
  181. $m[] = ['create_time','<',$hours['end']];
  182. $count = Db::name('daily_record')->where($m)->count();
  183. $lists[$k]['count'] = $count;
  184. $lists[$k]['zc'] = 0;
  185. if($count > 0){
  186. $lists[$k]['zc'] = 1;
  187. }
  188. $m[] = ['order_id','>',0];
  189. $orderIds = Db::name('daily_record')->where($m)->column('order_id');
  190. if($orderIds){
  191. $res = Db::name('orders')->where('id','in',$orderIds)->where('del',0)->where('order_mode','in',[1,4])->find();
  192. if($res){
  193. $lists[$k]['zc'] = 2;
  194. }
  195. }
  196. }
  197. $this->assign('hours',$hours);
  198. $this->assign('lists',$lists);
  199. return $this->fetch();
  200. }
  201. public function record($id=0,$s='',$e='') {
  202. if (request()->isAjax()) {
  203. //分页参数
  204. $length = input('rows', 10, 'intval'); //每页条数
  205. $page = input('page', 1, 'intval'); //第几页
  206. $start = ($page - 1) * $length; //分页开始位置
  207. $order = 'id desc';
  208. $map[] = ['dr.daily_id','=',$id];
  209. $map[] = ['dr.create_time','>=',$s];
  210. $map[] = ['dr.create_time','<',$e];
  211. $map[] = ['dr.org_id', '=', $this->orgId];
  212. $map = empty($map) ? true : $map;
  213. //数据查询
  214. $lists = Db::name('daily_record')
  215. ->alias('dr')
  216. ->join('daily d', 'd.id=dr.daily_id')
  217. ->field('dr.*,d.title,d.content as daily_details')
  218. ->where($map)
  219. ->limit($start, $length)
  220. ->order($order)
  221. ->select();
  222. foreach ($lists as $k => $v) {
  223. $lists[$k]['user_name'] = Db::name('user')
  224. ->where('id', $v['user_id'])
  225. ->value('real_name');
  226. $lists[$k]['zc'] = 0;
  227. if($v['order_id'] > 0){
  228. $lists[$k]['zc'] = 1;
  229. $res = Db::name('orders')->where('id',$v['order_id'])->where('del',0)->where('order_mode','in',[1,4])->find();
  230. if($res){
  231. $lists[$k]['zc'] = 2;
  232. }
  233. }
  234. }
  235. //数据返回
  236. $totalCount = Db::name('daily_record')
  237. ->alias('dr')
  238. ->field('dr.*,d.title,d.content as daily_details')
  239. ->join('daily d', 'd.id=dr.daily_id')
  240. ->where($map)->count();
  241. $totalPage = ceil($totalCount / $length);
  242. $result['page'] = $page;
  243. $result['total'] = $totalPage;
  244. $result['records'] = $totalCount;
  245. $result['rows'] = $lists;
  246. return json($result);
  247. } else {
  248. $this->assign('id',$id);
  249. $this->assign('s',$s);
  250. $this->assign('e',$e);
  251. $this->assign('meta_title', '检查记录列表');
  252. return $this->fetch();
  253. }
  254. }
  255. public function addrdown(){
  256. $orgid = $this->orgId;
  257. $map[] = ['del','=',0];
  258. $map[] = ['org_id','=',$this->orgId];
  259. $map= empty($map) ? true: $map;
  260. //数据查询
  261. $lists = db('daily')->where($map)->select();
  262. foreach ($lists as $k=>$v){
  263. $lists[$k]['code'] = get_qrcode_str('daily',$v['id']);
  264. }
  265. $path = date('Ymd').'_'.$orgid.'_'.time().mt_rand(1000,9999);
  266. $dir = './uploads/qrcodeimg/'.$path.'/';
  267. foreach ($lists as $v1){
  268. $name = $v1['title'].'('.$v1['id'].')'.'.jpg';
  269. $filepath = $dir.'/'.$name;
  270. create_qrcode($v1['code'],$filepath);
  271. }
  272. $zippath = './uploads/qrcodeimg/'.$path.'.zip';
  273. $spath = $dir;
  274. @unlink($zippath);
  275. $zip = new \ZipArchive();
  276. if($zip->open($zippath, \ZipArchive::CREATE)=== TRUE){
  277. add_file_to_zip($spath,$zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
  278. $zip->close(); //关闭处理的zip文件
  279. if(!file_exists($zippath)){
  280. $this->error("打包失败"); //即使创建,仍有可能失败。。。。
  281. }
  282. deldir($dir); // 删除生成的目录
  283. $downname = session('orgName').'日常工作地点二维码.zip';
  284. header("Cache-Control: public");
  285. header("Content-Description: File Transfer");
  286. header('Content-disposition: attachment; filename='.$downname); //文件名
  287. header("Content-Type: application/zip"); //zip格式的
  288. header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
  289. header('Content-Length: '. filesize($zippath)); //告诉浏览器,文件大小
  290. @readfile($zippath);
  291. }else{
  292. $this->error("打包失败");
  293. }
  294. }
  295. }