ProductionLot.php 18 KB

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