Decision.php 43 KB

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