Index.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\service\SalaryCalculationService;
  6. class Index extends Api
  7. {
  8. protected $noNeedLogin = ['*'];
  9. protected $noNeedRight = ['*'];
  10. public function index()
  11. {
  12. $this->success('请求成功');
  13. }
  14. /**
  15. * 第一屏
  16. */
  17. public function GetOrderNumber(){
  18. $nowTime = date('Y-m-d H:i:s',time());
  19. $oldTime = (date('Y')-1).'-'.date('m-d H:i:s');
  20. $currentYear = date('Y');
  21. $lastYear = $currentYear - 1;
  22. //按月份查询工单信息(两年)
  23. $monthData = \db('工单_基本资料')
  24. ->where('Mod_rq', 'between', [$oldTime, $nowTime])
  25. ->where('行号', '1')
  26. ->field('DATE_FORMAT(Mod_rq, "%Y-%m") as month, COUNT(*) as count')
  27. ->group('month')
  28. ->order('month')
  29. ->select();
  30. // 构建按月分组的索引
  31. $currentData = array_fill(1, 12, 0);
  32. $lastData = array_fill(1, 12, 0);
  33. foreach ($monthData as $item) {
  34. list($y, $m) = explode('-', $item['month']);
  35. $m = (int)$m;
  36. if ((int)$y == $currentYear) {
  37. $currentData[$m] = (int)$item['count'];
  38. } elseif ((int)$y == $lastYear) {
  39. $lastData[$m] = (int)$item['count'];
  40. }
  41. }
  42. // X 轴:1-12 月
  43. $categories = [];
  44. for ($i = 1; $i <= 12; $i++) {
  45. $categories[] = str_pad($i, 2, '0', STR_PAD_LEFT);
  46. }
  47. $result = [
  48. "status" => 0,
  49. "msg" => "",
  50. "data" => [
  51. "categories" => $categories,
  52. "series" => [
  53. [
  54. "name" => $lastYear . "年",
  55. "data" => array_values($lastData),
  56. "color" => "#7B3FF2"
  57. ],
  58. [
  59. "name" => $currentYear . "年",
  60. "data" => array_values($currentData),
  61. "color" => "#0ABEFA"
  62. ]
  63. ]
  64. ]
  65. ];
  66. return json($result);
  67. }
  68. //计划/排程/制程工单数量同步Redis
  69. public function numDbToRedis()
  70. {
  71. $numDbToRedis = [];
  72. //获取计划中的订单数量
  73. $jhz = db('工单_基本资料')
  74. ->where('gd_statu','like','%计划中%')
  75. ->count('Gd_gdbh');
  76. if($jhz!==false){
  77. $numDbToRedis['jhz'] = $jhz;
  78. }else{
  79. $numDbToRedis['jhz'] = 0;
  80. }
  81. //获取生产中的所有数据
  82. $rows = db('工单_基本资料')
  83. ->distinct('Gd_gdbh')
  84. ->where('gd_statu','like','%生产中%')
  85. ->column('Gd_gdbh');
  86. if($rows===false){
  87. $numDbToRedis['pcz'] = 0;
  88. $numDbToRedis['zcz'] = 0;
  89. $bool = cache('numDbToRedis',$jhz);
  90. if($bool===false) $this->error('订单数量存入redis失败','','000001');
  91. $this->success('请求成功');
  92. }
  93. //查询Gy0_sj1不等于1900-01-01 00:00:00的数据
  94. $data = db('工单_印件资料')->alias('y')
  95. ->join('工单_工艺资料 g','y.Yj_Gdbh = g.Gy0_gdbh AND y.yj_Yjno = g.Gy0_yjno')
  96. ->where('g.Gy0_sj1','<>','1900-01-01 00:00:00')
  97. ->where('y.Yj_Gdbh','in',$rows)
  98. ->group('y.Yj_Gdbh')
  99. ->column('y.Yj_Gdbh');
  100. $zcz = count($data);
  101. $pcz = count($rows) - count($data);
  102. $numDbToRedis['pcz'] = $pcz;
  103. $numDbToRedis['zcz'] = $zcz;
  104. $bool = cache('numDbToRedis',$numDbToRedis);
  105. if($bool===false) $this->error('订单数量存入redis失败','','000002');
  106. $this->success('同步成功');
  107. }
  108. //获取计划中工单数量
  109. public function getJhzByRedis()
  110. {
  111. $row = cache('numDbToRedis');
  112. if($row){
  113. $data = [
  114. 'status'=>0,
  115. 'msg'=>'',
  116. 'data'=>[
  117. 'name'=>'',
  118. 'value'=>$row['jhz']
  119. ]
  120. ];
  121. return json($data);
  122. }else{
  123. $data = [
  124. 'status'=>0,
  125. 'msg'=>'失败',
  126. 'data'=>[
  127. 'name'=>'',
  128. 'value'=>''
  129. ]
  130. ];
  131. return json($data);
  132. }
  133. }
  134. //获取排程中工单数量
  135. public function getPczByRedis()
  136. {
  137. $row = cache('numDbToRedis');
  138. if($row){
  139. $data = [
  140. 'status'=>0,
  141. 'msg'=>'',
  142. 'data'=>[
  143. 'name'=>'',
  144. 'value'=>$row['pcz']
  145. ]
  146. ];
  147. return json($data);
  148. }else{
  149. $data = [
  150. 'status'=>0,
  151. 'msg'=>'失败',
  152. 'data'=>[
  153. 'name'=>'',
  154. 'value'=>''
  155. ]
  156. ];
  157. return json($data);
  158. }
  159. }
  160. //获取制程中工单数量
  161. public function getZczByRedis()
  162. {
  163. $row = cache('numDbToRedis');
  164. if($row){
  165. $data = [
  166. 'status'=>0,
  167. 'msg'=>'',
  168. 'data'=>[
  169. 'name'=>'',
  170. 'value'=>$row['zcz']
  171. ]
  172. ];
  173. return json($data);
  174. }else{
  175. $data = [
  176. 'status'=>0,
  177. 'msg'=>'失败',
  178. 'data'=>[
  179. 'name'=>'',
  180. 'value'=>''
  181. ]
  182. ];
  183. return json($data);
  184. }
  185. }
  186. //印刷车间生产进度数据同步Redis
  187. public function yscjDbToRedis()
  188. {
  189. $yscjDbToRedis = [];
  190. //获取所有印刷车间
  191. // $rows = db('设备_基本资料')
  192. // ->where('使用部门','凹丝印车间')
  193. // ->column('设备编号');
  194. $rows = db('设备_基本资料')
  195. ->whereIn('使用部门', ['凹丝印车间', '胶印车间'])
  196. ->column('设备编号');
  197. foreach($rows as $value){
  198. $row = db('设备_产量采集')->alias('c')
  199. ->field('c.设备编号 as 机台号,
  200. c.工单编号 as 在产工单号,
  201. j.Gd_cpmc as 工单名称,
  202. c.工序名称 as 工序,
  203. g.Gy0_计划接货数 as 计划产量,
  204. g.Gy0_小时产能 as 小时产能,
  205. c.当前状态 as 机台状态
  206. ')
  207. ->join('工单_工艺资料 g','c.设备编号 = g.Gy0_sbbh AND c.工单编号 = g.Gy0_gdbh AND c.印件号 = g.Gy0_yjno')
  208. ->join('工单_基本资料 j','j.Gd_gdbh = c.工单编号')
  209. ->where('c.设备编号',$value)
  210. ->order('c.UniqId desc')
  211. ->find();
  212. if(!$row) continue;
  213. $cl = db('设备_产量计酬')
  214. ->where(['sczl_gdbh'=>$row['在产工单号'], 'sczl_jtbh' => $row['机台号']])
  215. ->sum('sczl_cl');
  216. $row['已完成产量'] = $cl;
  217. array_push($yscjDbToRedis, $row);
  218. }
  219. $bool = cache('yscjDbToRedis',$yscjDbToRedis);
  220. if($bool===false) $this->error('存入redis失败','','000002');
  221. $this->success('同步成功');
  222. }
  223. //印后车间生产进度数据同步Redis
  224. public function yhcjDbToRedis()
  225. {
  226. $yhcjDbToRedis = [];
  227. //获取所有印后车间
  228. $rows = db('设备_基本资料')
  229. ->where('使用部门','印后车间')
  230. ->column('设备编号');
  231. foreach($rows as $value){
  232. $row = db('设备_产量采集')->alias('c')
  233. ->field('c.设备编号 as 机台号,
  234. c.工单编号 as 在产工单号,
  235. j.Gd_cpmc as 工单名称,
  236. c.工序名称 as 工序,
  237. g.Gy0_计划接货数 as 计划产量,
  238. g.Gy0_小时产能 as 小时产能,
  239. c.当前状态 as 机台状态
  240. ')
  241. ->join('工单_工艺资料 g','c.设备编号 = g.Gy0_sbbh AND c.工单编号 = g.Gy0_gdbh AND c.印件号 = g.Gy0_yjno')
  242. ->join('工单_基本资料 j','j.Gd_gdbh = c.工单编号')
  243. ->where('c.设备编号',$value)
  244. ->order('c.UniqId desc')
  245. ->find();
  246. if(!$row) continue;
  247. $cl = db('设备_产量计酬')
  248. ->where(['sczl_gdbh'=>$row['在产工单号'], 'sczl_jtbh' => $row['机台号']])
  249. ->sum('sczl_cl');
  250. $row['已完成产量'] = $cl;
  251. array_push($yhcjDbToRedis, $row);
  252. }
  253. $bool = cache('yhcjDbToRedis',$yhcjDbToRedis);
  254. if($bool===false) $this->error('存入redis失败','','000002');
  255. $this->success('同步成功');
  256. }
  257. //获取印刷车间生产进度数据
  258. public function getYscjByRedis()
  259. {
  260. $row = cache('yscjDbToRedis');
  261. if (empty($row)) {
  262. return json([
  263. 'status' => 0,
  264. 'msg' => '',
  265. 'data' => [] // 或者可以返回 []
  266. ]);
  267. }
  268. $result['columns'] = [
  269. ['name' => '机台号', 'id' => 'jtbh', 'width' => '11', 'autoWrap' => "true", 'textAlign' => 'left'],
  270. ['name' => '在产工单号', 'id' => 'gdbh', 'width' => '15', 'textAlign' => 'left'],
  271. ['name' => '工单名称', 'id' => 'gdmc', 'width' => '35', 'autoWrap' => "true", 'textAlign' => 'left'],
  272. ['name' => '工序', 'id' => 'gx', 'width' => '15', 'textAlign' => 'left'],
  273. ['name' => '计划产量', 'id' => 'jhcl', 'width' => '8', 'autoWrap' => "true", 'textAlign' => 'left'],
  274. ['name' => '已完成产量', 'id' => 'wccl', 'width' => '9', 'autoWrap' => "true", 'textAlign' => 'center'],
  275. ['name' => '小时产能', 'id' => 'xscn', 'width' => '8', 'autoWrap' => "true", 'textAlign' => 'center'],
  276. ['name' => '机台状态', 'id' => 'status', 'width' => '8', 'autoWrap' => "true", 'textAlign' => 'center'],
  277. ];
  278. foreach ($row as $k => $v) {
  279. $result['rows'][$k] = [
  280. 'jtbh' => $v['机台号'],
  281. 'gdbh' => $v['在产工单号'],
  282. 'gdmc' => $v['工单名称'],
  283. 'gx' => $v['工序'],
  284. 'jhcl' => $v['计划产量'],
  285. 'wccl' => $v['已完成产量'],
  286. 'xscn' => $v['小时产能'],
  287. 'status' => $v['机台状态'],
  288. ];
  289. }
  290. return json([
  291. 'status' => 0,
  292. 'msg' => '',
  293. 'data' => $result
  294. ]);
  295. }
  296. //获取印后车间生产进度数据
  297. public function getYhcjByRedis()
  298. {
  299. $row = cache('yhcjDbToRedis');
  300. $result['columns']=[
  301. ['name'=>'机台号','id'=>'jtbh','width'=>'11','autoWrap'=>"true",'textAlign'=>'left'],
  302. ['name'=>'在产工单号','id'=>'gdbh','width'=>'15','textAlign'=>'left'],
  303. ['name'=>'工单名称','id'=>'gdmc','width'=>'35','autoWrap'=>"true",'textAlign'=>'left'],
  304. ['name'=>'工序','id'=>'gx','width'=>'15','textAlign'=>'left'],
  305. ['name'=>'计划产量','id'=>'jhcl','width'=>'8','autoWrap'=>"true",'textAlign'=>'left'],
  306. ['name'=>'已完成产量','id'=>'wccl','width'=>'9','autoWrap'=>"true",'textAlign'=>'center'],
  307. ['name'=>'小时产能','id'=>'xscn','width'=>'8','autoWrap'=>"true",'textAlign'=>'center'],
  308. ['name'=>'机台状态','id'=>'status','width'=>'8','autoWrap'=>"true",'textAlign'=>'center'],
  309. ];
  310. if($row){
  311. foreach($row as $k=>$v){
  312. $result['rows'][$k]['jtbh']=$v['机台号'];
  313. $result['rows'][$k]['gdbh']=$v['在产工单号'];
  314. $result['rows'][$k]['gdmc']=$v['工单名称'];
  315. $result['rows'][$k]['gx']=$v['工序'];
  316. $result['rows'][$k]['jhcl'] = $v['计划产量'];
  317. $result['rows'][$k]['wccl'] = $v['已完成产量'];
  318. $result['rows'][$k]['xscn'] = $v['小时产能'];
  319. $result['rows'][$k]['status'] = $v['机台状态'];
  320. }
  321. }
  322. $res['status']=0;
  323. $res['msg']='';
  324. $res['data']=$result;
  325. return json($res);
  326. }
  327. //车间产量数据同步Redis
  328. public function jtClDbToRedis()
  329. {
  330. $jtClDbToRedis = [];
  331. //获取机台
  332. $rows = db('设备_基本资料')
  333. ->whereNotNull('sys_sbID')
  334. ->column('设备编号');
  335. //获取24小时内的hour组成数组
  336. $hour = date('H');
  337. $arr1 = range($hour+1,'23');
  338. $arr2 = range('00',$hour);
  339. $arr = array_merge($arr1,$arr2);
  340. $arr = array_map(function($num) {
  341. return sprintf("%02d", $num);
  342. }, $arr);
  343. foreach($rows as $value){
  344. $brr = [];
  345. //查询该机台24内的产量数据
  346. $row = db('设备_产量计酬')
  347. ->where('sys_rq', '>', date('Y-m-d H:i:s', strtotime('-23 hours')))
  348. ->where('sczl_jtbh', $value)
  349. ->group('LEFT(sys_rq, 13)')
  350. ->order('sys_rq')
  351. ->column('DATE_FORMAT(sys_rq, "%H") as hour, SUM(sczl_cl) as total_cl');
  352. //将产量数据赋值给小时
  353. foreach($arr as $v){
  354. $brr[$v] = array_key_exists($v,$row) ? $row[$v] : '0';
  355. }
  356. $jtClDbToRedis[$value] = $brr;
  357. }
  358. $bool = cache('jtClDbToRedis',$jtClDbToRedis);
  359. if($bool===false) $this->error('存入redis失败','','000002');
  360. $this->success('同步成功');
  361. }
  362. //机台月度走势对比图
  363. public function getJtClDbByRedis()
  364. {
  365. $req = $this->request->param();
  366. $jtbh = isset($req['jtbh']) && !empty($req['jtbh']) ? $req['jtbh'] : null;
  367. $row = cache('jtClDbToRedis') ?: [];
  368. // 统一班次刻度 08~23
  369. $categories = [];
  370. for ($i = 8; $i < 24; $i++) {
  371. $categories[] = sprintf('%02d', $i);
  372. }
  373. // 从缓存获取机台列表
  374. $machines = array_keys($row);
  375. // 如果缓存为空,从设备基本资料表获取所有机台
  376. if (empty($machines)) {
  377. $machines = \db('设备_基本资料')
  378. ->where('使用状态', 1)
  379. ->column('rtrim(设备编号)');
  380. }
  381. // 传了 jtbh 但缓存里没有该机台,也要加入
  382. if ($jtbh !== null && !in_array($jtbh, $machines)) {
  383. $machines[] = $jtbh;
  384. }
  385. if (empty($machines)) {
  386. return json([
  387. 'status' => 0,
  388. 'msg' => '',
  389. 'data' => [
  390. 'categories' => $categories,
  391. 'series' => [],
  392. ]
  393. ]);
  394. }
  395. // 取各机台最新一条的当前状态
  396. $statusList = \db('设备_产量采集')
  397. ->whereIn('设备编号', $machines)
  398. ->order('UniqId desc')
  399. ->column('当前状态', '设备编号');
  400. $series = [];
  401. foreach ($machines as $key) {
  402. if ($jtbh !== null && $key !== $jtbh) {
  403. continue;
  404. }
  405. $hours = $row[$key] ?? [];
  406. $data = [];
  407. foreach ($categories as $h) {
  408. $data[] = isset($hours[$h]) ? (int)$hours[$h] : 0;
  409. }
  410. $series[] = [
  411. 'name' => $key,
  412. 'status' => $statusList[$key] ?? '',
  413. 'data' => $data,
  414. ];
  415. }
  416. return json([
  417. 'status' => 0,
  418. 'msg' => '',
  419. 'data' => [
  420. 'categories' => $categories,
  421. 'series' => $series,
  422. ]
  423. ]);
  424. }
  425. /**
  426. * 班组工作中心负荷
  427. */
  428. public function bzDbToRedis()
  429. {
  430. $bzDbToRedis = [];
  431. //计算当天时间
  432. $sczlTime = date('Y-m-d H:i:s',time());
  433. if ($sczlTime>date('Y-m-d 00:00:00') && $sczlTime<date('Y-m-d 08:30:00')){
  434. $time = date('Y-m-d 00:00:00',time()-86400);
  435. }else{
  436. $time = date('Y-m-d 00:00:00');
  437. }
  438. //获取所有机台数据
  439. $machineList = db('设备_基本资料')
  440. ->where('sys_sbID','<>','')
  441. ->where('使用部门','in',['印刷车间','印后车间'])
  442. ->field('设备编号')
  443. ->select();
  444. //循环获取班组、工单、生产数据
  445. foreach ($machineList as $key=>$value){
  446. //获取正在生产工单信息
  447. $workOrder = \db('设备_产量采集')
  448. ->where('设备编号',$value['设备编号'])
  449. ->field('工单编号,印件号,工序号,工序名称,当前状态')
  450. ->order('UniqId desc')
  451. ->find();
  452. if (!$workOrder) continue;
  453. //获取生产信息
  454. $list = db('设备_产量计酬')
  455. ->alias('a')
  456. ->join('工单_基本资料 b','b.Gd_gdbh = a.sczl_gdbh')
  457. ->join('工单_工艺资料 c','a.sczl_gdbh = c.Gy0_gdbh AND a.sczl_yjno = c.Gy0_yjno AND a.sczl_gxh = c.Gy0_gxh')
  458. ->field([
  459. 'SUM(a.sczl_cl)' => '已完成产量',
  460. 'b.成品名称' => '产品名称',
  461. 'c.Gy0_计划接货数' => '计划产量',
  462. 'c.Gy0_小时产能' => '小时产能',
  463. 'a.sczl_bzdh' => '班组代号'
  464. ])
  465. ->where([
  466. 'a.sczl_jtbh' => $value['设备编号'],
  467. 'a.sczl_gdbh' => $workOrder['工单编号'],
  468. 'a.sczl_yjno' => $workOrder['印件号'],
  469. 'a.sczl_gxh' => $workOrder['工序号'],
  470. 'a.sczl_rq' => $time
  471. ])
  472. ->group('a.sczl_jtbh,a.sczl_bzdh')
  473. ->select();
  474. if (!$list) continue;
  475. foreach ($list as $v){
  476. $v['机台号'] = $value['设备编号'];
  477. $v['机台状态'] = $workOrder['当前状态'];
  478. $v['工单编号'] = $workOrder['工单编号'];
  479. $v['印件号'] = $workOrder['印件号'];
  480. $v['工序名称'] = $workOrder['工序名称'];
  481. array_push($bzDbToRedis,$v);
  482. }
  483. }
  484. $bool = cache('bzDbToRedis',$bzDbToRedis);
  485. if($bool===false) $this->error('存入redis失败','','000002');
  486. $this->success('同步成功');
  487. }
  488. //获取班组工作负荷数据
  489. public function getBzByRedis()
  490. {
  491. $row = cache('bzDbToRedis');
  492. $result['columns']=[
  493. ['name'=>'机台号','id'=>'jtbh','width'=>'30','autoWrap'=>"true",'textAlign'=>'left'],
  494. // ['name'=>'班组','id'=>'bzdh','width'=>'16','autoWrap'=>"true",'textAlign'=>'left'],
  495. ['name'=>'在产工单号','id'=>'gdbh','width'=>'40','textAlign'=>'left'],
  496. // ['name'=>'工单名称','id'=>'gdmc','width'=>'20','autoWrap'=>"true",'textAlign'=>'left'],
  497. // ['name'=>'印件号','id'=>'yjno','width'=>'16','autoWrap'=>"true",'textAlign'=>'left'],
  498. // ['name'=>'工序','id'=>'gx','width'=>'16','autoWrap'=>"true",'textAlign'=>'left'],
  499. // ['name'=>'计划产量','id'=>'jhcl','width'=>'14','autoWrap'=>"true",'textAlign'=>'left'],
  500. // ['name'=>'已完成产量','id'=>'wccl','width'=>'14','autoWrap'=>"true",'textAlign'=>'center'],
  501. // ['name'=>'小时产能','id'=>'xscn','width'=>'14','autoWrap'=>"true",'textAlign'=>'center'],
  502. ['name'=>'机台状态','id'=>'status','width'=>'30','autoWrap'=>"true",'textAlign'=>'center'],
  503. ];
  504. if($row){
  505. foreach($row as $k=>$v){
  506. $result['rows'][$k]['jtbh']=$v['机台号'];
  507. // $result['rows'][$k]['bzdh']=$v['班组代号'];
  508. $result['rows'][$k]['gdbh']=$v['工单编号'];
  509. // $result['rows'][$k]['gdmc']=$v['产品名称'];
  510. // $result['rows'][$k]['yjno']=$v['机台号'];
  511. // $result['rows'][$k]['gx']=$v['工序名称'];
  512. // $result['rows'][$k]['jhcl'] = $v['计划产量'];
  513. // $result['rows'][$k]['wccl'] = $v['已完成产量'];
  514. // $result['rows'][$k]['xscn'] = $v['小时产能'];
  515. $result['rows'][$k]['status'] = $v['机台状态'];
  516. }
  517. }else{
  518. $result['rows'][0]['jtbh']='';
  519. // $result['rows'][0]['bzdh']='';
  520. $result['rows'][0]['gdbh']='';
  521. // $result['rows'][0]['gdmc']='';
  522. // $result['rows'][0]['yjno']='';
  523. // $result['rows'][0]['gx']='';
  524. // $result['rows'][0]['jhcl'] = '';
  525. // $result['rows'][0]['wccl'] = '';
  526. // $result['rows'][0]['xscn'] = '';
  527. $result['rows'][0]['status'] = '';
  528. }
  529. $res['status']=0;
  530. $res['msg']='';
  531. $res['data']=$result;
  532. return json($res);
  533. }
  534. /**
  535. * 第二屏
  536. */
  537. /**
  538. * 决策支持月度设备运行数据统计->班组(接口)
  539. */
  540. public function TeamsgGoups()
  541. {
  542. // 获取设备编组信息
  543. $result = \db('设备_基本资料')
  544. ->whereNotNull('设备编组')
  545. ->group('设备编组')
  546. ->column('rtrim(设备编组) as 设备编组');
  547. $data = [
  548. "columns" => [
  549. ["id" => "subsys"]
  550. ],
  551. "rows" => []
  552. ];
  553. foreach ($result as $value) {
  554. $data["rows"][] = ["subsys" => $value];
  555. }
  556. $res = [
  557. 'status' => 0,
  558. 'msg' => '',
  559. 'data' => $data
  560. ];
  561. return json($res);
  562. }
  563. /**
  564. * 决策支持月度设备运行数据统计->机台运行工时汇总(接口)
  565. */
  566. public function MonthlyEquipment() {
  567. // 获取请求参数,默认为 '01、切纸机组'
  568. $postData = input('post.dependence.item.subsys', '01、切纸机组');
  569. // 查询条件
  570. $where = [];
  571. if (!empty($postData)) {
  572. $where['设备编组'] = $postData;
  573. }
  574. // 获取当前年月
  575. $currentYearMonth = date('Y-m');
  576. // 获取去年的同月
  577. $lastYearMonth = date('Y-m', strtotime('-1 year'));
  578. // 查询 **今年** 当月的数据
  579. $currentData = \db('设备_基本资料')
  580. ->alias('a')
  581. ->join('设备_产量计酬 b', 'a.设备编号 = b.sczl_jtbh')
  582. ->field([
  583. 'a.设备编号' => '设备编号',
  584. 'SUM(b.sczl_cl)' => '产量'
  585. ])
  586. ->where($where)
  587. ->whereTime('b.sczl_rq', '>=', $currentYearMonth . '-01') // 查询当前月的第一天
  588. ->whereTime('b.sczl_rq', '<', date('Y-m-d', strtotime('next month', strtotime($currentYearMonth . '-01')))) // 下个月的第一天
  589. ->group('a.设备编号')
  590. ->order('a.设备编号')
  591. ->select();
  592. // 查询 **去年** 同月的数据
  593. $lastYearData = \db('设备_基本资料')
  594. ->alias('a')
  595. ->join('设备_产量计酬 b', 'a.设备编号 = b.sczl_jtbh')
  596. ->field([
  597. 'a.设备编号' => '设备编号',
  598. 'SUM(b.sczl_cl)' => '产量'
  599. ])
  600. ->where($where)
  601. ->whereTime('b.sczl_rq', '>=', $lastYearMonth . '-01') // 查询去年的同月第一天
  602. ->whereTime('b.sczl_rq', '<', date('Y-m-d', strtotime('next month', strtotime($lastYearMonth . '-01')))) // 下个月的第一天
  603. ->group('a.设备编号')
  604. ->order('a.设备编号')
  605. ->select();
  606. // 格式化数据
  607. $categories = [];
  608. $currentYearSeries = [];
  609. $lastYearSeries = [];
  610. // 转换查询结果为关联数组,方便查找
  611. $currentYearMap = [];
  612. $lastYearMap = [];
  613. foreach ($currentData as $item) {
  614. $currentYearMap[$item['设备编号']] = $item['产量'];
  615. }
  616. foreach ($lastYearData as $item) {
  617. $lastYearMap[$item['设备编号']] = $item['产量'];
  618. }
  619. // 统一设备编号,确保数据完整
  620. $allDevices = array_unique(array_merge(array_keys($currentYearMap), array_keys($lastYearMap)));
  621. foreach ($allDevices as $device) {
  622. $categories[] = $device;
  623. $currentYearSeries[] = isset($currentYearMap[$device]) ? intval($currentYearMap[$device]) : 0; // 今年产量
  624. $lastYearSeries[] = isset($lastYearMap[$device]) ? intval($lastYearMap[$device]) : 0; // 去年产量
  625. }
  626. // 组织返回数据
  627. $response = [
  628. 'status' => 0,
  629. 'msg' => '',
  630. 'data' => [
  631. 'categories' => $categories,
  632. 'series' => [
  633. [
  634. 'name' => '今年产量',
  635. 'data' => $currentYearSeries,
  636. 'type' => 'bar',
  637. 'yAxisIndex' => 0
  638. ],
  639. [
  640. 'name' => '去年产量',
  641. 'data' => $lastYearSeries,
  642. 'type' => 'bar',
  643. 'yAxisIndex' => 0
  644. ]
  645. ]
  646. ]
  647. ];
  648. return json($response);
  649. }
  650. /**
  651. * 决策支持月度设备运行数据统计->机台运行工时汇总列表(接口)
  652. */
  653. public function Equipmenttable(){
  654. // 获取请求数据
  655. $postData = input('post.dependence.item.subsys', '01、切纸机组');
  656. $where = [];
  657. if (!empty($postData)) {
  658. $where['设备编组'] = $postData;
  659. }
  660. // 获取当前月份
  661. $mouth = date_create_from_format('Ym', date('Ym'))->format('Y-m');
  662. // 查询设备数据
  663. $list = \db('设备_基本资料')->alias('a')
  664. ->join('设备_产量计酬 b', 'a.设备编号 = b.sczl_jtbh')
  665. ->field([
  666. 'a.设备编号' => '设备编号',
  667. 'rtrim(a.设备名称)' => '设备名称',
  668. 'SUM(b.sczl_cl)' => '产量',
  669. 'SUM(b.sczl_设备运行工时)' => '设备运行工时',
  670. 'SUM(b.sczl_保养工时)' => '保养工时',
  671. 'SUM(b.sczl_打样总工时)' => '打样总工时',
  672. 'SUM(b.sczl_打样工时)' => '打样补产工时',
  673. 'SUM(b.sczl_装版总工时)' => '装板总工时',
  674. 'SUM(b.sczl_装版工时)' => '装板补产工时',
  675. 'SUM(b.sczl_异常停机工时)' => '异常停机工时'
  676. ])
  677. ->where($where)
  678. ->whereTime('b.sczl_rq', $mouth)
  679. ->group('a.设备编号')
  680. ->order('a.设备编号')
  681. ->select();
  682. $rows = [];
  683. foreach ($list as $item) {
  684. $rows[] = [
  685. 'subsys' => $item['设备编号'],
  686. 'module' => $item['设备名称'],
  687. 'business' => $item['产量'],
  688. 'sbyxgs' => $item['设备运行工时'],
  689. 'dyzgs' => $item['打样总工时'],
  690. 'dybcgs' => $item['打样补产工时'],
  691. 'zbzgs' => $item['装板总工时'],
  692. 'zbbcgs' => $item['装板补产工时'],
  693. 'yctjgs' => $item['异常停机工时']
  694. ];
  695. }
  696. // 最终响应格式
  697. $response = [
  698. 'status' => 0,
  699. 'msg' => '',
  700. 'data' => [
  701. 'total' => count($rows),
  702. 'columns' => [
  703. ['name' => '设备编号','id' => 'subsys'],
  704. ['name' => '设备名称','id' => 'module'],
  705. ['name' => '产量','id' => 'business'],
  706. ['name' => '设备运行工时','id' => 'sbyxgs'],
  707. ['name' => '打样总工时','id' => 'dyzgs'],
  708. ['name' => '打样补产工时','id' => 'dybcgs'],
  709. ['name' => '装板总工时','id' => 'zbzgs'],
  710. ['name' => '装板补产工时','id' => 'zbbcgs'],
  711. ['name' => '异常停机工时','id' => 'yctjgs',]
  712. ],
  713. // 'superHeaders' => [
  714. // [
  715. // [
  716. // 'name' => '设备信息',
  717. // 'colspan' => 3
  718. // ],
  719. // [
  720. // 'name' => '工时统计',
  721. // 'colspan' => 2
  722. // ]
  723. // ]
  724. // ],
  725. 'rows' => $rows
  726. ]
  727. ];
  728. return json($response);
  729. }
  730. /**
  731. * 工序产出率月统计报表(接口)
  732. */
  733. public function Monthlystatistical(){
  734. }
  735. /**
  736. * 工序产出率月统计报表(缓存)
  737. */
  738. public function Workorderexcessloss_redis(){
  739. //链接定义 Redis
  740. // $redis = redis();
  741. // $redis_key = md5('Workorderexcessloss_redis');
  742. //获取当月日期
  743. $date = date('Y-m');
  744. $where = [];
  745. $where['a.jjcp_sj' ] = array('like',$date.'%');
  746. $sql = db('物料_收发记录')
  747. ->alias('d')
  748. ->where('d.仓库编号', 'Y101')
  749. ->group('d.st_gdbh, d.cpdh')
  750. ->field('d.st_gdbh, d.cpdh,d.st_rq, SUM(d.st_sl) as 实际投料,d.仓库编号')
  751. ->buildSql();
  752. $data = db('成品入仓')->alias('a')
  753. ->join('工单_基本资料 b', 'a.jjcp_gdbh = b.Gd_gdbh', 'left')
  754. ->join('工单_印件资料 c', 'a.jjcp_gdbh = c.Yj_Gdbh AND a.jjcp_cpdh = c.yj_Yjdh', 'left')
  755. ->join([$sql => 'd'], 'a.jjcp_gdbh = d.st_gdbh AND a.jjcp_cpdh = d.cpdh', 'left')
  756. ->where($where)
  757. ->where('d.仓库编号','Y101')
  758. // ->whereRaw('LEFT(a.成品编码, CASE WHEN a.成品编码 REGEXP "[a-zA-Z]" THEN 5 ELSE 4 END) = ?', [$params['code']])
  759. ->field('a.jjcp_gdbh as Gd_gdbh, a.jjcp_yjno, rtrim(a.jjcp_cpdh) as 成品编码, rtrim(a.jjcp_cpmc) as 成品名称,
  760. a.jjcp_sj, a.jjcp_smb, b.计量单位, b.Gd_khdh, d.实际投料, c.yj_Yjno, c.yj_ls, c.yj_ks, c.质量考核')
  761. ->group('a.jjcp_gdbh, a.jjcp_yjno')
  762. ->order('b.Gd_khdh, a.成品编码 asc, a.jjcp_yjno,d.st_rq desc')
  763. // ->page($pages)
  764. // ->limit($limit)
  765. ->select();
  766. foreach ($data as $key => $value){
  767. //查出成品数量及日期
  768. $cp_sql = "SELECT SUM(jjcp_sl) as cp_sl,MAX(jjcp_sj) as jjcp_sj FROM `成品入仓` WHERE jjcp_gdbh = '{$value['Gd_gdbh']}' AND jjcp_yjno = '{$value['jjcp_yjno']}' GROUP BY jjcp_gdbh,jjcp_yjno";
  769. $cp_data = Db::query($cp_sql);
  770. $data[$key]['warehousing_num'] = $cp_data[0]['cp_sl'];
  771. $data[$key]['warehousing_date'] = substr($cp_data[0]['jjcp_sj'],0,10);
  772. //查出进入超节损的工序,有上报产量的工序就进入超节损
  773. $gxh_sql = "SELECT sczl_gxh FROM
  774. (SELECT sczl_gxh FROM 设备_产量计酬 WHERE sczl_gdbh = '{$value['Gd_gdbh']}' AND sczl_yjno = '{$value['jjcp_yjno']}'
  775. UNION SELECT sczl_gxh FROM db_sczl WHERE sczl_gdbh = '{$value['Gd_gdbh']}' AND sczl_yjno = '{$value['jjcp_yjno']}') AS combined_result";
  776. $gxh_arr = Db::query($gxh_sql);
  777. $gxh_values = array_column($gxh_arr, 'sczl_gxh');
  778. // $gy_data =db('工单_工艺资料')
  779. // ->where('Gy0_gdbh',$value['Gd_gdbh'])
  780. // ->where('Gy0_yjno',$value['jjcp_yjno'])
  781. // ->where('Gy0_gxh','in',$gxh_values)
  782. // ->field('Gy0_gxmc,Gy0_计划接货数,Gy0_计划损耗')
  783. // ->select();
  784. $gy_data =db('工单_工艺资料')
  785. ->alias('a')
  786. ->field([
  787. 'a.Gy0_yjno', 'a.Gy0_gxh', 'RTRIM(a.Gy0_gxmc) as Gy0_gxmc','RTRIM(a.Add_gxmc) as Add_gxmc','a.Gy0_ks', 'a.Gy0_ls', 'a.Gy0_计划接货数',
  788. 'a.Gy0_计划损耗', 'a.超节损承担比例','SUM(b.sczl_zcfp) as total_fp','SUM(b.sczl_cl) as total_cl','SUM(c.sczl_cl) as cl','SUM(c.sczl_fp) as fp',
  789. ])
  790. ->join('设备_产量计酬 b', 'a.Gy0_gdbh = b.sczl_gdbh AND a.Gy0_yjno = b.sczl_yjno AND a.Gy0_gxh = b.sczl_gxh','left')
  791. ->join('db_sczl c', 'a.Gy0_gdbh = c.sczl_gdbh AND a.Gy0_yjno = c.sczl_yjno AND a.Gy0_gxh = c.sczl_gxh','left')
  792. ->where([
  793. 'a.Gy0_gdbh' => $value['Gd_gdbh'],
  794. 'a.Gy0_yjno' =>$value['jjcp_yjno'],
  795. 'a.Gy0_gxh' => ['in', $gxh_values]
  796. ])
  797. ->group('a.Gy0_yjno,a.Gy0_gxh')
  798. ->select();
  799. $arr = [];
  800. $plan_loss = [];//工单计划损耗
  801. $machine_plan_loss = 0;//机检计划损耗
  802. foreach ($gy_data as $k=>$v){
  803. if ($v['Gy0_计划接货数'] > 0){
  804. $rate = round($v['Gy0_计划损耗'] / $v['Gy0_计划接货数'],5);
  805. }else{
  806. $rate = 0;
  807. }
  808. $arr[$k] = floor($rate * 10000) /10000;
  809. $plan_loss[$k] = $v['Gy0_计划损耗'];
  810. // if (strpos($v['Gy0_gxmc'],'机检') !== false){
  811. // $machine_plan_loss = $v['Gy0_计划损耗'];
  812. // }
  813. }
  814. if ((int)$value['yj_ls'] > 0){
  815. $value['实际投料'] = number_format(((int)$value['实际投料']/10000)*$value['yj_ls']*$value['yj_ks'],4);
  816. }else{
  817. $value['实际投料'] = number_format($value['实际投料']/10000,4);
  818. }
  819. $data[$key]['实际投料'] = $value['实际投料'];
  820. if ($value['实际投料'] >0 ){
  821. $target_rate = (1-array_sum($arr))*100;
  822. $data[$key]['target_rate'] =$target_rate.'%'; //目标合格率
  823. $real_rate = $cp_data[0]['cp_sl'] / ($value['实际投料'] * 10000) *100;
  824. $data[$key]['real_rate'] = number_format($real_rate,2) . '%';//实际合格率
  825. }else{
  826. $data[$key]['target_rate'] = ''; //目标合格率
  827. $data[$key]['real_rate'] = '';//实际合格率
  828. }
  829. //制程废品
  830. $zzfp_data =db('设备_产量计酬')->where('sczl_gdbh',$value['Gd_gdbh'])->where('sczl_yjno',$value['jjcp_yjno'])->field('SUM(sczl_zcfp) as sczl_zcfp')->select();
  831. $data[$key]['zcfp'] = $zzfp_data[0]['sczl_zcfp'] * $value['yj_ls'];//制程废品
  832. //废品合计
  833. $wasteTotal = db('db_qczl')->where('qczl_gdbh',$value['Gd_gdbh'])->where('qczl_yjno',$value['jjcp_yjno'])->sum('qczl_fp');
  834. if (empty($wasteTotal)){
  835. $wasteTotal = 0;
  836. }
  837. $data[$key]['废品合计'] = $wasteTotal + $data[$key]['zcfp'];//废品合计
  838. $data[$key]['工单无形损'] = (int)($value['实际投料'] *10000) - $cp_data[0]['cp_sl'] - $data[$key]['废品合计'];//工单无形损
  839. $out_sql = "SELECT fp_sl1,fp_sl2,fp_sl3,fp_sl4,fp_sl5,fp_sl6,fp_sl7,fp_sl8,fp_sl9,fp_sl10,fp_sl11,fp_sl12,fp_sl13,
  840. fp_lb1,fp_lb2,fp_lb3,fp_lb4,fp_lb5,fp_lb6,fp_lb7,fp_lb8,fp_lb9,fp_lb10,fp_lb11,fp_lb12,fp_lb13,
  841. fp_bh1,fp_bh2,fp_bh3,fp_bh4,fp_bh5,fp_bh6,fp_bh7,fp_bh8,fp_bh9,fp_bh10,fp_bh11,fp_bh12,fp_bh13,
  842. fp_gxmc1,fp_gxmc2,fp_gxmc3,fp_gxmc4,fp_gxmc5,fp_gxmc6,fp_gxmc7,fp_gxmc8,fp_gxmc9,fp_gxmc10,fp_gxmc11,fp_gxmc12,fp_gxmc13
  843. FROM db_qczl WHERE qczl_gdbh = '{$value['Gd_gdbh']}' AND qczl_yjno = '{$value['jjcp_yjno']}'";
  844. $waste_out = Db::query($out_sql);
  845. $list = []; //外发废数组
  846. $quality = [];//质检废数组
  847. $material = [];//材料废数组
  848. $change = [];//零头处理数组
  849. $machine = [];//机检废品数组
  850. $j = 0;
  851. $m = 0;
  852. $n = 0;
  853. $l = 0;
  854. $o = 0;
  855. foreach ($waste_out as $entry) {
  856. for ($i = 1; $i <= 13; $i++) {
  857. $labelKey = "fp_lb" . $i;
  858. $bhKey = "fp_bh" . $i;
  859. $gxmcKey = "fp_gxmc" . $i;
  860. $slKey = "fp_sl" . $i;
  861. // if (!empty($entry[$labelKey])) {
  862. // if (substr($entry[$gxmcKey],0,2) == '99'){
  863. // $list[$j]= $entry[$slKey];
  864. // $j++;
  865. // }
  866. // if (substr($entry[$labelKey],0,1) == 'L' ){
  867. // $material[$n]= $entry[$slKey];
  868. // $n++;
  869. // }
  870. // if (substr($entry[$labelKey],0,3) == 'M04' ){
  871. // $change[$l]= $entry[$slKey];
  872. // $l++;
  873. // }
  874. // }
  875. // if (!empty($entry[$bhKey])) {
  876. // if ($entry[$bhKey] != '000000' && $entry[$slKey] > 0){
  877. // $quality[$m]= $entry[$slKey];
  878. // $m++;
  879. // }
  880. //// if ($entry[$bhKey] != '000000' && substr($entry[$labelKey],0,3) == 'K01'){
  881. //// $machine[$o]= $entry[$slKey];
  882. //// $o++;
  883. //// }
  884. // }
  885. if (!empty($entry[$labelKey])) {
  886. if (substr($entry[$labelKey],0,3) == 'M04'){
  887. $list[$j]= $entry[$slKey];
  888. $j++;
  889. }
  890. if (substr($entry[$labelKey],0,1) == 'L' ){
  891. $material[$n]= $entry[$slKey];
  892. $n++;
  893. }
  894. if (substr($entry[$labelKey],0,3) == 'M01' ){
  895. $change[$l]= $entry[$slKey];
  896. $l++;
  897. }
  898. }
  899. // if (!empty($entry[$bhKey])) {
  900. // if ($entry[$bhKey] != '000000' && $entry[$slKey] > 0){
  901. $quality[$m]= $entry[$slKey];
  902. $m++;
  903. // }
  904. // }
  905. }
  906. }
  907. $machine_waste = array_sum($machine);//机检废品
  908. if ($data[$key]['Gd_gdbh'] == '2401746'){
  909. halt($machine_waste);
  910. }
  911. $data[$key]['工单计划损耗'] = array_sum($plan_loss);//工单计划损耗
  912. /**
  913. * 奖惩系数及金额
  914. * 奖惩金额 = 工单节损数 * 0.02(0.09)
  915. * 工单节损数 = 工单计划损耗 - 废品总数 - 机检节损数
  916. * 机检节损数 = 机检计划损耗 - 机检质检废品
  917. */
  918. $machine_loss = $machine_plan_loss - $machine_waste;
  919. $order_loss = $data[$key]['工单计划损耗'] - ($data[$key]['废品合计'] - array_sum($list));
  920. if (strpos($data[$key]['成品名称'],'小盒')){
  921. $moneyRate = 0.02;
  922. }else{
  923. $moneyRate = 0.09;
  924. }
  925. $reward = $order_loss * $moneyRate;
  926. $data[$key]['reward_rate'] = '1';
  927. if ($data[$key]['质量考核'] === 0){
  928. $data[$key]['reward_money'] = number_format($reward,2);
  929. }else{
  930. $data[$key]['reward_money'] = '';
  931. }
  932. // $data[$key]['reward_money'] = '';
  933. $data[$key]['材料废'] = array_sum($material); //材料废
  934. $data[$key]['零头处理'] = array_sum($change); //零头处理
  935. $data[$key]['外发废'] = array_sum($list);//外发废
  936. $data[$key]['分摊废'] = '';//分摊废
  937. $data[$key]['工单质检废'] = array_sum($quality); //质检废
  938. }
  939. // if($data){
  940. // //将查询结果存入 Redis 缓存中
  941. // $redis->set($redis_key, json_encode($data));
  942. // echo date("Y-m-d H:i:s").' 存进去了';
  943. // return json($data);
  944. // }
  945. $bool = cache('cacheWorkorderexcessloss_redis',$data);
  946. if($bool===false) $this->error('存入redis失败','','000002');
  947. $this->success('存入成功');
  948. }
  949. /**
  950. * 工序产出率月统计报表(接口)
  951. */
  952. public function Workorderexcessloss(){
  953. $data = cache('cacheWorkorderexcessloss_redis');
  954. // $redis = redis();
  955. // $data = json_decode($redis->get(md5('Workorderexcessloss_redis')),true);
  956. $rows = [];
  957. foreach ($data as $item) {
  958. $rows[] = [
  959. 'Gd_gdbh' => $item['Gd_gdbh'],
  960. 'cpmc' => $item['成品名称'],
  961. 'jjcp_sj' => $item['jjcp_sj'],
  962. 'target_rate' => $item['target_rate'],
  963. 'real_rate' => $item['real_rate'],
  964. ];
  965. }
  966. // 最终响应格式
  967. $response = [
  968. 'status' => 0,
  969. 'msg' => '',
  970. 'data' => [
  971. 'columns' => [
  972. ['name' => '工单编号','width'=>'10','id' => 'Gd_gdbh'],
  973. ['name' => '产品名称','width'=>'38','id' => 'cpmc'],
  974. ['name' => '入仓时间','width'=>'20','id' => 'jjcp_sj'],
  975. ['name' => '目标合格率','width'=>'16','id' => 'target_rate'],
  976. ['name' => '实际合格率','width'=>'16','id' => 'real_rate'],
  977. ],
  978. 'rows' => $rows
  979. ]
  980. ];
  981. return json($response);
  982. }
  983. /**
  984. * 工单超节损(接口)Excessive_Structural
  985. * 大屏接口,默认近两个月数据
  986. */
  987. public function Excessive_Structural()
  988. {
  989. $where = [];
  990. // 默认近两个月
  991. $endDate = date('Y-m');
  992. $startDate = date('Y-m', strtotime('-2 months'));
  993. $where['a.jjcp_sj'] = ['between', [$startDate . '-01', $endDate . '-31']];
  994. $field = '
  995. b.sys_ny,
  996. a.jjcp_gdbh AS Gd_gdbh,
  997. a.jjcp_yjno AS jjcp_yjno,
  998. c.yj_cpdh AS 成品编码,
  999. c.yj_yjmc AS 成品名称,
  1000. b.`实际投料`,
  1001. b.`计量单位`,
  1002. DATE(a.jjcp_sj) AS warehousing_date,
  1003. total_sl.warehousing_num,
  1004. SUM(DISTINCT b.`计划损耗`) AS `工单计划损耗`,
  1005. b.`废品合计`,
  1006. SUM(b.`CjsJe`) AS reward_money,
  1007. b.`印件无形损` AS 工单无形损,
  1008. b.`材料废`,
  1009. b.`零头处理`,
  1010. b.`外发废`,
  1011. SUM(b.`班组制程废品`) AS zcfp,
  1012. `工单质检废` AS 工单质检废,
  1013. c.质量考核,
  1014. c.工单类型
  1015. ';
  1016. $subQuery = \db('成品入仓')
  1017. ->alias('a_sub')
  1018. ->field('a_sub.jjcp_gdbh AS Gd_gdbh_sub, a_sub.jjcp_yjno AS jjcp_yjno_sub, SUM(a_sub.jjcp_sl) AS warehousing_num')
  1019. ->group('a_sub.jjcp_gdbh, a_sub.jjcp_yjno')
  1020. ->buildSql();
  1021. $list = \db('成品入仓')
  1022. ->alias('a')
  1023. ->join('工单_质量考核汇总 b', 'a.jjcp_gdbh = b.Gy0_gdbh AND a.jjcp_yjno = b.印件及工序', 'left')
  1024. ->join('工单_印件资料 c', 'a.jjcp_gdbh = c.Yj_Gdbh AND a.jjcp_yjno = c.yj_Yjno')
  1025. ->join([$subQuery => 'total_sl'], 'a.jjcp_gdbh = total_sl.Gd_gdbh_sub AND a.jjcp_yjno = total_sl.jjcp_yjno_sub', 'left')
  1026. ->field($field)
  1027. ->where($where)
  1028. ->where('a.jjcp_smb', 'like', '末%')
  1029. ->group('Gd_gdbh,jjcp_yjno')
  1030. ->order('warehousing_date DESC')
  1031. ->select();
  1032. $result['columns'] = [
  1033. ['name' => '工单编号', 'id' => 'Gd_gdbh', 'width' => '15', 'textAlign' => 'left'],
  1034. // ['name' => '印件号', 'id' => 'jjcp_yjno', 'width' => '15', 'textAlign' => 'left'],
  1035. // ['name' => '成品编码', 'id' => '成品编码', 'width' => '12', 'textAlign' => 'left'],
  1036. ['name' => '成品名称', 'id' => '成品名称', 'width' => '35', 'autoWrap' => 'true', 'textAlign' => 'left'],
  1037. // ['name' => '实际投料', 'id' => '实际投料', 'width' => '10', 'textAlign' => 'center'],
  1038. // ['name' => '计量单位', 'id' => '计量单位', 'width' => '8', 'textAlign' => 'center'],
  1039. ['name' => '入仓日期', 'id' => 'warehousing_date', 'width' => '15', 'textAlign' => 'center'],
  1040. ['name' => '入仓数量', 'id' => 'warehousing_num', 'width' => '15', 'textAlign' => 'center'],
  1041. // ['name' => '计划损耗', 'id' => '工单计划损耗', 'width' => '10', 'textAlign' => 'center'],
  1042. // ['name' => '废品合计', 'id' => '废品合计', 'width' => '10', 'textAlign' => 'center'],
  1043. // ['name' => '奖惩金额', 'id' => 'reward_money', 'width' => '10', 'textAlign' => 'center'],
  1044. // ['name' => '无形损', 'id' => '工单无形损', 'width' => '10', 'textAlign' => 'center'],
  1045. // ['name' => '材料废', 'id' => '材料废', 'width' => '8', 'textAlign' => 'center'],
  1046. // ['name' => '零头处理', 'id' => '零头处理', 'width' => '8', 'textAlign' => 'center'],
  1047. // ['name' => '外发废', 'id' => '外发废', 'width' => '8', 'textAlign' => 'center'],
  1048. // ['name' => '质检废', 'id' => '工单质检废', 'width' => '8', 'textAlign' => 'center'],
  1049. ['name' => '目标合格率', 'id' => 'target_rate', 'width' => '15', 'textAlign' => 'center'],
  1050. ['name' => '实际合格率', 'id' => 'real_rate', 'width' => '15', 'textAlign' => 'center'],
  1051. // ['name' => '质量考核', 'id' => '考核', 'width' => '8', 'textAlign' => 'center'],
  1052. ];
  1053. foreach ($list as $key => $value) {
  1054. $status = \db('工单_印件资料')
  1055. ->where('Yj_Gdbh', $value['Gd_gdbh'])
  1056. ->where('yj_Yjno', $value['jjcp_yjno'])
  1057. ->value('质量考核');
  1058. $list[$key]['工单质检废'] = $value['废品合计'] - $value['zcfp'];
  1059. $list[$key]['质量考核'] = $status;
  1060. $list[$key]['考核'] = ($status == 1) ? '否' : '是';
  1061. if ($value['实际投料'] !== null) {
  1062. $actualMaterial = $value['实际投料'] * 10000;
  1063. $list[$key]['target_rate'] = $this->_calculateRate($actualMaterial, $value['工单计划损耗']);
  1064. $list[$key]['real_rate'] = $value['warehousing_num'] / ($value['实际投料'] * 10000) * 100;
  1065. $list[$key]['real_rate'] = number_format($list[$key]['real_rate'], 2) . '%';
  1066. } else {
  1067. $list[$key]['target_rate'] = null;
  1068. $list[$key]['real_rate'] = null;
  1069. }
  1070. $list[$key]['reward_rate'] = '1';
  1071. }
  1072. $result['rows'] = $list;
  1073. return json([
  1074. 'status' => 0,
  1075. 'msg' => '',
  1076. 'data' => $result
  1077. ]);
  1078. }
  1079. /**
  1080. * 计算目标合格率
  1081. */
  1082. private function _calculateRate($actual, $loss)
  1083. {
  1084. if ($actual == 0) {
  1085. return '0.00%';
  1086. }
  1087. $rate = ($actual - $loss) / $actual * 100;
  1088. return number_format($rate, 2) . '%';
  1089. }
  1090. /**
  1091. * 生产中心合格率统计(缓存)
  1092. * 同步数据到 Redis
  1093. */
  1094. public function ProcessOutputRateCache()
  1095. {
  1096. $param = $this->request->param();
  1097. $year = isset($param['year']) && !empty($param['year']) ? $param['year'] : date('Y');
  1098. // 1. 获取工序分组
  1099. $sist = ['胶印车间', '凹丝印车间', '印后车间', '检验车间'];
  1100. $processOrder = \db('设备_基本资料')
  1101. ->whereNotNull('设备编组')
  1102. ->whereIn('使用部门', $sist)
  1103. ->group('设备编组')
  1104. ->column('RTRIM(设备编组) as 设备编组');
  1105. if (empty($processOrder)) {
  1106. $this->success('成功', []);
  1107. }
  1108. // 2. 初始化结果数组(按工序)
  1109. $result = [];
  1110. foreach ($processOrder as $process) {
  1111. $key = substr($process, 5, 6);
  1112. $result[$key] = [];
  1113. for ($i = 1; $i <= 12; $i++) {
  1114. $result[$key][str_pad($i, 2, '0', STR_PAD_LEFT) . '月'] = '';
  1115. }
  1116. $result[$key]['平均值'] = '0.00%';
  1117. }
  1118. // 3. 汇总所有年份内数据,一次查出所有"工序-月份"数据
  1119. $dataList = \db('工单_质量考核汇总')
  1120. ->alias('a')
  1121. ->join('工单_工艺资料 b', 'a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
  1122. ->join('工单_印件资料 c', 'a.Gy0_gdbh = c.Yj_Gdbh AND a.印件及工序 = c.yj_Yjno')
  1123. ->join('成品入仓 d', 'a.Gy0_gdbh = d.jjcp_gdbh AND a.印件及工序 = d.jjcp_yjno')
  1124. ->join('设备_基本资料 e', 'a.sczl_jtbh = e.设备编号')
  1125. ->whereIn('e.设备编组', $processOrder)
  1126. ->where('d.jjcp_smb', 'like', '末%')
  1127. ->where('a.sys_ny', 'like', $year . '%')
  1128. ->field([
  1129. 'e.设备编组',
  1130. 'a.sys_ny',
  1131. 'SUM(a.班组制程废品/ a.联数) AS 制程废品',
  1132. 'SUM(a.班组质检废品/ a.联数) AS 质检废品',
  1133. 'SUM(a.班组产量/ a.联数) AS 产量'
  1134. ])
  1135. ->group('e.设备编组, a.sys_ny')
  1136. ->select();
  1137. // 4. 处理统计数据
  1138. $yearTotal = [];
  1139. foreach ($dataList as $item) {
  1140. $processKey = substr($item['设备编组'], 5, 6);
  1141. $monthKey = substr($item['sys_ny'], 4, 2) . '月';
  1142. $output = floatval($item['产量']);
  1143. $waste = floatval($item['制程废品']) + floatval($item['质检废品']);
  1144. $rate = $output > 0 ? (1 - $waste / $output) * 100 : 0;
  1145. $result[$processKey][$monthKey] = sprintf('%.2f%%', $rate);
  1146. if (!isset($yearTotal[$processKey])) {
  1147. $yearTotal[$processKey] = ['totalOutput' => 0, 'totalWaste' => 0];
  1148. }
  1149. $yearTotal[$processKey]['totalOutput'] += $output;
  1150. $yearTotal[$processKey]['totalWaste'] += $waste;
  1151. }
  1152. // 5. 计算年均合格率
  1153. foreach ($yearTotal as $processKey => $val) {
  1154. $totalOutput = $val['totalOutput'];
  1155. $totalWaste = $val['totalWaste'];
  1156. if ($totalOutput > 0) {
  1157. $avgRate = (1 - $totalWaste / $totalOutput) * 100;
  1158. $result[$processKey]['平均值'] = sprintf('%.2f%%', $avgRate);
  1159. } else {
  1160. $result[$processKey]['平均值'] = '0.00%';
  1161. }
  1162. }
  1163. // 6. 存入 Redis
  1164. $bool = cache('ProcessOutputRate_' . $year, $result);
  1165. if ($bool === false) {
  1166. $this->error('存入redis失败', '', '000002');
  1167. }
  1168. $this->success('同步成功');
  1169. }
  1170. /**
  1171. * 质检废品率统计(合格率图表)
  1172. * 从 Redis 读取数据返回 Sugar BI 图表格式(本月、有数据的前10工序)
  1173. */
  1174. public function ProcessOutputRate()
  1175. {
  1176. $param = $this->request->param();
  1177. $year = isset($param['year']) && !empty($param['year']) ? $param['year'] : date('Y');
  1178. $month = date('m'); // 本月
  1179. $data = cache('ProcessOutputRate_' . $year);
  1180. if (empty($data)) {
  1181. return json([
  1182. 'status' => 0,
  1183. 'msg' => '',
  1184. 'data' => [
  1185. 'categories' => [],
  1186. 'series' => []
  1187. ]
  1188. ]);
  1189. }
  1190. // 过滤掉本月无数据的工序,只保留有数据的
  1191. $hasDataKeys = [];
  1192. foreach ($data as $process => $row) {
  1193. $val = $row[$month . '月'] ?? '';
  1194. if ($val !== '' && $val !== null && $val !== 0) {
  1195. $hasDataKeys[] = $process;
  1196. }
  1197. }
  1198. // 只取前 10 道有数据的工序
  1199. $processKeys = array_slice($hasDataKeys, 0, 10);
  1200. // 本月刻度
  1201. $categories = [$month . '月'];
  1202. $series = [];
  1203. foreach ($processKeys as $process) {
  1204. $row = $data[$process] ?? [];
  1205. $val = $row[$month . '月'] ?? '';
  1206. if ($val !== '' && $val !== null) {
  1207. $value = floatval(str_replace('%', '', $val));
  1208. } else {
  1209. $value = 0;
  1210. }
  1211. $series[] = [
  1212. 'name' => $process,
  1213. 'data' => [$value],
  1214. ];
  1215. }
  1216. return json([
  1217. 'status' => 0,
  1218. 'msg' => '',
  1219. 'data' => [
  1220. 'categories' => $categories,
  1221. 'series' => $series,
  1222. ]
  1223. ]);
  1224. }
  1225. /**
  1226. * 年度质检废品率统计(表格)
  1227. * @return void
  1228. */
  1229. public function QualityInspectionList()
  1230. {
  1231. $param = $this->request->param();
  1232. $year = isset($param['year']) && !empty($param['year']) ? $param['year'] : date('Y');
  1233. $where = ['年月' => ['like', $year . '%']];
  1234. // 按废品类别统计数量
  1235. $wasteList = \db('rec_月度废品汇总')
  1236. ->where($where)
  1237. ->field('废品类别, sum(废品数量) as 废品数量')
  1238. ->group('废品类别')
  1239. ->order('废品数量 desc')
  1240. ->select();
  1241. // 总量
  1242. $total = 0;
  1243. foreach ($wasteList as $item) {
  1244. $total += $item['废品数量'];
  1245. }
  1246. // 构建 Sugar BI 表格格式
  1247. $result = [
  1248. 'columns' => [
  1249. ['name' => '废品名称', 'id' => '废品名称类别', 'width' => '40', 'autoWrap' => "true", 'textAlign' => 'left'],
  1250. ['name' => '数量', 'id' => '数量', 'width' => '30', 'autoWrap' => "true", 'textAlign' => 'center'],
  1251. ['name' => '占比', 'id' => '占比', 'width' => '30', 'autoWrap' => "true", 'textAlign' => 'center'],
  1252. ],
  1253. 'rows' => [],
  1254. ];
  1255. foreach ($wasteList as $item) {
  1256. $ratio = $total > 0 ? round($item['废品数量'] / $total * 100, 2) . '%' : '0%';
  1257. $result['rows'][] = [
  1258. '废品名称类别' => $item['废品类别'],
  1259. '数量' => $item['废品数量'],
  1260. '占比' => $ratio,
  1261. ];
  1262. }
  1263. $resOut['status'] = 0;
  1264. $resOut['msg'] = '';
  1265. $resOut['data'] = $result;
  1266. return json($resOut);
  1267. }
  1268. }