Decision.php 31 KB

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