Daily.php 12 KB

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