You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Technician.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. namespace app\controller\store\appointment;
  3. use app\common\logic\DataLogic;
  4. use app\controller\store\AuthController;
  5. use app\model\store\appointment\AppointmentModel;
  6. use app\model\store\appointment\TechnicianModel;
  7. use app\model\store\appointment\TechnicianProjectModel;
  8. use app\model\store\project\ProjectModel;
  9. use app\model\store\SystemStore;
  10. class Technician extends AuthController
  11. {
  12. protected $name = '技师';
  13. public function index()
  14. {
  15. $name = $this->request->param('name');
  16. $mobile = $this->request->param('mobile');
  17. $status = $this->request->param('status/d', 1);
  18. $store_id = $this->request->param('store_id/d', 0);
  19. $where = [
  20. ['status', '=', $status],
  21. ['is_delete', '=', 0]
  22. ];
  23. if (!empty($store_id)) $where[] = ['store_id', '=', $store_id];
  24. if (!empty($name)) $where[] = ['name', '=', $name];
  25. if (!empty($mobile)) $where[] = ['mobile', '=', $mobile];
  26. $technician_model = new TechnicianModel();
  27. $technician_model = $technician_model->withCount(['projects' => function ($query) {
  28. $query->where([['is_delete', '=', 0]]);
  29. }]);
  30. $order = ['add_time' => 'desc'];
  31. $field = '*';
  32. $append = ['add_time_format', 'tags_string', 'status_means'];
  33. $return = DataLogic::getDataList($technician_model, $where, $order, $field, $append);
  34. foreach ($return['list'] as $key => $val) {
  35. $return['list'][$key]['engaged_num'] = TechnicianProjectModel::where([
  36. ['store_id', '=', $store_id],
  37. ['technician_id', '=', $val['technician_id']]
  38. ])->sum('engaged_num') ?? 0;
  39. }
  40. return $this->success($return);
  41. }
  42. public function show()
  43. {
  44. $technician_id = $this->request->param('technician_id/d', 0);
  45. if ($technician_id == 0) return $this->success([]);
  46. if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
  47. if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
  48. $form = [
  49. 'face' => $technician->face,
  50. 'name' => $technician->name,
  51. 'mobile' => $technician->mobile,
  52. 'position' => $technician->position,
  53. 'working_collar' => $technician->working_collar,
  54. 'job_number' => $technician->job_number,
  55. 'brief_introduction' => $technician->brief_introduction,
  56. ];
  57. return $this->success(['form' => $form]);
  58. }
  59. public function save()
  60. {
  61. $technician_id = $this->request->param('technician_id/d', 0);
  62. $face = $this->request->param('face');
  63. $name = $this->request->param('name');
  64. $mobile = $this->request->param('mobile');
  65. $working_collar = $this->request->param('working_collar/d', 0);
  66. $job_number = $this->request->param('job_number');
  67. $position = $this->request->param('position');
  68. $brief_introduction = $this->request->param('brief_introduction');
  69. $store_id = $this->request->param('store_id/d', 0);
  70. //判断传来的参数
  71. if (empty($face)) return $this->fail('请上传头像');
  72. if (empty($store_id)) return $this->fail('门店为空');
  73. if (empty($name)) return $this->fail('请输入名称');
  74. if (empty($mobile) || !is_mobile($mobile)) return $this->fail('请输入手机号');
  75. if (empty($working_collar) || $working_collar <= 0) return $this->fail('请输入工龄,且不能小于0');
  76. if (empty($job_number)) return $this->fail('请输入工号');
  77. if (empty($position)) return $this->fail('请输入职位');
  78. if (empty($brief_introduction) || mb_strlen($brief_introduction) > 200) return $this->fail('请输入简介,且字数不能多于200');
  79. //查询门店是否存在
  80. $store = new SystemStore();
  81. $check = $store->where('id', $store_id)->value('id');
  82. if (empty($check)) {
  83. return $this->fail('门店为空');
  84. }
  85. if ($technician_id != 0) {
  86. if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
  87. if ($technician->status != 1) return $this->fail($this->name . '不存在');
  88. if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
  89. $pre_face = $technician->face;
  90. //判断手机号的唯一性
  91. if ($mobile != $technician->mobile) {
  92. if (null !== TechnicianModel::where([
  93. ['store_id', '=', $store_id],
  94. ['status', '=', 1],
  95. ['is_delete', '=', 0],
  96. ['mobile', '=', $mobile]
  97. ])->find()) {
  98. return $this->fail('该手机号已存在');
  99. }
  100. }
  101. //判断工号的唯一性
  102. if ($job_number != $technician->job_number) {
  103. if (null !== TechnicianModel::where([
  104. ['store_id', '=', $store_id],
  105. ['status', '=', 1],
  106. ['is_delete', '=', 0],
  107. ['job_number', '=', $job_number]
  108. ])->find()) {
  109. return $this->fail('该工号已存在');
  110. }
  111. }
  112. } else {
  113. $technician = new TechnicianModel();
  114. $technician->store_id = $store_id;
  115. //判断手机号的唯一性
  116. if (null !== TechnicianModel::where([
  117. ['store_id', '=', $store_id],
  118. ['status', '=', 1],
  119. ['is_delete', '=', 0],
  120. ['mobile', '=', $mobile]
  121. ])->find()) {
  122. return $this->fail('该手机号已存在');
  123. }
  124. //判断工号的唯一性
  125. if (null !== TechnicianModel::where([
  126. ['store_id', '=', $store_id],
  127. ['status', '=', 1],
  128. ['is_delete', '=', 0],
  129. ['job_number', '=', $job_number]
  130. ])->find()) {
  131. return $this->fail('该工号已存在');
  132. }
  133. }
  134. $technician->face = $face;
  135. $technician->name = $name;
  136. $technician->mobile = $mobile;
  137. $technician->position = $position;
  138. $technician->working_collar = $working_collar;
  139. $technician->job_number = $job_number;
  140. $technician->brief_introduction = $brief_introduction;
  141. $technician->save();
  142. // imgReplaceAct($face, $pre_face);
  143. return $this->success('操作成功');
  144. }
  145. public function quit()
  146. {
  147. $technician_id = $this->request->param('technician_id/d', 0);
  148. if ($technician_id == 0) return $this->fail($this->name . '不存在');
  149. if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
  150. if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
  151. if ($technician->status != 1) return $this->fail($this->name . '已离职,请勿重复操作');
  152. //补充判断???
  153. $technician->status = -1;
  154. $technician->save();
  155. return $this->success('离职成功');
  156. }
  157. public function del()
  158. {
  159. $technician_id = $this->request->param('technician_id/d', 0);
  160. if ($technician_id == 0) return $this->fail($this->name . '不存在');
  161. if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
  162. if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
  163. if ($technician->status != -1) return $this->fail($this->name . '离职状态才可删除');
  164. //补充判断???
  165. $technician->is_delete = 1;
  166. $technician->save();
  167. return $this->success('删除成功');
  168. }
  169. /********************************************************* start 技师详情 start **************************************************************/
  170. public function detail()
  171. {
  172. $technician_id = $this->request->param('technician_id/d', 0);
  173. if ($technician_id == 0) return $this->fail($this->name . '不存在');
  174. if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
  175. if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
  176. $engaged_num = TechnicianProjectModel::where([
  177. ['store_id', '=', $technician->store_id],
  178. ['technician_id', '=', $technician_id]
  179. ])->sum('engaged_num') ?? 0;
  180. $projects_count = TechnicianProjectModel::where([
  181. ['store_id', '=', $technician->store_id],
  182. ['technician_id', '=', $technician_id],
  183. ['is_delete', '=', 0]
  184. ])->count() ?? 0;
  185. $detail = [
  186. 'face' => $technician->face,
  187. 'name' => $technician->name,
  188. 'mobile' => $technician->mobile,
  189. 'money' => $technician->money,
  190. 'working_collar' => $technician->working_collar,
  191. 'job_number' => $technician->job_number,
  192. 'brief_introduction' => $technician->brief_introduction,
  193. 'status' => $technician->status,
  194. 'status_means' => $technician->status_means,
  195. 'tags' => $technician->tags ?? [],
  196. 'engaged_num' => $engaged_num,
  197. 'projects_count' => $projects_count,
  198. ];
  199. return $this->success(['detail' => $detail]);
  200. }
  201. public function changTags()
  202. {
  203. $technician_id = $this->request->param('technician_id/d', 0);
  204. $tags = $this->request->param('tags/a', []);
  205. if ($technician_id == 0) return $this->fail($this->name . '不存在');
  206. if (empty($tags) || !is_array($tags) || count($tags) == 0) return $this->fail('至少保留一个标签');
  207. if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
  208. if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
  209. $technician->tags = $tags;
  210. $technician->save();
  211. return $this->success('操作成功');
  212. }
  213. public function getTechnicianAppointmentList()
  214. {
  215. $technician_id = $this->request->param('technician_id/d', 0);
  216. $status = $this->request->param('status/d', 0);
  217. if ($technician_id == 0) return $this->fail($this->name . '不存在');
  218. if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
  219. if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
  220. $where = [
  221. ['store_id', '=', $technician->store_id],
  222. ['technician_id', '=', $technician_id],
  223. ];
  224. if (!empty($status)) $where[] = ['status', '=', $status];
  225. $appointment_model = new AppointmentModel();
  226. $appointment_model = $appointment_model->with(['member', 'technician']);
  227. $order = ['add_time' => 'desc'];
  228. $field = '*';
  229. $append = ['add_time_format', 'status_means', 'operate_type_means', 'arrival_time_format', 'cancel_time_format'];
  230. $return = DataLogic::getDataList($appointment_model, $where, $order, $field, $append);
  231. return $this->success($return);
  232. }
  233. public function getTechnicianBindProjectList()
  234. {
  235. $technician_id = $this->request->param('technician_id/d', 0);
  236. if ($technician_id == 0) return $this->fail($this->name . '不存在');
  237. if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
  238. if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
  239. $where = [
  240. ['store_id', '=', $technician->store_id],
  241. ['technician_id', '=', $technician_id],
  242. ['is_delete', '=', 0],
  243. ];
  244. $technician_project_model = new TechnicianProjectModel();
  245. $technician_project_model = $technician_project_model->with(['project']);
  246. $order = ['add_time' => 'desc'];
  247. $field = '*';
  248. $append = ['add_time_format'];
  249. $return = DataLogic::getDataList($technician_project_model, $where, $order, $field, $append);
  250. return $this->success($return);
  251. }
  252. public function getProjectListFromTechnicianBind()
  253. {
  254. $technician_id = $this->request->param('technician_id/d', 0);
  255. if ($technician_id == 0) return $this->fail($this->name . '不存在');
  256. if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
  257. if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
  258. $project_ids = TechnicianProjectModel::where([
  259. ['store_id', '=', $technician->store_id],
  260. ['technician_id', '=', $technician_id],
  261. ['is_delete', '=', 0],
  262. ])->column('project_id') ?? [];
  263. $where = [
  264. ['store_id', '=', $technician->store_id],
  265. ['is_delete', '=', 0],
  266. ['project_id', 'not in', $project_ids]
  267. ];
  268. $project_model = new ProjectModel();
  269. $order = ['add_time' => 'desc'];
  270. $field = '*';
  271. $append = ['add_time_format'];
  272. $return = DataLogic::getDataList($project_model, $where, $order, $field, $append);
  273. return $this->success($return);
  274. }
  275. public function technicianBindProject()
  276. {
  277. $technician_id = $this->request->param('technician_id/d', 0);
  278. $project_ids = $this->request->param('project_ids/a', []);
  279. if ($technician_id == 0) return $this->fail($this->name . '不存在');
  280. if (empty($project_ids) || !is_array($project_ids)) return $this->fail('请选择项目');
  281. if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail($this->name . '不存在');
  282. if ($technician->is_delete != 0) return $this->fail($this->name . '不存在');
  283. $bind_project_ids = TechnicianProjectModel::where([
  284. ['store_id', '=', $technician->store_id],
  285. ['technician_id', '=', $technician_id],
  286. ['is_delete', '=', 0],
  287. ])->column('project_id') ?? [];
  288. $project_ids = array_diff($project_ids, $bind_project_ids) ?? [];
  289. $project_ids = ProjectModel::where([
  290. ['store_id', '=', $technician->store_id],
  291. ['is_delete', '=', 0],
  292. ['project_id', 'in', $project_ids]
  293. ])->column('project_id') ?? [];
  294. if (empty($project_ids)) return $this->fail('请选择项目');
  295. $technician_project_data = [];
  296. foreach ($project_ids as $project_id) {
  297. $technician_project_data[] = [
  298. 'store_id', '=', $technician->store_id,
  299. 'project_id' => $project_id,
  300. 'technician_id' => $technician_id
  301. ];
  302. }
  303. $technician_project_model = new TechnicianProjectModel();
  304. $technician_project_model->saveAll($technician_project_data);
  305. return $this->success('成功绑定');
  306. }
  307. public function delTechnicianProject()
  308. {
  309. $bind_id = $this->request->param('bind_id/d', 0);
  310. if ($bind_id == 0) return $this->fail('参数错误');
  311. if (null === $technician_project = TechnicianProjectModel::find($bind_id)) return $this->fail('您未绑定这个项目');
  312. if ($technician_project->is_delete != 0) return $this->fail('您未绑定这个项目');
  313. $technician_project->is_delete = 1;
  314. $technician_project->save();
  315. return $this->success('删除成功');
  316. }
  317. /********************************************************* end 技师详情 end **************************************************************/
  318. }