Dep.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. public function batchDel(){
  88. $id = input('ids',[]);
  89. if(!$id){
  90. $this->error('参数错误');
  91. }
  92. $res = db($this->table)->where('id','in',$id)->setField('del',1);
  93. if($res){
  94. $this->success('删除成功');
  95. }else{
  96. $this->error('删除失败');
  97. }
  98. }
  99. /**
  100. * 改变字段值
  101. * @param int $fv
  102. * @param string $fn
  103. * @param int $fv
  104. */
  105. public function changeField($id=0,$fn='',$fv=0){
  106. if(!$fn||!$id){
  107. $this->error('参数错误');
  108. }
  109. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  110. if($res){
  111. $this->success('操作成功');
  112. }else{
  113. $this->error('操作失败');
  114. }
  115. }
  116. public function batchSort(){
  117. $data = input('data','','trim');
  118. if(!$data){
  119. $this->error('参数错误');
  120. }
  121. $data = json_decode($data,true);
  122. if(!$data){
  123. $this->error('参数错误');
  124. }
  125. Db::startTrans();
  126. try{
  127. foreach ($data as $k=>$v){
  128. Db::name('dep')->where('id',$v['id'])->setField('sort',$v['sort']);
  129. }
  130. Db::commit();
  131. }catch (Exception $e){
  132. Db::rollback();
  133. $this->error('操作失败');
  134. }
  135. $this->success('操作成功');
  136. }
  137. /**
  138. * 下载点模板
  139. */
  140. public function downloadtem(){
  141. set_time_limit(0);
  142. ini_set("memory_limit","512M");
  143. $header = [
  144. ['title' => '名称', 'name' => 'title','width'=>'20'],
  145. ['title' => '排序', 'name' => 'sort','width'=>'20'],
  146. ['title' => '备注', 'name' => 'remark','width'=>'20'],
  147. ];
  148. $lists = [
  149. [
  150. 'title' => '示例部门',
  151. 'sort' => '1',
  152. 'remark' => '备注',
  153. ]
  154. ];
  155. $filename = '部门模板';
  156. ExcelUtil::export($filename,$header,$lists);
  157. }
  158. public function import(){
  159. return $this->fetch();
  160. }
  161. /**
  162. * 导入
  163. */
  164. public function importexcel(){
  165. set_time_limit(0);
  166. ini_set("memory_limit", -1);
  167. ob_flush();//清空缓存
  168. flush();//刷新缓存
  169. try{
  170. $cols = ['title','sort','remark'];
  171. $lists = ExcelUtil::importExcel('file',$cols,2);
  172. if($lists === false){
  173. exit(ExcelUtil::getError());
  174. }
  175. if(empty($lists)){
  176. exit('文件内容为空');
  177. }
  178. foreach ($lists as $k=>$v){
  179. if($v['title']){
  180. $dt = [
  181. 'org_id'=>$this->orgId,
  182. 'title'=>$v['title'],
  183. 'sort'=>$v['sort'],
  184. 'remark'=>$v['remark'],
  185. 'create_time'=>date('Y-m-d H:i:s'),
  186. ];
  187. $inset = Db::name('dep')->insert($dt);
  188. if(!$inset){
  189. $msg = "第".($k+2)."行,导入失败";
  190. echo "<font color=\"red\">".$msg."</font><br />";
  191. continue;
  192. }
  193. }
  194. }
  195. echo "<font color=\"green\">导入完成</font><br />";
  196. }catch (Exception $e){
  197. trace($e->getMessage(),'error');
  198. echo $e->getMessage();
  199. echo "<font color=\"red\">数据异常,已停止导入</font><br />";
  200. }
  201. }
  202. //excel导出
  203. public function export() {
  204. set_time_limit(0);
  205. ini_set("memory_limit","1024M");
  206. $title = input('title','','trim');
  207. if($title){
  208. $map[] = ['title','like','%'.$title.'%'];
  209. }
  210. $enable = input('enable','','trim');
  211. if($enable != ''){
  212. $map[] = ['enable','=',$enable];
  213. }
  214. $map[] = ['del','=',0];
  215. $map[] = ['org_id','=',$this->orgId];
  216. $map= empty($map) ? true: $map;
  217. //数据查询
  218. $lists = db($this->table)->where($map)->order('id desc')->select();
  219. $header = [
  220. ['title' => 'id', 'name' => 'id','width'=>'10'],
  221. ['title' => '名称', 'name' => 'title','width'=>'20'],
  222. ];
  223. $filename = '部门';
  224. ExcelUtil::export($filename,$header,$lists);
  225. }
  226. }