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