| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use \think\Request;
- use think\Db;
- /**
- * 员工工资查询
- */
- class StaffSalary extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- // public function GetReportingWorkMonth()
- // {
- // if (!$this->request->isGet()) {
- // $this->error('请求方法错误');
- // }
- // $data = db('设备_工分计酬')
- // ->where('del_rq', null)
- // ->field('DISTINCT DATE_FORMAT(date, "%Y-%m") as month')
- // ->order('date desc')
- // ->select();
- //
- // //获取员工部门
- // $big_process = db('人员_基本资料')
- // ->field('DISTINCT big_process')
- // ->where('big_process','<>','')
- // ->select();
- // foreach ($big_process as $v) {
- // $big_process_name[] = $v['big_process'];
- // }
- // $result = [];
- // foreach ($data as $v) {
- // $result[$v['month']] = $big_process_name;
- // }
- //
- // $this->success('成功', $result);
- // }
- /**
- * 查询报工数据月份(年-年月-日期-工序-小组五级树)
- * @ApiMethod (GET)
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function GetReportingWorkMonth()
- {
- if (!$this->request->isGet()) {
- $this->error('请求方法错误');
- }
- $rows = db('设备_工分计酬')
- ->where('del_rq', null)
- ->where('sys_rq', 'not null')
- ->field('LEFT(sys_rq, 10) as date, IF(majorprocess IS NULL OR majorprocess = "", "其他", majorprocess) as process, rtrim(sys_id) as sys_id')
- ->group('LEFT(sys_rq, 10), process, sys_id')
- ->order('sys_rq desc, process asc, sys_id asc')
- ->select();
- if (empty($rows)) {
- $this->success('成功', []);
- }
- $treeMap = [];
- foreach ($rows as $row) {
- $date = $row['date'];
- if (empty($date)) {
- continue;
- }
- // 拆分年、年月、日期
- $year = substr($date, 0, 4);
- $yearMonth = substr($date, 0, 7);
- $process = $row['process'];
- $sysId = $row['sys_id'] !== '' ? $row['sys_id'] : '未知';
- // 构建五级结构
- if (!isset($treeMap[$year])) {
- $treeMap[$year] = [];
- }
- if (!isset($treeMap[$year][$yearMonth])) {
- $treeMap[$year][$yearMonth] = [];
- }
- if (!isset($treeMap[$year][$yearMonth][$date])) {
- $treeMap[$year][$yearMonth][$date] = [
- 'processes' => [],
- ];
- }
- if (!isset($treeMap[$year][$yearMonth][$date]['processes'][$process])) {
- $treeMap[$year][$yearMonth][$date]['processes'][$process] = [];
- }
- if (!in_array($sysId, $treeMap[$year][$yearMonth][$date]['processes'][$process], true)) {
- $treeMap[$year][$yearMonth][$date]['processes'][$process][] = $sysId;
- }
- }
- // 工序固定排序
- $processOrder = ['裁剪', '车缝', '手工', '大烫', '总检', '包装', '其他'];
- $result = [];
- krsort($treeMap);
- foreach ($treeMap as $year => $yearMonths) {
- $yearNode = [
- 'label' => (string)$year,
- 'value' => (string)$year,
- 'children' => [],
- ];
- krsort($yearMonths);
- foreach ($yearMonths as $yearMonth => $dates) {
- $yearMonthNode = [
- 'label' => $yearMonth,
- 'value' => $yearMonth,
- 'children' => [],
- ];
- krsort($dates);
- foreach ($dates as $dateKey => $dateData) {
- $processChildren = [];
- $sortedProcesses = $this->sortProcessList(array_keys($dateData['processes']), $processOrder);
- foreach ($sortedProcesses as $process) {
- $groups = $dateData['processes'][$process];
- sort($groups);
- $groupChildren = [];
- foreach ($groups as $group) {
- $groupChildren[] = [
- 'label' => $group,
- 'value' => $group,
- ];
- }
- $processChildren[] = [
- 'label' => $process,
- 'value' => $process,
- 'children' => $groupChildren,
- ];
- }
- $dateNode = [
- 'label' => $dateKey,
- 'value' => $dateKey,
- 'children' => $processChildren,
- ];
- $yearMonthNode['children'][] = $dateNode;
- }
- $yearNode['children'][] = $yearMonthNode;
- }
- $result[] = $yearNode;
- }
- $this->success('成功', $result);
- }
- /**
- * 按指定顺序排序工序
- * @param array $processList
- * @param array $order
- * @return array
- */
- private function sortProcessList(array $processList, array $order): array
- {
- $sorted = [];
- foreach ($order as $item) {
- if (in_array($item, $processList)) {
- $sorted[] = $item;
- unset($processList[array_search($item, $processList)]);
- }
- }
- return array_merge($sorted, $processList);
- }
- /**
- * 查询员工工资列表
- * @ApiMethod (GET)
- * @param string $sys_rq 可选,左侧树日期:2026 / 2026-06 / 2026-06-08
- * @param string $big_process 可选,大工序
- * @param string $group 可选,小组
- * @param string $search 可选,顶部搜索:订单编号/员工编号/员工姓名(也兼容 keyword、work_order、staff_no、staff_name)
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function GetStaffSalaryList()
- {
- if (!$this->request->isGet()) {
- $this->error('请求方法错误');
- }
- $rawParam = $this->request->param();
- $param = [];
- foreach ($rawParam as $key => $value) {
- $normKey = strtolower(rtrim(str_replace([' ', ' '], '', (string)$key), '_'));
- if ($normKey === '') {
- continue;
- }
- if (!array_key_exists($normKey, $param) || $param[$normKey] === '' || $param[$normKey] === null) {
- $param[$normKey] = $value;
- }
- }
- $sysRq = !empty($param['sys_rq']) ? trim((string)$param['sys_rq']) : '';
- $bigProcess = !empty($param['big_process']) ? trim((string)$param['big_process']) : '';
- $group = !empty($param['group']) ? trim((string)$param['group']) : '';
- $search = '';
- foreach (['search', 'keyword', 'work_order', 'staff_no', 'staff_name'] as $key) {
- if (!empty($param[$key])) {
- $search = trim((string)$param[$key]);
- break;
- }
- }
- //日期 或 搜索
- if ($sysRq === '' && $search === '') {
- $this->error('请选择左侧日期,或输入订单编号/员工编号/姓名进行查询');
- }
- if ($sysRq !== '') {
- if (!preg_match('/^\d{4}(-\d{2})?(-\d{2})?$/', $sysRq)) {
- $this->error('sys_rq 格式错误,请使用 2026 / 2026-06 / 2026-06-08 格式');
- }
- }
- // 直接查报工计酬表,支持无日期仅按订单/员工搜索
- $query = db('设备_工分计酬')->whereNull('del_rq');
- if ($sysRq !== '') {
- $query->where('sys_rq', 'like', $sysRq . '%');
- }
- if ($bigProcess !== '') {
- $query->where('majorprocess', $bigProcess);
- }
- if ($group !== '') {
- $query->where('sys_id', $group);
- }
- if ($search !== '') {
- // 订单编号 / 员工编号 / 员工姓名 模糊匹配
- $query->where('work_order|staff_no|staff_name', 'like', '%' . $search . '%');
- }
- $salaryRows = $query
- ->field('staff_no,staff_name,DATE_FORMAT(IFNULL(sys_rq, date), "%Y-%m-%d") as date,sum(salary) as salary,sys_id')
- ->group('staff_no,DATE_FORMAT(IFNULL(sys_rq, date), "%Y-%m-%d"),sys_id')
- ->order('staff_no asc,date asc')
- ->select();
- if (empty($salaryRows)) {
- // $this->error('当前条件下暂无工资数据');
- }
- $grouped = [];
- foreach ($salaryRows as $row) {
- $staffNo = $row['staff_no'];
- $name = !empty($row['staff_name']) ? $row['staff_name'] : '';
- if (!isset($grouped[$staffNo])) {
- $grouped[$staffNo] = [
- 'staff' => $staffNo . '-' . $name,
- 'total_salary' => 0,
- 'children' => [],
- ];
- }
- $grouped[$staffNo]['children'][] = [
- '员工编号' => $staffNo,
- '员工姓名' => $name,
- '日期' => $row['date'],
- '工资' => $row['salary'],
- '小组' => $row['sys_id'],
- ];
- $grouped[$staffNo]['total_salary'] += $row['salary'];
- }
- $this->success('成功', array_values($grouped));
- }
- /**
- * 查询员工工资详情
- *
- */
- public function GetStaffSalaryDetail()
- {
- if (!$this->request->isGet()){
- $this->error('请求方法错误');
- }
- $param = $this->request->param();
- if (empty($param['staff_no'])) {
- $this->error('请选择员工');
- }
- if(empty($param['date'])){
- $this->error('请选择日期');
- }
- $where = [
- 'a.staff_no' => $param['staff_no'],
- 'a.sys_rq' => ['like', $param['date'] . '%'],
- 'a.del_rq' => null,
- 'c.mod_rq' => null,
- ];
- $list = db('设备_工分计酬')
- ->alias('a')
- ->join('工单_部件资料 b', 'a.work_order = b.work_order and a.part_code = b.part_code', 'left')
- ->join('工单_基本资料 c', 'a.work_order = c.订单编号', 'left')
- ->field('a.id,a.work_order as 订单编号,DATE_FORMAT(a.date, "%Y-%m-%d") as 日期,a.sys_rq,b.part_name as 部件名称,
- a.part_code as 部件编号,a.salary as 工资,a.number as 数量,a.production_hour as 生产工时,a.production_score as 生产分数,
- a.machine as 设备名称,a.process_code as 工序编号,a.process_name as 工序名称,a.standard_hour as 标准工时,a.standard_score as 标准分数,
- a.coefficient as 系数,a.sys_id as 设备编号,c.生产款号,c.款式')
- ->where($where)
- ->order('a.sys_rq asc,a.process_code asc')
- ->select();
- if(empty($list)){
- $this->error('没有数据');
- }
- $this->success('成功', $list);
- }
- /**
- * 查询月份员工工资数据
- * @ApiMethod (GET)
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function GetStaffSalaryMonth()
- {
- if (!$this->request->isGet()){
- $this->error('请求方法错误');
- }
- $param = $this->request->param();
- if (empty($param['month'])) {
- $this->error('请选择月份');
- }
- $where = [
- 'date' => ['like', $param['month'] . '%'],
- 'del_rq' => null,
- ];
- if (!empty($param['search'])) {
- $where['staff_no|staff_name'] = ['like', '%' . $param['search'] . '%'];
- }
- $list = db('设备_工分计酬')
- ->where($where)
- ->field('staff_no,staff_name,sum(salary) as salary')
- ->group('staff_no,staff_name')
- ->field('staff_no,staff_name,DATE_FORMAT(date, "%Y-%m-%d") as date,sum(salary) as salary')
- ->group('staff_no,DATE_FORMAT(date, "%Y-%m-%d")')
- ->order('staff_no asc,sys_rq asc')
- ->select();
- if(empty($list)){
- $this->error('没有数据');
- }
- $this->success('成功', $list);
- }
- }
|