WorkOrderProcess.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use PhpOffice\PhpSpreadsheet\IOFactory;
  5. use think\Db;
  6. /**
  7. * 工单工艺管理
  8. *
  9. */
  10. class WorkOrderProcess extends Api
  11. {
  12. protected $noNeedLogin = ['*'];
  13. protected $noNeedRight = ['*'];
  14. /**
  15. * 工单大工序列表
  16. * @param workorder 工单编号
  17. */
  18. public function MajorprocessList()
  19. {
  20. if (!$this->request->isGet()) {
  21. $this->error('请求方法错误');
  22. }
  23. $params = $this->request->param();
  24. if (empty($params['workorder'])) {
  25. $this->error('工单编号不能为空');
  26. }
  27. $list = db('工单_工艺资料')
  28. ->where('订单编号', $params['workorder'])
  29. ->field('工序编号,工序名称,工序备注')
  30. ->distinct('工序名称')
  31. ->select();
  32. if(empty($list)){
  33. $this->error('该工单没有大工序');
  34. }
  35. $this->success('成功', $list);
  36. }
  37. /**
  38. * 工单部件列表
  39. * @param workorder 工单编号
  40. */
  41. public function PartList()
  42. {
  43. if (!$this->request->isGet()) {
  44. $this->error('请求方法错误');
  45. }
  46. $params = $this->request->param();
  47. if (empty($params['workorder'])) {
  48. $this->error('工单编号不能为空');
  49. }
  50. $list = db('工单_部件资料')
  51. ->where('work_order', $params['workorder'])
  52. ->where('del_rq', null)
  53. ->field('part_code as 部件编号,part_name as 部件名称,remark as 部件备注,part_type as 部件类型,status as 状态,
  54. sys_id as 操作人,sys_rq as 操作时间,mod_rq as 修改时间,mod_id as 修改人,id as 部件ID')
  55. ->order('part_code')
  56. ->select();
  57. if(empty($list)){
  58. // $this->error('该工单没有部件,请先添加部件');
  59. }
  60. $this->success('成功', $list);
  61. }
  62. /**
  63. * 新增工单部件
  64. * @param workorder 工单编号
  65. * @param part_code 部件编号
  66. * @param part_name 部件名称
  67. * @param remark 部件备注
  68. * @param part_type 部件类型
  69. * @param sys_id 操作人
  70. */
  71. public function AddPart()
  72. {
  73. if (!$this->request->isPost()) {
  74. $this->error('请求方法错误');
  75. }
  76. $params = $this->request->post();
  77. if (empty($params['workorder'])) {
  78. $this->error('工单编号不能为空');
  79. }
  80. if (empty($params['part_name'])) {
  81. $this->error('部件名称不能为空');
  82. }
  83. // 检查是否传入了part_code
  84. if (!empty($params['part_code'])) {
  85. $code = $params['part_code'];
  86. // 检查是否存在冲突
  87. $exists = db('工单_部件资料')
  88. ->where('work_order', $params['workorder'])
  89. ->where('part_code', '>=', $code)
  90. ->count();
  91. if ($exists > 0) {
  92. // 如果存在冲突,将后续编号自动加1
  93. db('工单_部件资料')
  94. ->where('work_order', $params['workorder'])
  95. ->where('part_code', '>=', $code)
  96. ->setInc('part_code');
  97. }
  98. } else {
  99. // 如果没有传入part_code,按照原来的逻辑生成
  100. $code = db('工单_部件资料')
  101. ->where('work_order', $params['workorder'])
  102. ->max('part_code');
  103. $code = $code ? $code + 1 : 1;
  104. }
  105. $data = [
  106. 'work_order' => $params['workorder'],
  107. 'part_code' => $code,
  108. 'part_name' => $params['part_name'],
  109. 'remark' => $params['remark'],
  110. 'part_type' => $params['part_type'],
  111. 'status' => 1,
  112. 'sys_id' => $params['sys_id'],
  113. 'sys_rq' => date('Y-m-d H:i:s'),
  114. ];
  115. $result = db('工单_部件资料')->insert($data);
  116. if (!$result) {
  117. $this->error('新增失败');
  118. }
  119. $this->success('新增成功');
  120. }
  121. /**
  122. * 修改部件状态
  123. * @param id 部件ID
  124. * @param status 状态
  125. */
  126. public function UpdatePartStatus()
  127. {
  128. if (!$this->request->isPost()) {
  129. $this->error('请求方法错误');
  130. }
  131. $params = $this->request->post();
  132. if (empty($params['id'])) {
  133. $this->error('部件ID不能为空');
  134. }
  135. if (!isset($params['status'])) {
  136. $this->error('状态不能为空');
  137. }
  138. $result = db('工单_部件资料')
  139. ->where('id', $params['id'])
  140. ->update(['status' => $params['status']]);
  141. if (!$result) {
  142. $this->error('修改失败');
  143. }
  144. $this->success('修改成功');
  145. }
  146. /**
  147. * 修改部件信息
  148. * @param workorder 工单编号
  149. * @param part_code 部件编号
  150. * @param part_name 部件名称
  151. * @param remark 部件备注
  152. * @param part_type 部件类型
  153. */
  154. public function UpdatePartInfo()
  155. {
  156. if (!$this->request->isPost()) {
  157. $this->error('请求方法错误');
  158. }
  159. $params = $this->request->post();
  160. if (empty($params['part_name'])) {
  161. $this->error('部件名称不能为空');
  162. }
  163. if (empty($params['remark'])) {
  164. $this->error('部件备注不能为空');
  165. }
  166. if (empty($params['part_type'])) {
  167. $this->error('部件类型不能为空');
  168. }
  169. if(empty($params['mod_id'])){
  170. $this->error('修改人不能为空');
  171. }
  172. if(empty($params['id'])){
  173. $this->error('部件ID不能为空');
  174. }
  175. $result = db('工单_部件资料')
  176. ->where('id', $params['id'])
  177. ->update([
  178. 'part_name' => $params['part_name'],
  179. 'remark' => $params['remark'],
  180. 'part_type' => $params['part_type'],
  181. 'mod_id' => $params['mod_id'],
  182. 'mod_rq' => date('Y-m-d H:i:s'),
  183. ]);
  184. if (!$result) {
  185. $this->error('修改失败');
  186. }
  187. $this->success('修改成功');
  188. }
  189. /** 软删除部件信息
  190. * @param id 部件ID
  191. */
  192. public function DeletePart()
  193. {
  194. if (!$this->request->isPost()) {
  195. $this->error('请求方法错误');
  196. }
  197. $params = $this->request->post();
  198. if (empty($params['id'])) {
  199. $this->error('部件ID不能为空');
  200. }
  201. $ids = explode(',', $params['id']);
  202. $ids = array_filter(array_map('intval', $ids));
  203. if (empty($ids)) {
  204. $this->error('无效的部件ID');
  205. }
  206. $result = db('工单_部件资料')
  207. ->whereIn('id', $ids)
  208. ->whereNull('del_rq')
  209. ->update(['del_rq' => date('Y-m-d H:i:s')]);
  210. if ($result === false) {
  211. $this->error('删除失败');
  212. }
  213. if ($result === 0) {
  214. $this->error('未找到可删除的部件');
  215. }
  216. $this->success('删除成功');
  217. }
  218. /**
  219. * 工单工艺列表
  220. * @param workorder 工单编号
  221. */
  222. public function GetProcessList()
  223. {
  224. if (!$this->request->isGet()) {
  225. $this->error('请求方法错误');
  226. }
  227. $params = $this->request->param();
  228. if (empty($params['workorder'])) {
  229. $this->error('工单编号不能为空');
  230. }
  231. $list = db('工单_基础工艺资料')
  232. ->alias('a')
  233. ->join('工单_部件资料 b', 'a.part_code = b.part_code and a.work_order = b.work_order', 'LEFT')
  234. ->where('a.del_rq', null)
  235. ->where('a.work_order', $params['workorder'])
  236. ->where('b.del_rq', null)
  237. ->field('a.id,a.part_code as 部件编号,
  238. IFNULL(b.part_name, "") as 部件名称,
  239. a.process_code as 工艺编号,
  240. a.process_name as 工艺名称,
  241. a.big_process as 大工艺,
  242. a.standard_hour as 标准工时,
  243. a.standard_score as 标准公分,
  244. a.remark as 备注,
  245. a.coefficient as 系数,
  246. a.sys_id as 系统人,
  247. a.sys_rq as 系统时间,
  248. a.mod_id as 修改人,
  249. a.mod_rq as 修改时间')
  250. ->order('a.id')
  251. ->select();
  252. $this->success('成功', $list);
  253. }
  254. /**
  255. * 获取部件列表
  256. * @param workorder 工单编号
  257. */
  258. public function getpartlist()
  259. {
  260. if(!$this->request->isGet()){
  261. $this->error('请求方法错误');
  262. }
  263. $params = $this->request->get();
  264. if (empty($params['workorder'])) {
  265. $this->error('工单编号不能为空');
  266. }
  267. $list = db('工单_部件资料')
  268. ->where('work_order', $params['workorder'])
  269. ->where('del_rq', null)
  270. ->field('part_code as 部件编号,part_name as 部件名称')
  271. ->order('part_code')
  272. ->select();
  273. if(empty($list)){
  274. $this->error('该工单没有部件,请先添加部件');
  275. }
  276. $this->success('成功', $list);
  277. }
  278. /**
  279. * 新增工单工艺资料
  280. * @param workorder 工单编号
  281. * @param part_code 部件编号
  282. * @param process_code 工艺编号
  283. * @param process_name 工艺名称
  284. * @param big_process 大工艺
  285. * @param standard_hour 标准工时
  286. * @param standard_score 标准公分
  287. * @param sys_id 系统人
  288. * @param coefficient 系数
  289. * @param remark 备注
  290. */
  291. public function AddProcess()
  292. {
  293. if (!$this->request->isPost()) {
  294. $this->error('请求方法错误');
  295. }
  296. $params = $this->request->post();
  297. if (empty($params['workorder'])) {
  298. $this->error('工单编号不能为空');
  299. }
  300. if (empty($params['part_code'])) {
  301. $this->error('部件编号不能为空');
  302. }
  303. if (empty($params['process_name'])) {
  304. $this->error('工艺名称不能为空');
  305. }
  306. if (empty($params['big_process'])) {
  307. $this->error('大工艺不能为空');
  308. }
  309. if (empty($params['standard_hour'])) {
  310. $this->error('标准工时不能为空');
  311. }
  312. if (empty($params['standard_score'])) {
  313. $this->error('标准公分不能为空');
  314. }
  315. if (empty($params['coefficient'])) {
  316. $params['coefficient'] = 1;
  317. }
  318. if (empty($params['sys_id'])) {
  319. $this->error('系统人不能为空');
  320. }
  321. // 检查是否传入了process_code
  322. if (!empty($params['process_code'])) {
  323. $code = $params['process_code'];
  324. // 检查是否存在冲突
  325. $exists = db('工单_基础工艺资料')
  326. ->where('work_order', $params['workorder'])
  327. ->where('part_code', $params['part_code'])
  328. ->where('process_code', '>=', $code)
  329. ->count();
  330. if ($exists > 0) {
  331. // 如果存在冲突,将后续编号自动加1
  332. db('工单_基础工艺资料')
  333. ->where('work_order', $params['workorder'])
  334. ->where('part_code', $params['part_code'])
  335. ->where('process_code', '>=', $code)
  336. ->setInc('process_code');
  337. }
  338. } else {
  339. // 如果没有传入process_code,按照原来的逻辑生成
  340. $code = db('工单_基础工艺资料')
  341. ->where('work_order', $params['workorder'])
  342. ->where('part_code', $params['part_code'])
  343. ->max('process_code');
  344. $code = $code ? $code + 1 : 1;
  345. }
  346. $data = [
  347. 'work_order' => $params['workorder'],
  348. 'part_code' => $params['part_code'],
  349. 'process_code' => $code,
  350. 'process_name' => $params['process_name'],
  351. 'big_process' => $params['big_process'],
  352. 'standard_hour' => $params['standard_hour'],
  353. 'standard_score' => $params['standard_score'],
  354. 'coefficient' => $params['coefficient'],
  355. 'remark' => $params['remark'],
  356. 'sys_id' => $params['sys_id'],
  357. 'sys_rq' => date('Y-m-d H:i:s'),
  358. ];
  359. $result = db('工单_基础工艺资料')->insert($data);
  360. if ($result === false) {
  361. $this->error('新增失败');
  362. }
  363. $this->success('新增成功');
  364. }
  365. /**
  366. * 更新工单工艺资料
  367. * @param id 工艺ID
  368. * @param mod_id 修改人
  369. * @param process_name 工艺名称
  370. * @param big_process 大工艺
  371. * @param standard_hour 标准工时
  372. * @param standard_score 标准公分
  373. * @param coefficient 系数
  374. * @param remark 备注
  375. */
  376. public function UpdateProcess()
  377. {
  378. if (!$this->request->isPost()) {
  379. $this->error('请求方法错误');
  380. }
  381. $params = $this->request->post();
  382. if (empty($params['id'])) {
  383. $this->error('工艺ID不能为空');
  384. }
  385. if (empty($params['mod_id'])) {
  386. $this->error('修改人不能为空');
  387. }
  388. if (empty($params['process_name'])) {
  389. $this->error('工艺名称不能为空');
  390. }
  391. if (empty($params['big_process'])) {
  392. $this->error('大工艺不能为空');
  393. }
  394. if (empty($params['standard_hour'])) {
  395. $this->error('标准工时不能为空');
  396. }
  397. if (empty($params['standard_score'])) {
  398. $this->error('标准公分不能为空');
  399. }
  400. if (!isset($params['coefficient']) || $params['coefficient'] === '') {
  401. $params['coefficient'] = 1;
  402. }
  403. $data = [
  404. 'process_name' => $params['process_name'],
  405. 'big_process' => $params['big_process'],
  406. 'standard_hour' => $params['standard_hour'],
  407. 'standard_score' => $params['standard_score'],
  408. 'coefficient' => $params['coefficient'],
  409. 'remark' => $params['remark'],
  410. 'mod_id' => $params['mod_id'],
  411. 'mod_rq' => date('Y-m-d H:i:s'),
  412. ];
  413. $result = db('工单_基础工艺资料')
  414. ->where('id', intval($params['id']))
  415. ->whereNull('del_rq')
  416. ->update($data);
  417. if ($result === false) {
  418. $this->error('更新失败');
  419. }
  420. if ($result === 0) {
  421. $this->error('未找到可更新的工艺或数据无变化');
  422. }
  423. $this->success('更新成功');
  424. }
  425. /**
  426. * 工单工艺资料软删除
  427. * @param id 工艺ID
  428. */
  429. public function DeleteProcess()
  430. {
  431. if (!$this->request->isPost()) {
  432. $this->error('请求方法错误');
  433. }
  434. $params = $this->request->post();
  435. if (empty($params['id'])) {
  436. $this->error('工艺ID不能为空');
  437. }
  438. $ids = explode(',', $params['id']);
  439. $ids = array_filter(array_map('intval', $ids));
  440. if (empty($ids)) {
  441. $this->error('无效的工艺ID');
  442. }
  443. $result = db('工单_基础工艺资料')
  444. ->whereIn('id', $ids)
  445. ->whereNull('del_rq')
  446. ->update(['del_rq' => date('Y-m-d H:i:s')]);
  447. if ($result === false) {
  448. $this->error('删除失败');
  449. }
  450. if ($result === 0) {
  451. $this->error('未找到可删除的工艺');
  452. }
  453. $this->success('删除成功');
  454. }
  455. /**
  456. * 工单工艺复制
  457. * @param workorder 工单编号
  458. * @param product_code 产品编号
  459. * @param sys_id 操作人
  460. * @return array
  461. * @throws \think\db\exception\DataNotFoundException
  462. * @throws \think\db\exception\ModelNotFoundException
  463. * @throws \think\exception\DbException
  464. * @throws \think\exception\PDOException
  465. */
  466. public function workorderprocessCopy()
  467. {
  468. if (!$this->request->isGet()) {
  469. $this->error('请求方法错误');
  470. }
  471. $params = $this->request->param();
  472. if (empty($params['workorder'])) {
  473. $this->error('工单编号不能为空');
  474. }
  475. if (empty($params['product_code'])) {
  476. $this->error('产品编号不能为空');
  477. }
  478. if (empty($params['sys_id'])) {
  479. $this->error('操作人不能为空');
  480. }
  481. $now = date('Y-m-d H:i:s');
  482. $productParts = db('产品_部件资料')
  483. ->where('product_code', $params['product_code'])
  484. ->where('mod_rq', null)
  485. ->field('part_sort as part_code,part_name')
  486. ->select();
  487. if (empty($productParts)) {
  488. $this->error('该产品没有有效的部件');
  489. }
  490. $productProcesses = db('产品_工艺资料')
  491. ->where('product_code', $params['product_code'])
  492. ->field('part_sort as part_code,part_name,gy_sort as process_code,gy_name as process_name,
  493. big_process,standard_hour,standard_score,difficulty_coef as coefficient')
  494. ->select();
  495. if (empty($productProcesses)) {
  496. $this->error('该产品没有工艺');
  497. }
  498. $workOrderParts = [];
  499. //配置工单部件数据
  500. foreach ($productParts as $value) {
  501. $workOrderParts[] = [
  502. 'work_order' => $params['workorder'],
  503. 'part_code' => $value['part_code'],
  504. 'part_name' => $value['part_name'],
  505. 'part_type' => '',
  506. 'remark' => '',
  507. 'status' => 1,
  508. 'sys_id' => $params['sys_id'],
  509. 'sys_rq' => $now,
  510. ];
  511. }
  512. $workOrderProcesses = [];
  513. //配置工单工艺数据
  514. foreach ($productProcesses as $value) {
  515. $workOrderProcesses[] = [
  516. 'work_order' => $params['workorder'],
  517. 'part_code' => $value['part_code'],
  518. 'part_name' => $value['part_name'],
  519. 'process_code' => $value['process_code'],
  520. 'process_name' => $value['process_name'],
  521. 'big_process' => $value['big_process'],
  522. 'standard_hour' => $value['standard_hour'],
  523. 'standard_score' => $value['standard_score'],
  524. 'coefficient' => $value['coefficient'],
  525. 'remark' => '',
  526. 'sys_id' => $params['sys_id'],
  527. 'sys_rq' => $now,
  528. ];
  529. }
  530. Db::startTrans();
  531. try {
  532. //删除数据库现有的工单部件数据
  533. Db::name('工单_部件资料')->where('work_order', $params['workorder'])->delete();
  534. //删除数据库现有的工单工艺数据
  535. Db::name('工单_基础工艺资料')->where('work_order', $params['workorder'])->delete();
  536. //插入工单部件数据
  537. $partInsertCount = Db::name('工单_部件资料')->insertAll($workOrderParts);
  538. if ($partInsertCount === false) {
  539. throw new \Exception('工单部件复制失败');
  540. }
  541. //插入工单工艺数据
  542. $processInsertCount = Db::name('工单_基础工艺资料')->insertAll($workOrderProcesses);
  543. if ($processInsertCount === false) {
  544. throw new \Exception('工单工艺复制失败');
  545. }
  546. //提交事务
  547. Db::commit();
  548. } catch (\Exception $e) {
  549. Db::rollback();
  550. $this->error('复制失败');
  551. }
  552. $this->success('复制成功');
  553. }
  554. /**
  555. * 查询产品类型
  556. * @ApiMethod (GET)
  557. * @param string $workorder 工单编号
  558. * @return array
  559. * @throws \think\db\exception\DataNotFoundException
  560. * @throws \think\db\exception\ModelNotFoundException
  561. * @throws \think\exception\DbException
  562. */
  563. public function getproducttype()
  564. {
  565. if(!$this->request->isGet()){
  566. $this->error('请求方法错误');
  567. }
  568. $params = $this->request->get();
  569. if (empty($params['product'])) {
  570. $this->error('产品类型不能为空');
  571. }
  572. $list = db('产品_基本资料')
  573. ->where('product_type', $params['product'])
  574. ->where('status', 1)
  575. ->field('product_code as 产品编号,product_type as 产品类型,product_name as 产品名称')
  576. ->select();
  577. if(empty($list)){
  578. $this->error('该产品类型没有产品');
  579. }
  580. $this->success('成功', $list);
  581. }
  582. /**
  583. * 工单工艺排序
  584. * @param workorder 工单编号
  585. * @param part_code 部件编号
  586. * @param process_code 工艺编号
  587. * @param process_name 工艺名称
  588. * @param big_process 大工艺
  589. * @param standard_hour 标准工时
  590. * @param standard_score 标准公分
  591. * @param coefficient 系数
  592. * @param remark 备注
  593. */
  594. public function sortProcess()
  595. {
  596. if (!$this->request->isPost()) {
  597. $this->error('请求方法错误');
  598. }
  599. $params = $this->request->post();
  600. if (empty($params)) {
  601. $this->error('参数不能为空');
  602. }
  603. if (!is_array($params)) {
  604. $this->error('参数格式错误');
  605. }
  606. Db::startTrans();
  607. try {
  608. foreach ($params as $value) {
  609. if (empty($value['id'])) {
  610. throw new \Exception('缺少工艺ID');
  611. }
  612. if (!isset($value['process_code']) || $value['process_code'] === '') {
  613. throw new \Exception('缺少工艺编号');
  614. }
  615. $result = db('工单_基础工艺资料')
  616. ->where('id', intval($value['id']))
  617. ->whereNull('del_rq')
  618. ->update(['process_code' => intval($value['process_code'])]);
  619. if ($result === false) {
  620. throw new \Exception('排序失败');
  621. }
  622. }
  623. Db::commit();
  624. } catch (\Exception $e) {
  625. Db::rollback();
  626. $this->error($e->getMessage());
  627. }
  628. $this->success('排序成功');
  629. }
  630. /**
  631. * 工单工艺excel导入
  632. * @param workorder 工单编号
  633. *
  634. */
  635. public function importProcess()
  636. {
  637. if (!$this->request->isPost()) {
  638. $this->error('请求方法错误');
  639. }
  640. $params = $this->request->post();
  641. if (empty($params['workorder'])) {
  642. $this->error('工单编号不能为空');
  643. }
  644. if (empty($params['sys_id'])) {
  645. $this->error('操作人不能为空');
  646. }
  647. $file = $this->request->file('file');
  648. if (!$file) {
  649. $this->error('文件不能为空');
  650. }
  651. $uploadDir = ROOT_PATH . 'public' . DS . 'uploads';
  652. $info = $file->validate(['size' => 1024 * 1024 * 10, 'ext' => 'xlsx,xls,csv,txt'])
  653. ->move($uploadDir);
  654. if (!$info) {
  655. $this->error($file->getError());
  656. }
  657. $filePath = $uploadDir . DS . $info->getSaveName();
  658. // 定额表:上方为标题与元数据,第 5 行表头,第 6 行起为数据
  659. $data = $this->readExcel($filePath, 5, 6);
  660. if (empty($data)) {
  661. $this->error('文件内容为空');
  662. }
  663. $seenSeq = [];
  664. foreach ($data as $row) {
  665. $seq = isset($row['序号']) ? $row['序号'] : null;
  666. if ($seq === null || $seq === '') {
  667. continue;
  668. }
  669. $seqKey = is_scalar($seq) ? (string)$seq : $seq;
  670. if (isset($seenSeq[$seqKey])) {
  671. $this->error('工序序号重复,请重新调整之后再上传');
  672. }
  673. $seenSeq[$seqKey] = true;
  674. }
  675. $now = date('Y-m-d H:i:s');
  676. $partNameToCode = [];
  677. $nextPartCode = 0;
  678. foreach ($data as $row) {
  679. $partName = isset($row['部件名称']) ? $row['部件名称'] : '';
  680. if ($partName === '' || isset($partNameToCode[$partName])) {
  681. continue;
  682. }
  683. $partNameToCode[$partName] = ++$nextPartCode;
  684. }
  685. $workOrderParts = [];
  686. $i = 0;
  687. foreach ($partNameToCode as $partName => $partCode) {
  688. $workOrderParts[$i++] = [
  689. 'work_order' => $params['workorder'],
  690. 'part_name' => $partName,
  691. 'part_code' => $partCode,
  692. 'part_type' => '',
  693. 'remark' => '',
  694. 'status' => 1,
  695. 'sys_id' => $params['sys_id'],
  696. 'sys_rq' => $now,
  697. ];
  698. }
  699. Db::startTrans();
  700. try {
  701. //删除数据库现有的工单部件数据
  702. Db::name('工单_部件资料')->where('work_order', $params['workorder'])->delete();
  703. //插入工单部件数据
  704. $partInsertCount = Db::name('工单_部件资料')->insertAll($workOrderParts);
  705. if ($partInsertCount === false) {
  706. throw new \Exception('工单部件导入失败');
  707. }
  708. //提交事务
  709. Db::commit();
  710. } catch (\Exception $e) {
  711. Db::rollback();
  712. $this->error($e->getMessage());
  713. }
  714. $workOrderProcesses = [];
  715. foreach ($data as $value) {
  716. $name = isset($value['部件名称']) ? $value['部件名称'] : '';
  717. $partCode = ($name !== '' && isset($partNameToCode[$name])) ? $partNameToCode[$name] : '';
  718. $workOrderProcesses[] = [
  719. 'work_order' => $params['workorder'],
  720. 'part_code' => $partCode,
  721. 'part_name' => $name,
  722. 'process_code' => $value['序号'],
  723. 'process_name' => $value['工序名称'],
  724. 'big_process' => $value['生产工序'],
  725. 'standard_hour' => $value['秒'],
  726. 'standard_minutes' => $value['分'],
  727. 'standard_score' => $value['定额分'],
  728. 'money' => $value['金额'],
  729. 'coefficient' => $value['难度系数'],
  730. 'remark' => isset($value['备注']) ? $value['备注'] : '',
  731. 'sys_id' => $params['sys_id'],
  732. 'sys_rq' => $now,
  733. ];
  734. }
  735. Db::startTrans();
  736. try {
  737. //删除数据库现有的工单工艺数据
  738. Db::name('工单_基础工艺资料')->where('work_order', $params['workorder'])->delete();
  739. //插入工单工艺数据
  740. $processInsertCount = Db::name('工单_基础工艺资料')->insertAll($workOrderProcesses);
  741. if ($processInsertCount === false) {
  742. throw new \Exception('工单工艺导入失败');
  743. }
  744. //提交事务
  745. Db::commit();
  746. } catch (\Exception $e) {
  747. Db::rollback();
  748. $this->error($e->getMessage());
  749. }
  750. $this->success('导入成功');
  751. }
  752. /**
  753. * 读取 Excel/CSV(PhpSpreadsheet)
  754. *
  755. * @param string $filePath
  756. * @param int $headerRowNum 表头所在行(1 起计,如定额表为第 5 行)
  757. * @param int $dataStartRowNum 首条数据行(1 起计,须大于表头行,如第 6 行)
  758. * @return array 每行一条关联数组,键为表头单元格文本
  759. */
  760. public function readExcel($filePath, $headerRowNum = 5, $dataStartRowNum = 6)
  761. {
  762. if (!is_file($filePath) || !is_readable($filePath)) {
  763. return [];
  764. }
  765. if ($dataStartRowNum <= $headerRowNum) {
  766. return [];
  767. }
  768. $spreadsheet = IOFactory::load($filePath);
  769. $sheet = $spreadsheet->getActiveSheet();
  770. $rows = $sheet->toArray();
  771. if (empty($rows)) {
  772. return [];
  773. }
  774. $headerIdx = $headerRowNum - 1;
  775. $dataStartIdx = $dataStartRowNum - 1;
  776. if (!isset($rows[$headerIdx])) {
  777. return [];
  778. }
  779. $headers = array_map(function ($cell) {
  780. return is_string($cell) ? trim($cell) : $cell;
  781. }, $rows[$headerIdx]);
  782. $data = [];
  783. $rowCount = count($rows);
  784. for ($r = $dataStartIdx; $r < $rowCount; $r++) {
  785. $row = $rows[$r];
  786. $hasCell = false;
  787. foreach ($row as $cell) {
  788. if ($cell !== null && $cell !== '') {
  789. $hasCell = true;
  790. break;
  791. }
  792. }
  793. if (!$hasCell) {
  794. continue;
  795. }
  796. $assoc = [];
  797. foreach ($headers as $i => $key) {
  798. if ($key === '' || $key === null) {
  799. continue;
  800. }
  801. $assoc[$key] = array_key_exists($i, $row) ? $row[$i] : null;
  802. }
  803. $data[] = $assoc;
  804. }
  805. return $data;
  806. }
  807. }