Area.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\service\products;
  10. use app\admin\service\Service;
  11. use app\common\models\Area as AreaModel;
  12. class Area extends Service {
  13. /**
  14. * 列表
  15. * @param $data
  16. * @return array
  17. * @throws \think\db\exception\DataNotFoundException
  18. * @throws \think\db\exception\DbException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. */
  21. public static function getList($data){
  22. $count = AreaModel::where("pid",$data["pid"]??0)->count();
  23. $list = array_map(function ($res){
  24. $res['count'] = AreaModel::where(['pid' => $res['id']])->count();
  25. return $res;
  26. },AreaModel::where("pid",$data["pid"]??0)->page($data["page"]??1,$data["limit"]??10)->order("id","asc")->select()->toArray());
  27. return [ "count"=>$count,"data"=>$list ];
  28. }
  29. /**
  30. * 详情
  31. * @param $params
  32. * @return array
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public static function detail($params){
  38. return [
  39. "data"=>AreaModel::where("id",$params["id"])->find()??[],
  40. "pid"=>$params["pid"]??0
  41. ];
  42. }
  43. /**
  44. * 保存数据
  45. * @param $data
  46. * @return AreaModel|bool|\think\Model
  47. */
  48. public static function save($data){
  49. if(AreaModel::where("id",$data["id"])->count()){
  50. return AreaModel::where("id",$data["id"])->save($data);
  51. }else{
  52. return AreaModel::create($data);
  53. }
  54. }
  55. /**
  56. * 删除
  57. * @param $id
  58. * @return bool
  59. * @throws \Exception
  60. */
  61. public static function delete($id){
  62. if(AreaModel::where('pid',$id)->count()){
  63. throw new \Exception("该地区下有子区域,请先删除!",0);
  64. }
  65. return AreaModel::where($id)->delete();
  66. }
  67. /**
  68. * 更新字段值
  69. * @return AreaModel
  70. */
  71. public static function setFields(){
  72. $data = self::getFields();
  73. return AreaModel::where("id",$data["id"])->update([$data["name"]=>$data["value"]]);
  74. }
  75. /**
  76. * 获取省级地区列表
  77. * @param $id
  78. * @return string
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public static function getArea($id){
  84. $result = AreaModel::where(['pid'=>$id])->select()->toArray();
  85. $string = '<option value="">请选择</option>';
  86. foreach($result as $val){
  87. $string .= '<option value="'.$val['id'].'">'.$val['name'].'</option>';
  88. }
  89. return $string;
  90. }
  91. /**
  92. * 获取所有地址信息
  93. * @return array
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. public static function getDistribution(){
  99. $result = AreaModel::where(['level'=>1])->order("id","ASC")->select()->toArray();
  100. foreach ($result as $key => $val) {
  101. $result[$key] = $val;
  102. $result[$key]['children'] = AreaModel::where(['pid' => $val["id"]])->select()->toArray();
  103. }
  104. return [ "data"=>$result ];
  105. }
  106. }