Staff.php 5.9 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('dept_name as 所在部门, big_process as 生产工序, 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. $result = array_values($tree);
  59. $this->success('获取部门数据', $result);
  60. }
  61. /**
  62. * 获取员工列表信息
  63. */
  64. public function getStaffList(){
  65. if (Request::instance()->isGet() == false){
  66. $this->error('非法请求');
  67. }
  68. $params = Request::instance()->param();
  69. $where = [];
  70. if (!empty($params['search'])){
  71. $where['staff_no|staff_name'] = array('like','%'.$params['search'].'%');
  72. }
  73. $limit = $params['limit'];
  74. if (empty($limit)){
  75. $limit = 15;
  76. }
  77. $pages = $params['page'];
  78. if (empty($pages)){
  79. $pages = 1;
  80. }
  81. $list = db('人员_基本资料')
  82. ->field('
  83. id,
  84. staff_no as 员工编号,
  85. staff_name as 员工姓名,
  86. gender as 性别,
  87. team_id,
  88. team_name as 所在小组,
  89. big_process as 生产工序,
  90. position as 职称职务,
  91. dept_name as 所在部门,
  92. status as 状态,
  93. sys_rq as 创建日期,
  94. sys_id as 创建人员
  95. ')
  96. ->where($where)->page($pages)->limit($limit)->order('id asc')->whereNull('mod_rq')->select();
  97. $total = db('人员_基本资料')->where($where)->whereNull('mod_rq')->count();
  98. $data['list'] = $list;
  99. $data['total'] = $total;
  100. $this->success('获取员工列表信息',$data);
  101. }
  102. /**
  103. * 获取员工资料
  104. * @ApiMethod GET
  105. * @params string code
  106. */
  107. public function getStaffInfo(){
  108. if (Request::instance()->isGet() == false){
  109. $this->error('非法请求');
  110. }
  111. $params = Request::instance()->param();
  112. $where = [];
  113. if (isset($params['id'])){
  114. $where['a.id'] = $params['id'];
  115. }
  116. // 1. 查询人员基础信息
  117. $staffList = db('人员_基本资料')->alias('a')
  118. ->where($where)
  119. ->whereNull('a.mod_rq')
  120. ->select();
  121. $this->success('获取员工资料', $staffList);
  122. }
  123. /**
  124. * 新增员工资料
  125. */
  126. public function PostStaffAdd(){
  127. if (Request::instance()->isPost() == false){
  128. $this->error('非法请求');
  129. }
  130. $params = Request::instance()->param();
  131. echo "<pre>";
  132. print_r($params);
  133. echo "<pre>";die;
  134. }
  135. /**
  136. * 修改员工资料
  137. * @ApiMethod POST
  138. *
  139. */
  140. public function PostStaffEdit(){
  141. if (Request::instance()->isPost() == false){
  142. $this->error('非法请求');
  143. }
  144. $params = Request::instance()->param();
  145. if (empty($params['id'])){
  146. $this->error('参数不能为空');
  147. }
  148. $staffCode = $params['id'];
  149. $sql = db('人员_基本资料')->where('员工编号',$staffCode)->fetchSql(true)->whereNull('mod_rq')->update($params);
  150. $res = Db::query($sql);
  151. if ($res !== false){
  152. $this->success('更新成功');
  153. }else{
  154. $this->error('更新失败');
  155. }
  156. }
  157. /**
  158. * 删除员工资料
  159. */
  160. public function delete()
  161. {
  162. if (Request::instance()->isPost() == false){
  163. $this->error('非法请求');
  164. }
  165. $params = Request::instance()->param();
  166. if (empty($params['id'])){
  167. $this->error('参数不能为空');
  168. }
  169. $staffCode = $params['id'];
  170. $sql = db('人员_基本资料')->where('id',$staffCode)->fetchSql(true)->update(['mod_rq'=>date('Y-m-d H:i:s')]);
  171. $res = Db::query($sql);
  172. if ($res !== false){
  173. $this->success('删除成功');
  174. }else{
  175. $this->error('删除失败');
  176. }
  177. }
  178. /**
  179. * 获取设备编组
  180. */
  181. public function GetDeviceNameList(){
  182. if (Request::instance()->isGet() == false){
  183. $this->error('非法请求');
  184. }
  185. $list = Db::name('设备_基本资料')
  186. ->field('UniqId,设备编组,生产工序,工序')
  187. ->where('设备名称','1')
  188. ->whereNull('mod_rq')
  189. ->group('设备编组')
  190. ->order('工序,UniqId asc')
  191. ->select();
  192. $this->success('获取设备编组成功', $list);
  193. }
  194. }