Address.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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($id > 0){
  17. $data['update_time'] = date('Y-m-d H:i:s');
  18. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  19. }else{
  20. $data['create_time'] = date('Y-m-d H:i:s');
  21. $ret = $this->allowField(true)->save($data);
  22. }
  23. if(!$ret){
  24. $this->error = '操作失败';
  25. return false;
  26. }
  27. return true;
  28. }
  29. /**
  30. * 根据类型获取地点列表
  31. * @param $type
  32. */
  33. public function getListByType($type,$orgId=0){
  34. if($orgId >0){
  35. $map[] = ['org_id','=',$orgId];
  36. }else{
  37. $map[] = ['org_id','=',cur_org_id()];
  38. }
  39. $map[] = ['del','=',0];
  40. $map[] = ['enable','=',1];
  41. $map[]=['','exp',Db::raw("FIND_IN_SET($type,types)")];
  42. $lists = Db::name('address')
  43. ->where($map)
  44. ->order('id desc')
  45. ->field('id,title,remark,sn,x,y')
  46. ->select();
  47. return $lists?$lists:[];
  48. }
  49. /**
  50. * 根据类型获取地点数量
  51. * @param $type
  52. */
  53. public function getListByTypeCount($type,$orgId=0){
  54. if($orgId >0){
  55. $map[] = ['org_id','=',$orgId];
  56. }else{
  57. $map[] = ['org_id','=',cur_org_id()];
  58. }
  59. $map[] = ['del','=',0];
  60. $map[] = ['enable','=',1];
  61. $map[]=['','exp',Db::raw("FIND_IN_SET($type,types)")];
  62. $count = Db::name('address')
  63. ->where($map)
  64. ->count();
  65. return $count;
  66. }
  67. // 类型获取字符串
  68. public function getTypeStr($ts){
  69. $types = $this->getTypes();
  70. $tstr = [];
  71. if(is_array($ts)){
  72. foreach ($ts as $val){
  73. $tstr[] = $types[$val];
  74. }
  75. }else{
  76. if(isset($types[$ts])){
  77. $tstr[] = $types[$ts];
  78. }
  79. }
  80. return implode(',',$tstr);
  81. }
  82. // 获取地点的类型
  83. public function getTypes(){
  84. return [1=>'报修',2=>'运送',3=>'巡更',4=>'巡视',5=>'巡查',6=>'巡检',7=>'医废',8=>'专项保洁'];
  85. }
  86. /**
  87. * 检查地点二维码是否合法
  88. * @param $code 二维码内容
  89. * @param $type 地点类型
  90. * @param int $orgId 组织
  91. * @return array/bool false=不合法
  92. */
  93. public function checkAddrCode($code,$type,$orgId=0){
  94. if(!$code){
  95. return false;
  96. }
  97. $addrs = get_qrcode_arr($code);
  98. if(!$addrs || $addrs['ucode'] != config('app.ucode') || $addrs['type'] != 'address'){
  99. return false;
  100. }
  101. // 检查类型
  102. $ainfo = Db::name('address')->where('enable',1)->where('id',$addrs['id'])->where('org_id',$orgId)->where('del',0)->find();
  103. if(!$ainfo){
  104. return false;
  105. }
  106. $types = $ainfo['types']?explode(',',$ainfo['types']):[];
  107. if(!$types || !in_array($type,$types)){
  108. return false;
  109. }
  110. $addrs['title'] = $ainfo['title'];
  111. return $addrs;
  112. }
  113. }