Address.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class Address extends Base
  5. {
  6. public function updates(){
  7. $data = request()->post();
  8. $data['org_id'] = cur_org_id();
  9. $result = validate('Address')->check($data,[],'');
  10. if(true !== $result){
  11. $this->error = validate('Address')->getError();
  12. return false;
  13. }
  14. $id = $data['id'];
  15. unset($data['id']);
  16. if($data['sn']){
  17. $deviceLog = Db::name('device_log')->where('sn',$data['sn'])->where('type',2)->where('del',0)->find();
  18. $txt = '添加';
  19. if($id > 0){
  20. $txt = '修改';
  21. }
  22. if(!$deviceLog){
  23. $this->error = '该设备无法'.$txt.',请联系管理员';
  24. return false;
  25. }
  26. }
  27. if($id > 0){
  28. $data['update_time'] = date('Y-m-d H:i:s');
  29. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  30. }else{
  31. $data['create_time'] = date('Y-m-d H:i:s');
  32. $ret = $this->allowField(true)->save($data);
  33. }
  34. if(!$ret){
  35. $this->error = '操作失败';
  36. return false;
  37. }
  38. return true;
  39. }
  40. /**
  41. * 根据类型获取地点列表
  42. * @param $type
  43. */
  44. public function getListByType($type,$orgId=0,$conveyCateId=0,$start=0){
  45. if($orgId >0){
  46. $map[] = ['org_id','=',$orgId];
  47. }else{
  48. $map[] = ['org_id','=',cur_org_id()];
  49. }
  50. if($conveyCateId > 0){
  51. $conveyCate = Db::name("convey_cate")
  52. ->where('id',$conveyCateId)
  53. ->find();
  54. $addrs = [];
  55. if($conveyCate){
  56. if($start == 1){
  57. $addrs = $conveyCate['starts']?explode(',',$conveyCate['starts']):[];
  58. }else{
  59. $addrs = $conveyCate['ends']?explode(',',$conveyCate['ends']):[];
  60. }
  61. }
  62. if($addrs){
  63. $map[] = ['id','in',$addrs];
  64. }
  65. }
  66. $map[] = ['del','=',0];
  67. $map[] = ['enable','=',1];
  68. $map[]=['','exp',Db::raw("FIND_IN_SET($type,types)")];
  69. $lists = Db::name('address')
  70. ->where($map)
  71. ->order('id desc')
  72. ->field('id,title,remark,sn,x,y,dep_id')
  73. ->select();
  74. return $lists?$lists:[];
  75. }
  76. public function getListByTypes($maps,$type,$orgId=0){
  77. if($orgId >0){
  78. $map[] = ['org_id','=',$orgId];
  79. }else{
  80. $map[] = ['org_id','=',cur_org_id()];
  81. }
  82. $map[] = ['del','=',0];
  83. $map[] = ['enable','=',1];
  84. if(!empty($maps)){
  85. $map[] = ['id','in',$maps];
  86. }
  87. $map[]=['','exp',Db::raw("FIND_IN_SET($type,types)")];
  88. $lists = Db::name('address')
  89. ->where($map)
  90. ->order('id desc')
  91. ->field('id,title,remark,sn,x,y')
  92. ->select();
  93. return $lists?$lists:[];
  94. }
  95. /**
  96. * 根据类型获取地点数量
  97. * @param $type
  98. */
  99. public function getListByTypeCount($type,$orgId=0){
  100. if($orgId >0){
  101. $map[] = ['org_id','=',$orgId];
  102. }else{
  103. $map[] = ['org_id','=',cur_org_id()];
  104. }
  105. $map[] = ['del','=',0];
  106. $map[] = ['enable','=',1];
  107. $map[]=['','exp',Db::raw("FIND_IN_SET($type,types)")];
  108. $count = Db::name('address')
  109. ->where($map)
  110. ->count();
  111. return $count;
  112. }
  113. // 类型获取字符串
  114. public function getTypeStr($ts){
  115. $types = $this->getTypes();
  116. $tstr = [];
  117. if(is_array($ts)){
  118. foreach ($ts as $val){
  119. $tstr[] = $types[$val];
  120. }
  121. }else{
  122. if(isset($types[$ts])){
  123. $tstr[] = $types[$ts];
  124. }
  125. }
  126. return implode(',',$tstr);
  127. }
  128. // 获取地点的类型
  129. public function getTypes(){
  130. return [1=>'报修',2=>'运送',3=>'安保巡更',4=>'保洁巡视',5=>'总部巡查',6=>'设备巡检',7=>'医废',8=>'专项保洁',9=>'被服管理',10=>'日常工作'];
  131. }
  132. /**
  133. * 检查地点二维码是否合法
  134. * @param $code 二维码内容
  135. * @param $type 地点类型
  136. * @param int $orgId 组织
  137. * @return array/bool false=不合法
  138. */
  139. public function checkAddrCode($code,$type,$orgId=0){
  140. if(!$code){
  141. return false;
  142. }
  143. $addrs = get_qrcode_arr($code);
  144. if(!$addrs || $addrs['ucode'] != config('app.ucode') || $addrs['type'] != 'address'){
  145. return false;
  146. }
  147. // 检查类型
  148. $ainfo = Db::name('address')->where('enable',1)->where('id',$addrs['id'])->where('org_id',$orgId)->where('del',0)->find();
  149. if(!$ainfo){
  150. return false;
  151. }
  152. $types = $ainfo['types']?explode(',',$ainfo['types']):[];
  153. if(!$types || !in_array($type,$types)){
  154. return false;
  155. }
  156. $addrs['title'] = $ainfo['title'];
  157. return $addrs;
  158. }
  159. }