Achievementatestatistics.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use Monolog\Handler\IFTTTHandler;
  5. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  6. use think\Db;
  7. use think\Request;
  8. use PhpOffice\PhpSpreadsheet\IOFactory;
  9. class Achievementatestatistics extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 1.1达成率统计->左侧菜单
  15. */
  16. public function Leftmenu(){
  17. // 获取所有部门(排除特定)
  18. $departments = Db::name('设备_基本资料')
  19. ->field('使用部门')
  20. ->whereNotIn('使用部门', ['打样室', '智能车间'])
  21. ->group('使用部门')
  22. ->select();
  23. // 部门名数组
  24. $deptList = array_map(function($item) {
  25. return trim($item['使用部门']);
  26. }, $departments);
  27. // 获取产量表日期
  28. $rawData = Db::name('设备_产量计酬')
  29. ->field("DATE(sys_rq) as rq")
  30. ->whereRaw("YEAR(sys_rq) >= 2000")
  31. ->order("rq desc")
  32. ->select();
  33. $workStats = [];
  34. $addedMonths = []; // 记录已处理年月
  35. foreach ($rawData as $item) {
  36. $rq = $item['rq'];
  37. $year = date('Y', strtotime($rq));
  38. $yearMonth = date('Ym', strtotime($rq));
  39. // 如果该年月还没加过,才添加
  40. if (!isset($addedMonths[$year][$yearMonth])) {
  41. $workStats[$year][$yearMonth] = $deptList;
  42. $addedMonths[$year][$yearMonth] = true;
  43. }
  44. }
  45. $result = [
  46. '排产计划达成率统计' => $departments,
  47. '设备工时达成率统计' => $workStats
  48. ];
  49. $this->success('成功',$result);
  50. }
  51. /**
  52. * 1.2达成率统计->机台列表
  53. */
  54. public function Machine_List()
  55. {
  56. if (!$this->request->isGet()) {
  57. $this->error('请求方式错误');
  58. }
  59. $param = $this->request->param();
  60. if (empty($param['Machine'])) {
  61. $this->error('缺少参数 Machine');
  62. }
  63. // 最近 7 天日期(含今天)
  64. $dateMap = [];
  65. $recentDates = []; // 用来存放最近七天的日期
  66. for ($i = 0; $i < 7; $i++) {
  67. $date = date('Y-m-d', strtotime("-{$i} days"));
  68. $label = date('m月d日', strtotime($date));
  69. $dateMap[$date] = $label;
  70. $recentDates[] = $date; // 记录日期
  71. }
  72. // 查设备
  73. $devices = Db::name('设备_基本资料')
  74. ->field('设备编号, 设备名称')
  75. ->whereLike('使用部门', '%' . $param['Machine'] . '%')
  76. ->select();
  77. $results = [];
  78. foreach ($devices as $device) {
  79. $jtbh = $device['设备编号'];
  80. $jtname = trim($device['设备名称']);
  81. // 查询上报数据(产量+工单+印件号+工序号)
  82. $records = Db::name('设备_产量计酬')
  83. ->field('sczl_bzdh as 班组, sczl_cl as 实际产量, sczl_rq as 日期, sczl_gdbh, sczl_yjno, sczl_gxh')
  84. ->where('sczl_jtbh', $jtbh)
  85. ->where('sczl_rq', '>=', date('Y-m-d 00:00:00', strtotime('-6 days')))
  86. ->where('sczl_rq', '<=', date('Y-m-d 23:59:59'))
  87. ->select();
  88. // 数据结构:班组 → 日期 → 实际产量 + 工单明细
  89. $bzData = [];
  90. foreach ($records as $row) {
  91. $bz = $row['班组'] ?: '未分组';
  92. $rq = date('Y-m-d', strtotime($row['日期']));
  93. if (!isset($bzData[$bz][$rq])) {
  94. $bzData[$bz][$rq] = [
  95. '实际产量' => 0,
  96. '排产产量' => 0,
  97. '工单明细' => []
  98. ];
  99. }
  100. $bzData[$bz][$rq]['实际产量'] += floatval($row['实际产量']);
  101. // 收集唯一组合键用于查询计划数
  102. $key = $row['sczl_gdbh'] . '_' . $row['sczl_yjno'] . '_' . $row['sczl_gxh'];
  103. if (!in_array($key, $bzData[$bz][$rq]['工单明细'])) {
  104. $bzData[$bz][$rq]['工单明细'][] = $key;
  105. }
  106. }
  107. // 查询排产产量(计划接货数)
  108. foreach ($bzData as $bz => &$dateList) {
  109. foreach ($dateList as $rq => &$info) {
  110. $totalPlan = 0;
  111. foreach ($info['工单明细'] as $key) {
  112. list($gdbh, $yjno, $gxh) = explode('_', $key);
  113. $plan = Db::name('工单_工艺资料')
  114. ->where('Gy0_gdbh', $gdbh)
  115. ->where('Gy0_yjno', $yjno)
  116. ->where('Gy0_gxh', $gxh)
  117. ->value('Gy0_计划接货数');
  118. $totalPlan += floatval($plan);
  119. }
  120. $info['排产产量'] = $totalPlan;
  121. }
  122. }
  123. // 输出结构:每个机台 + 班组一条
  124. foreach ($bzData as $bz => $dateList) {
  125. $item = [
  126. '机台编号' => $jtbh,
  127. '机台名称' => $jtname,
  128. '班组' => $bz,
  129. '实际总产量' => 0,
  130. '排产总产量' => 0
  131. ];
  132. $i = 1;
  133. foreach ($dateMap as $date => $label) {
  134. $sl = isset($dateList[$date]) ? $dateList[$date]['实际产量'] : 0;
  135. $jh = isset($dateList[$date]) ? $dateList[$date]['排产产量'] : 0;
  136. $rate = $jh > 0 ? round($sl / $jh * 100, 2) . '%' : '0%';
  137. $item[$label] = $rate;
  138. $item["实际产量{$i}"] = $sl;
  139. $item["排产产量{$i}"] = $jh;
  140. $item['实际总产量'] += $sl;
  141. $item['排产总产量'] += $jh;
  142. $i++;
  143. }
  144. $results[] = $item;
  145. }
  146. }
  147. $this->success('成功', [
  148. 'data' => $results,
  149. 'list' => $recentDates
  150. ]);
  151. }
  152. /**
  153. * 1.3达成率统计->机台生产详情
  154. */
  155. public function Machine_Detail()
  156. {
  157. if (!$this->request->isGet()) {
  158. $this->error('请求方式错误');
  159. }
  160. $param = $this->request->param();
  161. if (empty($param['jtbh']) || empty($param['bz'])) {
  162. $this->error('缺少参数:jtbh(机台编号)或 bz(班组)');
  163. }
  164. $jtbh = $param['jtbh'];
  165. $bz = $param['bz'];
  166. // 查询近 7 天产量记录
  167. $records = Db::name('设备_产量计酬')->alias('a')
  168. ->field('
  169. a.sczl_rq as 日期,
  170. a.sczl_jtbh as 机台编号,
  171. a.sczl_bzdh as 班组,
  172. a.sczl_gdbh as 工单编号,
  173. a.sczl_yjno as 印件号,
  174. b.yj_yjmc as 印件名称,
  175. a.sczl_gxh as 工序号,
  176. a.sczl_gxmc as 工序名称,
  177. a.sczl_cl as 实际产量,
  178. c.排单小时定额 as 排产标准产能,
  179. d.Gy0_计划接货数 as 排产产量,
  180. a.sczl_设备运行工时 as 上报运行工时,
  181. a.sczl_装版总工时 as 装版实际工时,
  182. a.sczl_保养工时 as 保养工时,
  183. a.sczl_打样总工时 as 打样工时,
  184. d.版距 as 版距,
  185. c.设备名称 as 机台名称
  186. ')
  187. ->join('工单_印件资料 b','a.sczl_gdbh = b.Yj_Gdbh')
  188. ->join('设备_基本资料 c','a.sczl_jtbh = c.设备编号')
  189. ->join('工单_工艺资料 d','a.sczl_gdbh = d.Gy0_gdbh AND a.sczl_yjno = d.Gy0_yjno AND a.sczl_gxh = d.Gy0_gxh')
  190. ->where('a.sczl_jtbh', $jtbh)
  191. ->where('a.sczl_bzdh', $bz)
  192. ->where('a.sczl_rq', '>=', date('Y-m-d 00:00:00', strtotime('-6 days')))
  193. ->where('a.sczl_rq', '<=', date('Y-m-d 23:59:59'))
  194. ->order('a.sczl_rq desc')
  195. ->select();
  196. $details = [];
  197. foreach ($records as $row) {
  198. $rq = date('Y-m-d', strtotime($row['日期']));
  199. $details[] = [
  200. '日期' => $rq,
  201. '机台编号' => $row['机台编号'],
  202. '班组' => $row['班组'],
  203. '工单编号' => $row['工单编号'],
  204. '印件号' => $row['印件号'],
  205. '印件名称' => $row['印件名称'],
  206. '工序号' => $row['工序号'],
  207. '工序名称' => $row['工序名称'],
  208. '实际产量' => floatval($row['实际产量']),
  209. '排产标准产能' => $row['排产标准产能'],
  210. '排产产量' => floatval($row['排产产量']),
  211. '上报运行工时' => $row['上报运行工时'],
  212. '装版实际工时' => $row['装版实际工时'],
  213. '保养工时' => $row['保养工时'],
  214. '打样工时' => $row['打样工时'],
  215. '版距' => $row['版距'],
  216. '机台名称' => $row['机台名称'],
  217. ];
  218. }
  219. $this->success('明细获取成功', $details);
  220. }
  221. /**
  222. * 1.4达成率统计 -> 按月份汇总机台产量(按班组分组)
  223. */
  224. public function Machine_production_list()
  225. {
  226. if (!$this->request->isGet()) {
  227. $this->error('请求方式错误');
  228. }
  229. $param = $this->request->param();
  230. if (empty($param['Machine']) || empty($param['rq'])) {
  231. $this->error('参数错误');
  232. }
  233. $machine = $param['Machine'];
  234. $rq = $param['rq'];
  235. $ym = substr($rq, 0, 4) . '-' . substr($rq, 4, 2);
  236. // 查询设备编号
  237. $devices = Db::name('设备_基本资料')
  238. ->where('使用部门', $machine)
  239. ->column('设备编号');
  240. $devices = array_map('trim', $devices);
  241. // 查询产量记录
  242. $records = Db::name('设备_产量计酬')
  243. ->field([
  244. 'sczl_jtbh as 机台编号',
  245. 'sczl_bzdh as 班组',
  246. 'sczl_rq',
  247. 'sczl_cl',
  248. 'sczl_ls',
  249. 'sczl_装版总工时 as 装板实际工时',
  250. 'sczl_装版工时 as 装板补产工时',
  251. 'sczl_保养工时 as 保养工时',
  252. 'sczl_打样总工时 as 打样总工时',
  253. 'sczl_打样工时 as 打样补产工时',
  254. 'sczl_异常停机工时 as 异常总工时',
  255. 'sczl_异常工时1 as 异常补时'
  256. ])
  257. ->whereIn('sczl_jtbh', $devices)
  258. ->where('sczl_rq', 'like', $ym . '%')
  259. ->select();
  260. // 分组汇总:按“机台编号 + 班组”
  261. $resultList = [];
  262. foreach ($records as $row) {
  263. $jtbh = $row['机台编号'];
  264. $bz = $row['班组'] ?: '未分组';
  265. $key = $jtbh . '|' . $bz;
  266. if (!isset($resultList[$key])) {
  267. $resultList[$key] = [
  268. '机台编号' => $jtbh,
  269. '班组' => $bz,
  270. '实际产量' => 0,
  271. '装板实际工时' => 0,
  272. '装板补产工时' => 0,
  273. '保养工时' => 0,
  274. '打样总工时' => 0,
  275. '打样补产工时' => 0,
  276. '异常总工时' => 0,
  277. '异常补时' => 0
  278. ];
  279. }
  280. //汇总逻辑
  281. $resultList[$key]['实际产量'] += floatval($row['sczl_cl']);
  282. $resultList[$key]['装板实际工时'] += floatval($row['装板实际工时']);
  283. $resultList[$key]['装板补产工时'] += floatval($row['装板补产工时']);
  284. $resultList[$key]['保养工时'] += floatval($row['保养工时']);
  285. $resultList[$key]['打样总工时'] += floatval($row['打样总工时']);
  286. $resultList[$key]['打样补产工时'] += floatval($row['打样补产工时']);
  287. $resultList[$key]['异常总工时'] += floatval($row['异常总工时']);
  288. $resultList[$key]['异常补时'] += floatval($row['异常补时']);
  289. }
  290. $this->success('汇总成功', array_values($resultList));
  291. }
  292. /**
  293. * 1.5达成率统计->按日期机台生产详情
  294. */
  295. public function Machine_production_details()
  296. {
  297. if (!$this->request->isGet()) {
  298. $this->error('请求方式错误');
  299. }
  300. $param = $this->request->param();
  301. if (empty($param['jtbh']) || empty($param['bz'])) {
  302. $this->error('缺少参数:jtbh(机台编号)或 bz(班组)');
  303. }
  304. $jtbh = $param['jtbh'];
  305. $bz = $param['bz'];
  306. // 查询近7天该机台该班组的产量明细
  307. $records = Db::name('设备_产量计酬')->alias('a')
  308. ->field([
  309. 'a.sczl_jtbh as 机台编号',
  310. 'a.sczl_rq as 日期',
  311. 'a.sczl_bzdh as 班组',
  312. 'a.sczl_gdbh as 工单编号',
  313. 'b.yj_yjmc as 印件名称',
  314. 'a.sczl_yjno as 印件号',
  315. 'a.sczl_gxh as 工序号',
  316. 'a.sczl_gxmc as 工序名称',
  317. 'a.sczl_ls',
  318. 'SUM(a.sczl_cl) as 实际产量',
  319. 'sczl_装版总工时 as 装板实际工时',
  320. 'sczl_装版工时 as 装板补产工时',
  321. 'sczl_保养工时 as 保养工时',
  322. 'sczl_打样总工时 as 打样总工时',
  323. 'sczl_打样工时 as 打样补产工时',
  324. 'sczl_异常停机工时 as 异常总工时',
  325. 'sczl_异常工时1 as 异常补时',
  326. 'sczl_设备运行工时 as 运行工时'
  327. ])
  328. ->join('工单_印件资料 b', 'a.sczl_gdbh = b.Yj_Gdbh')
  329. ->where('a.sczl_jtbh', $jtbh)
  330. ->where('a.sczl_bzdh', $bz)
  331. ->whereBetween('a.sczl_rq', [
  332. date('Y-m-d 00:00:00', strtotime('-6 days')),
  333. date('Y-m-d 23:59:59')
  334. ])
  335. ->order('a.sczl_rq desc')
  336. ->select();
  337. foreach ($records as &$row) {
  338. $row['日期'] = date('Y-m-d', strtotime($row['日期']));
  339. $row['印件工序'] = $row['印件号'].'-'.$row['工序名称'];
  340. // $row['目标产量'] = ($row['运行工时'] - $row['保养工时'] - $row['装板补产工时'] - $row['异常总工时']) * $row['小时产能'];
  341. }
  342. $this->success('明细获取成功', $records);
  343. }
  344. }