| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use \think\Request;
- use \think\Db;
- /**
- * 糊盒计时单维护
- */
- class GluChronographSheet extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- /**
- * 获取计件工计时单侧边栏
- * @ApiMethod (GET)
- */
- public function getTab()
- {
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $rows = db()->table('糊盒报工机时')
- ->field('LEFT(wgjs_rq, 7) as date')
- ->group('date')
- ->order('UniqId desc')
- ->limit(15)
- ->select();
- foreach($rows as $key=>$value){
- $rows[$key]['date'] = str_replace('-', '', $rows[$key]['date']);
- }
- $this->success('成功',$rows);
- }
- /**
- * 获取计件工计时单列表
- * @ApiMethod (GET)
- * @param string $date 时间
- * @param string $Sczl_bh1 员工编号
- */
- public function getList()
- {
- // 验证请求方式
- if (!$this->request->isGet()) {
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- // 必需参数校验
- if (empty($req['date'])) {
- $this->error('参数错误: 缺少日期参数');
- }
- $where['wgjs_rq'] = ['LIKE', $req['date'] . '%'];
- $usePagination = isset($req['page'], $req['limit']) && $req['page'] > 0 && $req['limit'] > 0;
- // 构建基础查询
- $query = db()->table('糊盒报工机时')
- ->field('LEFT(wgjs_rq, 10) as wgjs_rq, UniqId,
- wgjs_bh1, wgjs_js1, rtrim(wgjs_yy1) as wgjs_yy1,
- wgjs_bh2, wgjs_js2, rtrim(wgjs_yy2) as wgjs_yy2,
- wgjs_bh3, wgjs_js3, rtrim(wgjs_yy3) as wgjs_yy3,
- wgjs_bh4, wgjs_js4, rtrim(wgjs_yy4) as wgjs_yy4,
- wgjs_bh5, wgjs_js5, rtrim(wgjs_yy5) as wgjs_yy5,
- wgjs_bh6, wgjs_js6, rtrim(wgjs_yy6) as wgjs_yy6')
- ->where($where)
- ->order('wgjs_rq desc, UniqId asc');
- // 执行分页/全量查询
- if ($usePagination) {
- $rows = $query->page($req['page'], $req['limit'])->select();
- $total = db()->table('db_wgjs')->where($where)->count();
- } else {
- $rows = $query->select();
- $total = count($rows); // 避免额外COUNT查询
- }
- // 获取员工基础数据
- $employees = db()->table('人事_基本资料')->column('员工编号, 员工姓名, 所在部门');
- // 数据处理优化
- foreach ($rows as &$row) {
- // 处理主员工信息
- $mainId = $row['wgjs_bh1'];
- $hasMain = isset($employees[$mainId]);
- $row['wgjs_js1'] = $row['wgjs_js1'] == 0 ? '' : $row['wgjs_js1'];
- $row['department'] = $hasMain ? trim($employees[$mainId]['所在部门']) : '';
- $row['name1'] = $hasMain ? trim($employees[$mainId]['员工姓名']) : '';
- // 处理2-6号员工信息
- for ($i = 2; $i <= 6; $i++) {
- $fieldId = $row["wgjs_bh$i"];
- $row["wgjs_js$i"] = $row["wgjs_js$i"] == '0.0' ? '' : $row["wgjs_js$i"];
- $row["name$i"] = ($fieldId && isset($employees[$fieldId]))
- ? trim($employees[$fieldId]['员工姓名'])
- : ($fieldId === $mainId ? $row['name1'] : '');
- }
- }
- $this->success('成功', [
- 'total' => $total,
- 'rows' => $rows
- ]);
- }
- /**
- * 获取计件工计时单信息
- * @ApiMethod (GET)
- * @param string $UniqId UniqId
- */
- public function getInfo()
- {
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- if (isset($req['UniqId']) && !empty($req['UniqId'])){
- $UniqId = $req['UniqId'];
- }else{
- $this->error('参数错误');
- }
- $rows = db()->table('糊盒报工机时')->alias('d')
- ->field('d.*, ')
- ->join('工单_基本资料 g', 'd.')
- ->where('d.UniqId',$UniqId)
- ->select();
- $this->success('成功',$rows);
- }
- /**
- * 定位
- * @ApiMethod GET
- */
- public function search(){
- if (Request::instance()->isGet() == false){
- $this->error('非法请求');
- }
- $req = Request::instance()->param();
- if (!isset($req['search']) || !isset($req['date'])){
- $this->error('参数错误');
- }
- $page = 1;
- $limit = 10;
- if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
- if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
- $year=substr($req['date'],0,4);
- $month=substr($req['date'],-2);
- $start_time = $year . '-' . $month . '-01 00:00:00';
- $end_time = date('Y-m-t', strtotime("$year-$month-01")) . ' 23:59:59';
- $yg = db()->table('人事_基本资料')->where('员工姓名',$req['search'])->value('员工编号');
- if($yg){
- $req['search']=$yg;
- }
- $where = [
- 'a.wgjs_rq'=>['between',"$start_time,$end_time"],
- 'a.wgjs_bh1|a.wgjs_bh2|a.wgjs_bh3|a.wgjs_bh4|a.wgjs_bh5|a.wgjs_bh6'=>['like', '%'.$req['search'].'%']
- ];
- $rows = db()->table('糊盒报工机时')
- ->alias('a')
- ->field('LEFT(a.wgjs_rq, 10) as wgjs_rq,
- a.wgjs_bh1,trim(rs1.所在部门) as 所在部门, a.wgjs_js1, rtrim(a.wgjs_yy1) as wgjs_yy1,
- a.wgjs_bh2, a.wgjs_js2, rtrim(a.wgjs_yy2) as wgjs_yy2,
- a.wgjs_bh3, a.wgjs_js3, rtrim(a.wgjs_yy3) as wgjs_yy3,
- a.wgjs_bh4, a.wgjs_js4, rtrim(a.wgjs_yy4) as wgjs_yy4,
- a.wgjs_bh5, a.wgjs_js5, rtrim(a.wgjs_yy5) as wgjs_yy5,
- a.wgjs_bh6, a.wgjs_js6, rtrim(a.wgjs_yy6) as wgjs_yy6,
- rtrim(rs1.员工姓名) as name1,rtrim(rs2.员工姓名) as name2,rtrim(rs3.员工姓名) as name3,
- rtrim(rs4.员工姓名) as name4,rtrim(rs5.员工姓名) as name5,rtrim(rs6.员工姓名) as name6,
- a.UniqId')
- ->join('人事_基本资料 rs1','rs1.员工编号=a.wgjs_bh1','LEFT')
- ->join('人事_基本资料 rs2','rs2.员工编号=a.wgjs_bh2','LEFT')
- ->join('人事_基本资料 rs3','rs3.员工编号=a.wgjs_bh3','LEFT')
- ->join('人事_基本资料 rs4','rs4.员工编号=a.wgjs_bh4','LEFT')
- ->join('人事_基本资料 rs5','rs5.员工编号=a.wgjs_bh5','LEFT')
- ->join('人事_基本资料 rs6','rs6.员工编号=a.wgjs_bh6','LEFT')
- ->where($where)
- ->order('wgjs_rq desc, UniqId asc')
- ->page($page,$limit)
- ->select();
- $total = db()->table('糊盒报工机时')->where($where)->count();
- $data = ['total'=> $total,'rows'=> $rows];
- if($rows){
- $this->success('成功',$data);
- }else{
- $this->error('失败');
- }
- }
- /**
- * 详情
- * @ApiMethod (GET)
- * @param string $wgjs_rq 日期
- * @param string $wgjs_bh1 员工编号
- */
- public function detail(){
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
- $this->error('参数错误','UniqId',100);
- }
- $rows = db('糊盒报工机时')
- ->alias('a')
- ->field('LEFT(a.wgjs_rq, 10) as wgjs_rq, a.UniqId,
- wgjs_bh1, wgjs_js1, rtrim(wgjs_yy1) as wgjs_yy1,
- wgjs_bh2, wgjs_js2, rtrim(wgjs_yy2) as wgjs_yy2,
- wgjs_bh3, wgjs_js3, rtrim(wgjs_yy3) as wgjs_yy3,
- wgjs_bh4, wgjs_js4, rtrim(wgjs_yy4) as wgjs_yy4,
- wgjs_bh5, wgjs_js5, rtrim(wgjs_yy5) as wgjs_yy5,
- wgjs_bh6, wgjs_js6, rtrim(wgjs_yy6) as wgjs_yy6,
- rtrim(rs1.员工姓名) as name1,rtrim(rs2.员工姓名) as name2,rtrim(rs3.员工姓名)
- as name3,rtrim(rs4.员工姓名) as name4,rtrim(rs5.员工姓名) as name5,rtrim(rs6.员工姓名)
- as name6')
- ->join('人事_基本资料 rs1','rs1.员工编号=a.wgjs_bh1','LEFT')
- ->join('人事_基本资料 rs2','rs2.员工编号=a.wgjs_bh2','LEFT')
- ->join('人事_基本资料 rs3','rs3.员工编号=a.wgjs_bh3','LEFT')
- ->join('人事_基本资料 rs4','rs4.员工编号=a.wgjs_bh4','LEFT')
- ->join('人事_基本资料 rs5','rs5.员工编号=a.wgjs_bh5','LEFT')
- ->join('人事_基本资料 rs6','rs6.员工编号=a.wgjs_bh6','LEFT')
- ->where('a.UniqId', $req['UniqId'])
- ->find();
- if($rows!==false){
- $this->success('成功',$rows);
- }else{
- $this->error('失败');
- }
- }
- /**
- * 修改
- * @ApiMethod POST
- */
- public function edit()
- {
- if(!$this->request->isPost()){
- $this->error('请求方式错误');
- }
- $req = Request::instance()->post();
- if (!isset($req['UniqId']) || empty($req['UniqId'])) {
- $this->error('参数错误','UniqId',100);
- }
- $arr = [
- 'wgjs_rq',
- 'wgjs_bh1', 'wgjs_bh2', 'wgjs_bh3', 'wgjs_bh4', 'wgjs_bh5', 'wgjs_bh6',
- 'wgjs_js1', 'wgjs_js2', 'wgjs_js3', 'wgjs_js4', 'wgjs_js5', 'wgjs_js6',
- 'wgjs_yy1', 'wgjs_yy2', 'wgjs_yy3', 'wgjs_yy4', 'wgjs_yy5', 'wgjs_yy6'
- ];
- $data = [];
- foreach ($arr as $key => $value){
- if (!isset($req[$value])){
- continue;
- }
- $data[$value] = $req[$value];
- }
- if (count($data)==0){
- $this->error('参数错误','',111);
- }
- $data['wgjs_rq'] = $req['wgjs_rq'].' 00:00:00';
- $data['mod_rq'] = date('Y-m-d H:i:s');
- //开启事务
- db()->startTrans();
- try{
- $sql = db('糊盒报工机时')->where('UniqId',$req['UniqId'])->fetchSql(true)->update($data);
- $bool = db()->query($sql);
- // 提交事务
- db()->commit();
- } catch (\Exception $e) {
- // 回滚事务
- db()->rollback();
- $this->error($e->getMessage());
- }
- if($bool===false) $this->error('失败');
- $this->success('成功');
- }
- /**
- * 新增
- * @ApiMethod POST
- */
- public function add()
- {
- if(!$this->request->isPost()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- if (!isset($req['wgjs_rq']) || !isset($req['wgjs_bh1']) || !isset($req['sys_id']) ){
- $this->error('参数错误');
- }
- if (empty($req['wgjs_rq']) || empty($req['wgjs_bh1']) || empty($req['sys_id'])){
- $this->error('参数不能为空');
- }
- $req['wgjs_rq'] = $req['wgjs_rq'].' 00:00:00';
- $req['sys_rq'] = date('Y-m-d H:i:s');
- //开启事务
- db()->startTrans();
- try{
- $sql = db()->table('糊盒报工机时')->fetchSql(true)->insert($req);
- $res= db()->query($sql);
- // 提交事务
- db()->commit();
- } catch (\Exception $e) {
- // 回滚事务
- db()->rollback();
- $this->error($e->getMessage());
- }
- if($res===false) $this->error('失败');
- $this->success('成功');
- }
- /**
- * 删除
- * @ApiMethod (GET)
- * @param string $wgjs_rq 日期
- * @param string $wgjs_bh1 员工编号
- */
- public function del(){
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
- $this->error('参数错误','UniqId',100);
- }
- //开启事务
- db()->startTrans();
- try{
- $bool = db('糊盒报工机时')->where('UniqId',$req['UniqId'])->delete();
- // 提交事务
- db()->commit();
- } catch (\Exception $e) {
- // 回滚事务
- db()->rollback();
- $this->error($e->getMessage());
- }
- if($bool===false) $this->error('失败');
- $this->success('成功');
- }
- /**
- * 糊盒班组计时报工
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function GluingReportClock()
- {
- if (!$this->request->isPost()) {
- $this->error('请求错误');
- }
- $param = $this->request->post();
- $validate = new \think\Validate([
- 'sczl_rq' => 'require',
- 'type' => 'require',
- ], [
- 'sczl_rq.require' => '请选择上报日期',
- 'type.require' => '请选择计时类型',
- ]);
- if (!$validate->check($param)) {
- $this->error($validate->getError());
- }
- $employeeFields = $this->parseGluingReportClockEmployeeFields($param);
- $data = array_merge([
- 'sczl_rq' => $param['sczl_rq'],
- 'type' => $param['type'],
- 'duration' => $param['duration'],
- 'price' => $param['price'],
- 'remark' => $param['remark'],
- 'sys_id' => $param['sys_id'],
- 'sys_rq' => date('Y-m-d H:i:s'),
- ], $employeeFields);
- if (\db('糊盒班组计时')->insert($data) === false) {
- $this->error('报工失败');
- }
- $this->success('报工成功');
- }
- /**
- * 糊盒班组计时报工删除
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function deleteGluingReportClock()
- {
- if (!$this->request->isPost()) {
- $this->error('请求错误');
- }
- $params = $this->request->param();
- if (!isset($params['id']) || empty($params['id'])){
- $this->error('参数错误');
- }
- $ids = explode(',', $params['id']);
- $res = \db('糊盒班组计时')->where('Uid', 'in', $ids)->delete();
- if ($res === false) {
- $this->error('删除失败');
- }
- $this->success('删除成功');
- }
- /**
- * 糊盒班组计时报工修改
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function updateGluingReportClock()
- {
- if (!$this->request->isPost()) {
- $this->error('请求错误');
- }
- $param = $this->request->post();
- if (!isset($param['id']) || empty($param['id'])) {
- $this->error('参数错误');
- }
- $validate = new \think\Validate([
- 'sczl_rq' => 'require',
- 'type' => 'require',
- ], [
- 'sczl_rq.require' => '请选择上报日期',
- 'type.require' => '请选择计时类型',
- ]);
- if (!$validate->check($param)) {
- $this->error($validate->getError());
- }
- $id = intval($param['id']);
- $employeeFields = $this->parseGluingReportClockEmployeeFields($param);
- $data = array_merge([
- 'sczl_rq' => $param['sczl_rq'],
- 'type' => $param['type'],
- 'duration' => $param['duration'],
- 'price' => $param['price'],
- 'remark' => $param['remark'],
- 'mod_rq' => date('Y-m-d H:i:s'),
- ], $employeeFields);
- $result = \db('糊盒班组计时')->where('Uid', $id)->update($data);
- if ($result === false) {
- $this->error('修改失败');
- }
- $this->success('修改成功');
- }
- /**
- * 糊盒班组计时报工列表
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function getGluingReportClockList()
- {
- if (!$this->request->isPost()) {
- $this->error('请求错误');
- }
- $params = $this->request->param();
- if (empty($params['sczl_rq'])) {
- $this->error('参数错误');
- }
- $bhFields = [];
- for ($i = 1; $i <= 30; $i++) {
- $bhFields[] = 'bh' . $i;
- }
- $list = \db('糊盒班组计时')
- ->field(array_merge([
- 'id as 编号',
- 'sczl_rq as 上报日期',
- 'type as 计时类型',
- 'duration as 工时',
- 'price as 单价',
- 'remark as 备注',
- 'sys_id as 创建人员',
- 'sys_rq as 上报时间',
- ], $bhFields))
- ->where('sczl_rq', $params['sczl_rq'])
- ->order('id desc')
- ->select();
- if (empty($list)) {
- $this->error('未找到报工数据');
- }
- $employeeIds = [];
- foreach ($list as $row) {
- for ($i = 1; $i <= 30; $i++) {
- $bh = isset($row['bh' . $i]) ? trim($row['bh' . $i]) : '';
- if ($bh !== '' && $bh !== '000000') {
- $employeeIds[$bh] = true;
- }
- }
- }
- $employeeNames = [];
- if (!empty($employeeIds)) {
- $employees = \db('人事_基本资料')
- ->whereIn('员工编号', array_keys($employeeIds))
- ->field('员工编号, rtrim(员工姓名) as 姓名')
- ->select();
- foreach ($employees as $employee) {
- $employeeNames[$employee['员工编号']] = $employee['姓名'];
- }
- }
- foreach ($list as $key => $row) {
- for ($i = 1; $i <= 30; $i++) {
- $bh = isset($row['bh' . $i]) ? trim($row['bh' . $i]) : '';
- $list[$key]['员工姓名' . $i] = ($bh !== '' && $bh !== '000000')
- ? ($employeeNames[$bh] ?? '')
- : '';
- }
- }
- $this->success('获取成功', $list);
- }
- /**
- * 糊盒班组计时报工左侧日期菜单
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function getGluingReportClockDateMenu()
- {
- if (!$this->request->isGet()) {
- $this->error('请求错误');
- }
- $list = \db('糊盒班组计时')
- ->field('DISTINCT(sczl_rq) as sczl_rq')
- ->order('sczl_rq desc')
- ->select();
- $data = [];
- foreach ($list as $value) {
- $timestamp = strtotime($value['sczl_rq']);
- $month = date('Y-m', $timestamp);
- $date = date('Y-m-d', $timestamp);
- if (!isset($data[$month])) {
- $data[$month] = [];
- }
- $data[$month][] = $date;
- }
- $this->success('获取成功', $data);
- }
- /**
- * 解析糊盒班组计时报工员工编号
- * @param array $param
- * @return array
- */
- private function parseGluingReportClockEmployeeFields($param)
- {
- $employeeFields = [];
- for ($i = 1; $i <= 30; $i++) {
- $employeeFields['bh' . $i] = '';
- }
- $hasEmployee = false;
- for ($i = 1; $i <= 30; $i++) {
- $bh = isset($param['bh' . $i]) ? trim($param['bh' . $i]) : '';
- if ($bh !== '') {
- if (!preg_match('/^[a-zA-Z0-9]+$/', $bh)) {
- $this->error('员工' . $i . '编号只能由英文字母和数字组成');
- }
- $employeeFields['bh' . $i] = $bh;
- $hasEmployee = true;
- continue;
- }
- for ($j = $i + 1; $j <= 30; $j++) {
- $laterBh = isset($param['bh' . $j]) ? trim($param['bh' . $j]) : '';
- if ($laterBh !== '') {
- $this->error('员工编号不能间隔填报,请先填写员工' . $i . '编号');
- }
- }
- break;
- }
- if (!$hasEmployee) {
- $this->error('请至少填写一个员工编号');
- }
- return $employeeFields;
- }
- /**
- * 糊盒报工数据详情
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function getGluingReportClockDataDetail()
- {
- if (!$this->request->isGet()) {
- $this->error('请求错误');
- }
- $params = $this->request->param();
- if (!isset($params['id']) || empty($params['id'])) {
- $this->error('参数错误');
- }
- $detail = \db('糊盒班组计时')
- ->where('id', $params['id'])
- ->find();
- if (empty($detail)) {
- $this->error('未找到报工数据');
- }
- $employeeIds = [];
- for ($i = 1; $i <= 30; $i++) {
- $bh = isset($detail['bh' . $i]) ? trim($detail['bh' . $i]) : '';
- if ($bh !== '' && $bh !== '000000') {
- $employeeIds[$bh] = true;
- }
- }
- $employeeNames = [];
- if (!empty($employeeIds)) {
- $employees = \db('人事_基本资料')
- ->whereIn('员工编号', array_keys($employeeIds))
- ->field('员工编号, rtrim(员工姓名) as 姓名')
- ->select();
- foreach ($employees as $employee) {
- $employeeNames[$employee['员工编号']] = $employee['姓名'];
- }
- }
- $detail['class'] = [];
- for ($i = 1; $i <= 30; $i++) {
- $bh = isset($detail['bh' . $i]) ? trim($detail['bh' . $i]) : '';
- if ($bh !== '' && $bh !== '000000') {
- $detail['class'][] = [
- '编号' => $bh,
- '姓名' => $employeeNames[$bh] ?? '',
- ];
- }
- }
- $this->success('获取成功', $detail);
- }
- }
|