Decision.php 16 KB

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