Agent.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\BaseController;
  4. use app\common\model\User as UserModel;
  5. use app\common\model\Worker as WorkerModel;
  6. use app\common\model\Agent as AgentModel;
  7. use app\common\model\AgentForm as AgentFormModel;
  8. use app\common\model\Broker as BrokerModel;
  9. use app\common\model\AgentMoney as AgentMoneyModel;
  10. use app\common\service\AgentMoneyService;
  11. use app\common\validate\Agent as AgentValidate;
  12. use think\exception\ValidateException;
  13. use think\facade\Db;
  14. class Agent extends BaseController
  15. {
  16. public function agentList()
  17. {
  18. $workerlist = WorkerModel::order(['id' => 'desc'])->select();
  19. return view('agent/agentlist', [
  20. 'workerlist' => $workerlist,
  21. ]);
  22. }
  23. public function agentForm()
  24. {
  25. $id = input('id/d, 0');
  26. $agent = AgentModel::with(['muser'])->findOrEmpty($id);
  27. $workerlist = WorkerModel::order(['id' => 'desc'])->select();
  28. return view('agent/agentform', [
  29. 'workerlist' => $workerlist,
  30. 'agent' => $agent,
  31. ]);
  32. }
  33. public function editAgent()
  34. {
  35. $id = input('id/d');
  36. $vdata = [
  37. 'id' => $id,
  38. 'loginname' => input('loginname/s', ""),
  39. 'idnumber' => input('idnumber/s', ""),
  40. ];
  41. try {
  42. validate(AgentValidate::class)->check($vdata);
  43. } catch (ValidateException $e) {
  44. exit(json_encode([
  45. 'code' => 1,
  46. 'msg' => $e->getError(),
  47. ]));
  48. }
  49. $muser = UserModel::where(['mobile' => input('musermobile/s', '')])->findOrEmpty();
  50. if ($muser->isEmpty()) {
  51. exit(json_encode([
  52. 'code' => 1,
  53. 'msg' => "关联的用户不存在。",
  54. ]));
  55. }
  56. $data = [
  57. 'userid' => $muser->id,
  58. 'workerid' => input('workerid/d', 0),
  59. 'loginname' => input('loginname/s', ""),
  60. 'idnumber' => input('idnumber/s', ""),
  61. 'title' => input('title/s', ""),
  62. 'tilpic' => input('tilpic/s', ""),
  63. 'picall' => input('picall/a', []),
  64. 'realname' => input('realname/s', ""),
  65. 'mobile' => input('mobile/s', ""),
  66. 'telephone' => input('telephone/s', ""),
  67. 'latitude' => input('latitude/f', 0.00),
  68. 'longitude' => input('longitude/f', 0.00),
  69. 'province' => input('province/s', ""),
  70. 'city' => input('city/s', ""),
  71. 'district' => input('district/s', ""),
  72. 'address' => input('address/s', ""),
  73. 'details' => input('details/s', ""),
  74. 'priority' => input('priority/d', 0),
  75. 'remark' => input('remark/s', ""),
  76. 'status' => input('status/d', 1),
  77. 'is_settle' => input('is_settle/d', 1),
  78. 'type' => input('type/d', 1),
  79. ];
  80. $password = input('password/s', "");
  81. if (empty($id)) {
  82. $data['password'] = empty($password) ? md5("123456789") : md5($password);
  83. $data['createtime'] = time();
  84. $agent = AgentModel::create($data);
  85. } else {
  86. if (!empty($password)) {
  87. $data['password'] = md5($password);
  88. }
  89. $agent = AgentModel::find($id);
  90. $agent->save($data);
  91. }
  92. BrokerModel::update([
  93. 'workerid' => input('workerid/d', 0),
  94. ], ['agentid' => $agent->id]);
  95. exit(json_encode([
  96. 'code' => 0,
  97. ]));
  98. }
  99. public function fieldAgent()
  100. {
  101. $id = input('id/d', 0);
  102. $info = AgentModel::findOrEmpty($id);
  103. if ($info->isEmpty()) {
  104. exit(json_encode([
  105. 'code' => 1,
  106. 'msg' => "信息不存在",
  107. ]));
  108. } else {
  109. $info->save([
  110. input('field/s') => input('value/s', ""),
  111. ]);
  112. }
  113. exit(json_encode([
  114. 'code' => 0,
  115. ]));
  116. }
  117. public function delAgent()
  118. {
  119. $access_admin = session('access_admin');
  120. $password = input('password');
  121. if ($access_admin['password'] !== md5($password)) {
  122. exit(json_encode([
  123. 'code' => 1,
  124. 'msg' => "操作密码验证失败",
  125. ]));
  126. }
  127. $idarr = input('idarr/a');
  128. $result = Db::name('agent')->whereIn('id', $idarr)->update(['deletetime' => time()]);
  129. if ($result) {
  130. exit(json_encode([
  131. 'code' => 0,
  132. 'msg' => "",
  133. ]));
  134. }
  135. exit(json_encode([
  136. 'code' => 1,
  137. 'msg' => "删除失败,请稍后重试",
  138. ]));
  139. }
  140. public function listAgent()
  141. {
  142. $limit = input('limit');
  143. $page = input('page');
  144. $map = [];
  145. $keywords = input('keywords/s');
  146. if (!empty($keywords)) {
  147. $map[] = ['idnumber', 'like', '%' . $keywords . '%'];
  148. }
  149. $workerid = input('workerid/d');
  150. if (!empty($workerid)) {
  151. $map[] = ['workerid', '=', $workerid];
  152. }
  153. $status = input('status/d');
  154. if (!empty($status)) {
  155. $map[] = ['status', '=', $status];
  156. }
  157. $list = AgentModel::with(['worker', 'muser'])->where($map)->order(['priority' => 'DESC', 'id' => 'DESC'])->limit($limit)->page($page)->append(['status_text'])->select();
  158. $count = AgentModel::where($map)->count();
  159. if ($count == 0) {
  160. exit(json_encode([
  161. 'code' => 1,
  162. 'msg' => "未查询到数据",
  163. ]));
  164. }
  165. exit(json_encode([
  166. 'code' => 0,
  167. 'msg' => "",
  168. 'count' => $count,
  169. 'data' => $list,
  170. ]));
  171. }
  172. // 申请注册经纪人
  173. public function fagentList()
  174. {
  175. return view('agent/fagentlist');
  176. }
  177. public function fagentForm()
  178. {
  179. $id = input('id/d, 0');
  180. $fagent = AgentFormModel::findOrEmpty($id);
  181. return view('agent/fagentform', [
  182. 'fagent' => $fagent,
  183. ]);
  184. }
  185. public function editFagent()
  186. {
  187. $id = input('id/d');
  188. $fagent = AgentFormModel::findOrEmpty($id);
  189. $fagent->save([
  190. 'realname' => input('realname/s', ""),
  191. 'mobile' => input('mobile/s', ""),
  192. 'address' => input('address/s', ""),
  193. 'idcard' => input('idcard/s', ""),
  194. 'recommender' => input('recommender/s', ""),
  195. 'status' => input('status/d', 1),
  196. 'remark' => input('remark/s', ""),
  197. 'createtime' => input('createtime/s', ""),
  198. ]);
  199. exit(json_encode([
  200. 'code' => 0,
  201. ]));
  202. }
  203. public function fieldFagent()
  204. {
  205. $id = input('id/d', 0);
  206. $info = AgentFormModel::findOrEmpty($id);
  207. if ($info->isEmpty()) {
  208. exit(json_encode([
  209. 'code' => 1,
  210. 'msg' => "信息不存在",
  211. ]));
  212. } else {
  213. $info->save([
  214. input('field/s') => input('value'),
  215. ]);
  216. }
  217. exit(json_encode([
  218. 'code' => 0,
  219. ]));
  220. }
  221. public function delFagent()
  222. {
  223. $idarr = input('idarr/a');
  224. $fagent = AgentFormModel::whereIn('id', $idarr)->select();
  225. $result = $fagent->delete();
  226. if ($result) {
  227. exit(json_encode([
  228. 'code' => 0,
  229. 'msg' => "",
  230. ]));
  231. }
  232. exit(json_encode([
  233. 'code' => 1,
  234. 'msg' => "删除失败,请稍后重试",
  235. ]));
  236. }
  237. public function listFagent()
  238. {
  239. $limit = input('limit');
  240. $page = input('page');
  241. $map = [];
  242. $keywords = input('keywords/s');
  243. if (!empty($keywords)) {
  244. $map[] = ['realname|mobile', 'like', '%' . $keywords . '%'];
  245. }
  246. $status = input('status/d');
  247. if (!empty($status)) {
  248. $map[] = ['status', '=', $status];
  249. }
  250. $list = AgentFormModel::where($map)->order('id', 'DESC')->limit($limit)->page($page)->append(['status_text'])->select();
  251. $count = AgentFormModel::where($map)->count();
  252. if ($count == 0) {
  253. exit(json_encode([
  254. 'code' => 1,
  255. 'msg' => "未查询到数据",
  256. ]));
  257. }
  258. exit(json_encode([
  259. 'code' => 0,
  260. 'msg' => "",
  261. 'count' => $count,
  262. 'data' => $list,
  263. ]));
  264. }
  265. public function settleIncome()
  266. {
  267. $agentid = input('agentid');
  268. $value = input('value');
  269. if (empty($agentid) || $value <= 0) {
  270. exit(json_encode([
  271. 'code' => 1,
  272. 'msg' => "参数错误",
  273. ]));
  274. }
  275. $agent = AgentModel::find($agentid);
  276. if (empty($agent)) {
  277. exit(json_encode([
  278. 'code' => 1,
  279. 'msg' => "未查询到数据",
  280. ]));
  281. }
  282. if ($agent['money'] < $value) {
  283. exit(json_encode([
  284. 'code' => 1,
  285. 'msg' => "门店收益不足",
  286. ]));
  287. }
  288. $moneyService = new AgentMoneyService();
  289. $moneyService->settle($agentid, -$value, '门店结算', '平台与门店线下结算');
  290. exit(json_encode([
  291. 'code' => 0,
  292. 'msg' => "",
  293. ]));
  294. }
  295. public function moneyList()
  296. {
  297. $agentid = input('agentid');
  298. if (empty($agentid)) {
  299. exit("未查询到数据");
  300. }
  301. return view('agent/moneylist', [
  302. 'agentid' => $agentid,
  303. ]);
  304. }
  305. public function listMoney()
  306. {
  307. $agentid = input('agentid');
  308. if (empty($agentid)) {
  309. exit(json_encode([
  310. 'code' => 1,
  311. 'msg' => "未查询到数据",
  312. ]));
  313. }
  314. $limit = input('limit/d', 20);
  315. $page = input('page/d', 1);
  316. $map = [
  317. ['agentid', '=', $agentid],
  318. ];
  319. $list = AgentMoneyModel::where($map)->order('id', 'DESC')->limit($limit)->page($page)->select();
  320. $count = AgentMoneyModel::where($map)->count();
  321. if ($count == 0) {
  322. exit(json_encode([
  323. 'code' => 1,
  324. 'msg' => "未查询到数据",
  325. ]));
  326. }
  327. exit(json_encode([
  328. 'code' => 0,
  329. 'msg' => "",
  330. 'count' => $count,
  331. 'data' => $list,
  332. ]));
  333. }
  334. }