Agent.php 8.0 KB

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