Decision.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Request;
  6. /**
  7. * 设备运行跟踪
  8. */
  9. class Decision extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. //月度产量统计菜单
  14. public function OutputSstatisticsMenu()
  15. {
  16. if ($this->request->isGet() === false){
  17. $this->error('请求错误');
  18. }
  19. $mouth = \db('设备_产量计酬')
  20. ->distinct(true)
  21. ->field('DATE_FORMAT(sczl_rq, "%Y-%m") AS month')
  22. ->order('month desc')
  23. ->select();
  24. $sist = \db('设备_基本资料')
  25. ->whereNotNull('设备编组')
  26. ->group('设备编组')
  27. ->column('rtrim(设备编组) as 设备编组');
  28. $data = [];
  29. foreach ($mouth as $key=>$value) {
  30. $arr = [
  31. 'date'=>date('Ym',strtotime($value['month'])),
  32. 'sbbh'=>$sist,
  33. ];
  34. array_push($data,$arr);
  35. }
  36. $this->success('成功',$data);
  37. }
  38. /**
  39. * 月度产量统计上方机台生产数据
  40. * @return void
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. * @throws \think\exception\DbException
  44. */
  45. public function MachineProduction()
  46. {
  47. // 验证请求方式
  48. if ($this->request->isGet() === false) {
  49. $this->error('请求错误');
  50. }
  51. // 获取请求参数
  52. $param = $this->request->param();
  53. if (empty($param['mouth'])) {
  54. $this->error('参数错误');
  55. }
  56. // 初始化查询条件
  57. $where = [];
  58. if (!empty($param['sist'])) {
  59. $where['b.设备编组'] = $param['sist'];
  60. }
  61. // 转换日期格式
  62. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  63. // 使用单个查询获取所有需要的数据
  64. $results = \db('设备_产量计酬')
  65. ->alias('a')
  66. ->join('设备_基本资料 b', 'a.sczl_jtbh = b.设备编号')
  67. ->join('工单_印件资料 c', 'a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno', 'LEFT')
  68. ->join('工单_工艺资料 d', 'a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh', 'LEFT')
  69. ->field([
  70. 'a.sczl_jtbh',
  71. 'a.sczl_bzdh',
  72. 'DATE_FORMAT(a.sczl_rq, "%Y-%m-%d") as day',
  73. 'SUM(a.sczl_cl) AS total_cl',
  74. 'c.yj_yjmc AS 印件名称',
  75. 'CONCAT(d.Gy0_gxmc,"(",d.Add_gxmc,")") AS 工序名称',
  76. 'RTRIM(d.印刷方式) AS 印刷方式',
  77. 'RTRIM(d.版距) AS 版距'
  78. ])
  79. ->where('a.sczl_rq', 'like', $mouth . '%')
  80. ->where('b.sys_sbID', '<>', '')
  81. ->where($where)
  82. ->group('a.sczl_jtbh, a.sczl_bzdh, day, c.yj_yjmc, d.Gy0_gxmc')
  83. ->order('a.sczl_jtbh, a.sczl_bzdh, day')
  84. ->select();
  85. // 数据处理
  86. $data = [];
  87. // 获取唯一日期并排序
  88. $data['head'] = array_unique(array_column($results, 'day'));
  89. // 将日期转为时间戳以排序
  90. usort($data['head'], function($a, $b) {
  91. return strtotime($a) - strtotime($b);
  92. });
  93. // 准备按设备和班组整理的数据
  94. $total_by_machine = [];
  95. foreach ($results as $item) {
  96. $machine_id = $item['sczl_jtbh'];
  97. $group_id = $item['sczl_bzdh'];
  98. $day = $item['day'];
  99. $printed_type = $item['印刷方式'];
  100. // 初始化设备和班组
  101. if (!isset($total_by_machine[$machine_id])) {
  102. $total_by_machine[$machine_id] = [];
  103. }
  104. if (!isset($total_by_machine[$machine_id][$group_id])) {
  105. $total_by_machine[$machine_id][$group_id] = [
  106. '机台编号' => $machine_id,
  107. '班组编号' => $group_id,
  108. '印件名称' => $item['印件名称'],
  109. '工序名称' => $item['工序名称'],
  110. 'total_cl' => 0,
  111. 'day_total' => []
  112. ];
  113. }
  114. // 更新累计数量
  115. if ($printed_type === '卷对卷') {
  116. $total_by_machine[$machine_id][$group_id]['total_cl'] += round($item['total_cl'] / $item['版距'] * 1000);
  117. } else {
  118. $total_by_machine[$machine_id][$group_id]['total_cl'] += $item['total_cl'];
  119. }
  120. // 更新每日总计
  121. if (!isset($total_by_machine[$machine_id][$group_id]['day_total'][$day])) {
  122. $total_by_machine[$machine_id][$group_id]['day_total'][$day] = 0;
  123. }
  124. if ($printed_type === '卷对卷') {
  125. $total_by_machine[$machine_id][$group_id]['day_total'][$day] += round($item['total_cl'] / $item['版距'] * 1000);
  126. } else {
  127. $total_by_machine[$machine_id][$group_id]['day_total'][$day] += $item['total_cl'];
  128. }
  129. }
  130. // 格式化输出数据
  131. $data['total'] = [];
  132. foreach ($total_by_machine as $machine_group) {
  133. foreach ($machine_group as $group_data) {
  134. $data['total'][] = $group_data;
  135. }
  136. }
  137. $this->success('成功', $data);
  138. }
  139. /**
  140. * 机台班次生产工单明细
  141. * @return void
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. * @throws \think\exception\DbException
  145. */
  146. public function MachineProductDetail()
  147. {
  148. if (!$this->request->isGet()) {
  149. $this->error('请求错误');
  150. }
  151. $param = $this->request->param();
  152. // 检查必需的参数
  153. if (empty($param['mouth'])) {
  154. $this->error('参数错误');
  155. }
  156. // Initializing where conditions
  157. $where = [];
  158. // 添加可选的查询条件
  159. if (!empty($param['machine'])) {
  160. $where['a.sczl_jtbh'] = $param['machine'];
  161. }
  162. if (!empty($param['team'])) {
  163. $where['a.sczl_bzdh'] = ['like', substr($param['team'], 0, 1) . '%'];
  164. }
  165. // 将参数转换成标准日期格式
  166. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  167. $where['a.sczl_rq'] = ['like', $mouth . '%'];
  168. // 分页配置
  169. $page = !empty($param['page']) ? (int)$param['page'] : 1;
  170. $limit = !empty($param['limit']) ? (int)$param['limit'] : 9999; // 默认查询所有
  171. // 查询数据
  172. $list = \db('设备_产量计酬')
  173. ->alias('a')
  174. ->join('工单_印件资料 c', 'a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno')
  175. ->join('工单_工艺资料 d', 'a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh')
  176. ->field([
  177. 'a.sczl_gdbh' => '工单编号',
  178. 'a.sczl_yjno' => '印件号',
  179. 'a.sczl_gxh' => '工序号',
  180. 'c.yj_yjmc' => '印件名称',
  181. 'CONCAT(d.Gy0_gxmc,"(",d.Add_gxmc,")")' => '工序名称',
  182. 'DATE(a.sczl_rq)' => '工作日期',
  183. 'a.sczl_jtbh' => '机台编号',
  184. 'a.sczl_bzdh' => '班组编号',
  185. 'SUM(a.sczl_cl)' => '产量',
  186. 'a.sczl_ms' => '墨色数',
  187. 'rtrim(d.印刷方式)' => '印刷方式',
  188. 'rtrim(d.版距)' => '版距'
  189. ])
  190. ->where($where)
  191. ->group('a.sczl_gdbh, a.sczl_yjno, a.sczl_gxh, a.sczl_rq, a.sczl_jtbh, a.sczl_bzdh')
  192. ->order('工作日期')
  193. ->page($page, $limit) // 使用 page 和 limit 实现分页
  194. ->select();
  195. // 数据处理
  196. if (!empty($list)) {
  197. foreach ($list as $key => $value) {
  198. $list[$key]['印件名称'] = $value['印件号'] . '-' . $value['印件名称'];
  199. $list[$key]['工序名称'] = $value['工序号'] . '-' . $value['工序名称'];
  200. unset($list[$key]['印件号'], $list[$key]['工序号']);
  201. // 产量计算
  202. if ($value['印刷方式'] === '卷对卷') {
  203. $list[$key]['产量'] = round($value['产量'] / $value['版距'] * 1000);
  204. }
  205. // 墨色数修正
  206. if ($value['墨色数'] === '0.00') {
  207. $list[$key]['墨色数'] = '1.00';
  208. }
  209. }
  210. }
  211. $this->success('成功', $list);
  212. }
  213. /**
  214. * 月度机台运行工时汇总
  215. * @return void
  216. * @throws \think\db\exception\DataNotFoundException
  217. * @throws \think\db\exception\ModelNotFoundException
  218. * @throws \think\exception\DbException
  219. */
  220. public function MachineOperation()
  221. {
  222. // 确保请求是GET
  223. if ($this->request->isGet() === false) {
  224. $this->error('请求错误');
  225. }
  226. // 获取请求参数
  227. $param = $this->request->param();
  228. if (empty($param['mouth'])) {
  229. $this->error('参数错误');
  230. }
  231. // 构建查询条件
  232. $where = [];
  233. if (!empty($param['sist'])) {
  234. $where['a.设备编组'] = $param['sist'];
  235. }
  236. // 将参数转换成标准日期格式
  237. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  238. // 单次查询,计算产量和各项工时
  239. $list = \db('设备_基本资料')
  240. ->alias('a')
  241. ->join('设备_产量计酬 b', 'a.设备编号 = b.sczl_jtbh')
  242. ->join('工单_印件资料 c', 'b.sczl_gdbh = c.Yj_Gdbh AND b.sczl_yjno = c.yj_Yjno')
  243. ->join('工单_工艺资料 d', 'b.sczl_gdbh = d.Gy0_gdbh AND b.sczl_yjno = d.Gy0_yjno AND b.sczl_gxh = d.Gy0_gxh')
  244. ->field([
  245. 'a.设备编号' => '设备编号',
  246. 'rtrim(a.设备名称)' => '设备名称',
  247. 'SUM(CASE WHEN rtrim(d.版距) = "卷对卷" THEN b.sczl_cl / NULLIF(d.版距, 0) * 1000 ELSE b.sczl_cl END) AS 产量',
  248. 'SUM(b.sczl_设备运行工时)' => '设备运行工时',
  249. 'SUM(b.sczl_保养工时)' => '保养工时',
  250. 'SUM(b.sczl_打样总工时)' => '打样总工时',
  251. 'SUM(b.sczl_打样工时)' => '打样补产工时',
  252. 'SUM(b.sczl_装版总工时)' => '装版总工时',
  253. 'SUM(b.sczl_装版工时)' => '装版补产工时',
  254. 'SUM(b.sczl_异常停机工时)' => '异常停机工时',
  255. 'rtrim(d.印刷方式)' => '印刷方式',
  256. 'rtrim(d.版距)' => '版距'
  257. ])
  258. ->where($where)
  259. ->where('b.sczl_rq', 'like', $mouth . '%')
  260. ->group('a.设备编号')
  261. ->order('a.设备编号')
  262. ->select();
  263. // 处理总工时的计算
  264. $total = [
  265. '设备运行工时' => 0,
  266. '保养工时' => 0,
  267. '打样总工时' => 0,
  268. '打样补产工时' => 0,
  269. '装版总工时' => 0,
  270. '装版补产工时' => 0,
  271. '异常停机工时' => 0
  272. ];
  273. // 计算各项总工时
  274. foreach ($list as $item) {
  275. $total['设备运行工时'] += $item['设备运行工时'];
  276. $total['保养工时'] += $item['保养工时'];
  277. $total['打样总工时'] += $item['打样总工时'];
  278. $total['打样补产工时'] += $item['打样补产工时'];
  279. $total['装版总工时'] += $item['装版总工时'];
  280. $total['装版补产工时'] += $item['装版补产工时'];
  281. $total['异常停机工时'] += $item['异常停机工时'];
  282. }
  283. // 将总工时添加到列表中
  284. $list['total'] = $total;
  285. // 返回成功响应
  286. $this->success('成功', $list);
  287. }
  288. /**
  289. * 设备运行工时机台生产工单数据详情
  290. * @return void
  291. * @throws \think\db\exception\DataNotFoundException
  292. * @throws \think\db\exception\ModelNotFoundException
  293. * @throws \think\exception\DbException
  294. */
  295. public function MachineOperationProductDetail()
  296. {
  297. if (!$this->request->isGet()) {
  298. $this->error('请求错误');
  299. }
  300. $param = $this->request->param();
  301. if (empty($param['mouth'])) {
  302. $this->error('参数错误');
  303. }
  304. // Initialize where conditions
  305. $where = [];
  306. if (!empty($param['machine'])) {
  307. $where['a.sczl_jtbh'] = $param['machine'];
  308. }
  309. // 将参数转换成标准日期格式
  310. $mouth = date_create_from_format('Ym', $param['mouth'])->format('Y-m');
  311. $where['a.sczl_rq'] = ['like', $mouth . '%'];
  312. // 分页配置
  313. $page = !empty($param['page']) ? (int)$param['page'] : 1;
  314. $limit = !empty($param['limit']) ? (int)$param['limit'] : 9999; // 默认查询所有
  315. // 查询数据
  316. $list = \db('设备_产量计酬')
  317. ->alias('a')
  318. ->join('设备_基本资料 b', 'a.sczl_jtbh = b.设备编号')
  319. ->join('工单_印件资料 c', 'a.sczl_gdbh = c.Yj_Gdbh AND a.sczl_yjno = c.yj_Yjno')
  320. ->join('工单_工艺资料 d', 'a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh')
  321. ->field([
  322. 'a.sczl_jtbh' => '设备编号',
  323. 'rtrim(b.设备名称)' => '设备名称',
  324. 'DATE(a.sczl_rq)' => '日期',
  325. 'a.sczl_gdbh' => '工单编号',
  326. 'a.sczl_yjno' => '印件号',
  327. 'a.sczl_gxh' => '工序号',
  328. 'c.yj_yjmc' => '印件名称',
  329. 'CONCAT(d.Gy0_gxmc, "(", d.Add_gxmc, ")")' => '工序名称',
  330. 'SUM(a.sczl_cl)' => '产量',
  331. 'SUM(a.sczl_设备运行工时)' => '设备运行工时',
  332. 'SUM(a.sczl_保养工时)' => '保养工时',
  333. 'SUM(a.sczl_打样总工时)' => '打样总工时',
  334. 'SUM(a.sczl_打样工时)' => '打样补产工时',
  335. 'SUM(a.sczl_装版总工时)' => '装板总工时',
  336. 'SUM(a.sczl_装版工时)' => '装板补产工时',
  337. 'SUM(a.sczl_异常停机工时)' => '异常停机工时',
  338. 'a.sczl_ms' => '墨色数',
  339. 'rtrim(d.印刷方式)' => '印刷方式',
  340. 'rtrim(d.版距)' => '版距'
  341. ])
  342. ->where($where)
  343. ->group('a.sczl_rq, a.sczl_gdbh, a.sczl_gxh, a.sczl_jtbh')
  344. ->order('a.sczl_rq')
  345. ->page($page, $limit) // 使用 page 和 limit 技术实现分页
  346. ->select();
  347. // 数据处理
  348. if (!empty($list)) {
  349. foreach ($list as $key => $value) {
  350. $list[$key]['工序名称'] = $value['印件号'] . '-' . $value['工序号'] . '-' . $value['工序名称'];
  351. // 移除不需要的字段
  352. unset($list[$key]['印件号'], $list[$key]['工序号']);
  353. // 产量计算
  354. if ($value['印刷方式'] === '卷对卷') {
  355. $list[$key]['产量'] = round($value['产量'] / $value['版距'] * 1000);
  356. }
  357. // 墨色数修正
  358. if ($value['墨色数'] === '0.00') {
  359. $list[$key]['墨色数'] = '1.00';
  360. }
  361. }
  362. }
  363. $this->success('成功', $list);
  364. }
  365. /**
  366. * 工序产出率月度统计报表
  367. * @return void
  368. * @throws \think\db\exception\DataNotFoundException
  369. * @throws \think\db\exception\ModelNotFoundException
  370. * @throws \think\exception\DbException
  371. */
  372. // public function ProcessOutputRate()
  373. // {
  374. // if ($this->request->isGet() === false){
  375. // $this->error('请求错误');
  376. // }
  377. // $param = $this->request->param();
  378. // if (empty($param)){
  379. // $this->error('参数错误');
  380. // }
  381. // //查询已经进入超节损的月份
  382. // $mouth = \db('工单_质量考核汇总')
  383. // ->where('sys_ny','like',$param['year'].'%')
  384. // ->column('distinct(sys_ny) as mouth');
  385. // //创建工序数组
  386. // $processType = ['胶印','卷凹','圆烫','圆切','烫金','模切','丝印','喷码','单凹'];
  387. // $result = $data = [];
  388. // foreach ($mouth as $key=>$value){
  389. // foreach ($processType as $item){
  390. // //查询进入超节损一年内所有数据
  391. // $list = \db('工单_质量考核汇总')
  392. // ->alias('a')
  393. // ->join('工单_工艺资料 b','a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  394. // ->join('工单_印件资料 c','a.Gy0_gdbh = c.Yj_Gdbh AND a.印件及工序 = c.yj_Yjno')
  395. // ->field('a.sys_ny,((sum(a.班组制程废品)/a.联数)+(sum(a.班组质检废品)/a.联数))/a.印件工序产量 as 废品率')
  396. // ->where('b.Gy0_gxmc','like',$item.'%')
  397. // ->where('a.sys_ny',$value)
  398. // ->group('a.Gy0_gdbh,b.Gy0_yjno,a.工序')
  399. // ->select();
  400. // $data[$value][$item] = $list;
  401. // }
  402. // }
  403. // $months = [
  404. // $param['year'].'01' => '01月',
  405. // $param['year'].'02' => '02月',
  406. // $param['year'].'03' => '03月',
  407. // $param['year'].'04' => '04月',
  408. // $param['year'].'05' => '05月',
  409. // $param['year'].'06' => '06月',
  410. // $param['year'].'07' => '07月',
  411. // $param['year'].'08' => '08月',
  412. // $param['year'].'09' => '09月',
  413. // $param['year'].'10' => '10月',
  414. // $param['year'].'11' => '11月',
  415. // $param['year'].'12' => '12月'
  416. // ];
  417. // //汇总数据
  418. // foreach ($processType as $index => $process) {
  419. // $result[$index] = array(
  420. // "工序类型" => $process,
  421. // "01月" => "",
  422. // "02月" => "",
  423. // "03月" => "",
  424. // "04月" => "",
  425. // "05月" => "",
  426. // "06月" => "",
  427. // "07月" => "",
  428. // "08月" => "",
  429. // "09月" => "",
  430. // "10月" => "",
  431. // "11月" => "",
  432. // "12月" => ""
  433. // );
  434. //
  435. // // 遍历月份
  436. // foreach ($months as $month => $monthName) {
  437. // if (isset($data[$month])){
  438. // $count = count($data[$month][$process]);
  439. // }
  440. //
  441. // $totalRate = 0;
  442. //
  443. // // 检查月份是否存在于数据中
  444. // if (isset($data[$month])) {
  445. // // 检查工序是否存在于月份数据中
  446. // if (isset($data[$month][$process])) {
  447. // // 累加废品率
  448. // foreach ($data[$month][$process] as $rate) {
  449. // $totalRate += floatval(1-$rate["废品率"]);
  450. // }
  451. // }
  452. // }
  453. // if ($count > 0){
  454. // $rateNumber = $totalRate/$count;
  455. // }
  456. // // 格式化为百分比字符串,保留两位小数
  457. // if ($totalRate > 0) {
  458. // $result[$index][$monthName] = sprintf("%.2f%%", $rateNumber * 100);
  459. // } else {
  460. // $result[$index][$monthName] = ""; // 如果没有数据,则为空字符串
  461. // }
  462. // }
  463. // }
  464. // foreach ($result as &$item) {
  465. // $total = 0;
  466. // $count = 0;
  467. //
  468. // // 遍历月份,计算总和和数量
  469. // for ($i = 1; $i <= 12; $i++) {
  470. // $month = sprintf("%02d月", $i); // 格式化月份,例如 "01月"
  471. // if (isset($item[$month]) && $item[$month] !== "") {
  472. // // 移除百分号,并将字符串转换为浮点数
  473. // $value = floatval(str_replace("%", "", $item[$month]));
  474. // $total += $value;
  475. // $count++;
  476. // }
  477. // }
  478. //
  479. // // 计算平均值
  480. // if ($count > 0) {
  481. // $average = $total / $count;
  482. // // 格式化为百分比字符串,保留两位小数
  483. // $item["平均值"] = sprintf("%.2f%%", $average);
  484. // } else {
  485. // $item["平均值"] = "0.00%"; // 如果没有数据,则为 0.00%
  486. // }
  487. // }
  488. // $this->success('成功',$result);
  489. // }
  490. // public function ProcessOutputRate()
  491. // {
  492. // if (!$this->request->isGet()) {
  493. // $this->error('请求错误');
  494. // }
  495. //
  496. // $param = $this->request->param();
  497. // if (empty($param)) {
  498. // $this->error('参数错误');
  499. // }
  500. //
  501. // // 新版工序结构
  502. // $processOrder = [
  503. // 1 => ['name' => '胶印工序', 'keys' => ['胶印', '上光']],
  504. // 2 => ['name' => '凹印工序', 'keys' => ['卷凹']],
  505. // 3 => ['name' => '圆烫工序', 'keys' => ['圆烫']],
  506. // 4 => ['name' => '圆切工序', 'keys' => ['圆切']],
  507. // 5 => ['name' => '烫模工序', 'keys' => ['烫金', '模切', '凹凸']],
  508. // 6 => ['name' => '丝印工序', 'keys' => ['丝印']],
  509. // 7 => ['name' => '喷码工序', 'keys' => ['喷码']],
  510. // 8 => ['name' => '单凹工序', 'keys' => ['单凹']]
  511. // ];
  512. //
  513. // // 获取存在数据的月份
  514. // $mouth = \db('工单_质量考核汇总')
  515. // ->where('sys_ny', 'like', $param['year'].'%')
  516. // ->column('distinct(sys_ny) as mouth');
  517. //
  518. // $data = [];
  519. // foreach ($mouth as $month) {
  520. // foreach ($processOrder as $processId => $process) {
  521. // $query = \db('工单_质量考核汇总')
  522. // ->alias('a')
  523. // ->join('工单_工艺资料 b', 'a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  524. // ->join('工单_印件资料 c', 'a.Gy0_gdbh = c.Yj_Gdbh AND a.印件及工序 = c.yj_Yjno')
  525. // ->join('成品入仓 d','a.Gy0_gdbh = d.jjcp_gdbh AND a.印件及工序 = d.jjcp_yjno')
  526. // ->field('a.sys_ny, ((SUM(a.班组制程废品)) + (SUM(a.班组分摊质检废品))) / a.印件工序产量 as 废品率')
  527. // ->where(function($query) use ($process) {
  528. // foreach ($process['keys'] as $key) {
  529. // $query->whereOr('a.工序名称', 'like', "%{$key}%");
  530. // }
  531. // })
  532. // ->where('d.jjcp_smb','like','末%')
  533. // ->where('a.sys_ny', $month)
  534. // ->group('a.Gy0_gdbh, b.Gy0_yjno, a.工序');
  535. //
  536. // $data[$month][$processId] = $query->select();
  537. // }
  538. // }
  539. // // 构建月份模板
  540. // $months = [
  541. // $param['year'].'01' => '01月',
  542. // $param['year'].'02' => '02月',
  543. // $param['year'].'03' => '03月',
  544. // $param['year'].'04' => '04月',
  545. // $param['year'].'05' => '05月',
  546. // $param['year'].'06' => '06月',
  547. // $param['year'].'07' => '07月',
  548. // $param['year'].'08' => '08月',
  549. // $param['year'].'09' => '09月',
  550. // $param['year'].'10' => '10月',
  551. // $param['year'].'11' => '11月',
  552. // $param['year'].'12' => '12月'
  553. // ];
  554. //
  555. // $result = [];
  556. // foreach ($processOrder as $processId => $process) {
  557. // $processData = [
  558. // "工序类型" => $process['name'],
  559. // "01月" => "",
  560. // "02月" => "",
  561. // "03月" =>"",
  562. // "04月" => "",
  563. // "05月" => "",
  564. // "06月" => "",
  565. // "07月" => "",
  566. // "08月" => "",
  567. // "09月" => "",
  568. // "10月" => "",
  569. // "11月" => "",
  570. // "12月" => "",
  571. // "平均值" => "0.00%"
  572. // ];
  573. //
  574. // $totalAnnual = 0;
  575. // $monthCount = 0;
  576. //
  577. // foreach ($months as $sysMonth => $monthName) {
  578. // $count = 0;
  579. // $totalRate = 0;
  580. //
  581. // if (isset($data[$sysMonth][$processId])) {
  582. // $records = $data[$sysMonth][$processId];
  583. // $count = count($records);
  584. //
  585. // foreach ($records as $record) {
  586. // $totalRate += floatval(1 - $record['废品率']);
  587. // }
  588. // }
  589. //
  590. // if ($count > 0) {
  591. // $rateValue = ($totalRate / $count) * 100;
  592. // $processData[$monthName] = sprintf("%.2f%%", $rateValue);
  593. // $totalAnnual += $rateValue;
  594. // $monthCount++;
  595. // } else {
  596. // $processData[$monthName] = "";
  597. // }
  598. // }
  599. //
  600. // // 计算年度平均值
  601. // if ($monthCount > 0) {
  602. // $processData['平均值'] = sprintf("%.2f%%", $totalAnnual / $monthCount);
  603. // }
  604. //
  605. // $result[] = $processData;
  606. // }
  607. //
  608. // $this->success('成功', $result);
  609. //}
  610. // public function ProcessOutputRate()
  611. // {
  612. // if (!$this->request->isGet()) {
  613. // $this->error('请求错误');
  614. // }
  615. //
  616. // $param = $this->request->param();
  617. // if (empty($param)) {
  618. // $this->error('参数错误');
  619. // }
  620. //
  621. // // 工序分类
  622. // $processOrder = [
  623. // 1 => ['name' => '胶印工序', 'keys' => ['胶印', '上光']],
  624. // 2 => ['name' => '凹印工序', 'keys' => ['卷凹']],
  625. // 3 => ['name' => '圆烫工序', 'keys' => ['圆烫']],
  626. // 4 => ['name' => '圆切工序', 'keys' => ['圆切']],
  627. // 5 => ['name' => '烫模工序', 'keys' => ['烫金', '模切', '凹凸']],
  628. // 6 => ['name' => '丝印工序', 'keys' => ['丝印']],
  629. // 7 => ['name' => '喷码工序', 'keys' => ['喷码']],
  630. // 8 => ['name' => '单凹工序', 'keys' => ['单凹']]
  631. // ];
  632. //
  633. // // 获取存在数据的月份
  634. // $monthsWithData = \db('工单_质量考核汇总')
  635. // ->where('sys_ny', 'like', $param['year'] . '%')
  636. // ->column('distinct(sys_ny) as month');
  637. //
  638. // // 初始化结果数组,按工序名称归类
  639. // $result = [];
  640. // foreach ($processOrder as $processId => $process) {
  641. // $result[$process['name']] = [
  642. // '01月' => '',
  643. // '02月' => '',
  644. // '03月' => '',
  645. // '04月' => '',
  646. // '05月' => '',
  647. // '06月' => '',
  648. // '07月' => '',
  649. // '08月' => '',
  650. // '09月' => '',
  651. // '10月' => '',
  652. // '11月' => '',
  653. // '12月' => '',
  654. // '平均值' => '0.00%'
  655. // ];
  656. // }
  657. //
  658. // // 遍历每个月份
  659. // foreach ($monthsWithData as $month) {
  660. // foreach ($processOrder as $processId => $process) {
  661. // // 查询该月份该工序的合格率数据
  662. // $query = \db('工单_质量考核汇总')
  663. // ->alias('a')
  664. // ->join('工单_工艺资料 b', 'a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  665. // ->join('工单_印件资料 c', 'a.Gy0_gdbh = c.Yj_Gdbh AND a.印件及工序 = c.yj_Yjno')
  666. // ->join('成品入仓 d', 'a.Gy0_gdbh = d.jjcp_gdbh AND a.印件及工序 = d.jjcp_yjno')
  667. // ->field('a.sys_ny,
  668. // (1 - ((SUM(a.班组制程废品) + SUM(a.班组分摊质检废品)) / SUM(a.班组产量))) * 100 as 合格率')
  669. // ->where(function ($query) use ($process) {
  670. // foreach ($process['keys'] as $key) {
  671. // $query->whereOr('a.工序名称', 'like', "%{$key}%");
  672. // }
  673. // })
  674. // ->where('d.jjcp_smb', 'like', '末%')
  675. // ->where('a.sys_ny', $month)
  676. // ->group('a.sys_ny');
  677. //
  678. // $records = $query->select();
  679. //
  680. // if (!empty($records)) {
  681. // $monthName = substr($month, 4, 2) . '月';
  682. // $result[$process['name']][$monthName] = sprintf("%.2f%%", $records[0]['合格率']);
  683. // }
  684. // }
  685. // }
  686. //
  687. // // 计算年度平均值
  688. // foreach ($result as $processName => &$processData) {
  689. // $totalAnnual = 0;
  690. // $monthCount = 0;
  691. //
  692. // foreach ($processData as $month => $rate) {
  693. // if ($month !== '平均值' && $rate !== '') {
  694. // $totalAnnual += floatval(str_replace('%', '', $rate));
  695. // $monthCount++;
  696. // }
  697. // }
  698. //
  699. // if ($monthCount > 0) {
  700. // $processData['平均值'] = sprintf("%.2f%%", $totalAnnual / $monthCount);
  701. // }
  702. // }
  703. //
  704. // $this->success('成功', array_values($result));
  705. // }
  706. public function ProcessOutputRate()
  707. {
  708. if (!$this->request->isGet()) {
  709. $this->error('请求错误');
  710. }
  711. $param = $this->request->param();
  712. if (empty($param)) {
  713. $this->error('参数错误');
  714. }
  715. // 工序分类
  716. $processOrder = [
  717. 1 => ['name' => '胶印', 'keys' => ['胶印', '上光']],
  718. 2 => ['name' => '卷凹', 'keys' => ['卷凹']],
  719. 3 => ['name' => '圆烫', 'keys' => ['圆烫']],
  720. 4 => ['name' => '圆切', 'keys' => ['圆切']],
  721. 5 => ['name' => '烫模', 'keys' => ['烫金', '模切', '凹凸']],
  722. 6 => ['name' => '丝印', 'keys' => ['丝印']],
  723. 7 => ['name' => '喷码', 'keys' => ['喷码']],
  724. 8 => ['name' => '单凹', 'keys' => ['单凹']]
  725. ];
  726. // 获取存在数据的月份
  727. $monthsWithData = \db('工单_质量考核汇总')
  728. ->where('sys_ny', 'like', $param['year'] . '%')
  729. ->column('distinct(sys_ny) as month');
  730. // 初始化结果数组,按工序名称归类
  731. $result = [];
  732. foreach ($processOrder as $processId => $process) {
  733. $result[$process['name']] = [
  734. '01月' => '',
  735. '02月' => '',
  736. '03月' => '',
  737. '04月' => '',
  738. '05月' => '',
  739. '06月' => '',
  740. '07月' => '',
  741. '08月' => '',
  742. '09月' => '',
  743. '10月' => '',
  744. '11月' => '',
  745. '12月' => '',
  746. '平均值' => '0.00%'
  747. ];
  748. }
  749. // 遍历每个月份
  750. foreach ($monthsWithData as $month) {
  751. foreach ($processOrder as $processId => $process) {
  752. // 查询该月份该工序的合格率数据
  753. $query = \db('工单_质量考核汇总')
  754. ->alias('a')
  755. ->join('工单_工艺资料 b', 'a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  756. ->join('工单_印件资料 c', 'a.Gy0_gdbh = c.Yj_Gdbh AND a.印件及工序 = c.yj_Yjno')
  757. ->join('成品入仓 d', 'a.Gy0_gdbh = d.jjcp_gdbh AND a.印件及工序 = d.jjcp_yjno')
  758. ->field('a.sys_ny,
  759. (1 - ((SUM(a.班组制程废品) + SUM(a.班组质检废品)) / SUM(a.班组产量))) * 100 as 合格率')
  760. ->where(function ($query) use ($process) {
  761. foreach ($process['keys'] as $key) {
  762. $query->whereOr('a.工序名称', 'like', "%{$key}%");
  763. }
  764. })
  765. ->where('d.jjcp_smb', 'like', '末%')
  766. ->where('a.sys_ny', $month)
  767. ->group('a.sys_ny');
  768. $records = $query->select();
  769. if (!empty($records)) {
  770. $monthName = substr($month, 4, 2) . '月';
  771. $result[$process['name']][$monthName] = sprintf("%.2f%%", $records[0]['合格率']);
  772. }
  773. }
  774. }
  775. // 计算年度平均值
  776. foreach ($result as $processName => &$processData) {
  777. $totalAnnual = 0;
  778. $monthCount = 0;
  779. foreach ($processData as $month => $rate) {
  780. if ($month !== '平均值' && $rate !== '') {
  781. $totalAnnual += floatval(str_replace('%', '', $rate));
  782. $monthCount++;
  783. }
  784. }
  785. if ($monthCount > 0) {
  786. $processData['平均值'] = sprintf("%.2f%%", $totalAnnual / $monthCount);
  787. }
  788. }
  789. // 转换为目标格式
  790. $formattedData = [];
  791. foreach ($result as $processName => $processData) {
  792. $formattedData[] = array_merge(['工序类型' => $processName], $processData);
  793. }
  794. // 返回JSON响应
  795. $this->success('成功',$formattedData);
  796. }
  797. /**
  798. * 获取年分数据
  799. * @return void
  800. */
  801. public function GetYear()
  802. {
  803. if ($this->request->isGet() === false){
  804. $this->error('请求错误');
  805. }
  806. $data = \db('工单_质量考核汇总')
  807. ->group('year')
  808. ->column('YEAR(STR_TO_DATE(sys_ny, "%Y%m")) as year');
  809. $this->success('成功',$data);
  810. }
  811. /**
  812. * 数据透视表
  813. * @return null
  814. * @throws \think\Exception
  815. * @throws \think\db\exception\DataNotFoundException
  816. * @throws \think\db\exception\ModelNotFoundException
  817. * @throws \think\exception\DbException
  818. */
  819. public function PoductData()
  820. {
  821. if (!$this->request->isGet()) {
  822. $this->error('请求错误');
  823. }
  824. $param = $this->request->param();
  825. if (empty($param['year'])) {
  826. $this->error('参数错误');
  827. }
  828. // 定义分类规则及顺序
  829. $processOrder = [
  830. 1 => ['name' => '胶印工序', 'keys' => ['胶印', '上光']],
  831. 2 => ['name' => '凹印工序', 'keys' => ['卷凹']],
  832. 3 => ['name' => '圆烫工序', 'keys' => ['圆烫']],
  833. 4 => ['name' => '圆切工序', 'keys' => ['圆切']],
  834. 5 => ['name' => '烫模工序', 'keys' => ['烫金', '模切', '凹凸']],
  835. 6 => ['name' => '丝印工序', 'keys' => ['丝印']],
  836. 7 => ['name' => '喷码工序', 'keys' => ['喷码']],
  837. 8 => ['name' => '单凹工序', 'keys' => ['单凹']]
  838. ];
  839. // 构建基础查询
  840. $query = \db('工单_质量考核汇总')
  841. ->alias('a')
  842. ->field('
  843. a.sczl_jtbh AS 机台编号,
  844. a.Gy0_gdbh AS 工单编号,
  845. a.印件及工序 AS 工序号,
  846. a.产品名称 AS 印件名称,
  847. a.工序 AS 工序,
  848. a.工序名称,
  849. a.联数,
  850. a.班组产量,
  851. a.班组制程废品,
  852. a.班组质检废品,
  853. a.sczl_bzdh AS 班组编号,
  854. b.印刷方式,
  855. b.版距,
  856. DATE_FORMAT(a.入仓日期, \'%Y%m\') AS 完工年月
  857. ')
  858. ->join('工单_工艺资料 b', 'a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  859. ->join('成品入仓 c','a.Gy0_gdbh = c.jjcp_gdbh AND a.印件及工序 = c.jjcp_yjno')
  860. ->where('a.sys_ny', 'like', $param['year'] . '%')
  861. ->where('a.工序名称','not like','%机检%')
  862. ->where('c.jjcp_smb','like','末%')
  863. ->where('a.工序名称','not like','%拆片%');
  864. // 获取总数
  865. $total = clone $query;
  866. $total = $total->count();
  867. // 执行查询
  868. $rawList = isset($param['page'], $param['limit'])
  869. ? $query->page($param['page'])->limit($param['limit'])->select()
  870. : $query->select();
  871. // 处理分类标记
  872. $processedList = [];
  873. foreach ($rawList as $item) {
  874. $matched = false;
  875. // 顺序匹配分类规则
  876. foreach ($processOrder as $typeId => $rule) {
  877. foreach ($rule['keys'] as $keyword) {
  878. if (strpos($item['工序名称'], $keyword) !== false) {
  879. // 添加分类标记
  880. $item['type_id'] = $typeId;
  881. $item['type_name'] = $rule['name'];
  882. $processedList[] = $item;
  883. $matched = true;
  884. break 2; // 跳出两层循环
  885. }
  886. }
  887. }
  888. // 未匹配到分类的数据丢弃
  889. }
  890. // 按分类顺序排序(保持1-8的顺序)
  891. usort($processedList, function($a, $b) {
  892. return $a['type_id'] <=> $b['type_id'];
  893. });
  894. // 返回结构
  895. $data['data'] = $processedList;
  896. $data['total'] = $total; // 原始总数
  897. $data['filtered_total'] = count($processedList); // 实际有效数
  898. return count($processedList) > 0
  899. ? $this->success('成功', $data)
  900. : $this->error('未找到数据');
  901. }
  902. }