Decision.php 40 KB

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