GluingReport.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Request;
  6. class GluingReport extends Api
  7. {
  8. protected $noNeedLogin = ['*'];
  9. protected $noNeedRight = ['*'];
  10. /**
  11. * 设备角色菜单
  12. * @return void
  13. */
  14. public function getGluingcoleTab()
  15. {
  16. if ($this->request->isGet() === false){
  17. $this->error('请求错误');
  18. }
  19. $data = [];
  20. $sist = ['印后糊盒车间','精品自动化车间','数字化车间'];
  21. $department = \db('设备_基本资料')
  22. ->distinct(true)
  23. ->where('使用部门','in',$sist)
  24. ->where('设备编组','<>','')
  25. ->order('设备编组')
  26. ->column('rtrim(使用部门) as 使用部门');
  27. if (empty($department)){
  28. $this->success('未获取到机台数据');
  29. }
  30. foreach ($department as $value){
  31. $machine = \db('设备_基本资料')
  32. ->where('使用部门',$value)
  33. ->field('rtrim(设备编号) as 设备编号,rtrim(设备名称) as 设备名称')
  34. ->order('设备编号')
  35. ->select();
  36. foreach ($machine as $k=>$v){
  37. $data[rtrim($value)][] = $v['设备编号'].'-->'.$v['设备名称'];
  38. }
  39. }
  40. $this->success('成功',$data);
  41. }
  42. /**
  43. * 设备角色列表
  44. * @return void
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. * @throws \think\exception\DbException
  48. */
  49. public function getGluingcoleList()
  50. {
  51. if ($this->request->isGet() === false){
  52. $this->error('请求错误');
  53. }
  54. $param = $this->request->param();
  55. if (empty($param)){
  56. $this->error('参数错误');
  57. }
  58. $list = db('设备_糊盒班组角色')
  59. ->where('jtbh',$param['machine'])
  60. ->select();
  61. if (empty($list)){
  62. $this->error('未找到数据');
  63. }else{
  64. $this->success('成功',$list);
  65. }
  66. }
  67. /**
  68. * 设备角色资料详情
  69. * @return void
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. * @throws \think\exception\DbException
  73. */
  74. public function getGluingcoleDetail()
  75. {
  76. if ($this->request->isGet() === false){
  77. $this->error('请求错误');
  78. }
  79. $param = $this->request->param();
  80. if (empty($param)){
  81. $this->error('参数错误');
  82. }
  83. $list = db('设备_糊盒班组角色')
  84. ->where('id',$param['id'])
  85. ->find();
  86. if (empty($list)){
  87. $this->error('未找到数据详情');
  88. }else{
  89. $this->success('成功',$list);
  90. }
  91. }
  92. /**
  93. * 新增机台班组角色
  94. * @return void
  95. */
  96. public function saveGluingcoleDetail(){
  97. if ($this->request->isPost() === false){
  98. $this->error('请求错误');
  99. }
  100. $param = Request::instance()->post();
  101. if (empty($param)){
  102. $this->error('参数错误');
  103. }
  104. $param['sys_rq'] = date('Y-m-d H:i:s',time());
  105. $res = db('设备_糊盒班组角色')->insert($param);
  106. if (empty($res)){
  107. $this->error('新增失败');
  108. }else{
  109. $this->success('新增成功');
  110. }
  111. }
  112. /**
  113. * 修改设备角色资料
  114. * @return void
  115. * @throws \think\Exception
  116. * @throws \think\exception\PDOException
  117. */
  118. public function getGluingcoleDetaiEdit()
  119. {
  120. if ($this->request->isPost() === false){
  121. $this->error('请求错误');
  122. }
  123. $param = Request::instance()->post();
  124. if (empty($param['id'])){
  125. $this->error('参数错误');
  126. }
  127. $id = intval($param['id']);
  128. $param['mod_rq'] = date('Y-m-d H:i:s',time());
  129. unset($param['id']);
  130. $res = db('设备_糊盒班组角色')
  131. ->where('id',$id)
  132. ->update($param);
  133. if ($res === false){
  134. $this->error('修改失败');
  135. }else{
  136. $this->success('修改成功');
  137. }
  138. }
  139. /**
  140. * 设备当前班组角色
  141. * @return void
  142. * @throws \think\Exception
  143. * @throws \think\exception\PDOException
  144. */
  145. public function UpdateGluingcoleStatus()
  146. {
  147. if ($this->request->isGet() === false){
  148. $this->error('请求错误');
  149. }
  150. $param = $this->request->param();
  151. if (empty($param['id']) || empty($param['jtbh'])){
  152. $this->error('参数错误');
  153. }
  154. $updateRes = db('设备_糊盒班组角色')
  155. ->where('jtbh',$param['jtbh'])
  156. ->update(['status' => 0]);
  157. $res = db('设备_糊盒班组角色')
  158. ->where('id',$param['id'])
  159. ->update(['status' => 1]);
  160. if ($updateRes === false || $res === false){
  161. $this->error('更新失败');
  162. }else{
  163. $this->success('更新成功');
  164. }
  165. }
  166. /**
  167. * 设备班组资料列表
  168. * @return void
  169. * @throws \think\db\exception\DataNotFoundException
  170. * @throws \think\db\exception\ModelNotFoundException
  171. * @throws \think\exception\DbException
  172. */
  173. public function getGluingClassLList()
  174. {
  175. if ($this->request->isGet() === false){
  176. $this->error('请求错误');
  177. }
  178. $param = $this->request->param();
  179. if (empty($param)){
  180. $this->error('参数错误');
  181. }
  182. $list = db('设备_糊盒班组资料')
  183. ->where('jtbh',$param['machine'])
  184. ->select();
  185. foreach ($list as $k=>$v){
  186. for ($i = 1; $i <= 15; $i++){
  187. $bh = $v['bh'.$i];
  188. $list[$k]['name'.$i] = '';
  189. if (!empty($bh) && $bh !== ''){
  190. $name = \db('人事_基本资料')
  191. ->where('员工编号',$bh)
  192. ->value('员工姓名');
  193. $list[$k]['name'.$i] = $name;
  194. }
  195. }
  196. }
  197. if (empty($list)){
  198. $this->error('未找到数据');
  199. }else{
  200. $this->success('成功',$list);
  201. }
  202. }
  203. /**
  204. * 获取设备角色数据
  205. * @return void
  206. * @throws \think\db\exception\DataNotFoundException
  207. * @throws \think\db\exception\ModelNotFoundException
  208. * @throws \think\exception\DbException
  209. */
  210. public function getGluingcole()
  211. {
  212. if ($this->request->isGet() === false){
  213. $this->error('请求错误');
  214. }
  215. $param = $this->request->param();
  216. if (empty($param)){
  217. $this->error('参数错误');
  218. }
  219. $list = db('设备_糊盒班组角色')
  220. ->where('jtbh',$param['machine'])
  221. ->where('status',1)
  222. ->find();
  223. if (empty($list)){
  224. $this->error('未找到角色资料');
  225. }else{
  226. $this->success('成功',$list);
  227. }
  228. }
  229. /**
  230. * 新增糊盒机台班组资料
  231. * @return void
  232. */
  233. public function AddGluingClass()
  234. {
  235. if ($this->request->isPost() === false){
  236. $this->error('请求错误');
  237. }
  238. $param = Request::instance()->post();
  239. if (empty($param)){
  240. $this->error('参数错误');
  241. }
  242. $param['sys_rq'] = date('Y-m-d H:i:s',time());
  243. $res = db('设备_糊盒班组资料')->insert($param);
  244. if ($res === false){
  245. $this->error('新增失败');
  246. }else{
  247. $this->success('新增成功');
  248. }
  249. }
  250. /**
  251. * 修改设备糊盒班组资料
  252. * @return void
  253. * @throws \think\Exception
  254. * @throws \think\exception\PDOException
  255. */
  256. public function UpdateGluingClass()
  257. {
  258. if ($this->request->isPost() === false){
  259. $this->error('请求错误');
  260. }
  261. $param = Request::instance()->post();
  262. if (empty($param['id'])){
  263. $this->error('参数错误');
  264. }
  265. $id = intval($param['id']);
  266. $param['mod_rq'] = date('Y-m-d H:i:s',time());
  267. unset($param['id']);
  268. $res = db('设备_糊盒班组资料')
  269. ->where('id',$id)
  270. ->update($param);
  271. if ($res === false){
  272. $this->error('修改失败');
  273. }else{
  274. $this->success('修改成功');
  275. }
  276. }
  277. /**
  278. * 糊盒机台报工
  279. * @return void
  280. * @throws \think\db\exception\BindParamException
  281. * @throws \think\exception\PDOException
  282. */
  283. public function addGluingReportData()
  284. {
  285. // 1. 请求方式验证(严格限制POST请求)
  286. if (!$this->request->isPost()) {
  287. $this->error('仅支持POST请求');
  288. }
  289. // 2. 获取并验证参数(重点适配含#字符的sczl_jtbh字段)
  290. $param = $this->request->post();
  291. // 核心修复:验证规则调整,明确允许#字符,兼容特殊字符
  292. $validate = new \think\Validate([
  293. 'sczl_jtbh' => 'require|regex:/^[\w#\x{4e00}-\x{9fa5}]+$/u', // 允许字母、数字、下划线、#、中文
  294. 'sczl_gdbh' => 'require',
  295. 'sczl_gxmc' => 'require',
  296. 'sys_id' => 'require',
  297. 'sczl_rq' => 'require|dateFormat:Y-m-d H:i:s',
  298. ], [
  299. 'sczl_jtbh.require' => '机组编号不能为空',
  300. 'sczl_jtbh.regex' => '机组编号仅支持字母、数字、下划线、#号和中文',
  301. 'sczl_gdbh.require' => '工单号不能为空',
  302. 'sczl_gxmc.require' => '工序名称不能为空',
  303. 'sys_id.require' => '系统ID不能为空',
  304. 'sczl_rq.require' => '报工日期不能为空',
  305. 'sczl_rq.dateFormat' => '报工日期格式错误(需为Y-m-d H:i:s)',
  306. ]);
  307. // 验证前处理:仅去除前后空格(不影响#字符)
  308. foreach (['sczl_jtbh', 'sczl_gdbh', 'sczl_gxmc', 'sys_id', 'sczl_rq'] as $field) {
  309. if (isset($param[$field]) && is_string($param[$field])) {
  310. $param[$field] = trim($param[$field]); // 只去空格,保留#等特殊字符
  311. }
  312. }
  313. // 执行验证
  314. if (!$validate->check($param)) {
  315. $this->error($validate->getError());
  316. }
  317. // 3. 事务处理(确保数据一致性)
  318. $currentTime = date('Y-m-d H:i:s');
  319. $tableClass = '糊盒报工班组';
  320. $tableDevice = '设备_糊盒报工资料';
  321. // 4. 构建班组数据(保留#字符,无需额外处理)
  322. $classData = [
  323. 'sys_rq' => $currentTime,
  324. 'jtbh' => $param['sczl_jtbh'], // 直接保留含#的原始值
  325. 'gdbh' => $param['sczl_gdbh'],
  326. 'gxmc' => $param['sczl_gxmc'],
  327. 'sys_id' => $param['sys_id'],
  328. 'sczl_rq' => $param['sczl_rq'],
  329. 'sczl_bzdh' => $param['sczl_bzdh'],
  330. ];
  331. // 处理动态字段(1-30组bh/rate)
  332. $dynamicFields = [];
  333. for ($i = 1; $i <= 30; $i++) {
  334. $dynamicFields['bh' . $i] = isset($param['bh' . $i]) ? $param['bh' . $i] : '';
  335. $dynamicFields['rate' . $i] = isset($param['rate' . $i]) ? $param['rate' . $i] : '';
  336. }
  337. $classData = array_merge($classData, $dynamicFields);
  338. // 5. 保存班组数据(框架会自动处理特殊字符转义,避免SQL注入)
  339. $classId = Db::name($tableClass)->insertGetId($classData);
  340. if (empty($classId)) {
  341. throw new \Exception("{$tableClass}数据保存失败");
  342. }
  343. // 6. 构建设备数据(适配PHP7.2)
  344. $filteredParam = array_filter($param, function ($key) {
  345. return !preg_match('/^(bh|rate)\d+$/', $key);
  346. }, ARRAY_FILTER_USE_KEY);
  347. $deviceData = array_merge([
  348. 'role' => $classId,
  349. 'sys_rq' => $currentTime,
  350. ], $filteredParam);
  351. // 7. 保存设备数据(含#字符的字段会被自动转义,安全插入)
  352. $sql = Db::name($tableDevice)->fetchSql(true)->insert($deviceData);
  353. $saveResult = db()->query($sql);
  354. if ($saveResult !== false) {
  355. $this->success('报工提交成功');
  356. }else{
  357. $this->error('报工提交失败');
  358. }
  359. }
  360. /**
  361. * 糊盒机台报工数据菜单
  362. * @return void
  363. * @throws \think\db\exception\DataNotFoundException
  364. * @throws \think\db\exception\ModelNotFoundException
  365. * @throws \think\exception\DbException
  366. */
  367. public function getTab()
  368. {
  369. if ($this->request->isGet() === false){
  370. $this->error('请求错误');
  371. }
  372. $data = [];
  373. $date = date('Y-m-d 00:00:00',time()-3888000);
  374. $sist = ['印后糊盒车间','精品自动化车间','数字化车间'];
  375. $department = \db('设备_基本资料')
  376. ->distinct(true)
  377. ->where('使用部门','in',$sist)
  378. ->where('设备编组','<>','')
  379. ->order('设备编组')
  380. ->column('rtrim(使用部门) as 使用部门');
  381. if (empty($department)){
  382. $this->success('未获取到机台数据');
  383. }
  384. $list = \db('设备_糊盒报工资料')
  385. ->field([
  386. 'DISTINCT(sczl_rq)' => '时间',
  387. 'rtrim(sczl_jtbh)' => '机台编号'
  388. ])
  389. ->where('sczl_rq','>',$date)
  390. ->order('sczl_rq desc')
  391. ->select();
  392. foreach ($department as $value){
  393. $machine = \db('设备_基本资料')
  394. ->where('使用部门',$value)
  395. ->field('rtrim(设备编号) as 设备编号,rtrim(设备名称) as 设备名称')
  396. ->order('设备编号')
  397. ->select();
  398. foreach ($machine as $k=>$v){
  399. $data[rtrim($value)][$v['设备编号'].'-->'.$v['设备名称']] = [];
  400. foreach ($list as $kk=>$vv){
  401. if ($v['设备编号'] === $vv['机台编号']){
  402. array_push($data[rtrim($value)][$v['设备编号'].'-->'.$v['设备名称']],date('Y-m-d',strtotime($vv['时间'])));
  403. }
  404. }
  405. }
  406. }
  407. $this->success('成功',$data);
  408. }
  409. /**
  410. * 获取当前生产工单
  411. * @return void
  412. * @throws \think\db\exception\DataNotFoundException
  413. * @throws \think\db\exception\ModelNotFoundException
  414. * @throws \think\exception\DbException
  415. */
  416. public function GetProduction()
  417. {
  418. if (Request::instance()->isGet() == false) {
  419. $this->error('非法请求');
  420. }
  421. $params = Request::instance()->param();
  422. if (!isset($params['machine']) || empty($params['machine'])) {
  423. $this->error('参数错误');
  424. }
  425. $machine = $params['machine'];
  426. $machineCode = \db('dic_lzde')->where('适用机型',$machine)->value('sys_bh');
  427. $data = \db('设备_糊盒报工采集')->where('sczl_jtbh',$machine)->order('UID desc')->find();
  428. if (empty($data)){
  429. $this->success('未找到数据');
  430. }
  431. $list = [];
  432. $list['班组ID'] = $data['班组ID'];
  433. $row = [];
  434. if (!empty($data['sczl_gdbh'])){
  435. $endTime = \db('工单_工艺资料')
  436. ->where('Gy0_gdbh',$data['sczl_gdbh'])
  437. ->where('Gy0_yjno',$data['sczl_yjno'])
  438. ->where('Gy0_gxh',$data['sczl_gxh'])
  439. ->find();
  440. $list['工单编号'] = $data['sczl_gdbh'];
  441. if (!empty($endTime)){
  442. $list['印件号'] = $data['sczl_yjno'];
  443. $name = \db('工单_基本资料')->where('Gd_Gdbh',$data['sczl_gdbh'])->value('成品名称');
  444. $code = \db('工单_基本资料')->where('Gd_Gdbh',$data['sczl_gdbh'])->value('成品代号');
  445. $list['产品名称'] = rtrim($name);
  446. $list['产品代号'] = rtrim($code);
  447. $list['工序名称'] = $data['sczl_gxmc'];
  448. $list['联数'] = $endTime['Gy0_ls'];
  449. }
  450. $list['状态'] = rtrim($data['status']);
  451. $list['开工时间'] = $data['sczl_kgsj'];
  452. }else{
  453. $list['工单编号'] = '';
  454. $list['印件号'] = 0;
  455. $list['产品名称'] = '';
  456. $list['工序名称'] = '';
  457. $list['产品代号'] = '';
  458. $list['状态'] = '';
  459. $list['开工时间'] = '';
  460. $list['联数'] = '';
  461. }
  462. $list['班组编号'] = rtrim($data['sczl_bzbh']);
  463. $idList = explode(',',$data['班组ID']);
  464. foreach ($idList as $k=>$v){
  465. $class = \db('设备_糊盒班组资料')
  466. ->where('id',$v)
  467. ->field("role,rate,bh1,bh2,bh3,bh4,bh5,bh6,bh7,bh8,bh9,bh10,bh11,bh12,bh13,bh14,bh15")
  468. ->find();
  469. if (!empty($class)){
  470. for ($i=1;$i<16;$i++) {
  471. if ($class['bh' . $i] != '' && $class['bh' . $i] != '000000') {
  472. $name = \db('人事_基本资料')->where('员工编号', $class['bh' . $i])->field('rtrim(员工姓名) as 姓名')->find();
  473. $row[] = [
  474. '编号' => $class['bh' . $i],
  475. '姓名' => $name['姓名'],
  476. '比例' => $class['rate'],
  477. '角色' => $class['role'],
  478. ];
  479. }
  480. }
  481. $row = array_values($row);
  482. }
  483. }
  484. $list['班组成员'] = $row;
  485. $list['定额代号'] = $machineCode;
  486. $this->success('成功',$list);
  487. }
  488. /**
  489. * 设置当前工单和当前班组
  490. * @return void
  491. */
  492. public function setMachineTeam()
  493. {
  494. if (Request::instance()->isPost() == false) {
  495. $this->error('非法请求');
  496. }
  497. $params = Request::instance()->post();
  498. if (!isset($params['machine']) || empty($params['machine'] )) {
  499. $this->error('参数不能为空');
  500. }
  501. if (empty($params['team_id']) || empty($params['sczl_bzdh'])){
  502. $this->error('请先选择班组成员');
  503. }
  504. $machine = $params['machine'] . '#';
  505. $data = [];
  506. $data['status'] = $params['status'];
  507. $data['sczl_sj'] = date('Y-m-d H:i:s');
  508. $data['sczl_jtbh'] = $machine;
  509. $data['sczl_gdbh'] = empty($params['order']) ? '':$params['order'];
  510. $data['sczl_yjno'] = empty($params['yjno']) ? '':$params['yjno'];
  511. $data['sczl_gxh'] = empty($params['gy_name']) ? '' : (int)substr($params['gy_name'], 0, 2);
  512. $data['sczl_gxmc'] = empty($params['gy_name']) ? '' : $params['gy_name'];
  513. $data['sczl_bzbh'] = $params['sczl_bzdh'];
  514. $data['班组ID'] = $params['team_id'];
  515. // 获取当前时间
  516. $current_time = time();
  517. // 设置时间范围
  518. $start_time1 = strtotime(date('Y-m-d') . ' 08:30:00');
  519. $end_time1 = strtotime(date('Y-m-d') . ' 20:30:00');
  520. $end_time2 = strtotime(date('Y-m-d') . ' 24:00:00');
  521. $start_time3 = strtotime(date('Y-m-d', strtotime('+1 day')) . ' 08:30:00');
  522. $start_time4 = strtotime(date('Y-m-d', strtotime('+1 day')) . ' 00:00:00');
  523. // 判断当前时间属于哪个时间范围
  524. if ($current_time >= $start_time1 && $current_time <= $end_time1) {
  525. $data['sczl_kgsj'] = date('Y-m-d') . ' 08:30:00';
  526. } elseif ($current_time > $end_time1 && $current_time <= $end_time2) {
  527. $data['sczl_kgsj'] = date('Y-m-d') . ' 20:30:00';
  528. } elseif ($current_time > $end_time1 && $current_time <= $start_time3) {
  529. $data['sczl_kgsj'] = date('Y-m-d', strtotime('+1 day')) . ' 08:30:00';
  530. }elseif ($current_time > $start_time4 && $current_time <= $start_time3){
  531. $data['sczl_kgsj'] = date('Y-m-d') . ' 20:30:00';
  532. }
  533. if (!empty($params['order']) && !empty($params['yjno'])){
  534. $option['Gy0_gdbh'] = $params['order'];
  535. $option['Gy0_yjno'] = $params['yjno'];
  536. $option['Gy0_gxh'] = $data['sczl_gxh'];
  537. $data['任务ID'] = \db('工单_工艺资料')->where($option)->value('UniqId');
  538. }else{
  539. $data['任务ID'] = '';
  540. }
  541. $sql = \db('设备_糊盒报工采集')->fetchSql(true)->insert($data);
  542. $res = Db::query($sql);
  543. if ($res === false) {
  544. $this->error('设置失败');
  545. } else {
  546. $this->success('设置成功');
  547. }
  548. }
  549. /**
  550. * 糊盒设备运行跟踪数据页面显示
  551. * @return void
  552. * @throws \think\db\exception\DataNotFoundException
  553. * @throws \think\db\exception\ModelNotFoundException
  554. * @throws \think\exception\DbException
  555. */
  556. public function getGluingReportDataList()
  557. {
  558. if ($this->request->isGet() === false){
  559. $this->error('请求错误');
  560. }
  561. $params = $this->request->param();
  562. if (empty($params['machine']) || empty($params['day'])){
  563. $this->error('参数错误');
  564. }
  565. $where = [
  566. 'a.sczl_jtbh' => $params['machine'],
  567. 'a.sczl_rq' => ['like',$params['day'].'%']
  568. ];
  569. $field = ['a.sczl_gdbh as 工单编号','a.sczl_gxmc as 工序名称','a.来料数量','a.sczl_cl as 产量',
  570. 'a.sczl_zcfp as 制程废品','a.startTime as 开始时间','a.endTime as 结束时间','a.sczl_ls as 联数','a.sczl_rq as 日期',
  571. 'a.sczl_dedh as 定额代号','a.工价系数','a.保养工时','a.装版工时','a.异常工时','a.异常类型','a.设备运行工时','a.role',
  572. 'a.sys_id as 创建人员','a.sys_rq as 上报时间','a.mod_rq as 修改时间','a.Uid','a.sczl_jtbh as 机台编号','a.price',
  573. 'b.Gd_cpdh as 产品代号','b.Gd_cpmc as 产品名称','a.sczl_bzdh as 班组',];
  574. $list = \db('设备_糊盒报工资料')
  575. ->alias('a')
  576. ->join('工单_基本资料 b','a.sczl_gdbh = b.Gd_gdbh','left')
  577. ->field($field)
  578. ->where($where)
  579. ->group('a.Uid')
  580. ->select();
  581. if (empty($list)){
  582. $this->error('未找到报工数据');
  583. }
  584. foreach ($list as $key=>$value) {
  585. $list[$key]['class'] = [];
  586. $class = \db('糊盒报工班组')
  587. ->where('id', $value['role'])
  588. ->field("bh1,bh2,bh3,bh4,bh5,bh6,bh7,bh8,bh9,bh10,bh11,bh12,bh13,bh14,bh15,bh16,bh17,bh18,bh19,bh20,bh21,bh22,bh23,
  589. bh24,bh25,bh26,bh27,bh28,bh29,bh30,rate1,rate2,rate3,rate4,rate5,rate6,rate7,rate8,rate9,rate10,rate11,rate12,rate13,rate14,
  590. rate15,rate16,rate17,rate18,rate19,rate20,rate21,rate22,rate23,rate24,rate25,rate26,rate27,rate28,rate29,rate30")
  591. ->find();
  592. if (!empty($class)) {
  593. for ($i = 1; $i <=30; $i++) {
  594. if ($class['bh' . $i] != '' && $class['bh' . $i] != '000000') {
  595. $name = \db('人事_基本资料')->where('员工编号', $class['bh' . $i])->field('rtrim(员工姓名) as 姓名')->find();
  596. $list[$key]['class'][] = [
  597. '编号' => $class['bh' . $i],
  598. '姓名' => $name['姓名'],
  599. '比例' => $class['rate'.$i],
  600. ];
  601. }
  602. }
  603. $list[$key]['class'] = array_values($list[$key]['class']);
  604. }
  605. }
  606. $this->success('成功',$list);
  607. }
  608. /**
  609. * 报工数据详情显示
  610. * @return void
  611. * @throws \think\db\exception\DataNotFoundException
  612. * @throws \think\db\exception\ModelNotFoundException
  613. * @throws \think\exception\DbException
  614. */
  615. public function getGluingReportDataDetail()
  616. {
  617. if ($this->request->isGet() === false){
  618. $this->error('请求错误');
  619. }
  620. $params = $this->request->param();
  621. if(!isset($params['id']) || empty($params['id'])){
  622. $this->error('参数错误');
  623. }
  624. $where = ['Uid'=>$params['id']];
  625. $field = ['a.sczl_gdbh as 工单编号','a.sczl_gxmc as 工序名称','a.来料数量','a.sczl_cl as 产量',
  626. 'a.sczl_zcfp as 制程废品','a.startTime as 开始时间','a.endTime as 结束时间','a.sczl_ls as 联数','a.sczl_rq as 日期',
  627. 'a.sczl_dedh as 定额代号','a.工价系数','a.保养工时','a.装版工时','a.异常工时','a.异常类型','a.设备运行工时','a.role',
  628. 'a.sys_id as 创建人员','a.sys_rq as 上报时间','a.mod_rq as 修改时间','a.Uid','a.sczl_jtbh as 机台编号','a.price',
  629. 'b.Gd_cpdh as 产品代号','b.Gd_cpmc as 产品名称','a.sczl_bzdh as 班组',];
  630. $list = \db('设备_糊盒报工资料')
  631. ->alias('a')
  632. ->join('工单_基本资料 b','a.sczl_gdbh = b.Gd_gdbh','left')
  633. ->where($where)
  634. ->field($field)
  635. ->find();
  636. $list['class'] = [];
  637. $class = \db('糊盒报工班组')
  638. ->where('id', $list['role'])
  639. ->field("bh1,bh2,bh3,bh4,bh5,bh6,bh7,bh8,bh9,bh10,bh11,bh12,bh13,bh14,bh15,bh16,bh17,bh18,bh19,bh20,bh21,bh22,bh23,
  640. bh24,bh25,bh26,bh27,bh28,bh29,bh30,rate1,rate2,rate3,rate4,rate5,rate6,rate7,rate8,rate9,rate10,rate11,rate12,rate13,rate14,
  641. rate15,rate16,rate17,rate18,rate19,rate20,rate21,rate22,rate23,rate24,rate25,rate26,rate27,rate28,rate29,rate30")
  642. ->find();
  643. if (!empty($class)) {
  644. for ($i = 1; $i <=30; $i++) {
  645. if ($class['bh' . $i] != '' && $class['bh' . $i] != '000000') {
  646. $name = \db('人事_基本资料')->where('员工编号', $class['bh' . $i])->field('rtrim(员工姓名) as 姓名')->find();
  647. $list['class'][] = [
  648. '编号' => $class['bh' . $i],
  649. '姓名' => $name['姓名'],
  650. '比例' => $class['rate'.$i],
  651. ];
  652. }
  653. }
  654. $list['class'] = array_values($list['class']);
  655. }
  656. $this->success('成功',$list);
  657. }
  658. /**
  659. * 修改糊盒班组报工资料
  660. * @return void
  661. * @throws \think\Exception
  662. * @throws \think\db\exception\BindParamException
  663. * @throws \think\exception\PDOException
  664. */
  665. public function getGluingReportDetailUpdate()
  666. {
  667. if ($this->request->isPost() === false){
  668. $this->error('请求错误');
  669. }
  670. $param = Request::instance()->post();
  671. if(!isset($param['id']) || empty($param['id'])){
  672. $this->error('参数错误');
  673. }
  674. $id = $param['id'];
  675. $role = $param['role'];
  676. unset($param['id']);
  677. // 核心修复:验证规则调整,明确允许#字符,兼容特殊字符
  678. $validate = new \think\Validate([
  679. 'sczl_jtbh' => 'require|regex:/^[\w#\x{4e00}-\x{9fa5}]+$/u', // 允许字母、数字、下划线、#、中文
  680. 'sczl_gdbh' => 'require',
  681. 'sczl_gxmc' => 'require',
  682. 'sczl_rq' => 'require|dateFormat:Y-m-d H:i:s',
  683. ], [
  684. 'sczl_jtbh.require' => '机组编号不能为空',
  685. 'sczl_jtbh.regex' => '机组编号仅支持字母、数字、下划线、#号和中文',
  686. 'sczl_gdbh.require' => '工单号不能为空',
  687. 'sczl_gxmc.require' => '工序名称不能为空',
  688. 'sczl_rq.require' => '报工日期不能为空',
  689. 'sczl_rq.dateFormat' => '报工日期格式错误(需为Y-m-d H:i:s)',
  690. ]);
  691. // 验证前处理:仅去除前后空格(不影响#字符)
  692. foreach (['sczl_jtbh', 'sczl_gdbh', 'sczl_gxmc', 'sczl_rq'] as $field) {
  693. if (isset($param[$field]) && is_string($param[$field])) {
  694. $param[$field] = trim($param[$field]); // 只去空格,保留#等特殊字符
  695. }
  696. }
  697. // 执行验证
  698. if (!$validate->check($param)) {
  699. $this->error($validate->getError());
  700. }
  701. // 3. 事务处理(确保数据一致性)
  702. $currentTime = date('Y-m-d H:i:s');
  703. $tableClass = '糊盒报工班组';
  704. $tableDevice = '设备_糊盒报工资料';
  705. // 4. 构建班组数据(保留#字符,无需额外处理)
  706. $classData = [
  707. 'jtbh' => $param['sczl_jtbh'], // 直接保留含#的原始值
  708. 'gdbh' => $param['sczl_gdbh'],
  709. 'gxmc' => $param['sczl_gxmc'],
  710. 'sczl_rq' => $param['sczl_rq'],
  711. 'sczl_bzdh' => $param['sczl_bzdh'],
  712. ];
  713. // 处理动态字段(1-30组bh/rate)
  714. $dynamicFields = [];
  715. for ($i = 1; $i <= 30; $i++) {
  716. $dynamicFields['bh' . $i] = isset($param['bh' . $i]) ? $param['bh' . $i] : '';
  717. $dynamicFields['rate' . $i] = isset($param['rate' . $i]) ? $param['rate' . $i] : '';
  718. }
  719. $classData = array_merge($classData, $dynamicFields);
  720. // 5. 保存班组数据(框架会自动处理特殊字符转义,避免SQL注入)
  721. $classId = Db::name($tableClass)->where('id',$role)->update($classData);
  722. // 6. 构建设备数据(适配PHP7.2)
  723. $filteredParam = array_filter($param, function ($key) {
  724. return !preg_match('/^(bh|rate)\d+$/', $key);
  725. }, ARRAY_FILTER_USE_KEY);
  726. $deviceData = array_merge([
  727. 'mod_rq' => $currentTime,
  728. ], $filteredParam);
  729. // 7. 保存设备数据(含#字符的字段会被自动转义,安全插入)
  730. $sql = Db::name($tableDevice)->where('Uid',$id)->fetchSql(true)->update($deviceData);
  731. $saveResult = db()->query($sql);
  732. if ($saveResult !== false) {
  733. $this->success('修改成功');
  734. }else{
  735. $this->error('修改失败');
  736. }
  737. }
  738. /**
  739. * 修改报工数据班组
  740. * @return void
  741. * @throws \think\Exception
  742. * @throws \think\exception\PDOException
  743. */
  744. public function UpdateGluingReportClass()
  745. {
  746. if ($this->request->isPost() === false){
  747. $this->error('请求错误');
  748. }
  749. $params = Request::instance()->post();
  750. if(!isset($params['id']) || empty($params['id'])){
  751. $this->error('参数错误');
  752. }
  753. $id = $params['id'];
  754. $data['role'] = $params['role'];
  755. $res = \db('设备_糊盒报工资料')->where('Uid',$id)->update($data);
  756. if ($res === false) {
  757. $this->error('修改失败');
  758. }else{
  759. $this->success('修改成功');
  760. }
  761. }
  762. /**
  763. * 当班产量
  764. * @return void
  765. * @throws \think\db\exception\DataNotFoundException
  766. * @throws \think\db\exception\ModelNotFoundException
  767. * @throws \think\exception\DbException
  768. */
  769. public function GluingReportList()
  770. {
  771. if ($this->request->isGet() === false){
  772. $this->error('请求错误');
  773. }
  774. $params = $this->request->param();
  775. if (!isset($params['machine']) || empty($params['machine'])){
  776. $this->error('参数错误');
  777. }
  778. $machine = $params['machine'];
  779. $sczlTime = date('Y-m-d H:i:s',time());
  780. if ($sczlTime>date('Y-m-d 00:00:00') && $sczlTime<date('Y-m-d 08:30:00')){
  781. $time = date('Y-m-d 00:00:00',time()-86400);
  782. }else{
  783. $time = date('Y-m-d 00:00:00');
  784. }
  785. $where = [
  786. 'sczl_jtbh' => $machine,
  787. 'sczl_rq' => $time,
  788. ];
  789. $field = 'rtrim(sczl_gdbh) as 工单编号,rtrim(sczl_yjno) as yjno,rtrim(sczl_dedh) as dedh,rtrim(sczl_gxmc) as gxmc,
  790. rtrim(sczl_cl) as 产量,rtrim(sczl_zcfp) as 制程废品,rtrim(装版工时) as 装版工时,rtrim(保养工时) as 保养工时,
  791. rtrim(异常工时) as 异常工时,rtrim(设备运行工时) as 通电工时,startTime,endTime,Uid,rtrim(sczl_ls) as ls,来料数量';
  792. $list = \db('设备_糊盒报工资料')->where($where)->field($field)->order('Uid desc')->select();
  793. if (!empty($list)){
  794. foreach ($list as $k=>$v){
  795. $name = \db('工单_印件资料')->where('Yj_Gdbh',$v['工单编号'])->where('yj_Yjno',$v['yjno'])->field('rtrim(yj_yjmc) as cpmc')->find();
  796. if ($v['yjno']<10){
  797. $list[$k]['yjno'] = '0'.$v['yjno'];
  798. }
  799. $list[$k]['印件及工序'] = $list[$k]['yjno'].'-'.$v['gxmc'];
  800. $list[$k]['生产时间段'] = substr($v['startTime'],5,5).' '.substr($v['startTime'],11,5).'<-->'.substr($v['endTime'],5,5).' '.substr($v['endTime'],11,5);
  801. $list[$k]['产品名称'] = $name['cpmc'];
  802. }
  803. }
  804. $this->success('成功',$list);
  805. }
  806. /**
  807. * 工单查询
  808. * @return void
  809. * @throws \think\db\exception\DataNotFoundException
  810. * @throws \think\db\exception\ModelNotFoundException
  811. * @throws \think\exception\DbException
  812. */
  813. public function getWorkOrderList()
  814. {
  815. if ($this->request->isGet() === false){
  816. $this->error('请求错误');
  817. }
  818. $params = $this->request->param();
  819. if (!isset($params['search']) || empty($params['search'])){
  820. $this->error('参数错误');
  821. }
  822. $search = $params['search'];
  823. $list = \db('工单_基本资料')
  824. ->where('Gd_gdbh|Gd_cpmc','like',$search.'%')
  825. ->field('Gd_gdbh as 工单编号,Gd_cpdh as 产品代号,Gd_cpmc as 产品名称')
  826. ->order('Gd_gdbh desc')
  827. ->select();
  828. if (empty($list)){
  829. $this->error('未找到数据');
  830. }else{
  831. $this->success('成功',$list);
  832. }
  833. }
  834. /**
  835. * 查询机台编号
  836. * @return void
  837. */
  838. public function getMachineList()
  839. {
  840. if ($this->request->isGet() === false){
  841. $this->error('请求错误');
  842. }
  843. $params = $this->request->param();
  844. if (!isset($params['search']) || empty($params['search'])){
  845. $this->error('参数错误');
  846. }
  847. $search = $params['search'];
  848. $list = \db('设备_基本资料')
  849. ->where('使用部门',$search)
  850. ->column('设备名称','设备编号');
  851. if (empty($list)){
  852. $this->error('未找到数据');
  853. }else{
  854. $this->success('成功',$list);
  855. }
  856. }
  857. /**
  858. * 查询工艺名称
  859. * @return void
  860. * @throws \think\db\exception\DataNotFoundException
  861. * @throws \think\db\exception\ModelNotFoundException
  862. * @throws \think\exception\DbException
  863. */
  864. public function gitProcessList()
  865. {
  866. if ($this->request->isGet() === false){
  867. $this->error('请求错误');
  868. }
  869. $params = $this->request->param();
  870. if (!isset($params['search']) || empty($params['search'])){
  871. $this->error('参数错误');
  872. }
  873. $search = $params['search'];
  874. $list = \db('erp_常用字典')
  875. ->where('分类','糊盒工艺')
  876. ->where('名称','like','%'.$search.'%')
  877. ->field('名称')
  878. ->select();
  879. $data = [];
  880. if (empty($list)){
  881. $this->error('未找到工艺数据');
  882. }
  883. foreach ($list as $k=>$v){
  884. $name = explode('_',$v['名称']);
  885. $data[] = $name[2];
  886. }
  887. $this->success('成功',$data);
  888. }
  889. //最新糊盒班组查询
  890. public function classifyList()
  891. {
  892. // 1. 请求方法验证
  893. if (!$this->request->isGet()) {
  894. $this->error('请使用GET请求');
  895. }
  896. // 2. 参数验证
  897. $params = $this->request->param();
  898. // 验证必需参数
  899. $requiredParams = ['sczl_jtbh', 'sczl_bzdh'];
  900. foreach ($requiredParams as $param) {
  901. if (empty($params[$param] ?? null)) {
  902. $this->error("缺少必要参数: {$param}");
  903. }
  904. }
  905. $jtbh = trim($params['sczl_jtbh']);
  906. $bzdh = trim($params['sczl_bzdh']);
  907. // 3. 查询糊盒报工资料
  908. $role = \db('设备_糊盒报工资料')
  909. ->where('sczl_jtbh', $jtbh)
  910. ->where('sczl_bzdh', $bzdh)
  911. ->order('Uid', 'desc')
  912. ->value('role');
  913. if (empty($role)) {
  914. $this->error('未找到对应的报工班组信息');
  915. }
  916. // 4. 动态生成字段列表
  917. $fields = [];
  918. for ($i = 1; $i <= 30; $i++) {
  919. $fields[] = "bh{$i}";
  920. $fields[] = "rate{$i}";
  921. }
  922. // 5. 查询班组信息
  923. $class = \db('糊盒报工班组')
  924. ->where('id', $role)
  925. ->field($fields)
  926. ->find();
  927. if (empty($class)) {
  928. $this->error('班组信息不存在');
  929. }
  930. // 6. 处理班组成员信息
  931. $members = [];
  932. $employeeIds = [];
  933. // 收集所有有效的员工编号
  934. for ($i = 1; $i <= 30; $i++) {
  935. $bh = $class["bh{$i}"] ?? '';
  936. $rate = $class["rate{$i}"] ?? 0;
  937. // 验证员工编号是否有效
  938. $bh = trim($bh);
  939. if (!empty($bh) && $bh !== '000000') {
  940. $employeeIds[$bh] = [
  941. 'index' => $i,
  942. 'rate' => $rate
  943. ];
  944. }
  945. }
  946. // 如果有有效的员工编号,批量查询员工信息
  947. if (!empty($employeeIds)) {
  948. $employeeNames = [];
  949. $ids = array_keys($employeeIds);
  950. // 批量查询员工信息(减少数据库查询次数)
  951. $result = \db('人事_基本资料')
  952. ->whereIn('员工编号', $ids)
  953. ->field('员工编号, rtrim(员工姓名) as 姓名')
  954. ->select();
  955. foreach ($result as $item) {
  956. $employeeNames[$item['员工编号']] = $item['姓名'];
  957. }
  958. // 组装返回数据
  959. foreach ($employeeIds as $employeeId => $info) {
  960. $members[] = [
  961. '编号' => $employeeId,
  962. '姓名' => $employeeNames[$employeeId] ?? '未知',
  963. '比例' => $info['rate']
  964. ];
  965. }
  966. }
  967. $this->success('获取成功', array_values($members));
  968. }
  969. }