Staff.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. */
  23. public function getDepartment(){
  24. if (Request::instance()->isGet() == false){
  25. $this->error('非法请求');
  26. }
  27. // 1. 查询所有有效人员(按部门+工序分组)
  28. $list = db('人员_基本资料')
  29. ->field('所在部门, 生产工序, COUNT(*) AS count')
  30. ->whereNull('mod_rq')
  31. ->group('所在部门, 生产工序')
  32. ->order('id asc')
  33. ->select();
  34. // 2. 构建 部门 → 工序 二级结构
  35. $tree = [];
  36. foreach ($list as $item) {
  37. $dept = $item['所在部门'];
  38. $process = $item['生产工序'];
  39. $count = $item['count'];
  40. // 初始化部门
  41. if (!isset($tree[$dept])) {
  42. $tree[$dept] = [
  43. 'label' => $dept,
  44. 'count' => 0,
  45. 'children' => []
  46. ];
  47. }
  48. // 总数量累加
  49. $tree[$dept]['count'] += $count;
  50. // 有生产工序 → 加入二级
  51. if (!empty($process)) {
  52. $tree[$dept]['children'][] = [
  53. 'label' => $process,
  54. 'count' => $count
  55. ];
  56. }
  57. }
  58. // 转成索引数组返回
  59. $result = array_values($tree);
  60. $this->success('获取部门数据', $result);
  61. }
  62. /**
  63. * 获取员工列表信息
  64. */
  65. public function getStaffList(){
  66. if (Request::instance()->isGet() == false){
  67. $this->error('非法请求');
  68. }
  69. $params = Request::instance()->param();
  70. $where = [];
  71. if (!empty($params['search'])){
  72. $where['员工编号|员工姓名'] = array('like','%'.$params['search'].'%');
  73. }
  74. if (!empty($params['department_code'])) {
  75. $where['所在部门|生产工序'] = $params['department_code'];
  76. }
  77. $limit = $params['limit'];
  78. if (empty($limit)){
  79. $limit = 15;
  80. }
  81. $pages = $params['page'];
  82. if (empty($pages)){
  83. $pages = 1;
  84. }
  85. $list = db('人员_基本资料')->where($where)->page($pages)->limit($limit)->order('id asc')->whereNull('mod_rq')->select();
  86. $total = db('人员_基本资料')->where($where)->whereNull('mod_rq')->count();
  87. $data['list'] = $list;
  88. $data['total'] = $total;
  89. $this->success('获取员工列表信息',$data);
  90. }
  91. /**
  92. * 获取员工资料
  93. * @ApiMethod GET
  94. * @params string code
  95. */
  96. public function getStaffInfo(){
  97. if (Request::instance()->isGet() == false){
  98. $this->error('非法请求');
  99. }
  100. $params = Request::instance()->param();
  101. $where = [];
  102. if (isset($params['id'])){
  103. $where['a.id'] = $params['id'];
  104. }
  105. // 1. 查询人员基础信息
  106. $staffList = db('人员_基本资料')->alias('a')
  107. ->where($where)
  108. ->whereNull('a.mod_rq')
  109. ->select();
  110. // 2. 循环给每个人绑定 工序列表
  111. foreach ($staffList as &$item) {
  112. // 查询该人员的所有绑定工序
  113. $processList = db('人员_工序绑定关联')
  114. ->where('user_id', $item['id'])
  115. ->whereNull('mod_rq')
  116. ->column('工序ID,工序名称,工序编码,生产工序');
  117. // 把工序放进当前人员信息里
  118. $item['process_list'] = $processList;
  119. }
  120. $this->success('获取员工资料', $staffList);
  121. }
  122. /**
  123. * 新增员工资料
  124. */
  125. public function PostStaffAdd(){
  126. if (Request::instance()->isPost() == false){
  127. $this->error('非法请求');
  128. }
  129. $params = Request::instance()->param();
  130. echo "<pre>";
  131. print_r($params);
  132. echo "<pre>";die;
  133. }
  134. /**
  135. * 修改员工资料
  136. * @ApiMethod POST
  137. *
  138. */
  139. public function PostStaffEdit(){
  140. if (Request::instance()->isPost() == false){
  141. $this->error('非法请求');
  142. }
  143. $params = Request::instance()->param();
  144. if (empty($params['id'])){
  145. $this->error('参数不能为空');
  146. }
  147. $staffCode = $params['id'];
  148. $sql = db('人员_基本资料')->where('员工编号',$staffCode)->fetchSql(true)->whereNull('mod_rq')->update($params);
  149. $res = Db::query($sql);
  150. if ($res !== false){
  151. $this->success('更新成功');
  152. }else{
  153. $this->error('更新失败');
  154. }
  155. }
  156. /**
  157. * 删除员工资料
  158. */
  159. public function delete()
  160. {
  161. if (Request::instance()->isPost() == false){
  162. $this->error('非法请求');
  163. }
  164. $params = Request::instance()->param();
  165. if (empty($params['id'])){
  166. $this->error('参数不能为空');
  167. }
  168. $staffCode = $params['id'];
  169. $sql = db('人员_基本资料')->where('id',$staffCode)->fetchSql(true)->update(['mod_rq'=>date('Y-m-d H:i:s')]);
  170. $res = Db::query($sql);
  171. if ($res !== false){
  172. $this->success('删除成功');
  173. }else{
  174. $this->error('删除失败');
  175. }
  176. }
  177. /**
  178. * 获取设备编组
  179. */
  180. public function GetDeviceNameList(){
  181. if (Request::instance()->isGet() == false){
  182. $this->error('非法请求');
  183. }
  184. $list = Db::name('设备_基本资料')
  185. ->field('设备编组,生产工序,工序')
  186. ->where('设备名称','1')
  187. ->whereNull('mod_rq')
  188. ->group('设备编组')
  189. ->order('工序,UniqId asc')
  190. ->select();
  191. $this->success('获取设备编组成功', $list);
  192. }
  193. }