Index.php 40 KB

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