| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848 |
- <?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')
- ->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_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('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_score'])) {
- $this->error('标准公分不能为空');
- }
- if (!isset($params['coefficient']) || $params['coefficient'] === '') {
- $params['coefficient'] = 1;
- }
- $data = [
- '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'],
- 'mod_id' => $params['mod_id'],
- 'mod_rq' => date('Y-m-d H:i:s'),
- ];
- $result = db('工单_基础工艺资料')
- ->where('id', intval($params['id']))
- ->whereNull('del_rq')
- ->update($data);
- if ($result === false) {
- $this->error('更新失败');
- }
- if ($result === 0) {
- $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;
- }
- }
|