Decision.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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. public function MachineOperation()
  345. {
  346. // 确保请求是GET
  347. if ($this->request->isGet() === false) {
  348. $this->error('请求错误');
  349. }
  350. // 获取请求参数
  351. $param = $this->request->param();
  352. if (empty($param['mouth'])) {
  353. $this->error('参数错误');
  354. }
  355. // 构建查询条件
  356. $where = [];
  357. if (!empty($param['sist'])) {
  358. $where['a.设备编组'] = $param['sist'];
  359. }
  360. // 将参数转换成标准日期格式
  361. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  362. // 单次查询,计算产量和各项工时
  363. $list = \db('设备_基本资料')
  364. ->alias('a')
  365. ->join('设备_产量计酬 b', 'a.设备编号 = b.sczl_jtbh')
  366. ->join('工单_印件资料 c', 'b.sczl_gdbh = c.Yj_Gdbh AND b.sczl_yjno = c.yj_Yjno')
  367. ->join('工单_工艺资料 d', 'b.sczl_gdbh = d.Gy0_gdbh AND b.sczl_yjno = d.GY0_yjno AND b.sczl_gxh = d.Gy0_gxh')
  368. ->field([
  369. 'a.设备编号' => '设备编号',
  370. 'rtrim(a.设备名称)' => '设备名称',
  371. 'SUM(CASE WHEN rtrim(d.版距) = "卷对卷" THEN b.sczl_cl / NULLIF(d.版距, 0) * 1000 ELSE b.sczl_cl END) AS 产量',
  372. 'SUM(b.sczl_设备运行工时)' => '设备运行工时',
  373. 'SUM(b.sczl_保养工时)' => '保养工时',
  374. 'SUM(b.sczl_打样总工时)' => '打样总工时',
  375. 'SUM(b.sczl_打样工时)' => '打样补产工时',
  376. 'SUM(b.sczl_装版总工时)' => '装版总工时',
  377. 'SUM(b.sczl_装版工时)' => '装版补产工时',
  378. 'SUM(b.sczl_异常停机工时)' => '异常停机工时',
  379. 'rtrim(d.印刷方式)' => '印刷方式',
  380. 'rtrim(d.版距)' => '版距'
  381. ])
  382. ->where($where)
  383. ->where('b.sczl_rq', 'like', $mouth . '%')
  384. ->group('a.设备编号')
  385. ->order('a.设备编号')
  386. ->select();
  387. // 处理总工时的计算
  388. $total = [
  389. '设备运行工时' => 0,
  390. '保养工时' => 0,
  391. '打样总工时' => 0,
  392. '打样补产工时' => 0,
  393. '装版总工时' => 0,
  394. '装版补产工时' => 0,
  395. '异常停机工时' => 0
  396. ];
  397. // 计算各项总工时
  398. foreach ($list as $item) {
  399. $total['设备运行工时'] += $item['设备运行工时'];
  400. $total['保养工时'] += $item['保养工时'];
  401. $total['打样总工时'] += $item['打样总工时'];
  402. $total['打样补产工时'] += $item['打样补产工时'];
  403. $total['装版总工时'] += $item['装版总工时'];
  404. $total['装版补产工时'] += $item['装版补产工时'];
  405. $total['异常停机工时'] += $item['异常停机工时'];
  406. }
  407. // 将总工时添加到列表中
  408. $list['total'] = $total;
  409. // 返回成功响应
  410. $this->success('成功', $list);
  411. }
  412. /**
  413. * 设备运行工时机台生产工单数据详情
  414. * @return void
  415. * @throws \think\db\exception\DataNotFoundException
  416. * @throws \think\db\exception\ModelNotFoundException
  417. * @throws \think\exception\DbException
  418. */
  419. public function MachineOperationProductDetail()
  420. {
  421. if (!$this->request->isGet()) {
  422. $this->error('请求错误');
  423. }
  424. $param = $this->request->param();
  425. if (empty($param['mouth'])) {
  426. $this->error('参数错误');
  427. }
  428. // Initialize where conditions
  429. $where = [];
  430. if (!empty($param['machine'])) {
  431. $where['a.sczl_jtbh'] = $param['machine'];
  432. }
  433. // 将参数转换成标准日期格式
  434. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  435. $where['a.sczl_rq'] = ['like', $mouth . '%'];
  436. // 分页配置
  437. $page = !empty($param['page']) ? (int)$param['page'] : 1;
  438. $limit = !empty($param['limit']) ? (int)$param['limit'] : 9999; // 默认查询所有
  439. // 查询数据
  440. $list = \db('设备_产量计酬')
  441. ->alias('a')
  442. ->join('设备_基本资料 b', 'a.sczl_jtbh = b.设备编号')
  443. ->join('工单_印件资料 c', 'a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno')
  444. ->join('工单_工艺资料 d', 'a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh')
  445. ->field([
  446. 'a.sczl_jtbh' => '设备编号',
  447. 'rtrim(b.设备名称)' => '设备名称',
  448. 'DATE(a.sczl_rq)' => '日期',
  449. 'a.sczl_gdbh' => '工单编号',
  450. 'a.sczl_yjno' => '印件号',
  451. 'a.sczl_gxh' => '工序号',
  452. 'c.yj_yjmc' => '印件名称',
  453. 'CONCAT(d.Gy0_gxmc, "(", d.Add_gxmc, ")")' => '工序名称',
  454. 'SUM(a.sczl_cl)' => '产量',
  455. 'SUM(a.sczl_设备运行工时)' => '设备运行工时',
  456. 'SUM(a.sczl_保养工时)' => '保养工时',
  457. 'SUM(a.sczl_打样总工时)' => '打样总工时',
  458. 'SUM(a.sczl_打样工时)' => '打样补产工时',
  459. 'SUM(a.sczl_装版总工时)' => '装板总工时',
  460. 'SUM(a.sczl_装版工时)' => '装板补产工时',
  461. 'SUM(a.sczl_异常停机工时)' => '异常停机工时',
  462. 'a.sczl_ms' => '墨色数',
  463. 'rtrim(d.印刷方式)' => '印刷方式',
  464. 'rtrim(d.版距)' => '版距'
  465. ])
  466. ->where($where)
  467. ->group('a.sczl_rq, a.sczl_gdbh, a.sczl_gxh, a.sczl_jtbh')
  468. ->order('a.sczl_rq')
  469. ->page($page, $limit) // 使用 page 和 limit 技术实现分页
  470. ->select();
  471. // 数据处理
  472. if (!empty($list)) {
  473. foreach ($list as $key => $value) {
  474. $list[$key]['工序名称'] = $value['印件号'] . '-' . $value['工序号'] . '-' . $value['工序名称'];
  475. // 移除不需要的字段
  476. unset($list[$key]['印件号'], $list[$key]['工序号']);
  477. // 产量计算
  478. if ($value['印刷方式'] === '卷对卷') {
  479. $list[$key]['产量'] = round($value['产量'] / $value['版距'] * 1000);
  480. }
  481. // 墨色数修正
  482. if ($value['墨色数'] === '0.00') {
  483. $list[$key]['墨色数'] = '1.00';
  484. }
  485. }
  486. }
  487. $this->success('成功', $list);
  488. }
  489. /**
  490. * 工序产出率月度统计报表
  491. * @return void
  492. * @throws \think\db\exception\DataNotFoundException
  493. * @throws \think\db\exception\ModelNotFoundException
  494. * @throws \think\exception\DbException
  495. */
  496. public function ProcessOutputRate()
  497. {
  498. if ($this->request->isGet() === false){
  499. $this->error('请求错误');
  500. }
  501. $param = $this->request->param();
  502. if (empty($param)){
  503. $this->error('参数错误');
  504. }
  505. //查询已经进入超节损的月份
  506. $mouth = \db('工单_质量考核汇总')
  507. ->where('sys_ny','like',$param['year'].'%')
  508. ->column('distinct(sys_ny) as mouth');
  509. //创建工序数组
  510. $processType = ['胶印','卷凹','圆烫','圆切','烫金','模切','丝印','喷码','单凹'];
  511. $result = $data = [];
  512. foreach ($mouth as $key=>$value){
  513. foreach ($processType as $item){
  514. //查询进入超节损一年内所有数据
  515. $list = \db('工单_质量考核汇总')
  516. ->alias('a')
  517. ->join('工单_工艺资料 b','a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  518. ->join('工单_印件资料 c','a.Gy0_gdbh = c.Yj_Gdbh AND a.印件及工序 = c.yj_Yjno')
  519. ->field('a.sys_ny,(a.计划损耗+sum(a.班组质检废品))/a.印件工序产量 as 废品率')
  520. ->where('b.Gy0_gxmc','like',$item.'%')
  521. ->where('a.sys_ny',$value)
  522. ->group('a.Gy0_gdbh,b.Gy0_yjno,a.工序')
  523. ->select();
  524. $data[$value][$item] = $list;
  525. }
  526. }
  527. $months = [
  528. $param['year'].'01' => '01月',
  529. $param['year'].'02' => '02月',
  530. $param['year'].'03' => '03月',
  531. $param['year'].'04' => '04月',
  532. $param['year'].'05' => '05月',
  533. $param['year'].'06' => '06月',
  534. $param['year'].'07' => '07月',
  535. $param['year'].'08' => '08月',
  536. $param['year'].'09' => '09月',
  537. $param['year'].'10' => '10月',
  538. $param['year'].'11' => '11月',
  539. $param['year'].'12' => '12月'
  540. ];
  541. //汇总数据
  542. foreach ($processType as $index => $process) {
  543. $result[$index] = array(
  544. "工序类型" => $process,
  545. "01月" => "",
  546. "02月" => "",
  547. "03月" => "",
  548. "04月" => "",
  549. "05月" => "",
  550. "06月" => "",
  551. "07月" => "",
  552. "08月" => "",
  553. "09月" => "",
  554. "10月" => "",
  555. "11月" => "",
  556. "12月" => ""
  557. );
  558. // 遍历月份
  559. foreach ($months as $month => $monthName) {
  560. if (isset($data[$month])){
  561. $count = count($data[$month][$process]);
  562. }
  563. $totalRate = 0;
  564. // 检查月份是否存在于数据中
  565. if (isset($data[$month])) {
  566. // 检查工序是否存在于月份数据中
  567. if (isset($data[$month][$process])) {
  568. // 累加废品率
  569. foreach ($data[$month][$process] as $rate) {
  570. $totalRate += floatval(1-$rate["废品率"]);
  571. }
  572. }
  573. }
  574. if ($count > 0){
  575. $rateNumber = $totalRate/$count;
  576. }
  577. // 格式化为百分比字符串,保留两位小数
  578. if ($totalRate > 0) {
  579. $result[$index][$monthName] = sprintf("%.2f%%", $rateNumber * 100);
  580. } else {
  581. $result[$index][$monthName] = ""; // 如果没有数据,则为空字符串
  582. }
  583. }
  584. }
  585. foreach ($result as &$item) {
  586. $total = 0;
  587. $count = 0;
  588. // 遍历月份,计算总和和数量
  589. for ($i = 1; $i <= 12; $i++) {
  590. $month = sprintf("%02d月", $i); // 格式化月份,例如 "01月"
  591. if (isset($item[$month]) && $item[$month] !== "") {
  592. // 移除百分号,并将字符串转换为浮点数
  593. $value = floatval(str_replace("%", "", $item[$month]));
  594. $total += $value;
  595. $count++;
  596. }
  597. }
  598. // 计算平均值
  599. if ($count > 0) {
  600. $average = $total / $count;
  601. // 格式化为百分比字符串,保留两位小数
  602. $item["平均值"] = sprintf("%.2f%%", $average);
  603. } else {
  604. $item["平均值"] = "0.00%"; // 如果没有数据,则为 0.00%
  605. }
  606. }
  607. $this->success('成功',$result);
  608. }
  609. /**
  610. * 获取年分数据
  611. * @return void
  612. */
  613. public function GetYear()
  614. {
  615. if ($this->request->isGet() === false){
  616. $this->error('请求错误');
  617. }
  618. $data = \db('工单_质量考核汇总')
  619. ->group('year')
  620. ->column('YEAR(STR_TO_DATE(sys_ny, "%Y%m")) as year');
  621. $this->success('成功',$data);
  622. }
  623. /**
  624. * 数据透视表
  625. * @return null
  626. * @throws \think\Exception
  627. * @throws \think\db\exception\DataNotFoundException
  628. * @throws \think\db\exception\ModelNotFoundException
  629. * @throws \think\exception\DbException
  630. */
  631. public function PoductData()
  632. {
  633. if (!$this->request->isGet()) {
  634. $this->error('请求错误');
  635. }
  636. $param = $this->request->param();
  637. if (empty($param['year'])) {
  638. $this->error('参数错误');
  639. }
  640. // 定义分类规则及顺序
  641. $processOrder = [
  642. 1 => ['name' => '胶印工序', 'keys' => ['胶印', '上光']],
  643. 2 => ['name' => '凹印工序', 'keys' => ['卷凹']],
  644. 3 => ['name' => '圆烫工序', 'keys' => ['圆烫']],
  645. 4 => ['name' => '圆切工序', 'keys' => ['圆切']],
  646. 5 => ['name' => '烫模工序', 'keys' => ['烫金', '模切', '凹凸']],
  647. 6 => ['name' => '丝印工序', 'keys' => ['丝印']],
  648. 7 => ['name' => '喷码工序', 'keys' => ['喷码']],
  649. 8 => ['name' => '单凹工序', 'keys' => ['单凹']]
  650. ];
  651. // 构建基础查询
  652. $query = \db('工单_质量考核汇总')
  653. ->alias('a')
  654. ->field('
  655. a.sczl_jtbh AS 机台编号,
  656. a.Gy0_gdbh AS 工单编号,
  657. a.印件及工序 AS 工序号,
  658. a.产品名称 AS 印件名称,
  659. a.工序 AS 工序,
  660. a.工序名称,
  661. a.联数,
  662. a.班组产量,
  663. a.班组制程废品,
  664. a.班组质检废品,
  665. a.sczl_bzdh AS 班组编号,
  666. b.印刷方式,
  667. b.版距,
  668. DATE_FORMAT(a.入仓日期, \'%Y%m\') AS 完工年月
  669. ')
  670. ->join('工单_工艺资料 b', 'a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  671. ->where('a.sys_ny', 'like', $param['year'] . '%')
  672. ->where('a.工序名称','not like','%机检%')
  673. ->where('a.工序名称','not like','%拆片%');
  674. // 获取总数
  675. $total = clone $query;
  676. $total = $total->count();
  677. // 执行查询
  678. $rawList = isset($param['page'], $param['limit'])
  679. ? $query->page($param['page'])->limit($param['limit'])->select()
  680. : $query->select();
  681. // 处理分类标记
  682. $processedList = [];
  683. foreach ($rawList as $item) {
  684. $matched = false;
  685. // 顺序匹配分类规则
  686. foreach ($processOrder as $typeId => $rule) {
  687. foreach ($rule['keys'] as $keyword) {
  688. if (strpos($item['工序名称'], $keyword) !== false) {
  689. // 添加分类标记
  690. $item['type_id'] = $typeId;
  691. $item['type_name'] = $rule['name'];
  692. $processedList[] = $item;
  693. $matched = true;
  694. break 2; // 跳出两层循环
  695. }
  696. }
  697. }
  698. // 未匹配到分类的数据丢弃
  699. }
  700. // 按分类顺序排序(保持1-8的顺序)
  701. usort($processedList, function($a, $b) {
  702. return $a['type_id'] <=> $b['type_id'];
  703. });
  704. // 返回结构
  705. $data['data'] = $processedList;
  706. $data['total'] = $total; // 原始总数
  707. $data['filtered_total'] = count($processedList); // 实际有效数
  708. return count($processedList) > 0
  709. ? $this->success('成功', $data)
  710. : $this->error('未找到数据');
  711. }
  712. }