ProductionLot.php 17 KB

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