Staff.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use \think\Request;
  5. use \think\Db;
  6. /**
  7. * 员工资料接口
  8. */
  9. class Staff extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 首页
  15. */
  16. public function index()
  17. {
  18. $this->success('员工资料接口');
  19. }
  20. /**
  21. * 获取部门列表
  22. * 一级:部门 dept_name
  23. * 二级:工序 big_process(按固定顺序:裁剪、车缝、手工、大烫、总检、包装、其他)
  24. * 空工序 = 其他
  25. */
  26. public function getDepartment()
  27. {
  28. if (Request::instance()->isGet() == false) {
  29. $this->error('非法请求');
  30. }
  31. // 1. 查询数据,空工序转为"其他"
  32. $list = db('人员_基本资料')
  33. ->field('dept_name, IF(big_process IS NULL OR big_process = "", "其他", big_process) as big_process, COUNT(*) AS count')
  34. ->whereNull('mod_rq')
  35. ->group('dept_name, big_process')
  36. ->select();
  37. // 2. 构建树形结构(一级:部门,二级:工序)
  38. $tree = [];
  39. foreach ($list as $item) {
  40. $dept = $item['dept_name'];
  41. $process = $item['big_process'];
  42. $count = $item['count'];
  43. // 初始化部门节点
  44. if (!isset($tree[$dept])) {
  45. $tree[$dept] = [
  46. 'label' => $dept,
  47. 'count' => 0,
  48. 'children' => []
  49. ];
  50. }
  51. // 部门总人数累加
  52. $tree[$dept]['count'] += $count;
  53. // 工序加入children
  54. $tree[$dept]['children'][] = [
  55. 'label' => $process,
  56. 'count' => $count
  57. ];
  58. }
  59. // 3. 对每个部门下的工序,按固定顺序排序
  60. $sortList = ['裁剪', '车缝', '手工', '大烫', '总检', '包装', '其他'];
  61. foreach ($tree as &$deptNode) {
  62. $sortedChildren = [];
  63. foreach ($sortList as $process) {
  64. foreach ($deptNode['children'] as $child) {
  65. if ($child['label'] === $process) {
  66. $sortedChildren[] = $child;
  67. break;
  68. }
  69. }
  70. }
  71. $deptNode['children'] = $sortedChildren;
  72. }
  73. // 4. 转成索引数组返回
  74. $result = array_values($tree);
  75. $this->success('获取部门数据', $result);
  76. }
  77. // public function getDepartment()
  78. // {
  79. // if (Request::instance()->isGet() == false) {
  80. // $this->error('非法请求');
  81. // }
  82. //
  83. // // 1. 查询数据
  84. // $list = db('人员_基本资料')
  85. // ->field('big_process, dept_name, COUNT(*) AS count')
  86. // ->whereNull('mod_rq')
  87. // ->group('big_process, dept_name')
  88. // ->select();
  89. //
  90. // // 2. 构建树形结构
  91. // $tree = [];
  92. // foreach ($list as $item) {
  93. // $process = $item['big_process'] ?: '其他'; // 空工序 = 其他
  94. // $dept = $item['dept_name'];
  95. // $count = $item['count'];
  96. //
  97. // if (!isset($tree[$process])) {
  98. // $tree[$process] = [
  99. // 'label' => $process,
  100. // 'count' => 0,
  101. // 'children' => []
  102. // ];
  103. // }
  104. //
  105. // $tree[$process]['count'] += $count;
  106. // $tree[$process]['children'][] = [
  107. // 'label' => $dept,
  108. // 'count' => $count
  109. // ];
  110. // }
  111. //
  112. // // 3. 固定排序:裁剪、车缝、手工、大烫、总检、包装、其他
  113. // $sortList = ['裁剪', '车缝', '手工', '大烫', '总检', '包装', '其他'];
  114. // $result = [];
  115. // foreach ($sortList as $item) {
  116. // if (isset($tree[$item])) {
  117. // $result[] = $tree[$item];
  118. // }
  119. // }
  120. //
  121. // $this->success('获取部门数据', $result);
  122. // }
  123. /**
  124. * 获取员工列表信息
  125. */
  126. public function getStaffList(){
  127. if (Request::instance()->isGet() == false){
  128. $this->error('非法请求');
  129. }
  130. $params = Request::instance()->param();
  131. $where = [];
  132. if (!empty($params['search'])){
  133. $where['staff_no|staff_name'] = array('like','%'.$params['search'].'%');
  134. }
  135. if (!empty($params['department_code'])){
  136. $where['big_process|dept_name'] = $params['department_code'];
  137. }
  138. $limit = $params['limit'];
  139. if (empty($limit)){
  140. $limit = 15;
  141. }
  142. $pages = $params['page'];
  143. if (empty($pages)){
  144. $pages = 1;
  145. }
  146. $list = db('人员_基本资料')
  147. ->field('
  148. id,
  149. staff_no as 员工编号,
  150. staff_name as 员工姓名,
  151. gender as 性别,
  152. big_process as 生产工序,
  153. dept_name as 所在部门,
  154. status as 状态,
  155. sys_rq as 创建日期,
  156. sys_id as 创建人员
  157. ')
  158. ->where($where)->page($pages)->limit($limit)->order('id desc')->whereNull('mod_rq')->select();
  159. $total = db('人员_基本资料')->where($where)->whereNull('mod_rq')->count();
  160. $data['list'] = $list;
  161. $data['total'] = $total;
  162. $this->success('获取员工列表信息',$data);
  163. }
  164. /**
  165. * 获取员工资料
  166. * @ApiMethod GET
  167. * @params string code
  168. */
  169. public function getStaffInfo(){
  170. if (Request::instance()->isGet() == false){
  171. $this->error('非法请求');
  172. }
  173. $params = Request::instance()->param();
  174. $where = [];
  175. if (isset($params['id'])){
  176. $where['id'] = $params['id'];
  177. }
  178. // 1. 查询人员基础信息
  179. $staffList = db('人员_基本资料')
  180. ->field('
  181. id,
  182. staff_no as 员工编号,
  183. staff_name as 员工姓名,
  184. gender as 性别,
  185. big_process as 生产工序,
  186. dept_name as 所在部门,
  187. status as 状态,
  188. sys_rq as 创建日期,
  189. sys_id as 创建人员
  190. ')
  191. ->where($where)
  192. ->whereNull('mod_rq')
  193. ->select();
  194. $this->success('获取员工资料', $staffList);
  195. }
  196. /**
  197. * 新增员工资料
  198. */
  199. public function PostStaffAdd(){
  200. if (Request::instance()->isPost() == false){
  201. $this->error('非法请求');
  202. }
  203. $params = Request::instance()->param();
  204. $staff_no = $params['员工编号'] ?? '';
  205. if(empty($staff_no)){
  206. $this->error('员工编号不能为空');
  207. }
  208. // 查询数据库是否已存在
  209. $Find_list = Db::name('人员_基本资料')->where('staff_no', $staff_no)->find();
  210. if($Find_list){
  211. $this->error('员工编号已存在');
  212. }
  213. $data = [];
  214. $data['staff_no'] = $params['员工编号'];
  215. $data['staff_name'] = $params['员工姓名'];
  216. $data['gender'] = $params['性别'];
  217. $data['big_process'] = $params['生产工序'];
  218. $data['dept_name'] = '生产部';
  219. $data['status'] = 1;
  220. $data['sys_rq'] = date('Y-m-d H:i:s');
  221. $data['sys_id'] = $params['sys_id'];
  222. $res = Db::name('人员_基本资料')->insert($data);
  223. if ($res !== false){
  224. $this->success('新增成功');
  225. }else{
  226. $this->error('新增失败');
  227. }
  228. }
  229. /**
  230. * 修改员工资料
  231. * @ApiMethod POST
  232. *
  233. */
  234. public function PostStaffEdit(){
  235. if (!Request::instance()->isPost()){
  236. $this->error('非法请求');
  237. }
  238. $params = Request::instance()->param();
  239. //必传参数校验
  240. if (empty($params['id'])){
  241. $this->error('参数不能为空');
  242. }
  243. if (empty($params['员工编号'])){
  244. $this->error('员工编号不能为空');
  245. }
  246. //查询员工是否存在(未删除)
  247. $info = db('人员_基本资料')
  248. ->where('id', $params['id'])
  249. ->whereNull('mod_rq')
  250. ->find();
  251. if (!$info){
  252. $this->error('员工不存在');
  253. }
  254. //员工编号重复判断(排除自身)
  255. $exist = db('人员_基本资料')
  256. ->where('staff_no', $params['员工编号'])
  257. ->where('id', '<>', $params['id']) // 排除自己
  258. ->whereNull('mod_rq')
  259. ->find();
  260. if ($exist) {
  261. $this->error('员工编号已存在');
  262. }
  263. //修改数据
  264. $data = [];
  265. $data['staff_no'] = $params['员工编号'];
  266. $data['staff_name'] = $params['员工姓名'];
  267. $data['gender'] = $params['性别'];
  268. $data['big_process'] = $params['生产工序'];
  269. $data['dept_name'] = '生产部';
  270. $data['updatetime'] = date('Y-m-d H:i:s');
  271. $data['sys_id'] = $params['sys_id'];
  272. $res = db('人员_基本资料')
  273. ->where('id', $params['id'])
  274. ->update($data);
  275. if ($res !== false){
  276. $this->success('更新成功');
  277. }else{
  278. $this->error('更新失败');
  279. }
  280. }
  281. /**
  282. * 删除员工资料
  283. */
  284. public function PostStaffDelete()
  285. {
  286. if (Request::instance()->isPost() == false){
  287. $this->error('非法请求');
  288. }
  289. $params = Request::instance()->param();
  290. if (empty($params['id'])){
  291. $this->error('参数不能为空');
  292. }
  293. $staffCode = $params['id'];
  294. $sql = db('人员_基本资料')->where('id',$staffCode)->fetchSql(true)->update(['mod_rq'=>date('Y-m-d H:i:s')]);
  295. $res = Db::query($sql);
  296. if ($res !== false){
  297. $this->success('删除成功');
  298. }else{
  299. $this->error('删除失败');
  300. }
  301. }
  302. /**
  303. * 获取小组
  304. */
  305. public function GetDeviceNameList(){
  306. if (!Request::instance()->isGet()){
  307. $this->error('非法请求');
  308. }
  309. // 定义需要过滤、不返回给前端的设备编组
  310. $filterGroups = [
  311. '自动裁床01组',
  312. '自动裁床02组',
  313. '模板机01组',
  314. '模板机02组',
  315. '上袖机01组',
  316. '上袖机02组',
  317. '裁床1',
  318. '裁床2',
  319. '模版机'
  320. ];
  321. $list = Db::name('设备_基本资料')
  322. ->field('UniqId,设备编组,生产工序,工序')
  323. ->where('设备名称', 1)
  324. ->whereNull('mod_rq')
  325. ->whereNotIn('设备编组', $filterGroups) // 过滤不想要的数据
  326. ->group('设备编组')
  327. ->order('工序,UniqId asc')
  328. ->select();
  329. $this->success('获取设备编组成功', $list);
  330. }
  331. /**
  332. * 获取小组及人员列表
  333. * 支持按工序/小组名称搜索,返回每个小组下的成员信息
  334. */
  335. public function GetTeamStaffList()
  336. {
  337. if (Request::instance()->isGet() == false) {
  338. $this->error('非法请求');
  339. }
  340. $params = Request::instance()->param();
  341. $where = [];
  342. if (!empty($params['search'])) {
  343. $where['b.staff_no|b.staff_name|a.team_name|b.big_process'] = array('like', '%' . $params['search'] . '%');
  344. }
  345. $list = Db::name('人员_小组资料')->alias('a')
  346. ->field('
  347. b.staff_no as 员工编号,
  348. b.staff_name as 员工姓名,
  349. a.*
  350. ')
  351. ->join('人员_基本资料 b', 'a.staff_no = b.staff_no')
  352. ->where($where)
  353. ->whereNull('a.mod_rq')
  354. ->whereNull('b.mod_rq')
  355. ->order('a.id asc')
  356. ->select();
  357. // 整理成"小组+成员"的树形结构
  358. $result = [];
  359. foreach ($list as $item) {
  360. $teamId = $item['team_id'];
  361. if (!isset($result[$teamId])) {
  362. $result[$teamId] = [
  363. '小组ID' => $teamId,
  364. '小组名称' => $item['team_name'],
  365. '生产工序' => $item['big_process'],
  366. 'staff_list' => []
  367. ];
  368. }
  369. $member = [
  370. 'id' => $item['id'],
  371. '员工编号' => $item['员工编号'],
  372. '员工姓名' => $item['员工姓名'],
  373. '小组名称' => $item['team_name'],
  374. '职位' => $item['position']
  375. ];
  376. // 组长排第一个
  377. if ($item['position'] == '组长') {
  378. array_unshift($result[$teamId]['staff_list'], $member);
  379. } else {
  380. $result[$teamId]['staff_list'][] = $member;
  381. }
  382. }
  383. // 转为索引数组返回
  384. $data = array_values($result);
  385. $this->success('获取成功', $data);
  386. }
  387. /**
  388. * 新增小组人员
  389. */
  390. public function AddTeamStaff()
  391. {
  392. if (!Request::instance()->isPost()) {
  393. $this->error('非法请求');
  394. }
  395. $params = Request::instance()->param();
  396. // 1. 校验员工是否存在
  397. $staffInfo = Db::name('人员_基本资料')
  398. ->where('staff_no', $params['staff_no'])
  399. ->whereNull('mod_rq')
  400. ->find();
  401. if (!$staffInfo) {
  402. $this->error('员工不存在或已删除');
  403. }
  404. // 2. 校验该员工是否已在小组中
  405. $exist = Db::name('人员_小组资料')
  406. ->where('staff_no', $params['staff_no'])
  407. ->whereNull('mod_rq')
  408. ->find();
  409. if ($exist) {
  410. $this->error('该员工已在'.$exist['team_name'].'小组中');
  411. }
  412. // 3. 组装数据
  413. $data = [
  414. 'big_process' => $params['big_process'],
  415. 'team_id' => $params['team_id'],
  416. 'team_name' => $params['team_name'],
  417. 'staff_no' => $params['staff_no'],
  418. 'position' => $params['position'],
  419. 'status' => 1,
  420. 'sys_id' => $params['sys_id'],
  421. 'createtime' => date('Y-m-d H:i:s')
  422. ];
  423. $res = Db::name('人员_小组资料')->insert($data);
  424. if ($res) {
  425. $this->success('新增成功');
  426. } else {
  427. $this->error('新增失败');
  428. }
  429. }
  430. /**
  431. * 修改小组人员
  432. */
  433. public function EditTeamStaff()
  434. {
  435. if (!Request::instance()->isPost()) {
  436. $this->error('非法请求');
  437. }
  438. $params = Request::instance()->param();
  439. // 必传参数校验
  440. if (empty($params['id'])) {
  441. $this->error('参数不完整');
  442. }
  443. // 1. 校验该记录是否存在
  444. $info = Db::name('人员_小组资料')
  445. ->where('id', $params['id'])
  446. ->whereNull('mod_rq')
  447. ->find();
  448. if (!$info) {
  449. $this->error('记录不存在或已删除');
  450. }
  451. // 2. 如果修改了员工ID,校验新员工是否已在该小组中(排除自身)
  452. if (!empty($params['staff_id']) && $params['staff_id'] != $info['staff_id']) {
  453. $exist = Db::name('人员_小组资料')
  454. ->where('team_id', $params['team_id'] ?? $info['team_id'])
  455. ->where('staff_id', $params['staff_id'])
  456. ->where('id', '<>', $params['id'])
  457. ->whereNull('mod_rq')
  458. ->find();
  459. if ($exist) {
  460. $this->error('该员工已在此小组中');
  461. }
  462. }
  463. // 3. 组装更新数据
  464. $data = [];
  465. if (isset($params['big_process'])) $data['big_process'] = $params['big_process'];
  466. if (isset($params['team_id'])) $data['team_id'] = $params['team_id'];
  467. if (isset($params['team_name'])) $data['team_name'] = $params['team_name'];
  468. if (isset($params['position'])) $data['position'] = $params['position'];
  469. if (isset($params['staff_id'])) {
  470. $data['staff_id'] = $params['staff_id'];
  471. // 更新员工姓名
  472. $staffInfo = Db::name('人员_基本资料')->where('id', $params['staff_id'])->find();
  473. if ($staffInfo) {
  474. $data['staff_name'] = $staffInfo['staff_name'];
  475. }
  476. }
  477. if (isset($params['position'])) $data['position'] = $params['position'];
  478. $data['updatetime'] = date('Y-m-d H:i:s');
  479. $res = Db::name('人员_小组资料')
  480. ->where('id', $params['id'])
  481. ->update($data);
  482. if ($res !== false) {
  483. $this->success('小组更换成功');
  484. } else {
  485. $this->error('小组更换失败');
  486. }
  487. }
  488. /**
  489. * 删除小组人员
  490. */
  491. public function DelTeamStaff()
  492. {
  493. if (!Request::instance()->isPost()) {
  494. $this->error('非法请求');
  495. }
  496. $params = Request::instance()->param();
  497. $res = Db::name('人员_小组资料')
  498. ->where('id', $params['id'])
  499. ->update([
  500. 'mod_rq' => date('Y-m-d H:i:s')
  501. ]);
  502. if ($res !== false) {
  503. $this->success('删除成功');
  504. } else {
  505. $this->error('删除失败');
  506. }
  507. }
  508. }