PrintingPlate.php 22 KB

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