ProductionLot.php 17 KB

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