Index.php 38 KB

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