ProductionLot.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. /**
  5. * 工单生产批次信息查询
  6. */
  7. class ProductionLot extends Api
  8. {
  9. protected $noNeedLogin = ['*'];
  10. protected $noNeedRight = ['*'];
  11. /**
  12. * 获取查询工单列表
  13. * @return void
  14. * @throws \think\db\exception\DataNotFoundException
  15. * @throws \think\db\exception\ModelNotFoundException
  16. * @throws \think\exception\DbException
  17. */
  18. public function GetOrderList()
  19. {
  20. if ($this->request->isGet() === false){
  21. $this->success('请求错误');
  22. }
  23. $param = $this->request->param();
  24. if (empty($param['search'])){
  25. $where['a.Mod_rq'] = ['between',[date('Y-m-d 00:00:00',time()-1209600),date('Y-m-d H:i:s',time())]];
  26. }else{
  27. $where['Gd_gdbh|Gd_客户代号|成品名称|Gd_cpmc'] = ['like','%'.$param['search'].'%'];
  28. }
  29. $page = $param['page'] ?? 1;
  30. $pageSize = $param['pageSize'] ?? 10;
  31. $total = \db('工单_基本资料')
  32. ->alias('a')
  33. ->join('设备_产量计酬 b','a.Gd_gdbh = b.sczl_gdbh and a.行号 = b.sczl_yjno')
  34. ->field('a.Gd_gdbh,a.Gd_cpmc,a.行号')
  35. ->where($where)
  36. ->group('a.Gd_gdbh,a.Gd_cpmc,a.行号')
  37. ->count();
  38. $list = \db('工单_基本资料')
  39. ->alias('a')
  40. ->join('设备_产量计酬 b','a.Gd_gdbh = b.sczl_gdbh and a.行号 = b.sczl_yjno')
  41. ->field('a.Gd_gdbh,a.Gd_cpmc,a.行号')
  42. ->where($where)
  43. ->page($page,$pageSize)
  44. ->group('a.Gd_gdbh,a.Gd_cpmc,a.行号')
  45. ->select();
  46. if (empty($list)){
  47. $this->success('未找到工单');
  48. }
  49. foreach ($list as $key=>$value){
  50. $list[$key]['工单'] = $value['Gd_gdbh'].'-'.$value['行号'].'-'.$value['Gd_cpmc'];
  51. }
  52. $data = [
  53. 'total' => $total,
  54. 'data' => $list,
  55. ];
  56. $this->success('成功',$data);
  57. }
  58. /**
  59. * 获取左侧菜单栏
  60. * @return void
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. * @throws \think\exception\DbException
  64. */
  65. public function GetList()
  66. {
  67. if ($this->request->isGet() === false){
  68. $this->success('参数错误');
  69. }
  70. $param = $this->request->param();
  71. if (empty($param) || isset($param['gdbh']) === false || isset($param['yjno']) === false){
  72. $this->success('参数错误');
  73. }
  74. $list = db('设备_产量计酬')
  75. ->field('DISTINCT(sczl_num) as num')
  76. ->where('sczl_gdbh',$param['gdbh'])
  77. ->where('sczl_yjno',$param['yjno'])
  78. ->order('num')
  79. ->select();
  80. if (empty($list)){
  81. $this->success('未找到流程单信息');
  82. }
  83. // 一次性批量查出该工单在 制程检验_记录 中按 (工单编号, 流程单号) 分组的提交次数(相同提交时间算一次)
  84. $escGdbh = str_replace("'", "''", $param['gdbh']);
  85. $yjno = intval($param['yjno']);
  86. $checkSql = "SELECT `流程单号` AS num, COUNT(DISTINCT `提交时间`) AS cnt FROM `制程检验_记录`
  87. WHERE `工单编号` = '{$escGdbh}' GROUP BY `流程单号`";
  88. $checkRows = \think\Db::query($checkSql);
  89. $checkMap = [];
  90. foreach ($checkRows as $cr) {
  91. $checkMap[(int)$cr['num']] = (int)$cr['cnt'];
  92. }
  93. foreach ($list as $key => $value){
  94. $num = (int)$value['num'];
  95. $sum = isset($checkMap[$num]) ? $checkMap[$num] : 0;
  96. $list[$key]['sum'] = $sum;
  97. $list[$key]['流程单'] = '第'.$value['num'].'个流程单'.($sum > 0 ? ' ('.$sum.')' : '');
  98. }
  99. $this->success('成功',$list);
  100. }
  101. /**
  102. * 获取工单信息
  103. * @return void
  104. * @throws \think\db\exception\DataNotFoundException
  105. * @throws \think\db\exception\ModelNotFoundException
  106. * @throws \think\exception\DbException
  107. */
  108. public function GetOrderDetail()
  109. {
  110. if ($this->request->isGet() === false){
  111. $this->success('请求错误');
  112. }
  113. $param = $this->request->param();
  114. if (empty($param) || isset($param['gdbh']) === false || isset($param['yjno']) === false){
  115. $this->success('参数错误');
  116. }
  117. $list = db('工单_基本资料')
  118. ->field('Gd_gdbh,Gd_cpdh,Gd_cpmc,订单数量,实际投料,计量单位,投料率')
  119. ->where('Gd_gdbh',$param['gdbh'])
  120. ->where('行号',$param['yjno'])
  121. ->find();
  122. if (empty($list)){
  123. $this->success('未找到该工单信息');
  124. }else{
  125. $this->success('成功',$list);
  126. }
  127. }
  128. /**
  129. * 工艺及生产班组数据获取
  130. * @return void
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\ModelNotFoundException
  133. * @throws \think\exception\DbException
  134. */
  135. public function ProcessList()
  136. {
  137. if ($this->request->isGet() === false){
  138. $this->success('请求错误');
  139. }
  140. $param = $this->request->param();
  141. if (!isset($param['gdbh']) || !isset($param['yjno']) || !isset($param['num'])){
  142. $this->success('参数错误');
  143. }
  144. $page = $param['page'] ?? 1;
  145. $pageSize = $param['pageSize'] ?? 50;
  146. $query= db('设备_产量计酬')
  147. ->alias('a')
  148. ->field('a.sczl_num as 流程单号,a.sczl_gxh as 工序号,a.sczl_gxmc as 工序名称,a.sczl_rq as 生产日期,
  149. a.sczl_jtbh as 机台编号,a.sczl_bh1,a.sczl_bh2,a.sczl_bh3,a.sczl_bh4,a.sczl_bh5,a.sczl_bh6,a.sczl_bh7,
  150. a.sczl_bh8,a.sczl_bh9,a.sczl_bh10,d1.员工姓名 as name1,d2.员工姓名 as name2,d3.员工姓名 as name3,d4.员工姓名 as name4,d5.员工姓名 as name5,d6.员工姓名 as name6,d7.员工姓名 as name7,d8.员工姓名 as name8
  151. ,d9.员工姓名 as name9,d10.员工姓名 as name10');
  152. // 循环连接 人事_基本资料 表(仅当 sczl_bh 不为空时才连接)
  153. for ($i = 1; $i <= 10; $i++) {
  154. $field = 'a.sczl_bh' . $i;
  155. $alias = 'd' . $i;
  156. $query->join("人事_基本资料 $alias", "$field = {$alias}.员工编号 AND {$field} IS NOT NULL", 'LEFT');
  157. }
  158. $list = $query->where('a.sczl_gdbh',$param['gdbh'])
  159. ->where('a.sczl_yjno',$param['yjno'])
  160. ->where('a.sczl_num',$param['num'])
  161. ->order('工序号,生产日期')
  162. ->page($page,$pageSize)
  163. ->select();
  164. $total = count($list);
  165. $data = [
  166. 'data' => $list,
  167. 'total' => $total
  168. ];
  169. if (empty($list)){
  170. $this->success('未找到该流程单的工艺数据');
  171. }else{
  172. $this->success('成功',$data);
  173. }
  174. }
  175. /**
  176. * 制程检验记录
  177. * @return void
  178. * @throws \think\db\exception\DataNotFoundException
  179. * @throws \think\db\exception\ModelNotFoundException
  180. * @throws \think\exception\DbException
  181. */
  182. public function ProcessInspection()
  183. {
  184. if ($this->request->isGet() === false){
  185. $this->success('请求错误');
  186. }
  187. $param = $this->request->param();
  188. if (!isset($param['gdbh']) || !isset($param['yjno'])|| !isset($param['num'])){
  189. $this->success('参数错误');
  190. }
  191. // 取出所有明细,PHP 端按 提交时间 分组合并
  192. $rawList = db('制程检验_记录')
  193. ->field('工单编号,流程单号,工序名称,班组编号,类别,检验项目,检验结果,检验备注 as 项目备注,相关标准 as 修改标准,提交时间 as 检验时间')
  194. ->where('工单编号',$param['gdbh'])
  195. ->where('印件号',$param['yjno'])
  196. ->where('流程单号',$param['num'])
  197. ->order('检验时间')
  198. ->select();
  199. // 按 提交时间 分组合并
  200. $grouped = [];
  201. $orderKey = [];
  202. foreach ($rawList as $row) {
  203. $key = $row['检验时间'];
  204. if (!isset($grouped[$key])) {
  205. $grouped[$key] = [
  206. '工单编号' => $row['工单编号'],
  207. '流程单号' => $row['流程单号'],
  208. '工序名称' => $row['工序名称'],
  209. '班组编号' => $row['班组编号'],
  210. '类别' => $row['类别'],
  211. '检验项目' => $row['检验项目'],
  212. '检验结果' => $row['检验结果'],
  213. '项目备注' => $row['项目备注'],
  214. '修改标准' => $row['修改标准'],
  215. '检验时间' => $row['检验时间'],
  216. 'items' => [], // 这一次提交的所有项目
  217. ];
  218. $orderKey[] = $key;
  219. }
  220. $grouped[$key]['items'][] = $row;
  221. }
  222. // 检验项目:只放首个 + "共N项"
  223. $result = [];
  224. foreach ($orderKey as $key) {
  225. $g = $grouped[$key];
  226. $cnt = count($g['items']);
  227. if ($cnt > 1) {
  228. $g['检验项目'] = $g['items'][0]['检验项目'] . ' ... (共' . $cnt . '项)';
  229. }
  230. $result[] = $g;
  231. }
  232. // 按 类别 统计提交次数(相同提交时间算一次;含现场巡查记录)
  233. $escGdbh = str_replace("'", "''", $param['gdbh']);
  234. $yjno = intval($param['yjno']);
  235. $num = intval($param['num']);
  236. $catSql = "SELECT `类别` AS category, COUNT(DISTINCT `提交时间`) AS cnt
  237. FROM `制程检验_记录`
  238. WHERE `工单编号` = '{$escGdbh}' AND `印件号` = {$yjno} AND `流程单号` = {$num}
  239. GROUP BY `类别`";
  240. $catRows = \think\Db::query($catSql);
  241. $categories = [];
  242. $catTotal = 0;
  243. foreach ($catRows as $cr) {
  244. $categories[] = [
  245. 'category' => $cr['category'],
  246. 'count' => (int)$cr['cnt'],
  247. ];
  248. $catTotal += (int)$cr['cnt'];
  249. }
  250. if (empty($result)){
  251. $this->success('未找到制程检验记录');
  252. }
  253. $this->success('成功', ['data' => $result, 'total' => count($result), 'categories' => $categories, 'catTotal' => $catTotal]);
  254. }
  255. /**
  256. * 制程异常记录
  257. * @return void
  258. * @throws \think\db\exception\DataNotFoundException
  259. * @throws \think\db\exception\ModelNotFoundException
  260. * @throws \think\exception\DbException
  261. */
  262. public function ProcessAnomaly()
  263. {
  264. if ($this->request->isGet() === false){
  265. $this->success('请求错误');
  266. }
  267. $param = $this->request->param();
  268. if (!isset($param['gdbh']) || !isset($param['yjno'])|| !isset($param['num'])){
  269. $this->success('参数错误');
  270. }
  271. $list = db('制程检验_记录附加')
  272. ->field('流程单号,缺陷备注')
  273. ->where('工单编号',$param['gdbh'])
  274. ->where('印件号',$param['yjno'])
  275. ->where('流程单号',$param['num'])
  276. ->order('流程单号')
  277. ->select();
  278. if (empty($list)){
  279. $this->success('未找到制程异常记录');
  280. }
  281. foreach ($list as $key => $value){
  282. $list[$key]['数量'] = '';
  283. $list[$key]['用户'] = '';
  284. }
  285. $total = count($list);
  286. $data = [
  287. 'data' => $list,
  288. 'total' => $total
  289. ];
  290. $this->success('成功',$data);
  291. }
  292. /**
  293. * 物料批次信息
  294. * @return void
  295. * @throws \think\db\exception\DataNotFoundException
  296. * @throws \think\db\exception\ModelNotFoundException
  297. * @throws \think\exception\DbException
  298. */
  299. public function MaterialsBatch()
  300. {
  301. // 1. 简化请求方法验证
  302. if (!$this->request->isGet()) {
  303. $this->success('请求错误');
  304. }
  305. // 2. 集中参数验证
  306. $requiredParams = ['gdbh', 'yjno', 'num'];
  307. $param = $this->request->param();
  308. $missingParams = [];
  309. foreach ($requiredParams as $requiredParam) {
  310. if (!isset($param[$requiredParam])) {
  311. $missingParams[] = $requiredParam;
  312. }
  313. }
  314. if (!empty($missingParams)) {
  315. $this->success('参数错误,缺少:' . implode(', ', $missingParams));
  316. }
  317. // 3. 使用常量或配置定义静态数据
  318. $department = [
  319. 'Y200' => ['裁切','滚切','胶印','上光','切废'],
  320. 'Y201' => ['丝印','喷码','覆膜'],
  321. 'Y202' => ['卷凹','单凹','圆烫','圆切','复卷','分条','配卷','拼板','机检(圆切)','手检(圆切)','包装(圆切)'],
  322. 'Y203' => ['烫金','烫金+凹凸(压纹)','模切','模切+凹凸(压纹)','凹凸(压纹)','拆片','覆膜','对裱'],
  323. 'Y204' => ['手检','次品检','机检(初检)','二次机检','核检','抽检','包装'],
  324. '200' => ['裁切','滚切','胶印','上光','切废'],
  325. '201' => ['丝印','喷码','覆膜'],
  326. '202' => ['卷凹','单凹','圆烫','圆切','复卷','分条','配卷','拼板','机检(圆切)','手检(圆切)','包装(圆切)'],
  327. '203' => ['烫金','烫金+凹凸(压纹)','模切','模切+凹凸(压纹)','凹凸(压纹)','拆片','覆膜','对裱'],
  328. '204' => ['手检','次品检','机检(初检)','二次机检','核检','抽检','包装'],
  329. ];
  330. $validWarehouseCodes = ['101','Y101','102','Y102','103','Y103','200','201','202','203','204','Y200','Y201','Y202','Y203','Y204'];
  331. // 4. 优化数据库查询,使用链式操作提高可读性
  332. $list = db('物料_收发记录')
  333. ->alias('a')
  334. ->join('物料_存货编码 b', 'a.st_wlbh = b.物料代码')
  335. ->field([
  336. 'a.st_wlbh as 物料编号',
  337. 'a.供方批次',
  338. 'a.仓库编号',
  339. 'rtrim(b.物料名称) as 物料名称'
  340. ])
  341. ->where('a.st_gdbh', $param['gdbh'])
  342. ->where('a.cpdh', $param['cpdh'] ?? '') // 使用空值合并运算符
  343. ->whereNotNull('a.供方批次')
  344. ->group('a.st_wlbh,a.供方批次')
  345. ->select();
  346. if (empty($list)) {
  347. $this->success('未找到物料资料');
  348. }
  349. // 5. 提前查询工序数据
  350. $process = db('设备_产量计酬')
  351. ->alias('a')
  352. ->join('工单_工艺资料 b', 'a.sczl_gdbh = b.Gy0_gdbh AND a.sczl_yjno = b.Gy0_yjno AND a.sczl_gxh = b.Gy0_gxh')
  353. ->where([
  354. 'a.sczl_gdbh' => $param['gdbh'],
  355. 'a.sczl_yjno' => $param['yjno']
  356. ])
  357. ->field([
  358. 'a.sczl_gxh as 工序号',
  359. 'b.Gy0_gxmc',
  360. 'a.sczl_gxmc as 工序名称'
  361. ])
  362. ->group('a.sczl_gxh')
  363. ->select();
  364. // 6. 重构数据处理逻辑
  365. $data = [];
  366. $processMap = []; // 缓存工序数据
  367. // 如果需要按工序名称快速查找,可以先建立映射
  368. foreach ($process as $proc) {
  369. $processMap[$proc['Gy0_gxmc']] = $proc['工序名称'];
  370. }
  371. foreach ($list as $item) {
  372. $warehouseCode = $item['仓库编号'];
  373. $baseData = [
  374. '流程单号' => $param['num'],
  375. '物料编号' => $item['物料编号'],
  376. '物料名称' => $item['物料名称'],
  377. '供方批次' => $item['供方批次']
  378. ];
  379. // 7. 优化条件判断逻辑
  380. if (in_array($warehouseCode, $validWarehouseCodes, true)) {
  381. if ($warehouseCode === '101' || $warehouseCode === 'Y101' || $warehouseCode === '102' || $warehouseCode === 'Y102' || $warehouseCode === '103' || $warehouseCode === 'Y103') {
  382. if (!empty($process[0])) {
  383. $baseData['工序名称'] = $process[0]['工序名称'];
  384. }
  385. } else {
  386. // 其他仓库的处理逻辑
  387. if (isset($department[$warehouseCode])) {
  388. $relatedProcesses = $department[$warehouseCode];
  389. foreach ($relatedProcesses as $processName) {
  390. if (isset($processMap[$processName])) {
  391. $baseData['工序名称'] = $processMap[$processName];
  392. }
  393. }
  394. }
  395. }
  396. }
  397. $data[] = $baseData;
  398. }
  399. $result = [
  400. 'data' => $data,
  401. 'total' => count($data),
  402. ];
  403. $this->success('成功',$result);
  404. }
  405. /**
  406. * 印版记录
  407. * @return void
  408. * @throws \think\db\exception\DataNotFoundException
  409. * @throws \think\db\exception\ModelNotFoundException
  410. * @throws \think\exception\DbException
  411. */
  412. public function PrintingPlateList()
  413. {
  414. // 1. 请求方法验证
  415. if (!$this->request->isGet()) {
  416. $this->success('请求错误');
  417. }
  418. // 2. 参数验证与提取
  419. $param = $this->request->param();
  420. $requiredParams = ['gdbh', 'yjno'];
  421. foreach ($requiredParams as $requiredParam) {
  422. if (empty($param[$requiredParam])) {
  423. $this->success("参数错误,缺少:{$requiredParam}");
  424. }
  425. }
  426. $gdbh = $param['gdbh'];
  427. $yjno = $param['yjno'];
  428. $page = $param['page'] ?? 1;
  429. $pageSize = $param['pageSize'] ?? 2;
  430. // 3. 主查询 - 获取印版领用记录
  431. $list = db('工单_印版领用记录')
  432. ->alias('a')
  433. ->join('物料_存货编码 b', 'a.Yb_存货编码 = b.物料代码')
  434. ->where([
  435. 'a.Yb_工单编号' => $gdbh,
  436. 'a.Yb_印件号' => $yjno,
  437. 'a.Yb_领用机台' => ['<>', '']
  438. ])
  439. ->field([
  440. 'a.Yb_存货编码 as 存货编码',
  441. 'a.Yb_供方批号 as 供方批号',
  442. 'a.Yb_领用机台 as 领用机台',
  443. 'TRIM(b.物料名称) as 物料名称', // 使用TRIM替代rtrim
  444. 'a.Sys_id as 用户'
  445. ])
  446. ->group('a.Yb_存货编码, a.Yb_供方批号') // 明确指定表字段
  447. ->order('a.Yb_存货编码')
  448. ->page($page,$pageSize)
  449. ->select();
  450. if (empty($list)) {
  451. $this->success('未找到印版数据');
  452. }
  453. // 4. 收集所有机台号,准备批量查询
  454. $machineCodes = array_column($list, '领用机台');
  455. $machineCodes = array_unique(array_filter($machineCodes)); // 去重并过滤空值
  456. // 5. 批量查询产量计酬数据 - 解决N+1查询问题
  457. $processMap = [];
  458. if (!empty($machineCodes)) {
  459. $processList = db('设备_产量计酬')
  460. ->where([
  461. 'sczl_gdbh' => $gdbh,
  462. 'sczl_yjno' => $yjno,
  463. 'sczl_jtbh' => ['IN', $machineCodes]
  464. ])
  465. ->field([
  466. 'sczl_jtbh as 机台编号',
  467. 'sczl_gxmc as 工序名称',
  468. 'sczl_num as 流程单号'
  469. ])
  470. ->order('sczl_num, sczl_jtbh')
  471. ->select();
  472. // 按机台编号分组,建立映射
  473. foreach ($processList as $process) {
  474. $machineCode = $process['机台编号'];
  475. if (!isset($processMap[$machineCode])) {
  476. $processMap[$machineCode] = [];
  477. }
  478. $processMap[$machineCode][] = [
  479. '工序名称' => $process['工序名称'],
  480. '流程单号' => $process['流程单号']
  481. ];
  482. }
  483. }
  484. // 6. 数据组装
  485. $data = [];
  486. foreach ($list as $item) {
  487. $machineCode = $item['领用机台'];
  488. $baseData = [
  489. '存货编码' => $item['存货编码'],
  490. '供方批号' => $item['供方批号'],
  491. '领用机台' => $machineCode,
  492. '物料名称' => $item['物料名称'],
  493. '用户' => $item['用户']
  494. ];
  495. // 如果该机台有对应的工序数据
  496. if (isset($processMap[$machineCode]) && !empty($processMap[$machineCode])) {
  497. foreach ($processMap[$machineCode] as $process) {
  498. $data[] = array_merge($baseData, $process);
  499. }
  500. } else {
  501. // 如果机台没有对应工序,至少返回基础信息
  502. $data[] = array_merge($baseData, [
  503. '工序名称' => '',
  504. '流程单号' => ''
  505. ]);
  506. }
  507. }
  508. // 7. 按流程单号排序(如果需要的话)
  509. usort($data, function($a, $b) {
  510. return strcmp($a['流程单号'] ?? '', $b['流程单号'] ?? '');
  511. });
  512. $result = [
  513. 'data' => $data,
  514. 'total' => count($data),
  515. ];
  516. $this->success('成功', $result);
  517. }
  518. }