Feeding.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Cache;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use think\Session;
  9. /**
  10. * 生产投料
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Feeding extends Backend
  15. {
  16. protected $searchFields = 'material,bach';
  17. /**
  18. * Feeding模型对象
  19. * @var \app\admin\model\Feeding
  20. */
  21. protected $model = null;
  22. protected $noNeedRight = ['get_formula','get_task','get_material'];
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\Feeding;
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 添加
  35. *
  36. * @return string
  37. * @throws \think\Exception
  38. */
  39. public function add()
  40. {
  41. if (false === $this->request->isPost()) {
  42. //车间=>操作人员
  43. $cjcz = Db::name('personnel')->where('bid',"=",5)->where('position','=',"cjcz")->order('name desc')->select();
  44. //车间=>检验人员
  45. $cjjy = Db::name('personnel')->where('bid',"=",5)->where('position','=',"cjjy")->order('name desc')->select();
  46. $this->assign('cjcz',$cjcz);
  47. $this->assign('cjjy',$cjjy);
  48. //通过账号自动获取 操作人员 检验人员 固定机台名称
  49. $user_info = Session::get('admin');
  50. $jinayan = [];
  51. if($user_info['username'] === '111'){
  52. $jinayan = '丙类一号机';
  53. }else if($user_info['username'] === '222'){
  54. $jinayan = '丙类二号机';
  55. }else if($user_info['username'] === '333'){
  56. $jinayan = '甲类一号机';
  57. }else if($user_info['username'] === '444'){
  58. $jinayan = '甲类二号机';
  59. }else{
  60. $jinayan = '甲类三号机';
  61. }
  62. $this->assign('jinayan',$jinayan);
  63. return $this->view->fetch();
  64. }
  65. $tid = Db::name('task')->where('bach',$_POST['bach'])->order('id desc')->find();
  66. // $year = date('Y'); // 获取当前年份
  67. //
  68. // $tid = Db::name('task')
  69. // ->where('bach', $_POST['bach']) // 根据批次过滤
  70. // ->where('create', 'like', "$year%") // 使用 LIKE 模糊查询,匹配当前年份
  71. // ->order('id desc') // 按照创建时间降序排序
  72. // ->find(); // 只取最新的一条记录
  73. if(empty($_POST['weight'])){
  74. $this->error('投料数据不能为空');
  75. }
  76. $feeding_no = Db::name('feeding')->where('bach',$_POST['bach'])->order('id desc')->find();
  77. if (isset($feeding_no['no'])) {
  78. // 确保 $feeding_no 是一个数组且 $feeding_no['no'] 已设置
  79. $param['no'] = $feeding_no['no'] + 1;
  80. } else {
  81. // $feeding_no 不存在,或者 $feeding_no['no'] 未设置
  82. $param['no'] = 1;
  83. }
  84. // $param['no'] = $fno['no'] +1;//操作顺序
  85. $param['tid'] = $tid['id'];//关联批次idx
  86. $param['fid'] = $_POST['fid'];//关联批次idx
  87. $param['bach'] = $_POST['bach'];//批次号
  88. $param['date'] = $_POST['date'];//日期
  89. $param['operator'] = $_POST['operator'];//操作人员
  90. $param['inspector'] = $_POST['inspector'];//检验人员
  91. $param['material'] = $_POST['material'];//原材料
  92. $param['nweight'] = $_POST['nweight'];//应投重量
  93. $param['gy_num'] = $_POST['gy_num'];//工艺序号
  94. $param['weight'] = $_POST['weight'];//投料重量
  95. $param['material_bach'] = $_POST['material_bach'];//原材料批次号
  96. $param['remark'] = $_POST['gy_name'];//操作记录
  97. $param['create'] = date('Y-m-d H:i:s');//创建时间
  98. $feeding = Db::name('feeding')->insert($param);
  99. if($feeding){return "添加成功";}else{return "添加失败";}
  100. // $params = $this->request->post('row/a');
  101. // $params = $this->preExcludeFields($params);
  102. // $tid = Db::name('task')->where('bach',$params['bach'])->order('id desc')->find();
  103. // $arr = [];
  104. // if(empty($params['weight'])){
  105. // $this->error('投料数据不能为空');
  106. // }
  107. // foreach($params['weight'] as $k=>$v){
  108. // if($v){
  109. // $param['bach'] = $params['bach'];
  110. // $param['date'] = $params['date'];
  111. // $param['operator'] = $params['operator'];
  112. // $param['inspector'] = $params['inspector'];
  113. // $param['weight'] = $params['weight'][$k];
  114. // $param['nweight'] = $params['nweight'][$k];
  115. // $param['material'] = $params['material'][$k];
  116. // $param['gy_num'] = $params['gy_num'][$k];
  117. // $param['tid'] = $tid['id'];
  118. // $param['create'] = date('Y-m-d H:i:s');
  119. // $arr[] = $param;
  120. // }
  121. // }
  122. // //去掉已经存入数据库的工艺数据
  123. // $list = array();
  124. // foreach ($arr as $key=>$value){
  125. // $map=[];
  126. // $map['bach'] = $value['bach'];
  127. // $map['material'] = $value['material'];
  128. // $map['gy_num'] = $value['gy_num'];
  129. // $is_feeding_gy = Db::name('feeding')->where($map)->order('id desc')->find();
  130. // if (!$is_feeding_gy){
  131. // $list[$key] = $value;
  132. // }
  133. // }
  134. // $result = false;
  135. // Db::startTrans();
  136. // try {
  137. // //是否采用模型验证
  138. // if ($this->modelValidate) {
  139. // $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  140. // $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  141. // $this->model->validateFailException()->validate($validate);
  142. // }
  143. //
  144. // $result = $this->model->allowField(true)->saveAll($list);
  145. // Db::commit();
  146. // } catch (ValidateException|PDOException|Exception $e) {
  147. // Db::rollback();
  148. // $this->error($e->getMessage());
  149. // }
  150. // if ($result === false) {
  151. // $this->error(__('No rows were inserted'));
  152. // }
  153. // $this->success();
  154. }
  155. //投料添加页面=>修改应投重量
  156. // public function submits(){
  157. // $param = $this->request->post();
  158. // $where = [];
  159. // $where['bach'] = $param['bach'];
  160. // $id = Db::name('feeding')->where($where)->select();
  161. // echo "<pre>";
  162. // print_r($id);
  163. // echo "</pre>";die;
  164. // $arr['weight'] = $param['weight'];
  165. // $feeding = Db::name('feeding')->where('id',$id['id'])->select();
  166. // echo "<pre>";
  167. // print_r($feeding);
  168. // echo "</pre>";die;
  169. // if(!$feeding){
  170. // return '修改失败';
  171. // }else{
  172. // return '修改成功';
  173. // }
  174. // }
  175. public function edit($ids = null){
  176. $row = $this->model->get($ids);
  177. if (!$row) {
  178. $this->error(__('No Results were found'));
  179. }
  180. $adminIds = $this->getDataLimitAdminIds();
  181. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  182. $this->error(__('You have no permission'));
  183. }
  184. if (false === $this->request->isPost()) {
  185. $row = Db::name('feeding')->where('id',$ids)->find();
  186. $this->assign('row',$row);
  187. return $this->view->fetch();
  188. }
  189. $params = $this->request->post('row/a');
  190. // echo "<pre>";
  191. // print_r($params);
  192. // echo "</pre>";
  193. // die;
  194. if (empty($params)) {
  195. $this->error(__('Parameter %s can not be empty', ''));
  196. }
  197. $params = $this->preExcludeFields($params);
  198. $result = false;
  199. Db::startTrans();
  200. try {
  201. //第三步
  202. $result = Db::name('feeding')->where('id',$ids)->update($params);
  203. Db::commit();
  204. } catch (ValidateException|PDOException|Exception $e) {
  205. Db::rollback();
  206. $this->error($e->getMessage());
  207. }
  208. if (false === $result) {
  209. $this->error(__('No rows were updated'));
  210. }
  211. $this->success();
  212. }
  213. //获取作业票信息
  214. public function get_task(){
  215. $bach = $this->request->post('bach');
  216. $year = date('Y'); // 获取当前年份
  217. $row = Db::name('feeding')->where('bach',$bach)->select();
  218. // $res = Db::name('task')->where('bach',$bach)->order('create','desc')->select();
  219. $year = date('Y'); // 获取当前年份
  220. $res = Db::name('task')
  221. ->where('bach', $bach) // 批次号
  222. // 使用 LIKE 模糊查询,匹配当前年份
  223. ->where('create', 'like', "$year%")
  224. ->order('create desc')
  225. ->select();
  226. //已有过工序
  227. if($row){
  228. $result['inspector'] = $row[0]['inspector'];
  229. $result['operator'] = $row[0]['operator'];
  230. }else{
  231. $result['inspector'] = '';
  232. $result['operator'] = '';
  233. }
  234. $result['data'] = $res;
  235. return json($result);
  236. }
  237. //获取配方信息 原始代码
  238. public function get_formulas(){
  239. $bach = $this->request->post('bach');//批次号
  240. $num = $this->request->post('num');//生产量
  241. $process = Db::name('feeding')->where('bach',$bach)->select();
  242. if($num){//如果有,批次号重复,需精确查找
  243. $res = Db::name('task')->alias('t')
  244. ->join('formula_detail f','f.pid=t.fid','left')
  245. ->field('f.id as fid,f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan')
  246. ->where('t.bach',$bach)->where('t.number',$num)->select();
  247. }else{//如果没有,,批次号未重复,直接差出数据
  248. $res = Db::name('task')->alias('t')
  249. ->join('formula_detail f','t.fid = f.pid','left')
  250. ->field('f.id as fid,f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan')
  251. ->where('t.bach',$bach)->select();
  252. }
  253. $pro = 0;
  254. //按照百分比计算出应投重量
  255. foreach($res as &$v){
  256. $v['weight']=0;
  257. $v['material_bach'] = '';
  258. if($process){ //已有工序,接上一次工序
  259. foreach ($process as $val){
  260. if($val['fid']==$v['fid'] && $val['tid']==$v['id'] && $val['gy_num'] == $v['gy_num']){
  261. $v['weight']=$val['weight'];
  262. $v['material'] = $val['material'];
  263. $v['material_bach']=$val['material_bach'];
  264. $pro = $val['gy_num'];
  265. }
  266. }
  267. }
  268. if($v['gy_name'] == null){
  269. $v['gy_name'] = '';
  270. }
  271. if($v['gy_num'] == null){
  272. $v['gy_num'] = '';
  273. }
  274. if($v['material'] == null){
  275. $v['material'] = '';
  276. }
  277. if($v['material_bach'] == null){
  278. $v['material_bach'] = '';
  279. }
  280. $total = array_column($res,'percentage');
  281. foreach ($total as $key=>$value){
  282. $total[$key] = decode($value);
  283. }
  284. $num = array_sum($total);
  285. if($v['kuodan']){
  286. $kd_sum = $v['number']+$v['kuodan'];
  287. if($v['percentage']){
  288. $number = ceil(decode($v['percentage']) / $num * $kd_sum *1000);
  289. $v['nweight'] = number_format($number/1000,3);
  290. }else{
  291. $v['nweight']='';
  292. }
  293. }else{
  294. if($v['percentage']){
  295. $number = ceil(decode($v['percentage']) / $num * $v['number'] *1000);
  296. $v['nweight'] = number_format($number/1000,3);
  297. }else{
  298. $v['nweight']='';
  299. }
  300. }
  301. }
  302. $row['total']=$num;
  303. $row['data'] = $res;
  304. $row['process'] = $pro+1;
  305. return json($row);
  306. }
  307. //获取配方信息
  308. public function get_formula(){
  309. //按照工序显示,第一个工序做完,才能显示下一个工序
  310. $bach = $this->request->post('bach');//批次号
  311. $num = $this->request->post('num');//生产量
  312. $year = date('Y'); // 获取当前年份
  313. $process = Db::name('feeding')->where('bach',$bach)->where('create', 'like', "$year%")->select();
  314. if($num){//如果有,批次号重复,需精确查找
  315. $res = Db::name('task')->alias('t')
  316. ->join('formula_detail f','f.pid=t.fid','left')
  317. ->field('f.id as fid, f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan')
  318. ->where('t.bach',$bach)
  319. ->where('t.create', 'like', "$year%")
  320. ->where('t.bach',$bach)->where('t.number',$num)->select();
  321. }else{//如果没有,,批次号未重复,直接差出数据
  322. $res = Db::name('task')->alias('t')
  323. ->join('formula_detail f','t.fid = f.pid','left')
  324. ->field('f.id as fid, f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan')
  325. ->where('t.bach',$bach)
  326. ->where('t.create', 'like', "$year%")
  327. ->where('t.bach',$bach)->select();
  328. }
  329. $pro = 0;
  330. //按照百分比计算出应投重量
  331. foreach($res as &$v){
  332. $v['weight']=0;
  333. $v['material_bach'] = '';
  334. if($process){ //已有工序,接上一次工序
  335. foreach ($process as $val){
  336. if( $val['fid'] == $v['fid'] && $val['tid'] == $v['id'] && $val['gy_num'] == $v['gy_num']){
  337. $v['weight']= $val['weight'];
  338. // $v['material'] = $val['material'];
  339. $v['material_bach']=$val['material_bach'];
  340. $pro = $val['gy_num'];
  341. }
  342. }
  343. }
  344. if($v['gy_name'] == null){
  345. $v['gy_name'] = '';
  346. }
  347. if($v['gy_num'] == null){
  348. $v['gy_num'] = '';
  349. }
  350. if($v['material'] == null){
  351. $v['material'] = '';
  352. }
  353. if($v['material_bach'] == null){
  354. $v['material_bach'] = '';
  355. }
  356. $total = array_column($res,'percentage');
  357. foreach ($total as $key=>$value){
  358. $total[$key] = decode($value);
  359. }
  360. $num = array_sum($total);
  361. if($v['kuodan']){
  362. $kd_sum = $v['number']+$v['kuodan'];
  363. if($v['percentage']){
  364. $number = ceil(decode($v['percentage']) / $num * $kd_sum *1000);
  365. $v['nweight'] = number_format($number/1000,3);
  366. }else{
  367. $v['nweight']='';
  368. }
  369. }else{
  370. if($v['percentage']){
  371. $number = ceil(decode($v['percentage']) / $num * $v['number'] *1000);
  372. $v['nweight'] = number_format($number/1000,3);
  373. }else{
  374. $v['nweight']='';
  375. }
  376. }
  377. }
  378. // 计算每个 gy_num 的 material 数量
  379. $gy_num_counts = [];
  380. $originalRes = $res;
  381. foreach ($res as $item) {
  382. if (!empty(trim($item['material'])) && (strpos($item['material'], '*') === false)) {
  383. $gy_num = $item['gy_num'];
  384. if (!isset($gy_num_counts[$gy_num])) {
  385. $gy_num_counts[$gy_num] = 0;
  386. }
  387. $gy_num_counts[$gy_num]++;
  388. }
  389. }
  390. if (empty($process)) {
  391. // 如果 feeding 表为空,则显示当前工序 gy_num = 1
  392. $current_gy_num = 1;
  393. } else {
  394. // 初始化一个数组来存储各个工序的完成情况
  395. $gy_num_status = [];
  396. // 遍历已经做的数据
  397. foreach ($process as $item) {
  398. $gy_num = $item['gy_num'];
  399. if (!isset($gy_num_status[$gy_num])) {
  400. $gy_num_status[$gy_num] = 0;
  401. }
  402. $gy_num_status[$gy_num]++;
  403. }
  404. // 遍历原始数据,检查每个工序是否完成
  405. $current_gy_num = 1;
  406. $all_completed = true;
  407. foreach ($gy_num_counts as $gy_num => $count) {
  408. if (isset($gy_num_status[$gy_num]) && $gy_num_status[$gy_num] === $count) {
  409. $current_gy_num++;
  410. } else {
  411. break;
  412. }
  413. }
  414. // 如果所有工序都完成,则显示下一工序
  415. // if ($all_completed) {
  416. //
  417. // }
  418. }
  419. // 筛选出当前工序的 $res 数据
  420. $filtered_res = array_filter($res, function ($item) use ($current_gy_num) {
  421. return $item['gy_num'] == $current_gy_num;
  422. });
  423. $filtered_res = array_values($filtered_res); // 重置数组索引
  424. // echo "当前工序: " . $current_gy_num;
  425. // echo "<pre>";print_r($filtered_res);echo "</pre>";
  426. if (!empty($filtered_res)) {
  427. $row['total']=$num;
  428. $row['data'] = $filtered_res;
  429. $row['process'] = $pro+1;
  430. return json($row);
  431. } else {
  432. $bach = $this->request->post('bach');//批次号
  433. $num = $this->request->post('num');//生产量
  434. $process = Db::name('feeding')->where('bach',$bach)->select();
  435. if($num){//如果有,批次号重复,需精确查找
  436. $res = Db::name('task')->alias('t')
  437. ->join('formula_detail f','f.pid=t.fid','left')
  438. ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan')
  439. ->where('t.create', 'like', "$year%")
  440. ->where('t.bach',$bach)->where('t.number',$num)->select();
  441. }else{//如果没有,,批次号未重复,直接差出数据
  442. $res = Db::name('task')->alias('t')
  443. ->join('formula_detail f','t.fid = f.pid','left')
  444. ->where('t.create', 'like', "$year%")
  445. ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan')
  446. ->where('t.bach',$bach)->select();
  447. }
  448. $pro = 0;
  449. //按照百分比计算出应投重量
  450. foreach($res as &$v){
  451. $v['weight']=0;
  452. $v['material_bach'] = '';
  453. if($process){ //已有工序,接上一次工序
  454. foreach ($process as $val){
  455. if(($val['material']==$v['material'] || in_array($val['material'],explode('/',$v['material']))) && $val['tid']==$v['id'] && $val['gy_num'] == $v['gy_num']){
  456. $v['weight']=$val['weight'];
  457. $v['material_bach']=$val['material_bach'];
  458. $pro = $val['gy_num'];
  459. }
  460. }
  461. }
  462. if($v['gy_name'] == null){
  463. $v['gy_name'] = '';
  464. }
  465. if($v['gy_num'] == null){
  466. $v['gy_num'] = '';
  467. }
  468. if($v['material'] == null){
  469. $v['material'] = '';
  470. }
  471. if($v['material_bach'] == null){
  472. $v['material_bach'] = '';
  473. }
  474. $total = array_column($res,'percentage');
  475. foreach ($total as $key=>$value){
  476. $total[$key] = decode($value);
  477. }
  478. $num = array_sum($total);
  479. if($v['kuodan']){
  480. $kd_sum = $v['number']+$v['kuodan'];
  481. if($v['percentage']){
  482. $number = ceil(decode($v['percentage']) / $num * $kd_sum *1000);
  483. $v['nweight'] = number_format($number/1000,3);
  484. }else{
  485. $v['nweight']='';
  486. }
  487. }else{
  488. if($v['percentage']){
  489. $number = ceil(decode($v['percentage']) / $num * $v['number'] *1000);
  490. $v['nweight'] = number_format($number/1000,3);
  491. }else{
  492. $v['nweight']='';
  493. }
  494. }
  495. }
  496. $row['total']=$num;
  497. $row['data'] = $res;
  498. $row['process'] = $pro+1;
  499. return json($row);
  500. }
  501. }
  502. public function get_material(){
  503. $bach = $this->request->post('bach');//批次号
  504. $material = $this->request->post('material');//原材料
  505. if (!$bach || !$material){
  506. $this->error('扫码数据不能为空');
  507. }
  508. $task = Db::name('task')->where('bach',$bach)->find();
  509. $map = [];
  510. $map['f_name'] = $task['name'];
  511. $map['name'] = $material;
  512. $data = Db::name('formula_material')->where($map)->select();
  513. $result['data'] = '';
  514. if ($data){
  515. $result['data'] = $data;
  516. if (count($data)>1){
  517. $str = [];
  518. foreach ($data as $key=>$value){
  519. $str[$key] = $value['m_name'];
  520. }
  521. $result['string'] = implode('或 ',$str);
  522. }else{
  523. $result['string'] = $data[0]['m_name'];
  524. }
  525. $result['total'] = count($data);
  526. }else{
  527. $result['total'] = 0;
  528. }
  529. return json($result);
  530. }
  531. //查找替代料
  532. /*public function replace(){
  533. $bach = $this->request->post('bach');
  534. $wuliao = $this->request->post('wuliao');
  535. $res = Db::name('task')->alias('t')
  536. ->join('formula f','f.id = t.fid','left')
  537. ->join('formula_detail fd','fd.pid = f.id','left')
  538. ->join('formula_replace fr','fr.fid = fd.id','left')
  539. ->where('t.bach',$bach)->where('is_replace=1')->where('fr.material',$wuliao)
  540. ->field('fd.material,fd.id')->find();
  541. $res['yuan'] = explode('/',$res['material']);
  542. $key = array_search($wuliao, $res['yuan']);
  543. if ($key !== false) array_splice($res['yuan'], $key, 1);
  544. return json($res);
  545. }*/
  546. public function cheng(){
  547. $user_info = Session::get('');
  548. return json($user_info);
  549. }
  550. //获取机台
  551. // public function matchCode() {
  552. // $code = $this->request->post('code');
  553. // preg_match('/^(M-\d{2,3})/', $code, $matches);
  554. // $res = Db::name('formula_detail')->where('material', 'like', $matches[0].'%')->select();
  555. // halt($res);
  556. // return json(Db::name('formula_detail')->where('material', 'like', $matches[0].'%')->select());
  557. // }
  558. public function matchCode() {
  559. $code = $this->request->post('code');
  560. preg_match('/^(M-\d{2,3})/', $code, $matches);
  561. $res = Db::name('formula_detail')->field('distinct material')->where('material', 'like', $matches[0].'%')->select();
  562. // 初始化结果数组
  563. $formattedMaterials = [];
  564. // 遍历查询结果
  565. foreach ($res as $item) {
  566. $material = $item['material'];
  567. // 去除括号和括号内内容
  568. $material = preg_replace('/\s*$.*?$\s*/', '', $material);
  569. // 分割材料字符串
  570. $parts = explode('/', $material);
  571. foreach ($parts as $part) {
  572. // 清除空格
  573. $part = trim($part);
  574. if (preg_match('/^(M-\d{2,3})$/', $part)) {
  575. // 纯基本代码,无需添加
  576. $formattedMaterials[] = $part;
  577. } elseif (preg_match('/^(M-\d{2,3})([A-Z]+)$/', $part, $partMatches)) {
  578. // 代码与后缀的组合
  579. $formattedMaterials[] = $part;
  580. } elseif (preg_match('/^[A-Z]+$/i', $part)) {
  581. // 只有后缀,需要与基本代码拼接
  582. $suffixes = str_split($part);
  583. foreach ($suffixes as $suffix) {
  584. $formattedMaterials[] = $matches[0] . $suffix;
  585. }
  586. }
  587. }
  588. }
  589. // 去除重复项并重新排序
  590. $formattedMaterials = array_unique($formattedMaterials);
  591. if (array_search($code, $formattedMaterials) === false) {
  592. // todo.. 写入一个临时物料数据 materials name
  593. array_push($formattedMaterials, $code);
  594. }
  595. sort($formattedMaterials);
  596. // 返回JSON格式的数据
  597. return json($formattedMaterials);
  598. }
  599. }