WorkOrderProcess.php 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940
  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 GetProcessList()
  259. // {
  260. // if (!$this->request->isGet()) {
  261. // $this->error('请求方法错误');
  262. // }
  263. // $params = $this->request->param();
  264. // if (empty($params['workorder'])) {
  265. // $this->error('工单编号不能为空');
  266. // }
  267. // $list = db('工单_基础工艺资料')
  268. // ->alias('a')
  269. // ->join('工单_部件资料 b', 'a.part_code = b.part_code and a.work_order = b.work_order')
  270. // ->where('a.del_rq', null)
  271. // ->where('a.work_order', $params['workorder'])
  272. // ->where('b.del_rq', null)
  273. // ->field('a.id,a.part_code as 部件编号,b.part_name as 部件名称,a.process_code as 序号,
  274. // a.process_name as 工序名称,a.big_process as 大工艺,a.standard_hour as 秒,a.standard_minutes as 分钟,
  275. // a.standard_score as 标准公分,a.money as 金额,a.remark as 备注,a.coefficient as 难度系数,a.sys_id as 操作人员,
  276. // a.sys_rq as 系统时间,a.mod_id as 修改人,a.mod_rq as 修改时间')
  277. // ->order('process_code')
  278. // ->select();
  279. // $this->success('成功', $list);
  280. // }
  281. /**
  282. * 获取部件列表
  283. * @param workorder 工单编号
  284. */
  285. public function getpartlist()
  286. {
  287. if(!$this->request->isGet()){
  288. $this->error('请求方法错误');
  289. }
  290. $params = $this->request->get();
  291. if (empty($params['workorder'])) {
  292. $this->error('工单编号不能为空');
  293. }
  294. $list = db('工单_部件资料')
  295. ->where('work_order', $params['workorder'])
  296. ->where('del_rq', null)
  297. ->field('part_code as 部件编号,part_name as 部件名称')
  298. ->order('part_code')
  299. ->select();
  300. if(empty($list)){
  301. $this->error('该工单没有部件,请先添加部件');
  302. }
  303. $this->success('成功', $list);
  304. }
  305. /**
  306. * 新增工单工艺资料
  307. * @param workorder 工单编号
  308. * @param part_code 部件编号
  309. * @param process_code 工艺编号
  310. * @param process_name 工艺名称
  311. * @param big_process 大工艺
  312. * @param standard_hour 标准工时
  313. * @param standard_score 标准公分
  314. * @param sys_id 系统人
  315. * @param coefficient 系数
  316. * @param remark 备注
  317. */
  318. public function AddProcess()
  319. {
  320. if (!$this->request->isPost()) {
  321. $this->error('请求方法错误');
  322. }
  323. $params = $this->request->post();
  324. if (empty($params['workorder'])) {
  325. $this->error('工单编号不能为空');
  326. }
  327. if (empty($params['part_code'])) {
  328. $this->error('部件编号不能为空');
  329. }
  330. if (empty($params['process_name'])) {
  331. $this->error('工艺名称不能为空');
  332. }
  333. if (empty($params['big_process'])) {
  334. $this->error('大工艺不能为空');
  335. }
  336. if (empty($params['sys_id'])) {
  337. $this->error('系统人不能为空');
  338. }
  339. if (!empty($params['process_code'])) {
  340. $code = intval($params['process_code']);
  341. $exists = db('工单_基础工艺资料')
  342. ->where('work_order', $params['workorder'])
  343. ->where('part_code', $params['part_code'])
  344. ->where('process_code', $code)
  345. ->whereNull('del_rq')
  346. ->count();
  347. if ($exists > 0) {
  348. $this->error('工序编号已存在');
  349. }
  350. } else {
  351. $code = db('工单_基础工艺资料')
  352. ->where('work_order', $params['workorder'])
  353. ->where('part_code', $params['part_code'])
  354. ->whereNull('del_rq')
  355. ->max('process_code');
  356. $code = $code ? $code + 1 : 1;
  357. }
  358. $data = [
  359. 'work_order' => $params['workorder'],
  360. 'part_code' => $params['part_code'],
  361. 'part_name' => $params['part_name'],
  362. 'process_code' => $code,
  363. 'process_name' => $params['process_name'],
  364. 'big_process' => $params['big_process'],
  365. 'standard_hour' => $params['standard_hour'],
  366. 'standard_score' => $params['standard_score'],
  367. 'coefficient' => $params['coefficient'],
  368. 'remark' => $params['remark'],
  369. 'sys_id' => $params['sys_id'],
  370. 'sys_rq' => date('Y-m-d H:i:s'),
  371. ];
  372. $result = db('工单_基础工艺资料')->insert($data);
  373. if ($result === false) {
  374. $this->error('新增失败');
  375. }
  376. $this->success('新增成功');
  377. }
  378. /**
  379. * 更新工单工艺资料
  380. * @param id 工艺ID
  381. * @param mod_id 修改人
  382. * @param process_name 工艺名称
  383. * @param big_process 大工艺
  384. * @param standard_hour 标准工时
  385. * @param standard_score 标准公分
  386. * @param coefficient 系数
  387. * @param remark 备注
  388. */
  389. public function UpdateProcess()
  390. {
  391. if (!$this->request->isPost()) {
  392. $this->error('请求方法错误');
  393. }
  394. $params = $this->request->post();
  395. if (empty($params['id'])) {
  396. $this->error('工艺ID不能为空');
  397. }
  398. if (empty($params['mod_id'])) {
  399. $this->error('修改人不能为空');
  400. }
  401. if (empty($params['process_name'])) {
  402. $this->error('工艺名称不能为空');
  403. }
  404. if (empty($params['big_process'])) {
  405. $this->error('大工艺不能为空');
  406. }
  407. if (!isset($params['coefficient']) || $params['coefficient'] === '') {
  408. $params['coefficient'] = 'C';
  409. }
  410. $process = Db::table('工单_基础工艺资料')
  411. ->where('id', intval($params['id']))
  412. ->whereNull('del_rq')
  413. ->find();
  414. if (empty($process)) {
  415. $this->error('工艺不存在');
  416. }
  417. $data = [
  418. 'part_code' => $params['part_code'],
  419. 'part_name' => $params['part_name'],
  420. 'standard_minutes' => $params['standard_minutes'],
  421. 'money' => $params['money'],
  422. 'remark' => $params['remark'],
  423. 'process_name' => $params['process_name'],
  424. 'big_process' => $params['big_process'],
  425. 'standard_hour' => $params['standard_hour'],
  426. 'standard_score' => $params['standard_score'],
  427. 'coefficient' => $params['coefficient'],
  428. 'mod_id' => $params['mod_id'],
  429. 'mod_rq' => date('Y-m-d H:i:s'),
  430. ];
  431. $fieldLabels = [
  432. 'part_code' => '部件编号',
  433. 'part_name' => '部件名称',
  434. 'standard_minutes' => '分',
  435. 'money' => '金额',
  436. 'remark' => '备注',
  437. 'process_name' => '工序名称',
  438. 'big_process' => '大工序',
  439. 'standard_hour' => '秒',
  440. 'standard_score' => '定额分',
  441. 'coefficient' => '难度系数',
  442. ];
  443. $operTime = date('Y-m-d H:i:s');
  444. $logList = [];
  445. foreach ($fieldLabels as $field => $label) {
  446. $oldValue = isset($process[$field]) ? $process[$field] : '';
  447. $newValue = $data[$field];
  448. if ((string)$oldValue === (string)$newValue) {
  449. continue;
  450. }
  451. $logList[] = [
  452. 'order_no' => $process['work_order'],
  453. 'process_code' => $params['process_code'],
  454. 'change_field' => $label,
  455. 'old_value' => $oldValue === null ? '' : (string)$oldValue,
  456. 'new_value' => (string)$newValue,
  457. 'oper_type' => '修改工单工艺',
  458. 'oper_user_name' => $params['mod_id'],
  459. 'oper_time' => $operTime,
  460. ];
  461. }
  462. if (empty($logList)) {
  463. $this->error('未找到可更新的工艺或数据无变化');
  464. }
  465. $standardScoreChanged = (string)$process['standard_score'] !== (string)$params['standard_score'];
  466. if ($standardScoreChanged && (string)$process['status'] === '0') {
  467. $workOrder = Db::table('工单_基本资料')
  468. ->where('订单编号', $process['work_order'])
  469. ->where('Mod_rq', null)
  470. ->field('计划制造工分')
  471. ->find();
  472. if (empty($workOrder)) {
  473. $this->error('工单不存在');
  474. }
  475. $totalStandardScore = Db::table('工单_基础工艺资料')
  476. ->where('work_order', $process['work_order'])
  477. ->where('status', 0)
  478. ->whereNull('del_rq')
  479. ->sum('standard_score');
  480. $totalStandardScore = $totalStandardScore ? floatval($totalStandardScore) : 0;
  481. $newTotal = $totalStandardScore - floatval($process['standard_score']) + floatval($params['standard_score']);
  482. if ($newTotal > floatval($workOrder['计划制造工分'])) {
  483. $this->error('工序定额分和大于制造工分,请确认之后再修改');
  484. }
  485. }
  486. Db::startTrans();
  487. try {
  488. if (!empty($params['part_code'])) {
  489. $partExists = Db::table('工单_部件资料')
  490. ->where('work_order', $process['work_order'])
  491. ->where('part_code', $params['part_code'])
  492. ->whereNull('del_rq')
  493. ->find();
  494. if (empty($partExists)) {
  495. $partData = [
  496. 'work_order' => $process['work_order'],
  497. 'part_code' => $params['part_code'],
  498. 'part_name' => $params['part_name'],
  499. 'remark' => '',
  500. 'part_type' => $params['big_process'],
  501. 'status' => 1,
  502. 'sys_id' => $params['mod_id'],
  503. 'sys_rq' => date('Y-m-d H:i:s'),
  504. ];
  505. $partInsertResult = Db::table('工单_部件资料')->insert($partData);
  506. if ($partInsertResult === false) {
  507. $dbError = Db::getError();
  508. $detail = is_array($dbError) && !empty($dbError) ? json_encode($dbError, JSON_UNESCAPED_UNICODE) : '';
  509. throw new \Exception('新增部件资料失败' . ($detail !== '' ? ':' . $detail : ''));
  510. }
  511. }
  512. }
  513. $updateResult = Db::table('工单_基础工艺资料')
  514. ->where('id', intval($params['id']))
  515. ->whereNull('del_rq')
  516. ->update($data);
  517. if ($updateResult === false) {
  518. $dbError = Db::getError();
  519. $detail = is_array($dbError) && !empty($dbError) ? json_encode($dbError, JSON_UNESCAPED_UNICODE) : '';
  520. throw new \Exception('更新工艺失败' . ($detail !== '' ? ':' . $detail : ''));
  521. }
  522. if ($updateResult === 0) {
  523. throw new \Exception('未找到可更新的工艺');
  524. }
  525. $logResult = Db::name('work_order_operation_log')->insertAll($logList);
  526. if ($logResult === false || $logResult === 0) {
  527. $dbError = Db::getError();
  528. $detail = is_array($dbError) && !empty($dbError) ? json_encode($dbError, JSON_UNESCAPED_UNICODE) : '';
  529. throw new \Exception('写入操作日志失败' . ($detail !== '' ? ':' . $detail : ''));
  530. }
  531. Db::commit();
  532. } catch (\think\exception\HttpResponseException $e) {
  533. throw $e;
  534. } catch (\Exception $e) {
  535. Db::rollback();
  536. $msg = trim($e->getMessage());
  537. $this->error('更新失败:' . ($msg !== '' ? $msg : '未知错误'));
  538. }
  539. $this->success('更新成功');
  540. }
  541. /**
  542. * 工单工艺资料软删除
  543. * @param id 工艺ID
  544. */
  545. public function DeleteProcess()
  546. {
  547. if (!$this->request->isPost()) {
  548. $this->error('请求方法错误');
  549. }
  550. $params = $this->request->post();
  551. if (empty($params['id'])) {
  552. $this->error('工艺ID不能为空');
  553. }
  554. $ids = explode(',', $params['id']);
  555. $ids = array_filter(array_map('intval', $ids));
  556. if (empty($ids)) {
  557. $this->error('无效的工艺ID');
  558. }
  559. $result = db('工单_基础工艺资料')
  560. ->whereIn('id', $ids)
  561. ->whereNull('del_rq')
  562. ->update(['del_rq' => date('Y-m-d H:i:s')]);
  563. if ($result === false) {
  564. $this->error('删除失败');
  565. }
  566. if ($result === 0) {
  567. $this->error('未找到可删除的工艺');
  568. }
  569. $this->success('删除成功');
  570. }
  571. /**
  572. * 工单工艺复制
  573. * @param workorder 工单编号
  574. * @param product_code 产品编号
  575. * @param sys_id 操作人
  576. * @return array
  577. * @throws \think\db\exception\DataNotFoundException
  578. * @throws \think\db\exception\ModelNotFoundException
  579. * @throws \think\exception\DbException
  580. * @throws \think\exception\PDOException
  581. */
  582. public function workorderprocessCopy()
  583. {
  584. if (!$this->request->isGet()) {
  585. $this->error('请求方法错误');
  586. }
  587. $params = $this->request->param();
  588. if (empty($params['workorder'])) {
  589. $this->error('工单编号不能为空');
  590. }
  591. if (empty($params['product_code'])) {
  592. $this->error('产品编号不能为空');
  593. }
  594. if (empty($params['sys_id'])) {
  595. $this->error('操作人不能为空');
  596. }
  597. $now = date('Y-m-d H:i:s');
  598. $productParts = db('产品_部件资料')
  599. ->where('product_code', $params['product_code'])
  600. ->where('mod_rq', null)
  601. ->field('part_sort as part_code,part_name')
  602. ->select();
  603. if (empty($productParts)) {
  604. $this->error('该产品没有有效的部件');
  605. }
  606. $productProcesses = db('产品_工艺资料')
  607. ->where('product_code', $params['product_code'])
  608. ->field('part_sort as part_code,part_name,gy_sort as process_code,gy_name as process_name,
  609. big_process,standard_hour,standard_score,difficulty_coef as coefficient')
  610. ->select();
  611. if (empty($productProcesses)) {
  612. $this->error('该产品没有工艺');
  613. }
  614. $workOrderParts = [];
  615. //配置工单部件数据
  616. foreach ($productParts as $value) {
  617. $workOrderParts[] = [
  618. 'work_order' => $params['workorder'],
  619. 'part_code' => $value['part_code'],
  620. 'part_name' => $value['part_name'],
  621. 'part_type' => '',
  622. 'remark' => '',
  623. 'status' => 1,
  624. 'sys_id' => $params['sys_id'],
  625. 'sys_rq' => $now,
  626. ];
  627. }
  628. $workOrderProcesses = [];
  629. //配置工单工艺数据
  630. foreach ($productProcesses as $value) {
  631. $workOrderProcesses[] = [
  632. 'work_order' => $params['workorder'],
  633. 'part_code' => $value['part_code'],
  634. 'part_name' => $value['part_name'],
  635. 'process_code' => $value['process_code'],
  636. 'process_name' => $value['process_name'],
  637. 'big_process' => $value['big_process'],
  638. 'standard_hour' => $value['standard_hour'],
  639. 'standard_score' => $value['standard_score'],
  640. 'coefficient' => $value['coefficient'],
  641. 'remark' => '',
  642. 'sys_id' => $params['sys_id'],
  643. 'sys_rq' => $now,
  644. ];
  645. }
  646. Db::startTrans();
  647. try {
  648. //删除数据库现有的工单部件数据
  649. Db::name('工单_部件资料')->where('work_order', $params['workorder'])->delete();
  650. //删除数据库现有的工单工艺数据
  651. Db::name('工单_基础工艺资料')->where('work_order', $params['workorder'])->delete();
  652. //插入工单部件数据
  653. $partInsertCount = Db::name('工单_部件资料')->insertAll($workOrderParts);
  654. if ($partInsertCount === false) {
  655. throw new \Exception('工单部件复制失败');
  656. }
  657. //插入工单工艺数据
  658. $processInsertCount = Db::name('工单_基础工艺资料')->insertAll($workOrderProcesses);
  659. if ($processInsertCount === false) {
  660. throw new \Exception('工单工艺复制失败');
  661. }
  662. //提交事务
  663. Db::commit();
  664. } catch (\Exception $e) {
  665. Db::rollback();
  666. $this->error('复制失败');
  667. }
  668. $this->success('复制成功');
  669. }
  670. /**
  671. * 查询产品类型
  672. * @ApiMethod (GET)
  673. * @param string $workorder 工单编号
  674. * @return array
  675. * @throws \think\db\exception\DataNotFoundException
  676. * @throws \think\db\exception\ModelNotFoundException
  677. * @throws \think\exception\DbException
  678. */
  679. public function getproducttype()
  680. {
  681. if(!$this->request->isGet()){
  682. $this->error('请求方法错误');
  683. }
  684. $params = $this->request->get();
  685. if (empty($params['product'])) {
  686. $this->error('产品类型不能为空');
  687. }
  688. $list = db('产品_基本资料')
  689. ->where('product_type', $params['product'])
  690. ->where('status', 1)
  691. ->field('product_code as 产品编号,product_type as 产品类型,product_name as 产品名称')
  692. ->select();
  693. if(empty($list)){
  694. $this->error('该产品类型没有产品');
  695. }
  696. $this->success('成功', $list);
  697. }
  698. /**
  699. * 工单工艺排序
  700. * @param workorder 工单编号
  701. * @param part_code 部件编号
  702. * @param process_code 工艺编号
  703. * @param process_name 工艺名称
  704. * @param big_process 大工艺
  705. * @param standard_hour 标准工时
  706. * @param standard_score 标准公分
  707. * @param coefficient 系数
  708. * @param remark 备注
  709. */
  710. public function sortProcess()
  711. {
  712. if (!$this->request->isPost()) {
  713. $this->error('请求方法错误');
  714. }
  715. $params = $this->request->post();
  716. if (empty($params)) {
  717. $this->error('参数不能为空');
  718. }
  719. if (!is_array($params)) {
  720. $this->error('参数格式错误');
  721. }
  722. Db::startTrans();
  723. try {
  724. foreach ($params as $value) {
  725. if (empty($value['id'])) {
  726. throw new \Exception('缺少工艺ID');
  727. }
  728. if (!isset($value['process_code']) || $value['process_code'] === '') {
  729. throw new \Exception('缺少工艺编号');
  730. }
  731. $result = db('工单_基础工艺资料')
  732. ->where('id', intval($value['id']))
  733. ->whereNull('del_rq')
  734. ->update(['process_code' => intval($value['process_code'])]);
  735. if ($result === false) {
  736. throw new \Exception('排序失败');
  737. }
  738. }
  739. Db::commit();
  740. } catch (\Exception $e) {
  741. Db::rollback();
  742. $this->error($e->getMessage());
  743. }
  744. $this->success('排序成功');
  745. }
  746. /**
  747. * 工单工艺excel导入
  748. * @param workorder 工单编号
  749. *
  750. */
  751. public function importProcess()
  752. {
  753. if (!$this->request->isPost()) {
  754. $this->error('请求方法错误');
  755. }
  756. $params = $this->request->post();
  757. if (empty($params['workorder'])) {
  758. $this->error('工单编号不能为空');
  759. }
  760. if (empty($params['sys_id'])) {
  761. $this->error('操作人不能为空');
  762. }
  763. $file = $this->request->file('file');
  764. if (!$file) {
  765. $this->error('文件不能为空');
  766. }
  767. $uploadDir = ROOT_PATH . 'public' . DS . 'uploads';
  768. $info = $file->validate(['size' => 1024 * 1024 * 10, 'ext' => 'xlsx,xls,csv,txt'])
  769. ->move($uploadDir);
  770. if (!$info) {
  771. $this->error($file->getError());
  772. }
  773. $filePath = $uploadDir . DS . $info->getSaveName();
  774. // 定额表:上方为标题与元数据,第 5 行表头,第 6 行起为数据
  775. $data = $this->readExcel($filePath, 5, 6);
  776. if (empty($data)) {
  777. $this->error('文件内容为空');
  778. }
  779. $seenSeq = [];
  780. foreach ($data as $row) {
  781. $seq = isset($row['序号']) ? $row['序号'] : null;
  782. if ($seq === null || $seq === '') {
  783. continue;
  784. }
  785. $seqKey = is_scalar($seq) ? (string)$seq : $seq;
  786. if (isset($seenSeq[$seqKey])) {
  787. $this->error('工序序号重复,请重新调整之后再上传');
  788. }
  789. $seenSeq[$seqKey] = true;
  790. }
  791. $now = date('Y-m-d H:i:s');
  792. $partMap = [];
  793. $nextPartCode = 0;
  794. foreach ($data as $row) {
  795. $partName = isset($row['部件名称']) ? trim((string)$row['部件名称']) : '';
  796. if ($partName === '') {
  797. continue;
  798. }
  799. $partType = isset($row['生产工序']) ? trim((string)$row['生产工序']) : '';
  800. if (!isset($partMap[$partName])) {
  801. $partMap[$partName] = [
  802. 'part_code' => ++$nextPartCode,
  803. 'part_type' => $partType,
  804. ];
  805. } elseif ($partType !== '' && $partMap[$partName]['part_type'] === '') {
  806. $partMap[$partName]['part_type'] = $partType;
  807. }
  808. }
  809. $workOrderParts = [];
  810. foreach ($partMap as $partName => $partInfo) {
  811. $workOrderParts[] = [
  812. 'work_order' => $params['workorder'],
  813. 'part_name' => $partName,
  814. 'part_code' => $partInfo['part_code'],
  815. 'part_type' => $partInfo['part_type'],
  816. 'remark' => '',
  817. 'status' => 1,
  818. 'sys_id' => $params['sys_id'],
  819. 'sys_rq' => $now,
  820. ];
  821. }
  822. $workOrderProcesses = [];
  823. foreach ($data as $value) {
  824. $seq = isset($value['序号']) ? $value['序号'] : null;
  825. if ($seq === null || $seq === '') {
  826. continue;
  827. }
  828. $name = isset($value['部件名称']) ? trim((string)$value['部件名称']) : '';
  829. $partCode = ($name !== '' && isset($partMap[$name])) ? $partMap[$name]['part_code'] : '';
  830. $workOrderProcesses[] = [
  831. 'work_order' => $params['workorder'],
  832. 'part_code' => $partCode,
  833. 'part_name' => $name,
  834. 'process_code' => $seq,
  835. 'process_name' => $value['工序名称'],
  836. 'big_process' => $value['生产工序'],
  837. 'standard_hour' => $value['秒'],
  838. 'standard_minutes' => $value['分'],
  839. 'standard_score' => $value['定额分'],
  840. 'money' => $value['金额'],
  841. 'coefficient' => $value['难度系数'],
  842. 'remark' => isset($value['备注']) ? $value['备注'] : '',
  843. 'sys_id' => $params['sys_id'],
  844. 'sys_rq' => $now,
  845. ];
  846. }
  847. Db::startTrans();
  848. try {
  849. Db::name('工单_部件资料')->where('work_order', $params['workorder'])->delete();
  850. if (!empty($workOrderParts)) {
  851. $partInsertCount = Db::name('工单_部件资料')->insertAll($workOrderParts);
  852. if ($partInsertCount === false || $partInsertCount === 0) {
  853. throw new \Exception('工单部件导入失败');
  854. }
  855. }
  856. Db::name('工单_基础工艺资料')->where('work_order', $params['workorder'])->delete();
  857. if (empty($workOrderProcesses)) {
  858. throw new \Exception('没有可导入的工序数据');
  859. }
  860. $processInsertCount = Db::name('工单_基础工艺资料')->insertAll($workOrderProcesses);
  861. if ($processInsertCount === false || $processInsertCount === 0) {
  862. throw new \Exception('工单工艺导入失败');
  863. }
  864. Db::commit();
  865. } catch (\think\exception\HttpResponseException $e) {
  866. throw $e;
  867. } catch (\Exception $e) {
  868. Db::rollback();
  869. $this->error('导入失败:' . $e->getMessage());
  870. }
  871. $this->success('导入成功', [
  872. 'part_count' => count($workOrderParts),
  873. 'process_count' => count($workOrderProcesses),
  874. ]);
  875. }
  876. /**
  877. * 读取 Excel/CSV(PhpSpreadsheet)
  878. *
  879. * @param string $filePath
  880. * @param int $headerRowNum 表头所在行(1 起计,如定额表为第 5 行)
  881. * @param int $dataStartRowNum 首条数据行(1 起计,须大于表头行,如第 6 行)
  882. * @return array 每行一条关联数组,键为表头单元格文本
  883. */
  884. public function readExcel($filePath, $headerRowNum = 5, $dataStartRowNum = 6)
  885. {
  886. if (!is_file($filePath) || !is_readable($filePath)) {
  887. return [];
  888. }
  889. if ($dataStartRowNum <= $headerRowNum) {
  890. return [];
  891. }
  892. $spreadsheet = IOFactory::load($filePath);
  893. $sheet = $spreadsheet->getActiveSheet();
  894. $rows = $sheet->toArray();
  895. if (empty($rows)) {
  896. return [];
  897. }
  898. $headerIdx = $headerRowNum - 1;
  899. $dataStartIdx = $dataStartRowNum - 1;
  900. if (!isset($rows[$headerIdx])) {
  901. return [];
  902. }
  903. $headers = array_map(function ($cell) {
  904. return is_string($cell) ? trim($cell) : $cell;
  905. }, $rows[$headerIdx]);
  906. $data = [];
  907. $rowCount = count($rows);
  908. for ($r = $dataStartIdx; $r < $rowCount; $r++) {
  909. $row = $rows[$r];
  910. $hasCell = false;
  911. foreach ($row as $cell) {
  912. if ($cell !== null && $cell !== '') {
  913. $hasCell = true;
  914. break;
  915. }
  916. }
  917. if (!$hasCell) {
  918. continue;
  919. }
  920. $assoc = [];
  921. foreach ($headers as $i => $key) {
  922. if ($key === '' || $key === null) {
  923. continue;
  924. }
  925. $assoc[$key] = array_key_exists($i, $row) ? $row[$i] : null;
  926. }
  927. $data[] = $assoc;
  928. }
  929. return $data;
  930. }
  931. /**
  932. * 获取工单工艺信息
  933. * @param workorder 工单编号
  934. * @return array
  935. * @throws \think\db\exception\DataNotFoundException
  936. * @throws \think\db\exception\ModelNotFoundException
  937. * @throws \think\exception\DbException
  938. */
  939. public function getWorkOrderProcess()
  940. {
  941. if (!$this->request->isGet()) {
  942. $this->error('请求方法错误');
  943. }
  944. $params = $this->request->param();
  945. if (empty($params['workorder'])) {
  946. $this->error('工单编号不能为空');
  947. }
  948. $where = [
  949. 'work_order' => $params['workorder'],
  950. 'del_rq' => null,
  951. ];
  952. $workOrderProcess = Db::table('工单_基础工艺资料')
  953. ->where($where)
  954. ->field('id,part_code as 部件编号,part_name as 部件名称,process_code as 工序编号,process_name as 工序名称,
  955. big_process as 大工序,standard_hour as 秒,standard_minutes as 分,standard_score as 定额分,money as 金额,
  956. coefficient as 难度系数,remark as 备注,status as 状态,pid')
  957. ->select();
  958. if (empty($workOrderProcess)) {
  959. $this->error('工单工艺不存在');
  960. }
  961. $pidList = [];
  962. foreach ($workOrderProcess as $row) {
  963. if (!empty($row['pid'])) {
  964. $pidList[(int)$row['pid']] = true;
  965. }
  966. }
  967. $parentCodeMap = [];
  968. if (!empty($pidList)) {
  969. $parentCodeMap = Db::table('工单_基础工艺资料')
  970. ->whereIn('id', array_keys($pidList))
  971. ->column('process_code', 'id');
  972. }
  973. foreach ($workOrderProcess as &$row) {
  974. $row['母工序编号'] = !empty($row['pid']) && isset($parentCodeMap[(int)$row['pid']])
  975. ? $parentCodeMap[(int)$row['pid']]
  976. : '';
  977. }
  978. unset($row);
  979. $this->success('成功', $workOrderProcess);
  980. }
  981. /**
  982. * 修改工单计划制造工分
  983. * @param id 工单Uniqid或订单编号
  984. * @param number 计划制造工分
  985. * @param sys_id 操作人
  986. */
  987. public function updateWorkOrderPlanManufacture()
  988. {
  989. if (!$this->request->isPost()) {
  990. $this->error('请求方法错误');
  991. }
  992. $params = $this->request->post();
  993. if (empty($params['id'])) {
  994. $this->error('工单ID不能为空');
  995. }
  996. if (!isset($params['number']) || $params['number'] === '') {
  997. $this->error('制造工分不能为空');
  998. }
  999. if (empty($params['sys_id'])) {
  1000. $this->error('操作人不能为空');
  1001. }
  1002. $workOrder = Db::table('工单_基本资料')
  1003. ->where('Uniqid|订单编号', $params['id'])
  1004. ->where('Mod_rq', null)
  1005. ->field('Uniqid,订单编号,计划制造工分')
  1006. ->find();
  1007. if (empty($workOrder)) {
  1008. $this->error('工单不存在');
  1009. }
  1010. $oldValue = $workOrder['计划制造工分'];
  1011. $newValue = $params['number'];
  1012. if ((string)$oldValue === (string)$newValue) {
  1013. $this->error('计划制造工分未发生变化');
  1014. }
  1015. $totalStandardScore = Db::table('工单_基础工艺资料')
  1016. ->where('work_order', $workOrder['订单编号'])
  1017. ->where('status', 0)
  1018. ->whereNull('del_rq')
  1019. ->sum('standard_score');
  1020. $totalStandardScore = $totalStandardScore ? floatval($totalStandardScore) : 0;
  1021. if ($totalStandardScore > floatval($newValue)) {
  1022. $this->error('工序定额分和大于制造工分,请确认之后再修改');
  1023. }
  1024. $operTime = date('Y-m-d H:i:s');
  1025. $logData = [
  1026. 'order_no' => $workOrder['订单编号'],
  1027. 'change_field' => '计划制造工分',
  1028. 'old_value' => $oldValue === null ? '' : (string)$oldValue,
  1029. 'new_value' => (string)$newValue,
  1030. 'oper_type' => '修改工单计划制造工分',
  1031. 'oper_user_name' => $params['sys_id'],
  1032. 'oper_time' => $operTime,
  1033. ];
  1034. Db::startTrans();
  1035. try {
  1036. $updateResultSql = Db::table('工单_基本资料')
  1037. ->where('Uniqid', $workOrder['Uniqid'])
  1038. ->fetchSql(true)
  1039. ->update(['计划制造工分' => $newValue]);
  1040. $updateResult = Db::query($updateResultSql);
  1041. if ($updateResult === false) {
  1042. throw new \Exception('更新工单失败');
  1043. }
  1044. $logResult = Db::name('work_order_operation_log')->insert($logData);
  1045. if ($logResult === false) {
  1046. throw new \Exception('写入操作日志失败');
  1047. }
  1048. Db::commit();
  1049. } catch (\Exception $e) {
  1050. Db::rollback();
  1051. $this->error('修改失败');
  1052. }
  1053. $this->success('修改成功');
  1054. }
  1055. /**
  1056. * 拆分工序时判断工序是否存在报工
  1057. * @param workorder 工单编号
  1058. * @param process_code 工艺编号
  1059. * @return array
  1060. * @throws \think\db\exception\DataNotFoundException
  1061. * @throws \think\db\exception\ModelNotFoundException
  1062. * @throws \think\exception\DbException
  1063. */
  1064. public function checkProcessReport()
  1065. {
  1066. if (!$this->request->isPost()) {
  1067. $this->error('请求方法错误');
  1068. }
  1069. $params = $this->request->post();
  1070. if (empty($params['workorder'])) {
  1071. $this->error('工单编号不能为空');
  1072. }
  1073. if (empty($params['process_code'])) {
  1074. $this->error('工艺编号不能为空');
  1075. }
  1076. $process = db('设备_工分计酬')
  1077. ->where('work_order', $params['workorder'])
  1078. ->where('process_code', $params['process_code'])
  1079. ->find();
  1080. if (!empty($process)) {
  1081. $this->error('');
  1082. }
  1083. $this->success('工序不存在报工,可以拆分');
  1084. }
  1085. /**
  1086. * 确认拆分工序:number>0 时母工序置为已拆分并生成子工序(子工序 pid 为母工序 id);
  1087. * number=0 时恢复母工序 status=0 并软删除其子工序
  1088. * @param id 工艺ID
  1089. * @param number 拆分数量(0 表示取消拆分)
  1090. * @param sys_id 操作人员
  1091. * @param sys_rq 操作时间(可选,默认当前时间)
  1092. */
  1093. public function confirmProcessSplit()
  1094. {
  1095. if (!$this->request->isPost()) {
  1096. $this->error('请求方法错误');
  1097. }
  1098. $params = $this->request->post();
  1099. if (empty($params['id'])) {
  1100. $this->error('工艺ID不能为空');
  1101. }
  1102. if (!isset($params['number']) || $params['number'] === '' || $params['number'] === null) {
  1103. $this->error('拆分工序数量不能为空');
  1104. }
  1105. if (empty($params['sys_id'])) {
  1106. $this->error('操作人不能为空');
  1107. }
  1108. $splitCount = intval($params['number']);
  1109. $operTime = !empty($params['sys_rq']) ? $params['sys_rq'] : date('Y-m-d H:i:s');
  1110. $processId = intval($params['id']);
  1111. $process = Db::table('工单_基础工艺资料')
  1112. ->where('id', $processId)
  1113. ->whereNull('del_rq')
  1114. ->find();
  1115. if (empty($process)) {
  1116. $this->error('工序不存在');
  1117. }
  1118. $workorder = $process['work_order'];
  1119. if ($splitCount === 0) {
  1120. $children = Db::table('工单_基础工艺资料')
  1121. ->where('pid', $processId)
  1122. ->whereNull('del_rq')
  1123. ->field('id,process_code')
  1124. ->select();
  1125. $logList = [
  1126. [
  1127. 'order_no' => $workorder,
  1128. 'process_code' => $process['process_code'],
  1129. 'change_field' => '状态',
  1130. 'old_value' => '1',
  1131. 'new_value' => '0',
  1132. 'oper_type' => '取消拆分工序',
  1133. 'oper_user_name' => $params['sys_id'],
  1134. 'oper_time' => $operTime,
  1135. ],
  1136. ];
  1137. foreach ($children as $child) {
  1138. $logList[] = [
  1139. 'order_no' => $workorder,
  1140. 'process_code' => $child['process_code'],
  1141. 'change_field' => '工序拆分',
  1142. 'old_value' => (string)$process['process_code'],
  1143. 'new_value' => '删除子工序',
  1144. 'oper_type' => '取消拆分工序',
  1145. 'oper_user_name' => $params['sys_id'],
  1146. 'oper_time' => $operTime,
  1147. ];
  1148. }
  1149. Db::startTrans();
  1150. try {
  1151. if (!empty($children)) {
  1152. $deleteResult = Db::table('工单_基础工艺资料')
  1153. ->where('pid', $processId)
  1154. ->whereNull('del_rq')
  1155. ->update(['del_rq' => $operTime]);
  1156. if ($deleteResult === false) {
  1157. throw new \Exception('删除子工序失败');
  1158. }
  1159. }
  1160. $updateResult = Db::table('工单_基础工艺资料')
  1161. ->where('id', $processId)
  1162. ->whereNull('del_rq')
  1163. ->update(['status' => 0]);
  1164. if ($updateResult === false) {
  1165. throw new \Exception('恢复母工序状态失败');
  1166. }
  1167. if (!empty($logList)) {
  1168. $logResult = Db::name('work_order_operation_log')->insertAll($logList);
  1169. if ($logResult === false || $logResult === 0) {
  1170. throw new \Exception('写入操作日志失败');
  1171. }
  1172. }
  1173. Db::commit();
  1174. } catch (\think\exception\HttpResponseException $e) {
  1175. throw $e;
  1176. } catch (\Exception $e) {
  1177. Db::rollback();
  1178. $this->error('取消拆分失败:' . $e->getMessage());
  1179. }
  1180. $this->success('取消拆分工序成功', ['count' => count($children)]);
  1181. return;
  1182. }
  1183. if ($splitCount < 1) {
  1184. $this->error('拆分工序数量必须大于0');
  1185. }
  1186. if (!in_array($process['status'], [0, '0'], true)) {
  1187. $this->error('工序不存在或已确认拆分');
  1188. }
  1189. $processCodes = Db::table('工单_基础工艺资料')
  1190. ->where('work_order', $workorder)
  1191. ->whereNull('del_rq')
  1192. ->column('process_code');
  1193. $maxNumeric = 0;
  1194. foreach ($processCodes as $code) {
  1195. if (preg_match('/^(\d+)/', (string)$code, $match)) {
  1196. $num = intval($match[1]);
  1197. if ($num > $maxNumeric) {
  1198. $maxNumeric = $num;
  1199. }
  1200. }
  1201. }
  1202. $baseCode = $maxNumeric + 1;
  1203. $splitFields = ['standard_hour', 'standard_minutes', 'standard_score', 'money'];
  1204. $splitValues = [];
  1205. foreach ($splitFields as $field) {
  1206. $splitValues[$field] = $this->splitNumericValue($process[$field], $splitCount);
  1207. }
  1208. $insertList = [];
  1209. for ($i = 1; $i <= $splitCount; $i++) {
  1210. $newProcessCode = $baseCode + $i - 1;
  1211. $exists = Db::table('工单_基础工艺资料')
  1212. ->where('work_order', $workorder)
  1213. ->where('process_code', $newProcessCode)
  1214. ->whereNull('del_rq')
  1215. ->count();
  1216. if ($exists > 0) {
  1217. $this->error('工序编号已存在:' . $newProcessCode);
  1218. }
  1219. $insertList[] = [
  1220. 'work_order' => $workorder,
  1221. 'part_code' => $process['part_code'],
  1222. 'part_name' => isset($process['part_name']) ? $process['part_name'] : '',
  1223. 'process_code' => $newProcessCode,
  1224. 'process_name' => $process['process_name'] . '-' . $i,
  1225. 'big_process' => $process['big_process'],
  1226. 'standard_hour' => $splitValues['standard_hour'][$i - 1],
  1227. 'standard_minutes' => $splitValues['standard_minutes'][$i - 1],
  1228. 'standard_score' => $splitValues['standard_score'][$i - 1],
  1229. 'money' => $splitValues['money'][$i - 1],
  1230. 'coefficient' => isset($process['coefficient']) && $process['coefficient'] !== '' && $process['coefficient'] !== null
  1231. ? $process['coefficient'] : 'C',
  1232. 'remark' => isset($process['remark']) && $process['remark'] !== null ? $process['remark'] : '',
  1233. 'status' => 0,
  1234. 'pid' => $processId,
  1235. 'sys_id' => $params['sys_id'],
  1236. 'sys_rq' => $operTime,
  1237. ];
  1238. }
  1239. $logList = [
  1240. [
  1241. 'order_no' => $workorder,
  1242. 'process_code' => $process['process_code'],
  1243. 'change_field' => '状态',
  1244. 'old_value' => '0',
  1245. 'new_value' => '1',
  1246. 'oper_type' => '确认拆分工序',
  1247. 'oper_user_name' => $params['sys_id'],
  1248. 'oper_time' => $operTime,
  1249. ],
  1250. ];
  1251. foreach ($insertList as $row) {
  1252. $logList[] = [
  1253. 'order_no' => $workorder,
  1254. 'process_code' => $row['process_code'],
  1255. 'change_field' => '工序拆分',
  1256. 'old_value' => (string)$process['process_code'],
  1257. 'new_value' => sprintf(
  1258. '秒:%s,分:%s,定额分:%s,金额:%s',
  1259. $row['standard_hour'],
  1260. $row['standard_minutes'],
  1261. $row['standard_score'],
  1262. $row['money']
  1263. ),
  1264. 'oper_type' => '确认拆分工序',
  1265. 'oper_user_name' => $params['sys_id'],
  1266. 'oper_time' => $operTime,
  1267. ];
  1268. }
  1269. Db::startTrans();
  1270. try {
  1271. $updateResult = Db::table('工单_基础工艺资料')
  1272. ->where('id', $process['id'])
  1273. ->where('status', 'in', [0, '0'])
  1274. ->update(['status' => 1]);
  1275. if ($updateResult === false) {
  1276. throw new \Exception('更新母工序状态失败');
  1277. }
  1278. if ($updateResult === 0) {
  1279. throw new \Exception('母工序状态已变更,请刷新后重试');
  1280. }
  1281. $insertResult = Db::name('工单_基础工艺资料')->insertAll($insertList);
  1282. if ($insertResult === false || $insertResult === 0) {
  1283. throw new \Exception('拆分工序新增失败');
  1284. }
  1285. $logResult = Db::name('work_order_operation_log')->insertAll($logList);
  1286. if ($logResult === false || $logResult === 0) {
  1287. throw new \Exception('写入操作日志失败,请确认日志表已包含 process_code 字段');
  1288. }
  1289. Db::commit();
  1290. } catch (\think\exception\HttpResponseException $e) {
  1291. throw $e;
  1292. } catch (\Exception $e) {
  1293. Db::rollback();
  1294. $this->error('修改失败:' . $e->getMessage());
  1295. }
  1296. $this->success('确认拆分工序成功', ['count' => $splitCount]);
  1297. }
  1298. /**
  1299. * 按拆分数量分配数值:不能整除时第一条为剩余后的最大值,其余为向下取整的平均值
  1300. * @param mixed $total
  1301. * @param int $count
  1302. * @return array
  1303. */
  1304. private function splitNumericValue($total, $count)
  1305. {
  1306. $count = intval($count);
  1307. if ($count <= 0) {
  1308. return [];
  1309. }
  1310. $total = floatval($total);
  1311. $avg = floor($total / $count * 100) / 100;
  1312. $first = round($total - $avg * ($count - 1), 2);
  1313. $values = [$first];
  1314. for ($i = 1; $i < $count; $i++) {
  1315. $values[] = $avg;
  1316. }
  1317. return $values;
  1318. }
  1319. /**
  1320. * 根据款号查询工单信息
  1321. * @param search 搜索内容
  1322. * @return array
  1323. * @throws \think\db\exception\DataNotFoundException
  1324. * @throws \think\db\exception\ModelNotFoundException
  1325. * @throws \think\exception\DbException
  1326. */
  1327. public function getWorkOrderInfoByStyle()
  1328. {
  1329. if (!$this->request->isGet()) {
  1330. $this->error('请求方法错误');
  1331. }
  1332. $params = $this->request->param();
  1333. if (empty($params['search'])) {
  1334. $this->error('搜索内容不能为空');
  1335. }
  1336. $where = [
  1337. 'a.订单编号|a.生产款号|a.款式' => ['like', '%' . $params['search'] . '%'],
  1338. 'a.Mod_rq' => null,
  1339. ];
  1340. $workOrder = db('工单_基本资料')->alias('a')
  1341. ->join('工单_基础工艺资料 b', 'a.订单编号 = b.work_order')
  1342. ->field('a.订单编号,a.生产款号,a.款式,a.客户编号')
  1343. ->where($where)
  1344. ->group('a.订单编号')
  1345. ->select();
  1346. if (empty($workOrder)) {
  1347. $this->error('未找到工单信息');
  1348. }
  1349. $this->success('成功', $workOrder);
  1350. }
  1351. /**
  1352. * 复制目标工单的部件与工艺到当前工单
  1353. * @param target_workorder 目标工单编号
  1354. * @param current_workorder 当前工单编号
  1355. * @param sys_id 操作人
  1356. */
  1357. public function copyProcessToCurrentWorkOrder()
  1358. {
  1359. if (!$this->request->isPost()) {
  1360. $this->error('请求方法错误');
  1361. }
  1362. $params = $this->request->post();
  1363. if (empty($params['target_workorder'])) {
  1364. $this->error('目标工单编号不能为空');
  1365. }
  1366. if (empty($params['current_workorder'])) {
  1367. $this->error('当前工单编号不能为空');
  1368. }
  1369. if (empty($params['sys_id'])) {
  1370. $this->error('操作人不能为空');
  1371. }
  1372. if ($params['target_workorder'] === $params['current_workorder']) {
  1373. $this->error('目标工单与当前工单不能相同');
  1374. }
  1375. if (!$this->workOrderExists($params['target_workorder'])) {
  1376. $this->error('目标工单不存在');
  1377. }
  1378. if (!$this->workOrderExists($params['current_workorder'])) {
  1379. $this->error('当前工单不存在');
  1380. }
  1381. $targetWorkOrderInfo = $this->getWorkOrderInfo($params['target_workorder']);
  1382. if (empty($targetWorkOrderInfo['partInfo']) && empty($targetWorkOrderInfo['processInfo'])) {
  1383. $this->error('目标工单没有可复制的部件或工艺');
  1384. }
  1385. $currentWorkOrderInfo = $this->getWorkOrderInfo($params['current_workorder']);
  1386. $oldPartCount = count($currentWorkOrderInfo['partInfo']);
  1387. $oldProcessCount = count($currentWorkOrderInfo['processInfo']);
  1388. $now = date('Y-m-d H:i:s');
  1389. $partInsertList = $this->buildWorkOrderPartCopyRows(
  1390. $targetWorkOrderInfo['partInfo'],
  1391. $params['current_workorder'],
  1392. $params['sys_id'],
  1393. $now
  1394. );
  1395. $processInsertList = $this->buildWorkOrderProcessCopyRows(
  1396. $targetWorkOrderInfo['processInfo'],
  1397. $params['current_workorder'],
  1398. $params['sys_id'],
  1399. $now
  1400. );
  1401. $logData = [
  1402. 'order_no' => $params['current_workorder'],
  1403. 'change_field' => $params['target_workorder'],
  1404. 'old_value' => sprintf('部件%d条,工艺%d条', $oldPartCount, $oldProcessCount),
  1405. 'new_value' => sprintf('部件%d条,工艺%d条', count($partInsertList), count($processInsertList)),
  1406. 'oper_type' => '复制工单工艺',
  1407. 'oper_user_name' => $params['sys_id'],
  1408. 'oper_time' => $now,
  1409. ];
  1410. Db::startTrans();
  1411. try {
  1412. if ($oldPartCount > 0 || $oldProcessCount > 0) {
  1413. $partDeleteResult = Db::table('工单_部件资料')
  1414. ->where('work_order', $params['current_workorder'])
  1415. ->whereNull('del_rq')
  1416. ->update(['del_rq' => $now]);
  1417. if ($partDeleteResult === false) {
  1418. throw new \Exception('清除当前工单部件失败');
  1419. }
  1420. $processDeleteResult = Db::table('工单_基础工艺资料')
  1421. ->where('work_order', $params['current_workorder'])
  1422. ->whereNull('del_rq')
  1423. ->update(['del_rq' => $now]);
  1424. if ($processDeleteResult === false) {
  1425. throw new \Exception('清除当前工单工艺失败');
  1426. }
  1427. }
  1428. if (!empty($partInsertList)) {
  1429. $partInsertResult = Db::name('工单_部件资料')->insertAll($partInsertList);
  1430. if ($partInsertResult === false || $partInsertResult === 0) {
  1431. throw new \Exception('复制部件失败');
  1432. }
  1433. }
  1434. if (!empty($processInsertList)) {
  1435. $processInsertResult = Db::name('工单_基础工艺资料')->insertAll($processInsertList);
  1436. if ($processInsertResult === false || $processInsertResult === 0) {
  1437. throw new \Exception('复制工艺失败');
  1438. }
  1439. }
  1440. $logResult = Db::name('work_order_operation_log')->insert($logData);
  1441. if ($logResult === false || $logResult === 0) {
  1442. throw new \Exception('写入操作日志失败');
  1443. }
  1444. Db::commit();
  1445. } catch (\think\exception\HttpResponseException $e) {
  1446. throw $e;
  1447. } catch (\Exception $e) {
  1448. Db::rollback();
  1449. $this->error('复制失败:' . $e->getMessage());
  1450. }
  1451. $this->success('复制成功', [
  1452. 'part_count' => count($partInsertList),
  1453. 'process_count' => count($processInsertList),
  1454. ]);
  1455. }
  1456. /**
  1457. * 判断工单是否存在
  1458. * @param string $workorder
  1459. * @return bool
  1460. */
  1461. private function workOrderExists($workorder)
  1462. {
  1463. return Db::table('工单_基本资料')
  1464. ->where('订单编号', $workorder)
  1465. ->whereNull('Mod_rq')
  1466. ->count() > 0;
  1467. }
  1468. /**
  1469. * 查询工单部件与工艺(未删除、工艺仅 status=0)
  1470. * @param string $workorder
  1471. * @return array
  1472. */
  1473. private function getWorkOrderInfo($workorder)
  1474. {
  1475. $partInfo = db('工单_部件资料')
  1476. ->where('work_order', $workorder)
  1477. ->whereNull('del_rq')
  1478. ->field('part_code,part_name,remark,part_type,status')
  1479. ->order('part_code')
  1480. ->select();
  1481. $processInfo = db('工单_基础工艺资料')
  1482. ->where('work_order', $workorder)
  1483. ->whereNull('del_rq')
  1484. ->where('status', 0)
  1485. ->field('part_code,part_name,process_code,process_name,standard_hour,standard_minutes,
  1486. standard_score,money,coefficient,remark,big_process')
  1487. ->order('process_code')
  1488. ->select();
  1489. return [
  1490. 'partInfo' => $partInfo ?: [],
  1491. 'processInfo' => $processInfo ?: [],
  1492. ];
  1493. }
  1494. /**
  1495. * 组装待复制的部件数据
  1496. */
  1497. private function buildWorkOrderPartCopyRows($partList, $workOrder, $sysId, $operTime)
  1498. {
  1499. $rows = [];
  1500. foreach ($partList as $part) {
  1501. $rows[] = [
  1502. 'work_order' => $workOrder,
  1503. 'part_code' => $part['part_code'],
  1504. 'part_name' => $part['part_name'],
  1505. 'remark' => isset($part['remark']) ? $part['remark'] : '',
  1506. 'part_type' => isset($part['part_type']) ? $part['part_type'] : '',
  1507. 'status' => 0,
  1508. 'sys_id' => $sysId,
  1509. 'sys_rq' => $operTime,
  1510. ];
  1511. }
  1512. return $rows;
  1513. }
  1514. /**
  1515. * 组装待复制的工艺数据
  1516. */
  1517. private function buildWorkOrderProcessCopyRows($processList, $workOrder, $sysId, $operTime)
  1518. {
  1519. $rows = [];
  1520. foreach ($processList as $process) {
  1521. $rows[] = [
  1522. 'work_order' => $workOrder,
  1523. 'part_code' => $process['part_code'],
  1524. 'part_name' => isset($process['part_name']) ? $process['part_name'] : '',
  1525. 'process_code' => $process['process_code'],
  1526. 'process_name' => $process['process_name'],
  1527. 'big_process' => $process['big_process'],
  1528. 'standard_hour' => $process['standard_hour'],
  1529. 'standard_minutes' => $process['standard_minutes'],
  1530. 'standard_score' => $process['standard_score'],
  1531. 'money' => $process['money'],
  1532. 'coefficient' => $process['coefficient'],
  1533. 'remark' => isset($process['remark']) ? $process['remark'] : '',
  1534. 'status' => 0,
  1535. 'pid' => 0,
  1536. 'sys_id' => $sysId,
  1537. 'sys_rq' => $operTime,
  1538. ];
  1539. }
  1540. return $rows;
  1541. }
  1542. /**
  1543. * 批量新增工序资料(请求体为 JSON 数组,每条含 work_order 等字段)
  1544. * @param processList 工序信息
  1545. * @return array
  1546. * @throws \think\db\exception\DataNotFoundException
  1547. * @throws \think\db\exception\ModelNotFoundException
  1548. * @throws \think\exception\DbException
  1549. **/
  1550. public function batchAddProcess()
  1551. {
  1552. if (!$this->request->isPost()) {
  1553. $this->error('请求方法错误');
  1554. }
  1555. $processList = $this->parseBatchProcessList();
  1556. if (empty($processList)) {
  1557. $this->error('工艺数据不能为空');
  1558. }
  1559. $requiredFields = [
  1560. 'work_order' => '工单编号',
  1561. 'part_code' => '部件编号',
  1562. 'part_name' => '部件名称',
  1563. 'process_code' => '工序编号',
  1564. 'process_name' => '工序名称',
  1565. 'big_process' => '大工序',
  1566. 'standard_hour' => '秒',
  1567. 'standard_score' => '定额分',
  1568. 'coefficient' => '难度系数',
  1569. 'standard_minutes' => '分',
  1570. 'money' => '金额',
  1571. 'remark' => '备注',
  1572. 'sys_id' => '操作人员',
  1573. ];
  1574. $workorder = '';
  1575. $now = date('Y-m-d H:i:s');
  1576. $insertList = [];
  1577. $processCodeMap = [];
  1578. $newBatchStatus0Sum = 0;
  1579. foreach ($processList as $index => $process) {
  1580. $rowNo = $index + 1;
  1581. if (!is_array($process)) {
  1582. $this->error('第' . $rowNo . '条工艺数据格式错误');
  1583. }
  1584. // foreach ($requiredFields as $field => $label) {
  1585. // if (!isset($process[$field]) || $process[$field] === '' || $process[$field] === null) {
  1586. // $this->error('第' . $rowNo . '条' . $label . '不能为空');
  1587. // }
  1588. // }
  1589. $itemWorkOrder = trim($process['work_order']);
  1590. if ($workorder === '') {
  1591. $workorder = $itemWorkOrder;
  1592. } elseif ($itemWorkOrder !== $workorder) {
  1593. $this->error('第' . $rowNo . '条工单编号与其它数据不一致');
  1594. }
  1595. $processCode = (string)$process['process_code'];
  1596. if (isset($processCodeMap[$processCode])) {
  1597. $this->error('批量数据中存在重复的工序编号:' . $processCode);
  1598. }
  1599. $processCodeMap[$processCode] = true;
  1600. $status = isset($process['status']) ? $process['status'] : 0;
  1601. if (in_array($status, [0, '0'], true)) {
  1602. $newBatchStatus0Sum += floatval($process['standard_score']);
  1603. }
  1604. $insertList[] = [
  1605. 'work_order' => $workorder,
  1606. 'part_code' => $process['part_code'],
  1607. 'part_name' => $process['part_name'],
  1608. 'process_code' => $processCode,
  1609. 'process_name' => $process['process_name'],
  1610. 'big_process' => $process['big_process'],
  1611. 'standard_hour' => $process['standard_hour'],
  1612. 'standard_minutes' => $process['standard_minutes'],
  1613. 'standard_score' => $process['standard_score'],
  1614. 'coefficient' => $process['coefficient'],
  1615. 'money' => $process['money'],
  1616. 'remark' => $process['remark'],
  1617. 'status' => $status,
  1618. 'sys_id' => $process['sys_id'],
  1619. 'sys_rq' => $now,
  1620. ];
  1621. }
  1622. $existingCodes = Db::table('工单_基础工艺资料')
  1623. ->where('work_order', $workorder)
  1624. ->whereIn('process_code', array_keys($processCodeMap))
  1625. ->whereNull('del_rq')
  1626. ->column('process_code');
  1627. if (!empty($existingCodes)) {
  1628. $this->error('工序编号已存在:' . $existingCodes[0]);
  1629. }
  1630. $workOrder = Db::table('工单_基本资料')
  1631. ->where('订单编号', $workorder)
  1632. ->where('Mod_rq', null)
  1633. ->field('订单编号,计划制造工分')
  1634. ->find();
  1635. if (empty($workOrder)) {
  1636. $this->error('工单不存在');
  1637. }
  1638. $existingTotal = Db::table('工单_基础工艺资料')
  1639. ->where('work_order', $workorder)
  1640. ->where('status', 0)
  1641. ->whereNull('del_rq')
  1642. ->sum('standard_score');
  1643. $existingTotal = $existingTotal ? floatval($existingTotal) : 0;
  1644. if ($existingTotal + $newBatchStatus0Sum > floatval($workOrder['计划制造工分'])) {
  1645. $this->error('工序定额分和大于制造工分,请确认之后再修改');
  1646. }
  1647. Db::startTrans();
  1648. try {
  1649. $insertResult = Db::name('工单_基础工艺资料')->insertAll($insertList);
  1650. if ($insertResult === false || $insertResult === 0) {
  1651. throw new \Exception('批量新增工序失败');
  1652. }
  1653. Db::commit();
  1654. } catch (\think\exception\HttpResponseException $e) {
  1655. throw $e;
  1656. } catch (\Exception $e) {
  1657. Db::rollback();
  1658. $this->error('批量新增工序资料失败');
  1659. }
  1660. $this->success('批量新增工序资料成功', ['count' => count($insertList)]);
  1661. }
  1662. /**
  1663. * 解析批量工序请求体(form 数组 / JSON 数组 / 单字段 JSON 字符串)
  1664. * @return array
  1665. */
  1666. private function parseBatchProcessList()
  1667. {
  1668. $params = $this->request->post();
  1669. if (isset($params[0]) && is_array($params[0])) {
  1670. return $params;
  1671. }
  1672. $decoded = json_decode($this->request->getInput(), true);
  1673. if (is_array($decoded) && isset($decoded[0]) && is_array($decoded[0])) {
  1674. return $decoded;
  1675. }
  1676. $jsonStr = is_string($params) ? $params : (count($params) === 1 ? reset($params) : null);
  1677. if (is_string($jsonStr)) {
  1678. $decoded = json_decode($jsonStr, true);
  1679. return is_array($decoded) ? $decoded : [];
  1680. }
  1681. return [];
  1682. }
  1683. /**
  1684. * 工序产量核查:按工单查询报工汇总(工单信息 / 工序列表 / 人员报工明细)
  1685. * @param workorder 工单编号
  1686. * @param process 工序(可选,按big_process过滤)
  1687. * @return array
  1688. */
  1689. public function checkProcessProduction()
  1690. {
  1691. if (!$this->request->isGet()) {
  1692. $this->error('请求方法错误');
  1693. }
  1694. $params = $this->request->param();
  1695. if (empty($params['workorder'])) {
  1696. $this->error('工单编号不能为空');
  1697. }
  1698. $workorder = $params['workorder'];
  1699. $processFilter = $params['process'] ?? ''; // 可选的工序过滤
  1700. // 1. 查询工单基本信息
  1701. $workOrderInfo = Db::name('工单_基本资料')
  1702. ->where('订单编号', $workorder)
  1703. ->whereNull('Mod_rq')
  1704. ->field('订单编号, 生产款号, 款式')
  1705. ->find();
  1706. if (empty($workOrderInfo)) {
  1707. $this->error('工单不存在');
  1708. }
  1709. // 2. 查询工单基础工艺资料(所有工序)
  1710. $processQuery = Db::name('工单_基础工艺资料')
  1711. ->where('work_order', $workorder)
  1712. ->whereNull('del_rq');
  1713. // 如果传了工序参数,按 big_process 过滤
  1714. if (!empty($processFilter)) {
  1715. $processQuery->where('big_process', $processFilter);
  1716. }
  1717. $processList = $processQuery
  1718. ->field('process_code as 工序号, process_name as 工序名称, part_name as 部件名称')
  1719. ->select();
  1720. if (empty($processList)) {
  1721. $this->error('该工单暂无工艺资料');
  1722. }
  1723. // 按工序号数字排序
  1724. usort($processList, function ($a, $b) {
  1725. return intval($a['工序号']) - intval($b['工序号']);
  1726. });
  1727. // 3. 查询设备_工分计酬中的报工数据
  1728. $reportList = Db::name('设备_工分计酬')->alias('a')
  1729. ->where('a.work_order', $workorder)
  1730. ->whereNull('a.del_rq')
  1731. ->field('a.process_code as 工序号, a.process_name as 工序名称, a.staff_name as 员工姓名,
  1732. a.sys_id as 小组,
  1733. SUM(a.number) as 数量, MIN(a.date) as 开工日期, MAX(a.date) as 完工日期,
  1734. SUM(a.standard_score) as 工分, SUM(a.production_score) as 工时, SUM(a.salary) as 工资')
  1735. ->group('a.process_code, a.process_name, a.staff_no, a.staff_name')
  1736. ->select();
  1737. // 按工序号、员工号数字排序
  1738. if (!empty($reportList)) {
  1739. usort($reportList, function ($a, $b) {
  1740. $codeCmp = intval($a['工序号']) - intval($b['工序号']);
  1741. if ($codeCmp !== 0) return $codeCmp;
  1742. return intval($a['员工号'] ?? 0) - intval($b['员工号'] ?? 0);
  1743. });
  1744. }
  1745. // 4. 组装返回数据
  1746. $data = [
  1747. '订单编号' => $workOrderInfo['订单编号'],
  1748. '生产款号' => $workOrderInfo['生产款号'],
  1749. '款式' => $workOrderInfo['款式'],
  1750. '工序列表' => [],
  1751. ];
  1752. // 5. 先构建基础工艺列表(按工序号+工序名称分组)
  1753. $processMap = [];
  1754. foreach ($processList as $process) {
  1755. $processKey = (string)$process['工序号'] . '|' . (string)$process['工序名称'];
  1756. if (!isset($processMap[$processKey])) {
  1757. $processMap[$processKey] = [
  1758. '工序号' => $process['工序号'],
  1759. '工序名称' => $process['工序名称'],
  1760. '部件名称' => $process['部件名称'] ?? '',
  1761. 'staffs' => [],
  1762. ];
  1763. }
  1764. }
  1765. // 6. 将报工数据合并到对应工序
  1766. if (!empty($reportList)) {
  1767. foreach ($reportList as $report) {
  1768. $processKey = (string)$report['工序号'] . '|' . (string)$report['工序名称'];
  1769. if (isset($processMap[$processKey])) {
  1770. $startDate = !empty($report['开工日期']) ? date('Y-m-d', strtotime($report['开工日期'])) : '';
  1771. $endDate = !empty($report['完工日期']) ? date('Y-m-d', strtotime($report['完工日期'])) : '';
  1772. $processMap[$processKey]['staffs'][] = [
  1773. '员工姓名' => $report['员工姓名'],
  1774. '小组' => $report['小组'],
  1775. '数量' => floatval($report['数量']),
  1776. '开工日期' => $startDate,
  1777. '完工日期' => $endDate,
  1778. '工分' => round(floatval($report['工分']), 4),
  1779. '工时' => round(floatval($report['工时']), 4),
  1780. '工资' => round(floatval($report['工资']), 4),
  1781. ];
  1782. }
  1783. }
  1784. }
  1785. $resultList = array_values($processMap);
  1786. // 按工序号数字排序
  1787. usort($resultList, function ($a, $b) {
  1788. return intval($a['工序号']) - intval($b['工序号']);
  1789. });
  1790. $data['工序列表'] = $resultList;
  1791. $this->success('成功', $data);
  1792. }
  1793. }