Staff.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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('人员_基本资料')
  118. ->field('
  119. id,
  120. staff_no as 员工编号,
  121. staff_name as 员工姓名,
  122. gender as 性别,
  123. team_id,
  124. team_name as 所在小组,
  125. big_process as 生产工序,
  126. position as 职称职务,
  127. dept_name as 所在部门,
  128. status as 状态,
  129. sys_rq as 创建日期,
  130. sys_id as 创建人员
  131. ')
  132. ->where($where)
  133. ->whereNull('mod_rq')
  134. ->select();
  135. $this->success('获取员工资料', $staffList);
  136. }
  137. /**
  138. * 新增员工资料
  139. */
  140. public function PostStaffAdd(){
  141. if (Request::instance()->isPost() == false){
  142. $this->error('非法请求');
  143. }
  144. $params = Request::instance()->param();
  145. echo "<pre>";
  146. print_r($params);
  147. echo "<pre>";die;
  148. }
  149. /**
  150. * 修改员工资料
  151. * @ApiMethod POST
  152. *
  153. */
  154. public function PostStaffEdit(){
  155. if (Request::instance()->isPost() == false){
  156. $this->error('非法请求');
  157. }
  158. $params = Request::instance()->param();
  159. if (empty($params['id'])){
  160. $this->error('参数不能为空');
  161. }
  162. $staffCode = $params['id'];
  163. $sql = db('人员_基本资料')->where('员工编号',$staffCode)->fetchSql(true)->whereNull('mod_rq')->update($params);
  164. $res = Db::query($sql);
  165. if ($res !== false){
  166. $this->success('更新成功');
  167. }else{
  168. $this->error('更新失败');
  169. }
  170. }
  171. /**
  172. * 删除员工资料
  173. */
  174. public function delete()
  175. {
  176. if (Request::instance()->isPost() == false){
  177. $this->error('非法请求');
  178. }
  179. $params = Request::instance()->param();
  180. if (empty($params['id'])){
  181. $this->error('参数不能为空');
  182. }
  183. $staffCode = $params['id'];
  184. $sql = db('人员_基本资料')->where('id',$staffCode)->fetchSql(true)->update(['mod_rq'=>date('Y-m-d H:i:s')]);
  185. $res = Db::query($sql);
  186. if ($res !== false){
  187. $this->success('删除成功');
  188. }else{
  189. $this->error('删除失败');
  190. }
  191. }
  192. /**
  193. * 获取设备编组
  194. */
  195. public function GetDeviceNameList(){
  196. if (Request::instance()->isGet() == false){
  197. $this->error('非法请求');
  198. }
  199. $list = Db::name('设备_基本资料')
  200. ->field('UniqId,设备编组,生产工序,工序')
  201. ->where('设备名称','1')
  202. ->whereNull('mod_rq')
  203. ->group('设备编组')
  204. ->order('工序,UniqId asc')
  205. ->select();
  206. $this->success('获取设备编组成功', $list);
  207. }
  208. }