Decision.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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. }
  368. }
  369. $this->success('成功', $list);
  370. }
  371. /**
  372. * 工序产出率月度统计报表
  373. * @return void
  374. * @throws \think\db\exception\DataNotFoundException
  375. * @throws \think\db\exception\ModelNotFoundException
  376. * @throws \think\exception\DbException
  377. */
  378. // public function ProcessOutputRate()
  379. // {
  380. // if (!$this->request->isGet()) {
  381. // $this->error('请求错误');
  382. // }
  383. //
  384. // $param = $this->request->param();
  385. // if (empty($param)) {
  386. // $this->error('参数错误');
  387. // }
  388. //
  389. // // 工序分类
  390. //// $processOrder = [
  391. //// 1 => ['name' => '胶印', 'keys' => ['胶印', '上光']],
  392. //// 2 => ['name' => '卷凹', 'keys' => ['卷凹']],
  393. //// 3 => ['name' => '圆烫', 'keys' => ['圆烫']],
  394. //// 4 => ['name' => '圆切', 'keys' => ['圆切']],
  395. //// 5 => ['name' => '烫模', 'keys' => ['烫金', '模切', '凹凸']],
  396. //// 6 => ['name' => '丝印', 'keys' => ['丝印']],
  397. //// 7 => ['name' => '喷码', 'keys' => ['喷码']],
  398. //// 8 => ['name' => '单凹', 'keys' => ['单凹']],
  399. //// 9 => ['name' => '覆膜', 'keys' => ['覆膜']]
  400. //// ];
  401. // $sist = ['胶印车间','凹丝印车间','印后车间','检验车间'];
  402. // $processOrder= \db('设备_基本资料')
  403. // ->whereNotNull('设备编组')
  404. // ->whereIn('使用部门',$sist)
  405. // ->group('设备编组')
  406. // ->column('rtrim(设备编组) as 设备编组');
  407. //
  408. //
  409. // // 获取存在数据的月份
  410. // $monthsWithData = \db('工单_质量考核汇总')
  411. // ->where('sys_ny', 'like', $param['year'] . '%')
  412. // ->column('distinct(sys_ny) as month');
  413. //
  414. // // 初始化结果数组,按工序名称归类
  415. // $result = [];
  416. // foreach ($processOrder as $processId => $process) {
  417. // $result[substr($process, 5, 6)] = [
  418. // '01月' => '',
  419. // '02月' => '',
  420. // '03月' => '',
  421. // '04月' => '',
  422. // '05月' => '',
  423. // '06月' => '',
  424. // '07月' => '',
  425. // '08月' => '',
  426. // '09月' => '',
  427. // '10月' => '',
  428. // '11月' => '',
  429. // '12月' => '',
  430. // '平均值' => '0.00%'
  431. // ];
  432. // }
  433. //
  434. // // 遍历每个月份
  435. // foreach ($monthsWithData as $month) {
  436. // foreach ($processOrder as $processId => $process) {
  437. // // 查询该月份该工序的合格率数据
  438. // $query = \db('工单_质量考核汇总')
  439. // ->alias('a')
  440. // ->join('工单_工艺资料 b', 'a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  441. // ->join('工单_印件资料 c', 'a.Gy0_gdbh = c.Yj_Gdbh AND a.印件及工序 = c.yj_Yjno')
  442. // ->join('成品入仓 d', 'a.Gy0_gdbh = d.jjcp_gdbh AND a.印件及工序 = d.jjcp_yjno')
  443. // ->join('设备_基本资料 e','a.sczl_jtbh = e.设备编号')
  444. //// ->field('a.sys_ny,(1 - ((SUM(a.班组制程废品) + SUM(a.班组质检废品)) / SUM(a.班组产量))) * 100 as 合格率')
  445. // ->field('a.sys_ny,SUM(a.班组制程废品)/a.联数 as 制程废品,SUM(a.班组质检废品)/a.联数 as 质检废品,SUM(a.班组产量)/a.联数 as 产量')
  446. // ->where('e.设备编组',$process)
  447. // ->where('d.jjcp_smb', 'like', '末%')
  448. // ->where('a.sys_ny', $month)
  449. // ->group('a.sys_ny,a.Gy0_gdbh,a.印件及工序,a.工序');
  450. //// ->group('a.sys_ny');
  451. //
  452. // $res = $query->select();
  453. // $data = ['sys_ny'=>'','制程废品'=>0, '质检废品'=>0, '产量'=>0];
  454. // foreach ($res as $value) {
  455. // $data['sys_ny'] = $value['sys_ny'];
  456. // $data['制程废品'] += $value['制程废品'];
  457. // $data['质检废品'] += $value['质检废品'];
  458. // $data['产量'] += $value['产量'];
  459. // }
  460. // $records = [
  461. // 'sys_ny' => $data['sys_ny'],
  462. // '合格率' => (1-(($data['制程废品']+$data['质检废品'])/$data['产量']))* 100
  463. // ];
  464. //// halt($records);
  465. // if (!empty($records)) {
  466. // $monthName = substr($month, 4, 2) . '月';
  467. // $result[substr($process, 5, 6)][$monthName] = sprintf("%.2f%%", $records['合格率']);
  468. // }
  469. // }
  470. // }
  471. //
  472. // // 计算年度平均值
  473. // foreach ($result as $processName => &$processData) {
  474. // $totalAnnual = 0;
  475. // $monthCount = 0;
  476. //
  477. // foreach ($processData as $month => $rate) {
  478. // if ($month !== '平均值' && $rate !== '') {
  479. // $totalAnnual += floatval(str_replace('%', '', $rate));
  480. // $monthCount++;
  481. // }
  482. // }
  483. //
  484. // if ($monthCount > 0) {
  485. // $processData['平均值'] = sprintf("%.2f%%", $totalAnnual / $monthCount);
  486. // }
  487. // }
  488. // unset($processData); // 关键修复:解除引用
  489. //
  490. // // 转换为目标格式
  491. // $formattedData = [];
  492. // foreach ($result as $processName => $processData) {
  493. // // 现在processData是独立值
  494. // $formattedRow = array_merge(['工序类型' => $processName], $processData);
  495. // $formattedData[] = $formattedRow;
  496. // }
  497. // // 返回JSON响应
  498. // $this->success('成功',$formattedData);
  499. // }
  500. public function ProcessOutputRate()
  501. {
  502. if (!$this->request->isGet()) {
  503. $this->error('请求错误');
  504. }
  505. $param = $this->request->param();
  506. if (empty($param) || empty($param['year'])) {
  507. $this->error('参数错误');
  508. }
  509. // 1. 获取设备分组,作为工序依据
  510. $sist = ['胶印车间','凹丝印车间','印后车间','检验车间'];
  511. $processOrder = \db('设备_基本资料')
  512. ->whereNotNull('设备编组')
  513. ->whereIn('使用部门', $sist)
  514. ->group('设备编组')
  515. ->column('RTRIM(设备编组) as 设备编组');
  516. if (empty($processOrder)) {
  517. $this->success('成功', []); // 没有设备直接返回
  518. }
  519. // 2. 初始化结果数组(按工序)
  520. $result = [];
  521. foreach ($processOrder as $process) {
  522. $key = substr($process, 5, 6);
  523. $result[$key] = [];
  524. for ($i = 1; $i <= 12; $i++) {
  525. $result[$key][str_pad($i, 2, '0', STR_PAD_LEFT) . '月'] = '';
  526. }
  527. $result[$key]['平均值'] = '0.00%';
  528. }
  529. // 3. 汇总所有年份内数据,一次查出所有“工序-月份”数据,避免嵌套多次SQL
  530. $dataList = \db('工单_质量考核汇总')
  531. ->alias('a')
  532. ->join('工单_工艺资料 b', 'a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  533. ->join('工单_印件资料 c', 'a.Gy0_gdbh = c.Yj_Gdbh AND a.印件及工序 = c.yj_Yjno')
  534. ->join('成品入仓 d', 'a.Gy0_gdbh = d.jjcp_gdbh AND a.印件及工序 = d.jjcp_yjno')
  535. ->join('设备_基本资料 e', 'a.sczl_jtbh = e.设备编号')
  536. ->whereIn('e.设备编组', $processOrder)
  537. ->where('d.jjcp_smb', 'like', '末%')
  538. ->where('a.sys_ny', 'like', $param['year'] . '%')
  539. ->field([
  540. 'e.设备编组',
  541. 'a.sys_ny',
  542. 'SUM(a.班组制程废品/ a.联数) AS 制程废品',
  543. 'SUM(a.班组质检废品/ a.联数) AS 质检废品',
  544. 'SUM(a.班组产量/ a.联数) AS 产量'
  545. ])
  546. ->group('e.设备编组, a.sys_ny')
  547. ->select();
  548. // 4. 处理统计数据,填充到$result
  549. $annual = []; // 年均合格率累加用
  550. foreach ($dataList as $item) {
  551. $processKey = substr($item['设备编组'], 5, 6);
  552. $monthKey = substr($item['sys_ny'], 4, 2) . '月';
  553. $output = floatval($item['产量']);
  554. $waste = floatval($item['制程废品']) + floatval($item['质检废品']);
  555. $rate = $output > 0 ? (1 - $waste / $output) * 100 : 0;
  556. $result[$processKey][$monthKey] = sprintf('%.2f%%', $rate);
  557. if (!isset($annual[$processKey])) {
  558. $annual[$processKey] = ['sum' => 0, 'count' => 0];
  559. }
  560. $annual[$processKey]['sum'] += $rate;
  561. $annual[$processKey]['count']++;
  562. }
  563. // 5. 补充“平均值”
  564. foreach ($annual as $processKey => $v) {
  565. if ($v['count'] > 0) {
  566. $result[$processKey]['平均值'] = sprintf('%.2f%%', $v['sum']/$v['count']);
  567. }
  568. }
  569. // 6. 转换为前端需要的格式
  570. $formattedData = [];
  571. foreach ($result as $process => $data) {
  572. $row = array_merge(['工序类型' => $process], $data);
  573. $formattedData[] = $row;
  574. }
  575. // 7. 返回
  576. $this->success('成功', $formattedData);
  577. }
  578. /**
  579. * 获取年分数据
  580. * @return void
  581. */
  582. public function GetYear()
  583. {
  584. if ($this->request->isGet() === false){
  585. $this->error('请求错误');
  586. }
  587. $data = \db('工单_质量考核汇总')
  588. ->group('year')
  589. ->column('YEAR(STR_TO_DATE(sys_ny, "%Y%m")) as year');
  590. $this->success('成功',$data);
  591. }
  592. /**
  593. * 数据透视表
  594. * @return null
  595. * @throws \think\Exception
  596. * @throws \think\db\exception\DataNotFoundException
  597. * @throws \think\db\exception\ModelNotFoundException
  598. * @throws \think\exception\DbException
  599. */
  600. public function PoductData()
  601. {
  602. if (!$this->request->isGet()) {
  603. $this->error('请求错误');
  604. }
  605. $param = $this->request->param();
  606. if (empty($param['year'])) {
  607. $this->error('参数错误');
  608. }
  609. // 获取设备编组列表(已按编号排序)
  610. $processGroups = \db('设备_基本资料')
  611. ->whereNotNull('设备编组')
  612. ->group('设备编组')
  613. ->column('rtrim(设备编组) as 设备编组');
  614. // 构建基础查询
  615. $query = \db('工单_质量考核汇总')
  616. ->alias('a')
  617. ->field('
  618. a.sczl_jtbh AS 机台编号,
  619. a.Gy0_gdbh AS 工单编号,
  620. a.印件及工序 AS 工序号,
  621. a.产品名称 AS 印件名称,
  622. a.工序 AS 工序,
  623. a.工序名称,
  624. b.Gy0_gxmc,
  625. a.联数,
  626. a.班组产量,
  627. a.班组制程废品,
  628. a.班组质检废品,
  629. a.sczl_bzdh AS 班组编号,
  630. b.印刷方式,
  631. b.版距,
  632. DATE_FORMAT(a.入仓日期, \'%Y%m\') AS 完工年月,
  633. d.设备编组
  634. ')
  635. ->join('工单_工艺资料 b', 'a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  636. ->join('成品入仓 c', 'a.Gy0_gdbh = c.jjcp_gdbh AND a.印件及工序 = c.jjcp_yjno')
  637. ->join('设备_基本资料 d', 'a.sczl_jtbh = d.设备编号')
  638. ->where('a.sys_ny', 'like', $param['year'] . '%')
  639. ->where('a.工序名称', 'not like', '%机检%')
  640. ->where('c.jjcp_smb', 'like', '末%')
  641. ->where('a.工序名称', 'not like', '%拆片%');
  642. // 获取总数
  643. $total = clone $query;
  644. $total = $total->count();
  645. // 执行查询
  646. $rawList = isset($param['page'], $param['limit'])
  647. ? $query->page($param['page'])->limit($param['limit'])->select()
  648. : $query->select();
  649. // 构建分类映射表
  650. $groupMap = [];
  651. foreach ($processGroups as $index => $group) {
  652. // 提取机组名称核心词(去除编号和"机组"字样)
  653. $groupName = preg_replace('/^\d+、/', '', $group); // 移除开头编号
  654. $coreName = str_replace('机组', '', $groupName); // 移除尾部的"机组"
  655. $groupMap[] = [
  656. 'id' => $index + 1,
  657. 'name' => $groupName,
  658. 'core' => $coreName
  659. ];
  660. }
  661. // 处理分类标记
  662. $processedList = [];
  663. foreach ($rawList as $item) {
  664. foreach ($groupMap as $group) {
  665. // 使用核心词进行模糊匹配
  666. if (strpos($item['设备编组'], $group['core']) !== false) {
  667. $item['type_id'] = $group['id'];
  668. $item['type_name'] = $group['name'];
  669. $processedList[] = $item;
  670. continue 2;
  671. }
  672. }
  673. }
  674. // 按分类ID排序
  675. usort($processedList, function($a, $b) {
  676. return $a['type_id'] <=> $b['type_id'];
  677. });
  678. // 返回结构
  679. $data['data'] = $processedList;
  680. $data['total'] = $total;
  681. $data['filtered_total'] = count($processedList);
  682. return count($processedList) > 0
  683. ? $this->success('成功', $data)
  684. : $this->error('未找到数据');
  685. }
  686. /**
  687. * 月度色度数导出
  688. * @return void
  689. * @throws \think\db\exception\DataNotFoundException
  690. * @throws \think\db\exception\ModelNotFoundException
  691. * @throws \think\exception\DbException
  692. */
  693. public function ChromaticityNumber()
  694. {
  695. if (!$this->request->isGet()) {
  696. $this->error('请求错误');
  697. }
  698. $param = $this->request->param();
  699. if (empty($param['month'])) {
  700. $this->error('参数错误');
  701. }
  702. $month = date_create_from_format('Ym', $param['month'])->format('Y-m');
  703. $where = ['a.sczl_rq' => ['like', $month . '%']];
  704. $list = \db('设备_产量计酬')
  705. ->alias('a')
  706. ->join('工单_印件资料 c', 'a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno')
  707. ->join('工单_工艺资料 d', 'a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh')
  708. ->join('设备_基本资料 e', 'a.sczl_jtbh = e.设备编号')
  709. ->field([
  710. 'a.sczl_gdbh' => '工单编号',
  711. 'a.sczl_yjno' => '印件号',
  712. 'a.sczl_gxh' => '工序号',
  713. 'd.Gy0_gxmc' => '工序名称',
  714. 'SUM(a.sczl_cl)' => '产量',
  715. 'a.sczl_ms' => '墨色数',
  716. 'rtrim(d.印刷方式)' => '印刷方式',
  717. 'rtrim(d.版距)' => '版距',
  718. 'rtrim(d.Gy0_SITE)' => '车间名称',
  719. 'rtrim(e.设备编组)' => '设备编组',
  720. 'a.sczl_jtbh'
  721. ])
  722. ->where($where)
  723. ->group('a.sczl_gdbh, a.sczl_yjno, a.sczl_gxh, a.sczl_rq, a.sczl_jtbh, a.sczl_bzdh')
  724. ->select();
  725. $data = [];
  726. if (!empty($list)) {
  727. foreach ($list as $key => $value) {
  728. // Convert ALL outputs to integers
  729. $value['产量'] = (int)round($value['产量']);
  730. if ($value['印刷方式'] === '卷对卷' && $value['版距'] != 0) {
  731. $value['产量'] = (int)round($value['产量'] / $value['版距'] * 1000);
  732. }
  733. if($value['墨色数'] == 0 && $value['设备编组'] !== '11、检品机组'){
  734. $value['墨色数'] = 1;
  735. }
  736. if($value['设备编组'] === '11、检品机组'){
  737. $value['墨色数'] = 0;
  738. }
  739. $data[$key] = [
  740. 'gdbh' => $value['工单编号'],
  741. 'ms' => $value['墨色数'],
  742. '产量' => $value['产量'],
  743. '墨色' => $value['产量'] * $value['墨色数'],
  744. '设备编组' => $value['设备编组']
  745. ];
  746. }
  747. }
  748. $grouped = [];
  749. foreach ($data as $item) {
  750. $key = substr($item['设备编组'], 5, 6);
  751. if (!isset($grouped[$key])) {
  752. $grouped[$key] = [
  753. '产量合计' => 0,
  754. '工序名称' => $key,
  755. '色度数' => 0
  756. ];
  757. }
  758. $grouped[$key]['产量合计'] += (int)$item['产量'];
  759. $grouped[$key]['色度数'] += (int)$item['墨色'];
  760. }
  761. foreach ($grouped as &$group) {
  762. $group['产量合计'] = (int)$group['产量合计'];
  763. $group['色度数'] = (int)$group['色度数'];
  764. }
  765. $this->success('成功', array_values($grouped));
  766. }
  767. /**
  768. * 年度质检废品率统计左侧菜单
  769. * @return void
  770. * @throws \think\db\exception\DataNotFoundException
  771. * @throws \think\db\exception\ModelNotFoundException
  772. * @throws \think\exception\DbException
  773. */
  774. public function QualityInspectionGetTab()
  775. {
  776. if (!$this->request->isGet()) {
  777. $this->error('请求错误');
  778. }
  779. $monthStats = \db('db_qczl')
  780. ->group('month')
  781. ->order('month desc')
  782. ->column("DATE_FORMAT(qczl_rq, '%Y%m') AS month");
  783. // 按年月层次结构归类(返回树形结构)
  784. $treeData = \db('db_qczl')
  785. ->field([
  786. "DATE_FORMAT(qczl_rq, '%Y') AS year",
  787. "DATE_FORMAT(qczl_rq, '%m') AS month",
  788. "COUNT(*) AS total"
  789. ])
  790. ->order('year desc,month desc')
  791. ->group('year, month')
  792. ->select();
  793. // 构建树形结构
  794. $result = [];
  795. foreach ($treeData as $item) {
  796. $year = $item['year'];
  797. $month = $year . $item['month'];
  798. if (!isset($result[$year])) {
  799. $result[$year] = [
  800. 'year' => $year,
  801. 'children' => []
  802. ];
  803. }
  804. $result[$year]['children'][] = [
  805. 'month' => $month,
  806. 'total' => $item['total']
  807. ];
  808. }
  809. $this->success('成功', $result);
  810. }
  811. /**
  812. * 年度质检废品统计右侧上方列表
  813. * @return void
  814. * @throws \think\db\exception\DataNotFoundException
  815. * @throws \think\db\exception\ModelNotFoundException
  816. * @throws \think\exception\DbException
  817. */
  818. public function QualityInspectionList()
  819. {
  820. if (!$this->request->isGet()) {
  821. $this->error('请求错误');
  822. }
  823. $param = $this->request->param();
  824. $where = ['年月' => ['like', $param['month'] . '%']];
  825. // 查询工单列表
  826. $workList = \db('rec_月度废品汇总')
  827. ->where($where)
  828. ->field("CONCAT(Gd_gdbh,'-',印件号) as 工单,实际投料")
  829. ->group('工单')
  830. ->select();
  831. $work = [];
  832. foreach ($workList as $item) {
  833. $work[$item['工单']] = $item['实际投料'];
  834. }
  835. // 查询工单对应的废品类别首字母(按工单和首字母分组)
  836. $WasteWorkList = \db('rec_月度废品汇总')
  837. ->where($where)
  838. ->field("CONCAT(Gd_gdbh,'-',印件号) as 工单, left(废品类别,1) as 废品类别首字母")
  839. ->group('工单, 废品类别首字母')
  840. ->order('废品类别首字母 asc')
  841. ->select();
  842. $waste = [];
  843. foreach ($WasteWorkList as $item) {
  844. if (!isset($waste[$item['废品类别首字母']])) {
  845. $waste[$item['废品类别首字母']] = [];
  846. }
  847. $waste[$item['废品类别首字母']][] = $item['工单'];
  848. }
  849. // 计算每个首字母的实际投料总和
  850. $res = [];
  851. foreach ($waste as $key => $item) {
  852. $res[$key] = array_sum(array_intersect_key($work, array_flip($item)));
  853. }
  854. // 查询废品类别对应的废品数量
  855. $wasteList = \db('rec_月度废品汇总')
  856. ->where($where)
  857. ->group('废品类别')
  858. ->order('废品类别 asc')
  859. ->column("sum(废品数量) as 废品数量", "废品类别");
  860. // 从废品类别数据计算每个首字母的废品数量总和
  861. $classification = [];
  862. foreach ($wasteList as $category => $amount) {
  863. $firstChar = substr($category, 0, 1);
  864. if (!isset($classification[$firstChar])) {
  865. $classification[$firstChar] = 0;
  866. }
  867. $classification[$firstChar] += $amount;
  868. }
  869. // 构建数据数组
  870. $data = [];
  871. foreach ($wasteList as $category => $amount) {
  872. $firstChar = substr($category, 0, 1);
  873. $number = $res[$firstChar] ?? 0;
  874. $data[] = [
  875. '废品类别' => $category,
  876. '实际投料' => $number,
  877. '废品数量' => $amount,
  878. '质检废品率' => (round($amount/$number, 7) * 100) . '%',
  879. ];
  880. }
  881. // 添加首字母合计行
  882. foreach ($classification as $firstChar => $amount) {
  883. $number = $res[$firstChar] ?? 0;
  884. $data[] = [
  885. '废品类别' => $firstChar . '-合计',
  886. '实际投料' => $number,
  887. '废品数量' => $amount,
  888. '质检废品率' => (round($amount/$number, 7) * 100) . '%',
  889. ];
  890. }
  891. $this->success('成功', $data);
  892. }
  893. //判断字符串首位是否为英文字母
  894. function isFirstCharEnglish($str) {
  895. if (mb_strlen($str) == 0) return false;
  896. $firstChar = mb_substr($str, 0, 1);
  897. return preg_match('/^[A-Za-z]$/u', $firstChar);
  898. }
  899. /**
  900. * 年度质检废品率统计下方列表
  901. * @return void
  902. * @throws \think\db\exception\DataNotFoundException
  903. * @throws \think\db\exception\ModelNotFoundException
  904. * @throws \think\exception\DbException
  905. */
  906. public function QualityInspectionDetailList()
  907. {
  908. if ($this->request->isGet() === false) {
  909. $this->error('请求错误');
  910. }
  911. $param = $this->request->param();
  912. $where = [
  913. '年月' => ['like',$param['month'].'%'],
  914. '废品类别' => $param['class']
  915. ];
  916. $list = \db('rec_月度废品汇总')
  917. ->where($where)
  918. ->field('年月,客户编号,客户名称,产品名称,Gd_gdbh as 工单编号,印件号,实际投料,废品类别,sum(废品数量) as 废品数量,质检完工时间')
  919. ->group('工单编号,印件号,废品类别')
  920. ->select();
  921. foreach ($list as $key => $item) {
  922. $list[$key]['质检废品率'] = (round($item['废品数量']/$item['实际投料'], 7)*100).'%';
  923. $list[$key]['年周数'] = substr($list[$key]['质检完工时间'],0,4).'年第'.date('W',strtotime($item['质检完工时间'])).'周';
  924. }
  925. $this->success('成功', $list);
  926. }
  927. /**
  928. * 创建月度废品率系数
  929. * @return void
  930. * @throws \think\db\exception\DataNotFoundException
  931. * @throws \think\db\exception\ModelNotFoundException
  932. * @throws \think\exception\DbException
  933. */
  934. public function QualityInspectionAdd()
  935. {
  936. if (!$this->request->isGet()) {
  937. $this->error('请求错误');
  938. }
  939. $param = $this->request->param();
  940. // 1. 修复WHERE条件(原来的第二个WHERE会覆盖第一个)
  941. $where = ['jjcp_sj' => ['between', [$param['startMonth'], $param['endMonth']]]];
  942. $where = ['jjcp_smb' => ['like', '末%']];
  943. // 2. 优化字段选择,只选择必要的字段
  944. $field = [
  945. 'a.qczl_gdbh', 'a.qczl_yjno', 'a.qczl_rq',
  946. 'c.成品编码', 'c.成品名称', 'd.规格',
  947. 'b.实际投料', 'e.客户编号', 'e.客户名称',
  948. "DATE_FORMAT(c.jjcp_sj, '%Y%m') AS ym",
  949. 'c.jjcp_cpdh', 'c.jjcp_cpmc'
  950. ];
  951. // 3. 添加fp_lb和fp_sl字段
  952. for ($i = 1; $i <= 13; $i++) {
  953. $field[] = "a.fp_lb{$i}";
  954. $field[] = "a.fp_sl{$i}";
  955. }
  956. // 4. 执行查询
  957. $list = \db('db_qczl')
  958. ->alias('a')
  959. ->join('工单_基本资料 b', 'a.qczl_gdbh = b.Gd_gdbh and a.qczl_yjno = b.行号', 'LEFT')
  960. ->join('成品入仓 c', 'a.qczl_gdbh = c.jjcp_gdbh and a.qczl_yjno = c.jjcp_yjno', 'LEFT')
  961. ->join('物料_存货编码 d', 'c.jjcp_cpdh = d.物料代码', 'LEFT')
  962. ->join('产品_基本资料 e', 'c.成品编码 = e.产品编号', 'LEFT')
  963. ->field($field)
  964. ->where($where)
  965. ->select();
  966. if (empty($list)) {
  967. $this->error('没有找到符合条件的数据');
  968. }
  969. $data = [];
  970. $currentTime = date('Y-m-d H:i:s');
  971. // 5. 优化循环处理
  972. foreach ($list as $item) {
  973. // 预先处理公共数据
  974. $commonData = [
  975. '年月' => $item['ym'],
  976. '客户编号' => $item['客户编号'],
  977. '客户名称' => $item['客户名称'],
  978. '产品类别' => $item['规格'],
  979. '产品编号' => $item['成品编码'],
  980. '产品名称' => $item['成品名称'],
  981. 'Gd_gdbh' => $item['qczl_gdbh'],
  982. '印件号' => $item['qczl_yjno'],
  983. 'Gd_cpdh' => $item['jjcp_cpdh'] ?? '',
  984. 'Gd_cpmc' => $item['jjcp_cpmc'] ?? '',
  985. '实际投料' => ($item['实际投料'] ?? 0) * 10000,
  986. '质检完工时间' => $item['qczl_rq'],
  987. 'sys_id' => $param['sys_id'],
  988. 'Sys_rq' => $currentTime
  989. ];
  990. // 处理13个废品类别
  991. for ($i = 1; $i <= 13; $i++) {
  992. $lbField = "fp_lb{$i}";
  993. $slField = "fp_sl{$i}";
  994. if (!empty($item[$lbField]) && $this->isFirstCharEnglish($item[$lbField]) && $item[$slField] != 0) {
  995. $data[] = array_merge($commonData, [
  996. '废品类别' => $item[$lbField],
  997. '废品数量' => $item[$slField] ?? 0
  998. ]);
  999. }
  1000. }
  1001. }
  1002. if (empty($data)) {
  1003. $this->error('没有符合条件的数据需要插入');
  1004. }
  1005. // 6. 分批插入避免单次数据量过大
  1006. $chunks = array_chunk($data, 100); // 每批100条
  1007. $successCount = 0;
  1008. foreach ($chunks as $chunk) {
  1009. $result = \db('rec_月度废品汇总')->insertAll($chunk);
  1010. if ($result !== false) {
  1011. $successCount += $result;
  1012. }
  1013. }
  1014. if ($successCount > 0) {
  1015. $this->success("成功插入 {$successCount} 条数据");
  1016. } else {
  1017. $this->error('插入数据失败');
  1018. }
  1019. }
  1020. }