ProductionLot.php 18 KB

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