Resident.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace app\worker\controller;
  3. use app\worker\BaseController;
  4. use app\common\model\ResidentLog as ResidentLogModel;
  5. use app\common\model\User as UserModel;
  6. use app\common\model\Worker as WorkerModel;
  7. use app\common\model\Broker as BrokerModel;
  8. use app\common\model\Resident as ResidentModel;
  9. use app\common\validate\Resident as ResidentValidate;
  10. use think\exception\ValidateException;
  11. class Resident extends BaseController
  12. {
  13. public function residentlist()
  14. {
  15. $workerlist = WorkerModel::order(['id' => 'desc'])->select();
  16. return view('resident/residentlist', [
  17. 'workerlist' => $workerlist,
  18. ]);
  19. }
  20. public function residentForm()
  21. {
  22. $id = input('id/d, 0');
  23. $resident = ResidentModel::findOrEmpty($id);
  24. return view('resident/residentform', [
  25. 'resident' => $resident,
  26. ]);
  27. }
  28. public function editresident()
  29. {
  30. $id = input('id/d');
  31. $data = [
  32. 'title' => input('title/s', ""),
  33. 'mobile' => input('mobile/s', ""),
  34. 'weixin' => input('weixin/s', ""),
  35. 'qq' => input('qq/s', ""),
  36. 'province' => input('province/s', ""),
  37. 'city' => input('city/s', ""),
  38. 'district' => input('district/s', ""),
  39. 'details' => input('details/s', ""),
  40. 'status' => input('status/d') == 1 ? 1 : 2,
  41. ];
  42. $workerid = $this->access_worker['id'];
  43. if (empty($id)) {
  44. $vdata = [
  45. 'id' => $id,
  46. 'mobile' => input('mobile/s'),
  47. ];
  48. try {
  49. validate(ResidentValidate::class)->check($vdata);
  50. } catch (ValidateException $e) {
  51. exit(json_encode([
  52. 'code' => 1,
  53. 'msg' => $e->getError(),
  54. ]));
  55. }
  56. $user = UserModel::where(['mobile' => input('usermobile/s', '')])->findOrEmpty();
  57. if ($user->isEmpty()) {
  58. exit(json_encode([
  59. 'code' => 1,
  60. 'msg' => "关联的用户不存在。",
  61. ]));
  62. }
  63. $broker = BrokerModel::where('userid', $user['id'])->findOrEmpty();
  64. if (empty($broker)) {
  65. exit(json_encode([
  66. 'code' => 1,
  67. 'msg' => "只有经纪人才能关联。",
  68. ]));
  69. }
  70. $resident_user = ResidentModel::where('userid', $user->id)->find();
  71. if (!empty($resident_user)) {
  72. exit(json_encode([
  73. 'code' => 1,
  74. 'msg' => "该用户已是经纪人。",
  75. ]));
  76. }
  77. $data['userid'] = $user->id;
  78. $data['workerid'] = $broker['workerid'];
  79. $data['agentid'] = $broker['agentid'];
  80. $data['createtime'] = time();
  81. ResidentModel::create($data);
  82. } else {
  83. $resident = ResidentModel::where('workerid', $workerid)->where('id', $id)->findOrEmpty();
  84. if (empty($resident)) {
  85. exit(json_encode([
  86. 'code' => 1,
  87. 'msg' => '信息不存在',
  88. ]));
  89. }
  90. $resident->save($data);
  91. }
  92. exit(json_encode([
  93. 'code' => 0,
  94. ]));
  95. }
  96. public function fieldresident()
  97. {
  98. $workerid = $this->access_worker['id'];
  99. $id = input('id/d', 0);
  100. $resident = ResidentModel::where('workerid', $workerid)->where('id', $id)->findOrEmpty();
  101. if ($resident->isEmpty()) {
  102. exit(json_encode([
  103. 'code' => 1,
  104. 'msg' => "信息不存在",
  105. ]));
  106. } else {
  107. $resident->save([
  108. input('field/s') => input('value'),
  109. ]);
  110. }
  111. exit(json_encode([
  112. 'code' => 0,
  113. ]));
  114. }
  115. public function delresident()
  116. {
  117. $access_admin = session('access_admin');
  118. $password = input('password');
  119. if ($access_admin['password'] !== md5($password)) {
  120. exit(json_encode([
  121. 'code' => 1,
  122. 'msg' => "操作密码验证失败",
  123. ]));
  124. }
  125. $idarr = input('idarr/a');
  126. $workerid = $this->access_worker['id'];
  127. $result = ResidentModel::whereIn('id', $idarr)->where('workerid', $workerid)->delete();
  128. if ($result) {
  129. exit(json_encode([
  130. 'code' => 0,
  131. 'msg' => "",
  132. ]));
  133. }
  134. exit(json_encode([
  135. 'code' => 1,
  136. 'msg' => "删除失败,请稍后重试",
  137. ]));
  138. }
  139. public function listresident()
  140. {
  141. $limit = input('limit/d', 20);
  142. $page = input('page/d', 1);
  143. $workerid = $this->access_worker['id'];
  144. $map = [
  145. ['workerid', '=', $workerid],
  146. ];
  147. $keywords = input('keywords/s');
  148. if (!empty($keywords)) {
  149. $map[] = ['title', 'like', '%' . $keywords . '%'];
  150. }
  151. $status = input('status/d');
  152. if (!empty($status)) {
  153. $map[] = ['status', '=', $status];
  154. }
  155. $list = ResidentModel::with(['worker', 'user'])->where($map)->order('id', 'DESC')->limit($limit)->page($page)->append(['status_text'])->select();
  156. $count = ResidentModel::where($map)->count();
  157. if ($count == 0) {
  158. exit(json_encode([
  159. 'code' => 1,
  160. 'msg' => "未查询到数据",
  161. ]));
  162. }
  163. exit(json_encode([
  164. 'code' => 0,
  165. 'msg' => "",
  166. 'count' => $count,
  167. 'data' => $list,
  168. ]));
  169. }
  170. // 用户跟进记录
  171. public function follow()
  172. {
  173. return view('resident/follow');
  174. }
  175. public function listfollow()
  176. {
  177. $limit = input('limit/d', 20);
  178. $page = input('page/d', 1);
  179. $workerid = $this->access_worker['id'];
  180. $map = [
  181. ['workerid', '=', $workerid],
  182. ];
  183. $mobile = input('mobile/s');
  184. if (!empty($mobile)) {
  185. $resident = ResidentModel::where('mobile', $mobile)->findOrEmpty();
  186. if (empty($resident)) {
  187. $map[] = ['id', '=', 0];
  188. } else {
  189. $map[] = ['residentid', '=', $resident['id']];
  190. }
  191. }
  192. $list = ResidentLogModel::with(['resident', 'worker'])->where($map)->order('createtime', 'DESC')->limit($limit)->page($page)->select();
  193. $count = ResidentLogModel::where($map)->count();
  194. if ($count == 0) {
  195. exit(json_encode([
  196. 'code' => 1,
  197. 'msg' => "未查询到数据",
  198. ]));
  199. }
  200. exit(json_encode([
  201. 'code' => 0,
  202. 'msg' => "",
  203. 'count' => $count,
  204. 'data' => $list,
  205. ]));
  206. }
  207. }