Decision.php 44 KB

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