Broker.php 8.3 KB

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