ConveyCate.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 ConveyCate extends Auth
  8. {
  9. public function __construct(App $app = null) {
  10. parent::__construct($app);
  11. $this->model= new \app\common\model\ConveyCate();
  12. $this->table= $this->model->table;
  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)
  37. ->order([$sortRow=>$sort,'id'=>'asc'])
  38. ->select();
  39. foreach ($lists as $k=>$v){
  40. $lists[$k]['cateName'] = $this->model->cate[$v['cate']];
  41. $lists[$k]['priorityName'] = $this->model->priority[$v['priority']];
  42. $lists[$k]['timeName'] = db('time')
  43. ->where('id',$v['time_id'])->value('title');
  44. $lists[$k]['endsName'] =!empty($v['ends'])?implode(',',db('address')
  45. ->whereIn('id',$v['ends'])->column('title')):'';
  46. }
  47. //数据返回
  48. $totalCount = db($this->table)->where($map)->count();
  49. $totalPage = ceil($totalCount/$length);
  50. $result['page'] = $page;
  51. $result['total'] = $totalPage;
  52. $result['records'] = $totalCount;
  53. $result['rows'] = $lists;
  54. return json($result);
  55. }else{
  56. $this->assign('m_name','运送类型');
  57. return $this->fetch();
  58. }
  59. }
  60. /**
  61. * 新增/编辑
  62. */
  63. public function add($id=0){
  64. if(request()->isPost()){
  65. $res = $this->model->updates();
  66. if($res){
  67. $this->success('操作成功',url('index'));
  68. }else{
  69. $this->error($this->model->getError());
  70. }
  71. }else{
  72. if($id){
  73. $info =db($this->table)->where('id',$id)->find();
  74. if($info){
  75. $info['ends'] = $info['ends']?explode(',',$info['ends']):[];
  76. }
  77. $this->assign('info',$info);
  78. }
  79. $time = (new \app\common\model\Time())->getByOrgIdList();
  80. $ends = (new \app\common\model\Address())->getListByType(2);
  81. $this->assign('time',$time);
  82. $this->assign('ends',$ends);
  83. $this->assign('cate',$this->model->cate);
  84. $this->assign('priority',$this->model->priority);
  85. $this->assign('time',$time);
  86. return $this->fetch();
  87. }
  88. }
  89. /**
  90. * 删除记录
  91. * @param int $id
  92. */
  93. public function del($id=0){
  94. if(!$id){
  95. $this->error('参数错误');
  96. }
  97. $res = db($this->table)->where('id',$id)->setField('del',1);
  98. if($res){
  99. $this->success('删除成功');
  100. }else{
  101. $this->error('删除失败');
  102. }
  103. }
  104. /**
  105. * 改变字段值
  106. * @param int $fv
  107. * @param string $fn
  108. * @param int $fv
  109. */
  110. public function changeField($id=0,$fn='',$fv=0){
  111. if(!$fn||!$id){
  112. $this->error('参数错误');
  113. }
  114. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  115. if($res){
  116. $this->success('操作成功');
  117. }else{
  118. $this->error('操作失败');
  119. }
  120. }
  121. public function batchSort(){
  122. $data = input('data','','trim');
  123. if(!$data){
  124. $this->error('参数错误');
  125. }
  126. $data = json_decode($data,true);
  127. if(!$data){
  128. $this->error('参数错误');
  129. }
  130. Db::startTrans();
  131. try{
  132. foreach ($data as $k=>$v){
  133. Db::name('convey_cate')->where('id',$v['id'])->setField('sort',$v['sort']);
  134. }
  135. Db::commit();
  136. }catch (Exception $e){
  137. Db::rollback();
  138. $this->error('操作失败');
  139. }
  140. $this->success('操作成功');
  141. }
  142. /**
  143. * 下载点模板
  144. */
  145. public function downloadtem(){
  146. set_time_limit(0);
  147. ini_set("memory_limit","512M");
  148. $header = [
  149. ['title' => '名称', 'name' => 'title','width'=>'20'],
  150. ['title' => '运送对象:1 病人2 普通3 标本4 预约5 药品', 'name' => 'cate','width'=>'20'],
  151. ['title' => '备注', 'name' => 'remark','width'=>'20'],
  152. ];
  153. $lists = [
  154. [
  155. 'title' => '名称',
  156. 'cate' => '1',
  157. 'remark' => '备注',
  158. ]
  159. ];
  160. $filename = '运送类型模板';
  161. ExcelUtil::export($filename,$header,$lists);
  162. }
  163. public function import(){
  164. return $this->fetch();
  165. }
  166. /**
  167. * 导入
  168. */
  169. public function importexcel(){
  170. set_time_limit(0);
  171. ini_set("memory_limit", -1);
  172. ob_flush();//清空缓存
  173. flush();//刷新缓存
  174. try{
  175. $cols = ['title','cate'];
  176. $lists = ExcelUtil::importExcel('file',$cols,2);
  177. if($lists === false){
  178. exit(ExcelUtil::getError());
  179. }
  180. if(empty($lists)){
  181. exit('文件内容为空');
  182. }
  183. // $timeId = 18;
  184. foreach ($lists as $k=>$v){
  185. if($v['title']){
  186. $check= Db::name('convey_cate')
  187. ->where('title',$v['title'])
  188. ->where('cate',$v['cate'])
  189. // ->where('time_id',$timeId)
  190. ->where('del',0)
  191. ->where('org_id',$this->orgId)
  192. ->find();
  193. if($check){
  194. $msg = "第".($k+2)."行,已存在,导入失败";
  195. echo "<font color=\"red\">".$msg."</font><br />";
  196. continue;
  197. }
  198. $dt = [
  199. 'org_id'=>$this->orgId,
  200. 'title'=>$v['title'],
  201. // 'time_id'=>$timeId,
  202. 'cate'=>$v['cate'],
  203. 'priority'=>2,
  204. 'create_time'=>date('Y-m-d H:i:s'),
  205. ];
  206. $inset = Db::name('convey_cate')->insert($dt);
  207. if(!$inset){
  208. $msg = "第".($k+2)."行,导入失败";
  209. echo "<font color=\"red\">".$msg."</font><br />";
  210. continue;
  211. }
  212. }
  213. }
  214. echo "<font color=\"green\">导入完成</font><br />";
  215. }catch (Exception $e){
  216. trace($e->getMessage(),'error');
  217. echo $e->getMessage();
  218. echo "<font color=\"red\">数据异常,已停止导入</font><br />";
  219. }
  220. }
  221. }