Broker.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. ];
  52. //镇街
  53. $townvillage = input('townvillage');
  54. if (empty($townvillage)) {
  55. exit(json_encode([
  56. 'code' => 1,
  57. 'msg' => '请选择镇街',
  58. ]));
  59. }
  60. $townvillage = explode(',', $townvillage);
  61. $data['town'] = $townvillage[0];
  62. $data['village'] = $townvillage[1];
  63. if (empty($id)) {
  64. $id = input('id/d', 0);
  65. $vdata = [
  66. 'id' => $id,
  67. 'mobile' => input('mobile/s'),
  68. ];
  69. try {
  70. validate(BrokerValidate::class)->check($vdata);
  71. } catch (ValidateException $e) {
  72. exit(json_encode([
  73. 'code' => 1,
  74. 'msg' => $e->getError(),
  75. ]));
  76. }
  77. $muser = UserModel::where(['mobile' => input('musermobile/s', '')])->findOrEmpty();
  78. if ($muser->isEmpty()) {
  79. exit(json_encode([
  80. 'code' => 1,
  81. 'msg' => "关联的用户不存在。",
  82. ]));
  83. }
  84. $broker_user = BrokerModel::where('userid', $muser->id)->find();
  85. if (!empty($broker_user)) {
  86. exit(json_encode([
  87. 'code' => 1,
  88. 'msg' => "该用户已是经纪人。",
  89. ]));
  90. }
  91. $data['userid'] = $muser->id;
  92. $data['workerid'] = $workerid;
  93. $data['createtime'] = time();
  94. $broker = BrokerModel::create($data);
  95. event('brokerAdd', $broker);
  96. } else {
  97. $broker = BrokerModel::find($id);
  98. $broker->save($data);
  99. }
  100. $muser->save([
  101. 'brokerid' => $broker->id,
  102. ]);
  103. exit(json_encode([
  104. 'code' => 0,
  105. ]));
  106. }
  107. public function fieldBroker()
  108. {
  109. $workerid = $this->access_worker['id'];
  110. $id = input('id/d', 0);
  111. $broker = BrokerModel::where('workerid', '=', $workerid)->findOrEmpty($id);
  112. if ($broker->isEmpty()) {
  113. exit(json_encode([
  114. 'code' => 1,
  115. 'msg' => "信息不存在",
  116. ]));
  117. } else {
  118. $broker->save([
  119. input('field/s') => input('value/s', ""),
  120. ]);
  121. }
  122. exit(json_encode([
  123. 'code' => 0,
  124. ]));
  125. }
  126. public function delBroker()
  127. {
  128. $workerid = $this->access_worker['id'];
  129. $password = input('password');
  130. if ($this->access_worker['password'] !== md5($password)) {
  131. exit(json_encode([
  132. 'code' => 1,
  133. 'msg' => "操作密码验证失败",
  134. ]));
  135. }
  136. $idarr = input('idarr/a');
  137. $user_check = UserModel::whereIn('brokerid', $idarr)->find();
  138. if (!empty($user_check)) {
  139. exit(json_encode([
  140. 'code' => 1,
  141. 'msg' => "该经纪人还有下线,请先转移再删除",
  142. ]));
  143. }
  144. $broker = BrokerModel::where('workerid', '=', $workerid)->whereIn('id', $idarr)->select();
  145. $result = $broker->delete();
  146. if ($result) {
  147. exit(json_encode([
  148. 'code' => 0,
  149. 'msg' => "",
  150. ]));
  151. }
  152. exit(json_encode([
  153. 'code' => 1,
  154. 'msg' => "删除失败,请稍后重试",
  155. ]));
  156. }
  157. public function listBroker()
  158. {
  159. $workerid = $this->access_worker['id'];
  160. $limit = input('limit/d', 20);
  161. $page = input('page/d', 1);
  162. $map = [];
  163. $map[] = ['workerid', '=', $workerid];
  164. $keywords = input('keywords/s');
  165. if (!empty($keywords)) {
  166. $map[] = ['title', 'like', '%' . $keywords . '%'];
  167. }
  168. $status = input('status/d');
  169. if (!empty($status)) {
  170. $map[] = ['status', '=', $status];
  171. }
  172. $agentid = input('agentid/d');
  173. if (!empty($agentid)) {
  174. $map[] = ['agentid', '=', $agentid];
  175. }
  176. $list = BrokerModel::with(['muser', 'worker', 'agent'])->withCount(['user'])->where($map)->order('id', 'DESC')->limit($limit)->page($page)->append(['status_text', 'powerreport_text'])->select();
  177. $count = BrokerModel::where($map)->count();
  178. if ($count == 0) {
  179. exit(json_encode([
  180. 'code' => 1,
  181. 'msg' => "未查询到数据",
  182. ]));
  183. }
  184. exit(json_encode([
  185. 'code' => 0,
  186. 'msg' => "",
  187. 'count' => $count,
  188. 'data' => $list,
  189. ]));
  190. }
  191. public function incomeList()
  192. {
  193. $brokerid = input('brokerid');
  194. if (empty($brokerid)) {
  195. exit("未查询到数据");
  196. }
  197. return view('broker/incomelist', [
  198. 'brokerid' => $brokerid,
  199. ]);
  200. }
  201. public function listIncome()
  202. {
  203. $brokerid = input('brokerid');
  204. if (empty($brokerid)) {
  205. exit(json_encode([
  206. 'code' => 1,
  207. 'msg' => "未查询到数据",
  208. ]));
  209. }
  210. $limit = input('limit/d', 20);
  211. $page = input('page/d', 1);
  212. $map = [
  213. ['brokerid', '=', $brokerid],
  214. ];
  215. $list = BrokerIncomeModel::where($map)->order('id', 'DESC')->limit($limit)->page($page)->append(['status_text', 'powerreport_text'])->select();
  216. $count = BrokerIncomeModel::where($map)->count();
  217. if ($count == 0) {
  218. exit(json_encode([
  219. 'code' => 1,
  220. 'msg' => "未查询到数据",
  221. ]));
  222. }
  223. exit(json_encode([
  224. 'code' => 0,
  225. 'msg' => "",
  226. 'count' => $count,
  227. 'data' => $list,
  228. ]));
  229. }
  230. public function settleIncome()
  231. {
  232. $brokerid = input('brokerid');
  233. $value = input('value');
  234. if (empty($brokerid) || $value <= 0) {
  235. exit(json_encode([
  236. 'code' => 1,
  237. 'msg' => "参数错误",
  238. ]));
  239. }
  240. $broker = BrokerModel::find($brokerid);
  241. if (empty($broker)) {
  242. exit(json_encode([
  243. 'code' => 1,
  244. 'msg' => "未查询到数据",
  245. ]));
  246. }
  247. if ($broker['income'] < $value) {
  248. exit(json_encode([
  249. 'code' => 1,
  250. 'msg' => "经纪人收益不足",
  251. ]));
  252. }
  253. $incomeService = new IncomeService();
  254. $incomeService->add($brokerid, -$value, '商家结算', '劳务公司与经纪人线下结算');
  255. exit(json_encode([
  256. 'code' => 0,
  257. 'msg' => "",
  258. ]));
  259. }
  260. }