Dep.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 Dep extends Auth
  8. {
  9. public function __construct(App $app = null) {
  10. parent::__construct($app);
  11. $this->table='dep';
  12. $this->model= new \app\common\model\Dep();
  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= empty($map) ? true: $map;
  35. //数据查询
  36. $lists = db($this->table)->where($map)->limit($start,$length)->order($order)->select();
  37. //数据返回
  38. $totalCount = db($this->table)->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. }else{
  46. return $this->fetch();
  47. }
  48. }
  49. /**
  50. * 新增/编辑
  51. */
  52. public function add($id=0){
  53. if(request()->isPost()){
  54. $res = $this->model->updates();
  55. if($res){
  56. $this->success('操作成功',url('index'));
  57. }else{
  58. $this->error($this->model->getError());
  59. }
  60. }else{
  61. if($id){
  62. $info =db($this->table)->where('id',$id)->find();
  63. $this->assign('info',$info);
  64. }
  65. $cateId = input('cate_id',0);
  66. $cate = model('DepCate')->getList();
  67. $this->assign('cate',$cate);
  68. $this->assign('cate_id',$cateId);
  69. return $this->fetch();
  70. }
  71. }
  72. /**
  73. * 删除记录
  74. * @param int $id
  75. */
  76. public function del($id=0){
  77. if(!$id){
  78. $this->error('参数错误');
  79. }
  80. $res = db($this->table)->where('id',$id)->setField('del',1);
  81. if($res){
  82. $this->success('删除成功');
  83. }else{
  84. $this->error('删除失败');
  85. }
  86. }
  87. /**
  88. * 改变字段值
  89. * @param int $fv
  90. * @param string $fn
  91. * @param int $fv
  92. */
  93. public function changeField($id=0,$fn='',$fv=0){
  94. if(!$fn||!$id){
  95. $this->error('参数错误');
  96. }
  97. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  98. if($res){
  99. $this->success('操作成功');
  100. }else{
  101. $this->error('操作失败');
  102. }
  103. }
  104. public function batchSort(){
  105. $data = input('data','','trim');
  106. if(!$data){
  107. $this->error('参数错误');
  108. }
  109. $data = json_decode($data,true);
  110. if(!$data){
  111. $this->error('参数错误');
  112. }
  113. Db::startTrans();
  114. try{
  115. foreach ($data as $k=>$v){
  116. Db::name('dep')->where('id',$v['id'])->setField('sort',$v['sort']);
  117. }
  118. Db::commit();
  119. }catch (Exception $e){
  120. Db::rollback();
  121. $this->error('操作失败');
  122. }
  123. $this->success('操作成功');
  124. }
  125. /**
  126. * 下载点模板
  127. */
  128. public function downloadtem(){
  129. set_time_limit(0);
  130. ini_set("memory_limit","512M");
  131. $header = [
  132. ['title' => '名称', 'name' => 'title','width'=>'20'],
  133. ['title' => '排序', 'name' => 'sort','width'=>'20'],
  134. ['title' => '备注', 'name' => 'remark','width'=>'20'],
  135. ];
  136. $lists = [
  137. [
  138. 'title' => '示例部门',
  139. 'sort' => '1',
  140. 'remark' => '备注',
  141. ]
  142. ];
  143. $filename = '部门模板';
  144. ExcelUtil::export($filename,$header,$lists);
  145. }
  146. public function import(){
  147. return $this->fetch();
  148. }
  149. /**
  150. * 导入
  151. */
  152. public function importexcel(){
  153. set_time_limit(0);
  154. ini_set("memory_limit", -1);
  155. ob_flush();//清空缓存
  156. flush();//刷新缓存
  157. try{
  158. $cols = ['title','sort','remark'];
  159. $lists = ExcelUtil::importExcel('file',$cols,2);
  160. if($lists === false){
  161. exit(ExcelUtil::getError());
  162. }
  163. if(empty($lists)){
  164. exit('文件内容为空');
  165. }
  166. foreach ($lists as $k=>$v){
  167. if($v['title']){
  168. $dt = [
  169. 'org_id'=>$this->orgId,
  170. 'title'=>$v['title'],
  171. 'sort'=>$v['sort'],
  172. 'remark'=>$v['remark'],
  173. 'create_time'=>date('Y-m-d H:i:s'),
  174. ];
  175. $inset = Db::name('dep')->insert($dt);
  176. if(!$inset){
  177. $msg = "第".($k+2)."行,导入失败";
  178. echo "<font color=\"red\">".$msg."</font><br />";
  179. continue;
  180. }
  181. }
  182. }
  183. echo "<font color=\"green\">导入完成</font><br />";
  184. }catch (Exception $e){
  185. trace($e->getMessage(),'error');
  186. echo $e->getMessage();
  187. echo "<font color=\"red\">数据异常,已停止导入</font><br />";
  188. }
  189. }
  190. }