YxDecsion.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Request;
  6. /**
  7. * 设备运行跟踪
  8. */
  9. class YxDecsion extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 月度产量统计菜单
  15. * @return void
  16. * @throws \think\db\exception\DataNotFoundException
  17. * @throws \think\db\exception\ModelNotFoundException
  18. * @throws \think\exception\DbException
  19. */
  20. public function OutputSstatisticsMenu()
  21. {
  22. if ($this->request->isGet() === false){
  23. $this->error('请求错误');
  24. }
  25. $mouth = \db('设备_产量计酬')
  26. ->distinct(true)
  27. ->field('DATE_FORMAT(sczl_rq, "%Y-%m") AS month')
  28. ->order('month desc')
  29. ->select();
  30. $machine = db('设备映射表')->column('MNmachine');
  31. $sist = \db('设备_基本资料')
  32. ->whereNotNull('设备编组')
  33. ->whereIn('设备编号',$machine)
  34. ->group('设备编组')
  35. ->column('rtrim(设备编组) as 设备编组');
  36. $data = [];
  37. foreach ($mouth as $key=>$value) {
  38. $arr = [
  39. 'date'=>date('Ym',strtotime($value['month'])),
  40. 'sbbh'=>$sist,
  41. ];
  42. array_push($data,$arr);
  43. }
  44. $this->success('成功',$data);
  45. }
  46. /**
  47. * 月度产量统计上方机台生产数据
  48. * @return void
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. * @throws \think\exception\DbException
  52. */
  53. public function MachineProduction()
  54. {
  55. // 验证请求方式
  56. if ($this->request->isGet() === false) {
  57. $this->error('请求错误');
  58. }
  59. // 获取请求参数
  60. $param = $this->request->param();
  61. if (empty($param['mouth'])) {
  62. $this->error('参数错误');
  63. }
  64. // 初始化查询条件
  65. $where = [];
  66. if (!empty($param['sist'])) {
  67. $where['b.设备编组'] = $param['sist'];
  68. }
  69. // 转换日期格式
  70. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  71. // 使用单个查询获取所有需要的数据
  72. $results = \db('设备_产量计酬')
  73. ->alias('a')
  74. ->join('设备_基本资料 b', 'a.sczl_jtbh = b.设备编号')
  75. ->join('工单_印件资料 c', 'a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno', 'LEFT')
  76. ->join('工单_工艺资料 d', 'a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh', 'LEFT')
  77. ->join('设备映射表 e', 'a.sczl_jtbh = e.MNmachine')
  78. ->field([
  79. 'a.sczl_jtbh',
  80. 'a.sczl_bzdh',
  81. 'DATE_FORMAT(a.sczl_rq, "%Y-%m-%d") as day',
  82. 'SUM(a.sczl_cl) AS total_cl',
  83. 'c.yj_yjmc AS 印件名称',
  84. 'CONCAT(d.Gy0_gxmc,"(",d.Add_gxmc,")") AS 工序名称',
  85. 'RTRIM(d.印刷方式) AS 印刷方式',
  86. 'RTRIM(d.版距) AS 版距'
  87. ])
  88. ->where('a.sczl_rq', 'like', $mouth . '%')
  89. ->where('b.sys_sbID', '<>', '')
  90. ->where($where)
  91. ->group('a.sczl_jtbh, a.sczl_bzdh, day, c.yj_yjmc, d.Gy0_gxmc')
  92. ->order('a.sczl_jtbh, a.sczl_bzdh, day')
  93. ->select();
  94. halt($results);
  95. // 数据处理
  96. $data = [];
  97. // 获取唯一日期并排序
  98. $data['head'] = array_unique(array_column($results, 'day'));
  99. // 将日期转为时间戳以排序
  100. usort($data['head'], function($a, $b) {
  101. return strtotime($a) - strtotime($b);
  102. });
  103. // 准备按设备和班组整理的数据
  104. $total_by_machine = [];
  105. foreach ($results as $item) {
  106. $machine_id = $item['sczl_jtbh'];
  107. $group_id = $item['sczl_bzdh'];
  108. $day = $item['day'];
  109. $printed_type = $item['印刷方式'];
  110. // 初始化设备和班组
  111. if (!isset($total_by_machine[$machine_id])) {
  112. $total_by_machine[$machine_id] = [];
  113. }
  114. if (!isset($total_by_machine[$machine_id][$group_id])) {
  115. $total_by_machine[$machine_id][$group_id] = [
  116. '机台编号' => $machine_id,
  117. '班组编号' => $group_id,
  118. '印件名称' => $item['印件名称'],
  119. '工序名称' => $item['工序名称'],
  120. 'total_cl' => 0,
  121. 'day_total' => []
  122. ];
  123. }
  124. // 更新累计数量
  125. if ($printed_type === '卷对卷') {
  126. $total_by_machine[$machine_id][$group_id]['total_cl'] += round($item['total_cl'] / $item['版距'] * 1000);
  127. } else {
  128. $total_by_machine[$machine_id][$group_id]['total_cl'] += $item['total_cl'];
  129. }
  130. // 更新每日总计
  131. if (!isset($total_by_machine[$machine_id][$group_id]['day_total'][$day])) {
  132. $total_by_machine[$machine_id][$group_id]['day_total'][$day] = 0;
  133. }
  134. if ($printed_type === '卷对卷') {
  135. $total_by_machine[$machine_id][$group_id]['day_total'][$day] += round($item['total_cl'] / $item['版距'] * 1000);
  136. } else {
  137. $total_by_machine[$machine_id][$group_id]['day_total'][$day] += $item['total_cl'];
  138. }
  139. }
  140. // 格式化输出数据
  141. $data['total'] = [];
  142. foreach ($total_by_machine as $machine_group) {
  143. foreach ($machine_group as $group_data) {
  144. $data['total'][] = $group_data;
  145. }
  146. }
  147. $this->success('成功', $data);
  148. }
  149. /**
  150. * 机台班次生产工单明细
  151. * @return void
  152. * @throws \think\db\exception\DataNotFoundException
  153. * @throws \think\db\exception\ModelNotFoundException
  154. * @throws \think\exception\DbException
  155. */
  156. public function MachineProductDetail()
  157. {
  158. if (!$this->request->isGet()) {
  159. $this->error('请求错误');
  160. }
  161. $param = $this->request->param();
  162. // 检查必需的参数
  163. if (empty($param['mouth'])) {
  164. $this->error('参数错误');
  165. }
  166. // Initializing where conditions
  167. $where = [];
  168. // 添加可选的查询条件
  169. if (!empty($param['machine'])) {
  170. $where['a.sczl_jtbh'] = $param['machine'];
  171. }
  172. if (!empty($param['team'])) {
  173. $where['a.sczl_bzdh'] = ['like', substr($param['team'], 0, 1) . '%'];
  174. }
  175. // 将参数转换成标准日期格式
  176. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  177. $where['a.sczl_rq'] = ['like', $mouth . '%'];
  178. // 分页配置
  179. $page = !empty($param['page']) ? (int)$param['page'] : 1;
  180. $limit = !empty($param['limit']) ? (int)$param['limit'] : 9999; // 默认查询所有
  181. // 查询数据
  182. $list = \db('设备_产量计酬')
  183. ->alias('a')
  184. ->join('工单_印件资料 c', 'a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno')
  185. ->join('工单_工艺资料 d', 'a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh')
  186. ->field([
  187. 'a.sczl_gdbh' => '工单编号',
  188. 'a.sczl_yjno' => '印件号',
  189. 'a.sczl_gxh' => '工序号',
  190. 'c.yj_yjmc' => '印件名称',
  191. 'CONCAT(d.Gy0_gxmc,"(",d.Add_gxmc,")")' => '工序名称',
  192. 'DATE(a.sczl_rq)' => '工作日期',
  193. 'a.sczl_jtbh' => '机台编号',
  194. 'a.sczl_bzdh' => '班组编号',
  195. 'SUM(a.sczl_cl)' => '产量',
  196. 'a.sczl_ms' => '墨色数',
  197. 'rtrim(d.印刷方式)' => '印刷方式',
  198. 'rtrim(d.版距)' => '版距'
  199. ])
  200. ->where($where)
  201. ->group('a.sczl_gdbh, a.sczl_yjno, a.sczl_gxh, a.sczl_rq, a.sczl_jtbh, a.sczl_bzdh')
  202. ->order('工作日期')
  203. ->page($page, $limit) // 使用 page 和 limit 实现分页
  204. ->select();
  205. // 数据处理
  206. if (!empty($list)) {
  207. foreach ($list as $key => $value) {
  208. $list[$key]['印件名称'] = $value['印件号'] . '-' . $value['印件名称'];
  209. $list[$key]['工序名称'] = $value['工序号'] . '-' . $value['工序名称'];
  210. unset($list[$key]['印件号'], $list[$key]['工序号']);
  211. // 产量计算
  212. if ($value['印刷方式'] === '卷对卷') {
  213. $list[$key]['产量'] = round($value['产量'] / $value['版距'] * 1000);
  214. }
  215. // 墨色数修正
  216. if ($value['墨色数'] === '0.00') {
  217. $list[$key]['墨色数'] = '1.00';
  218. }
  219. }
  220. }
  221. $this->success('成功', $list);
  222. }
  223. /**
  224. * 月度机台运行工时汇总
  225. * @return void
  226. * @throws \think\db\exception\DataNotFoundException
  227. * @throws \think\db\exception\ModelNotFoundException
  228. * @throws \think\exception\DbException
  229. */
  230. public function MachineOperation()
  231. {
  232. // 确保请求是GET
  233. if ($this->request->isGet() === false) {
  234. $this->error('请求错误');
  235. }
  236. // 获取请求参数
  237. $param = $this->request->param();
  238. if (empty($param['mouth'])) {
  239. $this->error('参数错误');
  240. }
  241. // 构建查询条件
  242. $where = [];
  243. if (!empty($param['sist'])) {
  244. $where['a.设备编组'] = $param['sist'];
  245. }
  246. // 将参数转换成标准日期格式
  247. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  248. // 单次查询,计算产量和各项工时
  249. $list = \db('设备_基本资料')
  250. ->alias('a')
  251. ->join('设备_产量计酬 b', 'a.设备编号 = b.sczl_jtbh')
  252. ->join('工单_印件资料 c', 'b.sczl_gdbh = c.Yj_Gdbh AND b.sczl_yjno = c.yj_Yjno')
  253. ->join('工单_工艺资料 d', 'b.sczl_gdbh = d.Gy0_gdbh AND b.sczl_yjno = d.Gy0_yjno AND b.sczl_gxh = d.Gy0_gxh')
  254. ->field([
  255. 'a.设备编号' => '设备编号',
  256. 'rtrim(a.设备名称)' => '设备名称',
  257. 'SUM(CASE WHEN rtrim(d.版距) = "卷对卷" THEN b.sczl_cl / NULLIF(d.版距, 0) * 1000 ELSE b.sczl_cl END) AS 产量',
  258. 'SUM(b.sczl_设备运行工时)' => '设备运行工时',
  259. 'SUM(b.sczl_保养工时)' => '保养工时',
  260. 'SUM(b.sczl_打样总工时)' => '打样总工时',
  261. 'SUM(b.sczl_打样工时)' => '打样补产工时',
  262. 'SUM(b.sczl_装版总工时)' => '装版总工时',
  263. 'SUM(b.sczl_装版工时)' => '装版补产工时',
  264. 'SUM(b.sczl_异常停机工时)' => '异常停机工时',
  265. 'rtrim(d.印刷方式)' => '印刷方式',
  266. 'rtrim(d.版距)' => '版距'
  267. ])
  268. ->where($where)
  269. ->where('b.sczl_rq', 'like', $mouth . '%')
  270. ->group('a.设备编号')
  271. ->order('a.设备编号')
  272. ->select();
  273. // 处理总工时的计算
  274. $total = [
  275. '设备运行工时' => 0,
  276. '保养工时' => 0,
  277. '打样总工时' => 0,
  278. '打样补产工时' => 0,
  279. '装版总工时' => 0,
  280. '装版补产工时' => 0,
  281. '异常停机工时' => 0
  282. ];
  283. // 计算各项总工时
  284. foreach ($list as $item) {
  285. $total['设备运行工时'] += $item['设备运行工时'];
  286. $total['保养工时'] += $item['保养工时'];
  287. $total['打样总工时'] += $item['打样总工时'];
  288. $total['打样补产工时'] += $item['打样补产工时'];
  289. $total['装版总工时'] += $item['装版总工时'];
  290. $total['装版补产工时'] += $item['装版补产工时'];
  291. $total['异常停机工时'] += $item['异常停机工时'];
  292. }
  293. // 将总工时添加到列表中
  294. $list['total'] = $total;
  295. // 返回成功响应
  296. $this->success('成功', $list);
  297. }
  298. /**
  299. * 设备运行工时机台生产工单数据详情
  300. * @return void
  301. * @throws \think\db\exception\DataNotFoundException
  302. * @throws \think\db\exception\ModelNotFoundException
  303. * @throws \think\exception\DbException
  304. */
  305. public function MachineOperationProductDetail()
  306. {
  307. if (!$this->request->isGet()) {
  308. $this->error('请求错误');
  309. }
  310. $param = $this->request->param();
  311. if (empty($param['mouth'])) {
  312. $this->error('参数错误');
  313. }
  314. // Initialize where conditions
  315. $where = [];
  316. if (!empty($param['machine'])) {
  317. $where['a.sczl_jtbh'] = $param['machine'];
  318. }
  319. // 将参数转换成标准日期格式
  320. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  321. $where['a.sczl_rq'] = ['like', $mouth . '%'];
  322. // 分页配置
  323. $page = !empty($param['page']) ? (int)$param['page'] : 1;
  324. $limit = !empty($param['limit']) ? (int)$param['limit'] : 9999; // 默认查询所有
  325. // 查询数据
  326. $list = \db('设备_产量计酬')
  327. ->alias('a')
  328. ->join('设备_基本资料 b', 'a.sczl_jtbh = b.设备编号')
  329. ->join('工单_印件资料 c', 'a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno')
  330. ->join('工单_工艺资料 d', 'a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh')
  331. ->field([
  332. 'a.sczl_jtbh' => '设备编号',
  333. 'rtrim(b.设备名称)' => '设备名称',
  334. 'DATE(a.sczl_rq)' => '日期',
  335. 'a.sczl_gdbh' => '工单编号',
  336. 'a.sczl_yjno' => '印件号',
  337. 'a.sczl_gxh' => '工序号',
  338. 'c.yj_yjmc' => '印件名称',
  339. 'CONCAT(d.Gy0_gxmc, "(", d.Add_gxmc, ")")' => '工序名称',
  340. 'SUM(a.sczl_cl)' => '产量',
  341. 'SUM(a.sczl_设备运行工时)' => '设备运行工时',
  342. 'SUM(a.sczl_保养工时)' => '保养工时',
  343. 'SUM(a.sczl_打样总工时)' => '打样总工时',
  344. 'SUM(a.sczl_打样工时)' => '打样补产工时',
  345. 'SUM(a.sczl_装版总工时)' => '装板总工时',
  346. 'SUM(a.sczl_装版工时)' => '装板补产工时',
  347. 'SUM(a.sczl_异常停机工时)' => '异常停机工时',
  348. 'a.sczl_ms' => '墨色数',
  349. 'rtrim(d.印刷方式)' => '印刷方式',
  350. 'rtrim(d.版距)' => '版距'
  351. ])
  352. ->where($where)
  353. ->group('a.sczl_rq, a.sczl_gdbh, a.sczl_gxh, a.sczl_jtbh')
  354. ->order('a.sczl_rq')
  355. ->page($page, $limit) // 使用 page 和 limit 技术实现分页
  356. ->select();
  357. // 数据处理
  358. if (!empty($list)) {
  359. foreach ($list as $key => $value) {
  360. $list[$key]['工序名称'] = $value['印件号'] . '-' . $value['工序号'] . '-' . $value['工序名称'];
  361. // 移除不需要的字段
  362. unset($list[$key]['印件号'], $list[$key]['工序号']);
  363. // 产量计算
  364. if ($value['印刷方式'] === '卷对卷') {
  365. $list[$key]['产量'] = round($value['产量'] / $value['版距'] * 1000);
  366. }
  367. // 墨色数修正
  368. if ($value['墨色数'] === '0.00') {
  369. $list[$key]['墨色数'] = '1.00';
  370. }
  371. }
  372. }
  373. $this->success('成功', $list);
  374. }
  375. /**
  376. * 工序产出率月度统计报表
  377. * @return void
  378. * @throws \think\db\exception\DataNotFoundException
  379. * @throws \think\db\exception\ModelNotFoundException
  380. * @throws \think\exception\DbException
  381. */
  382. public function ProcessOutputRate()
  383. {
  384. if (!$this->request->isGet()) {
  385. $this->error('请求错误');
  386. }
  387. $param = $this->request->param();
  388. if (empty($param)) {
  389. $this->error('参数错误');
  390. }
  391. // 工序分类
  392. // $processOrder = [
  393. // 1 => ['name' => '胶印', 'keys' => ['胶印', '上光']],
  394. // 2 => ['name' => '卷凹', 'keys' => ['卷凹']],
  395. // 3 => ['name' => '圆烫', 'keys' => ['圆烫']],
  396. // 4 => ['name' => '圆切', 'keys' => ['圆切']],
  397. // 5 => ['name' => '烫模', 'keys' => ['烫金', '模切', '凹凸']],
  398. // 6 => ['name' => '丝印', 'keys' => ['丝印']],
  399. // 7 => ['name' => '喷码', 'keys' => ['喷码']],
  400. // 8 => ['name' => '单凹', 'keys' => ['单凹']],
  401. // 9 => ['name' => '覆膜', 'keys' => ['覆膜']]
  402. // ];
  403. $processOrder= \db('设备_基本资料')
  404. ->whereNotNull('设备编组')
  405. ->group('设备编组')
  406. ->column('rtrim(设备编组) as 设备编组');
  407. // 获取存在数据的月份
  408. $monthsWithData = \db('工单_质量考核汇总')
  409. ->where('sys_ny', 'like', $param['year'] . '%')
  410. ->column('distinct(sys_ny) as month');
  411. // 初始化结果数组,按工序名称归类
  412. $result = [];
  413. foreach ($processOrder as $processId => $process) {
  414. $result[substr($process, 5, 6)] = [
  415. '01月' => '',
  416. '02月' => '',
  417. '03月' => '',
  418. '04月' => '',
  419. '05月' => '',
  420. '06月' => '',
  421. '07月' => '',
  422. '08月' => '',
  423. '09月' => '',
  424. '10月' => '',
  425. '11月' => '',
  426. '12月' => '',
  427. '平均值' => '0.00%'
  428. ];
  429. }
  430. // 遍历每个月份
  431. foreach ($monthsWithData as $month) {
  432. foreach ($processOrder as $processId => $process) {
  433. // 查询该月份该工序的合格率数据
  434. $query = \db('工单_质量考核汇总')
  435. ->alias('a')
  436. ->join('工单_工艺资料 b', 'a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  437. ->join('工单_印件资料 c', 'a.Gy0_gdbh = c.Yj_Gdbh AND a.印件及工序 = c.yj_Yjno')
  438. ->join('成品入仓 d', 'a.Gy0_gdbh = d.jjcp_gdbh AND a.印件及工序 = d.jjcp_yjno')
  439. ->join('设备_基本资料 e','a.sczl_jtbh = e.设备编号')
  440. ->field('a.sys_ny,
  441. (1 - ((SUM(a.班组制程废品) + SUM(a.班组质检废品)) / SUM(a.班组产量))) * 100 as 合格率')
  442. // ->where(function ($query) use ($process) {
  443. // foreach ($process as $key) {
  444. // $query->whereOr('b.Gy0_gxmc', 'like', "%{$key}%");
  445. // }
  446. // })
  447. ->where('e.设备编组',$process)
  448. ->where('d.jjcp_smb', 'like', '末%')
  449. ->where('a.sys_ny', $month)
  450. ->group('a.sys_ny');
  451. $records = $query->select();
  452. if (!empty($records)) {
  453. $monthName = substr($month, 4, 2) . '月';
  454. $result[substr($process, 5, 6)][$monthName] = sprintf("%.2f%%", $records[0]['合格率']);
  455. }
  456. }
  457. }
  458. // 计算年度平均值
  459. foreach ($result as $processName => &$processData) {
  460. $totalAnnual = 0;
  461. $monthCount = 0;
  462. foreach ($processData as $month => $rate) {
  463. if ($month !== '平均值' && $rate !== '') {
  464. $totalAnnual += floatval(str_replace('%', '', $rate));
  465. $monthCount++;
  466. }
  467. }
  468. if ($monthCount > 0) {
  469. $processData['平均值'] = sprintf("%.2f%%", $totalAnnual / $monthCount);
  470. }
  471. }
  472. unset($processData); // 关键修复:解除引用
  473. // 转换为目标格式
  474. $formattedData = [];
  475. foreach ($result as $processName => $processData) {
  476. // 现在processData是独立值
  477. $formattedRow = array_merge(['工序类型' => $processName], $processData);
  478. $formattedData[] = $formattedRow;
  479. }
  480. // 返回JSON响应
  481. $this->success('成功',$formattedData);
  482. }
  483. /**
  484. * 获取年分数据
  485. * @return void
  486. */
  487. public function GetYear()
  488. {
  489. if ($this->request->isGet() === false){
  490. $this->error('请求错误');
  491. }
  492. $data = \db('工单_质量考核汇总')
  493. ->group('year')
  494. ->column('YEAR(STR_TO_DATE(sys_ny, "%Y%m")) as year');
  495. $this->success('成功',$data);
  496. }
  497. /**
  498. * 数据透视表
  499. * @return null
  500. * @throws \think\Exception
  501. * @throws \think\db\exception\DataNotFoundException
  502. * @throws \think\db\exception\ModelNotFoundException
  503. * @throws \think\exception\DbException
  504. */
  505. public function PoductData()
  506. {
  507. if (!$this->request->isGet()) {
  508. $this->error('请求错误');
  509. }
  510. $param = $this->request->param();
  511. if (empty($param['year'])) {
  512. $this->error('参数错误');
  513. }
  514. // 获取设备编组列表(已按编号排序)
  515. $processGroups = \db('设备_基本资料')
  516. ->whereNotNull('设备编组')
  517. ->group('设备编组')
  518. ->column('rtrim(设备编组) as 设备编组');
  519. // 构建基础查询
  520. $query = \db('工单_质量考核汇总')
  521. ->alias('a')
  522. ->field('
  523. a.sczl_jtbh AS 机台编号,
  524. a.Gy0_gdbh AS 工单编号,
  525. a.印件及工序 AS 工序号,
  526. a.产品名称 AS 印件名称,
  527. a.工序 AS 工序,
  528. a.工序名称,
  529. b.Gy0_gxmc,
  530. a.联数,
  531. a.班组产量,
  532. a.班组制程废品,
  533. a.班组质检废品,
  534. a.sczl_bzdh AS 班组编号,
  535. b.印刷方式,
  536. b.版距,
  537. DATE_FORMAT(a.入仓日期, \'%Y%m\') AS 完工年月,
  538. d.设备编组
  539. ')
  540. ->join('工单_工艺资料 b', 'a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  541. ->join('成品入仓 c', 'a.Gy0_gdbh = c.jjcp_gdbh AND a.印件及工序 = c.jjcp_yjno')
  542. ->join('设备_基本资料 d', 'a.sczl_jtbh = d.设备编号')
  543. ->where('a.sys_ny', 'like', $param['year'] . '%')
  544. ->where('a.工序名称', 'not like', '%机检%')
  545. ->where('c.jjcp_smb', 'like', '末%')
  546. ->where('a.工序名称', 'not like', '%拆片%');
  547. // 获取总数
  548. $total = clone $query;
  549. $total = $total->count();
  550. // 执行查询
  551. $rawList = isset($param['page'], $param['limit'])
  552. ? $query->page($param['page'])->limit($param['limit'])->select()
  553. : $query->select();
  554. // 构建分类映射表
  555. $groupMap = [];
  556. foreach ($processGroups as $index => $group) {
  557. // 提取机组名称核心词(去除编号和"机组"字样)
  558. $groupName = preg_replace('/^\d+、/', '', $group); // 移除开头编号
  559. $coreName = str_replace('机组', '', $groupName); // 移除尾部的"机组"
  560. $groupMap[] = [
  561. 'id' => $index + 1,
  562. 'name' => $groupName,
  563. 'core' => $coreName
  564. ];
  565. }
  566. // 处理分类标记
  567. $processedList = [];
  568. foreach ($rawList as $item) {
  569. foreach ($groupMap as $group) {
  570. // 使用核心词进行模糊匹配
  571. if (strpos($item['Gy0_gxmc'], $group['core']) !== false) {
  572. $item['type_id'] = $group['id'];
  573. $item['type_name'] = $group['name'];
  574. $processedList[] = $item;
  575. continue 2;
  576. }
  577. }
  578. // 未匹配的条目将被丢弃
  579. }
  580. // 按分类ID排序
  581. usort($processedList, function($a, $b) {
  582. return $a['type_id'] <=> $b['type_id'];
  583. });
  584. // 返回结构
  585. $data['data'] = $processedList;
  586. $data['total'] = $total;
  587. $data['filtered_total'] = count($processedList);
  588. return count($processedList) > 0
  589. ? $this->success('成功', $data)
  590. : $this->error('未找到数据');
  591. }
  592. /**
  593. * 月度色度数导出
  594. * @return void
  595. * @throws \think\db\exception\DataNotFoundException
  596. * @throws \think\db\exception\ModelNotFoundException
  597. * @throws \think\exception\DbException
  598. */
  599. public function ChromaticityNumber()
  600. {
  601. if (!$this->request->isGet()) {
  602. $this->error('请求错误');
  603. }
  604. $param = $this->request->param();
  605. if (empty($param['month'])) {
  606. $this->error('参数错误');
  607. }
  608. $month = date_create_from_format('Ym', $param['month'])->format('Y-m');
  609. $where = ['a.sczl_rq' => ['like', $month . '%']];
  610. $list = \db('设备_产量计酬')
  611. ->alias('a')
  612. ->join('工单_印件资料 c', 'a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno')
  613. ->join('工单_工艺资料 d', 'a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh')
  614. ->join('设备_基本资料 e', 'a.sczl_jtbh = e.设备编号')
  615. ->field([
  616. 'a.sczl_gdbh' => '工单编号',
  617. 'a.sczl_yjno' => '印件号',
  618. 'a.sczl_gxh' => '工序号',
  619. 'd.Gy0_gxmc' => '工序名称',
  620. 'SUM(a.sczl_cl)' => '产量',
  621. 'a.sczl_ms' => '墨色数',
  622. 'rtrim(d.印刷方式)' => '印刷方式',
  623. 'rtrim(d.版距)' => '版距',
  624. 'rtrim(d.Gy0_SITE)' => '车间名称',
  625. 'rtrim(e.设备编组)' => '设备编组',
  626. 'a.sczl_jtbh'
  627. ])
  628. ->where($where)
  629. ->group('a.sczl_gdbh, a.sczl_yjno, a.sczl_gxh, a.sczl_rq, a.sczl_jtbh, a.sczl_bzdh')
  630. ->select();
  631. $data = [];
  632. if (!empty($list)) {
  633. foreach ($list as $key => $value) {
  634. // Convert ALL outputs to integers
  635. $value['产量'] = (int)round($value['产量']);
  636. if ($value['印刷方式'] === '卷对卷' && $value['版距'] != 0) {
  637. $value['产量'] = (int)round($value['产量'] / $value['版距'] * 1000);
  638. }
  639. if($value['墨色数'] == 0 && $value['设备编组'] !== '11、检品机组'){
  640. $value['墨色数'] = 1;
  641. }
  642. if($value['设备编组'] === '11、检品机组'){
  643. $value['墨色数'] = 0;
  644. }
  645. $data[$key] = [
  646. 'gdbh' => $value['工单编号'],
  647. 'ms' => $value['墨色数'],
  648. '产量' => $value['产量'],
  649. '墨色' => $value['产量'] * $value['墨色数'],
  650. '设备编组' => $value['设备编组']
  651. ];
  652. }
  653. }
  654. $grouped = [];
  655. foreach ($data as $item) {
  656. $key = substr($item['设备编组'], 5, 6);
  657. if (!isset($grouped[$key])) {
  658. $grouped[$key] = [
  659. '产量合计' => 0,
  660. '工序名称' => $key,
  661. '色度数' => 0
  662. ];
  663. }
  664. $grouped[$key]['产量合计'] += (int)$item['产量'];
  665. $grouped[$key]['色度数'] += (int)$item['墨色'];
  666. }
  667. foreach ($grouped as &$group) {
  668. $group['产量合计'] = (int)$group['产量合计'];
  669. $group['色度数'] = (int)$group['色度数'];
  670. }
  671. $this->success('成功', array_values($grouped));
  672. }
  673. }