ProductionLot.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. foreach ($list as $key => $value){
  84. $list[$key]['流程单'] = '第'.$value['num'].'个流程单';
  85. }
  86. $this->success('成功',$list);
  87. }
  88. /**
  89. * 获取工单信息
  90. * @return void
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. * @throws \think\exception\DbException
  94. */
  95. public function GetOrderDetail()
  96. {
  97. if ($this->request->isGet() === false){
  98. $this->success('请求错误');
  99. }
  100. $param = $this->request->param();
  101. if (empty($param) || isset($param['gdbh']) === false || isset($param['yjno']) === false){
  102. $this->success('参数错误');
  103. }
  104. $list = db('工单_基本资料')
  105. ->field('Gd_gdbh,Gd_cpdh,Gd_cpmc,订单数量,实际投料,计量单位,投料率')
  106. ->where('Gd_gdbh',$param['gdbh'])
  107. ->where('行号',$param['yjno'])
  108. ->find();
  109. if (empty($list)){
  110. $this->success('未找到该工单信息');
  111. }else{
  112. $this->success('成功',$list);
  113. }
  114. }
  115. /**
  116. * 工艺及生产班组数据获取
  117. * @return void
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. * @throws \think\exception\DbException
  121. */
  122. public function ProcessList()
  123. {
  124. if ($this->request->isGet() === false){
  125. $this->success('请求错误');
  126. }
  127. $param = $this->request->param();
  128. if (!isset($param['gdbh']) || !isset($param['yjno']) || !isset($param['num'])){
  129. $this->success('参数错误');
  130. }
  131. $page = $param['page'] ?? 1;
  132. $pageSize = $param['pageSize'] ?? 50;
  133. $query= db('设备_产量计酬')
  134. ->alias('a')
  135. ->field('a.sczl_num as 流程单号,a.sczl_gxh as 工序号,a.sczl_gxmc as 工序名称,a.sczl_rq as 生产日期,
  136. 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,
  137. 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
  138. ,d9.员工姓名 as name9,d10.员工姓名 as name10');
  139. // 循环连接 人事_基本资料 表(仅当 sczl_bh 不为空时才连接)
  140. for ($i = 1; $i <= 10; $i++) {
  141. $field = 'a.sczl_bh' . $i;
  142. $alias = 'd' . $i;
  143. $query->join("人事_基本资料 $alias", "$field = {$alias}.员工编号 AND {$field} IS NOT NULL", 'LEFT');
  144. }
  145. $list = $query->where('a.sczl_gdbh',$param['gdbh'])
  146. ->where('a.sczl_yjno',$param['yjno'])
  147. ->where('a.sczl_num',$param['num'])
  148. ->order('工序号,生产日期')
  149. ->page($page,$pageSize)
  150. ->select();
  151. $total = count($list);
  152. $data = [
  153. 'data' => $list,
  154. 'total' => $total
  155. ];
  156. if (empty($list)){
  157. $this->success('未找到该流程单的工艺数据');
  158. }else{
  159. $this->success('成功',$data);
  160. }
  161. }
  162. /**
  163. * 制程检验记录
  164. * @return void
  165. * @throws \think\db\exception\DataNotFoundException
  166. * @throws \think\db\exception\ModelNotFoundException
  167. * @throws \think\exception\DbException
  168. */
  169. public function ProcessInspection()
  170. {
  171. if ($this->request->isGet() === false){
  172. $this->success('请求错误');
  173. }
  174. $param = $this->request->param();
  175. if (!isset($param['gdbh']) || !isset($param['yjno'])|| !isset($param['num'])){
  176. $this->success('参数错误');
  177. }
  178. $page = $param['page'] ?? 1;
  179. $pageSize = $param['pageSize'] ?? 50;
  180. $list = db('制程检验_记录')
  181. ->field('工单编号,流程单号,工序名称,班组编号,类别,检验项目,检验结果,检验备注 as 项目备注,相关标准 as 修改标准')
  182. ->where('工单编号',$param['gdbh'])
  183. ->where('印件号',$param['yjno'])
  184. ->where('流程单号',$param['num'])
  185. ->where('类别', 'neq', '现场巡查记录')
  186. ->order('流程单号')
  187. ->page($page,$pageSize)
  188. ->select();
  189. $total = count($list);
  190. $data = [
  191. 'data' => $list,
  192. 'total' => $total
  193. ];
  194. if (empty($list)){
  195. $this->success('未找到制程检验记录');
  196. }
  197. $this->success('成功',$data);
  198. }
  199. /**
  200. * 制程异常记录
  201. * @return void
  202. * @throws \think\db\exception\DataNotFoundException
  203. * @throws \think\db\exception\ModelNotFoundException
  204. * @throws \think\exception\DbException
  205. */
  206. public function ProcessAnomaly()
  207. {
  208. if ($this->request->isGet() === false){
  209. $this->success('请求错误');
  210. }
  211. $param = $this->request->param();
  212. if (!isset($param['gdbh']) || !isset($param['yjno'])|| !isset($param['num'])){
  213. $this->success('参数错误');
  214. }
  215. $list = db('制程检验_记录附加')
  216. ->field('流程单号,缺陷备注')
  217. ->where('工单编号',$param['gdbh'])
  218. ->where('印件号',$param['yjno'])
  219. ->where('流程单号',$param['num'])
  220. ->order('流程单号')
  221. ->select();
  222. if (empty($list)){
  223. $this->success('未找到制程异常记录');
  224. }
  225. foreach ($list as $key => $value){
  226. $list[$key]['数量'] = '';
  227. $list[$key]['用户'] = '';
  228. }
  229. $total = count($list);
  230. $data = [
  231. 'data' => $list,
  232. 'total' => $total
  233. ];
  234. $this->success('成功',$data);
  235. }
  236. /**
  237. * 物料批次信息
  238. * @return void
  239. * @throws \think\db\exception\DataNotFoundException
  240. * @throws \think\db\exception\ModelNotFoundException
  241. * @throws \think\exception\DbException
  242. */
  243. public function MaterialsBatch()
  244. {
  245. // 1. 简化请求方法验证
  246. if (!$this->request->isGet()) {
  247. $this->success('请求错误');
  248. }
  249. // 2. 集中参数验证
  250. $requiredParams = ['gdbh', 'yjno', 'num'];
  251. $param = $this->request->param();
  252. $missingParams = [];
  253. foreach ($requiredParams as $requiredParam) {
  254. if (!isset($param[$requiredParam])) {
  255. $missingParams[] = $requiredParam;
  256. }
  257. }
  258. if (!empty($missingParams)) {
  259. $this->success('参数错误,缺少:' . implode(', ', $missingParams));
  260. }
  261. // 3. 使用常量或配置定义静态数据
  262. $department = [
  263. 'Y200' => ['裁切','滚切','胶印','上光','切废'],
  264. 'Y201' => ['丝印','喷码','覆膜'],
  265. 'Y202' => ['卷凹','单凹','圆烫','圆切','复卷','分条','配卷','拼板','机检(圆切)','手检(圆切)','包装(圆切)'],
  266. 'Y203' => ['烫金','烫金+凹凸(压纹)','模切','模切+凹凸(压纹)','凹凸(压纹)','拆片','覆膜','对裱'],
  267. 'Y204' => ['手检','次品检','机检(初检)','二次机检','核检','抽检','包装'],
  268. '200' => ['裁切','滚切','胶印','上光','切废'],
  269. '201' => ['丝印','喷码','覆膜'],
  270. '202' => ['卷凹','单凹','圆烫','圆切','复卷','分条','配卷','拼板','机检(圆切)','手检(圆切)','包装(圆切)'],
  271. '203' => ['烫金','烫金+凹凸(压纹)','模切','模切+凹凸(压纹)','凹凸(压纹)','拆片','覆膜','对裱'],
  272. '204' => ['手检','次品检','机检(初检)','二次机检','核检','抽检','包装'],
  273. ];
  274. $validWarehouseCodes = ['101','Y101','102','Y102','103','Y103','200','201','202','203','204','Y200','Y201','Y202','Y203','Y204'];
  275. // 4. 优化数据库查询,使用链式操作提高可读性
  276. $list = db('物料_收发记录')
  277. ->alias('a')
  278. ->join('物料_存货编码 b', 'a.st_wlbh = b.物料代码')
  279. ->field([
  280. 'a.st_wlbh as 物料编号',
  281. 'a.供方批次',
  282. 'a.仓库编号',
  283. 'rtrim(b.物料名称) as 物料名称'
  284. ])
  285. ->where('a.st_gdbh', $param['gdbh'])
  286. ->where('a.cpdh', $param['cpdh'] ?? '') // 使用空值合并运算符
  287. ->whereNotNull('a.供方批次')
  288. ->group('a.st_wlbh,a.供方批次')
  289. ->select();
  290. if (empty($list)) {
  291. $this->success('未找到物料资料');
  292. }
  293. // 5. 提前查询工序数据
  294. $process = db('设备_产量计酬')
  295. ->alias('a')
  296. ->join('工单_工艺资料 b', 'a.sczl_gdbh = b.Gy0_gdbh AND a.sczl_yjno = b.Gy0_yjno AND a.sczl_gxh = b.Gy0_gxh')
  297. ->where([
  298. 'a.sczl_gdbh' => $param['gdbh'],
  299. 'a.sczl_yjno' => $param['yjno']
  300. ])
  301. ->field([
  302. 'a.sczl_gxh as 工序号',
  303. 'b.Gy0_gxmc',
  304. 'a.sczl_gxmc as 工序名称'
  305. ])
  306. ->group('a.sczl_gxh')
  307. ->select();
  308. // 6. 重构数据处理逻辑
  309. $data = [];
  310. $processMap = []; // 缓存工序数据
  311. // 如果需要按工序名称快速查找,可以先建立映射
  312. foreach ($process as $proc) {
  313. $processMap[$proc['Gy0_gxmc']] = $proc['工序名称'];
  314. }
  315. foreach ($list as $item) {
  316. $warehouseCode = $item['仓库编号'];
  317. $baseData = [
  318. '流程单号' => $param['num'],
  319. '物料编号' => $item['物料编号'],
  320. '物料名称' => $item['物料名称'],
  321. '供方批次' => $item['供方批次']
  322. ];
  323. // 7. 优化条件判断逻辑
  324. if (in_array($warehouseCode, $validWarehouseCodes, true)) {
  325. if ($warehouseCode === '101' || $warehouseCode === 'Y101' || $warehouseCode === '102' || $warehouseCode === 'Y102' || $warehouseCode === '103' || $warehouseCode === 'Y103') {
  326. if (!empty($process[0])) {
  327. $baseData['工序名称'] = $process[0]['工序名称'];
  328. }
  329. } else {
  330. // 其他仓库的处理逻辑
  331. if (isset($department[$warehouseCode])) {
  332. $relatedProcesses = $department[$warehouseCode];
  333. foreach ($relatedProcesses as $processName) {
  334. if (isset($processMap[$processName])) {
  335. $baseData['工序名称'] = $processMap[$processName];
  336. }
  337. }
  338. }
  339. }
  340. }
  341. $data[] = $baseData;
  342. }
  343. $result = [
  344. 'data' => $data,
  345. 'total' => count($data),
  346. ];
  347. $this->success('成功',$result);
  348. }
  349. /**
  350. * 印版记录
  351. * @return void
  352. * @throws \think\db\exception\DataNotFoundException
  353. * @throws \think\db\exception\ModelNotFoundException
  354. * @throws \think\exception\DbException
  355. */
  356. public function PrintingPlateList()
  357. {
  358. // 1. 请求方法验证
  359. if (!$this->request->isGet()) {
  360. $this->success('请求错误');
  361. }
  362. // 2. 参数验证与提取
  363. $param = $this->request->param();
  364. $requiredParams = ['gdbh', 'yjno'];
  365. foreach ($requiredParams as $requiredParam) {
  366. if (empty($param[$requiredParam])) {
  367. $this->success("参数错误,缺少:{$requiredParam}");
  368. }
  369. }
  370. $gdbh = $param['gdbh'];
  371. $yjno = $param['yjno'];
  372. $page = $param['page'] ?? 1;
  373. $pageSize = $param['pageSize'] ?? 2;
  374. // 3. 主查询 - 获取印版领用记录
  375. $list = db('工单_印版领用记录')
  376. ->alias('a')
  377. ->join('物料_存货编码 b', 'a.Yb_存货编码 = b.物料代码')
  378. ->where([
  379. 'a.Yb_工单编号' => $gdbh,
  380. 'a.Yb_印件号' => $yjno,
  381. 'a.Yb_领用机台' => ['<>', '']
  382. ])
  383. ->field([
  384. 'a.Yb_存货编码 as 存货编码',
  385. 'a.Yb_供方批号 as 供方批号',
  386. 'a.Yb_领用机台 as 领用机台',
  387. 'TRIM(b.物料名称) as 物料名称', // 使用TRIM替代rtrim
  388. 'a.Sys_id as 用户'
  389. ])
  390. ->group('a.Yb_存货编码, a.Yb_供方批号') // 明确指定表字段
  391. ->order('a.Yb_存货编码')
  392. ->page($page,$pageSize)
  393. ->select();
  394. if (empty($list)) {
  395. $this->success('未找到印版数据');
  396. }
  397. // 4. 收集所有机台号,准备批量查询
  398. $machineCodes = array_column($list, '领用机台');
  399. $machineCodes = array_unique(array_filter($machineCodes)); // 去重并过滤空值
  400. // 5. 批量查询产量计酬数据 - 解决N+1查询问题
  401. $processMap = [];
  402. if (!empty($machineCodes)) {
  403. $processList = db('设备_产量计酬')
  404. ->where([
  405. 'sczl_gdbh' => $gdbh,
  406. 'sczl_yjno' => $yjno,
  407. 'sczl_jtbh' => ['IN', $machineCodes]
  408. ])
  409. ->field([
  410. 'sczl_jtbh as 机台编号',
  411. 'sczl_gxmc as 工序名称',
  412. 'sczl_num as 流程单号'
  413. ])
  414. ->order('sczl_num, sczl_jtbh')
  415. ->select();
  416. // 按机台编号分组,建立映射
  417. foreach ($processList as $process) {
  418. $machineCode = $process['机台编号'];
  419. if (!isset($processMap[$machineCode])) {
  420. $processMap[$machineCode] = [];
  421. }
  422. $processMap[$machineCode][] = [
  423. '工序名称' => $process['工序名称'],
  424. '流程单号' => $process['流程单号']
  425. ];
  426. }
  427. }
  428. // 6. 数据组装
  429. $data = [];
  430. foreach ($list as $item) {
  431. $machineCode = $item['领用机台'];
  432. $baseData = [
  433. '存货编码' => $item['存货编码'],
  434. '供方批号' => $item['供方批号'],
  435. '领用机台' => $machineCode,
  436. '物料名称' => $item['物料名称'],
  437. '用户' => $item['用户']
  438. ];
  439. // 如果该机台有对应的工序数据
  440. if (isset($processMap[$machineCode]) && !empty($processMap[$machineCode])) {
  441. foreach ($processMap[$machineCode] as $process) {
  442. $data[] = array_merge($baseData, $process);
  443. }
  444. } else {
  445. // 如果机台没有对应工序,至少返回基础信息
  446. $data[] = array_merge($baseData, [
  447. '工序名称' => '',
  448. '流程单号' => ''
  449. ]);
  450. }
  451. }
  452. // 7. 按流程单号排序(如果需要的话)
  453. usort($data, function($a, $b) {
  454. return strcmp($a['流程单号'] ?? '', $b['流程单号'] ?? '');
  455. });
  456. $result = [
  457. 'data' => $data,
  458. 'total' => count($data),
  459. ];
  460. $this->success('成功', $result);
  461. }
  462. }