Consult.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\users;
  10. use app\admin\service\Service;
  11. use app\common\models\goods\Goods as GoodsModel;
  12. use app\common\models\users\Users as UsersModel;
  13. use app\admin\model\users\UsersConsult as UsersConsultModel;
  14. use think\facade\Session;
  15. class Consult extends Service {
  16. /**
  17. * 列表
  18. * @param $data
  19. * @return array
  20. * @throws \think\db\exception\DataNotFoundException
  21. * @throws \think\db\exception\DbException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. */
  24. public static function getList($data){
  25. $condition = [];
  26. $condition[] = ["pid","=",0];
  27. $key = $data["key"]??[];
  28. if(isset($key["cat_id"]) && $key["cat_id"] != '-1'){
  29. $filed = $key["cat_id"] == 0 ? "users.username" : "goods.title";
  30. $condition[] = [$filed,"like",'%'.$key["title"].'%'];
  31. }
  32. $count = UsersConsultModel::withJoin(['goods','users'])->where($condition)->count();
  33. $result = UsersConsultModel::withJoin(['goods','users'])->where($condition)->page($data["page"]??1,$data["limit"]??10)->order("users_consult.id","desc")->select()->toArray();
  34. return [ "count"=>$count, "data"=>array_map(function ($res){
  35. $res["username"] = getUserName($res);
  36. return $res;
  37. },$result) ];
  38. }
  39. /**
  40. * 详情
  41. * @param $id
  42. * @return array
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. */
  47. public static function detail($id){
  48. if(!$row=UsersConsultModel::where("id",$id)->find()){
  49. throw new \Exception("您要查找的内容不存在!");
  50. }
  51. $row["goods_name"] = GoodsModel::where("id",$row["goods_id"])->value("title");
  52. $users = UsersModel::where("id",$row["user_id"])->find();
  53. $row["username"] = getUserName($users);
  54. $row["children"] = UsersConsultModel::where(["pid"=>$row["id"]])->select()->toArray();
  55. foreach($row["children"] as $key=>$value){
  56. $row["children"][$key]["username"] = $value["user_id"] == 0 ? "管理回复" : "会员回复";
  57. }
  58. return [ "data"=>$row ];
  59. }
  60. /**
  61. * 保存数据
  62. * @param $data
  63. * @return bool
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. public static function save($data){
  69. if(empty($data["content"])){
  70. throw new \Exception("请填写回复内容",0);
  71. }
  72. $data["admin_id"] = Session::get("system_user_id");
  73. $data["reply_time"] = time();
  74. $data["status"] = 1;
  75. if($obj=UsersConsultModel::where("id",$data["id"])->find()){
  76. $obj->save($data);
  77. $data["user_id"] = $obj["id"];
  78. $data["goods_id"] = $obj["id"];
  79. }
  80. $data["pid"] = $data["id"];
  81. unset($data["id"]);
  82. UsersConsultModel::create($data);
  83. return true;
  84. }
  85. /**
  86. * 删除
  87. * @param $id
  88. * @return bool
  89. */
  90. public static function delete($id){
  91. if(UsersConsultModel::where("id",$id)->delete()){
  92. return UsersConsultModel::where("pid",$id)->delete();
  93. }
  94. return false;
  95. }
  96. }