Broker.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace app\worker\controller;
  3. use app\common\service\IncomeService;
  4. use app\worker\BaseController;
  5. use app\common\model\Agent as AgentModel;
  6. use app\common\model\Broker as BrokerModel;
  7. use app\common\model\User as UserModel;
  8. use app\common\model\BrokerIncome as BrokerIncomeModel;
  9. use app\common\validate\Broker as BrokerValidate;
  10. use think\exception\ValidateException;
  11. class Broker extends BaseController
  12. {
  13. public function brokerList()
  14. {
  15. $workerid = $this->access_worker['id'];
  16. $agentlist = AgentModel::where('workerid', '=', $workerid)->order(['id' => 'desc'])->select();
  17. return view('broker/brokerlist', [
  18. 'agentlist' => $agentlist,
  19. ]);
  20. }
  21. public function brokerForm()
  22. {
  23. $workerid = $this->access_worker['id'];
  24. $id = input('id/d', 0);
  25. $broker = BrokerModel::where('workerid', '=', $workerid)->findOrEmpty($id);
  26. $agentlist = AgentModel::where('workerid', '=', $workerid)->order(['id' => 'desc'])->select();
  27. $townlist = file_get_contents(root_path('public/static/jscss') . 'village.json');
  28. return view('broker/brokerform', [
  29. 'townlist' => $townlist,
  30. 'agentlist' => $agentlist,
  31. 'broker' => $broker,
  32. 'worker' => $this->access_worker,
  33. ]);
  34. }
  35. public function editBroker()
  36. {
  37. $workerid = $this->access_worker['id'];
  38. $data = [
  39. 'agentid' => input('agentid/d', 0),
  40. 'title' => input('title/s', ""),
  41. 'mobile' => input('mobile/s', ""),
  42. 'weixin' => input('weixin/s', ""),
  43. 'qq' => input('qq/s', ""),
  44. 'province' => input('province/s', ""),
  45. 'city' => input('city/s', ""),
  46. 'district' => input('district/s', ""),
  47. 'region' => input('region/s', ""),
  48. 'details' => input('details/s', ""),
  49. 'powerreport' => input('powerreport/d', 0) == 1 ? 1 : 2,
  50. 'status' => input('status/d', 0) == 1 ? 1 : 2,
  51. 'latitude' => input('latitude/f', 0),
  52. 'longitude' => input('longitude/f', 0),
  53. ];
  54. if (empty($data['latitude']) || empty($data['longitude'])) {
  55. exit(json_encode([
  56. 'code' => 1,
  57. 'msg' => '请选择地理位置',
  58. ]));
  59. }
  60. //镇街
  61. $townvillage = input('townvillage');
  62. if (empty($townvillage)) {
  63. exit(json_encode([
  64. 'code' => 1,
  65. 'msg' => '请选择镇街',
  66. ]));
  67. }
  68. $townvillage = explode(',', $townvillage);
  69. $data['town'] = $townvillage[0];
  70. $data['village'] = $townvillage[1];
  71. if (empty($id)) {
  72. $id = input('id/d', 0);
  73. $vdata = [
  74. 'id' => $id,
  75. 'mobile' => input('mobile/s'),
  76. ];
  77. try {
  78. validate(BrokerValidate::class)->check($vdata);
  79. } catch (ValidateException $e) {
  80. exit(json_encode([
  81. 'code' => 1,
  82. 'msg' => $e->getError(),
  83. ]));
  84. }
  85. $muser = UserModel::where(['mobile' => input('musermobile/s', '')])->findOrEmpty();
  86. if ($muser->isEmpty()) {
  87. exit(json_encode([
  88. 'code' => 1,
  89. 'msg' => "关联的用户不存在。",
  90. ]));
  91. }
  92. $broker_user = BrokerModel::where('userid', $muser->id)->find();
  93. if (!empty($broker_user)) {
  94. exit(json_encode([
  95. 'code' => 1,
  96. 'msg' => "该用户已是经纪人。",
  97. ]));
  98. }
  99. $data['userid'] = $muser->id;
  100. $data['workerid'] = $workerid;
  101. $data['createtime'] = time();
  102. $broker = BrokerModel::create($data);
  103. event('brokerAdd', $broker);
  104. } else {
  105. $broker = BrokerModel::find($id);
  106. $broker->save($data);
  107. }
  108. $muser->save([
  109. 'brokerid' => $broker->id,
  110. ]);
  111. exit(json_encode([
  112. 'code' => 0,
  113. ]));
  114. }
  115. public function fieldBroker()
  116. {
  117. $workerid = $this->access_worker['id'];
  118. $id = input('id/d', 0);
  119. $broker = BrokerModel::where('workerid', '=', $workerid)->findOrEmpty($id);
  120. if ($broker->isEmpty()) {
  121. exit(json_encode([
  122. 'code' => 1,
  123. 'msg' => "信息不存在",
  124. ]));
  125. } else {
  126. $broker->save([
  127. input('field/s') => input('value/s', ""),
  128. ]);
  129. }
  130. exit(json_encode([
  131. 'code' => 0,
  132. ]));
  133. }
  134. public function delBroker()
  135. {
  136. $workerid = $this->access_worker['id'];
  137. $password = input('password');
  138. if ($this->access_worker['password'] !== md5($password)) {
  139. exit(json_encode([
  140. 'code' => 1,
  141. 'msg' => "操作密码验证失败",
  142. ]));
  143. }
  144. $idarr = input('idarr/a');
  145. $user_check = UserModel::whereIn('brokerid', $idarr)->find();
  146. if (!empty($user_check)) {
  147. exit(json_encode([
  148. 'code' => 1,
  149. 'msg' => "该经纪人还有下线,请先转移再删除",
  150. ]));
  151. }
  152. $broker = BrokerModel::where('workerid', '=', $workerid)->whereIn('id', $idarr)->select();
  153. $result = $broker->delete();
  154. if ($result) {
  155. exit(json_encode([
  156. 'code' => 0,
  157. 'msg' => "",
  158. ]));
  159. }
  160. exit(json_encode([
  161. 'code' => 1,
  162. 'msg' => "删除失败,请稍后重试",
  163. ]));
  164. }
  165. public function listBroker()
  166. {
  167. $workerid = $this->access_worker['id'];
  168. $limit = input('limit/d', 20);
  169. $page = input('page/d', 1);
  170. $map = [];
  171. $map[] = ['workerid', '=', $workerid];
  172. $keywords = input('keywords/s');
  173. if (!empty($keywords)) {
  174. $map[] = ['title', 'like', '%' . $keywords . '%'];
  175. }
  176. $status = input('status/d');
  177. if (!empty($status)) {
  178. $map[] = ['status', '=', $status];
  179. }
  180. $agentid = input('agentid/d');
  181. if (!empty($agentid)) {
  182. $map[] = ['agentid', '=', $agentid];
  183. }
  184. $list = BrokerModel::with(['muser', 'worker', 'agent'])->withCount(['user'])->where($map)->order('id', 'DESC')->limit($limit)->page($page)->append(['status_text', 'powerreport_text'])->select();
  185. $count = BrokerModel::where($map)->count();
  186. if ($count == 0) {
  187. exit(json_encode([
  188. 'code' => 1,
  189. 'msg' => "未查询到数据",
  190. ]));
  191. }
  192. exit(json_encode([
  193. 'code' => 0,
  194. 'msg' => "",
  195. 'count' => $count,
  196. 'data' => $list,
  197. ]));
  198. }
  199. public function incomeList()
  200. {
  201. $brokerid = input('brokerid');
  202. if (empty($brokerid)) {
  203. exit("未查询到数据");
  204. }
  205. return view('broker/incomelist', [
  206. 'brokerid' => $brokerid,
  207. ]);
  208. }
  209. public function listIncome()
  210. {
  211. $brokerid = input('brokerid');
  212. if (empty($brokerid)) {
  213. exit(json_encode([
  214. 'code' => 1,
  215. 'msg' => "未查询到数据",
  216. ]));
  217. }
  218. $limit = input('limit/d', 20);
  219. $page = input('page/d', 1);
  220. $map = [
  221. ['brokerid', '=', $brokerid],
  222. ];
  223. $list = BrokerIncomeModel::where($map)->order('id', 'DESC')->limit($limit)->page($page)->append(['status_text', 'powerreport_text'])->select();
  224. $count = BrokerIncomeModel::where($map)->count();
  225. if ($count == 0) {
  226. exit(json_encode([
  227. 'code' => 1,
  228. 'msg' => "未查询到数据",
  229. ]));
  230. }
  231. exit(json_encode([
  232. 'code' => 0,
  233. 'msg' => "",
  234. 'count' => $count,
  235. 'data' => $list,
  236. ]));
  237. }
  238. public function settleIncome()
  239. {
  240. $brokerid = input('brokerid');
  241. $value = input('value');
  242. if (empty($brokerid) || $value <= 0) {
  243. exit(json_encode([
  244. 'code' => 1,
  245. 'msg' => "参数错误",
  246. ]));
  247. }
  248. $broker = BrokerModel::find($brokerid);
  249. if (empty($broker)) {
  250. exit(json_encode([
  251. 'code' => 1,
  252. 'msg' => "未查询到数据",
  253. ]));
  254. }
  255. if ($broker['income'] < $value) {
  256. exit(json_encode([
  257. 'code' => 1,
  258. 'msg' => "经纪人收益不足",
  259. ]));
  260. }
  261. $incomeService = new IncomeService();
  262. $incomeService->add($brokerid, -$value, '商家结算', '劳务公司与经纪人线下结算');
  263. exit(json_encode([
  264. 'code' => 0,
  265. 'msg' => "",
  266. ]));
  267. }
  268. }