Train.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. use think\Exception;
  6. class Train extends Auth
  7. {
  8. public function __construct(App $app = null) {
  9. parent::__construct($app);
  10. $this->model= new \app\common\model\Train();
  11. $this->table= $this->model->table;
  12. $this->m_name = '培训计划';
  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. foreach ($lists as $k=>$v){
  38. $lists[$k]['kj'] = "";
  39. if($v['kj_id'] >0){
  40. $lists[$k]['kj'] = Db::name('train_kj')
  41. ->where('id',$v['kj_id'])->value('title');
  42. }
  43. }
  44. //数据返回
  45. $totalCount = db($this->table)->where($map)->count();
  46. $totalPage = ceil($totalCount/$length);
  47. $result['page'] = $page;
  48. $result['total'] = $totalPage;
  49. $result['records'] = $totalCount;
  50. $result['rows'] = $lists;
  51. return json($result);
  52. }else{
  53. $this->assign('m_name',$this->m_name);
  54. return $this->fetch();
  55. }
  56. }
  57. /**
  58. * 新增/编辑
  59. */
  60. public function add($id=0){
  61. if(request()->isPost()){
  62. $res = $this->model->updates();
  63. if($res){
  64. $this->success('操作成功',url('index'));
  65. }else{
  66. $this->error($this->model->getError());
  67. }
  68. }else{
  69. $kj = Db::name('train_kj')
  70. ->where('org_id',$this->orgId)
  71. ->where('del',0)
  72. ->where('enable',1)
  73. ->select();
  74. $this->assign('kj',$kj);
  75. if($id){
  76. $info =db($this->table)->where('id',$id)->find();
  77. $this->assign('info',$info);
  78. }
  79. return $this->fetch();
  80. }
  81. }
  82. /**
  83. * 删除记录
  84. * @param int $id
  85. */
  86. public function del($id=0){
  87. if(!$id){
  88. $this->error('参数错误');
  89. }
  90. $res = db($this->table)->where('id',$id)->setField('del',1);
  91. if($res){
  92. $this->success('删除成功');
  93. }else{
  94. $this->error('删除失败');
  95. }
  96. }
  97. /**
  98. * 改变字段值
  99. * @param int $fv
  100. * @param string $fn
  101. * @param int $fv
  102. */
  103. public function changeField($id=0,$fn='',$fv=0){
  104. if(!$fn||!$id){
  105. $this->error('参数错误');
  106. }
  107. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  108. if($res){
  109. $this->success('操作成功');
  110. }else{
  111. $this->error('操作失败');
  112. }
  113. }
  114. }