WorkOrderProcess.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 工单工艺管理
  7. *
  8. */
  9. class WorkOrderProcess extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 工单大工序列表
  15. * @param workorder 工单编号
  16. */
  17. public function MajorprocessList()
  18. {
  19. if (!$this->request->isGet()) {
  20. $this->error('请求方法错误');
  21. }
  22. $params = $this->request->param();
  23. if (empty($params['workorder'])) {
  24. $this->error('工单编号不能为空');
  25. }
  26. $list = db('工单_工艺资料')
  27. ->where('订单编号', $params['workorder'])
  28. ->field('工序编号,工序名称,工序备注')
  29. ->distinct('工序名称')
  30. ->select();
  31. if(empty($list)){
  32. $this->error('该工单没有大工序');
  33. }
  34. $this->success('成功', $list);
  35. }
  36. /**
  37. * 工单部件列表
  38. * @param workorder 工单编号
  39. */
  40. public function PartList()
  41. {
  42. if (!$this->request->isGet()) {
  43. $this->error('请求方法错误');
  44. }
  45. $params = $this->request->param();
  46. if (empty($params['workorder'])) {
  47. $this->error('工单编号不能为空');
  48. }
  49. $list = db('工单_部件资料')
  50. ->where('work_order', $params['workorder'])
  51. ->where('del_rq', null)
  52. ->field('part_code as 部件编号,part_name as 部件名称,remark as 部件备注,part_type as 部件类型,status as 状态,
  53. sys_id as 操作人,sys_rq as 操作时间,mod_rq as 修改时间,mod_id as 修改人,id as 部件ID')
  54. ->order('part_code')
  55. ->select();
  56. if(empty($list)){
  57. $this->error('该工单没有部件,请先添加部件');
  58. }
  59. $this->success('成功', $list);
  60. }
  61. /**
  62. * 新增工单部件
  63. * @param workorder 工单编号
  64. * @param part_code 部件编号
  65. * @param part_name 部件名称
  66. * @param remark 部件备注
  67. * @param part_type 部件类型
  68. * @param sys_id 操作人
  69. */
  70. public function AddPart()
  71. {
  72. if (!$this->request->isPost()) {
  73. $this->error('请求方法错误');
  74. }
  75. $params = $this->request->post();
  76. if (empty($params['workorder'])) {
  77. $this->error('工单编号不能为空');
  78. }
  79. if (empty($params['part_name'])) {
  80. $this->error('部件名称不能为空');
  81. }
  82. // 检查是否传入了part_code
  83. if (!empty($params['part_code'])) {
  84. $code = $params['part_code'];
  85. // 检查是否存在冲突
  86. $exists = db('工单_部件资料')
  87. ->where('work_order', $params['workorder'])
  88. ->where('part_code', '>=', $code)
  89. ->count();
  90. if ($exists > 0) {
  91. // 如果存在冲突,将后续编号自动加1
  92. db('工单_部件资料')
  93. ->where('work_order', $params['workorder'])
  94. ->where('part_code', '>=', $code)
  95. ->setInc('part_code');
  96. }
  97. } else {
  98. // 如果没有传入part_code,按照原来的逻辑生成
  99. $code = db('工单_部件资料')
  100. ->where('work_order', $params['workorder'])
  101. ->max('part_code');
  102. $code = $code ? $code + 1 : 1;
  103. }
  104. $data = [
  105. 'work_order' => $params['workorder'],
  106. 'part_code' => $code,
  107. 'part_name' => $params['part_name'],
  108. 'remark' => $params['remark'],
  109. 'part_type' => $params['part_type'],
  110. 'status' => 1,
  111. 'sys_id' => $params['sys_id'],
  112. 'sys_rq' => date('Y-m-d H:i:s'),
  113. ];
  114. $result = db('工单_部件资料')->insert($data);
  115. if (!$result) {
  116. $this->error('新增失败');
  117. }
  118. $this->success('新增成功');
  119. }
  120. /**
  121. * 修改部件状态
  122. * @param id 部件ID
  123. * @param status 状态
  124. */
  125. public function UpdatePartStatus()
  126. {
  127. if (!$this->request->isPost()) {
  128. $this->error('请求方法错误');
  129. }
  130. $params = $this->request->post();
  131. if (empty($params['id'])) {
  132. $this->error('部件ID不能为空');
  133. }
  134. if (!isset($params['status'])) {
  135. $this->error('状态不能为空');
  136. }
  137. $result = db('工单_部件资料')
  138. ->where('id', $params['id'])
  139. ->update(['status' => $params['status']]);
  140. if (!$result) {
  141. $this->error('修改失败');
  142. }
  143. $this->success('修改成功');
  144. }
  145. /**
  146. * 修改部件信息
  147. * @param workorder 工单编号
  148. * @param part_code 部件编号
  149. * @param part_name 部件名称
  150. * @param remark 部件备注
  151. * @param part_type 部件类型
  152. */
  153. public function UpdatePartInfo()
  154. {
  155. if (!$this->request->isPost()) {
  156. $this->error('请求方法错误');
  157. }
  158. $params = $this->request->post();
  159. if (empty($params['part_name'])) {
  160. $this->error('部件名称不能为空');
  161. }
  162. if (empty($params['remark'])) {
  163. $this->error('部件备注不能为空');
  164. }
  165. if (empty($params['part_type'])) {
  166. $this->error('部件类型不能为空');
  167. }
  168. if(empty($params['mod_id'])){
  169. $this->error('修改人不能为空');
  170. }
  171. if(empty($params['id'])){
  172. $this->error('部件ID不能为空');
  173. }
  174. $result = db('工单_部件资料')
  175. ->where('id', $params['id'])
  176. ->update([
  177. 'part_name' => $params['part_name'],
  178. 'remark' => $params['remark'],
  179. 'part_type' => $params['part_type'],
  180. 'mod_id' => $params['mod_id'],
  181. 'mod_rq' => date('Y-m-d H:i:s'),
  182. ]);
  183. if (!$result) {
  184. $this->error('修改失败');
  185. }
  186. $this->success('修改成功');
  187. }
  188. /** 软删除部件信息
  189. * @param id 部件ID
  190. */
  191. public function DeletePart()
  192. {
  193. if (!$this->request->isPost()) {
  194. $this->error('请求方法错误');
  195. }
  196. $params = $this->request->post();
  197. if (empty($params['id'])) {
  198. $this->error('部件ID不能为空');
  199. }
  200. $ids = explode(',', $params['id']);
  201. $ids = array_filter(array_map('intval', $ids));
  202. if (empty($ids)) {
  203. $this->error('无效的部件ID');
  204. }
  205. $result = db('工单_部件资料')
  206. ->whereIn('id', $ids)
  207. ->whereNull('del_rq')
  208. ->update(['del_rq' => date('Y-m-d H:i:s')]);
  209. if ($result === false) {
  210. $this->error('删除失败');
  211. }
  212. if ($result === 0) {
  213. $this->error('未找到可删除的部件');
  214. }
  215. $this->success('删除成功');
  216. }
  217. /**
  218. * 工单工艺列表
  219. * @param workorder 工单编号
  220. */
  221. public function GetProcessList()
  222. {
  223. if (!$this->request->isGet()) {
  224. $this->error('请求方法错误');
  225. }
  226. $params = $this->request->param();
  227. if (empty($params['workorder'])) {
  228. $this->error('工单编号不能为空');
  229. }
  230. $list = db('工单_基础工艺资料')
  231. ->alias('a')
  232. ->join('工单_部件资料 b', 'a.part_code = b.part_code and a.work_order = b.work_order')
  233. ->where('a.del_rq', null)
  234. ->where('a.work_order', $params['workorder'])
  235. ->where('b.del_rq', null)
  236. ->field('a.id,a.part_code as 部件编号,b.part_name as 部件名称,a.process_code as 工艺编号,
  237. a.process_name as 工艺名称,a.big_process as 大工艺,a.standard_hour as 标准工时,
  238. a.standard_score as 标准公分,a.remark as 备注,a.coefficient as 系数,a.sys_id as 系统人,a.sys_rq as 系统时间,a.mod_id as 修改人,
  239. a.mod_rq as 修改时间')
  240. ->order('process_code')
  241. ->select();
  242. $this->success('成功', $list);
  243. }
  244. /**
  245. * 获取部件列表
  246. * @param workorder 工单编号
  247. */
  248. public function getpartlist()
  249. {
  250. if(!$this->request->isGet()){
  251. $this->error('请求方法错误');
  252. }
  253. $params = $this->request->get();
  254. if (empty($params['workorder'])) {
  255. $this->error('工单编号不能为空');
  256. }
  257. $list = db('工单_部件资料')
  258. ->where('work_order', $params['workorder'])
  259. ->where('del_rq', null)
  260. ->field('part_code as 部件编号,part_name as 部件名称')
  261. ->order('part_code')
  262. ->select();
  263. if(empty($list)){
  264. $this->error('该工单没有部件,请先添加部件');
  265. }
  266. $this->success('成功', $list);
  267. }
  268. /**
  269. * 新增工单工艺资料
  270. * @param workorder 工单编号
  271. * @param part_code 部件编号
  272. * @param process_code 工艺编号
  273. * @param process_name 工艺名称
  274. * @param big_process 大工艺
  275. * @param standard_hour 标准工时
  276. * @param standard_score 标准公分
  277. * @param sys_id 系统人
  278. * @param coefficient 系数
  279. * @param remark 备注
  280. */
  281. public function AddProcess()
  282. {
  283. if (!$this->request->isPost()) {
  284. $this->error('请求方法错误');
  285. }
  286. $params = $this->request->post();
  287. if (empty($params['workorder'])) {
  288. $this->error('工单编号不能为空');
  289. }
  290. if (empty($params['part_code'])) {
  291. $this->error('部件编号不能为空');
  292. }
  293. if (empty($params['process_name'])) {
  294. $this->error('工艺名称不能为空');
  295. }
  296. if (empty($params['big_process'])) {
  297. $this->error('大工艺不能为空');
  298. }
  299. if (empty($params['standard_hour'])) {
  300. $this->error('标准工时不能为空');
  301. }
  302. if (empty($params['standard_score'])) {
  303. $this->error('标准公分不能为空');
  304. }
  305. if (empty($params['coefficient'])) {
  306. $params['coefficient'] = 1;
  307. }
  308. if (empty($params['sys_id'])) {
  309. $this->error('系统人不能为空');
  310. }
  311. // 检查是否传入了process_code
  312. if (!empty($params['process_code'])) {
  313. $code = $params['process_code'];
  314. // 检查是否存在冲突
  315. $exists = db('工单_基础工艺资料')
  316. ->where('work_order', $params['workorder'])
  317. ->where('part_code', $params['part_code'])
  318. ->where('process_code', '>=', $code)
  319. ->count();
  320. if ($exists > 0) {
  321. // 如果存在冲突,将后续编号自动加1
  322. db('工单_基础工艺资料')
  323. ->where('work_order', $params['workorder'])
  324. ->where('part_code', $params['part_code'])
  325. ->where('process_code', '>=', $code)
  326. ->setInc('process_code');
  327. }
  328. } else {
  329. // 如果没有传入process_code,按照原来的逻辑生成
  330. $code = db('工单_基础工艺资料')
  331. ->where('work_order', $params['workorder'])
  332. ->where('part_code', $params['part_code'])
  333. ->max('process_code');
  334. $code = $code ? $code + 1 : 1;
  335. }
  336. $data = [
  337. 'work_order' => $params['workorder'],
  338. 'part_code' => $params['part_code'],
  339. 'process_code' => $code,
  340. 'process_name' => $params['process_name'],
  341. 'big_process' => $params['big_process'],
  342. 'standard_hour' => $params['standard_hour'],
  343. 'standard_score' => $params['standard_score'],
  344. 'coefficient' => $params['coefficient'],
  345. 'remark' => $params['remark'],
  346. 'sys_id' => $params['sys_id'],
  347. 'sys_rq' => date('Y-m-d H:i:s'),
  348. ];
  349. $result = db('工单_基础工艺资料')->insert($data);
  350. if ($result === false) {
  351. $this->error('新增失败');
  352. }
  353. $this->success('新增成功');
  354. }
  355. /**
  356. * 更新工单工艺资料
  357. * @param id 工艺ID
  358. * @param mod_id 修改人
  359. * @param process_name 工艺名称
  360. * @param big_process 大工艺
  361. * @param standard_hour 标准工时
  362. * @param standard_score 标准公分
  363. * @param coefficient 系数
  364. * @param remark 备注
  365. */
  366. public function UpdateProcess()
  367. {
  368. if (!$this->request->isPost()) {
  369. $this->error('请求方法错误');
  370. }
  371. $params = $this->request->post();
  372. if (empty($params['id'])) {
  373. $this->error('工艺ID不能为空');
  374. }
  375. if (empty($params['mod_id'])) {
  376. $this->error('修改人不能为空');
  377. }
  378. if (empty($params['process_name'])) {
  379. $this->error('工艺名称不能为空');
  380. }
  381. if (empty($params['big_process'])) {
  382. $this->error('大工艺不能为空');
  383. }
  384. if (empty($params['standard_hour'])) {
  385. $this->error('标准工时不能为空');
  386. }
  387. if (empty($params['standard_score'])) {
  388. $this->error('标准公分不能为空');
  389. }
  390. if (!isset($params['coefficient']) || $params['coefficient'] === '') {
  391. $params['coefficient'] = 1;
  392. }
  393. $data = [
  394. 'process_name' => $params['process_name'],
  395. 'big_process' => $params['big_process'],
  396. 'standard_hour' => $params['standard_hour'],
  397. 'standard_score' => $params['standard_score'],
  398. 'coefficient' => $params['coefficient'],
  399. 'remark' => $params['remark'],
  400. 'mod_id' => $params['mod_id'],
  401. 'mod_rq' => date('Y-m-d H:i:s'),
  402. ];
  403. $result = db('工单_基础工艺资料')
  404. ->where('id', intval($params['id']))
  405. ->whereNull('del_rq')
  406. ->update($data);
  407. if ($result === false) {
  408. $this->error('更新失败');
  409. }
  410. if ($result === 0) {
  411. $this->error('未找到可更新的工艺或数据无变化');
  412. }
  413. $this->success('更新成功');
  414. }
  415. /**
  416. * 工单工艺资料软删除
  417. * @param id 工艺ID
  418. */
  419. public function DeleteProcess()
  420. {
  421. if (!$this->request->isPost()) {
  422. $this->error('请求方法错误');
  423. }
  424. $params = $this->request->post();
  425. if (empty($params['id'])) {
  426. $this->error('工艺ID不能为空');
  427. }
  428. $ids = explode(',', $params['id']);
  429. $ids = array_filter(array_map('intval', $ids));
  430. if (empty($ids)) {
  431. $this->error('无效的工艺ID');
  432. }
  433. $result = db('工单_基础工艺资料')
  434. ->whereIn('id', $ids)
  435. ->whereNull('del_rq')
  436. ->update(['del_rq' => date('Y-m-d H:i:s')]);
  437. if ($result === false) {
  438. $this->error('删除失败');
  439. }
  440. if ($result === 0) {
  441. $this->error('未找到可删除的工艺');
  442. }
  443. $this->success('删除成功');
  444. }
  445. /**
  446. * 工单工艺复制
  447. * @param workorder 工单编号
  448. * @param product_code 产品编号
  449. * @param sys_id 操作人
  450. * @return array
  451. * @throws \think\db\exception\DataNotFoundException
  452. * @throws \think\db\exception\ModelNotFoundException
  453. * @throws \think\exception\DbException
  454. * @throws \think\exception\PDOException
  455. */
  456. public function workorderprocessCopy()
  457. {
  458. if (!$this->request->isGet()) {
  459. $this->error('请求方法错误');
  460. }
  461. $params = $this->request->param();
  462. if (empty($params['workorder'])) {
  463. $this->error('工单编号不能为空');
  464. }
  465. if (empty($params['product_code'])) {
  466. $this->error('产品编号不能为空');
  467. }
  468. if (empty($params['sys_id'])) {
  469. $this->error('操作人不能为空');
  470. }
  471. $now = date('Y-m-d H:i:s');
  472. $productParts = db('产品_部件资料')
  473. ->where('product_code', $params['product_code'])
  474. ->where('mod_rq', null)
  475. ->field('part_sort as part_code,part_name')
  476. ->select();
  477. if (empty($productParts)) {
  478. $this->error('该产品没有有效的部件');
  479. }
  480. $productProcesses = db('产品_工艺资料')
  481. ->where('product_code', $params['product_code'])
  482. ->field('part_sort as part_code,part_name,gy_sort as process_code,gy_name as process_name,
  483. big_process,standard_hour,standard_score,difficulty_coef as coefficient')
  484. ->select();
  485. if (empty($productProcesses)) {
  486. $this->error('该产品没有工艺');
  487. }
  488. $workOrderParts = [];
  489. //配置工单部件数据
  490. foreach ($productParts as $value) {
  491. $workOrderParts[] = [
  492. 'work_order' => $params['workorder'],
  493. 'part_code' => $value['part_code'],
  494. 'part_name' => $value['part_name'],
  495. 'part_type' => '',
  496. 'remark' => '',
  497. 'status' => 1,
  498. 'sys_id' => $params['sys_id'],
  499. 'sys_rq' => $now,
  500. ];
  501. }
  502. $workOrderProcesses = [];
  503. //配置工单工艺数据
  504. foreach ($productProcesses as $value) {
  505. $workOrderProcesses[] = [
  506. 'work_order' => $params['workorder'],
  507. 'part_code' => $value['part_code'],
  508. 'part_name' => $value['part_name'],
  509. 'process_code' => $value['process_code'],
  510. 'process_name' => $value['process_name'],
  511. 'big_process' => $value['big_process'],
  512. 'standard_hour' => $value['standard_hour'],
  513. 'standard_score' => $value['standard_score'],
  514. 'coefficient' => $value['coefficient'],
  515. 'remark' => '',
  516. 'sys_id' => $params['sys_id'],
  517. 'sys_rq' => $now,
  518. ];
  519. }
  520. Db::startTrans();
  521. try {
  522. //删除数据库现有的工单部件数据
  523. Db::name('工单_部件资料')->where('work_order', $params['workorder'])->delete();
  524. //删除数据库现有的工单工艺数据
  525. Db::name('工单_基础工艺资料')->where('work_order', $params['workorder'])->delete();
  526. //插入工单部件数据
  527. $partInsertCount = Db::name('工单_部件资料')->insertAll($workOrderParts);
  528. if ($partInsertCount === false) {
  529. throw new \Exception('工单部件复制失败');
  530. }
  531. //插入工单工艺数据
  532. $processInsertCount = Db::name('工单_基础工艺资料')->insertAll($workOrderProcesses);
  533. if ($processInsertCount === false) {
  534. throw new \Exception('工单工艺复制失败');
  535. }
  536. //提交事务
  537. Db::commit();
  538. } catch (\Exception $e) {
  539. Db::rollback();
  540. $this->error('复制失败');
  541. }
  542. $this->success('复制成功');
  543. }
  544. /**
  545. * 查询产品类型
  546. * @ApiMethod (GET)
  547. * @param string $workorder 工单编号
  548. * @return array
  549. * @throws \think\db\exception\DataNotFoundException
  550. * @throws \think\db\exception\ModelNotFoundException
  551. * @throws \think\exception\DbException
  552. */
  553. public function getproducttype()
  554. {
  555. if(!$this->request->isGet()){
  556. $this->error('请求方法错误');
  557. }
  558. $params = $this->request->get();
  559. if (empty($params['product'])) {
  560. $this->error('产品类型不能为空');
  561. }
  562. $list = db('产品_基本资料')
  563. ->where('product_type', $params['product'])
  564. ->where('status', 1)
  565. ->field('product_code as 产品编号,product_type as 产品类型,product_name as 产品名称')
  566. ->select();
  567. if(empty($list)){
  568. $this->error('该产品类型没有产品');
  569. }
  570. $this->success('成功', $list);
  571. }
  572. /**
  573. * 工单工艺排序
  574. * @param workorder 工单编号
  575. * @param part_code 部件编号
  576. * @param process_code 工艺编号
  577. * @param process_name 工艺名称
  578. * @param big_process 大工艺
  579. * @param standard_hour 标准工时
  580. * @param standard_score 标准公分
  581. * @param coefficient 系数
  582. * @param remark 备注
  583. */
  584. public function sortProcess()
  585. {
  586. if (!$this->request->isPost()) {
  587. $this->error('请求方法错误');
  588. }
  589. $params = $this->request->post();
  590. if (empty($params)) {
  591. $this->error('参数不能为空');
  592. }
  593. if (!is_array($params)) {
  594. $this->error('参数格式错误');
  595. }
  596. Db::startTrans();
  597. try {
  598. foreach ($params as $value) {
  599. if (empty($value['id'])) {
  600. throw new \Exception('缺少工艺ID');
  601. }
  602. if (!isset($value['process_code']) || $value['process_code'] === '') {
  603. throw new \Exception('缺少工艺编号');
  604. }
  605. $result = db('工单_基础工艺资料')
  606. ->where('id', intval($value['id']))
  607. ->whereNull('del_rq')
  608. ->update(['process_code' => intval($value['process_code'])]);
  609. if ($result === false) {
  610. throw new \Exception('排序失败');
  611. }
  612. }
  613. Db::commit();
  614. } catch (\Exception $e) {
  615. Db::rollback();
  616. $this->error($e->getMessage());
  617. }
  618. $this->success('排序成功');
  619. }
  620. }