Decision.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. /**
  122. * 机台班次生产工单明细
  123. * @return void
  124. * @throws \think\db\exception\DataNotFoundException
  125. * @throws \think\db\exception\ModelNotFoundException
  126. * @throws \think\exception\DbException
  127. */
  128. public function MachineProductDetail()
  129. {
  130. if ($this->request->isGet() === false){
  131. $this->error('请求错误');
  132. }
  133. $param = $this->request->param();
  134. if (empty($param['mouth'])){
  135. $this->error('参数错误');
  136. }
  137. if (isset($param['machine']) && !empty($param['machine'])){
  138. $where['a.sczl_jtbh'] = $param['machine'];
  139. }
  140. if (isset($param['team']) && !empty($param['team'])){
  141. $where['a.sczl_bzdh'] = ['like',substr($param['team'],0,1).'%'];
  142. }
  143. //将参数装换成标准日期格式
  144. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  145. $where['a.sczl_rq'] = ['like',$mouth.'%'];
  146. // $team = substr($param['team'],0,1);
  147. $list = \db('设备_产量计酬')
  148. ->alias('a')
  149. ->join('工单_印件资料 c','a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno')
  150. ->join('工单_工艺资料 d','a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh')
  151. ->field([
  152. 'a.sczl_gdbh' => '工单编号',
  153. 'a.sczl_yjno' => '印件号',
  154. 'a.sczl_gxh' => '工序号',
  155. 'c.yj_yjmc' => '印件名称',
  156. 'CONCAT(d.Gy0_gxmc,"(",d.Add_gxmc,")")' => '工序名称',
  157. 'DATE(a.sczl_rq)' => '工作日期',
  158. 'a.sczl_jtbh' => '机台编号',
  159. 'a.sczl_bzdh' => '班组编号',
  160. 'SUM(a.sczl_cl)' => '产量',
  161. 'a.sczl_ms' => '墨色数',
  162. 'rtrim(d.印刷方式)' => '印刷方式',
  163. 'rtrim(d.版距)' => '版距'
  164. ])
  165. ->where($where)
  166. ->group('a.sczl_gdbh,a.sczl_yjno,a.sczl_gxh,a.sczl_rq')
  167. ->order('工作日期')
  168. ->select();
  169. if (!empty($list)){
  170. foreach ($list as $key=>$value){
  171. $list[$key]['印件名称'] = $value['印件号'].'-'.$value['印件名称'];
  172. $list[$key]['工序名称'] = $value['工序号'].'-'.$value['工序名称'];
  173. unset($list[$key]['印件号'],$list[$key]['工序号']);
  174. if ($value['印刷方式'] === '卷对卷'){
  175. $list[$key]['产量'] = round($value['产量']/$value['版距']*1000);
  176. }
  177. if ($value['墨色数'] === '0.00'){
  178. $list[$key]['墨色数'] = '1.00';
  179. }
  180. }
  181. }
  182. $this->success('成功',$list);
  183. }
  184. /**
  185. * 月度机台运行工时汇总
  186. * @return void
  187. * @throws \think\db\exception\DataNotFoundException
  188. * @throws \think\db\exception\ModelNotFoundException
  189. * @throws \think\exception\DbException
  190. */
  191. public function MachineOperation()
  192. {
  193. if ($this->request->isGet() === false){
  194. $this->error('请求错误');
  195. }
  196. $param = $this->request->param();
  197. if (empty($param['mouth'])){
  198. $this->error('参数错误');
  199. }
  200. $where = [];
  201. if(!empty($param['sist'])){
  202. $where['a.设备编组'] = $param['sist'];
  203. }
  204. //将参数装换成标准日期格式
  205. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  206. $list = \db('设备_基本资料')
  207. ->alias('a')
  208. ->join('设备_产量计酬 b', 'a.设备编号 = b.sczl_jtbh')
  209. ->join('工单_印件资料 c', 'b.sczl_gdbh = c.Yj_Gdbh AND b.sczl_yjno = c.yj_Yjno')
  210. ->join('工单_工艺资料 d', 'b.sczl_gdbh = d.Gy0_gdbh AND b.sczl_yjno = d.Gy0_yjno AND b.sczl_gxh = d.Gy0_gxh')
  211. ->field([
  212. 'a.设备编号' => '设备编号',
  213. 'rtrim(a.设备名称)' => '设备名称',
  214. 'SUM(CASE WHEN rtrim(d.版距) = "卷对卷" THEN b.sczl_cl / NULLIF(d.版距, 0) * 1000 ELSE b.sczl_cl END) AS 产量',
  215. 'SUM(b.sczl_设备运行工时)' => '设备运行工时',
  216. 'SUM(b.sczl_保养工时)' => '保养工时',
  217. 'SUM(b.sczl_打样总工时)' => '打样总工时',
  218. 'SUM(b.sczl_打样工时)' => '打样补产工时',
  219. 'SUM(b.sczl_装版总工时)' => '装版总工时',
  220. 'SUM(b.sczl_装版工时)' => '装板补产工时',
  221. 'SUM(b.sczl_异常停机工时)' => '异常停机工时',
  222. 'rtrim(d.印刷方式)' => '印刷方式',
  223. 'rtrim(d.版距)' => '版距'
  224. ])
  225. ->where($where)
  226. ->whereTime('b.sczl_rq', $mouth)
  227. ->group('a.设备编号')
  228. ->order('a.设备编号')
  229. ->select();
  230. $total = \db('设备_基本资料')
  231. ->alias('a')
  232. ->join('设备_产量计酬 b','a.设备编号 = b.sczl_jtbh')
  233. ->field([
  234. 'SUM(b.sczl_设备运行工时)' => '设备运行工时',
  235. 'SUM(b.sczl_保养工时)' => '保养工时',
  236. 'SUM(b.sczl_打样总工时)' => '打样总工时',
  237. 'SUM(b.sczl_打样工时)' => '打样补产工时',
  238. 'SUM(b.sczl_装版总工时)' => '装板总工时',
  239. 'SUM(b.sczl_装版工时)' => '装板补产工时',
  240. 'SUM(b.sczl_异常停机工时)' => '异常停机工时'
  241. ])
  242. ->where($where)
  243. ->whereTime('b.sczl_rq',$mouth)
  244. ->find();
  245. $list['total'] = $total;
  246. $this->success('成功',$list);
  247. }
  248. /**
  249. * 设备运行工时机台生产工单数据详情
  250. * @return void
  251. * @throws \think\db\exception\DataNotFoundException
  252. * @throws \think\db\exception\ModelNotFoundException
  253. * @throws \think\exception\DbException
  254. */
  255. public function MachineOperationProductDetail()
  256. {
  257. if ($this->request->isGet() === false){
  258. $this->error('请求错误');
  259. }
  260. $param = $this->request->param();
  261. if (empty($param['mouth'])){
  262. $this->error('参数错误');
  263. }
  264. if (isset($param['machine']) && !empty($param['machine'])){
  265. $where['a.sczl_jtbh'] = $param['machine'];
  266. }
  267. //将参数装换成标准日期格式
  268. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  269. $where['a.sczl_rq'] = ['like',$mouth.'%'];
  270. $list = \db('设备_产量计酬')
  271. ->alias('a')
  272. ->join('设备_基本资料 b','a.sczl_jtbh = b.设备编号')
  273. ->join('工单_印件资料 c','a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno')
  274. ->join('工单_工艺资料 d','a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh')
  275. ->field([
  276. 'a.sczl_jtbh' => '设备编号',
  277. 'rtrim(b.设备名称)' => '设备名称',
  278. 'DATE(a.sczl_rq)' => '日期',
  279. 'a.sczl_gdbh' => '工单编号',
  280. 'a.sczl_yjno' => '印件号',
  281. 'a.sczl_gxh' => '工序号',
  282. 'c.yj_yjmc' => '印件名称',
  283. 'CONCAT(d.Gy0_gxmc,"(",d.Add_gxmc,")")' => '工序名称',
  284. 'SUM(a.sczl_cl)' => '产量',
  285. 'SUM(a.sczl_设备运行工时)' => '设备运行工时',
  286. 'SUM(a.sczl_保养工时)' => '保养工时',
  287. 'SUM(a.sczl_打样总工时)' => '打样总工时',
  288. 'SUM(a.sczl_打样工时)' => '打样补产工时',
  289. 'SUM(a.sczl_装版总工时)' => '装板总工时',
  290. 'SUM(a.sczl_装版工时)' => '装板补产工时',
  291. 'SUM(a.sczl_异常停机工时)' => '异常停机工时',
  292. 'a.sczl_ms' => '墨色数',
  293. 'rtrim(d.印刷方式)' => '印刷方式',
  294. 'rtrim(d.版距)' => '版距'
  295. ])
  296. ->where($where)
  297. ->group('a.sczl_rq,a.sczl_gdbh,a.sczl_rq')
  298. ->order('a.sczl_rq')
  299. ->select();
  300. if (!empty($list)){
  301. foreach ($list as $key=>$value){
  302. $list[$key]['工序名称'] = $value['印件号'].'-'.$value['工序号'].'-'.$value['工序名称'];
  303. unset($list[$key]['印件号'],$list[$key]['工序号']);
  304. if ($value['印刷方式'] === '卷对卷'){
  305. $list[$key]['产量'] = round($value['产量']/$value['版距']*1000);
  306. }
  307. if ($value['墨色数'] === '0.00'){
  308. $list[$key]['墨色数'] = '1.00';
  309. }
  310. }
  311. }
  312. $this->success('成功',$list);
  313. }
  314. //工序产出率月度统计报表
  315. public function ProcessOutputRate()
  316. {
  317. if ($this->request->isGet() === false){
  318. $this->error('请求错误');
  319. }
  320. $param = $this->request->param();
  321. if (empty($param)){
  322. $this->error('参数错误');
  323. }
  324. //查询已经进入超节损的月份
  325. $mouth = \db('工单_质量考核汇总')
  326. ->where('sys_ny','like',$param['year'].'%')
  327. ->column('distinct(sys_ny) as mouth');
  328. //创建工序数组
  329. $processType = ['胶印','卷凹','圆烫','圆切','烫金','模切','丝印','喷码','单凹'];
  330. $result = $data = [];
  331. foreach ($mouth as $key=>$value){
  332. foreach ($processType as $item){
  333. //查询进入超节损一年内所有数据
  334. $list = \db('工单_质量考核汇总')
  335. ->alias('a')
  336. ->join('工单_工艺资料 b','a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  337. ->join('工单_印件资料 c','a.Gy0_gdbh = c.Yj_Gdbh AND a.印件及工序 = c.yj_Yjno')
  338. ->field('a.sys_ny,(a.计划损耗+sum(a.班组质检废品))/a.印件工序产量 as 废品率')
  339. ->where('b.Gy0_gxmc','like',$item.'%')
  340. ->where('a.sys_ny',$value)
  341. ->group('a.Gy0_gdbh,b.Gy0_yjno,a.工序')
  342. ->select();
  343. $data[$value][$item] = $list;
  344. }
  345. }
  346. $months = [
  347. $param['year'].'01' => '01月',
  348. $param['year'].'02' => '02月',
  349. $param['year'].'03' => '03月',
  350. $param['year'].'04' => '04月',
  351. $param['year'].'05' => '05月',
  352. $param['year'].'06' => '06月',
  353. $param['year'].'07' => '07月',
  354. $param['year'].'08' => '08月',
  355. $param['year'].'09' => '09月',
  356. $param['year'].'10' => '10月',
  357. $param['year'].'11' => '11月',
  358. $param['year'].'12' => '12月'
  359. ];
  360. //汇总数据
  361. foreach ($processType as $index => $process) {
  362. $result[$index] = array(
  363. "工序类型" => $process,
  364. "01月" => "",
  365. "02月" => "",
  366. "03月" => "",
  367. "04月" => "",
  368. "05月" => "",
  369. "06月" => "",
  370. "07月" => "",
  371. "08月" => "",
  372. "09月" => "",
  373. "10月" => "",
  374. "11月" => "",
  375. "12月" => ""
  376. );
  377. // 遍历月份
  378. foreach ($months as $month => $monthName) {
  379. if (isset($data[$month])){
  380. $count = count($data[$month][$process]);
  381. }
  382. $totalRate = 0;
  383. // 检查月份是否存在于数据中
  384. if (isset($data[$month])) {
  385. // 检查工序是否存在于月份数据中
  386. if (isset($data[$month][$process])) {
  387. // 累加废品率
  388. foreach ($data[$month][$process] as $rate) {
  389. $totalRate += floatval(1-$rate["废品率"]);
  390. }
  391. }
  392. }
  393. if ($count > 0){
  394. $rateNumber = $totalRate/$count;
  395. }
  396. // 格式化为百分比字符串,保留两位小数
  397. if ($totalRate > 0) {
  398. $result[$index][$monthName] = sprintf("%.2f%%", $rateNumber * 100);
  399. } else {
  400. $result[$index][$monthName] = ""; // 如果没有数据,则为空字符串
  401. }
  402. }
  403. }
  404. foreach ($result as &$item) {
  405. $total = 0;
  406. $count = 0;
  407. // 遍历月份,计算总和和数量
  408. for ($i = 1; $i <= 12; $i++) {
  409. $month = sprintf("%02d月", $i); // 格式化月份,例如 "01月"
  410. if (isset($item[$month]) && $item[$month] !== "") {
  411. // 移除百分号,并将字符串转换为浮点数
  412. $value = floatval(str_replace("%", "", $item[$month]));
  413. $total += $value;
  414. $count++;
  415. }
  416. }
  417. // 计算平均值
  418. if ($count > 0) {
  419. $average = $total / $count;
  420. // 格式化为百分比字符串,保留两位小数
  421. $item["平均值"] = sprintf("%.2f%%", $average);
  422. } else {
  423. $item["平均值"] = "0.00%"; // 如果没有数据,则为 0.00%
  424. }
  425. }
  426. $this->success('成功',$result);
  427. }
  428. /**
  429. * 获取年分数据
  430. * @return void
  431. */
  432. public function GetYear()
  433. {
  434. if ($this->request->isGet() === false){
  435. $this->error('请求错误');
  436. }
  437. $data = \db('工单_质量考核汇总')
  438. ->group('year')
  439. ->column('YEAR(STR_TO_DATE(sys_ny, "%Y%m")) as year');
  440. $this->success('成功',$data);
  441. }
  442. }