Index.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 首页接口
  7. */
  8. class Index extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. /**
  13. * 首页
  14. *
  15. */
  16. public function index()
  17. {
  18. $this->success('请求成功');
  19. }
  20. public function GetOrderNumber(){
  21. if ($this->request->isGet() === false){
  22. $this->error('请求错误');
  23. }
  24. $nowTime = date('Y-m-d H:i:s',time());
  25. $oldTime = (date('Y')-1).'-'.date('m-d H:i:s');
  26. $where =[
  27. 'Mod_rq' => ['between',[$oldTime,$nowTime]],
  28. '行号' => '1'
  29. ];
  30. //按月份查询工单信息
  31. $monthData = \db('工单_基本资料')
  32. ->where('Mod_rq', 'between', [$oldTime, $nowTime])
  33. ->field('DATE_FORMAT(Mod_rq, "%Y-%m") as month, COUNT(*) as count')
  34. ->group('month')
  35. ->order('month')
  36. ->select();
  37. // 转换为接口需要的格式
  38. $result = [
  39. "status" => 0,
  40. "msg" => "",
  41. "data" => [
  42. "categories" => [],
  43. "series" => [
  44. [
  45. "name" => "工单数量",
  46. "data" => []
  47. ]
  48. ]
  49. ]
  50. ];
  51. // 填充数据
  52. foreach ($monthData as $item) {
  53. $result['data']['categories'][] = $item['month'];
  54. $result['data']['series'][0]['data'][] = (int)$item['count'];
  55. }
  56. // 返回JSON格式数据
  57. return json($result);
  58. }
  59. //计划/排程/制程工单数量同步Redis
  60. public function numDbToRedis()
  61. {
  62. $numDbToRedis = [];
  63. //获取计划中的订单数量
  64. $jhz = db('工单_基本资料')
  65. ->where('gd_statu','like','%计划中%')
  66. ->count('Gd_gdbh');
  67. if($jhz!==false){
  68. $numDbToRedis['jhz'] = $jhz;
  69. }else{
  70. $numDbToRedis['jhz'] = 0;
  71. }
  72. //获取生产中的所有数据
  73. $rows = db('工单_基本资料')
  74. ->distinct('Gd_gdbh')
  75. ->where('gd_statu','like','%生产中%')
  76. ->column('Gd_gdbh');
  77. if($rows===false){
  78. $numDbToRedis['pcz'] = 0;
  79. $numDbToRedis['zcz'] = 0;
  80. $bool = cache('numDbToRedis',$jhz);
  81. if($bool===false) $this->error('订单数量存入redis失败','','000001');
  82. $this->success('请求成功');
  83. }
  84. //查询Gy0_sj1不等于1900-01-01 00:00:00的数据
  85. $data = db('工单_印件资料')->alias('y')
  86. ->join('工单_工艺资料 g','y.Yj_Gdbh = g.Gy0_gdbh AND y.yj_Yjno = g.Gy0_yjno')
  87. ->where('g.Gy0_sj1','<>','1900-01-01 00:00:00')
  88. ->where('y.Yj_Gdbh','in',$rows)
  89. ->group('y.Yj_Gdbh')
  90. ->column('y.Yj_Gdbh');
  91. $zcz = count($data);
  92. $pcz = count($rows) - count($data);
  93. $numDbToRedis['pcz'] = $pcz;
  94. $numDbToRedis['zcz'] = $zcz;
  95. $bool = cache('numDbToRedis',$numDbToRedis);
  96. if($bool===false) $this->error('订单数量存入redis失败','','000002');
  97. $this->success('同步成功');
  98. }
  99. //获取计划中工单数量
  100. public function getJhzByRedis()
  101. {
  102. $row = cache('numDbToRedis');
  103. if($row){
  104. $data = [
  105. 'status'=>0,
  106. 'msg'=>'',
  107. 'data'=>[
  108. 'name'=>'',
  109. 'value'=>$row['jhz']
  110. ]
  111. ];
  112. return json($data);
  113. }else{
  114. $data = [
  115. 'status'=>0,
  116. 'msg'=>'失败',
  117. 'data'=>[
  118. 'name'=>'',
  119. 'value'=>''
  120. ]
  121. ];
  122. return json($data);
  123. }
  124. }
  125. //获取排程中工单数量
  126. public function getPczByRedis()
  127. {
  128. $row = cache('numDbToRedis');
  129. if($row){
  130. $data = [
  131. 'status'=>0,
  132. 'msg'=>'',
  133. 'data'=>[
  134. 'name'=>'',
  135. 'value'=>$row['pcz']
  136. ]
  137. ];
  138. return json($data);
  139. }else{
  140. $data = [
  141. 'status'=>0,
  142. 'msg'=>'失败',
  143. 'data'=>[
  144. 'name'=>'',
  145. 'value'=>''
  146. ]
  147. ];
  148. return json($data);
  149. }
  150. }
  151. //获取制程中工单数量
  152. public function getZczByRedis()
  153. {
  154. $row = cache('numDbToRedis');
  155. if($row){
  156. $data = [
  157. 'status'=>0,
  158. 'msg'=>'',
  159. 'data'=>[
  160. 'name'=>'',
  161. 'value'=>$row['zcz']
  162. ]
  163. ];
  164. return json($data);
  165. }else{
  166. $data = [
  167. 'status'=>0,
  168. 'msg'=>'失败',
  169. 'data'=>[
  170. 'name'=>'',
  171. 'value'=>''
  172. ]
  173. ];
  174. return json($data);
  175. }
  176. }
  177. //印刷车间生产进度数据同步Redis
  178. public function yscjDbToRedis()
  179. {
  180. $yscjDbToRedis = [];
  181. //获取所有印刷车间
  182. // $rows = db('设备_基本资料')
  183. // ->where('使用部门','凹丝印车间')
  184. // ->column('设备编号');
  185. $rows = db('设备_基本资料')
  186. ->whereIn('使用部门', ['凹丝印车间', '胶印车间'])
  187. ->column('设备编号');
  188. foreach($rows as $value){
  189. $row = db('设备_产量采集')->alias('c')
  190. ->field('c.设备编号 as 机台号,
  191. c.工单编号 as 在产工单号,
  192. j.Gd_cpmc as 工单名称,
  193. c.工序名称 as 工序,
  194. g.Gy0_计划接货数 as 计划产量,
  195. g.Gy0_小时产能 as 小时产能,
  196. c.当前状态 as 机台状态
  197. ')
  198. ->join('工单_工艺资料 g','c.设备编号 = g.Gy0_sbbh AND c.工单编号 = g.Gy0_gdbh AND c.印件号 = g.Gy0_yjno')
  199. ->join('工单_基本资料 j','j.Gd_gdbh = c.工单编号')
  200. ->where('c.设备编号',$value)
  201. ->order('c.UniqId desc')
  202. ->find();
  203. if(!$row) continue;
  204. $cl = db('设备_产量计酬')
  205. ->where(['sczl_gdbh'=>$row['在产工单号'], 'sczl_jtbh' => $row['机台号']])
  206. ->sum('sczl_cl');
  207. $row['已完成产量'] = $cl;
  208. array_push($yscjDbToRedis, $row);
  209. }
  210. $bool = cache('yscjDbToRedis',$yscjDbToRedis);
  211. if($bool===false) $this->error('存入redis失败','','000002');
  212. $this->success('同步成功');
  213. }
  214. //印后车间生产进度数据同步Redis
  215. public function yhcjDbToRedis()
  216. {
  217. $yhcjDbToRedis = [];
  218. //获取所有印后车间
  219. $rows = db('设备_基本资料')
  220. ->where('使用部门','印后车间')
  221. ->column('设备编号');
  222. foreach($rows as $value){
  223. $row = db('设备_产量采集')->alias('c')
  224. ->field('c.设备编号 as 机台号,
  225. c.工单编号 as 在产工单号,
  226. j.Gd_cpmc as 工单名称,
  227. c.工序名称 as 工序,
  228. g.Gy0_计划接货数 as 计划产量,
  229. g.Gy0_小时产能 as 小时产能,
  230. c.当前状态 as 机台状态
  231. ')
  232. ->join('工单_工艺资料 g','c.设备编号 = g.Gy0_sbbh AND c.工单编号 = g.Gy0_gdbh AND c.印件号 = g.Gy0_yjno')
  233. ->join('工单_基本资料 j','j.Gd_gdbh = c.工单编号')
  234. ->where('c.设备编号',$value)
  235. ->order('c.UniqId desc')
  236. ->find();
  237. if(!$row) continue;
  238. $cl = db('设备_产量计酬')
  239. ->where(['sczl_gdbh'=>$row['在产工单号'], 'sczl_jtbh' => $row['机台号']])
  240. ->sum('sczl_cl');
  241. $row['已完成产量'] = $cl;
  242. array_push($yhcjDbToRedis, $row);
  243. }
  244. $bool = cache('yhcjDbToRedis',$yhcjDbToRedis);
  245. if($bool===false) $this->error('存入redis失败','','000002');
  246. $this->success('同步成功');
  247. }
  248. //获取印刷车间生产进度数据
  249. public function getYscjByRedis()
  250. {
  251. $row = cache('yscjDbToRedis');
  252. if (empty($row)) {
  253. return json([
  254. 'status' => 0,
  255. 'msg' => '',
  256. 'data' => [] // 或者可以返回 []
  257. ]);
  258. }
  259. $result['columns'] = [
  260. ['name' => '机台号', 'id' => 'jtbh', 'width' => '11', 'autoWrap' => "true", 'textAlign' => 'left'],
  261. ['name' => '在产工单号', 'id' => 'gdbh', 'width' => '15', 'textAlign' => 'left'],
  262. ['name' => '工单名称', 'id' => 'gdmc', 'width' => '35', 'autoWrap' => "true", 'textAlign' => 'left'],
  263. ['name' => '工序', 'id' => 'gx', 'width' => '15', 'textAlign' => 'left'],
  264. ['name' => '计划产量', 'id' => 'jhcl', 'width' => '8', 'autoWrap' => "true", 'textAlign' => 'left'],
  265. ['name' => '已完成产量', 'id' => 'wccl', 'width' => '9', 'autoWrap' => "true", 'textAlign' => 'center'],
  266. ['name' => '小时产能', 'id' => 'xscn', 'width' => '8', 'autoWrap' => "true", 'textAlign' => 'center'],
  267. ['name' => '机台状态', 'id' => 'status', 'width' => '8', 'autoWrap' => "true", 'textAlign' => 'center'],
  268. ];
  269. foreach ($row as $k => $v) {
  270. $result['rows'][$k] = [
  271. 'jtbh' => $v['机台号'],
  272. 'gdbh' => $v['在产工单号'],
  273. 'gdmc' => $v['工单名称'],
  274. 'gx' => $v['工序'],
  275. 'jhcl' => $v['计划产量'],
  276. 'wccl' => $v['已完成产量'],
  277. 'xscn' => $v['小时产能'],
  278. 'status' => $v['机台状态'],
  279. ];
  280. }
  281. return json([
  282. 'status' => 0,
  283. 'msg' => '',
  284. 'data' => $result
  285. ]);
  286. }
  287. //获取印后车间生产进度数据
  288. public function getYhcjByRedis()
  289. {
  290. $row = cache('yhcjDbToRedis');
  291. $result['columns']=[
  292. ['name'=>'机台号','id'=>'jtbh','width'=>'11','autoWrap'=>"true",'textAlign'=>'left'],
  293. ['name'=>'在产工单号','id'=>'gdbh','width'=>'15','textAlign'=>'left'],
  294. ['name'=>'工单名称','id'=>'gdmc','width'=>'35','autoWrap'=>"true",'textAlign'=>'left'],
  295. ['name'=>'工序','id'=>'gx','width'=>'15','textAlign'=>'left'],
  296. ['name'=>'计划产量','id'=>'jhcl','width'=>'8','autoWrap'=>"true",'textAlign'=>'left'],
  297. ['name'=>'已完成产量','id'=>'wccl','width'=>'9','autoWrap'=>"true",'textAlign'=>'center'],
  298. ['name'=>'小时产能','id'=>'xscn','width'=>'8','autoWrap'=>"true",'textAlign'=>'center'],
  299. ['name'=>'机台状态','id'=>'status','width'=>'8','autoWrap'=>"true",'textAlign'=>'center'],
  300. ];
  301. if($row){
  302. foreach($row as $k=>$v){
  303. $result['rows'][$k]['jtbh']=$v['机台号'];
  304. $result['rows'][$k]['gdbh']=$v['在产工单号'];
  305. $result['rows'][$k]['gdmc']=$v['工单名称'];
  306. $result['rows'][$k]['gx']=$v['工序'];
  307. $result['rows'][$k]['jhcl'] = $v['计划产量'];
  308. $result['rows'][$k]['wccl'] = $v['已完成产量'];
  309. $result['rows'][$k]['xscn'] = $v['小时产能'];
  310. $result['rows'][$k]['status'] = $v['机台状态'];
  311. }
  312. }
  313. $res['status']=0;
  314. $res['msg']='';
  315. $res['data']=$result;
  316. return json($res);
  317. }
  318. //车间产量数据同步Redis
  319. public function jtClDbToRedis()
  320. {
  321. $jtClDbToRedis = [];
  322. //获取机台
  323. $rows = db('设备_基本资料')
  324. ->whereNotNull('sys_sbID')
  325. ->column('设备编号');
  326. //获取24小时内的hour组成数组
  327. $hour = date('H');
  328. $arr1 = range($hour+1,'23');
  329. $arr2 = range('00',$hour);
  330. $arr = array_merge($arr1,$arr2);
  331. $arr = array_map(function($num) {
  332. return sprintf("%02d", $num);
  333. }, $arr);
  334. foreach($rows as $value){
  335. $brr = [];
  336. //查询该机台24内的产量数据
  337. $row = db('设备_产量计酬')
  338. ->where('sys_rq', '>', date('Y-m-d H:i:s', strtotime('-23 hours')))
  339. ->where('sczl_jtbh', $value)
  340. ->group('LEFT(sys_rq, 13)')
  341. ->order('sys_rq')
  342. ->column('DATE_FORMAT(sys_rq, "%H") as hour, SUM(sczl_cl) as total_cl');
  343. //将产量数据赋值给小时
  344. foreach($arr as $v){
  345. $brr[$v] = array_key_exists($v,$row) ? $row[$v] : '0';
  346. }
  347. $jtClDbToRedis[$value] = $brr;
  348. }
  349. $bool = cache('jtClDbToRedis',$jtClDbToRedis);
  350. if($bool===false) $this->error('存入redis失败','','000002');
  351. $this->success('同步成功');
  352. }
  353. //获取机台产量数据
  354. public function getJtClDbByRedis()
  355. {
  356. $req = $this->request->param();
  357. // if (isset($req['jtbh']) && !empty($req['jtbh'])){
  358. // $jtbh = $req['jtbh'];
  359. // }else{
  360. // $this->error('参数错误');
  361. // }
  362. $row = cache('jtClDbToRedis');
  363. // $result['categories']=[];
  364. // $result['series'][0]['name']=$jtbh;
  365. // $result['series'][0]['data']=[];
  366. // if($row && array_key_exists($jtbh,$row)){
  367. // $result['categories'] = array_map('strval', array_keys($row[$jtbh]));
  368. // $result['series'][0]['data'] = array_values($row[$jtbh]);
  369. // }
  370. //
  371. // $res['status']=0;
  372. // $res['msg']='';
  373. // $res['data']=$result;
  374. return json($row);
  375. }
  376. /**
  377. * 班组工作中心负荷
  378. */
  379. public function bzDbToRedis()
  380. {
  381. $bzDbToRedis = [];
  382. //计算当天时间
  383. $sczlTime = date('Y-m-d H:i:s',time());
  384. if ($sczlTime>date('Y-m-d 00:00:00') && $sczlTime<date('Y-m-d 08:30:00')){
  385. $time = date('Y-m-d 00:00:00',time()-86400);
  386. }else{
  387. $time = date('Y-m-d 00:00:00');
  388. }
  389. //获取所有机台数据
  390. $machineList = db('设备_基本资料')
  391. ->where('sys_sbID','<>','')
  392. ->where('使用部门','in',['印刷车间','印后车间'])
  393. ->field('设备编号')
  394. ->select();
  395. //循环获取班组、工单、生产数据
  396. foreach ($machineList as $key=>$value){
  397. //获取正在生产工单信息
  398. $workOrder = \db('设备_产量采集')
  399. ->where('设备编号',$value['设备编号'])
  400. ->field('工单编号,印件号,工序号,工序名称,当前状态')
  401. ->order('UniqId desc')
  402. ->find();
  403. if (!$workOrder) continue;
  404. //获取生产信息
  405. $list = db('设备_产量计酬')
  406. ->alias('a')
  407. ->join('工单_基本资料 b','b.Gd_gdbh = a.sczl_gdbh')
  408. ->join('工单_工艺资料 c','a.sczl_gdbh = c.Gy0_gdbh AND a.sczl_yjno = c.Gy0_yjno AND a.sczl_gxh = c.Gy0_gxh')
  409. ->field([
  410. 'SUM(a.sczl_cl)' => '已完成产量',
  411. 'b.成品名称' => '产品名称',
  412. 'c.Gy0_计划接货数' => '计划产量',
  413. 'c.Gy0_小时产能' => '小时产能',
  414. 'a.sczl_bzdh' => '班组代号'
  415. ])
  416. ->where([
  417. 'a.sczl_jtbh' => $value['设备编号'],
  418. 'a.sczl_gdbh' => $workOrder['工单编号'],
  419. 'a.sczl_yjno' => $workOrder['印件号'],
  420. 'a.sczl_gxh' => $workOrder['工序号'],
  421. 'a.sczl_rq' => $time
  422. ])
  423. ->group('a.sczl_jtbh,a.sczl_bzdh')
  424. ->select();
  425. if (!$list) continue;
  426. foreach ($list as $v){
  427. $v['机台号'] = $value['设备编号'];
  428. $v['机台状态'] = $workOrder['当前状态'];
  429. $v['工单编号'] = $workOrder['工单编号'];
  430. $v['印件号'] = $workOrder['印件号'];
  431. $v['工序名称'] = $workOrder['工序名称'];
  432. array_push($bzDbToRedis,$v);
  433. }
  434. }
  435. $bool = cache('bzDbToRedis',$bzDbToRedis);
  436. if($bool===false) $this->error('存入redis失败','','000002');
  437. $this->success('同步成功');
  438. }
  439. //获取班组工作负荷数据
  440. public function getBzByRedis()
  441. {
  442. $row = cache('bzDbToRedis');
  443. $result['columns']=[
  444. ['name'=>'机台号','id'=>'jtbh','width'=>'14','autoWrap'=>"true",'textAlign'=>'left'],
  445. // ['name'=>'班组','id'=>'bzdh','width'=>'16','autoWrap'=>"true",'textAlign'=>'left'],
  446. ['name'=>'在产工单号','id'=>'gdbh','width'=>'16','textAlign'=>'left'],
  447. // ['name'=>'工单名称','id'=>'gdmc','width'=>'20','autoWrap'=>"true",'textAlign'=>'left'],
  448. // ['name'=>'印件号','id'=>'yjno','width'=>'16','autoWrap'=>"true",'textAlign'=>'left'],
  449. // ['name'=>'工序','id'=>'gx','width'=>'16','autoWrap'=>"true",'textAlign'=>'left'],
  450. // ['name'=>'计划产量','id'=>'jhcl','width'=>'14','autoWrap'=>"true",'textAlign'=>'left'],
  451. // ['name'=>'已完成产量','id'=>'wccl','width'=>'14','autoWrap'=>"true",'textAlign'=>'center'],
  452. // ['name'=>'小时产能','id'=>'xscn','width'=>'14','autoWrap'=>"true",'textAlign'=>'center'],
  453. ['name'=>'机台状态','id'=>'status','width'=>'14','autoWrap'=>"true",'textAlign'=>'center'],
  454. ];
  455. if($row){
  456. foreach($row as $k=>$v){
  457. $result['rows'][$k]['jtbh']=$v['机台号'];
  458. // $result['rows'][$k]['bzdh']=$v['班组代号'];
  459. $result['rows'][$k]['gdbh']=$v['工单编号'];
  460. // $result['rows'][$k]['gdmc']=$v['产品名称'];
  461. // $result['rows'][$k]['yjno']=$v['机台号'];
  462. // $result['rows'][$k]['gx']=$v['工序名称'];
  463. // $result['rows'][$k]['jhcl'] = $v['计划产量'];
  464. // $result['rows'][$k]['wccl'] = $v['已完成产量'];
  465. // $result['rows'][$k]['xscn'] = $v['小时产能'];
  466. $result['rows'][$k]['status'] = $v['机台状态'];
  467. }
  468. }else{
  469. $result['rows'][0]['jtbh']='';
  470. // $result['rows'][0]['bzdh']='';
  471. $result['rows'][0]['gdbh']='';
  472. // $result['rows'][0]['gdmc']='';
  473. // $result['rows'][0]['yjno']='';
  474. // $result['rows'][0]['gx']='';
  475. // $result['rows'][0]['jhcl'] = '';
  476. // $result['rows'][0]['wccl'] = '';
  477. // $result['rows'][0]['xscn'] = '';
  478. $result['rows'][0]['status'] = '';
  479. }
  480. $res['status']=0;
  481. $res['msg']='';
  482. $res['data']=$result;
  483. return json($res);
  484. }
  485. /**
  486. * 决策支持月度设备运行数据统计->班组(接口)
  487. */
  488. public function TeamsgGoups()
  489. {
  490. // 获取设备编组信息
  491. $result = \db('设备_基本资料')
  492. ->whereNotNull('设备编组')
  493. ->group('设备编组')
  494. ->column('rtrim(设备编组) as 设备编组');
  495. $data = [
  496. "columns" => [
  497. ["id" => "subsys"]
  498. ],
  499. "rows" => []
  500. ];
  501. foreach ($result as $value) {
  502. $data["rows"][] = ["subsys" => $value];
  503. }
  504. $res = [
  505. 'status' => 0,
  506. 'msg' => '',
  507. 'data' => $data
  508. ];
  509. return json($res);
  510. }
  511. /**
  512. * 决策支持月度设备运行数据统计->机台运行工时汇总(接口)
  513. */
  514. public function MonthlyEquipment() {
  515. // 获取请求参数,默认为 '01、切纸机组'
  516. $postData = input('post.dependence.item.subsys', '01、切纸机组');
  517. // 查询条件
  518. $where = [];
  519. if (!empty($postData)) {
  520. $where['设备编组'] = $postData;
  521. }
  522. // 获取当前年月
  523. $currentYearMonth = date('Y-m');
  524. // 获取去年的同月
  525. $lastYearMonth = date('Y-m', strtotime('-1 year'));
  526. // 查询 **今年** 当月的数据
  527. $currentData = \db('设备_基本资料')
  528. ->alias('a')
  529. ->join('设备_产量计酬 b', 'a.设备编号 = b.sczl_jtbh')
  530. ->field([
  531. 'a.设备编号' => '设备编号',
  532. 'SUM(b.sczl_cl)' => '产量'
  533. ])
  534. ->where($where)
  535. ->whereTime('b.sczl_rq', '>=', $currentYearMonth . '-01') // 查询当前月的第一天
  536. ->whereTime('b.sczl_rq', '<', date('Y-m-d', strtotime('next month', strtotime($currentYearMonth . '-01')))) // 下个月的第一天
  537. ->group('a.设备编号')
  538. ->order('a.设备编号')
  539. ->select();
  540. // 查询 **去年** 同月的数据
  541. $lastYearData = \db('设备_基本资料')
  542. ->alias('a')
  543. ->join('设备_产量计酬 b', 'a.设备编号 = b.sczl_jtbh')
  544. ->field([
  545. 'a.设备编号' => '设备编号',
  546. 'SUM(b.sczl_cl)' => '产量'
  547. ])
  548. ->where($where)
  549. ->whereTime('b.sczl_rq', '>=', $lastYearMonth . '-01') // 查询去年的同月第一天
  550. ->whereTime('b.sczl_rq', '<', date('Y-m-d', strtotime('next month', strtotime($lastYearMonth . '-01')))) // 下个月的第一天
  551. ->group('a.设备编号')
  552. ->order('a.设备编号')
  553. ->select();
  554. // 格式化数据
  555. $categories = [];
  556. $currentYearSeries = [];
  557. $lastYearSeries = [];
  558. // 转换查询结果为关联数组,方便查找
  559. $currentYearMap = [];
  560. $lastYearMap = [];
  561. foreach ($currentData as $item) {
  562. $currentYearMap[$item['设备编号']] = $item['产量'];
  563. }
  564. foreach ($lastYearData as $item) {
  565. $lastYearMap[$item['设备编号']] = $item['产量'];
  566. }
  567. // 统一设备编号,确保数据完整
  568. $allDevices = array_unique(array_merge(array_keys($currentYearMap), array_keys($lastYearMap)));
  569. foreach ($allDevices as $device) {
  570. $categories[] = $device;
  571. $currentYearSeries[] = isset($currentYearMap[$device]) ? intval($currentYearMap[$device]) : 0; // 今年产量
  572. $lastYearSeries[] = isset($lastYearMap[$device]) ? intval($lastYearMap[$device]) : 0; // 去年产量
  573. }
  574. // 组织返回数据
  575. $response = [
  576. 'status' => 0,
  577. 'msg' => '',
  578. 'data' => [
  579. 'categories' => $categories,
  580. 'series' => [
  581. [
  582. 'name' => '今年产量',
  583. 'data' => $currentYearSeries,
  584. 'type' => 'bar',
  585. 'yAxisIndex' => 0
  586. ],
  587. [
  588. 'name' => '去年产量',
  589. 'data' => $lastYearSeries,
  590. 'type' => 'bar',
  591. 'yAxisIndex' => 0
  592. ]
  593. ]
  594. ]
  595. ];
  596. return json($response);
  597. }
  598. /**
  599. * 决策支持月度设备运行数据统计->机台运行工时汇总列表(接口)
  600. */
  601. public function Equipmenttable(){
  602. // 获取请求数据
  603. $postData = input('post.dependence.item.subsys', '01、切纸机组');
  604. $where = [];
  605. if (!empty($postData)) {
  606. $where['设备编组'] = $postData;
  607. }
  608. // 获取当前月份
  609. $mouth = date_create_from_format('Ym', date('Ym'))->format('Y-m');
  610. // 查询设备数据
  611. $list = \db('设备_基本资料')->alias('a')
  612. ->join('设备_产量计酬 b', 'a.设备编号 = b.sczl_jtbh')
  613. ->field([
  614. 'a.设备编号' => '设备编号',
  615. 'rtrim(a.设备名称)' => '设备名称',
  616. 'SUM(b.sczl_cl)' => '产量',
  617. 'SUM(b.sczl_设备运行工时)' => '设备运行工时',
  618. 'SUM(b.sczl_保养工时)' => '保养工时',
  619. 'SUM(b.sczl_打样总工时)' => '打样总工时',
  620. 'SUM(b.sczl_打样工时)' => '打样补产工时',
  621. 'SUM(b.sczl_装版总工时)' => '装板总工时',
  622. 'SUM(b.sczl_装版工时)' => '装板补产工时',
  623. 'SUM(b.sczl_异常停机工时)' => '异常停机工时'
  624. ])
  625. ->where($where)
  626. ->whereTime('b.sczl_rq', $mouth)
  627. ->group('a.设备编号')
  628. ->order('a.设备编号')
  629. ->select();
  630. $rows = [];
  631. foreach ($list as $item) {
  632. $rows[] = [
  633. 'subsys' => $item['设备编号'],
  634. 'module' => $item['设备名称'],
  635. 'business' => $item['产量'],
  636. 'sbyxgs' => $item['设备运行工时'],
  637. 'dyzgs' => $item['打样总工时'],
  638. 'dybcgs' => $item['打样补产工时'],
  639. 'zbzgs' => $item['装板总工时'],
  640. 'zbbcgs' => $item['装板补产工时'],
  641. 'yctjgs' => $item['异常停机工时']
  642. ];
  643. }
  644. // 最终响应格式
  645. $response = [
  646. 'status' => 0,
  647. 'msg' => '',
  648. 'data' => [
  649. 'total' => count($rows),
  650. 'columns' => [
  651. ['name' => '设备编号','id' => 'subsys'],
  652. ['name' => '设备名称','id' => 'module'],
  653. ['name' => '产量','id' => 'business'],
  654. ['name' => '设备运行工时','id' => 'sbyxgs'],
  655. ['name' => '打样总工时','id' => 'dyzgs'],
  656. ['name' => '打样补产工时','id' => 'dybcgs'],
  657. ['name' => '装板总工时','id' => 'zbzgs'],
  658. ['name' => '装板补产工时','id' => 'zbbcgs'],
  659. ['name' => '异常停机工时','id' => 'yctjgs',]
  660. ],
  661. // 'superHeaders' => [
  662. // [
  663. // [
  664. // 'name' => '设备信息',
  665. // 'colspan' => 3
  666. // ],
  667. // [
  668. // 'name' => '工时统计',
  669. // 'colspan' => 2
  670. // ]
  671. // ]
  672. // ],
  673. 'rows' => $rows
  674. ]
  675. ];
  676. return json($response);
  677. }
  678. /**
  679. * 工序产出率月统计报表(接口)
  680. */
  681. public function Monthlystatistical(){
  682. }
  683. /**
  684. * 工序产出率月统计报表(缓存)
  685. */
  686. public function Workorderexcessloss_redis(){
  687. //链接定义 Redis
  688. // $redis = redis();
  689. // $redis_key = md5('Workorderexcessloss_redis');
  690. //获取当月日期
  691. $date = date('Y-m');
  692. $where = [];
  693. $where['a.jjcp_sj' ] = array('like',$date.'%');
  694. $sql = db('物料_收发记录')
  695. ->alias('d')
  696. ->where('d.仓库编号', 'Y101')
  697. ->group('d.st_gdbh, d.cpdh')
  698. ->field('d.st_gdbh, d.cpdh,d.st_rq, SUM(d.st_sl) as 实际投料,d.仓库编号')
  699. ->buildSql();
  700. $data = db('成品入仓')->alias('a')
  701. ->join('工单_基本资料 b', 'a.jjcp_gdbh = b.Gd_gdbh', 'left')
  702. ->join('工单_印件资料 c', 'a.jjcp_gdbh = c.Yj_Gdbh AND a.jjcp_cpdh = c.yj_Yjdh', 'left')
  703. ->join([$sql => 'd'], 'a.jjcp_gdbh = d.st_gdbh AND a.jjcp_cpdh = d.cpdh', 'left')
  704. ->where($where)
  705. ->where('d.仓库编号','Y101')
  706. // ->whereRaw('LEFT(a.成品编码, CASE WHEN a.成品编码 REGEXP "[a-zA-Z]" THEN 5 ELSE 4 END) = ?', [$params['code']])
  707. ->field('a.jjcp_gdbh as Gd_gdbh, a.jjcp_yjno, rtrim(a.jjcp_cpdh) as 成品编码, rtrim(a.jjcp_cpmc) as 成品名称,
  708. a.jjcp_sj, a.jjcp_smb, b.计量单位, b.Gd_khdh, d.实际投料, c.yj_Yjno, c.yj_ls, c.yj_ks, c.质量考核')
  709. ->group('a.jjcp_gdbh, a.jjcp_yjno')
  710. ->order('b.Gd_khdh, a.成品编码 asc, a.jjcp_yjno,d.st_rq desc')
  711. // ->page($pages)
  712. // ->limit($limit)
  713. ->select();
  714. foreach ($data as $key => $value){
  715. //查出成品数量及日期
  716. $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";
  717. $cp_data = Db::query($cp_sql);
  718. $data[$key]['warehousing_num'] = $cp_data[0]['cp_sl'];
  719. $data[$key]['warehousing_date'] = substr($cp_data[0]['jjcp_sj'],0,10);
  720. //查出进入超节损的工序,有上报产量的工序就进入超节损
  721. $gxh_sql = "SELECT sczl_gxh FROM
  722. (SELECT sczl_gxh FROM 设备_产量计酬 WHERE sczl_gdbh = '{$value['Gd_gdbh']}' AND sczl_yjno = '{$value['jjcp_yjno']}'
  723. UNION SELECT sczl_gxh FROM db_sczl WHERE sczl_gdbh = '{$value['Gd_gdbh']}' AND sczl_yjno = '{$value['jjcp_yjno']}') AS combined_result";
  724. $gxh_arr = Db::query($gxh_sql);
  725. $gxh_values = array_column($gxh_arr, 'sczl_gxh');
  726. // $gy_data =db('工单_工艺资料')
  727. // ->where('Gy0_gdbh',$value['Gd_gdbh'])
  728. // ->where('Gy0_yjno',$value['jjcp_yjno'])
  729. // ->where('Gy0_gxh','in',$gxh_values)
  730. // ->field('Gy0_gxmc,Gy0_计划接货数,Gy0_计划损耗')
  731. // ->select();
  732. $gy_data =db('工单_工艺资料')
  733. ->alias('a')
  734. ->field([
  735. '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_计划接货数',
  736. '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',
  737. ])
  738. ->join('设备_产量计酬 b', 'a.Gy0_gdbh = b.sczl_gdbh AND a.Gy0_yjno = b.sczl_yjno AND a.Gy0_gxh = b.sczl_gxh','left')
  739. ->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')
  740. ->where([
  741. 'a.Gy0_gdbh' => $value['Gd_gdbh'],
  742. 'a.Gy0_yjno' =>$value['jjcp_yjno'],
  743. 'a.Gy0_gxh' => ['in', $gxh_values]
  744. ])
  745. ->group('a.Gy0_yjno,a.Gy0_gxh')
  746. ->select();
  747. $arr = [];
  748. $plan_loss = [];//工单计划损耗
  749. $machine_plan_loss = 0;//机检计划损耗
  750. foreach ($gy_data as $k=>$v){
  751. if ($v['Gy0_计划接货数'] > 0){
  752. $rate = round($v['Gy0_计划损耗'] / $v['Gy0_计划接货数'],5);
  753. }else{
  754. $rate = 0;
  755. }
  756. $arr[$k] = floor($rate * 10000) /10000;
  757. $plan_loss[$k] = $v['Gy0_计划损耗'];
  758. // if (strpos($v['Gy0_gxmc'],'机检') !== false){
  759. // $machine_plan_loss = $v['Gy0_计划损耗'];
  760. // }
  761. }
  762. if ((int)$value['yj_ls'] > 0){
  763. $value['实际投料'] = number_format(((int)$value['实际投料']/10000)*$value['yj_ls']*$value['yj_ks'],4);
  764. }else{
  765. $value['实际投料'] = number_format($value['实际投料']/10000,4);
  766. }
  767. $data[$key]['实际投料'] = $value['实际投料'];
  768. if ($value['实际投料'] >0 ){
  769. $target_rate = (1-array_sum($arr))*100;
  770. $data[$key]['target_rate'] =$target_rate.'%'; //目标合格率
  771. $real_rate = $cp_data[0]['cp_sl'] / ($value['实际投料'] * 10000) *100;
  772. $data[$key]['real_rate'] = number_format($real_rate,2) . '%';//实际合格率
  773. }else{
  774. $data[$key]['target_rate'] = ''; //目标合格率
  775. $data[$key]['real_rate'] = '';//实际合格率
  776. }
  777. //制程废品
  778. $zzfp_data =db('设备_产量计酬')->where('sczl_gdbh',$value['Gd_gdbh'])->where('sczl_yjno',$value['jjcp_yjno'])->field('SUM(sczl_zcfp) as sczl_zcfp')->select();
  779. $data[$key]['zcfp'] = $zzfp_data[0]['sczl_zcfp'] * $value['yj_ls'];//制程废品
  780. //废品合计
  781. $wasteTotal = db('db_qczl')->where('qczl_gdbh',$value['Gd_gdbh'])->where('qczl_yjno',$value['jjcp_yjno'])->sum('qczl_fp');
  782. if (empty($wasteTotal)){
  783. $wasteTotal = 0;
  784. }
  785. $data[$key]['废品合计'] = $wasteTotal + $data[$key]['zcfp'];//废品合计
  786. $data[$key]['工单无形损'] = (int)($value['实际投料'] *10000) - $cp_data[0]['cp_sl'] - $data[$key]['废品合计'];//工单无形损
  787. $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,
  788. 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,
  789. 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,
  790. 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
  791. FROM db_qczl WHERE qczl_gdbh = '{$value['Gd_gdbh']}' AND qczl_yjno = '{$value['jjcp_yjno']}'";
  792. $waste_out = Db::query($out_sql);
  793. $list = []; //外发废数组
  794. $quality = [];//质检废数组
  795. $material = [];//材料废数组
  796. $change = [];//零头处理数组
  797. $machine = [];//机检废品数组
  798. $j = 0;
  799. $m = 0;
  800. $n = 0;
  801. $l = 0;
  802. $o = 0;
  803. foreach ($waste_out as $entry) {
  804. for ($i = 1; $i <= 13; $i++) {
  805. $labelKey = "fp_lb" . $i;
  806. $bhKey = "fp_bh" . $i;
  807. $gxmcKey = "fp_gxmc" . $i;
  808. $slKey = "fp_sl" . $i;
  809. // if (!empty($entry[$labelKey])) {
  810. // if (substr($entry[$gxmcKey],0,2) == '99'){
  811. // $list[$j]= $entry[$slKey];
  812. // $j++;
  813. // }
  814. // if (substr($entry[$labelKey],0,1) == 'L' ){
  815. // $material[$n]= $entry[$slKey];
  816. // $n++;
  817. // }
  818. // if (substr($entry[$labelKey],0,3) == 'M04' ){
  819. // $change[$l]= $entry[$slKey];
  820. // $l++;
  821. // }
  822. // }
  823. // if (!empty($entry[$bhKey])) {
  824. // if ($entry[$bhKey] != '000000' && $entry[$slKey] > 0){
  825. // $quality[$m]= $entry[$slKey];
  826. // $m++;
  827. // }
  828. //// if ($entry[$bhKey] != '000000' && substr($entry[$labelKey],0,3) == 'K01'){
  829. //// $machine[$o]= $entry[$slKey];
  830. //// $o++;
  831. //// }
  832. // }
  833. if (!empty($entry[$labelKey])) {
  834. if (substr($entry[$labelKey],0,3) == 'M04'){
  835. $list[$j]= $entry[$slKey];
  836. $j++;
  837. }
  838. if (substr($entry[$labelKey],0,1) == 'L' ){
  839. $material[$n]= $entry[$slKey];
  840. $n++;
  841. }
  842. if (substr($entry[$labelKey],0,3) == 'M01' ){
  843. $change[$l]= $entry[$slKey];
  844. $l++;
  845. }
  846. }
  847. // if (!empty($entry[$bhKey])) {
  848. // if ($entry[$bhKey] != '000000' && $entry[$slKey] > 0){
  849. $quality[$m]= $entry[$slKey];
  850. $m++;
  851. // }
  852. // }
  853. }
  854. }
  855. $machine_waste = array_sum($machine);//机检废品
  856. if ($data[$key]['Gd_gdbh'] == '2401746'){
  857. halt($machine_waste);
  858. }
  859. $data[$key]['工单计划损耗'] = array_sum($plan_loss);//工单计划损耗
  860. /**
  861. * 奖惩系数及金额
  862. * 奖惩金额 = 工单节损数 * 0.02(0.09)
  863. * 工单节损数 = 工单计划损耗 - 废品总数 - 机检节损数
  864. * 机检节损数 = 机检计划损耗 - 机检质检废品
  865. */
  866. $machine_loss = $machine_plan_loss - $machine_waste;
  867. $order_loss = $data[$key]['工单计划损耗'] - ($data[$key]['废品合计'] - array_sum($list));
  868. if (strpos($data[$key]['成品名称'],'小盒')){
  869. $moneyRate = 0.02;
  870. }else{
  871. $moneyRate = 0.09;
  872. }
  873. $reward = $order_loss * $moneyRate;
  874. $data[$key]['reward_rate'] = '1';
  875. if ($data[$key]['质量考核'] === 0){
  876. $data[$key]['reward_money'] = number_format($reward,2);
  877. }else{
  878. $data[$key]['reward_money'] = '';
  879. }
  880. // $data[$key]['reward_money'] = '';
  881. $data[$key]['材料废'] = array_sum($material); //材料废
  882. $data[$key]['零头处理'] = array_sum($change); //零头处理
  883. $data[$key]['外发废'] = array_sum($list);//外发废
  884. $data[$key]['分摊废'] = '';//分摊废
  885. $data[$key]['工单质检废'] = array_sum($quality); //质检废
  886. }
  887. // if($data){
  888. // //将查询结果存入 Redis 缓存中
  889. // $redis->set($redis_key, json_encode($data));
  890. // echo date("Y-m-d H:i:s").' 存进去了';
  891. // return json($data);
  892. // }
  893. $bool = cache('cacheWorkorderexcessloss_redis',$data);
  894. if($bool===false) $this->error('存入redis失败','','000002');
  895. $this->success('存入成功');
  896. }
  897. /**
  898. * 工序产出率月统计报表(接口)
  899. */
  900. public function Workorderexcessloss(){
  901. $data = cache('cacheWorkorderexcessloss_redis');
  902. // $redis = redis();
  903. // $data = json_decode($redis->get(md5('Workorderexcessloss_redis')),true);
  904. $rows = [];
  905. foreach ($data as $item) {
  906. $rows[] = [
  907. 'Gd_gdbh' => $item['Gd_gdbh'],
  908. 'cpmc' => $item['成品名称'],
  909. 'jjcp_sj' => $item['jjcp_sj'],
  910. 'target_rate' => $item['target_rate'],
  911. 'real_rate' => $item['real_rate'],
  912. ];
  913. }
  914. // 最终响应格式
  915. $response = [
  916. 'status' => 0,
  917. 'msg' => '',
  918. 'data' => [
  919. 'columns' => [
  920. ['name' => '工单编号','width'=>'10','id' => 'Gd_gdbh'],
  921. ['name' => '产品名称','width'=>'38','id' => 'cpmc'],
  922. ['name' => '入仓时间','width'=>'20','id' => 'jjcp_sj'],
  923. ['name' => '目标合格率','width'=>'16','id' => 'target_rate'],
  924. ['name' => '实际合格率','width'=>'16','id' => 'real_rate'],
  925. ],
  926. 'rows' => $rows
  927. ]
  928. ];
  929. return json($response);
  930. }
  931. }