Address.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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){
  45. if($orgId >0){
  46. $map[] = ['org_id','=',$orgId];
  47. }else{
  48. $map[] = ['org_id','=',cur_org_id()];
  49. }
  50. $map[] = ['del','=',0];
  51. $map[] = ['enable','=',1];
  52. $map[]=['','exp',Db::raw("FIND_IN_SET($type,types)")];
  53. $lists = Db::name('address')
  54. ->where($map)
  55. ->order('id desc')
  56. ->field('id,title,remark,sn,x,y')
  57. ->select();
  58. return $lists?$lists:[];
  59. }
  60. /**
  61. * 根据类型获取地点数量
  62. * @param $type
  63. */
  64. public function getListByTypeCount($type,$orgId=0){
  65. if($orgId >0){
  66. $map[] = ['org_id','=',$orgId];
  67. }else{
  68. $map[] = ['org_id','=',cur_org_id()];
  69. }
  70. $map[] = ['del','=',0];
  71. $map[] = ['enable','=',1];
  72. $map[]=['','exp',Db::raw("FIND_IN_SET($type,types)")];
  73. $count = Db::name('address')
  74. ->where($map)
  75. ->count();
  76. return $count;
  77. }
  78. // 类型获取字符串
  79. public function getTypeStr($ts){
  80. $types = $this->getTypes();
  81. $tstr = [];
  82. if(is_array($ts)){
  83. foreach ($ts as $val){
  84. $tstr[] = $types[$val];
  85. }
  86. }else{
  87. if(isset($types[$ts])){
  88. $tstr[] = $types[$ts];
  89. }
  90. }
  91. return implode(',',$tstr);
  92. }
  93. // 获取地点的类型
  94. public function getTypes(){
  95. return [1=>'报修',2=>'运送',3=>'巡更',4=>'巡视',5=>'巡查',6=>'巡检',7=>'医废',8=>'专项保洁',9=>'被服管理',10=>'日常工作'];
  96. }
  97. /**
  98. * 检查地点二维码是否合法
  99. * @param $code 二维码内容
  100. * @param $type 地点类型
  101. * @param int $orgId 组织
  102. * @return array/bool false=不合法
  103. */
  104. public function checkAddrCode($code,$type,$orgId=0){
  105. if(!$code){
  106. return false;
  107. }
  108. $addrs = get_qrcode_arr($code);
  109. if(!$addrs || $addrs['ucode'] != config('app.ucode') || $addrs['type'] != 'address'){
  110. return false;
  111. }
  112. // 检查类型
  113. $ainfo = Db::name('address')->where('enable',1)->where('id',$addrs['id'])->where('org_id',$orgId)->where('del',0)->find();
  114. if(!$ainfo){
  115. return false;
  116. }
  117. $types = $ainfo['types']?explode(',',$ainfo['types']):[];
  118. if(!$types || !in_array($type,$types)){
  119. return false;
  120. }
  121. $addrs['title'] = $ainfo['title'];
  122. return $addrs;
  123. }
  124. }