| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- use think\Request;
- class GluingReport extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- /**
- * 设备角色菜单
- * @return void
- */
- public function getGluingcoleTab()
- {
- if ($this->request->isGet() === false){
- $this->error('请求错误');
- }
- $data = [];
- $sist = ['印后糊盒车间','精品自动化车间','数字化车间'];
- $department = \db('设备_基本资料')
- ->distinct(true)
- ->where('使用部门','in',$sist)
- ->where('设备编组','<>','')
- ->order('设备编组')
- ->column('rtrim(使用部门) as 使用部门');
- if (empty($department)){
- $this->success('未获取到机台数据');
- }
- foreach ($department as $value){
- $machine = \db('设备_基本资料')
- ->where('使用部门',$value)
- ->field('rtrim(设备编号) as 设备编号,rtrim(设备名称) as 设备名称')
- ->order('设备编号')
- ->select();
- foreach ($machine as $k=>$v){
- $data[rtrim($value)][] = $v['设备编号'].'-->'.$v['设备名称'];
- }
- }
- $this->success('成功',$data);
- }
- /**
- * 设备角色列表
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getGluingcoleList()
- {
- if ($this->request->isGet() === false){
- $this->error('请求错误');
- }
- $param = $this->request->param();
- if (empty($param)){
- $this->error('参数错误');
- }
- $list = db('设备_糊盒班组角色')
- ->where('jtbh',$param['machine'])
- ->select();
- if (empty($list)){
- $this->error('未找到数据');
- }else{
- $this->success('成功',$list);
- }
- }
- /**
- * 设备角色资料详情
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getGluingcoleDetail()
- {
- if ($this->request->isGet() === false){
- $this->error('请求错误');
- }
- $param = $this->request->param();
- if (empty($param)){
- $this->error('参数错误');
- }
- $list = db('设备_糊盒班组角色')
- ->where('id',$param['id'])
- ->find();
- if (empty($list)){
- $this->error('未找到数据详情');
- }else{
- $this->success('成功',$list);
- }
- }
- /**
- * 新增机台班组角色
- * @return void
- */
- public function saveGluingcoleDetail(){
- if ($this->request->isPost() === false){
- $this->error('请求错误');
- }
- $param = Request::instance()->post();
- if (empty($param)){
- $this->error('参数错误');
- }
- $param['sys_rq'] = date('Y-m-d H:i:s',time());
- $res = db('设备_糊盒班组角色')->insert($param);
- if (empty($res)){
- $this->error('新增失败');
- }else{
- $this->success('新增成功');
- }
- }
- /**
- * 修改设备角色资料
- * @return void
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function getGluingcoleDetaiEdit()
- {
- if ($this->request->isPost() === false){
- $this->error('请求错误');
- }
- $param = Request::instance()->post();
- if (empty($param['id'])){
- $this->error('参数错误');
- }
- $id = intval($param['id']);
- $param['mod_rq'] = date('Y-m-d H:i:s',time());
- unset($param['id']);
- $res = db('设备_糊盒班组角色')
- ->where('id',$id)
- ->update($param);
- if ($res === false){
- $this->error('修改失败');
- }else{
- $this->success('修改成功');
- }
- }
- /**
- * 设备当前班组角色
- * @return void
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function UpdateGluingcoleStatus()
- {
- if ($this->request->isGet() === false){
- $this->error('请求错误');
- }
- $param = $this->request->param();
- if (empty($param['id']) || empty($param['jtbh'])){
- $this->error('参数错误');
- }
- $updateRes = db('设备_糊盒班组角色')
- ->where('jtbh',$param['jtbh'])
- ->update(['status' => 0]);
- $res = db('设备_糊盒班组角色')
- ->where('id',$param['id'])
- ->update(['status' => 1]);
- if ($updateRes === false || $res === false){
- $this->error('更新失败');
- }else{
- $this->success('更新成功');
- }
- }
- /**
- * 设备班组资料列表
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getGluingClassLList()
- {
- if ($this->request->isGet() === false){
- $this->error('请求错误');
- }
- $param = $this->request->param();
- if (empty($param)){
- $this->error('参数错误');
- }
- $list = db('设备_糊盒班组资料')
- ->where('jtbh',$param['machine'])
- ->select();
- foreach ($list as $k=>$v){
- for ($i = 1; $i <= 15; $i++){
- $bh = $v['bh'.$i];
- $list[$k]['name'.$i] = '';
- if (!empty($bh) && $bh !== ''){
- $name = \db('人事_基本资料')
- ->where('员工编号',$bh)
- ->value('员工姓名');
- $list[$k]['name'.$i] = $name;
- }
- }
- }
- if (empty($list)){
- $this->error('未找到数据');
- }else{
- $this->success('成功',$list);
- }
- }
- /**
- * 获取设备角色数据
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getGluingcole()
- {
- if ($this->request->isGet() === false){
- $this->error('请求错误');
- }
- $param = $this->request->param();
- if (empty($param)){
- $this->error('参数错误');
- }
- $list = db('设备_糊盒班组角色')
- ->where('jtbh',$param['machine'])
- ->where('status',1)
- ->find();
- if (empty($list)){
- $this->error('未找到角色资料');
- }else{
- $this->success('成功',$list);
- }
- }
- /**
- * 新增糊盒机台班组资料
- * @return void
- */
- public function AddGluingClass()
- {
- if ($this->request->isPost() === false){
- $this->error('请求错误');
- }
- $param = Request::instance()->post();
- if (empty($param)){
- $this->error('参数错误');
- }
- $param['sys_rq'] = date('Y-m-d H:i:s',time());
- $res = db('设备_糊盒班组资料')->insert($param);
- if ($res === false){
- $this->error('新增失败');
- }else{
- $this->success('新增成功');
- }
- }
- /**
- * 修改设备糊盒班组资料
- * @return void
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function UpdateGluingClass()
- {
- if ($this->request->isPost() === false){
- $this->error('请求错误');
- }
- $param = Request::instance()->post();
- if (empty($param['id'])){
- $this->error('参数错误');
- }
- $id = intval($param['id']);
- $param['mod_rq'] = date('Y-m-d H:i:s',time());
- unset($param['id']);
- $res = db('设备_糊盒班组资料')
- ->where('id',$id)
- ->update($param);
- if ($res === false){
- $this->error('修改失败');
- }else{
- $this->success('修改成功');
- }
- }
- /**
- * 糊盒机台报工
- * @return void
- * @throws \think\db\exception\BindParamException
- * @throws \think\exception\PDOException
- */
- public function addGluingReportData()
- {
- // 1. 请求方式验证(严格限制POST请求)
- if (!$this->request->isPost()) {
- $this->error('仅支持POST请求');
- }
- // 2. 获取并验证参数(重点适配含#字符的sczl_jtbh字段)
- $param = $this->request->post();
- // 核心修复:验证规则调整,明确允许#字符,兼容特殊字符
- $validate = new \think\Validate([
- 'sczl_jtbh' => 'require|regex:/^[\w#\x{4e00}-\x{9fa5}]+$/u', // 允许字母、数字、下划线、#、中文
- 'sczl_gdbh' => 'require',
- 'sczl_gxmc' => 'require',
- 'sys_id' => 'require',
- 'sczl_rq' => 'require|dateFormat:Y-m-d H:i:s',
- ], [
- 'sczl_jtbh.require' => '机组编号不能为空',
- 'sczl_jtbh.regex' => '机组编号仅支持字母、数字、下划线、#号和中文',
- 'sczl_gdbh.require' => '工单号不能为空',
- 'sczl_gxmc.require' => '工序名称不能为空',
- 'sys_id.require' => '系统ID不能为空',
- 'sczl_rq.require' => '报工日期不能为空',
- 'sczl_rq.dateFormat' => '报工日期格式错误(需为Y-m-d H:i:s)',
- ]);
- // 验证前处理:仅去除前后空格(不影响#字符)
- foreach (['sczl_jtbh', 'sczl_gdbh', 'sczl_gxmc', 'sys_id', 'sczl_rq'] as $field) {
- if (isset($param[$field]) && is_string($param[$field])) {
- $param[$field] = trim($param[$field]); // 只去空格,保留#等特殊字符
- }
- }
- // 执行验证
- if (!$validate->check($param)) {
- $this->error($validate->getError());
- }
- if ($this->isEmptyReportPrice($param['price'] ?? null)) {
- $resolvedPrice = $this->resolveReportPrice($param);
- if ($resolvedPrice !== null) {
- $param['price'] = $resolvedPrice;
- } else {
- unset($param['price']);
- }
- }
- // 3. 事务处理(确保数据一致性)
- $currentTime = date('Y-m-d H:i:s');
- $tableClass = '糊盒报工班组';
- $tableDevice = '设备_糊盒报工资料';
- // 4. 构建班组数据(保留#字符,无需额外处理)
- $classData = [
- 'sys_rq' => $currentTime,
- 'jtbh' => $param['sczl_jtbh'], // 直接保留含#的原始值
- 'gdbh' => $param['sczl_gdbh'],
- 'gxmc' => $param['sczl_gxmc'],
- 'sys_id' => $param['sys_id'],
- 'sczl_rq' => $param['sczl_rq'],
- 'sczl_bzdh' => $param['sczl_bzdh'],
- ];
- // 处理动态字段(1-30组bh/rate)
- $dynamicFields = [];
- for ($i = 1; $i <= 30; $i++) {
- $dynamicFields['bh' . $i] = isset($param['bh' . $i]) ? $param['bh' . $i] : '';
- $dynamicFields['rate' . $i] = isset($param['rate' . $i]) ? $param['rate' . $i] : '';
- }
- $classData = array_merge($classData, $dynamicFields);
- // 5. 保存班组数据(框架会自动处理特殊字符转义,避免SQL注入)
- $classId = Db::name($tableClass)->insertGetId($classData);
- if (empty($classId)) {
- throw new \Exception("{$tableClass}数据保存失败");
- }
- // 6. 构建设备数据(适配PHP7.2)
- $filteredParam = array_filter($param, function ($key) {
- return !preg_match('/^(bh|rate)\d+$/', $key);
- }, ARRAY_FILTER_USE_KEY);
- $deviceData = array_merge([
- 'role' => $classId,
- 'sys_rq' => $currentTime,
- ], $filteredParam);
- // 7. 保存设备数据(含#字符的字段会被自动转义,安全插入)
- $sql = Db::name($tableDevice)->fetchSql(true)->insert($deviceData);
- $saveResult = db()->query($sql);
- if ($saveResult !== false) {
- $this->success('报工提交成功');
- }else{
- $this->error('报工提交失败');
- }
- }
- /**
- * 糊盒机台报工数据菜单
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getTab()
- {
- if ($this->request->isGet() === false){
- $this->error('请求错误');
- }
- $data = [];
- $date = date('Y-m-d 00:00:00',time()-3888000);
- $sist = ['印后糊盒车间','精品自动化车间','精品手工车间','数字化车间'];
- $department = \db('设备_基本资料')
- ->distinct(true)
- ->where('使用部门','in',$sist)
- ->where('设备编组','<>','')
- ->order('设备编组')
- ->column('rtrim(使用部门) as 使用部门');
- if (empty($department)){
- $this->success('未获取到机台数据');
- }
- $list = \db('设备_糊盒报工资料')
- ->field([
- 'DISTINCT(sczl_rq)' => '时间',
- 'rtrim(sczl_jtbh)' => '机台编号'
- ])
- ->where('sczl_rq','>',$date)
- ->order('sczl_rq desc')
- ->select();
- foreach ($department as $value){
- $machine = \db('设备_基本资料')
- ->where('使用部门',$value)
- ->field('rtrim(设备编号) as 设备编号,rtrim(设备名称) as 设备名称')
- ->order('设备编号')
- ->select();
- foreach ($machine as $k=>$v){
- $data[rtrim($value)][$v['设备编号'].'-->'.$v['设备名称']] = [];
- foreach ($list as $kk=>$vv){
- if ($v['设备编号'] === $vv['机台编号']){
- array_push($data[rtrim($value)][$v['设备编号'].'-->'.$v['设备名称']],date('Y-m-d',strtotime($vv['时间'])));
- }
- }
- }
- }
- $this->success('成功',$data);
- }
- /**
- * 获取当前生产工单
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function GetProduction()
- {
- if (Request::instance()->isGet() == false) {
- $this->error('非法请求');
- }
- $params = Request::instance()->param();
- if (!isset($params['machine']) || empty($params['machine'])) {
- $this->error('参数错误');
- }
- $machine = $params['machine'];
- $machineCode = \db('dic_lzde')->where('适用机型',$machine)->value('sys_bh');
- $data = \db('设备_糊盒报工采集')->where('sczl_jtbh',$machine)->order('UID desc')->find();
- if (empty($data)){
- $this->success('未找到数据');
- }
- $list = [];
- $list['班组ID'] = $data['班组ID'];
- $row = [];
- if (!empty($data['sczl_gdbh'])){
- $endTime = \db('工单_工艺资料')
- ->where('Gy0_gdbh',$data['sczl_gdbh'])
- ->where('Gy0_yjno',$data['sczl_yjno'])
- ->where('Gy0_gxh',$data['sczl_gxh'])
- ->find();
- $list['工单编号'] = $data['sczl_gdbh'];
- if (!empty($endTime)){
- $list['印件号'] = $data['sczl_yjno'];
- $name = \db('工单_基本资料')->where('Gd_Gdbh',$data['sczl_gdbh'])->value('成品名称');
- $code = \db('工单_基本资料')->where('Gd_Gdbh',$data['sczl_gdbh'])->value('成品代号');
- $list['产品名称'] = rtrim($name);
- $list['产品代号'] = rtrim($code);
- $list['工序名称'] = $data['sczl_gxmc'];
- $list['联数'] = $endTime['Gy0_ls'];
- }
- $list['状态'] = rtrim($data['status']);
- $list['开工时间'] = $data['sczl_kgsj'];
- }else{
- $list['工单编号'] = '';
- $list['印件号'] = 0;
- $list['产品名称'] = '';
- $list['工序名称'] = '';
- $list['产品代号'] = '';
- $list['状态'] = '';
- $list['开工时间'] = '';
- $list['联数'] = '';
- }
- $list['班组编号'] = rtrim($data['sczl_bzbh']);
- $idList = explode(',',$data['班组ID']);
- foreach ($idList as $k=>$v){
- $class = \db('设备_糊盒班组资料')
- ->where('id',$v)
- ->field("role,rate,bh1,bh2,bh3,bh4,bh5,bh6,bh7,bh8,bh9,bh10,bh11,bh12,bh13,bh14,bh15")
- ->find();
- if (!empty($class)){
- for ($i=1;$i<16;$i++) {
- if ($class['bh' . $i] != '' && $class['bh' . $i] != '000000') {
- $name = \db('人事_基本资料')->where('员工编号', $class['bh' . $i])->field('rtrim(员工姓名) as 姓名')->find();
- $row[] = [
- '编号' => $class['bh' . $i],
- '姓名' => $name['姓名'],
- '比例' => $class['rate'],
- '角色' => $class['role'],
- ];
- }
- }
- $row = array_values($row);
- }
- }
- $list['班组成员'] = $row;
- $list['定额代号'] = $machineCode;
- $this->success('成功',$list);
- }
- /**
- * 设置当前工单和当前班组
- * @return void
- */
- public function setMachineTeam()
- {
- if (Request::instance()->isPost() == false) {
- $this->error('非法请求');
- }
- $params = Request::instance()->post();
- if (!isset($params['machine']) || empty($params['machine'] )) {
- $this->error('参数不能为空');
- }
- if (empty($params['team_id']) || empty($params['sczl_bzdh'])){
- $this->error('请先选择班组成员');
- }
- $machine = $params['machine'] . '#';
- $data = [];
- $data['status'] = $params['status'];
- $data['sczl_sj'] = date('Y-m-d H:i:s');
- $data['sczl_jtbh'] = $machine;
- $data['sczl_gdbh'] = empty($params['order']) ? '':$params['order'];
- $data['sczl_yjno'] = empty($params['yjno']) ? '':$params['yjno'];
- $data['sczl_gxh'] = empty($params['gy_name']) ? '' : (int)substr($params['gy_name'], 0, 2);
- $data['sczl_gxmc'] = empty($params['gy_name']) ? '' : $params['gy_name'];
- $data['sczl_bzbh'] = $params['sczl_bzdh'];
- $data['班组ID'] = $params['team_id'];
- // 获取当前时间
- $current_time = time();
- // 设置时间范围
- $start_time1 = strtotime(date('Y-m-d') . ' 08:30:00');
- $end_time1 = strtotime(date('Y-m-d') . ' 20:30:00');
- $end_time2 = strtotime(date('Y-m-d') . ' 24:00:00');
- $start_time3 = strtotime(date('Y-m-d', strtotime('+1 day')) . ' 08:30:00');
- $start_time4 = strtotime(date('Y-m-d', strtotime('+1 day')) . ' 00:00:00');
- // 判断当前时间属于哪个时间范围
- if ($current_time >= $start_time1 && $current_time <= $end_time1) {
- $data['sczl_kgsj'] = date('Y-m-d') . ' 08:30:00';
- } elseif ($current_time > $end_time1 && $current_time <= $end_time2) {
- $data['sczl_kgsj'] = date('Y-m-d') . ' 20:30:00';
- } elseif ($current_time > $end_time1 && $current_time <= $start_time3) {
- $data['sczl_kgsj'] = date('Y-m-d', strtotime('+1 day')) . ' 08:30:00';
- }elseif ($current_time > $start_time4 && $current_time <= $start_time3){
- $data['sczl_kgsj'] = date('Y-m-d') . ' 20:30:00';
- }
- if (!empty($params['order']) && !empty($params['yjno'])){
- $option['Gy0_gdbh'] = $params['order'];
- $option['Gy0_yjno'] = $params['yjno'];
- $option['Gy0_gxh'] = $data['sczl_gxh'];
- $data['任务ID'] = \db('工单_工艺资料')->where($option)->value('UniqId');
- }else{
- $data['任务ID'] = '';
- }
- $sql = \db('设备_糊盒报工采集')->fetchSql(true)->insert($data);
- $res = Db::query($sql);
- if ($res === false) {
- $this->error('设置失败');
- } else {
- $this->success('设置成功');
- }
- }
- /**
- * 糊盒设备运行跟踪数据页面显示
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getGluingReportDataList()
- {
- if ($this->request->isGet() === false){
- $this->error('请求错误');
- }
- $params = $this->request->param();
- if (empty($params['machine']) || empty($params['day'])){
- $this->error('参数错误');
- }
- $where = [
- 'a.sczl_jtbh' => $params['machine'],
- 'a.sczl_rq' => ['like',$params['day'].'%']
- ];
- $field = ['a.sczl_gdbh as 工单编号','a.sczl_gxmc as 工序名称','a.来料数量','a.sczl_cl as 产量',
- 'a.sczl_zcfp as 制程废品','a.startTime as 开始时间','a.endTime as 结束时间','a.sczl_ls as 联数','a.sczl_rq as 日期',
- 'a.sczl_dedh as 定额代号','a.工价系数','a.保养工时','a.装版工时','a.异常工时','a.异常类型','a.设备运行工时','a.role',
- 'a.sys_id as 创建人员','a.sys_rq as 上报时间','a.mod_rq as 修改时间','a.Uid','a.sczl_jtbh as 机台编号','a.price',
- 'b.Gd_cpdh as 产品代号','b.Gd_cpmc as 产品名称','a.sczl_bzdh as 班组','a.来料异常'];
- $list = \db('设备_糊盒报工资料')
- ->alias('a')
- ->join('工单_基本资料 b','a.sczl_gdbh = b.Gd_gdbh','left')
- ->field($field)
- ->where($where)
- ->group('a.Uid')
- ->select();
- if (empty($list)){
- $this->error('未找到报工数据');
- }
- foreach ($list as $key=>$value) {
- $list[$key]['class'] = [];
- $class = \db('糊盒报工班组')
- ->where('id', $value['role'])
- ->field("bh1,bh2,bh3,bh4,bh5,bh6,bh7,bh8,bh9,bh10,bh11,bh12,bh13,bh14,bh15,bh16,bh17,bh18,bh19,bh20,bh21,bh22,bh23,
- bh24,bh25,bh26,bh27,bh28,bh29,bh30,rate1,rate2,rate3,rate4,rate5,rate6,rate7,rate8,rate9,rate10,rate11,rate12,rate13,rate14,
- rate15,rate16,rate17,rate18,rate19,rate20,rate21,rate22,rate23,rate24,rate25,rate26,rate27,rate28,rate29,rate30")
- ->find();
- if (!empty($class)) {
- for ($i = 1; $i <=30; $i++) {
- if ($class['bh' . $i] != '' && $class['bh' . $i] != '000000') {
- $name = \db('人事_基本资料')->where('员工编号', $class['bh' . $i])->field('rtrim(员工姓名) as 姓名')->find();
- $list[$key]['class'][] = [
- '编号' => $class['bh' . $i],
- '姓名' => $name['姓名'],
- '比例' => $class['rate'.$i],
- ];
- }
- }
- $list[$key]['class'] = array_values($list[$key]['class']);
- }
- }
- $this->success('成功',$list);
- }
- /**
- * 报工数据详情显示
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getGluingReportDataDetail()
- {
- if ($this->request->isGet() === false){
- $this->error('请求错误');
- }
- $params = $this->request->param();
- if(!isset($params['id']) || empty($params['id'])){
- $this->error('参数错误');
- }
- $where = ['Uid'=>$params['id']];
- $field = ['a.sczl_gdbh as 工单编号','a.sczl_gxmc as 工序名称','a.来料数量','a.sczl_cl as 产量',
- 'a.sczl_zcfp as 制程废品','a.startTime as 开始时间','a.endTime as 结束时间','a.sczl_ls as 联数','a.sczl_rq as 日期',
- 'a.sczl_dedh as 定额代号','a.工价系数','a.保养工时','a.装版工时','a.异常工时','a.异常类型','a.设备运行工时','a.role',
- 'a.sys_id as 创建人员','a.sys_rq as 上报时间','a.mod_rq as 修改时间','a.Uid','a.sczl_jtbh as 机台编号','a.price',
- 'b.Gd_cpdh as 产品代号','b.Gd_cpmc as 产品名称','a.sczl_bzdh as 班组','a.来料异常'];
- $list = \db('设备_糊盒报工资料')
- ->alias('a')
- ->join('工单_基本资料 b','a.sczl_gdbh = b.Gd_gdbh','left')
- ->where($where)
- ->field($field)
- ->find();
- $list['class'] = [];
- $class = \db('糊盒报工班组')
- ->where('id', $list['role'])
- ->field("bh1,bh2,bh3,bh4,bh5,bh6,bh7,bh8,bh9,bh10,bh11,bh12,bh13,bh14,bh15,bh16,bh17,bh18,bh19,bh20,bh21,bh22,bh23,
- bh24,bh25,bh26,bh27,bh28,bh29,bh30,rate1,rate2,rate3,rate4,rate5,rate6,rate7,rate8,rate9,rate10,rate11,rate12,rate13,rate14,
- rate15,rate16,rate17,rate18,rate19,rate20,rate21,rate22,rate23,rate24,rate25,rate26,rate27,rate28,rate29,rate30")
- ->find();
- if (!empty($class)) {
- for ($i = 1; $i <=30; $i++) {
- if ($class['bh' . $i] != '' && $class['bh' . $i] != '000000') {
- $name = \db('人事_基本资料')->where('员工编号', $class['bh' . $i])->field('rtrim(员工姓名) as 姓名')->find();
- $list['class'][] = [
- '编号' => $class['bh' . $i],
- '姓名' => $name['姓名'],
- '比例' => $class['rate'.$i],
- ];
- }
- }
- $list['class'] = array_values($list['class']);
- }
- $this->success('成功',$list);
- }
- /**
- * 修改糊盒班组报工资料
- * @return void
- * @throws \think\Exception
- * @throws \think\db\exception\BindParamException
- * @throws \think\exception\PDOException
- */
- public function getGluingReportDetailUpdate()
- {
- if ($this->request->isPost() === false){
- $this->error('请求错误');
- }
- $param = Request::instance()->post();
- if(!isset($param['id']) || empty($param['id'])){
- $this->error('参数错误');
- }
- $id = $param['id'];
- $role = $param['role'];
- unset($param['id']);
- // 核心修复:验证规则调整,明确允许#字符,兼容特殊字符
- $validate = new \think\Validate([
- 'sczl_jtbh' => 'require|regex:/^[\w#\x{4e00}-\x{9fa5}]+$/u', // 允许字母、数字、下划线、#、中文
- 'sczl_gdbh' => 'require',
- 'sczl_gxmc' => 'require',
- 'sczl_rq' => 'require|dateFormat:Y-m-d H:i:s',
- ], [
- 'sczl_jtbh.require' => '机组编号不能为空',
- 'sczl_jtbh.regex' => '机组编号仅支持字母、数字、下划线、#号和中文',
- 'sczl_gdbh.require' => '工单号不能为空',
- 'sczl_gxmc.require' => '工序名称不能为空',
- 'sczl_rq.require' => '报工日期不能为空',
- 'sczl_rq.dateFormat' => '报工日期格式错误(需为Y-m-d H:i:s)',
- ]);
- // 验证前处理:仅去除前后空格(不影响#字符)
- foreach (['sczl_jtbh', 'sczl_gdbh', 'sczl_gxmc', 'sczl_rq'] as $field) {
- if (isset($param[$field]) && is_string($param[$field])) {
- $param[$field] = trim($param[$field]); // 只去空格,保留#等特殊字符
- }
- }
- // 执行验证
- if (!$validate->check($param)) {
- $this->error($validate->getError());
- }
- // 3. 事务处理(确保数据一致性)
- $currentTime = date('Y-m-d H:i:s');
- $tableClass = '糊盒报工班组';
- $tableDevice = '设备_糊盒报工资料';
- // 4. 构建班组数据(保留#字符,无需额外处理)
- $classData = [
- 'jtbh' => $param['sczl_jtbh'], // 直接保留含#的原始值
- 'gdbh' => $param['sczl_gdbh'],
- 'gxmc' => $param['sczl_gxmc'],
- 'sczl_rq' => $param['sczl_rq'],
- 'sczl_bzdh' => $param['sczl_bzdh'],
- ];
- // 处理动态字段(1-30组bh/rate)
- $dynamicFields = [];
- for ($i = 1; $i <= 30; $i++) {
- $dynamicFields['bh' . $i] = isset($param['bh' . $i]) ? $param['bh' . $i] : '';
- $dynamicFields['rate' . $i] = isset($param['rate' . $i]) ? $param['rate' . $i] : '';
- }
- $classData = array_merge($classData, $dynamicFields);
- // 5. 保存班组数据(框架会自动处理特殊字符转义,避免SQL注入)
- $classId = Db::name($tableClass)->where('id',$role)->update($classData);
- // 6. 构建设备数据(适配PHP7.2)
- $filteredParam = array_filter($param, function ($key) {
- return !preg_match('/^(bh|rate)\d+$/', $key);
- }, ARRAY_FILTER_USE_KEY);
- $deviceData = array_merge([
- 'mod_rq' => $currentTime,
- ], $filteredParam);
- // 7. 保存设备数据(含#字符的字段会被自动转义,安全插入)
- $sql = Db::name($tableDevice)->where('Uid',$id)->fetchSql(true)->update($deviceData);
- $saveResult = db()->query($sql);
- if ($saveResult !== false) {
- $this->success('修改成功');
- }else{
- $this->error('修改失败');
- }
- }
- /**
- * 修改报工数据班组
- * @return void
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function UpdateGluingReportClass()
- {
- if ($this->request->isPost() === false){
- $this->error('请求错误');
- }
- $params = Request::instance()->post();
- if(!isset($params['id']) || empty($params['id'])){
- $this->error('参数错误');
- }
- $id = $params['id'];
- $data['role'] = $params['role'];
- $res = \db('设备_糊盒报工资料')->where('Uid',$id)->update($data);
- if ($res === false) {
- $this->error('修改失败');
- }else{
- $this->success('修改成功');
- }
- }
- /**
- * 当班产量
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function GluingReportList()
- {
- if ($this->request->isGet() === false){
- $this->error('请求错误');
- }
- $params = $this->request->param();
- if (!isset($params['machine']) || empty($params['machine'])){
- $this->error('参数错误');
- }
- $machine = $params['machine'];
- $sczlTime = date('Y-m-d H:i:s',time());
- if ($sczlTime>date('Y-m-d 00:00:00') && $sczlTime<date('Y-m-d 08:30:00')){
- $time = date('Y-m-d 00:00:00',time()-86400);
- }else{
- $time = date('Y-m-d 00:00:00');
- }
- $where = [
- 'sczl_jtbh' => $machine,
- 'sczl_rq' => $time,
- ];
- $field = 'rtrim(sczl_gdbh) as 工单编号,rtrim(sczl_yjno) as yjno,rtrim(sczl_dedh) as dedh,rtrim(sczl_gxmc) as gxmc,
- rtrim(sczl_cl) as 产量,rtrim(sczl_zcfp) as 制程废品,rtrim(装版工时) as 装版工时,rtrim(保养工时) as 保养工时,
- rtrim(异常工时) as 异常工时,rtrim(设备运行工时) as 通电工时,startTime,endTime,Uid,rtrim(sczl_ls) as ls,来料数量,来料异常';
- $list = \db('设备_糊盒报工资料')->where($where)->field($field)->order('Uid desc')->select();
- if (!empty($list)){
- foreach ($list as $k=>$v){
- $name = \db('工单_印件资料')->where('Yj_Gdbh',$v['工单编号'])->where('yj_Yjno',$v['yjno'])->field('rtrim(yj_yjmc) as cpmc')->find();
- if ($v['yjno']<10){
- $list[$k]['yjno'] = '0'.$v['yjno'];
- }
- $list[$k]['印件及工序'] = $list[$k]['yjno'].'-'.$v['gxmc'];
- $list[$k]['生产时间段'] = substr($v['startTime'],5,5).' '.substr($v['startTime'],11,5).'<-->'.substr($v['endTime'],5,5).' '.substr($v['endTime'],11,5);
- $list[$k]['产品名称'] = $name['cpmc'];
- }
- }
- $this->success('成功',$list);
- }
- /**
- * 工单查询
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getWorkOrderList()
- {
- if ($this->request->isGet() === false){
- $this->error('请求错误');
- }
- $params = $this->request->param();
- if (!isset($params['search']) || empty($params['search'])){
- $this->error('参数错误');
- }
- $search = $params['search'];
- $list = \db('工单_基本资料')
- ->where('Gd_gdbh|Gd_cpmc','like','%'.$search.'%')
- ->field('Gd_gdbh as 工单编号,Gd_cpdh as 产品代号,Gd_cpmc as 产品名称')
- ->order('Gd_gdbh desc')
- ->select();
- if (empty($list)){
- $this->error('未找到数据');
- }else{
- $this->success('成功',$list);
- }
- }
- /**
- * 查询机台编号
- * @return void
- */
- public function getMachineList()
- {
- if ($this->request->isGet() === false){
- $this->error('请求错误');
- }
- $params = $this->request->param();
- if (!isset($params['search']) || empty($params['search'])){
- $this->error('参数错误');
- }
- $search = $params['search'];
- $list = \db('设备_基本资料')
- ->where('使用部门',$search)
- ->order('设备编号')
- ->column('设备名称','设备编号');
- if (empty($list)){
- $this->error('未找到数据');
- }else{
- $this->success('成功',$list);
- }
- }
- /**
- * 查询工艺名称
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function gitProcessList()
- {
- if ($this->request->isGet() === false){
- $this->error('请求错误');
- }
- $params = $this->request->param();
- if (!isset($params['search']) || empty($params['search'])){
- $this->error('参数错误');
- }
- $search = $params['search'];
- $list = \db('erp_常用字典')
- ->where('分类','糊盒工艺')
- ->where('名称','like','%'.$search.'%')
- ->field('名称')
- ->select();
- $data = [];
- if (empty($list)){
- $this->error('未找到工艺数据');
- }
- foreach ($list as $k=>$v){
- $name = explode('_',$v['名称']);
- $data[] = $name[2];
- }
- $this->success('成功',$data);
- }
- //最新糊盒班组查询
- public function classifyList()
- {
- // 1. 请求方法验证
- if (!$this->request->isGet()) {
- $this->error('请使用GET请求');
- }
- // 2. 参数验证
- $params = $this->request->param();
- // 验证必需参数
- $requiredParams = ['sczl_jtbh', 'sczl_bzdh'];
- foreach ($requiredParams as $param) {
- if (empty($params[$param] ?? null)) {
- $this->error("缺少必要参数: {$param}");
- }
- }
- $jtbh = trim($params['sczl_jtbh']);
- $bzdh = trim($params['sczl_bzdh']);
- // 3. 查询糊盒报工资料
- $role = \db('设备_糊盒报工资料')
- ->where('sczl_jtbh', $jtbh)
- ->where('sczl_bzdh', $bzdh)
- ->order('Uid', 'desc')
- ->value('role');
- if (empty($role)) {
- $this->error('未找到对应的报工班组信息');
- }
- // 4. 动态生成字段列表
- $fields = [];
- for ($i = 1; $i <= 30; $i++) {
- $fields[] = "bh{$i}";
- $fields[] = "rate{$i}";
- }
- // 5. 查询班组信息
- $class = \db('糊盒报工班组')
- ->where('id', $role)
- ->field($fields)
- ->find();
- if (empty($class)) {
- $this->error('班组信息不存在');
- }
- // 6. 处理班组成员信息
- $members = [];
- $employeeIds = [];
- // 收集所有有效的员工编号
- for ($i = 1; $i <= 30; $i++) {
- $bh = $class["bh{$i}"] ?? '';
- $rate = $class["rate{$i}"] ?? 0;
- // 验证员工编号是否有效
- $bh = trim($bh);
- if (!empty($bh) && $bh !== '000000') {
- $employeeIds[$bh] = [
- 'index' => $i,
- 'rate' => $rate
- ];
- }
- }
- // 如果有有效的员工编号,批量查询员工信息
- if (!empty($employeeIds)) {
- $employeeNames = [];
- $ids = array_keys($employeeIds);
- // 批量查询员工信息(减少数据库查询次数)
- $result = \db('人事_基本资料')
- ->whereIn('员工编号', $ids)
- ->field('员工编号, rtrim(员工姓名) as 姓名')
- ->select();
- foreach ($result as $item) {
- $employeeNames[$item['员工编号']] = $item['姓名'];
- }
- // 组装返回数据
- foreach ($employeeIds as $employeeId => $info) {
- $members[] = [
- '编号' => $employeeId,
- '姓名' => $employeeNames[$employeeId] ?? '未知',
- '比例' => $info['rate']
- ];
- }
- }
- $this->success('获取成功', array_values($members));
- }
- /**
- * 判断报工单价是否为空
- * @param mixed $price
- * @return bool
- */
- protected function isEmptyReportPrice($price)
- {
- return $price === null || $price === '';
- }
- /**
- * 报工时自动解析单价
- * @param array $param
- * @return float|null
- */
- protected function resolveReportPrice(array $param)
- {
- $machine = trim($param['sczl_jtbh']);
- $gxmc = trim($param['sczl_gxmc']);
- $count = $this->extractManualProcessCountSilent($param);
- $machinePrices = $this->collectMachinePriceValues($machine);
- if (!empty($machinePrices)) {
- return (float)$machinePrices[0];
- }
- $manualPrice = $this->resolveManualProcessPriceSilent($gxmc, $count);
- if ($manualPrice !== null) {
- return $manualPrice;
- }
- $aiService = new Aicompute();
- $rates = $aiService->fetchWorkshopRatesByMachine($machine);
- if (empty($rates)) {
- return null;
- }
- $productName = db('工单_基本资料')
- ->where('Gd_gdbh', trim($param['sczl_gdbh']))
- ->value('Gd_cpmc');
- $productName = $productName ? trim($productName) : '';
- if ($productName !== '') {
- $matched = $aiService->matchWorkshopRateByProductName($rates, $productName);
- if (!empty($matched)) {
- return (float)$matched['piece_price'];
- }
- }
- $aiPrice = $aiService->resolvePiecePriceByAi(trim($param['sczl_gdbh']), $machine);
- if ($aiPrice !== null) {
- return $aiPrice;
- }
- return (float)$rates[0]['piece_price'];
- }
- /**
- * 收集机台单价列表
- * @param string $machine
- * @return array
- */
- protected function collectMachinePriceValues($machine)
- {
- $list = db('machine_price')
- ->where('machine_id', $machine)
- ->field('price1,price2,price3,price4,price5,price6,price7,price8,price9,price10')
- ->select();
- $result = [];
- foreach ($list as $item) {
- foreach ($item as $value) {
- if ($value !== '' && $value !== null) {
- $result[] = $value;
- }
- }
- }
- return $result;
- }
- /**
- * 静默解析手工工序单价(报工场景,不中断请求)
- * @param string $gxmc
- * @param int|null $count
- * @return float|null
- */
- protected function resolveManualProcessPriceSilent($gxmc, $count)
- {
- if (strpos($gxmc, '割围条') !== false) {
- $row = db('machine_price')->where('machine_id', '手工割围条')->find();
- return $row ? (float)$row['price1'] : null;
- }
- if (strpos($gxmc, '打孔') !== false) {
- if ($count === null) {
- return null;
- }
- return $this->resolvePunchHolePrice($count);
- }
- if (strpos($gxmc, '拆片') !== false) {
- $rows = db('machine_price')->where('machine_id', '手工拆片')->select();
- if (empty($rows)) {
- return null;
- }
- return $this->resolveManualStripPriceSilent($rows, $gxmc, $count);
- }
- if (strpos($gxmc, '贴磁铁') !== false) {
- return $this->resolveManualCountPriceFromDb('手工贴磁铁', $count, false);
- }
- if (strpos($gxmc, '贴片') !== false) {
- return $this->resolveManualCountPriceFromDb('手工贴片', $count, false);
- }
- $row = db('machine_price')
- ->where('machine_id', $gxmc)
- ->whereOr('machine_name', 'like', '%' . $gxmc . '%')
- ->find();
- return $row ? (float)$row['price1'] : null;
- }
- /**
- * 静默提取孔数/拼数
- * @param array $params
- * @return int|null
- */
- protected function extractManualProcessCountSilent(array $params)
- {
- if (!isset($params['count']) || $params['count'] === '') {
- return null;
- }
- if (!is_numeric($params['count']) || (int)$params['count'] != $params['count'] || (int)$params['count'] <= 0) {
- return null;
- }
- return (int)$params['count'];
- }
- /**
- * 静默解析手工拆片单价
- * @param array $rows
- * @param string $gxmc
- * @param int|null $count
- * @return float|null
- */
- protected function resolveManualStripPriceSilent(array $rows, $gxmc, $count)
- {
- $gxmc = trim($gxmc);
- if ($gxmc === '手工拆片' || $gxmc === '拆片') {
- if ($count === null) {
- return null;
- }
- return $this->resolveDefaultStripPriceByCount($count);
- }
- if ($gxmc !== '') {
- $row = $this->matchManualPriceRow($rows, $gxmc);
- if (empty($row)) {
- return null;
- }
- return $this->calculateStripPriceByRowSilent($row, $count);
- }
- if ($count === null) {
- return null;
- }
- return $this->resolveDefaultStripPriceByCount($count);
- }
- /**
- * 静默计算拆片规格单价
- * @param array $row
- * @param int|null $count
- * @return float|null
- */
- protected function calculateStripPriceByRowSilent(array $row, $count)
- {
- $name = $row['machine_name'];
- $basePrice = (float)$row['price1'];
- if (strpos($name, '1-4拼(特种纸') !== false) {
- if ($count === null) {
- return null;
- }
- if ($count <= 4) {
- return $basePrice;
- }
- return floatval(number_format($basePrice + ($count - 4) * 0.001, 4, '.', ''));
- }
- if (strpos($name, '1-4拼(小盒二次拆片') !== false) {
- if ($count === null) {
- return null;
- }
- if ($count === 8) {
- return 0.01;
- }
- if ($count === 10) {
- return 0.012;
- }
- return $basePrice;
- }
- if (strpos($name, '5-6拼(小盒二次拆片') !== false) {
- return $basePrice;
- }
- if (strpos($name, '6拼及以上') !== false) {
- return $basePrice;
- }
- if (preg_match('/^(\d+)拼/u', $name) && $count === null) {
- return null;
- }
- return $basePrice;
- }
- /**
- * 获取机台报工单价数据
- * 参数:machine(机台编号)、gxmc(工序名称)、count(孔数/拼数,纯数字)
- * 优先按机台编号查询;查不到则按手工工序(gxmc + count)匹配单价
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getMachinePriceData()
- {
- if ($this->request->isGet() === false) {
- $this->error('请求错误');
- }
- $params = $this->request->param();
- if (!isset($params['machine']) || $params['machine'] === '') {
- $this->error('参数错误');
- }
- $machine = trim($params['machine']);
- // 1. 优先按机台编号查询
- $list = db('machine_price')
- ->where('machine_id', $machine)
- ->field('price1,price2,price3,price4,price5,price6,price7,price8,price9,price10')
- ->select();
- $result = [];
- foreach ($list as $item) {
- foreach ($item as $value) {
- if ($value !== '' && $value !== null) {
- $result[] = $value;
- }
- }
- }
- if (!empty($result)) {
- $this->success('获取成功', $result);
- }
- // 2. 机台编号查不到,按手工工序查询
- $gxmc = trim((string)($params['gxmc'] ?? ''));
- if ($gxmc === '') {
- $this->error('未找到机台单价,请传入工序名称');
- }
- $price = $this->resolveManualProcessPriceByGxmc($gxmc, $params);
- if ($price === null) {
- $this->error('未找到匹配的单价');
- }
- $this->success('获取成功', [$price]);
- }
- /**
- * 按工序名称解析手工工序单价
- * @param string $gxmc 工序名称
- * @param array $params count=孔数/拼数(纯数字)
- * @return float|null
- */
- protected function resolveManualProcessPriceByGxmc($gxmc, array $params)
- {
- $count = $this->extractManualProcessCount($params);
- if (strpos($gxmc, '割围条') !== false) {
- $row = db('machine_price')->where('machine_id', '手工割围条')->find();
- return $row ? (float)$row['price1'] : null;
- }
- if (strpos($gxmc, '打孔') !== false) {
- if ($count === null) {
- $this->error('请传入孔数(纯数字)');
- }
- return $this->resolvePunchHolePrice($count);
- }
- if (strpos($gxmc, '拆片') !== false) {
- $rows = db('machine_price')->where('machine_id', '手工拆片')->select();
- if (empty($rows)) {
- return null;
- }
- return $this->resolveManualStripPrice($rows, $gxmc, $count);
- }
- if (strpos($gxmc, '贴磁铁') !== false) {
- return $this->resolveManualCountPriceFromDb('手工贴磁铁', $count, true);
- }
- if (strpos($gxmc, '贴片') !== false) {
- return $this->resolveManualCountPriceFromDb('手工贴片', $count, true);
- }
- // 其他手工工序:按 machine_id 或 machine_name 匹配
- $row = db('machine_price')
- ->where('machine_id', $gxmc)
- ->whereOr('machine_name', 'like', '%' . $gxmc . '%')
- ->find();
- return $row ? (float)$row['price1'] : null;
- }
- /**
- * 按拼数查询手工工序单价(machine_name 存拼数)
- * @param string $machineId
- * @param int|null $count
- * @param bool $errorOnMissingCount
- * @return float|null
- */
- protected function resolveManualCountPriceFromDb($machineId, $count, $errorOnMissingCount = false)
- {
- if ($count === null) {
- if ($errorOnMissingCount) {
- $this->error('请传入拼数(纯数字)');
- }
- return null;
- }
- $row = db('machine_price')
- ->where('machine_id', $machineId)
- ->where('machine_name', (string)$count)
- ->find();
- return $row ? (float)$row['price1'] : null;
- }
- /**
- * 提取孔数/拼数(前端传入纯数字)
- * @param array $params
- * @return int|null
- */
- protected function extractManualProcessCount(array $params)
- {
- if (!isset($params['count']) || $params['count'] === '') {
- return null;
- }
- if (!is_numeric($params['count']) || (int)$params['count'] != $params['count'] || (int)$params['count'] <= 0) {
- $this->error('孔数/拼数必须为大于0的整数');
- }
- return (int)$params['count'];
- }
- /**
- * 手工打孔单价(按孔数区间)
- * @param int $holeCount
- * @return float
- */
- protected function resolvePunchHolePrice($holeCount)
- {
- $holeCount = (int)$holeCount;
- if ($holeCount === 1) {
- return 0.0055;
- }
- if ($holeCount >= 2 && $holeCount <= 4) {
- return 0.007;
- }
- if ($holeCount >= 5 && $holeCount <= 8) {
- return 0.012;
- }
- if ($holeCount >= 9 && $holeCount <= 12) {
- return 0.016;
- }
- if ($holeCount >= 13 && $holeCount <= 18) {
- return 0.025;
- }
- return floatval(number_format(0.025 + ($holeCount - 18) * 0.001, 4, '.', ''));
- }
- /**
- * 手工拆片单价(按规格名称 + 拼数)
- * @param array $rows
- * @param string $gxmc 工序规格名称(文字)
- * @param int|null $count 拼数(纯数字)
- * @return float|null
- */
- protected function resolveManualStripPrice(array $rows, $gxmc, $count)
- {
- $gxmc = trim($gxmc);
- // 通用拆片工序,仅按拼数数字匹配默认单价
- if ($gxmc === '手工拆片' || $gxmc === '拆片') {
- if ($count === null) {
- $this->error('请传入拼数(纯数字)');
- }
- return $this->resolveDefaultStripPriceByCount($count);
- }
- if ($gxmc !== '') {
- $row = $this->matchManualPriceRow($rows, $gxmc);
- if (empty($row)) {
- return null;
- }
- return $this->calculateStripPriceByRow($row, $count);
- }
- if ($count === null) {
- $this->error('请传入拼数(纯数字)');
- }
- return $this->resolveDefaultStripPriceByCount($count);
- }
- /**
- * 默认拆片单价(1.0厚以上灰白板,按拼数数字匹配)
- * @param int $count
- * @return float
- */
- protected function resolveDefaultStripPriceByCount($count)
- {
- $prices = [
- 1 => 0.008,
- 2 => 0.01,
- 3 => 0.013,
- 4 => 0.017,
- 5 => 0.022,
- 6 => 0.028,
- ];
- $pieceCount = min((int)$count, 6);
- return $prices[$pieceCount];
- }
- /**
- * 按 machine_name 匹配手工单价行
- * @param array $rows
- * @param string $gxmc
- * @return array|null
- */
- protected function matchManualPriceRow(array $rows, $gxmc)
- {
- $gxmc = trim($gxmc);
- foreach ($rows as $row) {
- if ($row['machine_name'] === $gxmc) {
- return $row;
- }
- }
- $best = null;
- $bestLen = 0;
- foreach ($rows as $row) {
- $name = $row['machine_name'];
- if (strpos($name, $gxmc) !== false || strpos($gxmc, $name) !== false) {
- $len = mb_strlen($name);
- if ($len > $bestLen) {
- $best = $row;
- $bestLen = $len;
- }
- }
- }
- return $best;
- }
- /**
- * 根据拆片规格行及拼数计算单价
- * @param array $row
- * @param int|null $count 拼数(纯数字)
- * @return float|null
- */
- protected function calculateStripPriceByRow(array $row, $count)
- {
- $name = $row['machine_name'];
- $basePrice = (float)$row['price1'];
- if (strpos($name, '1-4拼(特种纸') !== false) {
- if ($count === null) {
- $this->error('请传入拼数(纯数字)');
- }
- if ($count <= 4) {
- return $basePrice;
- }
- return floatval(number_format($basePrice + ($count - 4) * 0.001, 4, '.', ''));
- }
- if (strpos($name, '1-4拼(小盒二次拆片') !== false) {
- if ($count === null) {
- $this->error('请传入拼数(纯数字)');
- }
- if ($count === 8) {
- return 0.01;
- }
- if ($count === 10) {
- return 0.012;
- }
- return $basePrice;
- }
- if (strpos($name, '5-6拼(小盒二次拆片') !== false) {
- return $basePrice;
- }
- if (strpos($name, '6拼及以上') !== false) {
- return $basePrice;
- }
- if (preg_match('/^(\d+)拼/u', $name, $matches)) {
- if ($count === null) {
- $this->error('请传入拼数(纯数字)');
- }
- }
- return $basePrice;
- }
- /**
- * 报工数据删除
- * @return void
- * @throws \Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function deleteGluingReportData()
- {
- if ($this->request->isPost() === false){
- $this->error('请求错误');
- }
- $params = $this->request->param();
- if (!isset($params['id']) || empty($params['id'])){
- $this->error('参数错误');
- }
- $ids = explode(',', $params['id']);
- $roleIds = \db('设备_糊盒报工资料')
- ->where('Uid', 'in', $ids)
- ->column('role');
- $roleIds = array_filter(array_unique($roleIds));
- db()->startTrans();
- try {
- $res = \db('设备_糊盒报工资料')->where('Uid', 'in', $ids)->delete();
- if ($res === false) {
- throw new \Exception('删除报工数据失败');
- }
- if (!empty($roleIds)) {
- $classRes = \db('糊盒报工班组')->where('id', 'in', $roleIds)->delete();
- if ($classRes === false) {
- throw new \Exception('删除班组数据失败');
- }
- }
- db()->commit();
- } catch (\Exception $e) {
- db()->rollback();
- $this->error($e->getMessage());
- }
- $this->success('删除成功');
- }
- }
|