Agent.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. namespace app\worker\controller;
  3. use app\common\service\AgentMoneyService;
  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\ComjobsReport as ComjobsReportModel;
  8. use app\common\model\User as UserModel;
  9. use app\common\model\AgentMoney as AgentMoneyModel;
  10. use app\common\validate\Agent as AgentValidate;
  11. use think\exception\ValidateException;
  12. use think\facade\Db;
  13. use think\facade\Request;
  14. class Agent extends BaseController
  15. {
  16. public function agentList()
  17. {
  18. return view('agent/agentlist');
  19. }
  20. public function agentForm()
  21. {
  22. $workerid = $this->access_worker['id'];
  23. $id = input('id/d, 0');
  24. $agent = AgentModel::where('workerid', '=', $workerid)->findOrEmpty($id);
  25. return view('agent/agentform', [
  26. 'agent' => $agent,
  27. 'worker' => $this->access_worker,
  28. ]);
  29. }
  30. public function editAgent()
  31. {
  32. $workerid = $this->access_worker['id'];
  33. $id = input('id/d');
  34. $vdata = [
  35. 'id' => $id,
  36. 'loginname' => input('loginname/s', ""),
  37. 'idnumber' => input('idnumber/s', ""),
  38. ];
  39. try {
  40. validate(AgentValidate::class)->check($vdata);
  41. } catch (ValidateException $e) {
  42. exit(json_encode([
  43. 'code' => 1,
  44. 'msg' => $e->getError(),
  45. ]));
  46. }
  47. $muser = UserModel::where(['mobile' => input('musermobile/s', '')])->findOrEmpty();
  48. if ($muser->isEmpty()) {
  49. exit(json_encode([
  50. 'code' => 1,
  51. 'msg' => "关联的用户不存在。",
  52. ]));
  53. }
  54. $data = [
  55. 'userid' => $muser->id,
  56. 'workerid' => $workerid,
  57. 'loginname' => input('loginname/s', ""),
  58. 'idnumber' => input('idnumber/s', ""),
  59. 'title' => input('title/s', ""),
  60. 'tilpic' => input('tilpic/s', ""),
  61. 'picall' => input('picall/a', []),
  62. 'realname' => input('realname/s', ""),
  63. 'mobile' => input('mobile/s', ""),
  64. 'telephone' => input('telephone/s', ""),
  65. 'latitude' => input('latitude/f', 0.00),
  66. 'longitude' => input('longitude/f', 0.00),
  67. 'province' => input('province/s', ""),
  68. 'city' => input('city/s', ""),
  69. 'district' => input('district/s', ""),
  70. 'address' => input('address/s', ""),
  71. 'details' => input('details/s', ""),
  72. 'priority' => input('priority/d', 0),
  73. 'remark' => input('remark/s', ""),
  74. 'status' => input('status/d', 0),
  75. 'is_settle' => input('is_settle/d', 1),
  76. ];
  77. $password = input('password/s', "");
  78. if (empty($id)) {
  79. $data['password'] = empty($password) ? md5("123456789") : md5($password);
  80. $data['createtime'] = time();
  81. $agent = AgentModel::create($data);
  82. } else {
  83. if (!empty($password)) {
  84. $data['password'] = md5($password);
  85. }
  86. $agent = AgentModel::find($id);
  87. $agent->save($data);
  88. }
  89. BrokerModel::update([
  90. 'workerid' => $workerid,
  91. ], ['agentid' => $agent->id]);
  92. exit(json_encode([
  93. 'code' => 0,
  94. ]));
  95. }
  96. public function agent_join()
  97. {
  98. if (Request::isAjax()) {
  99. $data['realname'] = trim(input('post.realname/s'));
  100. $data['mobile'] = trim(input('post.mobile/s'));
  101. $data['address'] = trim(input('post.province/s')) . trim(input('post.city/s')) . trim(input('post.district/s')) . trim(input('post.address/s'));
  102. $data['idcard'] = trim(input('post.idcard/s'));
  103. $data['recommender'] = trim(input('post.recommender/s'));
  104. $data['createtime'] = time();
  105. $data['status'] = (int)1;
  106. $res = Db::name('agent_form')->insert($data);
  107. if ($res) {
  108. $rtn['code'] = 0;
  109. $rtn['msg'] = '您的申请已提交,请静心等待';
  110. } else {
  111. $rtn['code'] = 1;
  112. $rtn['msg'] = '对不起,您的申请失败,请重新申请';
  113. }
  114. return $rtn;
  115. } else {
  116. return view('agent/agent_join');
  117. }
  118. }
  119. public function fieldAgent()
  120. {
  121. $workerid = $this->access_worker['id'];
  122. $id = input('id/d', 0);
  123. $agent = AgentModel::where('workerid', '=', $workerid)->findOrEmpty($id);
  124. if ($agent->isEmpty()) {
  125. exit(json_encode([
  126. 'code' => 1,
  127. 'msg' => "信息不存在",
  128. ]));
  129. } else {
  130. $agent->save([
  131. input('field/s') => input('value'),
  132. ]);
  133. }
  134. exit(json_encode([
  135. 'code' => 0,
  136. ]));
  137. }
  138. public function delAgent()
  139. {
  140. $workerid = $this->access_worker['id'];
  141. $idarr = input('idarr/a');
  142. $broker_ids = BrokerModel::whereIn('agentid', $idarr)->column('id');
  143. if (!empty($broker_ids)) {
  144. BrokerModel::destroy($broker_ids,true);
  145. UserModel::where('brokerid', 'in', $broker_ids)->update(['brokerid' => 0]);
  146. }
  147. AgentModel::destroy(function($query)use($workerid,$idarr){
  148. $query->where('workerid', '=', $workerid)->whereIn('id', $idarr);
  149. },true);
  150. exit(json_encode([
  151. 'code' => 0,
  152. 'msg' => "",
  153. ]));
  154. }
  155. public function listAgent()
  156. {
  157. $workerid = $this->access_worker['id'];
  158. $limit = input('limit');
  159. $page = input('page');
  160. $map = [];
  161. $map[] = ['workerid', '=', $workerid];
  162. $keywords = input('keywords/s');
  163. if (!empty($keywords)) {
  164. $map[] = ['idnumber', 'like', '%' . $keywords . '%'];
  165. }
  166. $status = input('status/d');
  167. if (!empty($status)) {
  168. $map[] = ['status', '=', $status];
  169. }
  170. $list = AgentModel::with(['muser'])->where($map)->order(['priority' => 'DESC', 'id' => 'DESC'])->limit($limit)->page($page)->append(['status_text'])->select();
  171. $count = AgentModel::where($map)->count();
  172. if ($count == 0) {
  173. exit(json_encode([
  174. 'code' => 1,
  175. 'msg' => "未查询到数据",
  176. ]));
  177. }
  178. exit(json_encode([
  179. 'code' => 0,
  180. 'msg' => "",
  181. 'count' => $count,
  182. 'data' => $list,
  183. ]));
  184. }
  185. public function transferfrom()
  186. {
  187. $agent_id = input('agent_id/d, 0');
  188. $agent_list = AgentModel::where('status', 1)->where('id', '<>', $agent_id)->select();
  189. return view('agent/transferform', [
  190. 'origin_agent_id' => $agent_id,
  191. 'agent_list' => $agent_list,
  192. ]);
  193. }
  194. public function edittransfer()
  195. {
  196. $origin_agent_id = input('origin_agent_id/d, 0');
  197. $agent_id = input('agent_id/d, 0');
  198. if (empty($origin_agent_id) || empty($agent_id)) {
  199. exit(json_encode([
  200. 'code' => 1,
  201. 'msg' => "参数错误",
  202. ]));
  203. }
  204. if ($origin_agent_id == $agent_id) {
  205. exit(json_encode([
  206. 'code' => 1,
  207. 'msg' => "不可以转移给自己",
  208. ]));
  209. }
  210. $origin_agent = AgentModel::where('id', $origin_agent_id)->find();
  211. $agent = AgentModel::where('id', $agent_id)->find();
  212. if (empty($origin_agent) || empty($agent)) {
  213. exit(json_encode([
  214. 'code' => 1,
  215. 'msg' => "参数错误",
  216. ]));
  217. }
  218. BrokerModel::where('agentid', $origin_agent_id)->update(['agent_id' => $agent_id, 'workerid' => $agent['workerid']]);
  219. ComjobsReportModel::where('agentid', $origin_agent_id)->update(['agent_id' => $agent_id, 'workerid' => $agent['workerid']]);
  220. exit(json_encode([
  221. 'code' => 0,
  222. ]));
  223. }
  224. public function settleIncome()
  225. {
  226. $agentid = input('agentid');
  227. $value = input('value');
  228. if (empty($agentid) || $value <= 0) {
  229. exit(json_encode([
  230. 'code' => 1,
  231. 'msg' => "参数错误",
  232. ]));
  233. }
  234. $agent = AgentModel::find($agentid);
  235. if (empty($agent)) {
  236. exit(json_encode([
  237. 'code' => 1,
  238. 'msg' => "未查询到数据",
  239. ]));
  240. }
  241. if ($agent['money'] < $value) {
  242. exit(json_encode([
  243. 'code' => 1,
  244. 'msg' => "门店收益不足",
  245. ]));
  246. }
  247. $moneyService = new AgentMoneyService();
  248. $moneyService->settle($agentid, -$value, '门店结算', '企业与门店线下结算');
  249. exit(json_encode([
  250. 'code' => 0,
  251. 'msg' => "",
  252. ]));
  253. }
  254. public function moneyList()
  255. {
  256. $agentid = input('agentid');
  257. if (empty($agentid)) {
  258. exit("未查询到数据");
  259. }
  260. return view('agent/moneylist', [
  261. 'agentid' => $agentid,
  262. ]);
  263. }
  264. public function listMoney()
  265. {
  266. $agentid = input('agentid');
  267. if (empty($agentid)) {
  268. exit(json_encode([
  269. 'code' => 1,
  270. 'msg' => "未查询到数据",
  271. ]));
  272. }
  273. $limit = input('limit/d', 20);
  274. $page = input('page/d', 1);
  275. $map = [
  276. ['agentid', '=', $agentid],
  277. ];
  278. $list = AgentMoneyModel::where($map)->order('id', 'DESC')->limit($limit)->page($page)->select();
  279. $count = AgentMoneyModel::where($map)->count();
  280. if ($count == 0) {
  281. exit(json_encode([
  282. 'code' => 1,
  283. 'msg' => "未查询到数据",
  284. ]));
  285. }
  286. exit(json_encode([
  287. 'code' => 0,
  288. 'msg' => "",
  289. 'count' => $count,
  290. 'data' => $list,
  291. ]));
  292. }
  293. }