| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use think\Db;
- /**
- * 工单工艺管理
- *
- */
- class WorkOrderProcess extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- /**
- * 工单大工序列表
- * @param workorder 工单编号
- */
- public function MajorprocessList()
- {
- if (!$this->request->isGet()) {
- $this->error('请求方法错误');
- }
- $params = $this->request->param();
- if (empty($params['workorder'])) {
- $this->error('工单编号不能为空');
- }
- $list = db('工单_工艺资料')
- ->where('订单编号', $params['workorder'])
- ->field('工序编号,工序名称,工序备注')
- ->distinct('工序名称')
- ->select();
- if(empty($list)){
- $this->error('该工单没有大工序');
- }
- $this->success('成功', $list);
- }
- /**
- * 工单部件列表
- * @param workorder 工单编号
- */
- public function PartList()
- {
- if (!$this->request->isGet()) {
- $this->error('请求方法错误');
- }
- $params = $this->request->param();
- if (empty($params['workorder'])) {
- $this->error('工单编号不能为空');
- }
- $list = db('工单_部件资料')
- ->where('work_order', $params['workorder'])
- ->where('del_rq', null)
- ->field('part_code as 部件编号,part_name as 部件名称,remark as 部件备注,part_type as 部件类型,status as 状态,
- sys_id as 操作人,sys_rq as 操作时间,mod_rq as 修改时间,mod_id as 修改人,id as 部件ID')
- ->order('part_code')
- ->select();
- if(empty($list)){
- // $this->error('该工单没有部件,请先添加部件');
- }
- $this->success('成功', $list);
- }
- /**
- * 新增工单部件
- * @param workorder 工单编号
- * @param part_code 部件编号
- * @param part_name 部件名称
- * @param remark 部件备注
- * @param part_type 部件类型
- * @param sys_id 操作人
- */
- public function AddPart()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方法错误');
- }
- $params = $this->request->post();
- if (empty($params['workorder'])) {
- $this->error('工单编号不能为空');
- }
- if (empty($params['part_name'])) {
- $this->error('部件名称不能为空');
- }
- // 检查是否传入了part_code
- if (!empty($params['part_code'])) {
- $code = $params['part_code'];
- // 检查是否存在冲突
- $exists = db('工单_部件资料')
- ->where('work_order', $params['workorder'])
- ->where('part_code', '>=', $code)
- ->count();
- if ($exists > 0) {
- // 如果存在冲突,将后续编号自动加1
- db('工单_部件资料')
- ->where('work_order', $params['workorder'])
- ->where('part_code', '>=', $code)
- ->setInc('part_code');
- }
- } else {
- // 如果没有传入part_code,按照原来的逻辑生成
- $code = db('工单_部件资料')
- ->where('work_order', $params['workorder'])
- ->max('part_code');
- $code = $code ? $code + 1 : 1;
- }
- $data = [
- 'work_order' => $params['workorder'],
- 'part_code' => $code,
- 'part_name' => $params['part_name'],
- 'remark' => $params['remark'],
- 'part_type' => $params['part_type'],
- 'status' => 1,
- 'sys_id' => $params['sys_id'],
- 'sys_rq' => date('Y-m-d H:i:s'),
- ];
- $result = db('工单_部件资料')->insert($data);
- if (!$result) {
- $this->error('新增失败');
- }
- $this->success('新增成功');
- }
- /**
- * 修改部件状态
- * @param id 部件ID
- * @param status 状态
- */
- public function UpdatePartStatus()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方法错误');
- }
- $params = $this->request->post();
- if (empty($params['id'])) {
- $this->error('部件ID不能为空');
- }
- if (!isset($params['status'])) {
- $this->error('状态不能为空');
- }
- $result = db('工单_部件资料')
- ->where('id', $params['id'])
- ->update(['status' => $params['status']]);
- if (!$result) {
- $this->error('修改失败');
- }
- $this->success('修改成功');
- }
- /**
- * 修改部件信息
- * @param workorder 工单编号
- * @param part_code 部件编号
- * @param part_name 部件名称
- * @param remark 部件备注
- * @param part_type 部件类型
- */
- public function UpdatePartInfo()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方法错误');
- }
- $params = $this->request->post();
- if (empty($params['part_name'])) {
- $this->error('部件名称不能为空');
- }
- if (empty($params['remark'])) {
- $this->error('部件备注不能为空');
- }
- if (empty($params['part_type'])) {
- $this->error('部件类型不能为空');
- }
- if(empty($params['mod_id'])){
- $this->error('修改人不能为空');
- }
- if(empty($params['id'])){
- $this->error('部件ID不能为空');
- }
- $result = db('工单_部件资料')
- ->where('id', $params['id'])
- ->update([
- 'part_name' => $params['part_name'],
- 'remark' => $params['remark'],
- 'part_type' => $params['part_type'],
- 'mod_id' => $params['mod_id'],
- 'mod_rq' => date('Y-m-d H:i:s'),
- ]);
- if (!$result) {
- $this->error('修改失败');
- }
- $this->success('修改成功');
- }
- /** 软删除部件信息
- * @param id 部件ID
- */
- public function DeletePart()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方法错误');
- }
- $params = $this->request->post();
- if (empty($params['id'])) {
- $this->error('部件ID不能为空');
- }
- $ids = explode(',', $params['id']);
- $ids = array_filter(array_map('intval', $ids));
- if (empty($ids)) {
- $this->error('无效的部件ID');
- }
- $result = db('工单_部件资料')
- ->whereIn('id', $ids)
- ->whereNull('del_rq')
- ->update(['del_rq' => date('Y-m-d H:i:s')]);
- if ($result === false) {
- $this->error('删除失败');
- }
- if ($result === 0) {
- $this->error('未找到可删除的部件');
- }
- $this->success('删除成功');
- }
- /**
- * 工单工艺列表
- * @param workorder 工单编号
- */
- public function GetProcessList()
- {
- if (!$this->request->isGet()) {
- $this->error('请求方法错误');
- }
- $params = $this->request->param();
- if (empty($params['workorder'])) {
- $this->error('工单编号不能为空');
- }
- $list = db('工单_基础工艺资料')
- ->alias('a')
- ->join('工单_部件资料 b', 'a.part_code = b.part_code and a.work_order = b.work_order', 'LEFT')
- ->where('a.del_rq', null)
- ->where('a.work_order', $params['workorder'])
- ->where('b.del_rq', null)
- ->field('a.id,a.part_code as 部件编号,
- IFNULL(b.part_name, "") as 部件名称,
- a.process_code as 工艺编号,
- a.process_name as 工艺名称,
- a.big_process as 大工艺,
- a.standard_hour as 标准工时,
- a.standard_score as 标准公分,
- a.remark as 备注,
- a.coefficient as 系数,
- a.sys_id as 系统人,
- a.sys_rq as 系统时间,
- a.mod_id as 修改人,
- a.mod_rq as 修改时间')
- ->order('a.id')
- ->select();
- $this->success('成功', $list);
- }
- /**
- * 工单工艺列表
- * @param workorder 工单编号
- */
- // public function GetProcessList()
- // {
- // if (!$this->request->isGet()) {
- // $this->error('请求方法错误');
- // }
- // $params = $this->request->param();
- // if (empty($params['workorder'])) {
- // $this->error('工单编号不能为空');
- // }
- // $list = db('工单_基础工艺资料')
- // ->alias('a')
- // ->join('工单_部件资料 b', 'a.part_code = b.part_code and a.work_order = b.work_order')
- // ->where('a.del_rq', null)
- // ->where('a.work_order', $params['workorder'])
- // ->where('b.del_rq', null)
- // ->field('a.id,a.part_code as 部件编号,b.part_name as 部件名称,a.process_code as 序号,
- // a.process_name as 工序名称,a.big_process as 大工艺,a.standard_hour as 秒,a.standard_minutes as 分钟,
- // a.standard_score as 标准公分,a.money as 金额,a.remark as 备注,a.coefficient as 难度系数,a.sys_id as 操作人员,
- // a.sys_rq as 系统时间,a.mod_id as 修改人,a.mod_rq as 修改时间')
- // ->order('process_code')
- // ->select();
- // $this->success('成功', $list);
- // }
- /**
- * 获取部件列表
- * @param workorder 工单编号
- */
- public function getpartlist()
- {
- if(!$this->request->isGet()){
- $this->error('请求方法错误');
- }
- $params = $this->request->get();
- if (empty($params['workorder'])) {
- $this->error('工单编号不能为空');
- }
- $list = db('工单_部件资料')
- ->where('work_order', $params['workorder'])
- ->where('del_rq', null)
- ->field('part_code as 部件编号,part_name as 部件名称')
- ->order('part_code')
- ->select();
- if(empty($list)){
- $this->error('该工单没有部件,请先添加部件');
- }
- $this->success('成功', $list);
- }
- /**
- * 新增工单工艺资料
- * @param workorder 工单编号
- * @param part_code 部件编号
- * @param process_code 工艺编号
- * @param process_name 工艺名称
- * @param big_process 大工艺
- * @param standard_hour 标准工时
- * @param standard_score 标准公分
- * @param sys_id 系统人
- * @param coefficient 系数
- * @param remark 备注
- */
- public function AddProcess()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方法错误');
- }
- $params = $this->request->post();
- if (empty($params['workorder'])) {
- $this->error('工单编号不能为空');
- }
- if (empty($params['part_code'])) {
- $this->error('部件编号不能为空');
- }
- if (empty($params['process_name'])) {
- $this->error('工艺名称不能为空');
- }
- if (empty($params['big_process'])) {
- $this->error('大工艺不能为空');
- }
- if (empty($params['standard_hour'])) {
- $this->error('标准工时不能为空');
- }
- if (empty($params['standard_score'])) {
- $this->error('标准公分不能为空');
- }
- if (empty($params['coefficient'])) {
- $params['coefficient'] = 1;
- }
- if (empty($params['sys_id'])) {
- $this->error('系统人不能为空');
- }
- // 检查是否传入了process_code
- if (!empty($params['process_code'])) {
- $code = $params['process_code'];
- // 检查是否存在冲突
- $exists = db('工单_基础工艺资料')
- ->where('work_order', $params['workorder'])
- ->where('part_code', $params['part_code'])
- ->where('process_code', '>=', $code)
- ->count();
- if ($exists > 0) {
- // 如果存在冲突,将后续编号自动加1
- db('工单_基础工艺资料')
- ->where('work_order', $params['workorder'])
- ->where('part_code', $params['part_code'])
- ->where('process_code', '>=', $code)
- ->setInc('process_code');
- }
- } else {
- // 如果没有传入process_code,按照原来的逻辑生成
- $code = db('工单_基础工艺资料')
- ->where('work_order', $params['workorder'])
- ->where('part_code', $params['part_code'])
- ->max('process_code');
- $code = $code ? $code + 1 : 1;
- }
- $data = [
- 'work_order' => $params['workorder'],
- 'part_code' => $params['part_code'],
- 'process_code' => $code,
- 'process_name' => $params['process_name'],
- 'big_process' => $params['big_process'],
- 'standard_hour' => $params['standard_hour'],
- 'standard_score' => $params['standard_score'],
- 'coefficient' => $params['coefficient'],
- 'remark' => $params['remark'],
- 'sys_id' => $params['sys_id'],
- 'sys_rq' => date('Y-m-d H:i:s'),
- ];
- $result = db('工单_基础工艺资料')->insert($data);
- if ($result === false) {
- $this->error('新增失败');
- }
- $this->success('新增成功');
- }
- /**
- * 更新工单工艺资料
- * @param id 工艺ID
- * @param mod_id 修改人
- * @param process_name 工艺名称
- * @param big_process 大工艺
- * @param standard_hour 标准工时
- * @param standard_score 标准公分
- * @param coefficient 系数
- * @param remark 备注
- */
- public function UpdateProcess()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方法错误');
- }
- $params = $this->request->post();
- if (empty($params['id'])) {
- $this->error('工艺ID不能为空');
- }
- if (empty($params['mod_id'])) {
- $this->error('修改人不能为空');
- }
- if (empty($params['process_name'])) {
- $this->error('工艺名称不能为空');
- }
- if (empty($params['big_process'])) {
- $this->error('大工艺不能为空');
- }
- if (empty($params['standard_hour'])) {
- $this->error('秒不能为空');
- }
- if (empty($params['standard_minutes'])) {
- $this->error('分钟不能为空');
- }
- if (empty($params['standard_score'])) {
- $this->error('定额分不能为空');
- }
- if (empty($params['money'])) {
- $this->error('金额不能为空');
- }
- if (!isset($params['coefficient']) || $params['coefficient'] === '') {
- $params['coefficient'] = 'C';
- }
- $process = Db::table('工单_基础工艺资料')
- ->where('id', intval($params['id']))
- ->whereNull('del_rq')
- ->find();
- if (empty($process)) {
- $this->error('工艺不存在');
- }
- $data = [
- 'part_code' => $params['part_code'],
- 'part_name' => $params['part_name'],
- 'standard_minutes' => $params['standard_minutes'],
- 'money' => $params['money'],
- 'remark' => $params['remark'],
- 'process_name' => $params['process_name'],
- 'big_process' => $params['big_process'],
- 'standard_hour' => $params['standard_hour'],
- 'standard_score' => $params['standard_score'],
- 'coefficient' => $params['coefficient'],
- 'mod_id' => $params['mod_id'],
- 'mod_rq' => date('Y-m-d H:i:s'),
- ];
- $fieldLabels = [
- 'part_code' => '部件编号',
- 'part_name' => '部件名称',
- 'process_code' => '工序编号',
- 'standard_minutes' => '分',
- 'money' => '金额',
- 'remark' => '备注',
- 'process_name' => '工序名称',
- 'big_process' => '大工序',
- 'standard_hour' => '秒',
- 'standard_score' => '定额分',
- 'coefficient' => '难度系数',
- ];
- $operTime = date('Y-m-d H:i:s');
- $logList = [];
- foreach ($fieldLabels as $field => $label) {
- $oldValue = isset($process[$field]) ? $process[$field] : '';
- $newValue = $data[$field];
- if ((string)$oldValue === (string)$newValue) {
- continue;
- }
- $logList[] = [
- 'order_no' => $process['work_order'],
- 'process_code' => $process['process_code'],
- 'change_field' => $label,
- 'old_value' => $oldValue === null ? '' : (string)$oldValue,
- 'new_value' => (string)$newValue,
- 'oper_type' => '修改工单工艺',
- 'oper_user_name' => $params['mod_id'],
- 'oper_time' => $operTime,
- ];
- }
- if (empty($logList)) {
- $this->error('未找到可更新的工艺或数据无变化');
- }
- $standardScoreChanged = (string)$process['standard_score'] !== (string)$params['standard_score'];
- if ($standardScoreChanged && (string)$process['status'] === '0') {
- $workOrder = Db::table('工单_基本资料')
- ->where('订单编号', $process['work_order'])
- ->where('Mod_rq', null)
- ->field('计划制造工分')
- ->find();
- if (empty($workOrder)) {
- $this->error('工单不存在');
- }
- $totalStandardScore = Db::table('工单_基础工艺资料')
- ->where('work_order', $process['work_order'])
- ->where('status', 0)
- ->whereNull('del_rq')
- ->sum('standard_score');
- $totalStandardScore = $totalStandardScore ? floatval($totalStandardScore) : 0;
- $newTotal = $totalStandardScore - floatval($process['standard_score']) + floatval($params['standard_score']);
- if ($newTotal > floatval($workOrder['计划制造工分'])) {
- $this->error('工序定额分和大于制造工分,请确认之后再修改');
- }
- }
- Db::startTrans();
- try {
- $updateResult = Db::table('工单_基础工艺资料')
- ->where('id', intval($params['id']))
- ->whereNull('del_rq')
- ->update($data);
- if ($updateResult === false) {
- throw new \Exception('更新工艺失败');
- }
- if ($updateResult === 0) {
- throw new \Exception('未找到可更新的工艺');
- }
- $logResult = Db::name('work_order_operation_log')->insertAll($logList);
- if ($logResult === false) {
- throw new \Exception('写入操作日志失败');
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error('更新失败');
- }
- $this->success('更新成功');
- }
- /**
- * 工单工艺资料软删除
- * @param id 工艺ID
- */
- public function DeleteProcess()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方法错误');
- }
- $params = $this->request->post();
- if (empty($params['id'])) {
- $this->error('工艺ID不能为空');
- }
- $ids = explode(',', $params['id']);
- $ids = array_filter(array_map('intval', $ids));
- if (empty($ids)) {
- $this->error('无效的工艺ID');
- }
- $result = db('工单_基础工艺资料')
- ->whereIn('id', $ids)
- ->whereNull('del_rq')
- ->update(['del_rq' => date('Y-m-d H:i:s')]);
- if ($result === false) {
- $this->error('删除失败');
- }
- if ($result === 0) {
- $this->error('未找到可删除的工艺');
- }
- $this->success('删除成功');
- }
- /**
- * 工单工艺复制
- * @param workorder 工单编号
- * @param product_code 产品编号
- * @param sys_id 操作人
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function workorderprocessCopy()
- {
- if (!$this->request->isGet()) {
- $this->error('请求方法错误');
- }
- $params = $this->request->param();
- if (empty($params['workorder'])) {
- $this->error('工单编号不能为空');
- }
- if (empty($params['product_code'])) {
- $this->error('产品编号不能为空');
- }
- if (empty($params['sys_id'])) {
- $this->error('操作人不能为空');
- }
- $now = date('Y-m-d H:i:s');
- $productParts = db('产品_部件资料')
- ->where('product_code', $params['product_code'])
- ->where('mod_rq', null)
- ->field('part_sort as part_code,part_name')
- ->select();
- if (empty($productParts)) {
- $this->error('该产品没有有效的部件');
- }
- $productProcesses = db('产品_工艺资料')
- ->where('product_code', $params['product_code'])
- ->field('part_sort as part_code,part_name,gy_sort as process_code,gy_name as process_name,
- big_process,standard_hour,standard_score,difficulty_coef as coefficient')
- ->select();
- if (empty($productProcesses)) {
- $this->error('该产品没有工艺');
- }
- $workOrderParts = [];
- //配置工单部件数据
- foreach ($productParts as $value) {
- $workOrderParts[] = [
- 'work_order' => $params['workorder'],
- 'part_code' => $value['part_code'],
- 'part_name' => $value['part_name'],
- 'part_type' => '',
- 'remark' => '',
- 'status' => 1,
- 'sys_id' => $params['sys_id'],
- 'sys_rq' => $now,
- ];
- }
- $workOrderProcesses = [];
- //配置工单工艺数据
- foreach ($productProcesses as $value) {
- $workOrderProcesses[] = [
- 'work_order' => $params['workorder'],
- 'part_code' => $value['part_code'],
- 'part_name' => $value['part_name'],
- 'process_code' => $value['process_code'],
- 'process_name' => $value['process_name'],
- 'big_process' => $value['big_process'],
- 'standard_hour' => $value['standard_hour'],
- 'standard_score' => $value['standard_score'],
- 'coefficient' => $value['coefficient'],
- 'remark' => '',
- 'sys_id' => $params['sys_id'],
- 'sys_rq' => $now,
- ];
- }
- Db::startTrans();
- try {
- //删除数据库现有的工单部件数据
- Db::name('工单_部件资料')->where('work_order', $params['workorder'])->delete();
- //删除数据库现有的工单工艺数据
- Db::name('工单_基础工艺资料')->where('work_order', $params['workorder'])->delete();
- //插入工单部件数据
- $partInsertCount = Db::name('工单_部件资料')->insertAll($workOrderParts);
- if ($partInsertCount === false) {
- throw new \Exception('工单部件复制失败');
- }
- //插入工单工艺数据
- $processInsertCount = Db::name('工单_基础工艺资料')->insertAll($workOrderProcesses);
- if ($processInsertCount === false) {
- throw new \Exception('工单工艺复制失败');
- }
- //提交事务
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error('复制失败');
- }
- $this->success('复制成功');
- }
- /**
- * 查询产品类型
- * @ApiMethod (GET)
- * @param string $workorder 工单编号
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getproducttype()
- {
- if(!$this->request->isGet()){
- $this->error('请求方法错误');
- }
- $params = $this->request->get();
- if (empty($params['product'])) {
- $this->error('产品类型不能为空');
- }
- $list = db('产品_基本资料')
- ->where('product_type', $params['product'])
- ->where('status', 1)
- ->field('product_code as 产品编号,product_type as 产品类型,product_name as 产品名称')
- ->select();
- if(empty($list)){
- $this->error('该产品类型没有产品');
- }
- $this->success('成功', $list);
- }
- /**
- * 工单工艺排序
- * @param workorder 工单编号
- * @param part_code 部件编号
- * @param process_code 工艺编号
- * @param process_name 工艺名称
- * @param big_process 大工艺
- * @param standard_hour 标准工时
- * @param standard_score 标准公分
- * @param coefficient 系数
- * @param remark 备注
- */
- public function sortProcess()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方法错误');
- }
- $params = $this->request->post();
- if (empty($params)) {
- $this->error('参数不能为空');
- }
- if (!is_array($params)) {
- $this->error('参数格式错误');
- }
- Db::startTrans();
- try {
- foreach ($params as $value) {
- if (empty($value['id'])) {
- throw new \Exception('缺少工艺ID');
- }
- if (!isset($value['process_code']) || $value['process_code'] === '') {
- throw new \Exception('缺少工艺编号');
- }
- $result = db('工单_基础工艺资料')
- ->where('id', intval($value['id']))
- ->whereNull('del_rq')
- ->update(['process_code' => intval($value['process_code'])]);
- if ($result === false) {
- throw new \Exception('排序失败');
- }
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('排序成功');
- }
- /**
- * 工单工艺excel导入
- * @param workorder 工单编号
- *
- */
- public function importProcess()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方法错误');
- }
- $params = $this->request->post();
- if (empty($params['workorder'])) {
- $this->error('工单编号不能为空');
- }
- if (empty($params['sys_id'])) {
- $this->error('操作人不能为空');
- }
- $file = $this->request->file('file');
- if (!$file) {
- $this->error('文件不能为空');
- }
- $uploadDir = ROOT_PATH . 'public' . DS . 'uploads';
- $info = $file->validate(['size' => 1024 * 1024 * 10, 'ext' => 'xlsx,xls,csv,txt'])
- ->move($uploadDir);
- if (!$info) {
- $this->error($file->getError());
- }
- $filePath = $uploadDir . DS . $info->getSaveName();
- // 定额表:上方为标题与元数据,第 5 行表头,第 6 行起为数据
- $data = $this->readExcel($filePath, 5, 6);
- if (empty($data)) {
- $this->error('文件内容为空');
- }
- $seenSeq = [];
- foreach ($data as $row) {
- $seq = isset($row['序号']) ? $row['序号'] : null;
- if ($seq === null || $seq === '') {
- continue;
- }
- $seqKey = is_scalar($seq) ? (string)$seq : $seq;
- if (isset($seenSeq[$seqKey])) {
- $this->error('工序序号重复,请重新调整之后再上传');
- }
- $seenSeq[$seqKey] = true;
- }
- $now = date('Y-m-d H:i:s');
- $partNameToCode = [];
- $nextPartCode = 0;
- foreach ($data as $row) {
- $partName = isset($row['部件名称']) ? $row['部件名称'] : '';
- if ($partName === '' || isset($partNameToCode[$partName])) {
- continue;
- }
- $partNameToCode[$partName] = ++$nextPartCode;
- }
- $workOrderParts = [];
- $i = 0;
- foreach ($partNameToCode as $partName => $partCode) {
- $workOrderParts[$i++] = [
- 'work_order' => $params['workorder'],
- 'part_name' => $partName,
- 'part_code' => $partCode,
- 'part_type' => '',
- 'remark' => '',
- 'status' => 1,
- 'sys_id' => $params['sys_id'],
- 'sys_rq' => $now,
- ];
- }
- Db::startTrans();
- try {
- //删除数据库现有的工单部件数据
- Db::name('工单_部件资料')->where('work_order', $params['workorder'])->delete();
- //插入工单部件数据
- $partInsertCount = Db::name('工单_部件资料')->insertAll($workOrderParts);
- if ($partInsertCount === false) {
- throw new \Exception('工单部件导入失败');
- }
- //提交事务
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $workOrderProcesses = [];
- foreach ($data as $value) {
- $name = isset($value['部件名称']) ? $value['部件名称'] : '';
- $partCode = ($name !== '' && isset($partNameToCode[$name])) ? $partNameToCode[$name] : '';
- $workOrderProcesses[] = [
- 'work_order' => $params['workorder'],
- 'part_code' => $partCode,
- 'part_name' => $name,
- 'process_code' => $value['序号'],
- 'process_name' => $value['工序名称'],
- 'big_process' => $value['生产工序'],
- 'standard_hour' => $value['秒'],
- 'standard_minutes' => $value['分'],
- 'standard_score' => $value['定额分'],
- 'money' => $value['金额'],
- 'coefficient' => $value['难度系数'],
- 'remark' => isset($value['备注']) ? $value['备注'] : '',
- 'sys_id' => $params['sys_id'],
- 'sys_rq' => $now,
- ];
- }
- Db::startTrans();
- try {
- //删除数据库现有的工单工艺数据
- Db::name('工单_基础工艺资料')->where('work_order', $params['workorder'])->delete();
- //插入工单工艺数据
- $processInsertCount = Db::name('工单_基础工艺资料')->insertAll($workOrderProcesses);
- if ($processInsertCount === false) {
- throw new \Exception('工单工艺导入失败');
- }
- //提交事务
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('导入成功');
- }
- /**
- * 读取 Excel/CSV(PhpSpreadsheet)
- *
- * @param string $filePath
- * @param int $headerRowNum 表头所在行(1 起计,如定额表为第 5 行)
- * @param int $dataStartRowNum 首条数据行(1 起计,须大于表头行,如第 6 行)
- * @return array 每行一条关联数组,键为表头单元格文本
- */
- public function readExcel($filePath, $headerRowNum = 5, $dataStartRowNum = 6)
- {
- if (!is_file($filePath) || !is_readable($filePath)) {
- return [];
- }
- if ($dataStartRowNum <= $headerRowNum) {
- return [];
- }
- $spreadsheet = IOFactory::load($filePath);
- $sheet = $spreadsheet->getActiveSheet();
- $rows = $sheet->toArray();
- if (empty($rows)) {
- return [];
- }
- $headerIdx = $headerRowNum - 1;
- $dataStartIdx = $dataStartRowNum - 1;
- if (!isset($rows[$headerIdx])) {
- return [];
- }
- $headers = array_map(function ($cell) {
- return is_string($cell) ? trim($cell) : $cell;
- }, $rows[$headerIdx]);
- $data = [];
- $rowCount = count($rows);
- for ($r = $dataStartIdx; $r < $rowCount; $r++) {
- $row = $rows[$r];
- $hasCell = false;
- foreach ($row as $cell) {
- if ($cell !== null && $cell !== '') {
- $hasCell = true;
- break;
- }
- }
- if (!$hasCell) {
- continue;
- }
- $assoc = [];
- foreach ($headers as $i => $key) {
- if ($key === '' || $key === null) {
- continue;
- }
- $assoc[$key] = array_key_exists($i, $row) ? $row[$i] : null;
- }
- $data[] = $assoc;
- }
- return $data;
- }
- /**
- * 获取工单信息和工单制造工分
- * @param workorder 工单编号
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getWorkOrderInfo()
- {
- if (!$this->request->isGet()) {
- $this->error('请求方法错误');
- }
- $params = $this->request->param();
- if (empty($params['workorder'])) {
- $this->error('工单编号不能为空');
- }
- $where = [
- '订单编号' => $params['workorder'],
- 'Mod_rq' => null,
- ];
- $workOrderInfo = Db::table('工单_基本资料')
- ->where($where)
- ->field('订单编号,客户编号,生产款号,款式,落货日期,审核日期,接单日期,审核,订单数量,计划制造工分,Uniqid')
- ->find();
- if (empty($workOrderInfo)) {
- $this->error('工单不存在');
- }
- $this->success('成功', $workOrderInfo);
-
- }
- /**
- * 获取工单工艺信息
- * @param workorder 工单编号
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getWorkOrderProcess()
- {
- if (!$this->request->isGet()) {
- $this->error('请求方法错误');
- }
- $params = $this->request->param();
- if (empty($params['workorder'])) {
- $this->error('工单编号不能为空');
- }
- $where = [
- 'work_order' => $params['workorder'],
- 'del_rq' => null,
- ];
- $workOrderProcess = Db::table('工单_基础工艺资料')
- ->where($where)
- ->field('id,part_code as 部件编号,part_name as 部件名称,process_code as 工序编号,process_name as 工序名称,
- big_process as 大工序,standard_hour as 秒,standard_minutes as 分,standard_score as 定额分,money as 金额,
- coefficient as 难度系数,remark as 备注,status as 状态')
- ->select();
- if (empty($workOrderProcess)) {
- $this->error('工单工艺不存在');
- }
- $this->success('成功', $workOrderProcess);
- }
- /**
- * 修改工单计划制造工分
- * @param id 工单Uniqid或订单编号
- * @param number 计划制造工分
- * @param sys_id 操作人
- */
- public function updateWorkOrderPlanManufacture()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方法错误');
- }
- $params = $this->request->post();
- if (empty($params['id'])) {
- $this->error('工单ID不能为空');
- }
- if (!isset($params['number']) || $params['number'] === '') {
- $this->error('制造工分不能为空');
- }
- if (empty($params['sys_id'])) {
- $this->error('操作人不能为空');
- }
- $workOrder = Db::table('工单_基本资料')
- ->where('Uniqid|订单编号', $params['id'])
- ->where('Mod_rq', null)
- ->field('Uniqid,订单编号,计划制造工分')
- ->find();
- if (empty($workOrder)) {
- $this->error('工单不存在');
- }
- $oldValue = $workOrder['计划制造工分'];
- $newValue = $params['number'];
- if ((string)$oldValue === (string)$newValue) {
- $this->error('计划制造工分未发生变化');
- }
- $totalStandardScore = Db::table('工单_基础工艺资料')
- ->where('work_order', $workOrder['订单编号'])
- ->where('status', 0)
- ->whereNull('del_rq')
- ->sum('standard_score');
- $totalStandardScore = $totalStandardScore ? floatval($totalStandardScore) : 0;
- if ($totalStandardScore > floatval($newValue)) {
- $this->error('工序定额分和大于制造工分,请确认之后再修改');
- }
- $operTime = date('Y-m-d H:i:s');
- $logData = [
- 'order_no' => $workOrder['订单编号'],
- 'change_field' => '计划制造工分',
- 'old_value' => $oldValue === null ? '' : (string)$oldValue,
- 'new_value' => (string)$newValue,
- 'oper_type' => '修改工单计划制造工分',
- 'oper_user_name' => $params['sys_id'],
- 'oper_time' => $operTime,
- ];
- Db::startTrans();
- try {
- $updateResultSql = Db::table('工单_基本资料')
- ->where('Uniqid', $workOrder['Uniqid'])
- ->fetchSql(true)
- ->update(['计划制造工分' => $newValue]);
- $updateResult = Db::query($updateResultSql);
- if ($updateResult === false) {
- throw new \Exception('更新工单失败');
- }
- $logResult = Db::name('work_order_operation_log')->insert($logData);
- if ($logResult === false) {
- throw new \Exception('写入操作日志失败');
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error('修改失败');
- }
- $this->success('修改成功');
- }
-
- }
|