ProductionLot.php 17 KB

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