Decision.php 27 KB

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