Decision.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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. // 验证请求方式
  48. if ($this->request->isGet() === false) {
  49. $this->error('请求错误');
  50. }
  51. // 获取请求参数
  52. $param = $this->request->param();
  53. if (empty($param['mouth'])) {
  54. $this->error('参数错误');
  55. }
  56. // 初始化查询条件
  57. $where = [];
  58. if (!empty($param['sist'])) {
  59. $where['b.设备编组'] = $param['sist'];
  60. }
  61. // 转换日期格式
  62. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  63. // 使用单个查询获取所有需要的数据
  64. $results = \db('设备_产量计酬')
  65. ->alias('a')
  66. ->join('设备_基本资料 b', 'a.sczl_jtbh = b.设备编号')
  67. ->join('工单_印件资料 c', 'a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno', 'LEFT')
  68. ->join('工单_工艺资料 d', 'a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh', 'LEFT')
  69. ->field([
  70. 'a.sczl_jtbh',
  71. 'a.sczl_bzdh',
  72. 'DATE_FORMAT(a.sczl_rq, "%Y-%m-%d") as day',
  73. 'SUM(a.sczl_cl) AS total_cl',
  74. 'c.yj_yjmc AS 印件名称',
  75. 'CONCAT(d.Gy0_gxmc,"(",d.Add_gxmc,")") AS 工序名称',
  76. 'RTRIM(d.印刷方式) AS 印刷方式',
  77. 'RTRIM(d.版距) AS 版距'
  78. ])
  79. ->where('a.sczl_rq', 'like', $mouth . '%')
  80. ->where('b.sys_sbID', '<>', '')
  81. ->where($where)
  82. ->group('a.sczl_jtbh, a.sczl_bzdh, day, c.yj_yjmc, d.Gy0_gxmc')
  83. ->order('a.sczl_jtbh, a.sczl_bzdh, day')
  84. ->select();
  85. // 数据处理
  86. $data = [];
  87. // 获取唯一日期并排序
  88. $data['head'] = array_unique(array_column($results, 'day'));
  89. // 将日期转为时间戳以排序
  90. usort($data['head'], function($a, $b) {
  91. return strtotime($a) - strtotime($b);
  92. });
  93. // 准备按设备和班组整理的数据
  94. $total_by_machine = [];
  95. foreach ($results as $item) {
  96. $machine_id = $item['sczl_jtbh'];
  97. $group_id = $item['sczl_bzdh'];
  98. $day = $item['day'];
  99. $printed_type = $item['印刷方式'];
  100. // 初始化设备和班组
  101. if (!isset($total_by_machine[$machine_id])) {
  102. $total_by_machine[$machine_id] = [];
  103. }
  104. if (!isset($total_by_machine[$machine_id][$group_id])) {
  105. $total_by_machine[$machine_id][$group_id] = [
  106. '机台编号' => $machine_id,
  107. '班组编号' => $group_id,
  108. '印件名称' => $item['印件名称'],
  109. '工序名称' => $item['工序名称'],
  110. 'total_cl' => 0,
  111. 'day_total' => []
  112. ];
  113. }
  114. // 更新累计数量
  115. if ($printed_type === '卷对卷') {
  116. $total_by_machine[$machine_id][$group_id]['total_cl'] += round($item['total_cl'] / $item['版距'] * 1000);
  117. } else {
  118. $total_by_machine[$machine_id][$group_id]['total_cl'] += $item['total_cl'];
  119. }
  120. // 更新每日总计
  121. if (!isset($total_by_machine[$machine_id][$group_id]['day_total'][$day])) {
  122. $total_by_machine[$machine_id][$group_id]['day_total'][$day] = 0;
  123. }
  124. if ($printed_type === '卷对卷') {
  125. $total_by_machine[$machine_id][$group_id]['day_total'][$day] += round($item['total_cl'] / $item['版距'] * 1000);
  126. } else {
  127. $total_by_machine[$machine_id][$group_id]['day_total'][$day] += $item['total_cl'];
  128. }
  129. }
  130. // 格式化输出数据
  131. $data['total'] = [];
  132. foreach ($total_by_machine as $machine_group) {
  133. foreach ($machine_group as $group_data) {
  134. $data['total'][] = $group_data;
  135. }
  136. }
  137. $this->success('成功', $data);
  138. }
  139. /**
  140. * 机台班次生产工单明细
  141. * @return void
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. * @throws \think\exception\DbException
  145. */
  146. public function MachineProductDetail()
  147. {
  148. if (!$this->request->isGet()) {
  149. $this->error('请求错误');
  150. }
  151. $param = $this->request->param();
  152. // 检查必需的参数
  153. if (empty($param['mouth'])) {
  154. $this->error('参数错误');
  155. }
  156. // Initializing where conditions
  157. $where = [];
  158. // 添加可选的查询条件
  159. if (!empty($param['machine'])) {
  160. $where['a.sczl_jtbh'] = $param['machine'];
  161. }
  162. if (!empty($param['team'])) {
  163. $where['a.sczl_bzdh'] = ['like', substr($param['team'], 0, 1) . '%'];
  164. }
  165. // 将参数转换成标准日期格式
  166. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  167. $where['a.sczl_rq'] = ['like', $mouth . '%'];
  168. // 分页配置
  169. $page = !empty($param['page']) ? (int)$param['page'] : 1;
  170. $limit = !empty($param['limit']) ? (int)$param['limit'] : 9999; // 默认查询所有
  171. // 查询数据
  172. $list = \db('设备_产量计酬')
  173. ->alias('a')
  174. ->join('工单_印件资料 c', 'a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno')
  175. ->join('工单_工艺资料 d', 'a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh')
  176. ->field([
  177. 'a.sczl_gdbh' => '工单编号',
  178. 'a.sczl_yjno' => '印件号',
  179. 'a.sczl_gxh' => '工序号',
  180. 'c.yj_yjmc' => '印件名称',
  181. 'CONCAT(d.Gy0_gxmc,"(",d.Add_gxmc,")")' => '工序名称',
  182. 'DATE(a.sczl_rq)' => '工作日期',
  183. 'a.sczl_jtbh' => '机台编号',
  184. 'a.sczl_bzdh' => '班组编号',
  185. 'SUM(a.sczl_cl)' => '产量',
  186. 'a.sczl_ms' => '墨色数',
  187. 'rtrim(d.印刷方式)' => '印刷方式',
  188. 'rtrim(d.版距)' => '版距'
  189. ])
  190. ->where($where)
  191. ->group('a.sczl_gdbh, a.sczl_yjno, a.sczl_gxh, a.sczl_rq, a.sczl_jtbh, a.sczl_bzdh')
  192. ->order('工作日期')
  193. ->page($page, $limit) // 使用 page 和 limit 实现分页
  194. ->select();
  195. // 数据处理
  196. if (!empty($list)) {
  197. foreach ($list as $key => $value) {
  198. $list[$key]['印件名称'] = $value['印件号'] . '-' . $value['印件名称'];
  199. $list[$key]['工序名称'] = $value['工序号'] . '-' . $value['工序名称'];
  200. unset($list[$key]['印件号'], $list[$key]['工序号']);
  201. // 产量计算
  202. if ($value['印刷方式'] === '卷对卷') {
  203. $list[$key]['产量'] = round($value['产量'] / $value['版距'] * 1000);
  204. }
  205. // 墨色数修正
  206. if ($value['墨色数'] === '0.00') {
  207. $list[$key]['墨色数'] = '1.00';
  208. }
  209. }
  210. }
  211. $this->success('成功', $list);
  212. }
  213. /**
  214. * 月度机台运行工时汇总
  215. * @return void
  216. * @throws \think\db\exception\DataNotFoundException
  217. * @throws \think\db\exception\ModelNotFoundException
  218. * @throws \think\exception\DbException
  219. */
  220. public function MachineOperation()
  221. {
  222. // 确保请求是GET
  223. if ($this->request->isGet() === false) {
  224. $this->error('请求错误');
  225. }
  226. // 获取请求参数
  227. $param = $this->request->param();
  228. if (empty($param['mouth'])) {
  229. $this->error('参数错误');
  230. }
  231. // 构建查询条件
  232. $where = [];
  233. if (!empty($param['sist'])) {
  234. $where['a.设备编组'] = $param['sist'];
  235. }
  236. // 将参数转换成标准日期格式
  237. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  238. // 单次查询,计算产量和各项工时
  239. $list = \db('设备_基本资料')
  240. ->alias('a')
  241. ->join('设备_产量计酬 b', 'a.设备编号 = b.sczl_jtbh')
  242. ->join('工单_印件资料 c', 'b.sczl_gdbh = c.Yj_Gdbh AND b.sczl_yjno = c.yj_Yjno')
  243. ->join('工单_工艺资料 d', 'b.sczl_gdbh = d.Gy0_gdbh AND b.sczl_yjno = d.Gy0_yjno AND b.sczl_gxh = d.Gy0_gxh')
  244. ->field([
  245. 'a.设备编号' => '设备编号',
  246. 'rtrim(a.设备名称)' => '设备名称',
  247. 'SUM(CASE WHEN rtrim(d.版距) = "卷对卷" THEN b.sczl_cl / NULLIF(d.版距, 0) * 1000 ELSE b.sczl_cl END) AS 产量',
  248. 'SUM(b.sczl_设备运行工时)' => '设备运行工时',
  249. 'SUM(b.sczl_保养工时)' => '保养工时',
  250. 'SUM(b.sczl_打样总工时)' => '打样总工时',
  251. 'SUM(b.sczl_打样工时)' => '打样补产工时',
  252. 'SUM(b.sczl_装版总工时)' => '装版总工时',
  253. 'SUM(b.sczl_装版工时)' => '装版补产工时',
  254. 'SUM(b.sczl_异常停机工时)' => '异常停机工时',
  255. 'rtrim(d.印刷方式)' => '印刷方式',
  256. 'rtrim(d.版距)' => '版距'
  257. ])
  258. ->where($where)
  259. ->where('b.sczl_rq', 'like', $mouth . '%')
  260. ->group('a.设备编号')
  261. ->order('a.设备编号')
  262. ->select();
  263. // 处理总工时的计算
  264. $total = [
  265. '设备运行工时' => 0,
  266. '保养工时' => 0,
  267. '打样总工时' => 0,
  268. '打样补产工时' => 0,
  269. '装版总工时' => 0,
  270. '装版补产工时' => 0,
  271. '异常停机工时' => 0
  272. ];
  273. // 计算各项总工时
  274. foreach ($list as $item) {
  275. $total['设备运行工时'] += $item['设备运行工时'];
  276. $total['保养工时'] += $item['保养工时'];
  277. $total['打样总工时'] += $item['打样总工时'];
  278. $total['打样补产工时'] += $item['打样补产工时'];
  279. $total['装版总工时'] += $item['装版总工时'];
  280. $total['装版补产工时'] += $item['装版补产工时'];
  281. $total['异常停机工时'] += $item['异常停机工时'];
  282. }
  283. // 将总工时添加到列表中
  284. $list['total'] = $total;
  285. // 返回成功响应
  286. $this->success('成功', $list);
  287. }
  288. /**
  289. * 设备运行工时机台生产工单数据详情
  290. * @return void
  291. * @throws \think\db\exception\DataNotFoundException
  292. * @throws \think\db\exception\ModelNotFoundException
  293. * @throws \think\exception\DbException
  294. */
  295. public function MachineOperationProductDetail()
  296. {
  297. if (!$this->request->isGet()) {
  298. $this->error('请求错误');
  299. }
  300. $param = $this->request->param();
  301. if (empty($param['mouth'])) {
  302. $this->error('参数错误');
  303. }
  304. // Initialize where conditions
  305. $where = [];
  306. if (!empty($param['machine'])) {
  307. $where['a.sczl_jtbh'] = $param['machine'];
  308. }
  309. // 将参数转换成标准日期格式
  310. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  311. $where['a.sczl_rq'] = ['like', $mouth . '%'];
  312. // 分页配置
  313. $page = !empty($param['page']) ? (int)$param['page'] : 1;
  314. $limit = !empty($param['limit']) ? (int)$param['limit'] : 9999; // 默认查询所有
  315. // 查询数据
  316. $list = \db('设备_产量计酬')
  317. ->alias('a')
  318. ->join('设备_基本资料 b', 'a.sczl_jtbh = b.设备编号')
  319. ->join('工单_印件资料 c', 'a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno')
  320. ->join('工单_工艺资料 d', 'a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh')
  321. ->field([
  322. 'a.sczl_jtbh' => '设备编号',
  323. 'rtrim(b.设备名称)' => '设备名称',
  324. 'DATE(a.sczl_rq)' => '日期',
  325. 'a.sczl_gdbh' => '工单编号',
  326. 'a.sczl_yjno' => '印件号',
  327. 'a.sczl_gxh' => '工序号',
  328. 'c.yj_yjmc' => '印件名称',
  329. 'CONCAT(d.Gy0_gxmc, "(", d.Add_gxmc, ")")' => '工序名称',
  330. 'SUM(a.sczl_cl)' => '产量',
  331. 'SUM(a.sczl_设备运行工时)' => '设备运行工时',
  332. 'SUM(a.sczl_保养工时)' => '保养工时',
  333. 'SUM(a.sczl_打样总工时)' => '打样总工时',
  334. 'SUM(a.sczl_打样工时)' => '打样补产工时',
  335. 'SUM(a.sczl_装版总工时)' => '装板总工时',
  336. 'SUM(a.sczl_装版工时)' => '装板补产工时',
  337. 'SUM(a.sczl_异常停机工时)' => '异常停机工时',
  338. 'a.sczl_ms' => '墨色数',
  339. 'rtrim(d.印刷方式)' => '印刷方式',
  340. 'rtrim(d.版距)' => '版距'
  341. ])
  342. ->where($where)
  343. ->group('a.sczl_rq, a.sczl_gdbh, a.sczl_gxh, a.sczl_jtbh')
  344. ->order('a.sczl_rq')
  345. ->page($page, $limit) // 使用 page 和 limit 技术实现分页
  346. ->select();
  347. // 数据处理
  348. if (!empty($list)) {
  349. foreach ($list as $key => $value) {
  350. $list[$key]['工序名称'] = $value['印件号'] . '-' . $value['工序号'] . '-' . $value['工序名称'];
  351. // 移除不需要的字段
  352. unset($list[$key]['印件号'], $list[$key]['工序号']);
  353. // 产量计算
  354. if ($value['印刷方式'] === '卷对卷') {
  355. $list[$key]['产量'] = round($value['产量'] / $value['版距'] * 1000);
  356. }
  357. // 墨色数修正
  358. if ($value['墨色数'] === '0.00') {
  359. $list[$key]['墨色数'] = '1.00';
  360. }
  361. }
  362. }
  363. $this->success('成功', $list);
  364. }
  365. /**
  366. * 工序产出率月度统计报表
  367. * @return void
  368. * @throws \think\db\exception\DataNotFoundException
  369. * @throws \think\db\exception\ModelNotFoundException
  370. * @throws \think\exception\DbException
  371. */
  372. // public function ProcessOutputRate()
  373. // {
  374. // if (!$this->request->isGet()) {
  375. // $this->error('请求错误');
  376. // }
  377. //
  378. // $param = $this->request->param();
  379. // if (empty($param)) {
  380. // $this->error('参数错误');
  381. // }
  382. //
  383. // // 工序分类
  384. //// $processOrder = [
  385. //// 1 => ['name' => '胶印', 'keys' => ['胶印', '上光']],
  386. //// 2 => ['name' => '卷凹', 'keys' => ['卷凹']],
  387. //// 3 => ['name' => '圆烫', 'keys' => ['圆烫']],
  388. //// 4 => ['name' => '圆切', 'keys' => ['圆切']],
  389. //// 5 => ['name' => '烫模', 'keys' => ['烫金', '模切', '凹凸']],
  390. //// 6 => ['name' => '丝印', 'keys' => ['丝印']],
  391. //// 7 => ['name' => '喷码', 'keys' => ['喷码']],
  392. //// 8 => ['name' => '单凹', 'keys' => ['单凹']],
  393. //// 9 => ['name' => '覆膜', 'keys' => ['覆膜']]
  394. //// ];
  395. // $sist = ['胶印车间','凹丝印车间','印后车间','检验车间'];
  396. // $processOrder= \db('设备_基本资料')
  397. // ->whereNotNull('设备编组')
  398. // ->whereIn('使用部门',$sist)
  399. // ->group('设备编组')
  400. // ->column('rtrim(设备编组) as 设备编组');
  401. //
  402. //
  403. // // 获取存在数据的月份
  404. // $monthsWithData = \db('工单_质量考核汇总')
  405. // ->where('sys_ny', 'like', $param['year'] . '%')
  406. // ->column('distinct(sys_ny) as month');
  407. //
  408. // // 初始化结果数组,按工序名称归类
  409. // $result = [];
  410. // foreach ($processOrder as $processId => $process) {
  411. // $result[substr($process, 5, 6)] = [
  412. // '01月' => '',
  413. // '02月' => '',
  414. // '03月' => '',
  415. // '04月' => '',
  416. // '05月' => '',
  417. // '06月' => '',
  418. // '07月' => '',
  419. // '08月' => '',
  420. // '09月' => '',
  421. // '10月' => '',
  422. // '11月' => '',
  423. // '12月' => '',
  424. // '平均值' => '0.00%'
  425. // ];
  426. // }
  427. //
  428. // // 遍历每个月份
  429. // foreach ($monthsWithData as $month) {
  430. // foreach ($processOrder as $processId => $process) {
  431. // // 查询该月份该工序的合格率数据
  432. // $query = \db('工单_质量考核汇总')
  433. // ->alias('a')
  434. // ->join('工单_工艺资料 b', 'a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  435. // ->join('工单_印件资料 c', 'a.Gy0_gdbh = c.Yj_Gdbh AND a.印件及工序 = c.yj_Yjno')
  436. // ->join('成品入仓 d', 'a.Gy0_gdbh = d.jjcp_gdbh AND a.印件及工序 = d.jjcp_yjno')
  437. // ->join('设备_基本资料 e','a.sczl_jtbh = e.设备编号')
  438. //// ->field('a.sys_ny,(1 - ((SUM(a.班组制程废品) + SUM(a.班组质检废品)) / SUM(a.班组产量))) * 100 as 合格率')
  439. // ->field('a.sys_ny,SUM(a.班组制程废品)/a.联数 as 制程废品,SUM(a.班组质检废品)/a.联数 as 质检废品,SUM(a.班组产量)/a.联数 as 产量')
  440. // ->where('e.设备编组',$process)
  441. // ->where('d.jjcp_smb', 'like', '末%')
  442. // ->where('a.sys_ny', $month)
  443. // ->group('a.sys_ny,a.Gy0_gdbh,a.印件及工序,a.工序');
  444. //// ->group('a.sys_ny');
  445. //
  446. // $res = $query->select();
  447. // $data = ['sys_ny'=>'','制程废品'=>0, '质检废品'=>0, '产量'=>0];
  448. // foreach ($res as $value) {
  449. // $data['sys_ny'] = $value['sys_ny'];
  450. // $data['制程废品'] += $value['制程废品'];
  451. // $data['质检废品'] += $value['质检废品'];
  452. // $data['产量'] += $value['产量'];
  453. // }
  454. // $records = [
  455. // 'sys_ny' => $data['sys_ny'],
  456. // '合格率' => (1-(($data['制程废品']+$data['质检废品'])/$data['产量']))* 100
  457. // ];
  458. //// halt($records);
  459. // if (!empty($records)) {
  460. // $monthName = substr($month, 4, 2) . '月';
  461. // $result[substr($process, 5, 6)][$monthName] = sprintf("%.2f%%", $records['合格率']);
  462. // }
  463. // }
  464. // }
  465. //
  466. // // 计算年度平均值
  467. // foreach ($result as $processName => &$processData) {
  468. // $totalAnnual = 0;
  469. // $monthCount = 0;
  470. //
  471. // foreach ($processData as $month => $rate) {
  472. // if ($month !== '平均值' && $rate !== '') {
  473. // $totalAnnual += floatval(str_replace('%', '', $rate));
  474. // $monthCount++;
  475. // }
  476. // }
  477. //
  478. // if ($monthCount > 0) {
  479. // $processData['平均值'] = sprintf("%.2f%%", $totalAnnual / $monthCount);
  480. // }
  481. // }
  482. // unset($processData); // 关键修复:解除引用
  483. //
  484. // // 转换为目标格式
  485. // $formattedData = [];
  486. // foreach ($result as $processName => $processData) {
  487. // // 现在processData是独立值
  488. // $formattedRow = array_merge(['工序类型' => $processName], $processData);
  489. // $formattedData[] = $formattedRow;
  490. // }
  491. // // 返回JSON响应
  492. // $this->success('成功',$formattedData);
  493. // }
  494. public function ProcessOutputRate()
  495. {
  496. if (!$this->request->isGet()) {
  497. $this->error('请求错误');
  498. }
  499. $param = $this->request->param();
  500. if (empty($param) || empty($param['year'])) {
  501. $this->error('参数错误');
  502. }
  503. // 1. 获取设备分组,作为工序依据
  504. $sist = ['胶印车间','凹丝印车间','印后车间','检验车间'];
  505. $processOrder = \db('设备_基本资料')
  506. ->whereNotNull('设备编组')
  507. ->whereIn('使用部门', $sist)
  508. ->group('设备编组')
  509. ->column('RTRIM(设备编组) as 设备编组');
  510. if (empty($processOrder)) {
  511. $this->success('成功', []); // 没有设备直接返回
  512. }
  513. // 2. 初始化结果数组(按工序)
  514. $result = [];
  515. foreach ($processOrder as $process) {
  516. $key = substr($process, 5, 6);
  517. $result[$key] = [];
  518. for ($i = 1; $i <= 12; $i++) {
  519. $result[$key][str_pad($i, 2, '0', STR_PAD_LEFT) . '月'] = '';
  520. }
  521. $result[$key]['平均值'] = '0.00%';
  522. }
  523. // 3. 汇总所有年份内数据,一次查出所有“工序-月份”数据,避免嵌套多次SQL
  524. $dataList = \db('工单_质量考核汇总')
  525. ->alias('a')
  526. ->join('工单_工艺资料 b', 'a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  527. ->join('工单_印件资料 c', 'a.Gy0_gdbh = c.Yj_Gdbh AND a.印件及工序 = c.yj_Yjno')
  528. ->join('成品入仓 d', 'a.Gy0_gdbh = d.jjcp_gdbh AND a.印件及工序 = d.jjcp_yjno')
  529. ->join('设备_基本资料 e', 'a.sczl_jtbh = e.设备编号')
  530. ->whereIn('e.设备编组', $processOrder)
  531. ->where('d.jjcp_smb', 'like', '末%')
  532. ->where('a.sys_ny', 'like', $param['year'] . '%')
  533. ->field([
  534. 'e.设备编组',
  535. 'a.sys_ny',
  536. 'SUM(a.班组制程废品/ a.联数) AS 制程废品',
  537. 'SUM(a.班组质检废品/ a.联数) AS 质检废品',
  538. 'SUM(a.班组产量/ a.联数) AS 产量'
  539. ])
  540. ->group('e.设备编组, a.sys_ny')
  541. ->select();
  542. // 4. 处理统计数据,填充到$result
  543. $annual = []; // 年均合格率累加用
  544. foreach ($dataList as $item) {
  545. $processKey = substr($item['设备编组'], 5, 6);
  546. $monthKey = substr($item['sys_ny'], 4, 2) . '月';
  547. $output = floatval($item['产量']);
  548. $waste = floatval($item['制程废品']) + floatval($item['质检废品']);
  549. $rate = $output > 0 ? (1 - $waste / $output) * 100 : 0;
  550. $result[$processKey][$monthKey] = sprintf('%.2f%%', $rate);
  551. if (!isset($annual[$processKey])) {
  552. $annual[$processKey] = ['sum' => 0, 'count' => 0];
  553. }
  554. $annual[$processKey]['sum'] += $rate;
  555. $annual[$processKey]['count']++;
  556. }
  557. // 5. 补充“平均值”
  558. foreach ($annual as $processKey => $v) {
  559. if ($v['count'] > 0) {
  560. $result[$processKey]['平均值'] = sprintf('%.2f%%', $v['sum']/$v['count']);
  561. }
  562. }
  563. // 6. 转换为前端需要的格式
  564. $formattedData = [];
  565. foreach ($result as $process => $data) {
  566. $row = array_merge(['工序类型' => $process], $data);
  567. $formattedData[] = $row;
  568. }
  569. // 7. 返回
  570. $this->success('成功', $formattedData);
  571. }
  572. /**
  573. * 获取年分数据
  574. * @return void
  575. */
  576. public function GetYear()
  577. {
  578. if ($this->request->isGet() === false){
  579. $this->error('请求错误');
  580. }
  581. $data = \db('工单_质量考核汇总')
  582. ->group('year')
  583. ->column('YEAR(STR_TO_DATE(sys_ny, "%Y%m")) as year');
  584. $this->success('成功',$data);
  585. }
  586. /**
  587. * 数据透视表
  588. * @return null
  589. * @throws \think\Exception
  590. * @throws \think\db\exception\DataNotFoundException
  591. * @throws \think\db\exception\ModelNotFoundException
  592. * @throws \think\exception\DbException
  593. */
  594. public function PoductData()
  595. {
  596. if (!$this->request->isGet()) {
  597. $this->error('请求错误');
  598. }
  599. $param = $this->request->param();
  600. if (empty($param['year'])) {
  601. $this->error('参数错误');
  602. }
  603. // 获取设备编组列表(已按编号排序)
  604. $processGroups = \db('设备_基本资料')
  605. ->whereNotNull('设备编组')
  606. ->group('设备编组')
  607. ->column('rtrim(设备编组) as 设备编组');
  608. // 构建基础查询
  609. $query = \db('工单_质量考核汇总')
  610. ->alias('a')
  611. ->field('
  612. a.sczl_jtbh AS 机台编号,
  613. a.Gy0_gdbh AS 工单编号,
  614. a.印件及工序 AS 工序号,
  615. a.产品名称 AS 印件名称,
  616. a.工序 AS 工序,
  617. a.工序名称,
  618. b.Gy0_gxmc,
  619. a.联数,
  620. a.班组产量,
  621. a.班组制程废品,
  622. a.班组质检废品,
  623. a.sczl_bzdh AS 班组编号,
  624. b.印刷方式,
  625. b.版距,
  626. DATE_FORMAT(a.入仓日期, \'%Y%m\') AS 完工年月,
  627. d.设备编组
  628. ')
  629. ->join('工单_工艺资料 b', 'a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  630. ->join('成品入仓 c', 'a.Gy0_gdbh = c.jjcp_gdbh AND a.印件及工序 = c.jjcp_yjno')
  631. ->join('设备_基本资料 d', 'a.sczl_jtbh = d.设备编号')
  632. ->where('a.sys_ny', 'like', $param['year'] . '%')
  633. ->where('a.工序名称', 'not like', '%机检%')
  634. ->where('c.jjcp_smb', 'like', '末%')
  635. ->where('a.工序名称', 'not like', '%拆片%');
  636. // 获取总数
  637. $total = clone $query;
  638. $total = $total->count();
  639. // 执行查询
  640. $rawList = isset($param['page'], $param['limit'])
  641. ? $query->page($param['page'])->limit($param['limit'])->select()
  642. : $query->select();
  643. // 构建分类映射表
  644. $groupMap = [];
  645. foreach ($processGroups as $index => $group) {
  646. // 提取机组名称核心词(去除编号和"机组"字样)
  647. $groupName = preg_replace('/^\d+、/', '', $group); // 移除开头编号
  648. $coreName = str_replace('机组', '', $groupName); // 移除尾部的"机组"
  649. $groupMap[] = [
  650. 'id' => $index + 1,
  651. 'name' => $groupName,
  652. 'core' => $coreName
  653. ];
  654. }
  655. // 处理分类标记
  656. $processedList = [];
  657. foreach ($rawList as $item) {
  658. foreach ($groupMap as $group) {
  659. // 使用核心词进行模糊匹配
  660. if (strpos($item['设备编组'], $group['core']) !== false) {
  661. $item['type_id'] = $group['id'];
  662. $item['type_name'] = $group['name'];
  663. $processedList[] = $item;
  664. continue 2;
  665. }
  666. }
  667. }
  668. // 按分类ID排序
  669. usort($processedList, function($a, $b) {
  670. return $a['type_id'] <=> $b['type_id'];
  671. });
  672. // 返回结构
  673. $data['data'] = $processedList;
  674. $data['total'] = $total;
  675. $data['filtered_total'] = count($processedList);
  676. return count($processedList) > 0
  677. ? $this->success('成功', $data)
  678. : $this->error('未找到数据');
  679. }
  680. /**
  681. * 月度色度数导出
  682. * @return void
  683. * @throws \think\db\exception\DataNotFoundException
  684. * @throws \think\db\exception\ModelNotFoundException
  685. * @throws \think\exception\DbException
  686. */
  687. public function ChromaticityNumber()
  688. {
  689. if (!$this->request->isGet()) {
  690. $this->error('请求错误');
  691. }
  692. $param = $this->request->param();
  693. if (empty($param['month'])) {
  694. $this->error('参数错误');
  695. }
  696. $month = date_create_from_format('Ym', $param['month'])->format('Y-m');
  697. $where = ['a.sczl_rq' => ['like', $month . '%']];
  698. $list = \db('设备_产量计酬')
  699. ->alias('a')
  700. ->join('工单_印件资料 c', 'a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno')
  701. ->join('工单_工艺资料 d', 'a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh')
  702. ->join('设备_基本资料 e', 'a.sczl_jtbh = e.设备编号')
  703. ->field([
  704. 'a.sczl_gdbh' => '工单编号',
  705. 'a.sczl_yjno' => '印件号',
  706. 'a.sczl_gxh' => '工序号',
  707. 'd.Gy0_gxmc' => '工序名称',
  708. 'SUM(a.sczl_cl)' => '产量',
  709. 'a.sczl_ms' => '墨色数',
  710. 'rtrim(d.印刷方式)' => '印刷方式',
  711. 'rtrim(d.版距)' => '版距',
  712. 'rtrim(d.Gy0_SITE)' => '车间名称',
  713. 'rtrim(e.设备编组)' => '设备编组',
  714. 'a.sczl_jtbh'
  715. ])
  716. ->where($where)
  717. ->group('a.sczl_gdbh, a.sczl_yjno, a.sczl_gxh, a.sczl_rq, a.sczl_jtbh, a.sczl_bzdh')
  718. ->select();
  719. $data = [];
  720. if (!empty($list)) {
  721. foreach ($list as $key => $value) {
  722. // Convert ALL outputs to integers
  723. $value['产量'] = (int)round($value['产量']);
  724. if ($value['印刷方式'] === '卷对卷' && $value['版距'] != 0) {
  725. $value['产量'] = (int)round($value['产量'] / $value['版距'] * 1000);
  726. }
  727. if($value['墨色数'] == 0 && $value['设备编组'] !== '11、检品机组'){
  728. $value['墨色数'] = 1;
  729. }
  730. if($value['设备编组'] === '11、检品机组'){
  731. $value['墨色数'] = 0;
  732. }
  733. $data[$key] = [
  734. 'gdbh' => $value['工单编号'],
  735. 'ms' => $value['墨色数'],
  736. '产量' => $value['产量'],
  737. '墨色' => $value['产量'] * $value['墨色数'],
  738. '设备编组' => $value['设备编组']
  739. ];
  740. }
  741. }
  742. $grouped = [];
  743. foreach ($data as $item) {
  744. $key = substr($item['设备编组'], 5, 6);
  745. if (!isset($grouped[$key])) {
  746. $grouped[$key] = [
  747. '产量合计' => 0,
  748. '工序名称' => $key,
  749. '色度数' => 0
  750. ];
  751. }
  752. $grouped[$key]['产量合计'] += (int)$item['产量'];
  753. $grouped[$key]['色度数'] += (int)$item['墨色'];
  754. }
  755. foreach ($grouped as &$group) {
  756. $group['产量合计'] = (int)$group['产量合计'];
  757. $group['色度数'] = (int)$group['色度数'];
  758. }
  759. $this->success('成功', array_values($grouped));
  760. }
  761. /**
  762. * 年度质检废品率统计左侧菜单
  763. * @return void
  764. * @throws \think\db\exception\DataNotFoundException
  765. * @throws \think\db\exception\ModelNotFoundException
  766. * @throws \think\exception\DbException
  767. */
  768. public function QualityInspectionGetTab()
  769. {
  770. if (!$this->request->isGet()) {
  771. $this->error('请求错误');
  772. }
  773. $monthStats = \db('db_qczl')
  774. ->group('month')
  775. ->order('month desc')
  776. ->column("DATE_FORMAT(qczl_rq, '%Y%m') AS month");
  777. // 按年月层次结构归类(返回树形结构)
  778. $treeData = \db('db_qczl')
  779. ->field([
  780. "DATE_FORMAT(qczl_rq, '%Y') AS year",
  781. "DATE_FORMAT(qczl_rq, '%m') AS month",
  782. "COUNT(*) AS total"
  783. ])
  784. ->order('year desc,month desc')
  785. ->group('year, month')
  786. ->select();
  787. // 构建树形结构
  788. $result = [];
  789. foreach ($treeData as $item) {
  790. $year = $item['year'];
  791. $month = $year . $item['month'];
  792. if (!isset($result[$year])) {
  793. $result[$year] = [
  794. 'year' => $year,
  795. 'children' => []
  796. ];
  797. }
  798. $result[$year]['children'][] = [
  799. 'month' => $month,
  800. 'total' => $item['total']
  801. ];
  802. }
  803. $this->success('成功', $result);
  804. }
  805. /**
  806. * 年度质检废品统计右侧上方列表
  807. * @return void
  808. * @throws \think\db\exception\DataNotFoundException
  809. * @throws \think\db\exception\ModelNotFoundException
  810. * @throws \think\exception\DbException
  811. */
  812. public function QualityInspectionList()
  813. {
  814. if (!$this->request->isGet()) {
  815. $this->error('请求错误');
  816. }
  817. $param = $this->request->param();
  818. $where = ['年月' => ['like', $param['month'] . '%']];
  819. // 查询工单列表
  820. $workList = \db('rec_月度废品汇总')
  821. ->where($where)
  822. ->field("CONCAT(Gd_gdbh,'-',印件号) as 工单,实际投料")
  823. ->group('工单')
  824. ->select();
  825. $work = [];
  826. foreach ($workList as $item) {
  827. $work[$item['工单']] = $item['实际投料'];
  828. }
  829. // 查询工单对应的废品类别首字母(按工单和首字母分组)
  830. $WasteWorkList = \db('rec_月度废品汇总')
  831. ->where($where)
  832. ->field("CONCAT(Gd_gdbh,'-',印件号) as 工单, left(废品类别,1) as 废品类别首字母")
  833. ->group('工单, 废品类别首字母')
  834. ->order('废品类别首字母 asc')
  835. ->select();
  836. $waste = [];
  837. foreach ($WasteWorkList as $item) {
  838. if (!isset($waste[$item['废品类别首字母']])) {
  839. $waste[$item['废品类别首字母']] = [];
  840. }
  841. $waste[$item['废品类别首字母']][] = $item['工单'];
  842. }
  843. // 计算每个首字母的实际投料总和
  844. $res = [];
  845. foreach ($waste as $key => $item) {
  846. $res[$key] = array_sum(array_intersect_key($work, array_flip($item)));
  847. }
  848. // 查询废品类别对应的废品数量
  849. $wasteList = \db('rec_月度废品汇总')
  850. ->where($where)
  851. ->group('废品类别')
  852. ->order('废品类别 asc')
  853. ->column("sum(废品数量) as 废品数量", "废品类别");
  854. // 从废品类别数据计算每个首字母的废品数量总和
  855. $classification = [];
  856. foreach ($wasteList as $category => $amount) {
  857. $firstChar = substr($category, 0, 1);
  858. if (!isset($classification[$firstChar])) {
  859. $classification[$firstChar] = 0;
  860. }
  861. $classification[$firstChar] += $amount;
  862. }
  863. // 构建数据数组
  864. $data = [];
  865. foreach ($wasteList as $category => $amount) {
  866. $firstChar = substr($category, 0, 1);
  867. $number = $res[$firstChar] ?? 0;
  868. $data[] = [
  869. '废品类别' => $category,
  870. '实际投料' => $number,
  871. '废品数量' => $amount,
  872. '质检废品率' => (round($amount/$number, 7) * 100) . '%',
  873. ];
  874. }
  875. // 添加首字母合计行
  876. foreach ($classification as $firstChar => $amount) {
  877. $number = $res[$firstChar] ?? 0;
  878. $data[] = [
  879. '废品类别' => $firstChar . '-合计',
  880. '实际投料' => $number,
  881. '废品数量' => $amount,
  882. '质检废品率' => (round($amount/$number, 7) * 100) . '%',
  883. ];
  884. }
  885. $this->success('成功', $data);
  886. }
  887. //判断字符串首位是否为英文字母
  888. function isFirstCharEnglish($str) {
  889. if (mb_strlen($str) == 0) return false;
  890. $firstChar = mb_substr($str, 0, 1);
  891. return preg_match('/^[A-Za-z]$/u', $firstChar);
  892. }
  893. /**
  894. * 年度质检废品率统计下方列表
  895. * @return void
  896. * @throws \think\db\exception\DataNotFoundException
  897. * @throws \think\db\exception\ModelNotFoundException
  898. * @throws \think\exception\DbException
  899. */
  900. public function QualityInspectionDetailList()
  901. {
  902. if ($this->request->isGet() === false) {
  903. $this->error('请求错误');
  904. }
  905. $param = $this->request->param();
  906. $where = [
  907. '年月' => ['like',$param['month'].'%'],
  908. '废品类别' => $param['class']
  909. ];
  910. $list = \db('rec_月度废品汇总')
  911. ->where($where)
  912. ->field('年月,客户编号,客户名称,产品名称,Gd_gdbh as 工单编号,印件号,实际投料,废品类别,sum(废品数量) as 废品数量,质检完工时间')
  913. ->group('工单编号,印件号,废品类别')
  914. ->select();
  915. foreach ($list as $key => $item) {
  916. $list[$key]['质检废品率'] = (round($item['废品数量']/$item['实际投料'], 7)*100).'%';
  917. $list[$key]['年周数'] = substr($list[$key]['质检完工时间'],0,4).'年第'.date('W',strtotime($item['质检完工时间'])).'周';
  918. }
  919. $this->success('成功', $list);
  920. }
  921. /**
  922. * 创建月度废品率系数
  923. * @return void
  924. * @throws \think\db\exception\DataNotFoundException
  925. * @throws \think\db\exception\ModelNotFoundException
  926. * @throws \think\exception\DbException
  927. */
  928. public function QualityInspectionAdd()
  929. {
  930. if (!$this->request->isGet()) {
  931. $this->error('请求错误');
  932. }
  933. $param = $this->request->param();
  934. // 1. 修复WHERE条件(原来的第二个WHERE会覆盖第一个)
  935. $where = ['jjcp_sj' => ['between', [$param['startMonth'], $param['endMonth']]]];
  936. $where = ['jjcp_smb' => ['like', '末%']];
  937. // 2. 优化字段选择,只选择必要的字段
  938. $field = [
  939. 'a.qczl_gdbh', 'a.qczl_yjno', 'a.qczl_rq',
  940. 'c.成品编码', 'c.成品名称', 'd.规格',
  941. 'b.实际投料', 'e.客户编号', 'e.客户名称',
  942. "DATE_FORMAT(c.jjcp_sj, '%Y%m') AS ym",
  943. 'c.jjcp_cpdh', 'c.jjcp_cpmc'
  944. ];
  945. // 3. 添加fp_lb和fp_sl字段
  946. for ($i = 1; $i <= 13; $i++) {
  947. $field[] = "a.fp_lb{$i}";
  948. $field[] = "a.fp_sl{$i}";
  949. }
  950. // 4. 执行查询
  951. $list = \db('db_qczl')
  952. ->alias('a')
  953. ->join('工单_基本资料 b', 'a.qczl_gdbh = b.Gd_gdbh and a.qczl_yjno = b.行号', 'LEFT')
  954. ->join('成品入仓 c', 'a.qczl_gdbh = c.jjcp_gdbh and a.qczl_yjno = c.jjcp_yjno', 'LEFT')
  955. ->join('物料_存货编码 d', 'c.jjcp_cpdh = d.物料代码', 'LEFT')
  956. ->join('产品_基本资料 e', 'c.成品编码 = e.产品编号', 'LEFT')
  957. ->field($field)
  958. ->where($where)
  959. ->select();
  960. if (empty($list)) {
  961. $this->error('没有找到符合条件的数据');
  962. }
  963. $data = [];
  964. $currentTime = date('Y-m-d H:i:s');
  965. // 5. 优化循环处理
  966. foreach ($list as $item) {
  967. // 预先处理公共数据
  968. $commonData = [
  969. '年月' => $item['ym'],
  970. '客户编号' => $item['客户编号'],
  971. '客户名称' => $item['客户名称'],
  972. '产品类别' => $item['规格'],
  973. '产品编号' => $item['成品编码'],
  974. '产品名称' => $item['成品名称'],
  975. 'Gd_gdbh' => $item['qczl_gdbh'],
  976. '印件号' => $item['qczl_yjno'],
  977. 'Gd_cpdh' => $item['jjcp_cpdh'] ?? '',
  978. 'Gd_cpmc' => $item['jjcp_cpmc'] ?? '',
  979. '实际投料' => ($item['实际投料'] ?? 0) * 10000,
  980. '质检完工时间' => $item['qczl_rq'],
  981. 'sys_id' => $param['sys_id'],
  982. 'Sys_rq' => $currentTime
  983. ];
  984. // 处理13个废品类别
  985. for ($i = 1; $i <= 13; $i++) {
  986. $lbField = "fp_lb{$i}";
  987. $slField = "fp_sl{$i}";
  988. if (!empty($item[$lbField]) && $this->isFirstCharEnglish($item[$lbField]) && $item[$slField] != 0) {
  989. $data[] = array_merge($commonData, [
  990. '废品类别' => $item[$lbField],
  991. '废品数量' => $item[$slField] ?? 0
  992. ]);
  993. }
  994. }
  995. }
  996. if (empty($data)) {
  997. $this->error('没有符合条件的数据需要插入');
  998. }
  999. // 6. 分批插入避免单次数据量过大
  1000. $chunks = array_chunk($data, 100); // 每批100条
  1001. $successCount = 0;
  1002. foreach ($chunks as $chunk) {
  1003. $result = \db('rec_月度废品汇总')->insertAll($chunk);
  1004. if ($result !== false) {
  1005. $successCount += $result;
  1006. }
  1007. }
  1008. if ($successCount > 0) {
  1009. $this->success("成功插入 {$successCount} 条数据");
  1010. } else {
  1011. $this->error('插入数据失败');
  1012. }
  1013. }
  1014. }