123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- <?php
-
- namespace app\controller\store\appointment;
-
- use app\common\logic\DataLogic;
- use app\controller\store\AuthController;
- use app\model\store\appointment\AppointmentModel;
- use app\model\store\appointment\TechnicianModel;
- use app\model\store\appointment\TechnicianProjectModel;
- use app\model\store\project\ProjectModel;
- use app\model\store\SystemStore;
-
- class Technician extends AuthController
- {
-
- protected $name = '技师';
-
- public function index()
- {
- $name = $this->request->param('name');
- $mobile = $this->request->param('mobile');
- $status = $this->request->param('status/d', 1);
- $store_id = $this->request->param('store_id/d', 0);
- $where = [
- ['status', '=', $status],
- ['is_delete', '=', 0]
- ];
- if (!empty($store_id)) $where[] = ['store_id', '=', $store_id];
- if (!empty($name)) $where[] = ['name', '=', $name];
- if (!empty($mobile)) $where[] = ['mobile', '=', $mobile];
-
- $technician_model = new TechnicianModel();
- $technician_model = $technician_model->withCount(['projects' => function ($query) {
- $query->where([['is_delete', '=', 0]]);
- }]);
- $order = ['add_time' => 'desc'];
- $field = '*';
- $append = ['add_time_format', 'tags_string', 'status_means'];
- $return = DataLogic::getDataList($technician_model, $where, $order, $field, $append);
-
- foreach ($return['list'] as $key => $val) {
- $return['list'][$key]['engaged_num'] = TechnicianProjectModel::where([
- ['store_id', '=', $store_id],
- ['technician_id', '=', $val['technician_id']]
- ])->sum('engaged_num') ?? 0;
- }
- return $this->success($return);
- }
-
- public function show()
- {
- $technician_id = $this->request->param('technician_id/d', 0);
- if ($technician_id == 0) return $this->success([]);
-
- if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
-
- if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
-
- $form = [
- 'face' => $technician->face,
- 'name' => $technician->name,
- 'mobile' => $technician->mobile,
- 'position' => $technician->position,
- 'working_collar' => $technician->working_collar,
- 'job_number' => $technician->job_number,
- 'brief_introduction' => $technician->brief_introduction,
- ];
-
- return $this->success(['form' => $form]);
- }
-
- public function save()
- {
- $technician_id = $this->request->param('technician_id/d', 0);
- $face = $this->request->param('face');
- $name = $this->request->param('name');
- $mobile = $this->request->param('mobile');
- $working_collar = $this->request->param('working_collar/d', 0);
- $job_number = $this->request->param('job_number');
- $position = $this->request->param('position');
- $brief_introduction = $this->request->param('brief_introduction');
- $store_id = $this->request->param('store_id/d', 0);
-
- //判断传来的参数
- if (empty($face)) return $this->fail('请上传头像');
- if (empty($store_id)) return $this->fail('门店为空');
- if (empty($name)) return $this->fail('请输入名称');
- if (empty($mobile) || !is_mobile($mobile)) return $this->fail('请输入手机号');
- if (empty($working_collar) || $working_collar <= 0) return $this->fail('请输入工龄,且不能小于0');
- if (empty($job_number)) return $this->fail('请输入工号');
- if (empty($position)) return $this->fail('请输入职位');
- if (empty($brief_introduction) || mb_strlen($brief_introduction) > 200) return $this->fail('请输入简介,且字数不能多于200');
-
- //查询门店是否存在
- $store = new SystemStore();
- $check = $store->where('id', $store_id)->value('id');
- if (empty($check)) {
- return $this->fail('门店为空');
- }
-
- if ($technician_id != 0) {
- if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
- if ($technician->status != 1) return $this->fail($this->name . '不存在');
- if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
- $pre_face = $technician->face;
- //判断手机号的唯一性
- if ($mobile != $technician->mobile) {
- if (null !== TechnicianModel::where([
- ['store_id', '=', $store_id],
- ['status', '=', 1],
- ['is_delete', '=', 0],
- ['mobile', '=', $mobile]
- ])->find()) {
- return $this->fail('该手机号已存在');
- }
- }
- //判断工号的唯一性
- if ($job_number != $technician->job_number) {
- if (null !== TechnicianModel::where([
- ['store_id', '=', $store_id],
- ['status', '=', 1],
- ['is_delete', '=', 0],
- ['job_number', '=', $job_number]
- ])->find()) {
- return $this->fail('该工号已存在');
- }
- }
- } else {
- $technician = new TechnicianModel();
- $technician->store_id = $store_id;
- //判断手机号的唯一性
- if (null !== TechnicianModel::where([
- ['store_id', '=', $store_id],
- ['status', '=', 1],
- ['is_delete', '=', 0],
- ['mobile', '=', $mobile]
- ])->find()) {
- return $this->fail('该手机号已存在');
- }
- //判断工号的唯一性
- if (null !== TechnicianModel::where([
- ['store_id', '=', $store_id],
- ['status', '=', 1],
- ['is_delete', '=', 0],
- ['job_number', '=', $job_number]
- ])->find()) {
- return $this->fail('该工号已存在');
- }
- }
-
- $technician->face = $face;
- $technician->name = $name;
- $technician->mobile = $mobile;
- $technician->position = $position;
- $technician->working_collar = $working_collar;
- $technician->job_number = $job_number;
- $technician->brief_introduction = $brief_introduction;
- $technician->save();
-
- // imgReplaceAct($face, $pre_face);
- return $this->success('操作成功');
- }
-
- public function quit()
- {
- $technician_id = $this->request->param('technician_id/d', 0);
-
- if ($technician_id == 0) return $this->fail($this->name . '不存在');
- if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
-
- if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
-
- if ($technician->status != 1) return $this->fail($this->name . '已离职,请勿重复操作');
-
- //补充判断???
-
- $technician->status = -1;
- $technician->save();
- return $this->success('离职成功');
- }
-
- public function del()
- {
- $technician_id = $this->request->param('technician_id/d', 0);
-
- if ($technician_id == 0) return $this->fail($this->name . '不存在');
- if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
-
- if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
-
- if ($technician->status != -1) return $this->fail($this->name . '离职状态才可删除');
-
- //补充判断???
-
- $technician->is_delete = 1;
- $technician->save();
- return $this->success('删除成功');
- }
-
- /********************************************************* start 技师详情 start **************************************************************/
-
- public function detail()
- {
- $technician_id = $this->request->param('technician_id/d', 0);
-
- if ($technician_id == 0) return $this->fail($this->name . '不存在');
- if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
-
- if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
-
- $engaged_num = TechnicianProjectModel::where([
- ['store_id', '=', $technician->store_id],
- ['technician_id', '=', $technician_id]
- ])->sum('engaged_num') ?? 0;
-
- $projects_count = TechnicianProjectModel::where([
- ['store_id', '=', $technician->store_id],
- ['technician_id', '=', $technician_id],
- ['is_delete', '=', 0]
- ])->count() ?? 0;
-
- $detail = [
- 'face' => $technician->face,
- 'name' => $technician->name,
- 'mobile' => $technician->mobile,
- 'money' => $technician->money,
- 'working_collar' => $technician->working_collar,
- 'job_number' => $technician->job_number,
- 'brief_introduction' => $technician->brief_introduction,
- 'status' => $technician->status,
- 'status_means' => $technician->status_means,
- 'tags' => $technician->tags ?? [],
- 'engaged_num' => $engaged_num,
- 'projects_count' => $projects_count,
- ];
-
- return $this->success(['detail' => $detail]);
- }
-
- public function changTags()
- {
- $technician_id = $this->request->param('technician_id/d', 0);
- $tags = $this->request->param('tags/a', []);
-
- if ($technician_id == 0) return $this->fail($this->name . '不存在');
- if (empty($tags) || !is_array($tags) || count($tags) == 0) return $this->fail('至少保留一个标签');
-
- if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
-
- if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
-
- $technician->tags = $tags;
- $technician->save();
- return $this->success('操作成功');
- }
-
- public function getTechnicianAppointmentList()
- {
- $technician_id = $this->request->param('technician_id/d', 0);
- $status = $this->request->param('status/d', 0);
-
- if ($technician_id == 0) return $this->fail($this->name . '不存在');
- if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
-
- if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
-
- $where = [
- ['store_id', '=', $technician->store_id],
- ['technician_id', '=', $technician_id],
- ];
-
- if (!empty($status)) $where[] = ['status', '=', $status];
- $appointment_model = new AppointmentModel();
- $appointment_model = $appointment_model->with(['member', 'technician']);
- $order = ['add_time' => 'desc'];
- $field = '*';
- $append = ['add_time_format', 'status_means', 'operate_type_means', 'arrival_time_format', 'cancel_time_format'];
- $return = DataLogic::getDataList($appointment_model, $where, $order, $field, $append);
- return $this->success($return);
- }
-
- public function getTechnicianBindProjectList()
- {
- $technician_id = $this->request->param('technician_id/d', 0);
- if ($technician_id == 0) return $this->fail($this->name . '不存在');
- if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
-
- if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
-
- $where = [
- ['store_id', '=', $technician->store_id],
- ['technician_id', '=', $technician_id],
- ['is_delete', '=', 0],
- ];
-
- $technician_project_model = new TechnicianProjectModel();
- $technician_project_model = $technician_project_model->with(['project']);
- $order = ['add_time' => 'desc'];
- $field = '*';
- $append = ['add_time_format'];
-
- $return = DataLogic::getDataList($technician_project_model, $where, $order, $field, $append);
-
- return $this->success($return);
- }
-
- public function getProjectListFromTechnicianBind()
- {
- $technician_id = $this->request->param('technician_id/d', 0);
- if ($technician_id == 0) return $this->fail($this->name . '不存在');
- if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
-
- if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
-
- $project_ids = TechnicianProjectModel::where([
- ['store_id', '=', $technician->store_id],
- ['technician_id', '=', $technician_id],
- ['is_delete', '=', 0],
- ])->column('project_id') ?? [];
-
- $where = [
- ['store_id', '=', $technician->store_id],
- ['is_delete', '=', 0],
- ['project_id', 'not in', $project_ids]
- ];
-
- $project_model = new ProjectModel();
- $order = ['add_time' => 'desc'];
- $field = '*';
- $append = ['add_time_format'];
- $return = DataLogic::getDataList($project_model, $where, $order, $field, $append);
- return $this->success($return);
- }
-
- public function technicianBindProject()
- {
- $technician_id = $this->request->param('technician_id/d', 0);
- $project_ids = $this->request->param('project_ids/a', []);
-
- if ($technician_id == 0) return $this->fail($this->name . '不存在');
- if (empty($project_ids) || !is_array($project_ids)) return $this->fail('请选择项目');
-
- if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
-
- if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
-
- $bind_project_ids = TechnicianProjectModel::where([
- ['store_id', '=', $technician->store_id],
- ['technician_id', '=', $technician_id],
- ['is_delete', '=', 0],
- ])->column('project_id') ?? [];
-
- $project_ids = array_diff($project_ids, $bind_project_ids) ?? [];
-
- $project_ids = ProjectModel::where([
- ['store_id', '=', $technician->store_id],
- ['is_delete', '=', 0],
- ['project_id', 'in', $project_ids]
- ])->column('project_id') ?? [];
-
- if (empty($project_ids)) return $this->fail('请选择项目');
- $technician_project_data = [];
- foreach ($project_ids as $project_id) {
- $technician_project_data[] = [
- 'store_id', '=', $technician->store_id,
- 'project_id' => $project_id,
- 'technician_id' => $technician_id
- ];
- }
- $technician_project_model = new TechnicianProjectModel();
- $technician_project_model->saveAll($technician_project_data);
-
- return $this->success('成功绑定');
- }
-
- public function delTechnicianProject()
- {
- $bind_id = $this->request->param('bind_id/d', 0);
- if ($bind_id == 0) return $this->fail('参数错误');
- if (null === $technician_project = TechnicianProjectModel::find($bind_id)) return $this->fail('您未绑定这个项目');
- if ($technician_project->is_delete != 0) return $this->fail('您未绑定这个项目');
-
- $technician_project->is_delete = 1;
- $technician_project->save();
- return $this->success('删除成功');
- }
-
- /********************************************************* end 技师详情 end **************************************************************/
-
- }
|