ConveyCron.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. use think\Exception;
  6. class ConveyCron extends Auth
  7. {
  8. public function __construct(App $app = null) {
  9. parent::__construct($app);
  10. $this->table='convey_cron';
  11. $this->model= new \app\common\model\ConveyCron();
  12. }
  13. private $weeks = ['周日','周一','周二','周三','周四','周五','周六'];
  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::name($this->table)->where($map)
  37. ->limit($start,$length)
  38. ->order([$sortRow=>$sort,'id'=>'desc'])
  39. ->select();
  40. foreach ($lists as $k=>$v){
  41. $lists[$k]['dep_title'] = Db::name('dep')->where('id',$v['dep_id'])->value("title");
  42. $userIds = empty($v['to_user_ids'])?[]:explode(",",$v['to_user_ids']);
  43. $lists[$k]['unames'] = "";
  44. if($userIds){
  45. $names = Db::name('user')->where('id','in',$userIds)->column('real_name');
  46. $lists[$k]['unames'] = $names?implode(',',$names):'';
  47. }
  48. $lists[$k]['type_title'] = Db::name('convey_cate')->where('id',$v['type'])->value("title");
  49. $lists[$k]['device_title'] = "";
  50. if($v['device_id']){
  51. $lists[$k]['device_title'] = Db::name('convey_device')->where('id',$v['device_id'])->value("title");
  52. }
  53. $lists[$k]['week_day'] = isset($this->weeks[$v['week']])? $this->weeks[$v['week']]:"";
  54. $lists[$k]['start_title'] = Db::name('address')->where('id',$v['start'])->value("title");
  55. $lists[$k]['end_title'] = Db::name('address')->where('id',$v['end'])->value("title");
  56. }
  57. //数据返回
  58. $totalCount = Db::name($this->table)->where($map)->count();
  59. $totalPage = ceil($totalCount/$length);
  60. $result['page'] = $page;
  61. $result['total'] = $totalPage;
  62. $result['records'] = $totalCount;
  63. $result['rows'] = $lists;
  64. return json($result);
  65. }else{
  66. return $this->fetch();
  67. }
  68. }
  69. /**
  70. * 新增/编辑
  71. */
  72. public function add($id=0){
  73. if(request()->isPost()){
  74. $res = $this->model->updates();
  75. if($res){
  76. $this->success('操作成功',url('index'));
  77. }else{
  78. $this->error($this->model->getError());
  79. }
  80. }else{
  81. if($id){
  82. $info =db($this->table)->where('id',$id)->find();
  83. $this->assign('info',$info);
  84. }
  85. $depList = (new \app\common\model\Dep())->getList();
  86. $this->assign('dep_list', $depList);
  87. $address = (new \app\common\model\Address())->getListByType(2);
  88. $conveyCate = (new \app\common\model\ConveyCate());
  89. $priority = $conveyCate->priority;
  90. $order_convey = $conveyCate->getList();
  91. $order_device = (new \app\common\model\ConveyDevice())->getList();
  92. $this->assign('address', $address);
  93. $this->assign('priority', $priority);
  94. $this->assign('order_convey_type', $order_convey);
  95. $this->assign('order_device', $order_device);
  96. $this->assign('user_list', (new \app\common\model\WorkTypeMode())->getRolesUserByNum(3, $this->orgId,-1));
  97. return $this->fetch();
  98. }
  99. }
  100. public function getAddr(){
  101. $id = input('id/d',0);
  102. $info = Db::name('convey_cate')
  103. ->where('id',$id)
  104. ->find();
  105. if(!empty($info['starts'])){
  106. $st = explode(',',$info['starts']);
  107. $s = (new \app\common\model\Address())->getListByTypes($st,2);
  108. }else{
  109. $s = (new \app\common\model\Address())->getListByType(2);
  110. }
  111. if(!empty($info['ends'])){
  112. $ent = explode(',',$info['ends']);
  113. $en = (new \app\common\model\Address())->getListByTypes($ent,2);
  114. }else{
  115. $en = (new \app\common\model\Address())->getListByType(2);
  116. }
  117. $a = [
  118. 's'=>$s,
  119. 'e'=>$en,
  120. ];
  121. $this->success('','',$a);
  122. }
  123. /**
  124. * 删除记录
  125. * @param int $id
  126. */
  127. public function del($id=0){
  128. if(!$id){
  129. $this->error('参数错误');
  130. }
  131. $res = db($this->table)->where('id',$id)->setField('del',1);
  132. if($res){
  133. $this->success('删除成功');
  134. }else{
  135. $this->error('删除失败');
  136. }
  137. }
  138. /**
  139. * 改变字段值
  140. * @param int $fv
  141. * @param string $fn
  142. * @param int $fv
  143. */
  144. public function changeField($id=0,$fn='',$fv=0){
  145. if(!$fn||!$id){
  146. $this->error('参数错误');
  147. }
  148. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  149. if($res){
  150. $this->success('操作成功');
  151. }else{
  152. $this->error('操作失败');
  153. }
  154. }
  155. public function plan(){
  156. $res = model("ConveyCron")->plan($this->orgId);
  157. if($res){
  158. $this->success('操作成功');
  159. }else{
  160. $this->error('操作失败');
  161. }
  162. }
  163. public function batchSort(){
  164. $data = input('data','','trim');
  165. if(!$data){
  166. $this->error('参数错误');
  167. }
  168. $data = json_decode($data,true);
  169. if(!$data){
  170. $this->error('参数错误');
  171. }
  172. Db::startTrans();
  173. try{
  174. foreach ($data as $k=>$v){
  175. Db::name('convey_device')->where('id',$v['id'])->setField('sort',$v['sort']);
  176. }
  177. Db::commit();
  178. }catch (Exception $e){
  179. Db::rollback();
  180. $this->error('操作失败');
  181. }
  182. $this->success('操作成功');
  183. }
  184. }