Agent.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. ];
  79. $password = input('password/s', "");
  80. if (empty($id)) {
  81. $data['password'] = empty($password) ? md5("123456789") : md5($password);
  82. $data['createtime'] = time();
  83. $agent = AgentModel::create($data);
  84. } else {
  85. if (!empty($password)) {
  86. $data['password'] = md5($password);
  87. }
  88. $agent = AgentModel::find($id);
  89. $agent->save($data);
  90. }
  91. BrokerModel::update([
  92. 'workerid' => input('workerid/d', 0),
  93. ], ['agentid' => $agent->id]);
  94. exit(json_encode([
  95. 'code' => 0,
  96. ]));
  97. }
  98. public function fieldAgent()
  99. {
  100. $id = input('id/d', 0);
  101. $info = AgentModel::findOrEmpty($id);
  102. if ($info->isEmpty()) {
  103. exit(json_encode([
  104. 'code' => 1,
  105. 'msg' => "信息不存在",
  106. ]));
  107. } else {
  108. $info->save([
  109. input('field/s') => input('value/s', ""),
  110. ]);
  111. }
  112. exit(json_encode([
  113. 'code' => 0,
  114. ]));
  115. }
  116. public function delAgent()
  117. {
  118. $access_admin = session('access_admin');
  119. $password = input('password');
  120. if ($access_admin['password'] !== md5($password)) {
  121. exit(json_encode([
  122. 'code' => 1,
  123. 'msg' => "操作密码验证失败",
  124. ]));
  125. }
  126. $idarr = input('idarr/a');
  127. $result = Db::name('agent')->whereIn('id', $idarr)->update(['deletetime' => time()]);
  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 listAgent()
  140. {
  141. $limit = input('limit');
  142. $page = input('page');
  143. $map = [];
  144. $keywords = input('keywords/s');
  145. if (!empty($keywords)) {
  146. $map[] = ['idnumber', 'like', '%' . $keywords . '%'];
  147. }
  148. $workerid = input('workerid/d');
  149. if (!empty($workerid)) {
  150. $map[] = ['workerid', '=', $workerid];
  151. }
  152. $status = input('status/d');
  153. if (!empty($status)) {
  154. $map[] = ['status', '=', $status];
  155. }
  156. $list = AgentModel::with(['worker', 'muser'])->where($map)->order(['priority' => 'DESC', 'id' => 'DESC'])->limit($limit)->page($page)->append(['status_text'])->select();
  157. $count = AgentModel::where($map)->count();
  158. if ($count == 0) {
  159. exit(json_encode([
  160. 'code' => 1,
  161. 'msg' => "未查询到数据",
  162. ]));
  163. }
  164. exit(json_encode([
  165. 'code' => 0,
  166. 'msg' => "",
  167. 'count' => $count,
  168. 'data' => $list,
  169. ]));
  170. }
  171. // 申请注册经纪人
  172. public function fagentList()
  173. {
  174. return view('agent/fagentlist');
  175. }
  176. public function fagentForm()
  177. {
  178. $id = input('id/d, 0');
  179. $fagent = AgentFormModel::findOrEmpty($id);
  180. return view('agent/fagentform', [
  181. 'fagent' => $fagent,
  182. ]);
  183. }
  184. public function editFagent()
  185. {
  186. $id = input('id/d');
  187. $fagent = AgentFormModel::findOrEmpty($id);
  188. $fagent->save([
  189. 'realname' => input('realname/s', ""),
  190. 'mobile' => input('mobile/s', ""),
  191. 'address' => input('address/s', ""),
  192. 'idcard' => input('idcard/s', ""),
  193. 'recommender' => input('recommender/s', ""),
  194. 'status' => input('status/d', 1),
  195. 'remark' => input('remark/s', ""),
  196. 'createtime' => input('createtime/s', ""),
  197. ]);
  198. exit(json_encode([
  199. 'code' => 0,
  200. ]));
  201. }
  202. public function fieldFagent()
  203. {
  204. $id = input('id/d', 0);
  205. $info = AgentFormModel::findOrEmpty($id);
  206. if ($info->isEmpty()) {
  207. exit(json_encode([
  208. 'code' => 1,
  209. 'msg' => "信息不存在",
  210. ]));
  211. } else {
  212. $info->save([
  213. input('field/s') => input('value'),
  214. ]);
  215. }
  216. exit(json_encode([
  217. 'code' => 0,
  218. ]));
  219. }
  220. public function delFagent()
  221. {
  222. $idarr = input('idarr/a');
  223. $fagent = AgentFormModel::whereIn('id', $idarr)->select();
  224. $result = $fagent->delete();
  225. if ($result) {
  226. exit(json_encode([
  227. 'code' => 0,
  228. 'msg' => "",
  229. ]));
  230. }
  231. exit(json_encode([
  232. 'code' => 1,
  233. 'msg' => "删除失败,请稍后重试",
  234. ]));
  235. }
  236. public function listFagent()
  237. {
  238. $limit = input('limit');
  239. $page = input('page');
  240. $map = [];
  241. $keywords = input('keywords/s');
  242. if (!empty($keywords)) {
  243. $map[] = ['realname|mobile', 'like', '%' . $keywords . '%'];
  244. }
  245. $status = input('status/d');
  246. if (!empty($status)) {
  247. $map[] = ['status', '=', $status];
  248. }
  249. $list = AgentFormModel::where($map)->order('id', 'DESC')->limit($limit)->page($page)->append(['status_text'])->select();
  250. $count = AgentFormModel::where($map)->count();
  251. if ($count == 0) {
  252. exit(json_encode([
  253. 'code' => 1,
  254. 'msg' => "未查询到数据",
  255. ]));
  256. }
  257. exit(json_encode([
  258. 'code' => 0,
  259. 'msg' => "",
  260. 'count' => $count,
  261. 'data' => $list,
  262. ]));
  263. }
  264. public function settleIncome()
  265. {
  266. $agentid = input('agentid');
  267. $value = input('value');
  268. if (empty($agentid) || $value <= 0) {
  269. exit(json_encode([
  270. 'code' => 1,
  271. 'msg' => "参数错误",
  272. ]));
  273. }
  274. $agent = AgentModel::find($agentid);
  275. if (empty($agent)) {
  276. exit(json_encode([
  277. 'code' => 1,
  278. 'msg' => "未查询到数据",
  279. ]));
  280. }
  281. if ($agent['money'] < $value) {
  282. exit(json_encode([
  283. 'code' => 1,
  284. 'msg' => "门店收益不足",
  285. ]));
  286. }
  287. $moneyService = new AgentMoneyService();
  288. $moneyService->settle($agentid, -$value, '门店结算', '平台与门店线下结算');
  289. exit(json_encode([
  290. 'code' => 0,
  291. 'msg' => "",
  292. ]));
  293. }
  294. public function moneyList()
  295. {
  296. $agentid = input('agentid');
  297. if (empty($agentid)) {
  298. exit("未查询到数据");
  299. }
  300. return view('agent/moneylist', [
  301. 'agentid' => $agentid,
  302. ]);
  303. }
  304. public function listMoney()
  305. {
  306. $agentid = input('agentid');
  307. if (empty($agentid)) {
  308. exit(json_encode([
  309. 'code' => 1,
  310. 'msg' => "未查询到数据",
  311. ]));
  312. }
  313. $limit = input('limit/d', 20);
  314. $page = input('page/d', 1);
  315. $map = [
  316. ['agentid', '=', $agentid],
  317. ];
  318. $list = AgentMoneyModel::where($map)->order('id', 'DESC')->limit($limit)->page($page)->select();
  319. $count = AgentMoneyModel::where($map)->count();
  320. if ($count == 0) {
  321. exit(json_encode([
  322. 'code' => 1,
  323. 'msg' => "未查询到数据",
  324. ]));
  325. }
  326. exit(json_encode([
  327. 'code' => 0,
  328. 'msg' => "",
  329. 'count' => $count,
  330. 'data' => $list,
  331. ]));
  332. }
  333. }