ConveyCate.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. $times = model('Time')->getByOrgIdList();
  40. foreach ($lists as $k=>$v){
  41. $lists[$k]['cateName'] = $this->model->cate[$v['cate']];
  42. $lists[$k]['priorityName'] = isset($this->model->priority[$v['priority']])?$this->model->priority[$v['priority']]:"";
  43. $lists[$k]['timeName'] = db('time')
  44. ->where('id',$v['time_id'])->value('title');
  45. $lists[$k]['endsName'] =!empty($v['ends'])?implode(',',db('address')
  46. ->whereIn('id',$v['ends'])->column('title')):'';
  47. $lists[$k]['startsName'] =!empty($v['starts'])?implode(',',db('address')
  48. ->whereIn('id',$v['starts'])->column('title')):'';
  49. $lists[$k]['priorityList'] = $this->model->priority;
  50. $lists[$k]['times'] = $times;
  51. }
  52. //数据返回
  53. $totalCount = db($this->table)->where($map)->count();
  54. $totalPage = ceil($totalCount/$length);
  55. $result['page'] = $page;
  56. $result['total'] = $totalPage;
  57. $result['records'] = $totalCount;
  58. $result['rows'] = $lists;
  59. return json($result);
  60. }else{
  61. $this->assign('m_name','运送类型');
  62. return $this->fetch();
  63. }
  64. }
  65. /**
  66. * 新增/编辑
  67. */
  68. public function add($id=0){
  69. if(request()->isPost()){
  70. $res = $this->model->updates();
  71. if($res){
  72. $this->success('操作成功',url('index'));
  73. }else{
  74. $this->error($this->model->getError());
  75. }
  76. }else{
  77. if($id){
  78. $info =db($this->table)->where('id',$id)->find();
  79. if($info){
  80. $info['ends'] = $info['ends']?explode(',',$info['ends']):[];
  81. $info['starts'] = $info['starts']?explode(',',$info['starts']):[];
  82. }
  83. $this->assign('info',$info);
  84. }
  85. $time = (new \app\common\model\Time())->getByOrgIdList();
  86. $ends = (new \app\common\model\Address())->getListByType(2);
  87. $this->assign('time',$time);
  88. $this->assign('ends',$ends);
  89. $this->assign('cate',$this->model->cate);
  90. $this->assign('priority',$this->model->priority);
  91. $this->assign('time',$time);
  92. return $this->fetch();
  93. }
  94. }
  95. /**
  96. * 删除记录
  97. * @param int $id
  98. */
  99. public function del($id=0){
  100. if(!$id){
  101. $this->error('参数错误');
  102. }
  103. $res = db($this->table)->where('id',$id)->setField('del',1);
  104. if($res){
  105. $this->success('删除成功');
  106. }else{
  107. $this->error('删除失败');
  108. }
  109. }
  110. /**
  111. * 改变字段值
  112. * @param int $fv
  113. * @param string $fn
  114. * @param int $fv
  115. */
  116. public function changeField($id=0,$fn='',$fv=0){
  117. if(!$fn||!$id){
  118. $this->error('参数错误');
  119. }
  120. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  121. if($res){
  122. $this->success('操作成功');
  123. }else{
  124. $this->error('操作失败');
  125. }
  126. }
  127. public function batchSort(){
  128. $data = input('data','','trim');
  129. if(!$data){
  130. $this->error('参数错误');
  131. }
  132. $data = json_decode($data,true);
  133. if(!$data){
  134. $this->error('参数错误');
  135. }
  136. Db::startTrans();
  137. try{
  138. foreach ($data as $k=>$v){
  139. Db::name('convey_cate')->where('id',$v['id'])->setField('sort',$v['sort']);
  140. }
  141. Db::commit();
  142. }catch (Exception $e){
  143. Db::rollback();
  144. $this->error('操作失败');
  145. }
  146. $this->success('操作成功');
  147. }
  148. /**
  149. * 下载点模板
  150. */
  151. public function downloadtem(){
  152. set_time_limit(0);
  153. ini_set("memory_limit","512M");
  154. $header = [
  155. ['title' => '名称', 'name' => 'title','width'=>'20'],
  156. ['title' => '时间代码(时间代码id)', 'name' => 'time_id','width'=>'20'],
  157. ['title' => '运送对象(1 病人2 普通3 标本4 预约5 药品)', 'name' => 'cate','width'=>'20'],
  158. ['title' => '运送积分', 'name' => 'score','width'=>'20'],
  159. ['title' => '开始地点(地点id多个用英文,隔开)', 'name' => 'starts','width'=>'20'],
  160. ['title' => '结束地点(地点id多个用英文,隔开)', 'name' => 'ends','width'=>'20'],
  161. ['title' => '优先级( 2=常规 3=紧急)', 'name' => 'priority','width'=>'20'],
  162. ['title' => '排序', 'name' => 'sort','width'=>'20'],
  163. ];
  164. $lists = [
  165. [
  166. 'title' => '名称',
  167. 'time_id' => '1',
  168. 'cate' => '1',
  169. 'score' => '1',
  170. 'starts' => '1,2',
  171. 'ends' => '3,4',
  172. 'priority' => '2',
  173. 'sort' => '50',
  174. ]
  175. ];
  176. $filename = '运送类型模板';
  177. ExcelUtil::export($filename,$header,$lists);
  178. }
  179. public function import(){
  180. return $this->fetch();
  181. }
  182. /**
  183. * 导入
  184. */
  185. public function importexcel(){
  186. set_time_limit(0);
  187. ini_set("memory_limit", -1);
  188. ob_flush();//清空缓存
  189. flush();//刷新缓存
  190. try{
  191. $cols = ['title','time_id','cate','score','starts','ends','priority','sort'];
  192. $lists = ExcelUtil::importExcel('file',$cols,2);
  193. if($lists === false){
  194. exit(ExcelUtil::getError());
  195. }
  196. if(empty($lists)){
  197. exit('文件内容为空');
  198. }
  199. // $timeId = 18;
  200. foreach ($lists as $k=>$v){
  201. // $aa = mb_substr($v['title'],0,2);
  202. // $v['cate'] = 2;
  203. // if($aa == '陪检'){
  204. // $v['cate'] == 1;
  205. // }
  206. // if($aa == '预约'){
  207. // $v['cate'] == 4;
  208. // }
  209. // if($aa == '取单'){
  210. // $v['cate'] == 3;
  211. // }
  212. if($v['title']){
  213. $check= Db::name('convey_cate')
  214. ->where('title',$v['title'])
  215. // ->where('cate',$v['cate'])
  216. // ->where('time_id',$timeId)
  217. ->where('del',0)
  218. ->where('org_id',$this->orgId)
  219. ->find();
  220. if($check){
  221. $msg = "第".($k+2)."行,已存在,导入失败";
  222. echo "<font color=\"red\">".$msg."</font><br />";
  223. continue;
  224. }
  225. if(!$v['time_id']){
  226. $msg = "第".($k+2)."行,时间代码不能为空";
  227. echo "<font color=\"red\">".$msg."</font><br />";
  228. continue;
  229. }
  230. if($v['starts']){
  231. $saddr = explode(',',$v['starts']);
  232. $addrCount = Db::name('address')->where('org_id',$this->orgId)->where('del',0)->where('find_in_set(2,types)')->whereIn('id',$saddr)->count();
  233. if(count($saddr) != $addrCount){
  234. $msg = "第".($k+2)."行,地点id录入不正确";
  235. echo "<font color=\"red\">".$msg."</font><br />";
  236. continue;
  237. }
  238. }
  239. if($v['ends']){
  240. $eaddr = explode(',',$v['ends']);
  241. $addrCount = Db::name('address')->where('org_id',$this->orgId)->where('del',0)->whereIn('id',$eaddr)->count();
  242. if(count($eaddr) != $addrCount){
  243. $msg = "第".($k+2)."行,地点id录入不正确";
  244. echo "<font color=\"red\">".$msg."</font><br />";
  245. continue;
  246. }
  247. }
  248. if(!$v['cate']){
  249. $msg = "第".($k+2)."行,运送对象不能为空";
  250. echo "<font color=\"red\">".$msg."</font><br />";
  251. continue;
  252. }
  253. $dt = [
  254. 'org_id'=>$this->orgId,
  255. 'title'=>$v['title'],
  256. 'time_id'=>$v['time_id'],
  257. 'starts'=>$v['starts'],
  258. 'ends'=>$v['ends'],
  259. 'cate'=>$v['cate'],
  260. 'score'=>$v['score']?$v['score']:1,
  261. 'sort'=>$v['sort']?$v['sort']:50,
  262. 'priority'=>$v['priority']?$v['priority']:2,
  263. 'create_time'=>date('Y-m-d H:i:s'),
  264. ];
  265. $inset = Db::name('convey_cate')->insert($dt);
  266. if(!$inset){
  267. $msg = "第".($k+2)."行,导入失败";
  268. echo "<font color=\"red\">".$msg."</font><br />";
  269. continue;
  270. }
  271. }
  272. }
  273. echo "<font color=\"green\">导入完成</font><br />";
  274. }catch (Exception $e){
  275. trace($e->getMessage(),'error');
  276. echo $e->getMessage();
  277. echo "<font color=\"red\">数据异常,已停止导入</font><br />";
  278. }
  279. }
  280. public function batchSave(){
  281. $param = input('data','','trim');
  282. $data = json_decode($param,true);
  283. $scoreList = $data['score'] ?array_remove_empty($data['score']):[];
  284. $yxjList = $data['yxj'] ? array_remove_empty($data['yxj']):[];
  285. $timesList = $data['time'] ? array_remove_empty($data['time']) :[];
  286. if(!$scoreList && !$yxjList && !$timesList){
  287. $this->error('参数错误');
  288. }
  289. Db::startTrans();
  290. try{
  291. foreach ($scoreList as $k=>$v){
  292. Db::name('convey_cate')->where('id',$v['id'])->setField('score',$v['score']);
  293. }
  294. foreach ($yxjList as $k=>$v){
  295. Db::name('convey_cate')->where('id',$v['id'])->setField('priority',$v['priority']);
  296. }
  297. foreach ($timesList as $k=>$v){
  298. Db::name('convey_cate')->where('id',$v['id'])->setField('time_id',$v['timeId']);
  299. }
  300. Db::commit();
  301. }catch (Exception $e){
  302. Db::rollback();
  303. $this->error($e->getMessage());
  304. }
  305. $this->success('操作成功');
  306. }
  307. }