PrintingPlate.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Request;
  5. /**
  6. * 印版库管理
  7. */
  8. class PrintingPlate extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. /**
  13. * 印版库管理左侧菜单
  14. * @return void
  15. */
  16. public function getTab()
  17. {
  18. if (!$this->request->isGet()) {
  19. $this->error('请求错误');
  20. }
  21. $data = [
  22. 'MN印版' => $this->buildTree('05'),
  23. '翌星印版' => $this->buildTree('Y05')
  24. ];
  25. $this->success('成功', $data);
  26. }
  27. //私有方法,构建数据
  28. private function buildTree($prefix)
  29. {
  30. $list = db('物料_存货结构')
  31. ->where('编号', 'like', $prefix . '%')
  32. ->order('编号', 'asc')
  33. ->select();
  34. $map = [];
  35. $tree = [];
  36. // 创建编号映射表
  37. foreach ($list as $item) {
  38. $map[$item['编号']] = $item['名称'];
  39. }
  40. // 构建树形结构
  41. foreach ($list as $item) {
  42. $code = $item['编号'];
  43. $name = $item['名称'];
  44. $key = "{$code} {$name}";
  45. $depth = strlen($code) - strlen($prefix);
  46. switch ($depth) {
  47. case 0: // 根节点 (05/Y05)
  48. $tree[$key] = [];
  49. break;
  50. case 2: // 二级节点 (0501/Y0501)
  51. $parentCode = substr($code, 0, strlen($prefix));
  52. $parentKey = "{$parentCode} {$map[$parentCode]}";
  53. $tree[$parentKey][$key] = [];
  54. break;
  55. case 4: // 三级节点 (050100/Y050101)
  56. $parentCode = substr($code, 0, strlen($prefix) + 2);
  57. $grandCode = substr($code, 0, strlen($prefix));
  58. $grandKey = "{$grandCode} {$map[$grandCode]}";
  59. $parentKey = "{$parentCode} {$map[$parentCode]}";
  60. if (isset($tree[$grandKey][$parentKey])) {
  61. $tree[$grandKey][$parentKey][] = $key;
  62. }
  63. break;
  64. }
  65. }
  66. return $tree;
  67. }
  68. /**
  69. * 存货编码列表
  70. * @return void
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. * @throws \think\exception\DbException
  74. */
  75. public function MaterailCodeList()
  76. {
  77. if (!$this->request->isGet()) {
  78. $this->error('请求错误');
  79. }
  80. $params = $this->request->param();
  81. if (empty($params)) {
  82. $this->error('参数错误');
  83. }
  84. $page = max(intval($params['page']), 1); // 确保页码 >=1
  85. $limit = max(intval($params['limit']), 10); // 最小限制10条
  86. $where = [];
  87. // 条件优化:使用索引列优先
  88. if (!empty($params['code'])) {
  89. $codeLength = preg_match('/[a-zA-Z]/', $params['code']) ? 7 : 6;
  90. $where['a.存货编码'] = ['like', substr($params['code'], 0, $codeLength) . '%'];
  91. }
  92. if (!empty($params['key']) && isset($params['key'])) {
  93. $where['a.报废日期'] = '1900-01-01 00:00:00';
  94. }
  95. // 全文搜索优化:拆分为单字段 OR 避免全局模糊索引失效
  96. if (!empty($params['search'])) {
  97. $search = trim($params['search']);
  98. $where['a.存货编码|b.物料名称|c.Yb_工单编号'] = ['like', "%{$search}%"];
  99. }
  100. // 1. 解决 COUNT 性能问题:单独处理避免大表 GROUP BY
  101. $countQuery = db('产品_印版库')
  102. ->alias('a')
  103. ->join('物料_存货编码 b', 'a.存货编码 = b.物料代码')
  104. ->join('工单_印版领用记录 c', 'a.存货编码 = c.Yb_存货编码 AND a.供方批号 = c.Yb_供方批号', 'LEFT')
  105. ->where($where);
  106. $total = $countQuery->group('a.存货编码, a.供方批号')->count();
  107. // 2. 主查询优化:移除子查询改用 JOIN 聚合
  108. $baseQuery = db('产品_印版库')
  109. ->alias('a')
  110. ->join('物料_存货编码 b', 'a.存货编码 = b.物料代码')
  111. ->join('工单_印版领用记录 c', 'a.存货编码 = c.Yb_存货编码 AND a.供方批号 = c.Yb_供方批号', 'LEFT')
  112. ->where($where)
  113. ->group('a.存货编码, a.供方批号')
  114. ->order('a.报废日期, a.存货编码') // 明确表别名
  115. ->limit(($page - 1) * $limit, $limit);
  116. // 3. 累计印数计算优化:使用 LEFT JOIN 聚合结果
  117. $subSum = db('工单_印版领用记录')
  118. ->field('Yb_存货编码, Yb_供方批号, SUM(Yb_印数) as total')
  119. ->group('Yb_存货编码, Yb_供方批号')
  120. ->buildSql();
  121. $list = $baseQuery
  122. ->Join([$subSum => 'd'], 'd.Yb_存货编码 = a.存货编码 AND d.Yb_供方批号 = a.供方批号','left')
  123. ->field([
  124. 'rtrim(a.存货编码) as 存货编码',
  125. 'rtrim(b.物料名称) as 物料名称',
  126. 'rtrim(a.印版名称) as 印版名称',
  127. 'rtrim(a.供方批号) as 供方批号',
  128. 'DATE(a.制造日期) as 制造日期',
  129. "CASE
  130. WHEN a.报废日期 = '1900-01-01 00:00:00' THEN NULL
  131. ELSE DATE(a.报废日期)
  132. END as 报废日期",
  133. 'a.原始印数',
  134. 'a.考核印数',
  135. 'a.UniqID',
  136. "MAX(c.UniqID) as GDUID",
  137. 'rtrim(a.Sys_id) as 创建用户',
  138. 'a.Sys_rq as 创建日期',
  139. 'a.Mod_rq as 修改时间',
  140. "COALESCE(d.total, 0) as 累计印数", // 直接使用聚合结果
  141. "MAX(CASE
  142. WHEN c.Yb_领用日期 IS NOT NULL AND c.Yb_退还日期 IS NULL
  143. THEN c.Yb_工单编号
  144. END) as 工单编号"
  145. ])
  146. ->select();
  147. if (empty($list)) {
  148. $this->error('未找到相关记录');
  149. }
  150. $this->success('查询成功', [
  151. 'data' => $list,
  152. 'total' => $total,
  153. ]);
  154. }
  155. /**
  156. * 印版管理印版修改
  157. * @return void
  158. * @throws \think\db\exception\DataNotFoundException
  159. * @throws \think\db\exception\ModelNotFoundException
  160. * @throws \think\exception\DbException
  161. */
  162. public function MaterailDetail()
  163. {
  164. if ($this->request->isGet() === false){
  165. $this->error('请求错误');
  166. }
  167. $param = $this->request->param();
  168. if (empty($param) || !isset($param['UniqID'])){
  169. $this->error('参数错误');
  170. }
  171. $result = db('产品_印版库')
  172. ->alias('a')
  173. ->join('物料_存货编码 b', 'a.存货编码 = b.物料代码')
  174. ->where('a.UniqID', $param['UniqID'])
  175. ->order('报废日期, a.存货编码')
  176. ->field('
  177. rtrim(a.存货编码) as 存货编码,
  178. rtrim(b.物料名称) as 物料名称,
  179. rtrim(a.印版名称) as 印版备注,
  180. rtrim(a.供方批号) as 供方批号,
  181. DATE(a.制造日期) as 制造日期,
  182. CASE
  183. WHEN a.报废日期 = "1900-01-01 00:00:00" THEN NULL
  184. ELSE DATE(a.报废日期)
  185. END as 报废日期,
  186. a.原始印数,
  187. a.考核印数,
  188. a.UniqID
  189. ')
  190. ->find();
  191. if (empty($result)) {
  192. $this->error('未找到数据');
  193. }else{
  194. $this->success('成功', $result);
  195. }
  196. }
  197. /**
  198. * 印版资料新增修改
  199. * @return void
  200. * @throws \think\Exception
  201. * @throws \think\db\exception\BindParamException
  202. * @throws \think\db\exception\DataNotFoundException
  203. * @throws \think\db\exception\ModelNotFoundException
  204. * @throws \think\exception\DbException
  205. * @throws \think\exception\PDOException
  206. */
  207. public function MaterailEdit()
  208. {
  209. if ($this->request->isPost() === false){
  210. $this->error('请求错误');
  211. }
  212. $param = Request::instance()->post();
  213. if (empty($param) || !isset($param['code'])){
  214. $this->error('参数错误');
  215. }
  216. $data = [
  217. '存货编码' => $param['code'],
  218. '供方批号' => $param['batch'],
  219. '印版名称' => $param['desc'],
  220. '制造日期' => $param['Manufactur_date'],
  221. '原始印数' => $param['start_num'],
  222. '考核印数' => $param['Assessment_num'],
  223. '报废日期' => $param['Scrappe_date']?:'1900-01-01 00:00:00',
  224. 'Sys_id' => $param['sys_id']
  225. ];
  226. //查询数据是否存在,存在则修改,不存在则增加
  227. $res = db('产品_印版库')
  228. ->where('存货编码',$param['code'])
  229. ->where('供方批号',$param['batch'])
  230. ->find();
  231. if (empty($res)){
  232. $data['Sys_rq'] = date('Y-m-d H:i:s',time());
  233. if ($param['number'] !== '' || $param['number'] !== '0'){
  234. if ($param['sist'] === '凹丝印车间'){
  235. $data['供方批号'] = $param['batch'].'-'.$param['number'];
  236. $sql = db('产品_印版库')->fetchSql(true)->insert($data);
  237. }else{
  238. for ($i = 1; $i <= $param['number']; $i++){
  239. if ($i<10){
  240. $number = '0'.$i;
  241. }else{
  242. $number = $i;
  243. }
  244. $data['供方批号'] = $param['batch'].'-'.$number;
  245. $resData[] = $data;
  246. }
  247. $sql = db('产品_印版库')->fetchSql(true)->insertAll($resData);
  248. }
  249. }else{
  250. $sql = db('产品_印版库')->fetchSql(true)->insert($data);
  251. }
  252. }else{
  253. $UniqID = $param['UniqID'];
  254. $data['Mod_rq'] = date('Y-m-d H:i:s',time());
  255. $sql = db('产品_印版库')->where('UniqID',$UniqID)->fetchSql(true)->update($data);
  256. }
  257. $result = db()->query($sql);
  258. if ($result === false){
  259. $this->error('修改失败');
  260. }else{
  261. $this->success('修改成功');
  262. }
  263. }
  264. /**
  265. * 印版新增->存货编码获取
  266. * @return void
  267. * @throws \think\db\exception\DataNotFoundException
  268. * @throws \think\db\exception\ModelNotFoundException
  269. * @throws \think\exception\DbException
  270. */
  271. public function getInventoryCode()
  272. {
  273. if (!$this->request->isGet()) {
  274. $this->error('请求错误');
  275. }
  276. $param = $this->request->param();
  277. if (empty($param) || !isset($param['code'])) {
  278. $this->error('参数错误');
  279. }
  280. // 提取前缀
  281. $codeLength = preg_match('/[a-zA-Z]/', $param['code']) ? 5 : 4;
  282. $code = substr($param['code'], 0, $codeLength);
  283. $where['物料代码'] = ['like', $code . '%'];
  284. if (!empty($param['search'])) {
  285. $where['物料代码'] = $param['search'];
  286. }
  287. // 查询物料存货编码
  288. $list = db('物料_存货编码')
  289. ->where($where)
  290. ->field('rtrim(物料代码) as 物料代码, rtrim(物料名称) as 物料名称, rtrim(规格) as 规格')
  291. ->select();
  292. // 数据处理
  293. $data = [];
  294. foreach ($list as $item) {
  295. // 提取物料代码的前缀
  296. $prefixLength = preg_match('/[a-zA-Z]/', $item['物料代码']) ? [3, 5, 7] : [2, 4, 6];
  297. // 使用普通的数组映射替代箭头函数
  298. $num1 = substr($item['物料代码'], 0, $prefixLength[0]);
  299. $num2 = substr($item['物料代码'], 0, $prefixLength[1]);
  300. $num3 = substr($item['物料代码'], 0, $prefixLength[2]);
  301. // 批量查询物料结构名称
  302. $names = db('物料_存货结构')
  303. ->whereIn('编号', [$num1, $num2, $num3])
  304. ->column('名称', '编号');
  305. // 将数据组织进数组
  306. $name1 = isset($names[$num1]) ? $names[$num1] : '';
  307. $name2 = isset($names[$num2]) ? $names[$num2] : '';
  308. $name3 = isset($names[$num3]) ? $names[$num3] : '';
  309. if (!isset($data["$num1/$name1"][$num2][$num3.'/'.$name3])) {
  310. $data["$num1/$name1"][$num2][$num3.'/'.$name3] = [];
  311. }
  312. $data["$num1/$name1"][$num2][$num3.'/'.$name3][] = "{$item['物料代码']}/{$item['物料名称']}/{$item['规格']}";
  313. }
  314. $this->success('成功', $data);
  315. }
  316. /**
  317. * 印版领用
  318. * @return void
  319. * @throws \think\db\exception\BindParamException
  320. * @throws \think\exception\PDOException
  321. */
  322. public function PrintingPlateReceive()
  323. {
  324. if (!$this->request->isPost()) {
  325. $this->error('请求错误');
  326. }
  327. $param = Request::instance()->post();
  328. if (empty($param)) {
  329. $this->error('参数错误');
  330. }
  331. $data = [];
  332. foreach ($param as $item) {
  333. $data[] = [
  334. 'Yb_工单编号' => $item['gdbh'],
  335. 'Yb_印件号' => $item['yjno'],
  336. 'Yb_存货编码' => $item['code'],
  337. 'Yb_供方批号' => $item['batch'],
  338. 'Yb_领用日期' => date('Y-m-d',time()),
  339. 'Yb_领用机台' => $item['machine'],
  340. 'Sys_id' => $item['sys_id'],
  341. 'Sys_rq' => date('Y-m-d H:i:s',time()),
  342. ];
  343. }
  344. $sql = db('工单_印版领用记录')
  345. ->fetchSql(true)
  346. ->insertAll($data);
  347. $res = db('')->query($sql);
  348. if ($res === false){
  349. $this->error('领用失败');
  350. }else{
  351. $this->success('领用成功');
  352. }
  353. }
  354. /**
  355. * 印版工单领用
  356. * @return void
  357. * @throws \think\db\exception\DataNotFoundException
  358. * @throws \think\db\exception\ModelNotFoundException
  359. * @throws \think\exception\DbException
  360. */
  361. public function PrintDetailReceive()
  362. {
  363. if (!$this->request->isGet()) {
  364. $this->error('请求错误');
  365. }
  366. $param = $this->request->param();
  367. if (empty($param) || !isset($param['code'])) {
  368. $this->error('参数错误');
  369. }
  370. $list = db('工单_印版领用记录')
  371. ->alias('a')
  372. ->field('a.Yb_工单编号 as 工单编号,a.Yb_印件号 as 印件号,a.Yb_领用日期 as 领用日期,a.Yb_退还日期 as 退还日期,a.Yb_领用机台 as 领用机台,
  373. a.Yb_印数 as 印数,a.Sys_id as 创建用户,a.Sys_rq as 创建时间,a.UniqID,b.成品代号,rtrim(b.成品名称) as 成品名称')
  374. ->join('工单_基本资料 b', 'a.Yb_工单编号 = b.Gd_gdbh','left')
  375. ->where('a.Yb_存货编码',$param['code'])
  376. ->where('a.Yb_供方批号',$param['batch'])
  377. ->order('a.UniqID desc')
  378. ->group('a.Yb_工单编号,a.Yb_印件号,a.UniqID')
  379. ->select();
  380. if (empty($list)){
  381. $this->error('未找到领用记录');
  382. }else{
  383. $this->success('成功', $list);
  384. }
  385. }
  386. /**
  387. * 印版领用记录删除
  388. * @return void
  389. * @throws \think\Exception
  390. * @throws \think\exception\PDOException
  391. */
  392. public function PrintReceiveDelete()
  393. {
  394. if ($this->request->isGet() === false) {
  395. $this->error('请求错误');
  396. }
  397. $param = $this->request->param();
  398. if (empty($param) || !isset($param['id'])) {
  399. $this->error('参数错误');
  400. }
  401. $id = explode(',', $param['id']);
  402. if (empty($id)) {
  403. $this->error('请先选中要删除数据');
  404. }
  405. $res = db('工单_印版领用记录')
  406. ->where('UniqID','in',$id)
  407. ->delete();
  408. if ($res === false){
  409. $this->error('删除失败');
  410. }else{
  411. $this->success('删除成功');
  412. }
  413. }
  414. //印版库管理菜单下方客户产品印版管理
  415. public function PrintDetailTab()
  416. {
  417. if($this->request->isGet() === false){
  418. $this->error('请求错误');
  419. }
  420. $list = db('产品_印版资料')
  421. ->alias('a')
  422. ->field('
  423. rtrim(a.YB_Cpdh) as 产品代号,
  424. rtrim(b.产品名称) as 产品名称,
  425. rtrim(b.客户编号) as 客户编号,
  426. rtrim(b.客户名称) as 客户名称,
  427. rtrim(a.存货编码) as 存货编码,
  428. rtrim(c.名称) as 名称
  429. ')
  430. ->join('产品_基本资料 b', 'a.YB_Cpdh = b.产品编号')
  431. ->join('物料_存货结构 c',
  432. "(CASE
  433. WHEN a.存货编码 REGEXP '[A-Za-z]'
  434. THEN LEFT(rtrim(a.存货编码), 5)
  435. ELSE LEFT(rtrim(a.存货编码), 4)
  436. END) = rtrim(c.编号)"
  437. )
  438. ->group('a.YB_Cpdh,名称')
  439. ->order('a.YB_Cpdh,a.存货编码')
  440. ->select();
  441. if (empty($list)){
  442. $this->error('未找到客户数据');
  443. }
  444. foreach ($list as $item) {
  445. $customerKey = $item['客户编号'] . '【' . $item['客户名称'] . '】';
  446. $productKey = $item['产品代号'] . '【' . $item['产品名称'] . '】';
  447. if (!isset($data[$customerKey])) {
  448. $data[$customerKey] = [];
  449. }
  450. if (!isset($data[$customerKey][$productKey])) {
  451. $data[$customerKey][$productKey] = [];
  452. }
  453. if (!in_array($item['名称'], $data[$customerKey][$productKey])) {
  454. $data[$customerKey][$productKey][] = $item['名称'];
  455. }
  456. }
  457. $this->success('成功', $data);
  458. }
  459. //客户印版库右侧列表
  460. public function PrintDetail()
  461. {
  462. if ($this->request->isGet() === false) {
  463. $this->error('请求错误');
  464. }
  465. $param = $this->request->param();
  466. if (empty($param) || !isset($param['code']) || !isset($param['category'])) {
  467. $this->error('请求错误');
  468. }
  469. //获取印版库分类编号
  470. $categoryNumber = db('物料_存货');
  471. $where = [
  472. 'a.YB_Cpdh' => $param['code'],
  473. 'b.YB_Cpdh' => $param['category'],
  474. ];
  475. // $list = db('<UNK>_<UNK>')
  476. }
  477. /**
  478. * 印版删除
  479. * @return void
  480. * @throws \think\Exception
  481. * @throws \think\exception\PDOException
  482. */
  483. public function PrintDetaiDel()
  484. {
  485. if ($this->request->isGet() === false) {
  486. $this->error('请求错误');
  487. }
  488. $param = $this->request->param();
  489. if (empty($param) || !isset($param['UniqID'])) {
  490. $this->error('参数错误');
  491. }
  492. $id = explode(',', $param['UniqID']);
  493. $res = db('产品_印版库')
  494. ->where('UniqID','in',$id)
  495. ->delete();
  496. if ($res === false){
  497. $this->error('删除失败');
  498. }else{
  499. $this->success('删除成功');
  500. }
  501. }
  502. }