request->server("HTTP_SID")); } parent::_initialize(); } /** * 工单资料菜单列表 * * @ApiMethod (GET) * @return false|string * @throws \think\Exception */ public function DataList() { $where['j.Mod_rq'] = null; $allCustomers = \db('erp_客户供应商')->alias('e') ->field('e.编号 as 客户编号') ->select(); $customerData = []; foreach ($allCustomers as $customer) { $customerID = $customer['客户编号']; $customerData[$customerID] = ['计划中' => 0, '生产中' => 0]; } $data = \db('erp_客户供应商')->alias('e') ->join('工单_基本资料 j', 'e.编号 = j.客户编号', 'LEFT') ->field('e.编号 as 客户编号, j.订单编号, j.gd_statu') ->where($where) ->select(); foreach ($data as $row) { $customerID = $row['客户编号']; $status = $row['gd_statu']; if ($status == '1-计划中') { $customerData[$customerID]['计划中']++; } elseif ($status == '2-生产中') { $customerData[$customerID]['生产中']++; } } ksort($customerData); $output = []; foreach ($customerData as $customerID => $statusCounts) { $statusString = []; if ($statusCounts['计划中'] > 0) { $statusString[] = "计划中:{$statusCounts['计划中']}"; } if ($statusCounts['生产中'] > 0) { $statusString[] = "生产中:{$statusCounts['生产中']}"; } $output[] = "{$customerID}【" . implode(' ', $statusString) . "】"; } $this->success('成功', $output); } /** * 工单基本资料列表 * @ApiMethod (GET) * @param string $limit 查询长度 * @param string $Gd_khdh 客户代号 * @param string $startTime 接单日期开始时间 * @param string $endTime 接单日期结束时间 * @return \think\response\Json * @throws \think\exception\DbException */ public function WorkOrderList() { if ($this->request->isGet() === false) { $this->error('请求错误'); } $search = input('search'); $page = input('page'); $limit = input('limit'); $param = $this->request->param(); $where = []; if (!empty($search)) { $where['订单编号|生产款号|客户编号|款式|审核|Sys_id'] = ['like', '%' . $search . '%']; } $where['Mod_rq'] = null; $list = \db('工单_基本资料') ->where($where) ->order('订单编号 desc, Gd_statu desc, Sys_rq desc') ->limit(($page - 1) * $limit, $limit) ->select(); // 提取所有订单编号 $orderIds = array_column($list, '订单编号'); // 查询所有在“工单_相关附件”表中存在的订单编号 $relatedOrders = \db('工单_相关附件') ->whereIn('关联编号', $orderIds) ->whereIn('附件备注', '技术附件') ->column('关联编号'); // 遍历数据,判断每个订单编号是否在相关附件表中 foreach ($list as &$value) { if (in_array($value['订单编号'], $relatedOrders)) { $value['status'] = ''; // 有相关附件,status为空 } else { $value['status'] = '*'; // 没有相关附件,标记为新订单 } } $this->success('成功', ['data' => $list, 'total' => count($list)]); } /** * 编辑页面展示 * @ApiMethod (GET) * @param string $workOrder 工单编号 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ // public function DataCorrection() // { // if ($this->request->isGet() === false){ // $this->error('请求错误'); // } // $workOrder = input('Gd_gdbh'); // if (empty($workOrder)){ // $this->error('参数错误'); // } // $field = 'rtrim(Gd_lx) as 重点工单,rtrim(Gd_gdbh) as 工单编号,rtrim(Gd_生产分类) as 生产类型,rtrim(Gd_khdh) as 客户代号,rtrim(Gd_客户名称) as 客户名称, // rtrim(Gd_cpdh) as 印件代号,rtrim(Gd_cpmc) as 印件名称,rtrim(成品代号) as 产品代号,rtrim(成品名称) as 产品名称,rtrim(接单日期) as 开单日期,rtrim(订单数量) as 订单数量,rtrim(交货日期) as 交货日期, // rtrim(投料率) as 投料率,rtrim(实际投料) as 万小张,rtrim(计量单位) as 单位,rtrim(投料大箱) as 投料大箱,rtrim(排产时库存) as 库存大箱,rtrim(警语版面) as 警语版面, // rtrim(销售订单号) as 销售订单号,rtrim(产品版本号) as 版本号,rtrim(客户ERP编码) as 客户ERP编码,rtrim(码源数量) as 码源数量,rtrim(进程备注) as 进程备注,rtrim(Gd_desc) as 备注,rtrim(Uniqid) as Uniqid,rtrim(计划投料) as 平张投料'; // $list = \db('工单_基本资料')->where('Gd_gdbh',$workOrder)->field($field)->find(); // if (empty($list)){ // $this->error('未找到该工单信息'); // } // $printData = \db('工单_印件资料') // ->where('Yj_Gdbh',$workOrder) // ->field('rtrim(yj_yjmc) as 印件名称,rtrim(yj_Yjdh) as 印件代号,rtrim(Uniqid) as id') // ->cache(true,84600) // ->find(); // if (empty($printData)){ // $list['印件名称'] = ''; // $list['印件代号'] = ''; // $list['印件ID'] = ''; // }else{ // $list['印件名称'] = $printData['印件名称']; // $list['印件代号'] = $printData['印件代号']; // $list['印件ID'] = $printData['id']; // } // $this->success('成功',$list); // } /** * 工单资料修改 * @ApiMethod (POST) * @param void * @return void * @throws \think\Exception * @throws \think\exception\PDOException */ // public function WorkOrderEdit() // { // if (Request::instance()->isPost() === false){ // $this->error('请求错误'); // } // $param = Request::instance()->post(); // if (empty($param) || isset($param['Uniqid']) === false){ // $this->error('参数错误'); // } // $row = [ // 'Gd_lx' => isset($param['lx'])?$param['lx']:'', // '开单日期' => isset($param['kdrq'])?$param['kdrq']:'', // 'Gd_gdbh' => isset($param['gdbh'])?$param['gdbh']:'', // 'Gd_生产分类' => isset($param['scfl'])?$param['scfl']:'', // 'Gd_客户代号' => isset($param['khdh'])?$param['khdh']:'', // 'Gd_客户名称' => isset($param['khmc'])?$param['khmc']:'', // '成品代号' => isset($param['cpdh'])?$param['cpdh']:'', // '成品名称' => isset($param['cpmc'])?$param['cpmc']:'', // '订单数量' => isset($param['ddsl'])?$param['ddsl']:'', // '交货日期' => isset($param['jhrq'])?$param['jhrq']:'', // '投料率' => isset($param['tll'])?$param['tll']:'', // '计划投料' => isset($param['jhtl'])?$param['jhtl']:'', // '实际投料' => isset($param['sjtl'])?$param['sjtl']:'', // '计量单位' => isset($param['jldw'])?$param['jldw']:'', // '投料大箱' => isset($param['tldx'])?$param['tldx']:'', // '销售订单号' => isset($param['xsddh'])?$param['xsddh']:'', // '警语版面' => isset($param['jymb'])?$param['jymb']:'', // '产品版本号' => isset($param['bbh'])?$param['bbh']:'', // '客户ERP编码' => isset($param['erp'])?$param['erp']:'', // '码源数量' => isset($param['ymsl'])?$param['ymsl']:'', // '进程备注' => isset($param['jcbz'])?$param['jcbz']:'', // 'Gd_desc' => isset($param['desc'])?$param['desc']:'', // '排产时库存' => isset($param['kc'])?$param['kc']:'', // '平均合格率' => isset($param['avg'])?$param['avg']:'', // ]; // //获取印件资料 // $printCode = \db('工单_印件资料') // ->where('Uniqid',$param['printID']) // ->value('yj_Yjno'); // if (!empty($printCode)){ // if ((int)$param['jhtl'] >0){ // //修改工单工艺计划产量 // $result = $this->PlannedProcessYield($param['gdbh'],$printCode,0,$param['jhtl']); // if ($result === false){ // $this->success('分配工序计划产量失败'); // } // } // // //修改印件信息 // $printSql = \db('工单_印件资料') // ->where('Uniqid',$param['printID']) // ->fetchSql(true) // ->update(['Yj_Gdbh'=>$param['gdbh'],'yj_yjmc'=>$param['yjmc'],'yj_Yjdh'=>$param['yjdh'],'yj_平张投料'=>$param['jhtl']]); // $printRes = Db::query($printSql); // } // //修改工单信息 // $sql = \db('工单_基本资料')->where('Uniqid',$param['Uniqid'])->fetchSql(true)->update($row); // $res = Db::query($sql); // //修改订单数量该工单下的订单数量 // //1. 查询该id下的工单编号, 防止其手动修改工单编号导致条件错误 // $gdbh = \db('工单_基本资料')->where('Uniqid',$param['Uniqid'])->value('Gd_gdbh'); // //2. 根据查出的工单编号修改订单数量 // $sql = \db('工单_基本资料')->where('Gd_gdbh',$param['gdbh'])->fetchSql(true)->update(['订单数量'=>$row['订单数量']]); // $res1 = Db::query($sql); // if ($res !== false && $res1 !== false){ // $this->success('成功'); // }else{ // $this->error('失败'); // } // } /** * U8投料试算 * @ApiMethod (GET) * @param string $processCode 产品编号 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ // public function U8Trial() // { // if ($this->request->isGet() === false) // { // $this->error('请求错误'); // } // $productCode = input('productCode'); // if (empty($productCode)){ // $this->error('参数错误'); // } // $field = 'rtrim(Gy0_cpdh) as 产品代号,rtrim(Gy0_yjno) as 印件号,rtrim(Gy0_gxh) as 工序号,rtrim(gy0_gxmc) as gxmc,rtrim(Add_gxmc) as add_gxmc, // rtrim(Gy0_Ms) as 墨色数,rtrim(Gy0_shdh) as 损耗代号,rtrim(损耗系数) as 损耗系数,rtrim(Gy0_ls) as 加工联数'; // $list = \db('产品_工艺资料')->where('Gy0_cpdh',$productCode)->field($field)->select(); // if (empty($list)){ // $this->success('未找到该产品工序'); // } // foreach ($list as $key=>$value){ // $data = \db('dic_lzsh')->where('sys_bh',$value['损耗代号'])->field('rtrim(sys_rate0) as rate0,rtrim(sys_rate1) as rate1')->cache(true)->find(); // $list[$key]['调机损耗'] = isset($data['rate0'])?$data['rate0']:''; // $list[$key]['运行损耗率'] = isset($data['rate1'])?$data['rate1']:''; // if ($value['add_gxmc'] !== ''){ // $list[$key]['工序名称'] = $value['gxmc'].'【'.$value['add_gxmc'].'】'; // }else{ // $list[$key]['工序名称'] = $value['gxmc']; // } // unset($list[$key]['gxmc'],$list[$key]['add_gxmc']); // } // $this->success('成功',$list); // } /** * 月度客户订单汇总 */ public function ProductInformation(){ if ($this->request->isGet() === false) { $this->error('请求错误'); } $where['j.gd_statu'] = '2-生产中'; $where['j.Mod_rq'] = null; $data = \db('工单_基本资料')->alias('j') ->field('j.客户编号, REPLACE(DATE_FORMAT(j.Sys_rq, "%Y-%m"), "-", "") as 年月, GROUP_CONCAT(DISTINCT j.订单编号) as 订单编号') ->where($where) ->order('j.客户编号 asc') ->group('j.客户编号, 年月') ->select(); $result = []; foreach ($data as $item) { $orderlist = count(explode(',', $item['订单编号'])); $result[$item['年月']][] = [$item['客户编号'] . '【生产中:' . $orderlist . '】', ]; } $this->success('请求成功', ['data' => $result]); } /** * 引用产品资料->复制产品工艺资料 * @ApiMethod (POST) * @param string $oldWorkOrder 被复制工单编号 * @param string $newWorkOrder 复制工单编号 * @return void * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ // public function ProductInformationEdit() // { // if (Request::instance()->isPost() === false) // { // $this->error('请求错误'); // } // $oldWorkOrder = input('oldWorkOrder'); // $newWorkOrder = input('newWorkOrder'); // if (empty($oldWorkOrder) || empty($newWorkOrder)) // { // $this->error('参数错误'); // } // $lastId = \db('工单_工艺资料')->order('UniqId desc')->value('UniqId'); // if ($lastId<10000000){ // $lastId = 10000000; // }else{ // $lastId = $lastId + 1; // } // //获取原工单工艺资料 // $oldProcessData = \db('工单_工艺资料')->where('Gy0_gdbh',$newWorkOrder)->select(); // $ProsessUniqId = \db('工单_工艺资料')->field('UniqId')->order('UniqId desc')->find(); // foreach ($oldProcessData as $k=>$v){ // $oldProcessData[$k]['Gy0_gdbh'] = $oldWorkOrder; // $oldProcessData[$k]['Sys_id'] = ''; // $oldProcessData[$k]['Gy0_sj1'] = '1900-01-01 00:00:00'; // $oldProcessData[$k]['Gy0_sj2'] = '1900-01-01 00:00:00'; // $oldProcessData[$k]['UniqId'] = $lastId + $k; // $oldProcessData[$k]['PD_WG'] = '1900-01-01 00:00:00'; // } // if (\db('工单_工艺资料')->where('Gy0_gdbh',$oldWorkOrder)->find()){ // \db('工单_工艺资料')->where('Gy0_gdbh',$oldWorkOrder)->delete(); // } // //获取原工单印件资料 // $lastUniqId = \db('工单_印件资料')->order('UniqId desc')->value('UniqId'); // if ($lastUniqId<1000000){ // $lastUniqId = 1000000; // }else{ // $lastUniqId = $lastUniqId + 1; // } // $oldPrintData = \db('工单_印件资料')->where('Yj_Gdbh',$newWorkOrder)->select(); // $PrintUniqId = \db('工单_印件资料')->field('Uniqid')->order('Uniqid desc')->find(); // foreach ($oldPrintData as $k=>$v){ // $oldPrintData[$k]['Yj_Gdbh'] = $oldWorkOrder; // $oldPrintData[$k]['Sys_id'] = ''; // $oldPrintData[$k]['Uniqid'] = $lastUniqId +$k +1; // } // if (\db('工单_印件资料')->where('Yj_Gdbh',$oldWorkOrder)->find()){ // \db('工单_印件资料')->where('Yj_Gdbh',$oldWorkOrder)->delete(); // } // //复制印件、工艺资料 // $ProcessSQL = \db('工单_工艺资料')->fetchSql(true)->insertAll($oldProcessData); // $ProcessRes = Db::query($ProcessSQL); // $PrintSQL = \db('工单_印件资料')->fetchSql(true)->insertAll($oldPrintData); // $PrintRes = Db::query($PrintSQL); // if ($ProcessRes !== false && $PrintRes !== false){ // $this->success('成功'); // }else{ // $this->error('失败'); // } // } /** * 工艺流程调成->获取当前工单工艺资料 * @ApiMethod (GET) * @param string $workorder 当前工单编号 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ // public function ProcessFlow() // { // if ($this->request->isGet() === false){ // $this->error('请求错误'); // } // $workOrder = input('workOrder'); // if (empty($workOrder)){ // $this->error('参数错误'); // } // $list = \db('工单_基本资料')->where('Gd_gdbh',$workOrder)->field('rtrim(成品代号) as 成品编号,rtrim(成品名称) as 成品名称')->find(); // if (empty($list)){ // $this->success('未找到工单信息'); // } // $filed = 'rtrim(Gy0_方案) as 方案,rtrim(Gy0_yjno) as 印件号,rtrim(Gy0_gxh) as 工序号,rtrim(Gy0_gxmc) as gxmc,rtrim(Add_gxmc) as add_gxmc, // rtrim(工价系数) as 工价系数,rtrim(损耗系数) as 损耗系数,rtrim(Gy0_ks) as ks,rtrim(Gy0_ls) as ls,rtrim(工序备注) as 备注,rtrim(Gy0_SITE) as 车间, // rtrim(Gy0_sbbh) as 设备编号,rtrim(Gy0_sbmc) as 设备名称,rtrim(Sys_id) as 建档用户,rtrim(Sys_rq) as 建档时间,rtrim(Mod_rq) as 更新时间,rtrim(UniqId) as UniqId'; // $process = \db('工单_工艺资料')->where('Gy0_gdbh',$workOrder)->cache(true,84600)->field($filed)->select(); // if (empty($process)){ // $this->error('未找到该工单工艺资料'); // } // foreach ($process as $key=>$value){ // if (isset($value['add_gxmc'])){ // $process[$key]['工序名称'] = $value['gxmc'] . '【'.$value['add_gxmc'].'】'; // }else{ // $process[$key]['工序名称'] = $value['gxmc']; // } // $process[$key]['工价系数'] = (float)$value['工价系数']; // $process[$key]['损耗系数'] = (float)$value['损耗系数']; // $process[$key]['开数*联数'] = $value['ks'].'*'.$value['ls']; // } // $list['process'] = $process; // $this->success('成功',$list); // } /** * 参照工单列表获取 * @ApiMethod (GET) * @param string $workOrder 工单编号 * @param string $productCode 产品代号 * @return void */ // public function ReferenceWorkOrder() // { // if ($this->request->isGet() === false){ // $this->error('请求错误'); // } // $productCode = input('productCode'); // $workOrder = input('workOrder'); // if (empty($productCode) || empty($workOrder)){ // $this->error('参数错误'); // } // $list = \db('工单_基本资料')->where('成品代号',$productCode)->distinct(true)->where('Gd_gdbh','<>',$workOrder)->column('Gd_gdbh'); // if (empty($list)){ // $this->success('未获取该产品其他工单信息'); // } // $this->success('成功',$list); // } /** * 工艺资料复制 * @ApiMethod (POST) * @param string $oldWorkOrder 被复制工单编号 * @param string $newWorkOrder 复制工单编号 * @return void * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException * */ // public function ProcessCopy() // { // if (Request::instance()->isPost() === false){ // $this->error('请求错误'); // } // $param = Request::instance()->post(); // if (empty($param)){ // $this->error('参数错误'); // } // $UniqId = []; // $lastId = \db('工单_工艺资料')->field('rtrim(UniqId) as id')->order('UniqId desc')->find(); // if ($lastId['id']>10000000){ // $lastUniqId = $lastId['id']; // }else{ // $lastUniqId = 10000000; // } // foreach ($param as $key=>$value){ // $UniqId[$key] = $value['UniqId']; // } // $processList = \db('工单_工艺资料')->where('UniqId','in',$UniqId)->select(); // if (empty($processList)){ // $this->success('未找到工艺资料'); // } // $data = []; // foreach ($param as $key=>$value){ // foreach ($processList as $k=>$v){ // if ($value['UniqId'] = $v['UniqId']){ // $data[$key] = $v; // $data[$key]['Gy0_gxh'] = $value['gxh']; // $data[$key]['Gy0_gdbh'] = $value['workOrder']; // $data[$key]['UniqId'] = $lastUniqId + $key + 1; // } // } // } // $res = \db('工单_工艺资料')->where(['Gy0_gdbh'=>$param[0]['workOrder']])->delete(); // if ($res !== false){ // $result = \db('工单_工艺资料')->insertAll($data); // if ($result !== false){ // $this->success('成功'); // }else{ // $this->error('失败'); // } // }else{ // $this->error('失败'); // } // } /** * U8工单列表 * @ApiMethod (GET) * @param void * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ // public function U8workOrder() // { // if ($this->request->isGet() === false){ // $this->error('请求错误'); // } // $param = $this->request->param(); // if (empty($param)){ // $this->error('参数错误'); // } // $filed = 'rtrim(Gd_gdbh) as 工单编号,rtrim(行号) as 行号,rtrim(Gd_客户代号) as 客户代号,rtrim(Gd_客户名称) as 客户名称, // rtrim(成品代号) as 成品代号,rtrim(成品名称) as 成品名称,rtrim(Mod_rq) as 获取日期,rtrim(Uniqid) as 序号'; // $list = \db('工单_基本资料')->where('Gd_gdbh',$param['workOrder'])->field($filed)->select(); // if (empty($list)){ // $this->success('未找到工单'); // } // $this->success('成功',$list); // } /** * U8工单资料删除 * @param string $workOrder 工单编号 * @return void * @throws \think\Exception * @throws \think\exception\PDOException */ public function orderDataDel() { if($this->request->isGet() === false){ $this->error('请求错误'); } $workOrder = input('Uniqid'); if (empty($workOrder)){ $this->error('参数错误'); } $order = \db('工单_基本资料')->where('UniqId',$workOrder)->find(); \db()->startTrans(); try { \db('工单_印件资料')->where('订单编号',$order['订单编号'])->update(['Mod_rq'=>date('Y-m-d H:i:s')]); \db('工单_工艺资料')->where('订单编号',$order['订单编号'])->update(['Mod_rq'=>date('Y-m-d H:i:s')]); $res = \db('工单_基本资料')->where('UniqId',$workOrder)->update(['Mod_rq'=>date('Y-m-d H:i:s')]); \db()->commit(); } catch (\Exception $e){ \db()->rollback(); } if ($res !== false){ $this->success('成功'); }else{ $this->error('失败'); } } /** * */ // public function TestCoefficient() // { // } /** * */ // public function TestCoefficientEdit() // { // } /** * */ // public function AccountingParameter() // { // } /** * */ // public function AccountingParameterEdit() // { // } /** * 印件资料修改 * @ApiMethod (POST) * @param void * @return void * @throws \think\Exception * @throws \think\exception\PDOException */ public function PrintedEdit() { if (Request::instance()->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param) || isset($param['Uniqid']) === false){ $this->error('参数错误'); } $param['Yj_Gdbh'] = \db('工单_印件资料')->where('Uniqid',$param['Uniqid'])->value('Yj_Gdbh'); $data = [ 'Yj_Gdbh' =>$param['Yj_Gdbh'], 'yj_Yjno' =>$param['yjno'], 'yj_Yjdh' =>$param['yjdh'], 'yj_yjmc' =>$param['yjmc'], 'yj_zzdh' =>$param['zzdh'], 'yj_zzdh1' =>$param['zzdh1'], 'yj_zzdh2' =>$param['zzdh2'], 'yj_zzdh3' =>$param['zzdh3'], 'yj_zzdh4' =>$param['zzdh4'], 'yj_zzmc' =>$param['zzmc'], 'yj_zzmc1' =>$param['zzmc1'], 'yj_zzmc2' =>$param['zzmc2'], 'yj_zzmc3' =>$param['zzmc3'], 'yj_zzmc4' =>$param['zzmc4'], 'yj_tlgg' =>$param['tlgg'], 'yj_klgg' =>$param['klgg'], 'Yj_核算规格' =>$param['hsgg'], 'yj_成品数量' =>$param['cpsl'], 'yj_平张投料' =>$param['pztl'], 'yj_ks' =>$param['ks'], 'yj_ls' =>$param['ls'], 'yj_desc' =>$param['desc'], ]; $UniqId = $param['Uniqid']; $sql = \db('工单_印件资料')->where('Uniqid',$UniqId)->fetchSql(true)->update($data); $res = Db::query($sql); $process = \db('工单_工艺资料') ->where('Gy0_gdbh',$data['Yj_Gdbh']) ->where('Gy0_yjno',$data['yj_Yjno']) ->select(); if ((int)$data['yj_平张投料'] > 0 && !empty($process)){ //重新分配工序计划产量 $result = $this->PlannedProcessYield($data['Yj_Gdbh'],$data['yj_Yjno'],0,$data['yj_平张投料']); if ($result === false){ $this->success('修改工序产量失败'); } } if ($res !== false){ $this->success('成功'); }else{ $this->error('失败'); } } /** * 工艺资料修改 * @ApiMethod (POST) */ public function ProcessDetailEdit() { if (Request::instance()->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param) || isset($param['UniqId']) === false){ $this->error('参数错误'); } if (empty($param['Gy0_shdh'])){ $rate['rate0'] = 0; $rate['rate1'] = 0; }else{ $rate = \db('dic_lzsh')->where('sys_bh',$param['Gy0_shdh'])->field('rtrim(sys_rate0) as rate0,rtrim(sys_rate1) as rate1')->find(); } $param['Gy0_Rate0']= isset($rate['rate0'])?$rate['rate0']:0; $param['Gy0_Rate1'] = isset($rate['rate1'])?$rate['rate1']:0; $param['Mod_rq'] = date('Y-m-d H:i:s',time()); $UniqId = $param['UniqId']; unset($param['UniqId']); //修改工艺资料 $sql = \db('工单_工艺资料')->where('UniqId',$UniqId)->fetchSql(true)->update($param); $res = Db::query($sql); //获取工艺资料数据 $list = \db('工单_工艺资料')->where('UniqId',$UniqId)->field('Gy0_yjno,Gy0_gxh,rtrim(Gy0_计划接货数) as 计划接货数')->find(); // //修改工单状态 // $status = \db('工单_基本资料')->where('Gd_gdbh',$param['Gy0_gdbh'])->field('rtrim(gd_statu) as status')->find(); // if ($status['status'] !== '2-生产中'){ // $statusSql = \db('工单_基本资料')->where('Gd_gdbh',$param['Gy0_gdbh'])->fetchSql(true)->update(['gd_statu'=>'2-生产中']); // Db::query($statusSql); // } //重新分配工序计划产量 if ((int)$list['计划接货数'] > 0){ $result = $this->PlannedProcessYield($param['Gy0_gdbh'],$list['Gy0_yjno'],$list['Gy0_gxh'],$list['计划接货数']); } if ($res !== false){ $this->success('成功'); }else{ $this->error('失败'); } } /** * */ // public function MachineList() // { // } /** * */ // public function PrintCodeList() // { // } /** * */ // public function ProcessCodeList() // { // } /** * */ // public function PrintJobOrder() // { // } /** * */ // public function workOrderDetailGet($workOrder,$yjno) // { // } /** */ // public function MaterielDetailGet($workOrder) // { // } /** */ // public function PrintDetailGet($workOrder,$yjno,$gxh) // { // } /** * */ // public function capacityList(){ // } /** * 新增工单->添加工单 * @ApiMethod (POST) * @param */ public function WorkOrderAdd() { // var_dump(1);exit; if (Request::instance()->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param)){ $this->error('参数错误'); } $param['Sys_rq'] = date('Y-m-d H:i:s'); $param['gd_statu'] = '1-计划中'; $prefix = substr($param['订单编号'], 0, 2); // e.g., "DC" $maxOrder = \db('工单_基本资料') ->where('订单编号', 'like', "{$prefix}%") ->order('订单编号', 'desc') ->limit(1) ->value('订单编号'); if ($maxOrder) { $numericPart = substr($maxOrder, 2); $newNumericPart = str_pad((int)$numericPart + 1, strlen($numericPart), '0', STR_PAD_LEFT); $param['订单编号'] = $prefix . $newNumericPart; } else { $param['订单编号'] = $param['订单编号']; } $sql= \db('工单_基本资料')->fetchSql(true)->insert($param); $res = \db()->query($sql); if ($res !== false){ $this->success('成功'); }else{ $this->error('失败'); } } /** * */ // public function ClientList(){ // } /** * */ // public function ProductCodeList(){ // } /** * 新增印件资料->印件资料添加 * @return void * @throws \think\db\exception\BindParamException * @throws \think\exception\PDOException */ public function PrintDetailAdd() { if (Request::instance()->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param)){ $this->error('参数错误'); } $param['Sys_rq'] = date('Y-m-d H:i:s',time()); $process = [ ['1','仓库出库'],['2','裁剪'],['3','车缝'],['4','后道收样'],['5','大烫'],['6','总检'],['7','包装'] ]; $processDetail = []; foreach ($process as $key=>$value){ $total = null; if ($key !== 0){ $total = $param['zdtotal']; } $processDetail[$key] = [ '订单编号' => $param['订单编号'], '子订单编号' => $param['子订单编号'], '款号' => $param['款号'], '颜色' => $param['颜色'], '颜色备注' => $param['颜色备注'], '工序编号' => $value[0], '工序名称' => $value[1], '计划产量' => $total, 'Sys_id' => $param['Sys_id'], 'Sys_rq' => $param['Sys_rq'] ]; } //开启事务 db()->startTrans(); try{ $priSql = \db('工单_印件资料')->fetchSql(true)->insert($param); $priRes = \db()->query($priSql); $proSql = \db('工单_工艺资料')->fetchSql(true)->insertAll($processDetail); $proRes = \db()->query($proSql); // 提交事务 db()->commit(); } catch (\Exception $e) { // 回滚事务 db()->rollback(); $this->error($e->getMessage()); } if ($priRes !== false && $proRes !== false){ $this->success('成功'); }else{ $this->error('失败'); } } /** * */ // public function ProcessDetailAdd(){ // } /** * */ // public function PrintDetailList(){ // } /** * 子订单列表 * @return null * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function PrintListData() { // 检查请求方式 if ($this->request->isGet() === false) { $this->error('请求错误'); } $param = $this->request->param(); // 检查参数是否存在 if (isset($param) === false) { $this->error('参数错误'); } // 查询型号 $where['Mod_rq'] = null; $xhdata = \db('工单_印件资料') ->where('订单编号', $param['order']) ->where($where) ->field('cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10') ->select(); $arr = []; // 收集cm1到cm10的非空非null值 foreach ($xhdata as $key => $value) { for ($i = 1; $i <= 10; $i++) { if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) { array_push($arr, $value['cm' . $i]); } } } $arr = array_unique($arr); sort($arr); // 查询详细列表 $list = \db('工单_印件资料') ->where('订单编号', $param['order']) ->where($where) ->field('订单编号,子订单编号,款号,颜色,船样,zdtotal,Sys_id,Sys_rq,ck_rq,sc_rq,cm1,cm2,cm3,cm4,cm5,cm6,updatatime as 更新时间,颜色备注,color_id,cm7,cm8,cm9,cm10,cmsl1,cmsl2,cmsl3,cmsl4,cmsl5,cmsl6,cmsl7,cmsl8,cmsl9,cmsl10,Uniqid') ->select(); // 遍历列表并处理cm和cmsl字段 foreach ($list as $key => $value) { for ($i = 1; $i <= 10; $i++) { if ($value['cm' . $i] !== '') { // 如果 cmsl 的值为 0,则设置为空字符串 $list[$key][$value['cm' . $i]] = ($value['cmsl' . $i] === 0) ? '' : $value['cmsl' . $i]; } // 删除原有的 cm 和 cmsl 字段 // unset($list[$key]['cm' . $i], $list[$key]['cmsl' . $i]); } } // 自定义型号排序 $customOrder = array('XXS','XS','S', 'M', 'L', 'XL','XXL', '2XL', '3XL', '4XL'); usort($arr, function ($a, $b) use ($customOrder) { $posA = array_search($a, $customOrder); $posB = array_search($b, $customOrder); return $posA - $posB; }); // 返回结果 $data['型号'] = $arr; $data['列表'] = $list; $this->success('成功', $data); } /** * 工单资料管理->印件资料删除 * @return void * @throws \think\Exception * @throws \think\exception\PDOException */ public function PrintDetailDel() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (isset($param['UniqId']) === false){ $this->error('参数错误'); } $printId = explode(',',$param['UniqId']); $i = 0; foreach ($printId as $value){ $res = \db('工单_印件资料') ->where('Uniqid',$value) ->update(['Mod_rq'=>date('Y-m-d H:i:s',time())]); if ($res === false){ $i++; } } if ($i === 0){ $this->success('删除成功'); }else{ $this->error('删除失败'); } } /** * 月度车间报工汇总->报工删除记录 */ public function ProcessDetailDel() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $where['a.mod_rq'] = ['neq', '']; $list = \db('设备_产量计酬')->alias('a') ->join('工单_印件资料 b', 'b.订单编号 = a.订单编号 AND a.子订单编号 = a.子订单编号') ->join('工单_基本资料 j', 'b.订单编号 = j.订单编号', 'LEFT') ->field(' b.订单编号, b.子订单编号, b.款号, b.颜色, b.船样, a.尺码, b.zdtotal as 制单数,b.颜色备注, a.数量, a.sys_rq as 上报时间,a.UniqId,a.mod_rq,a.delsys_id,a.sczl_bh, j.客户编号,j.生产款号,j.款式 ') ->where($where) ->order('a.mod_rq desc') ->group('a.UniqId') ->select(); // 提取所有的尺码,并去重 $sizeList = array_values(array_unique(array_column($list, '尺码'))); // 动态将尺码的数量添加到每个订单中,并替换已完成字段 foreach ($list as &$item) { $size = $item['尺码']; $item[$size] = $item['数量']; // 动态添加尺码字段,值为数量 // unset($item['数量']); // 移除原来的已完成字段 } $data['total'] = count($list); $data['table'] = $list; $this->success('成功',$data); } /** * */ // public function WastageList(){ // } /** * */ // public function getAnnexTable(){ // } /** * */ // public function annexAdd(){ // } /** * */ // public function annexDel(){ // } /** * */ // public function getGdAnnexTable(){ // } /** * 产品附件新增 * 1.前端进行上传xlsx文件表格 * 2.接口接受数据文件进行保存xlsx文件以及pdf转换 * 3.前端进行预览pdf图片显示文件中的内容再页面上 */ public function gdAnnexAdd() { ini_set('display_errors', 'On'); ini_set('error_reporting', E_ALL); if (!$this->request->isPost()) { $this->error('请求方式错误'); } // 获取请求参数 $req = $this->request->param(); $relateId = $req['关联编号']; $attachmentContent = $req['附件内容']; $attachmentType = $req['附件类型']; $prefixDir = ROOT_PATH . '/public/'; $uploadDir = ''.'uploads/' . date('Ymd') . '/' . $relateId; // 检查并创建目录 if (!is_dir($prefixDir . $uploadDir)) { mkdir($prefixDir . $uploadDir, 0777, true); } // 处理 Base64 附件内容 if (strpos($attachmentContent, 'base64,') !== false) { $attachmentContent = explode('base64,', $attachmentContent)[1]; } $fileContent = base64_decode($attachmentContent); $filename = time(); // 初始化文件路径变量 $xlsxFilePath = ''; $pdfFilePath = ''; if ($attachmentType === 'pdf') { // 保存 PDF 文件 $pdfFilePath = $uploadDir . '/' . $filename . '.pdf'; file_put_contents($prefixDir . $pdfFilePath, $fileContent); } elseif ($attachmentType === 'xlsx') { // 保存 Excel 文件 $xlsxFilePath = $uploadDir . '/' . $filename . '.xlsx'; file_put_contents($prefixDir . $xlsxFilePath, $fileContent); // 转换为 PDF 文件 $pdfFilePath = $uploadDir . '/' . $filename . '.pdf'; $cmd = sprintf( 'libreoffice --headless --convert-to pdf --outdir %s %s', escapeshellarg($prefixDir . $uploadDir), escapeshellarg($prefixDir . $xlsxFilePath) ); exec($cmd, $out, $retval); if ($retval !== 0) { $this->error('Excel 转 PDF 失败'); } } else { $this->error('不支持的附件类型'); } // 组织数据 $data = [ '关联编号' => $relateId, 'sys_id' => $req['sys_id'], '附件备注' => $req['附件备注'], '附件类型' => $attachmentType, 'sys_rq' => date('Y-m-d H:i:s'), 'url' => $xlsxFilePath, // 保存 xlsx 文件路径(如果存在) 'pdf' => $pdfFilePath // 保存 pdf 文件路径 ]; // 数据库事务处理 db()->startTrans(); try { // 插入数据 $sql = db('工单_相关附件')->fetchSql(true)->insert($data); $result = db()->query($sql); db()->commit(); } catch (\Exception $e) { // 回滚事务 db()->rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error('失败'); } $this->success('成功'); } /** * */ // public function gdAnnexDel() // { // } /** * */ // public function WorkOrderDetailCope() // { // } // /** * */ // public function WorkOrderDetailListCope() // { // } /** * */ // public function ProductCopeDetail() // { // } /** * */ // public function PlannedProcessYield($workOrder,$yjno,$gxh,$feed) // { // } /** * */ // public function WorkOrderDel() // { // } /** * */ // public function ProductNameData() // { // } /** * */ // public function getCouplet() // } /** * 订单打印接口 * order:订单编号 * 通过订单编号获取订单表数据以及子订单数据 */ public function orderPrint(){ if ($this->request->isGet() === false){$this->error('请求错误');} $param = $this->request->param(); if (empty($param['order'])){$this->error('参数错误');} $where['Mod_rq'] = null; //订单信息 $list = \db('工单_基本资料') ->where('订单编号',$param['order']) ->where($where) ->field('订单编号,img,生产款号,客户编号,款式,落货日期,箱唛要求,面料,船样描述,船样合计,粘衬,订单数量,审核,审核日期,要求,water') ->find(); //尺码表格表头 $xhdata = \db('工单_印件资料')->where('订单编号', $param['order'])->where($where)->select(); $arr = []; foreach ($xhdata as $key => $value) { for ($i = 1; $i <= 10; $i++) { if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) { array_push($arr, $value['cm' . $i]); } } } // 去重并重新索引 $arr = array_unique($arr); $arr = array_values($arr); // 自定义排序函数 usort($arr, function($a, $b) { // 判断是否为数字 $isNumericA = is_numeric($a); $isNumericB = is_numeric($b); if ($isNumericA && $isNumericB) { // 如果都是数字,按从小到大排序 return $a - $b; } elseif (!$isNumericA && !$isNumericB) { // 如果都是字母,按字母顺序排序(可以自定义顺序) $sizeOrder = ['XXS', 'XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL']; $posA = array_search($a, $sizeOrder); $posB = array_search($b, $sizeOrder); return $posA - $posB; } else { // 如果一个是数字一个是字母,数字排在前 return $isNumericA ? -1 : 1; } }); //打印table数据表格 $porlis = \db('工单_印件资料') ->where('订单编号',$param['order']) ->where('船样',0) ->where($where) ->field('子订单编号') ->select(); //合并后的子订单条码数据 $subOrder = $porlis[0]['子订单编号']; // 找到子订单编号中的 '-' 位置 $dashPos = strpos($subOrder, '-'); if ($dashPos !== false) { // 提取 '-' 后面的部分 $afterDash = substr($subOrder, $dashPos + 1); // 判断长度是否等于2或等于4 if (strlen($afterDash) == 2) { // 查询船样为0的数据 $processlist = \db('工单_印件资料') ->where('订单编号', $param['order']) ->where($where) ->where('船样', 0) ->field('子订单编号,颜色,款号,zdtotal,颜色备注,color_id, cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10, cmsl1,cmsl2,cmsl3,cmsl4,cmsl5,cmsl6,cmsl7,cmsl8,cmsl9,cmsl10,Uniqid') ->select(); // 初始化汇总数组 $scslTotals = [ "cmsl1" => 0, "cmsl2" => 0, "cmsl3" => 0, "cmsl4" => 0, "cmsl5" => 0, "cmsl6" => 0, "cmsl7" => 0, "cmsl8" => 0, "cmsl9" => 0, "cmsl10" => 0, "zdtotal" => 0, // 总计数量 ]; // 遍历数据集进行汇总 foreach ($processlist as $item) { // 遍历每个尺码字段(cmsl1 到 cmsl10) for ($i = 1; $i <= 10; $i++) { // 获取当前字段的数量 $sizeQty = $item['cmsl' . $i]; // 判断数量是否有效(非空且大于0) if (!empty($sizeQty)) { // 累加当前字段的数量 $scslTotals['cmsl' . $i] += $sizeQty; // 累加到总计字段 $scslTotals['zdtotal'] += $sizeQty; } } } $data['scslTotals'] = $scslTotals; foreach ($processlist as $key => $value) { // 将尺码和对应的数量从 cm1-cm10 和 cmsl1-cmsl10 转换为动态键值对 for ($i = 1; $i <= 10; $i++) { if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) { $processlist[$key][$value['cm' . $i]] = $value['cmsl' . $i]; } // 移除原始的 cm 和 cmsl 字段 unset($processlist[$key]['cm' . $i], $processlist[$key]['cmsl' . $i]); } } // 用于存储合并后的数据 $mergedData = []; // 按颜色备注进行合并 foreach ($processlist as $item) { $key = $item['颜色备注']; if (!isset($mergedData[$key])) { $mergedData[$key] = $item; // 添加条码字段,值为子订单编号 $mergedData[$key]['条码'] = $item['子订单编号']; } else { // 合并尺码对应的数量 foreach ($item as $size => $quantity) { if (is_numeric($size)) { if (!isset($mergedData[$key][$size])) { $mergedData[$key][$size] = 0; } $mergedData[$key][$size] += $quantity; } } // 合并 zdtotal $mergedData[$key]['zdtotal'] += $item['zdtotal']; } } // 查询船样为1的数据 $chuanyang = \db('工单_印件资料') ->where('订单编号', $param['order']) ->where($where) ->where('船样', 1) ->field('子订单编号,颜色,款号,zdtotal,颜色备注,color_id, cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10, cmsl1,cmsl2,cmsl3,cmsl4,cmsl5,cmsl6,cmsl7,cmsl8,cmsl9,cmsl10,Uniqid') ->select(); foreach ($chuanyang as $key => $value) { // 将尺码和对应的数量从 cm1-cm10 和 cmsl1-cmsl10 转换为动态键值对 for ($i = 1; $i <= 10; $i++) { if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) { $chuanyang[$key][$value['cm' . $i]] = $value['cmsl' . $i]; } // 移除原始的 cm 和 cmsl 字段 unset($chuanyang[$key]['cm' . $i], $chuanyang[$key]['cmsl' . $i]); } // 添加条码字段,值为子订单编号 $chuanyang[$key]['条码'] = $value['子订单编号']; } // 将合并后的数据插入到相应颜色备注的原始数据尾部 $finalList = []; $groupedData = []; // 将原始数据按颜色备注分组 foreach ($processlist as $item) { $key = $item['颜色备注']; if (!isset($groupedData[$key])) { $groupedData[$key] = []; } $groupedData[$key][] = $item; } // 将合并后的数据插入到对应的颜色备注组的尾部 foreach ($groupedData as $key => $items) { $finalList = array_merge($finalList, $items); // 先添加原始数据 if (isset($mergedData[$key])) { $finalList[] = $mergedData[$key]; // 在组的尾部添加合并数据 } } // 将船样为1的数据添加到最终列表中 $finalList = array_merge($finalList, $chuanyang); $data['process'] = $finalList; $data['order'] = $list; $data['xhdata'] = $arr; $this->success('成功',$data); } elseif (strlen($afterDash) == 4) { // if($list['water'] == 1){ // 查询非船样数据 // 获取数据 // $processlist = \db('工单_印件资料') // ->where('订单编号', $param['order']) // ->where($where) // ->where('船样', 0) // ->field('子订单编号,款号, color_id, 颜色, 颜色备注, zdtotal,船样, Uniqid, // cm1, cm2, cm3, cm4, cm5, cm6, cm7, cm8, cm9, cm10, // cmsl1, cmsl2, cmsl3, cmsl4, cmsl5, cmsl6, cmsl7, cmsl8, cmsl9, cmsl10') // ->select(); // //// 格式化原始数据 // $formattedData = []; // foreach ($processlist as $item) { // for ($i = 1; $i <= 10; $i++) { // if (!empty($item['cm' . $i])) { // $size = $item['cm' . $i]; // $item[$size] = $item['cmsl' . $i]; // 动态映射尺码字段 // } // unset($item['cm' . $i], $item['cmsl' . $i]); // 删除原始字段 // } // $formattedData[] = $item; // } // //// 合并数据逻辑 // $result = []; // foreach ($formattedData as $item) { // $key = $item['color_id'] . '_' . $item['颜色备注']; // if (!isset($result[$key])) { // $result[$key] = $item; // $result[$key]['条码'] = $item['子订单编号']; // 初始化条码字段 // } else { // foreach ($item as $size => $value) { // if (is_numeric($size)) { // $result[$key][$size] = ($result[$key][$size] ?? 0) + $value; // 累加尺码 // } // } // $result[$key]['zdtotal'] += $item['zdtotal']; // 累加总数 // } // } // //// 按照合并逻辑插入合并数据 // $finalData = $formattedData; // 复制原始数据 // foreach ($result as $key => $mergeItem) { // foreach (array_reverse($finalData, true) as $index => $originalItem) { // if ($originalItem['color_id'] . '_' . $originalItem['颜色备注'] === $key) { // // 插入合并数据到最后一条记录之后 // $finalData = array_merge( // array_slice($finalData, 0, $index + 1), // [$mergeItem], // array_slice($finalData, $index + 1) // ); // break; // } // } // } // //// 按 color_id 和 颜色备注 排序 // usort($finalData, function ($a, $b) { // // 按 color_id 排序 // if ($a['color_id'] === $b['color_id']) { // // 如果 color_id 相同,按颜色备注排序 // return strcmp($a['颜色备注'], $b['颜色备注']); // } // return strcmp($a['color_id'], $b['color_id']); // }); // // // 查询船样为 1 的数据 // $chuanyang = \db('工单_印件资料') // ->where('订单编号', $param['order']) // ->where($where) // ->where('船样', 1) // ->field('子订单编号,款号, color_id, 颜色, 颜色备注, zdtotal,船样, Uniqid, // cm1, cm2, cm3, cm4, cm5, cm6, cm7, cm8, cm9, cm10, // cmsl1, cmsl2, cmsl3, cmsl4, cmsl5, cmsl6, cmsl7, cmsl8, cmsl9, cmsl10') // ->select(); // //// 格式化船样数据// 格式化原始数据 // $chuanyangvval = []; // foreach ($chuanyang as $item) { // for ($i = 1; $i <= 10; $i++) { // if (!empty($item['cm' . $i])) { // $size = $item['cm' . $i]; // $item[$size] = $item['cmsl' . $i]; // 动态映射尺码字段 // } // unset($item['cm' . $i], $item['cmsl' . $i]); // 删除原始字段 // } // $item['条码'] = $item['子订单编号']; // 添加条码字段 // // $chuanyangvval[] = $item; // } // // // 合并船样和非船样数据并去重 // $final = array_map('unserialize', array_unique(array_map('serialize', array_merge($finalData, $chuanyangvval)))); // // // 清理数据:对于船样为 0 的记录,删除子订单编号和颜色字段 // foreach ($final as &$item) { // if (isset($item['条码']) && $item['船样'] == 0) { // unset($item['子订单编号'], $item['颜色']); // } // } // // // 统计累积数据 // $counthz = \db('工单_印件资料') // ->where('订单编号', $param['order']) // ->where($where) // ->select(); // // // 初始化统计数组 // $scslTotals = [ // 'cmsl1' => 0, 'cmsl2' => 0, 'cmsl3' => 0, 'cmsl4' => 0, 'cmsl5' => 0, // 'cmsl6' => 0, 'cmsl7' => 0, 'cmsl8' => 0, 'cmsl9' => 0, 'cmsl10' => 0, // 'zdtotal' => 0, // ]; // // // 累加统计尺码数量 // foreach ($counthz as $item) { // for ($i = 1; $i <= 10; $i++) { // $key = "cmsl{$i}"; // $scslTotals[$key] += (int)($item[$key] ?? 0); // 累加每个尺码的数量 // } // } // $scslTotals['zdtotal'] = array_sum(array_slice($scslTotals, 0, 10)); // 计算总数 // echo "
";
//                print_r($processlist);
//                echo "
";
//                // 返回数据给前端
//                $data['process'] = $processlist;
//                $data['scslTotals'] = $scslTotals;//汇总数据集
//                $data['order'] = $list;//表格数据
//                $data['xhdata'] = $arr;//尺码表头
//
//                $this->success('成功', $data);
//                }else{
                    //一条子订单编号一个条码,统计颜色
                $processlist = \db('工单_印件资料')
                    ->where('订单编号', $param['order'])
                    ->whereNull('Mod_rq') // 查询未删除数据
                    ->select();
                $table = [];
                foreach ($processlist as $item) {
                    // 当前子订单编号的数据
                    $subOrder = [
                        '颜色备注' => $item['颜色备注'],
                        '色系名称' => $item['颜色'],
                        '订单编号' => $item['订单编号'],
                        '子订单编号' => $item['子订单编号'],
                        '条码' => $item['子订单编号'],
                        '款号' => $item['款号'],
                    ];
                    // 当前子订单编号的合计
                    $subOrderTotal = 0;
                    for ($i = 1; $i <= 10; $i++) {
                        if (!empty($item['cm' . $i])) {
                            $subOrder[$item['cm' . $i]] = $item['cmsl' . $i];
                            // 累加每个尺码的数量
                            $subOrderTotal += $item['cmsl' . $i];
                        }
                        unset($item['cm' . $i], $item['cmsl' . $i]);
                    }
                    $subOrder['合计'] = $subOrderTotal; // 添加合计
                    $table[] = $subOrder; // 将当前子订单的数据添加到 $table 中
                }

                // 初始化汇总数组
                $scslTotals = [
                    "cmsl1" => 0,
                    "cmsl2" => 0,
                    "cmsl3" => 0,
                    "cmsl4" => 0,
                    "cmsl5" => 0,
                    "cmsl6" => 0,
                    "cmsl7" => 0,
                    "cmsl8" => 0,
                    "cmsl9" => 0,
                    "cmsl10" => 0,
                    "zdtotal" => 0, // 总计数量
                ];

                // 遍历数据集进行汇总
                foreach ($processlist as $item) {
                    // 遍历每个尺码字段(cmsl1 到 cmsl10)
                    for ($i = 1; $i <= 10; $i++) {
                        // 获取当前字段的数量
                        $sizeQty = $item['cmsl' . $i];
                        // 判断数量是否有效(非空且大于0)
                        if (!empty($sizeQty)) {
                            // 累加当前字段的数量
                            $scslTotals['cmsl' . $i] += $sizeQty;
                            // 累加到总计字段
                            $scslTotals['zdtotal'] += $sizeQty;
                        }
                    }
                }
                    $data['scslTotals'] = $scslTotals;
                    $data['process'] = $table;//汇总数据集
                    $data['order'] = $list;//表格数据
                    $data['xhdata'] = $arr;//尺码表头
                    $this->success('成功', $data);
//                }
            } else {
                echo "子订单编号 - 后不是2位也不是4位:$afterDash";
            }
        } else {
            echo "子订单编号中没有找到'-'";
        }
    }

    /**
     * 订单编号自动获取
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getWorkOrder()
    {
        if ($this->request->isGet() === false){
            $this->error('请求错误');
        }
        $time =substr( date('Ym',time()),2);
        $lastOrder = \db('工单_基本资料')
            ->where('订单编号','like','%'.$time.'%')
            ->order('Uniqid desc')
            ->find();
        if (empty($lastOrder)){
            $newNumber = 1;
        }else{
            $lastNumber = substr($lastOrder['订单编号'],6);
            $newNumber = (int)$lastNumber + 1;
        }
        if ($newNumber<10){
            $newOrder = 'DC'.$time.'00'.$newNumber;
        }elseif ($newNumber>=10 && $newNumber<100){
            $newOrder = 'DC'.$time.'0'.$newNumber;
        }else{
            $newOrder = 'DC'.$time.$newNumber;
        }
        $this->success('成功',$newOrder);
    }

    /**
     * 获取子订单编号
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getSuborder(){
        // 确保是GET请求
        if ($this->request->isGet() === false) {
            $this->error('请求错误');
        }
        $param = $this->request->param();
        if (empty($param) || !isset($param['cy']) || !isset($param['order'])) {
            $this->error('参数错误');
        }
        // 判断是否“船样”获取对应的子订单编号
        if ($param['cy'] == '否') {

            //1.通过色系名称查询对应的编号
            $colorlist = \db('工单_颜色编号')
                ->field('colorcode, colorname')
                ->where('colorname', $param['colorname'] ?? '')
                ->find();

            if (empty($colorlist)) {
                $this->error('未找到对应的颜色编号');
            }
            $num = $param['order'] . '-' . $colorlist['colorcode'];

            // 查询子订单编号
            $data = \db('工单_印件资料')
                ->field('子订单编号,cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10')
                ->where('子订单编号', 'like', '%' . $num . '%')
                ->where('船样', '=', 0)
                ->order('子订单编号', 'desc')
                ->find();

            if (empty($data)) {
                // 如果没有找到数据,生成默认的订单编号,后两位从00开始
                $order = $param['order'] . '-' . $colorlist['colorcode'] . '00';
            } else {

                if (strlen($data['子订单编号']) == 12) {
                    $data = \db('工单_印件资料')
                        ->where('订单编号',$param['order'])
                        ->where('船样',0)
                        ->order('子订单编号 desc')
                        ->find();
                    if(empty($data)){
                        $order = $param['order'].'-01';
                    }else{
                        $num = (int)substr($data['子订单编号'],10) + 1;
                        if ($num<10){
                            $order = $param['order'].'-0'.$num;
                        }else{
                            $order = $param['order'].'-'.$num;
                        }
                    }
                }else{
                    // 如果找到数据,提取子订单编号并递增
                    $order = $data['子订单编号'];
                    if (preg_match('/(.*-' . $colorlist['colorcode'] . ')(\d{2})$/', $order, $matches)) {
                        $prefix = $matches[1]; // 前缀部分(包括订单编号和颜色代码)
                        $number = $matches[2]; // 数字部分(后两位)

                        // 循环生成子订单编号并检查数据库中是否存在相同编号
                        do {
                            $incrementedNumber = (int)$number + 1;
                            // 将数字部分补充为两位数,例如 1 -> 01,2 -> 02
                            $order = $prefix . str_pad($incrementedNumber, 2, '0', STR_PAD_LEFT);
                            $exists = \db('工单_印件资料')->where('子订单编号', '=', $order)->find();
                            $number = $incrementedNumber; // 更新 number 用于下一次循环
                        } while ($exists); // 如果存在相同编号则继续循环递增
                    } else {
                        $this->error('订单编号格式错误');
                    }
                }

            }

            //2.获取色系名称信息
            $colorlist = \db('工单_颜色编号')->select();

            //3.获取历史尺码数据
            $cm_list = \db('工单_印件资料')
                ->where('订单编号', $param['order'])
                ->group('订单编号')
                ->find();

            $cm_data = [];
            if ($cm_list) {
                // 精准筛选字段名以 "cm" 开头并跟1到2位数字的字段
                foreach ($cm_list as $key => $value) {
                    if (preg_match('/^cm\d{1,2}$/', $key)) {
                        $cm_data[$key] = $value ?? ''; // 如果值为 null,设为空字符串
                    }
                }
            } else {
                // 如果查询结果为空,将 cm1 到 cm10 都设置为空
                for ($i = 1; $i <= 10; $i++) {
                    $cm_data["cm$i"] = '';
                }
            }



            $result = ['order' => $order,'colorlist' => $colorlist,'cm' => $cm_data];
            $this->success('成功', $result);

        } else if ($param['cy'] == '是') {

            //1.获取船样子订单编号
            $data = \db('工单_印件资料')
                ->where('订单编号', $param['order'])
                ->where('船样', '=', 1)
                ->order('子订单编号 asc')
                ->find();

            if (empty($data)) {
                // 如果没有数据,初始子订单编号为 '99'
                $order = $param['order'] . '-99';
            } else {
                // 提取子订单编号中的数字部分
                $subOrder = $data['子订单编号'];  // 例如 "DC2410006-99"
                if (preg_match('/-(\d+)$/', $subOrder, $matches)) {
                    $numberPart = (int)$matches[1];  // 提取到的数字部分
                    $newNumber = $numberPart - 1;    // 减 1
                    // 将新的数字拼接回订单编号
                    $order = preg_replace('/-\d+$/', '-' . $newNumber, $subOrder);
                } else {
                    $this->error('订单编号格式错误');
                }
            }

            $this->success('成功', $order);
        }
    }

    /**
     * 获取PO号 【代码展示未用到】
     */
    public function getPonumber() {
        if ($this->request->isGet() === false){
            $this->error('请求错误');
        }
        $param = $this->request->param();
        if (empty($param)){
            $this->error('参数错误');
        }
        // $num = substr($param['child_order'], 0, 12);  // 如果需要,可以用这个方式截取子订单编号
//        $sql = "SELECT * FROM `工单_印件资料` WHERE `子订单编号` LIKE '{$num}%' ORDER BY `子订单编号` DESC LIMIT 1";
//        $data = \db()->query($sql);
        $colorlist = \db('工单_颜色编号')
            ->field('colorcode,colorname')
            ->where('colorname',$param['child_order'])
            ->find();
        $num = $param['order'] . '-' . $colorlist['colorcode'];  // 生成 num,用于模糊查询
        $data = \db('工单_印件资料')
            ->where('子订单编号', 'like', '%' . $num . '%')
            ->order('子订单编号', 'desc')
            ->find();
        if(count($data) === 1){
            $order = $num.'01';
        }else{
            $number = (int)substr($data['子订单编号'],12,14) + 1;
            if ($num<10){
                $order = $num.'0'.$number;
            }else{
                $order = $num.$number;
            }
        }
        $this->success('成功',$order);
    }

    /**
     * 订单资料修改
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\PDOException
     */
    public function WorkOrderEdit()
    {
        if (Request::instance()->isPost() === false){
            $this->error('请求错误');
        }
        $param = Request::instance()->post();
        if (empty($param)){
            $this->error('参数错误');
        }
        $id = $param['id'];
        unset($param['id']);
        $sql = \db('工单_基本资料')
            ->where('Uniqid',$id)
            ->fetchSql(true)
            ->update($param);
        $res = \db()->query($sql);
        if ($res === false){
            $this->error('失败');
        }else{
            $this->success('成功');
        }
    }


    /**
     * 颜色资料修改
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\PDOException
     */
    public function PrintDataEdit()
    {
        if(Request::instance()->post() === false){
            $this->error('请求错误');
        }
        $param = Request::instance()->post();
        if (empty($param)){
            $this->error('参数错误');
        }
//        $id = $param['id'];
//        unset($param['id']);

//        $findlist = \db('工单_印件资料')
//            ->where('Uniqid', $param['id'])
//            ->find();

        $updata = [
            '订单编号' => $param['订单编号'],
            '子订单编号' => $param['子订单编号'],
            '款号' => $param['款号'],
            '船样' => $param['船样'],
            '颜色' => $param['颜色'],
            'color_id' => $param['color_id'],
            '颜色备注' => $param['颜色备注'],
            'zdtotal' => $param['zdtotal']
        ];
        for ($i = 1; $i <= 10; $i++) {
            $updata["cmsl{$i}"] = isset($param["cmsl{$i}"]) ? $param["cmsl{$i}"] : '';
        }
        $sql = \db('工单_印件资料')
            ->where('Uniqid', $param['id'])
            ->fetchSql(true)
            ->update($updata);
        $res = \db()->query($sql);
        if ($res !== false) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }

    /**
     * 图片上传
     * @return void
     */
    public function ImgUpload(){
        $file = request()->file('image');
        if($file){
            $info = $file->validate(['size'=>10485760,'ext'=>'jpg,png'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $fileName = $info->getSaveName();
//                $ymd = date('Ymd');
//                $imageUrl = '/uploads/' . $ymd.'/'.$fileName;
                $imageUrl = '/uploads/' . str_replace('\\', '/', $fileName);
                return json(['code' => 0, 'msg' => '成功', 'data' => ['url' => $imageUrl]]);
            }else{
                $res = $file->getError();
                return json(['code' => 1, 'msg' => '失败', 'data' => $res]);
            }
        }
        return json(['code' => 1, 'msg' => '没有文件上传', 'data' => null]);
    }

    /**
     * 工单技术附件
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function OrderAttachments()
    {
        if ($this->request->isGet() === false){
            $this->error('请求错误');
        }
        $param = $this->request->param();
        if (empty($param)){
            $this->error('参数错误');
        }
        $list = \db('工单_相关附件')
            ->where('关联编号',$param['order'])
            ->where('附件备注',$param['desc'])
            ->whereNull('Mod_rq')
            ->order('sys_rq desc')
            ->limit(1)
            ->select();
        $this->success('成功',$list);
    }
}