Feeding.php 25 KB

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