Decision.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. if ($this->request->isGet() === false) {
  124. $this->error('请求错误');
  125. }
  126. $param = $this->request->param();
  127. if (empty($param['mouth'])) {
  128. $this->error('参数错误');
  129. }
  130. $where = [];
  131. if (!empty($param['sist'])) {
  132. $where['设备编组'] = $param['sist'];
  133. }
  134. // 将参数转换成标准日期格式
  135. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  136. // 使用单个查询获取所有需要的数据
  137. $results = \db('设备_产量计酬')
  138. ->alias('a')
  139. ->join('设备_基本资料 b', 'a.sczl_jtbh = b.设备编号')
  140. ->join('工单_印件资料 c', 'a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno', 'LEFT')
  141. ->join('工单_工艺资料 d', 'a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh', 'LEFT')
  142. ->field([
  143. 'a.sczl_jtbh',
  144. 'a.sczl_bzdh',
  145. 'DATE_FORMAT(a.sczl_rq, "%Y-%m-%d") as day',
  146. 'SUM(a.sczl_cl) as total_cl',
  147. 'c.yj_yjmc as 印件名称',
  148. 'CONCAT(d.Gy0_gxmc,"(",d.Add_gxmc,")") as 工序名称',
  149. 'RTRIM(d.印刷方式) as 印刷方式',
  150. 'RTRIM(d.版距) as 版距'
  151. ])
  152. ->where('a.sczl_rq', 'like', $mouth . '%')
  153. ->where('b.sys_sbID', '<>', '')
  154. ->where($where)
  155. ->group('a.sczl_bzdh, day, a.sczl_jtbh, c.yj_yjmc, d.Gy0_gxmc')
  156. ->order('a.sczl_jtbh, a.sczl_bzdh, day')
  157. ->select();
  158. // 处理数据
  159. $data = [];
  160. // 获取唯一日期并排序
  161. $data['head'] = array_unique(array_column($results, 'day'));
  162. // 将日期转为时间戳以排序
  163. usort($data['head'], function($a, $b) {
  164. return strtotime($a) - strtotime($b);
  165. });
  166. $total_by_machine = [];
  167. foreach ($results as $item) {
  168. $machine_id = $item['sczl_jtbh'];
  169. $group_id = $item['sczl_bzdh'];
  170. $day = $item['day'];
  171. $printed_type = $item['印刷方式'];
  172. if (!isset($total_by_machine[$machine_id])) {
  173. $total_by_machine[$machine_id] = [];
  174. }
  175. if (!isset($total_by_machine[$machine_id][$group_id])) {
  176. $total_by_machine[$machine_id][$group_id] = [
  177. '印件名称' => $item['印件名称'],
  178. '工序名称' => $item['工序名称'],
  179. 'total_cl' => 0,
  180. 'day_total' => []
  181. ];
  182. }
  183. // 更新总数量
  184. if ($printed_type === '卷对卷') {
  185. $total_by_machine[$machine_id][$group_id]['total_cl'] += round($item['total_cl'] / $item['版距'] * 1000);
  186. } else {
  187. $total_by_machine[$machine_id][$group_id]['total_cl'] += $item['total_cl'];
  188. }
  189. // 按天汇总
  190. if (!isset($total_by_machine[$machine_id][$group_id]['day_total'][$day])) {
  191. $total_by_machine[$machine_id][$group_id]['day_total'][$day] = 0;
  192. }
  193. // 日总数
  194. if ($printed_type === '卷对卷') {
  195. $total_by_machine[$machine_id][$group_id]['day_total'][$day] += round($item['total_cl'] / $item['版距'] * 1000);
  196. } else {
  197. $total_by_machine[$machine_id][$group_id]['day_total'][$day] += $item['total_cl'];
  198. }
  199. }
  200. // 格式化输出数据
  201. $data['total'] = array_values(array_map(function ($machine) {
  202. return array_values($machine);
  203. }, $total_by_machine));
  204. $this->success('成功', $data);
  205. }
  206. /**
  207. * 机台班次生产工单明细
  208. * @return void
  209. * @throws \think\db\exception\DataNotFoundException
  210. * @throws \think\db\exception\ModelNotFoundException
  211. * @throws \think\exception\DbException
  212. */
  213. public function MachineProductDetail()
  214. {
  215. if (!$this->request->isGet()) {
  216. $this->error('请求错误');
  217. }
  218. $param = $this->request->param();
  219. // 检查必需的参数
  220. if (empty($param['mouth'])) {
  221. $this->error('参数错误');
  222. }
  223. // Initializing where conditions
  224. $where = [];
  225. // 添加可选的查询条件
  226. if (!empty($param['machine'])) {
  227. $where['a.sczl_jtbh'] = $param['machine'];
  228. }
  229. if (!empty($param['team'])) {
  230. $where['a.sczl_bzdh'] = ['like', substr($param['team'], 0, 1) . '%'];
  231. }
  232. // 将参数转换成标准日期格式
  233. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  234. $where['a.sczl_rq'] = ['like', $mouth . '%'];
  235. // 分页配置
  236. $page = !empty($param['page']) ? (int)$param['page'] : 1;
  237. $limit = !empty($param['limit']) ? (int)$param['limit'] : 9999; // 默认查询所有
  238. // 查询数据
  239. $list = \db('设备_产量计酬')
  240. ->alias('a')
  241. ->join('工单_印件资料 c', 'a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno')
  242. ->join('工单_工艺资料 d', 'a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh')
  243. ->field([
  244. 'a.sczl_gdbh' => '工单编号',
  245. 'a.sczl_yjno' => '印件号',
  246. 'a.sczl_gxh' => '工序号',
  247. 'c.yj_yjmc' => '印件名称',
  248. 'CONCAT(d.Gy0_gxmc,"(",d.Add_gxmc,")")' => '工序名称',
  249. 'DATE(a.sczl_rq)' => '工作日期',
  250. 'a.sczl_jtbh' => '机台编号',
  251. 'a.sczl_bzdh' => '班组编号',
  252. 'SUM(a.sczl_cl)' => '产量',
  253. 'a.sczl_ms' => '墨色数',
  254. 'rtrim(d.印刷方式)' => '印刷方式',
  255. 'rtrim(d.版距)' => '版距'
  256. ])
  257. ->where($where)
  258. ->group('a.sczl_gdbh, a.sczl_yjno, a.sczl_gxh, a.sczl_rq, a.sczl_jtbh, a.sczl_bzdh')
  259. ->order('工作日期')
  260. ->page($page, $limit) // 使用 page 和 limit 实现分页
  261. ->select();
  262. // 数据处理
  263. if (!empty($list)) {
  264. foreach ($list as $key => $value) {
  265. $list[$key]['印件名称'] = $value['印件号'] . '-' . $value['印件名称'];
  266. $list[$key]['工序名称'] = $value['工序号'] . '-' . $value['工序名称'];
  267. unset($list[$key]['印件号'], $list[$key]['工序号']);
  268. // 产量计算
  269. if ($value['印刷方式'] === '卷对卷') {
  270. $list[$key]['产量'] = round($value['产量'] / $value['版距'] * 1000);
  271. }
  272. // 墨色数修正
  273. if ($value['墨色数'] === '0.00') {
  274. $list[$key]['墨色数'] = '1.00';
  275. }
  276. }
  277. }
  278. $this->success('成功', $list);
  279. }
  280. /**
  281. * 月度机台运行工时汇总
  282. * @return void
  283. * @throws \think\db\exception\DataNotFoundException
  284. * @throws \think\db\exception\ModelNotFoundException
  285. * @throws \think\exception\DbException
  286. */
  287. public function MachineOperation()
  288. {
  289. if ($this->request->isGet() === false){
  290. $this->error('请求错误');
  291. }
  292. $param = $this->request->param();
  293. if (empty($param['mouth'])){
  294. $this->error('参数错误');
  295. }
  296. $where = [];
  297. if(!empty($param['sist'])){
  298. $where['a.设备编组'] = $param['sist'];
  299. }
  300. //将参数装换成标准日期格式
  301. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  302. $list = \db('设备_基本资料')
  303. ->alias('a')
  304. ->join('设备_产量计酬 b', 'a.设备编号 = b.sczl_jtbh')
  305. ->join('工单_印件资料 c', 'b.sczl_gdbh = c.Yj_Gdbh AND b.sczl_yjno = c.yj_Yjno')
  306. ->join('工单_工艺资料 d', 'b.sczl_gdbh = d.Gy0_gdbh AND b.sczl_yjno = d.Gy0_yjno AND b.sczl_gxh = d.Gy0_gxh')
  307. ->field([
  308. 'a.设备编号' => '设备编号',
  309. 'rtrim(a.设备名称)' => '设备名称',
  310. 'SUM(CASE WHEN rtrim(d.版距) = "卷对卷" THEN b.sczl_cl / NULLIF(d.版距, 0) * 1000 ELSE b.sczl_cl END) AS 产量',
  311. 'SUM(b.sczl_设备运行工时)' => '设备运行工时',
  312. 'SUM(b.sczl_保养工时)' => '保养工时',
  313. 'SUM(b.sczl_打样总工时)' => '打样总工时',
  314. 'SUM(b.sczl_打样工时)' => '打样补产工时',
  315. 'SUM(b.sczl_装版总工时)' => '装版总工时',
  316. 'SUM(b.sczl_装版工时)' => '装板补产工时',
  317. 'SUM(b.sczl_异常停机工时)' => '异常停机工时',
  318. 'rtrim(d.印刷方式)' => '印刷方式',
  319. 'rtrim(d.版距)' => '版距'
  320. ])
  321. ->where($where)
  322. ->where('b.sczl_rq','like', $mouth.'%')
  323. ->group('a.设备编号')
  324. ->order('a.设备编号')
  325. ->select();
  326. $total = \db('设备_基本资料')
  327. ->alias('a')
  328. ->join('设备_产量计酬 b','a.设备编号 = b.sczl_jtbh')
  329. ->field([
  330. 'SUM(b.sczl_设备运行工时)' => '设备运行工时',
  331. 'SUM(b.sczl_保养工时)' => '保养工时',
  332. 'SUM(b.sczl_打样总工时)' => '打样总工时',
  333. 'SUM(b.sczl_打样工时)' => '打样补产工时',
  334. 'SUM(b.sczl_装版总工时)' => '装板总工时',
  335. 'SUM(b.sczl_装版工时)' => '装板补产工时',
  336. 'SUM(b.sczl_异常停机工时)' => '异常停机工时'
  337. ])
  338. ->where($where)
  339. ->where('b.sczl_rq','like', $mouth.'%')
  340. ->find();
  341. $list['total'] = $total;
  342. $this->success('成功',$list);
  343. }
  344. /**
  345. * 设备运行工时机台生产工单数据详情
  346. * @return void
  347. * @throws \think\db\exception\DataNotFoundException
  348. * @throws \think\db\exception\ModelNotFoundException
  349. * @throws \think\exception\DbException
  350. */
  351. public function MachineOperationProductDetail()
  352. {
  353. if (!$this->request->isGet()) {
  354. $this->error('请求错误');
  355. }
  356. $param = $this->request->param();
  357. if (empty($param['mouth'])) {
  358. $this->error('参数错误');
  359. }
  360. // Initialize where conditions
  361. $where = [];
  362. if (!empty($param['machine'])) {
  363. $where['a.sczl_jtbh'] = $param['machine'];
  364. }
  365. // 将参数转换成标准日期格式
  366. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  367. $where['a.sczl_rq'] = ['like', $mouth . '%'];
  368. // 分页配置
  369. $page = !empty($param['page']) ? (int)$param['page'] : 1;
  370. $limit = !empty($param['limit']) ? (int)$param['limit'] : 9999; // 默认查询所有
  371. // 查询数据
  372. $list = \db('设备_产量计酬')
  373. ->alias('a')
  374. ->join('设备_基本资料 b', 'a.sczl_jtbh = b.设备编号')
  375. ->join('工单_印件资料 c', 'a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno')
  376. ->join('工单_工艺资料 d', 'a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh')
  377. ->field([
  378. 'a.sczl_jtbh' => '设备编号',
  379. 'rtrim(b.设备名称)' => '设备名称',
  380. 'DATE(a.sczl_rq)' => '日期',
  381. 'a.sczl_gdbh' => '工单编号',
  382. 'a.sczl_yjno' => '印件号',
  383. 'a.sczl_gxh' => '工序号',
  384. 'c.yj_yjmc' => '印件名称',
  385. 'CONCAT(d.Gy0_gxmc, "(", d.Add_gxmc, ")")' => '工序名称',
  386. 'SUM(a.sczl_cl)' => '产量',
  387. 'SUM(a.sczl_设备运行工时)' => '设备运行工时',
  388. 'SUM(a.sczl_保养工时)' => '保养工时',
  389. 'SUM(a.sczl_打样总工时)' => '打样总工时',
  390. 'SUM(a.sczl_打样工时)' => '打样补产工时',
  391. 'SUM(a.sczl_装版总工时)' => '装板总工时',
  392. 'SUM(a.sczl_装版工时)' => '装板补产工时',
  393. 'SUM(a.sczl_异常停机工时)' => '异常停机工时',
  394. 'a.sczl_ms' => '墨色数',
  395. 'rtrim(d.印刷方式)' => '印刷方式',
  396. 'rtrim(d.版距)' => '版距'
  397. ])
  398. ->where($where)
  399. ->group('a.sczl_rq, a.sczl_gdbh, a.sczl_gxh, a.sczl_jtbh')
  400. ->order('a.sczl_rq')
  401. ->page($page, $limit) // 使用 page 和 limit 技术实现分页
  402. ->select();
  403. // 数据处理
  404. if (!empty($list)) {
  405. foreach ($list as $key => $value) {
  406. $list[$key]['工序名称'] = $value['印件号'] . '-' . $value['工序号'] . '-' . $value['工序名称'];
  407. // 移除不需要的字段
  408. unset($list[$key]['印件号'], $list[$key]['工序号']);
  409. // 产量计算
  410. if ($value['印刷方式'] === '卷对卷') {
  411. $list[$key]['产量'] = round($value['产量'] / $value['版距'] * 1000);
  412. }
  413. // 墨色数修正
  414. if ($value['墨色数'] === '0.00') {
  415. $list[$key]['墨色数'] = '1.00';
  416. }
  417. }
  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 ProcessOutputRate()
  429. {
  430. if ($this->request->isGet() === false){
  431. $this->error('请求错误');
  432. }
  433. $param = $this->request->param();
  434. if (empty($param)){
  435. $this->error('参数错误');
  436. }
  437. //查询已经进入超节损的月份
  438. $mouth = \db('工单_质量考核汇总')
  439. ->where('sys_ny','like',$param['year'].'%')
  440. ->column('distinct(sys_ny) as mouth');
  441. //创建工序数组
  442. $processType = ['胶印','卷凹','圆烫','圆切','烫金','模切','丝印','喷码','单凹'];
  443. $result = $data = [];
  444. foreach ($mouth as $key=>$value){
  445. foreach ($processType as $item){
  446. //查询进入超节损一年内所有数据
  447. $list = \db('工单_质量考核汇总')
  448. ->alias('a')
  449. ->join('工单_工艺资料 b','a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  450. ->join('工单_印件资料 c','a.Gy0_gdbh = c.Yj_Gdbh AND a.印件及工序 = c.yj_Yjno')
  451. ->field('a.sys_ny,(a.计划损耗+sum(a.班组质检废品))/a.印件工序产量 as 废品率')
  452. ->where('b.Gy0_gxmc','like',$item.'%')
  453. ->where('a.sys_ny',$value)
  454. ->group('a.Gy0_gdbh,b.Gy0_yjno,a.工序')
  455. ->select();
  456. $data[$value][$item] = $list;
  457. }
  458. }
  459. $months = [
  460. $param['year'].'01' => '01月',
  461. $param['year'].'02' => '02月',
  462. $param['year'].'03' => '03月',
  463. $param['year'].'04' => '04月',
  464. $param['year'].'05' => '05月',
  465. $param['year'].'06' => '06月',
  466. $param['year'].'07' => '07月',
  467. $param['year'].'08' => '08月',
  468. $param['year'].'09' => '09月',
  469. $param['year'].'10' => '10月',
  470. $param['year'].'11' => '11月',
  471. $param['year'].'12' => '12月'
  472. ];
  473. //汇总数据
  474. foreach ($processType as $index => $process) {
  475. $result[$index] = array(
  476. "工序类型" => $process,
  477. "01月" => "",
  478. "02月" => "",
  479. "03月" => "",
  480. "04月" => "",
  481. "05月" => "",
  482. "06月" => "",
  483. "07月" => "",
  484. "08月" => "",
  485. "09月" => "",
  486. "10月" => "",
  487. "11月" => "",
  488. "12月" => ""
  489. );
  490. // 遍历月份
  491. foreach ($months as $month => $monthName) {
  492. if (isset($data[$month])){
  493. $count = count($data[$month][$process]);
  494. }
  495. $totalRate = 0;
  496. // 检查月份是否存在于数据中
  497. if (isset($data[$month])) {
  498. // 检查工序是否存在于月份数据中
  499. if (isset($data[$month][$process])) {
  500. // 累加废品率
  501. foreach ($data[$month][$process] as $rate) {
  502. $totalRate += floatval(1-$rate["废品率"]);
  503. }
  504. }
  505. }
  506. if ($count > 0){
  507. $rateNumber = $totalRate/$count;
  508. }
  509. // 格式化为百分比字符串,保留两位小数
  510. if ($totalRate > 0) {
  511. $result[$index][$monthName] = sprintf("%.2f%%", $rateNumber * 100);
  512. } else {
  513. $result[$index][$monthName] = ""; // 如果没有数据,则为空字符串
  514. }
  515. }
  516. }
  517. foreach ($result as &$item) {
  518. $total = 0;
  519. $count = 0;
  520. // 遍历月份,计算总和和数量
  521. for ($i = 1; $i <= 12; $i++) {
  522. $month = sprintf("%02d月", $i); // 格式化月份,例如 "01月"
  523. if (isset($item[$month]) && $item[$month] !== "") {
  524. // 移除百分号,并将字符串转换为浮点数
  525. $value = floatval(str_replace("%", "", $item[$month]));
  526. $total += $value;
  527. $count++;
  528. }
  529. }
  530. // 计算平均值
  531. if ($count > 0) {
  532. $average = $total / $count;
  533. // 格式化为百分比字符串,保留两位小数
  534. $item["平均值"] = sprintf("%.2f%%", $average);
  535. } else {
  536. $item["平均值"] = "0.00%"; // 如果没有数据,则为 0.00%
  537. }
  538. }
  539. $this->success('成功',$result);
  540. }
  541. /**
  542. * 获取年分数据
  543. * @return void
  544. */
  545. public function GetYear()
  546. {
  547. if ($this->request->isGet() === false){
  548. $this->error('请求错误');
  549. }
  550. $data = \db('工单_质量考核汇总')
  551. ->group('year')
  552. ->column('YEAR(STR_TO_DATE(sys_ny, "%Y%m")) as year');
  553. $this->success('成功',$data);
  554. }
  555. /**
  556. * 数据透视表
  557. * @return null
  558. * @throws \think\Exception
  559. * @throws \think\db\exception\DataNotFoundException
  560. * @throws \think\db\exception\ModelNotFoundException
  561. * @throws \think\exception\DbException
  562. */
  563. public function PoductData()
  564. {
  565. if (!$this->request->isGet()) {
  566. $this->error('请求错误');
  567. }
  568. $param = $this->request->param();
  569. if (empty($param['year'])) {
  570. $this->error('参数错误');
  571. }
  572. // 定义分类规则及顺序
  573. $processOrder = [
  574. 1 => ['name' => '胶印工序', 'keys' => ['胶印', '上光']],
  575. 2 => ['name' => '凹印工序', 'keys' => ['卷凹']],
  576. 3 => ['name' => '圆烫工序', 'keys' => ['圆烫']],
  577. 4 => ['name' => '圆切工序', 'keys' => ['圆切']],
  578. 5 => ['name' => '烫模工序', 'keys' => ['烫金', '模切', '凹凸']],
  579. 6 => ['name' => '丝印工序', 'keys' => ['丝印']],
  580. 7 => ['name' => '喷码工序', 'keys' => ['喷码']],
  581. 8 => ['name' => '单凹工序', 'keys' => ['单凹']]
  582. ];
  583. // 构建基础查询
  584. $query = \db('工单_质量考核汇总')
  585. ->alias('a')
  586. ->field('
  587. a.sczl_jtbh AS 机台编号,
  588. a.Gy0_gdbh AS 工单编号,
  589. a.印件及工序 AS 工序号,
  590. a.产品名称 AS 印件名称,
  591. a.工序 AS 工序,
  592. a.工序名称,
  593. a.联数,
  594. a.班组产量,
  595. a.班组制程废品,
  596. a.班组质检废品,
  597. a.sczl_bzdh AS 班组编号,
  598. b.印刷方式,
  599. b.版距,
  600. DATE_FORMAT(a.入仓日期, \'%Y%m\') AS 完工年月
  601. ')
  602. ->join('工单_工艺资料 b', 'a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  603. ->where('a.sys_ny', 'like', $param['year'] . '%')
  604. ->where('a.工序名称','not like','%机检%')
  605. ->where('a.工序名称','not like','%拆片%');
  606. // 获取总数
  607. $total = clone $query;
  608. $total = $total->count();
  609. // 执行查询
  610. $rawList = isset($param['page'], $param['limit'])
  611. ? $query->page($param['page'])->limit($param['limit'])->select()
  612. : $query->select();
  613. // 处理分类标记
  614. $processedList = [];
  615. foreach ($rawList as $item) {
  616. $matched = false;
  617. // 顺序匹配分类规则
  618. foreach ($processOrder as $typeId => $rule) {
  619. foreach ($rule['keys'] as $keyword) {
  620. if (strpos($item['工序名称'], $keyword) !== false) {
  621. // 添加分类标记
  622. $item['type_id'] = $typeId;
  623. $item['type_name'] = $rule['name'];
  624. $processedList[] = $item;
  625. $matched = true;
  626. break 2; // 跳出两层循环
  627. }
  628. }
  629. }
  630. // 未匹配到分类的数据丢弃
  631. }
  632. // 按分类顺序排序(保持1-8的顺序)
  633. usort($processedList, function($a, $b) {
  634. return $a['type_id'] <=> $b['type_id'];
  635. });
  636. // 返回结构
  637. $data['data'] = $processedList;
  638. $data['total'] = $total; // 原始总数
  639. $data['filtered_total'] = count($processedList); // 实际有效数
  640. return count($processedList) > 0
  641. ? $this->success('成功', $data)
  642. : $this->error('未找到数据');
  643. }
  644. }