WorkOrderProcess.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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')
  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 部件编号,b.part_name as 部件名称,a.process_code as 工艺编号,
  238. a.process_name as 工艺名称,a.big_process as 大工艺,a.standard_hour as 标准工时,
  239. a.standard_score as 标准公分,a.remark as 备注,a.coefficient as 系数,a.sys_id as 系统人,a.sys_rq as 系统时间,a.mod_id as 修改人,
  240. a.mod_rq as 修改时间')
  241. ->order('process_code')
  242. ->select();
  243. $this->success('成功', $list);
  244. }
  245. /**
  246. * 获取部件列表
  247. * @param workorder 工单编号
  248. */
  249. public function getpartlist()
  250. {
  251. if(!$this->request->isGet()){
  252. $this->error('请求方法错误');
  253. }
  254. $params = $this->request->get();
  255. if (empty($params['workorder'])) {
  256. $this->error('工单编号不能为空');
  257. }
  258. $list = db('工单_部件资料')
  259. ->where('work_order', $params['workorder'])
  260. ->where('del_rq', null)
  261. ->field('part_code as 部件编号,part_name as 部件名称')
  262. ->order('part_code')
  263. ->select();
  264. if(empty($list)){
  265. $this->error('该工单没有部件,请先添加部件');
  266. }
  267. $this->success('成功', $list);
  268. }
  269. /**
  270. * 新增工单工艺资料
  271. * @param workorder 工单编号
  272. * @param part_code 部件编号
  273. * @param process_code 工艺编号
  274. * @param process_name 工艺名称
  275. * @param big_process 大工艺
  276. * @param standard_hour 标准工时
  277. * @param standard_score 标准公分
  278. * @param sys_id 系统人
  279. * @param coefficient 系数
  280. * @param remark 备注
  281. */
  282. public function AddProcess()
  283. {
  284. if (!$this->request->isPost()) {
  285. $this->error('请求方法错误');
  286. }
  287. $params = $this->request->post();
  288. if (empty($params['workorder'])) {
  289. $this->error('工单编号不能为空');
  290. }
  291. if (empty($params['part_code'])) {
  292. $this->error('部件编号不能为空');
  293. }
  294. if (empty($params['process_name'])) {
  295. $this->error('工艺名称不能为空');
  296. }
  297. if (empty($params['big_process'])) {
  298. $this->error('大工艺不能为空');
  299. }
  300. if (empty($params['standard_hour'])) {
  301. $this->error('标准工时不能为空');
  302. }
  303. if (empty($params['standard_score'])) {
  304. $this->error('标准公分不能为空');
  305. }
  306. if (empty($params['coefficient'])) {
  307. $params['coefficient'] = 1;
  308. }
  309. if (empty($params['sys_id'])) {
  310. $this->error('系统人不能为空');
  311. }
  312. // 检查是否传入了process_code
  313. if (!empty($params['process_code'])) {
  314. $code = $params['process_code'];
  315. // 检查是否存在冲突
  316. $exists = db('工单_基础工艺资料')
  317. ->where('work_order', $params['workorder'])
  318. ->where('part_code', $params['part_code'])
  319. ->where('process_code', '>=', $code)
  320. ->count();
  321. if ($exists > 0) {
  322. // 如果存在冲突,将后续编号自动加1
  323. db('工单_基础工艺资料')
  324. ->where('work_order', $params['workorder'])
  325. ->where('part_code', $params['part_code'])
  326. ->where('process_code', '>=', $code)
  327. ->setInc('process_code');
  328. }
  329. } else {
  330. // 如果没有传入process_code,按照原来的逻辑生成
  331. $code = db('工单_基础工艺资料')
  332. ->where('work_order', $params['workorder'])
  333. ->where('part_code', $params['part_code'])
  334. ->max('process_code');
  335. $code = $code ? $code + 1 : 1;
  336. }
  337. $data = [
  338. 'work_order' => $params['workorder'],
  339. 'part_code' => $params['part_code'],
  340. 'process_code' => $code,
  341. 'process_name' => $params['process_name'],
  342. 'big_process' => $params['big_process'],
  343. 'standard_hour' => $params['standard_hour'],
  344. 'standard_score' => $params['standard_score'],
  345. 'coefficient' => $params['coefficient'],
  346. 'remark' => $params['remark'],
  347. 'sys_id' => $params['sys_id'],
  348. 'sys_rq' => date('Y-m-d H:i:s'),
  349. ];
  350. $result = db('工单_基础工艺资料')->insert($data);
  351. if ($result === false) {
  352. $this->error('新增失败');
  353. }
  354. $this->success('新增成功');
  355. }
  356. /**
  357. * 更新工单工艺资料
  358. * @param id 工艺ID
  359. * @param mod_id 修改人
  360. * @param process_name 工艺名称
  361. * @param big_process 大工艺
  362. * @param standard_hour 标准工时
  363. * @param standard_score 标准公分
  364. * @param coefficient 系数
  365. * @param remark 备注
  366. */
  367. public function UpdateProcess()
  368. {
  369. if (!$this->request->isPost()) {
  370. $this->error('请求方法错误');
  371. }
  372. $params = $this->request->post();
  373. if (empty($params['id'])) {
  374. $this->error('工艺ID不能为空');
  375. }
  376. if (empty($params['mod_id'])) {
  377. $this->error('修改人不能为空');
  378. }
  379. if (empty($params['process_name'])) {
  380. $this->error('工艺名称不能为空');
  381. }
  382. if (empty($params['big_process'])) {
  383. $this->error('大工艺不能为空');
  384. }
  385. if (empty($params['standard_hour'])) {
  386. $this->error('标准工时不能为空');
  387. }
  388. if (empty($params['standard_score'])) {
  389. $this->error('标准公分不能为空');
  390. }
  391. if (!isset($params['coefficient']) || $params['coefficient'] === '') {
  392. $params['coefficient'] = 1;
  393. }
  394. $data = [
  395. 'process_name' => $params['process_name'],
  396. 'big_process' => $params['big_process'],
  397. 'standard_hour' => $params['standard_hour'],
  398. 'standard_score' => $params['standard_score'],
  399. 'coefficient' => $params['coefficient'],
  400. 'remark' => $params['remark'],
  401. 'mod_id' => $params['mod_id'],
  402. 'mod_rq' => date('Y-m-d H:i:s'),
  403. ];
  404. $result = db('工单_基础工艺资料')
  405. ->where('id', intval($params['id']))
  406. ->whereNull('del_rq')
  407. ->update($data);
  408. if ($result === false) {
  409. $this->error('更新失败');
  410. }
  411. if ($result === 0) {
  412. $this->error('未找到可更新的工艺或数据无变化');
  413. }
  414. $this->success('更新成功');
  415. }
  416. /**
  417. * 工单工艺资料软删除
  418. * @param id 工艺ID
  419. */
  420. public function DeleteProcess()
  421. {
  422. if (!$this->request->isPost()) {
  423. $this->error('请求方法错误');
  424. }
  425. $params = $this->request->post();
  426. if (empty($params['id'])) {
  427. $this->error('工艺ID不能为空');
  428. }
  429. $ids = explode(',', $params['id']);
  430. $ids = array_filter(array_map('intval', $ids));
  431. if (empty($ids)) {
  432. $this->error('无效的工艺ID');
  433. }
  434. $result = db('工单_基础工艺资料')
  435. ->whereIn('id', $ids)
  436. ->whereNull('del_rq')
  437. ->update(['del_rq' => date('Y-m-d H:i:s')]);
  438. if ($result === false) {
  439. $this->error('删除失败');
  440. }
  441. if ($result === 0) {
  442. $this->error('未找到可删除的工艺');
  443. }
  444. $this->success('删除成功');
  445. }
  446. /**
  447. * 工单工艺复制
  448. * @param workorder 工单编号
  449. * @param product_code 产品编号
  450. * @param sys_id 操作人
  451. * @return array
  452. * @throws \think\db\exception\DataNotFoundException
  453. * @throws \think\db\exception\ModelNotFoundException
  454. * @throws \think\exception\DbException
  455. * @throws \think\exception\PDOException
  456. */
  457. public function workorderprocessCopy()
  458. {
  459. if (!$this->request->isGet()) {
  460. $this->error('请求方法错误');
  461. }
  462. $params = $this->request->param();
  463. if (empty($params['workorder'])) {
  464. $this->error('工单编号不能为空');
  465. }
  466. if (empty($params['product_code'])) {
  467. $this->error('产品编号不能为空');
  468. }
  469. if (empty($params['sys_id'])) {
  470. $this->error('操作人不能为空');
  471. }
  472. $now = date('Y-m-d H:i:s');
  473. $productParts = db('产品_部件资料')
  474. ->where('product_code', $params['product_code'])
  475. ->where('mod_rq', null)
  476. ->field('part_sort as part_code,part_name')
  477. ->select();
  478. if (empty($productParts)) {
  479. $this->error('该产品没有有效的部件');
  480. }
  481. $productProcesses = db('产品_工艺资料')
  482. ->where('product_code', $params['product_code'])
  483. ->field('part_sort as part_code,part_name,gy_sort as process_code,gy_name as process_name,
  484. big_process,standard_hour,standard_score,difficulty_coef as coefficient')
  485. ->select();
  486. if (empty($productProcesses)) {
  487. $this->error('该产品没有工艺');
  488. }
  489. $workOrderParts = [];
  490. //配置工单部件数据
  491. foreach ($productParts as $value) {
  492. $workOrderParts[] = [
  493. 'work_order' => $params['workorder'],
  494. 'part_code' => $value['part_code'],
  495. 'part_name' => $value['part_name'],
  496. 'part_type' => '',
  497. 'remark' => '',
  498. 'status' => 1,
  499. 'sys_id' => $params['sys_id'],
  500. 'sys_rq' => $now,
  501. ];
  502. }
  503. $workOrderProcesses = [];
  504. //配置工单工艺数据
  505. foreach ($productProcesses as $value) {
  506. $workOrderProcesses[] = [
  507. 'work_order' => $params['workorder'],
  508. 'part_code' => $value['part_code'],
  509. 'part_name' => $value['part_name'],
  510. 'process_code' => $value['process_code'],
  511. 'process_name' => $value['process_name'],
  512. 'big_process' => $value['big_process'],
  513. 'standard_hour' => $value['standard_hour'],
  514. 'standard_score' => $value['standard_score'],
  515. 'coefficient' => $value['coefficient'],
  516. 'remark' => '',
  517. 'sys_id' => $params['sys_id'],
  518. 'sys_rq' => $now,
  519. ];
  520. }
  521. Db::startTrans();
  522. try {
  523. //删除数据库现有的工单部件数据
  524. Db::name('工单_部件资料')->where('work_order', $params['workorder'])->delete();
  525. //删除数据库现有的工单工艺数据
  526. Db::name('工单_基础工艺资料')->where('work_order', $params['workorder'])->delete();
  527. //插入工单部件数据
  528. $partInsertCount = Db::name('工单_部件资料')->insertAll($workOrderParts);
  529. if ($partInsertCount === false) {
  530. throw new \Exception('工单部件复制失败');
  531. }
  532. //插入工单工艺数据
  533. $processInsertCount = Db::name('工单_基础工艺资料')->insertAll($workOrderProcesses);
  534. if ($processInsertCount === false) {
  535. throw new \Exception('工单工艺复制失败');
  536. }
  537. //提交事务
  538. Db::commit();
  539. } catch (\Exception $e) {
  540. Db::rollback();
  541. $this->error('复制失败');
  542. }
  543. $this->success('复制成功');
  544. }
  545. /**
  546. * 查询产品类型
  547. * @ApiMethod (GET)
  548. * @param string $workorder 工单编号
  549. * @return array
  550. * @throws \think\db\exception\DataNotFoundException
  551. * @throws \think\db\exception\ModelNotFoundException
  552. * @throws \think\exception\DbException
  553. */
  554. public function getproducttype()
  555. {
  556. if(!$this->request->isGet()){
  557. $this->error('请求方法错误');
  558. }
  559. $params = $this->request->get();
  560. if (empty($params['product'])) {
  561. $this->error('产品类型不能为空');
  562. }
  563. $list = db('产品_基本资料')
  564. ->where('product_type', $params['product'])
  565. ->where('status', 1)
  566. ->field('product_code as 产品编号,product_type as 产品类型,product_name as 产品名称')
  567. ->select();
  568. if(empty($list)){
  569. $this->error('该产品类型没有产品');
  570. }
  571. $this->success('成功', $list);
  572. }
  573. /**
  574. * 工单工艺排序
  575. * @param workorder 工单编号
  576. * @param part_code 部件编号
  577. * @param process_code 工艺编号
  578. * @param process_name 工艺名称
  579. * @param big_process 大工艺
  580. * @param standard_hour 标准工时
  581. * @param standard_score 标准公分
  582. * @param coefficient 系数
  583. * @param remark 备注
  584. */
  585. public function sortProcess()
  586. {
  587. if (!$this->request->isPost()) {
  588. $this->error('请求方法错误');
  589. }
  590. $params = $this->request->post();
  591. if (empty($params)) {
  592. $this->error('参数不能为空');
  593. }
  594. if (!is_array($params)) {
  595. $this->error('参数格式错误');
  596. }
  597. Db::startTrans();
  598. try {
  599. foreach ($params as $value) {
  600. if (empty($value['id'])) {
  601. throw new \Exception('缺少工艺ID');
  602. }
  603. if (!isset($value['process_code']) || $value['process_code'] === '') {
  604. throw new \Exception('缺少工艺编号');
  605. }
  606. $result = db('工单_基础工艺资料')
  607. ->where('id', intval($value['id']))
  608. ->whereNull('del_rq')
  609. ->update(['process_code' => intval($value['process_code'])]);
  610. if ($result === false) {
  611. throw new \Exception('排序失败');
  612. }
  613. }
  614. Db::commit();
  615. } catch (\Exception $e) {
  616. Db::rollback();
  617. $this->error($e->getMessage());
  618. }
  619. $this->success('排序成功');
  620. }
  621. /**
  622. * 工单工艺excel导入
  623. * @param workorder 工单编号
  624. *
  625. */
  626. public function importProcess()
  627. {
  628. if (!$this->request->isPost()) {
  629. $this->error('请求方法错误');
  630. }
  631. $params = $this->request->post();
  632. if (empty($params['workorder'])) {
  633. $this->error('工单编号不能为空');
  634. }
  635. if (empty($params['sys_id'])) {
  636. $this->error('操作人不能为空');
  637. }
  638. $file = $this->request->file('file');
  639. if (!$file) {
  640. $this->error('文件不能为空');
  641. }
  642. $uploadDir = ROOT_PATH . 'public' . DS . 'uploads';
  643. $info = $file->validate(['size' => 1024 * 1024 * 10, 'ext' => 'xlsx,xls,csv,txt'])
  644. ->move($uploadDir);
  645. if (!$info) {
  646. $this->error($file->getError());
  647. }
  648. $filePath = $uploadDir . DS . $info->getSaveName();
  649. // 定额表:上方为标题与元数据,第 5 行表头,第 6 行起为数据
  650. $data = $this->readExcel($filePath, 5, 6);
  651. if (empty($data)) {
  652. $this->error('文件内容为空');
  653. }
  654. $seenSeq = [];
  655. foreach ($data as $row) {
  656. $seq = isset($row['序号']) ? $row['序号'] : null;
  657. if ($seq === null || $seq === '') {
  658. continue;
  659. }
  660. $seqKey = is_scalar($seq) ? (string)$seq : $seq;
  661. if (isset($seenSeq[$seqKey])) {
  662. $this->error('工序序号重复,请重新调整之后再上传');
  663. }
  664. $seenSeq[$seqKey] = true;
  665. }
  666. $now = date('Y-m-d H:i:s');
  667. $partNameToCode = [];
  668. $nextPartCode = 0;
  669. foreach ($data as $row) {
  670. $partName = isset($row['部件名称']) ? $row['部件名称'] : '';
  671. if ($partName === '' || isset($partNameToCode[$partName])) {
  672. continue;
  673. }
  674. $partNameToCode[$partName] = ++$nextPartCode;
  675. }
  676. $workOrderParts = [];
  677. $i = 0;
  678. foreach ($partNameToCode as $partName => $partCode) {
  679. $workOrderParts[$i++] = [
  680. 'work_order' => $params['workorder'],
  681. 'part_name' => $partName,
  682. 'part_code' => $partCode,
  683. 'part_type' => '',
  684. 'remark' => '',
  685. 'status' => 1,
  686. 'sys_id' => $params['sys_id'],
  687. 'sys_rq' => $now,
  688. ];
  689. }
  690. Db::startTrans();
  691. try {
  692. //删除数据库现有的工单部件数据
  693. Db::name('工单_部件资料')->where('work_order', $params['workorder'])->delete();
  694. //插入工单部件数据
  695. $partInsertCount = Db::name('工单_部件资料')->insertAll($workOrderParts);
  696. if ($partInsertCount === false) {
  697. throw new \Exception('工单部件导入失败');
  698. }
  699. //提交事务
  700. Db::commit();
  701. } catch (\Exception $e) {
  702. Db::rollback();
  703. $this->error($e->getMessage());
  704. }
  705. $workOrderProcesses = [];
  706. foreach ($data as $value) {
  707. $name = isset($value['部件名称']) ? $value['部件名称'] : '';
  708. $partCode = ($name !== '' && isset($partNameToCode[$name])) ? $partNameToCode[$name] : '';
  709. $workOrderProcesses[] = [
  710. 'work_order' => $params['workorder'],
  711. 'part_code' => $partCode,
  712. 'part_name' => $name,
  713. 'process_code' => $value['序号'],
  714. 'process_name' => $value['工序名称'],
  715. 'big_process' => $value['生产工序'],
  716. 'standard_hour' => $value['秒'],
  717. 'standard_minutes' => $value['分'],
  718. 'standard_score' => $value['定额分'],
  719. 'money' => $value['金额'],
  720. 'coefficient' => $value['难度系数'],
  721. 'remark' => isset($value['备注']) ? $value['备注'] : '',
  722. 'sys_id' => $params['sys_id'],
  723. 'sys_rq' => $now,
  724. ];
  725. }
  726. Db::startTrans();
  727. try {
  728. //删除数据库现有的工单工艺数据
  729. Db::name('工单_基础工艺资料')->where('work_order', $params['workorder'])->delete();
  730. //插入工单工艺数据
  731. $processInsertCount = Db::name('工单_基础工艺资料')->insertAll($workOrderProcesses);
  732. if ($processInsertCount === false) {
  733. throw new \Exception('工单工艺导入失败');
  734. }
  735. //提交事务
  736. Db::commit();
  737. } catch (\Exception $e) {
  738. Db::rollback();
  739. $this->error($e->getMessage());
  740. }
  741. $this->success('导入成功');
  742. }
  743. /**
  744. * 读取 Excel/CSV(PhpSpreadsheet)
  745. *
  746. * @param string $filePath
  747. * @param int $headerRowNum 表头所在行(1 起计,如定额表为第 5 行)
  748. * @param int $dataStartRowNum 首条数据行(1 起计,须大于表头行,如第 6 行)
  749. * @return array 每行一条关联数组,键为表头单元格文本
  750. */
  751. public function readExcel($filePath, $headerRowNum = 5, $dataStartRowNum = 6)
  752. {
  753. if (!is_file($filePath) || !is_readable($filePath)) {
  754. return [];
  755. }
  756. if ($dataStartRowNum <= $headerRowNum) {
  757. return [];
  758. }
  759. $spreadsheet = IOFactory::load($filePath);
  760. $sheet = $spreadsheet->getActiveSheet();
  761. $rows = $sheet->toArray();
  762. if (empty($rows)) {
  763. return [];
  764. }
  765. $headerIdx = $headerRowNum - 1;
  766. $dataStartIdx = $dataStartRowNum - 1;
  767. if (!isset($rows[$headerIdx])) {
  768. return [];
  769. }
  770. $headers = array_map(function ($cell) {
  771. return is_string($cell) ? trim($cell) : $cell;
  772. }, $rows[$headerIdx]);
  773. $data = [];
  774. $rowCount = count($rows);
  775. for ($r = $dataStartIdx; $r < $rowCount; $r++) {
  776. $row = $rows[$r];
  777. $hasCell = false;
  778. foreach ($row as $cell) {
  779. if ($cell !== null && $cell !== '') {
  780. $hasCell = true;
  781. break;
  782. }
  783. }
  784. if (!$hasCell) {
  785. continue;
  786. }
  787. $assoc = [];
  788. foreach ($headers as $i => $key) {
  789. if ($key === '' || $key === null) {
  790. continue;
  791. }
  792. $assoc[$key] = array_key_exists($i, $row) ? $row[$i] : null;
  793. }
  794. $data[] = $assoc;
  795. }
  796. return $data;
  797. }
  798. }