Index.php 40 KB

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