Staff.php 18 KB

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