GluingReport.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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. ];
  330. // 处理动态字段(1-30组bh/rate)
  331. $dynamicFields = [];
  332. for ($i = 1; $i <= 30; $i++) {
  333. $dynamicFields['bh' . $i] = isset($param['bh' . $i]) ? $param['bh' . $i] : '';
  334. $dynamicFields['rate' . $i] = isset($param['rate' . $i]) ? $param['rate' . $i] : '';
  335. }
  336. $classData = array_merge($classData, $dynamicFields);
  337. // 5. 保存班组数据(框架会自动处理特殊字符转义,避免SQL注入)
  338. $classId = Db::name($tableClass)->insertGetId($classData);
  339. if (empty($classId)) {
  340. throw new \Exception("{$tableClass}数据保存失败");
  341. }
  342. // 6. 构建设备数据(适配PHP7.2)
  343. $filteredParam = array_filter($param, function ($key) {
  344. return !preg_match('/^(bh|rate)\d+$/', $key);
  345. }, ARRAY_FILTER_USE_KEY);
  346. $deviceData = array_merge([
  347. 'role' => $classId,
  348. 'sys_rq' => $currentTime,
  349. ], $filteredParam);
  350. // 7. 保存设备数据(含#字符的字段会被自动转义,安全插入)
  351. $sql = Db::name($tableDevice)->fetchSql(true)->insert($deviceData);
  352. $saveResult = db()->query($sql);
  353. if ($saveResult !== false) {
  354. $this->success('报工提交成功');
  355. }else{
  356. $this->error('报工提交失败');
  357. }
  358. }
  359. /**
  360. * 糊盒机台报工数据菜单
  361. * @return void
  362. * @throws \think\db\exception\DataNotFoundException
  363. * @throws \think\db\exception\ModelNotFoundException
  364. * @throws \think\exception\DbException
  365. */
  366. public function getTab()
  367. {
  368. if ($this->request->isGet() === false){
  369. $this->error('请求错误');
  370. }
  371. $data = [];
  372. $date = date('Y-m-d 00:00:00',time()-3888000);
  373. $sist = ['印后糊盒车间','精品自动化车间','数字化车间'];
  374. $department = \db('设备_基本资料')
  375. ->distinct(true)
  376. ->where('使用部门','in',$sist)
  377. ->where('设备编组','<>','')
  378. ->order('设备编组')
  379. ->column('rtrim(使用部门) as 使用部门');
  380. if (empty($department)){
  381. $this->success('未获取到机台数据');
  382. }
  383. $list = \db('设备_糊盒报工资料')
  384. ->field([
  385. 'DISTINCT(sczl_rq)' => '时间',
  386. 'rtrim(sczl_jtbh)' => '机台编号'
  387. ])
  388. ->where('sczl_rq','>',$date)
  389. ->order('sczl_rq desc')
  390. ->select();
  391. foreach ($department as $value){
  392. $machine = \db('设备_基本资料')
  393. ->where('使用部门',$value)
  394. ->field('rtrim(设备编号) as 设备编号,rtrim(设备名称) as 设备名称')
  395. ->order('设备编号')
  396. ->select();
  397. foreach ($machine as $k=>$v){
  398. $data[rtrim($value)][$v['设备编号'].'-->'.$v['设备名称']] = [];
  399. foreach ($list as $kk=>$vv){
  400. if ($v['设备编号'] === $vv['机台编号']){
  401. array_push($data[rtrim($value)][$v['设备编号'].'-->'.$v['设备名称']],date('Y-m-d',strtotime($vv['时间'])));
  402. }
  403. }
  404. }
  405. }
  406. $this->success('成功',$data);
  407. }
  408. /**
  409. * 获取当前生产工单
  410. * @return void
  411. * @throws \think\db\exception\DataNotFoundException
  412. * @throws \think\db\exception\ModelNotFoundException
  413. * @throws \think\exception\DbException
  414. */
  415. public function GetProduction()
  416. {
  417. if (Request::instance()->isGet() == false) {
  418. $this->error('非法请求');
  419. }
  420. $params = Request::instance()->param();
  421. if (!isset($params['machine']) || empty($params['machine'])) {
  422. $this->error('参数错误');
  423. }
  424. $machine = $params['machine'];
  425. $machineCode = \db('dic_lzde')->where('适用机型',$machine)->value('sys_bh');
  426. $data = \db('设备_糊盒报工采集')->where('sczl_jtbh',$machine)->order('UID desc')->find();
  427. if (empty($data)){
  428. $this->success('未找到数据');
  429. }
  430. $list = [];
  431. $list['班组ID'] = $data['班组ID'];
  432. $row = [];
  433. if (!empty($data['sczl_gdbh'])){
  434. $endTime = \db('工单_工艺资料')
  435. ->where('Gy0_gdbh',$data['sczl_gdbh'])
  436. ->where('Gy0_yjno',$data['sczl_yjno'])
  437. ->where('Gy0_gxh',$data['sczl_gxh'])
  438. ->find();
  439. $list['工单编号'] = $data['sczl_gdbh'];
  440. if (!empty($endTime)){
  441. $list['印件号'] = $data['sczl_yjno'];
  442. $name = \db('工单_基本资料')->where('Gd_Gdbh',$data['sczl_gdbh'])->value('成品名称');
  443. $code = \db('工单_基本资料')->where('Gd_Gdbh',$data['sczl_gdbh'])->value('成品代号');
  444. $list['产品名称'] = rtrim($name);
  445. $list['产品代号'] = rtrim($code);
  446. $list['工序名称'] = $data['sczl_gxmc'];
  447. $list['联数'] = $endTime['Gy0_ls'];
  448. }
  449. $list['状态'] = rtrim($data['status']);
  450. $list['开工时间'] = $data['sczl_kgsj'];
  451. }else{
  452. $list['工单编号'] = '';
  453. $list['印件号'] = 0;
  454. $list['产品名称'] = '';
  455. $list['工序名称'] = '';
  456. $list['产品代号'] = '';
  457. $list['状态'] = '';
  458. $list['开工时间'] = '';
  459. $list['联数'] = '';
  460. }
  461. $list['班组编号'] = rtrim($data['sczl_bzbh']);
  462. $idList = explode(',',$data['班组ID']);
  463. foreach ($idList as $k=>$v){
  464. $class = \db('设备_糊盒班组资料')
  465. ->where('id',$v)
  466. ->field("role,rate,bh1,bh2,bh3,bh4,bh5,bh6,bh7,bh8,bh9,bh10,bh11,bh12,bh13,bh14,bh15")
  467. ->find();
  468. if (!empty($class)){
  469. for ($i=1;$i<16;$i++) {
  470. if ($class['bh' . $i] != '' && $class['bh' . $i] != '000000') {
  471. $name = \db('人事_基本资料')->where('员工编号', $class['bh' . $i])->field('rtrim(员工姓名) as 姓名')->find();
  472. $row[] = [
  473. '编号' => $class['bh' . $i],
  474. '姓名' => $name['姓名'],
  475. '比例' => $class['rate'],
  476. '角色' => $class['role'],
  477. ];
  478. }
  479. }
  480. $row = array_values($row);
  481. }
  482. }
  483. $list['班组成员'] = $row;
  484. $list['定额代号'] = $machineCode;
  485. $this->success('成功',$list);
  486. }
  487. /**
  488. * 设置当前工单和当前班组
  489. * @return void
  490. */
  491. public function setMachineTeam()
  492. {
  493. if (Request::instance()->isPost() == false) {
  494. $this->error('非法请求');
  495. }
  496. $params = Request::instance()->post();
  497. if (!isset($params['machine']) || empty($params['machine'] )) {
  498. $this->error('参数不能为空');
  499. }
  500. if (empty($params['team_id']) || empty($params['sczl_bzdh'])){
  501. $this->error('请先选择班组成员');
  502. }
  503. $machine = $params['machine'] . '#';
  504. $data = [];
  505. $data['status'] = $params['status'];
  506. $data['sczl_sj'] = date('Y-m-d H:i:s');
  507. $data['sczl_jtbh'] = $machine;
  508. $data['sczl_gdbh'] = empty($params['order']) ? '':$params['order'];
  509. $data['sczl_yjno'] = empty($params['yjno']) ? '':$params['yjno'];
  510. $data['sczl_gxh'] = empty($params['gy_name']) ? '' : (int)substr($params['gy_name'], 0, 2);
  511. $data['sczl_gxmc'] = empty($params['gy_name']) ? '' : $params['gy_name'];
  512. $data['sczl_bzbh'] = $params['sczl_bzdh'];
  513. $data['班组ID'] = $params['team_id'];
  514. // 获取当前时间
  515. $current_time = time();
  516. // 设置时间范围
  517. $start_time1 = strtotime(date('Y-m-d') . ' 08:30:00');
  518. $end_time1 = strtotime(date('Y-m-d') . ' 20:30:00');
  519. $end_time2 = strtotime(date('Y-m-d') . ' 24:00:00');
  520. $start_time3 = strtotime(date('Y-m-d', strtotime('+1 day')) . ' 08:30:00');
  521. $start_time4 = strtotime(date('Y-m-d', strtotime('+1 day')) . ' 00:00:00');
  522. // 判断当前时间属于哪个时间范围
  523. if ($current_time >= $start_time1 && $current_time <= $end_time1) {
  524. $data['sczl_kgsj'] = date('Y-m-d') . ' 08:30:00';
  525. } elseif ($current_time > $end_time1 && $current_time <= $end_time2) {
  526. $data['sczl_kgsj'] = date('Y-m-d') . ' 20:30:00';
  527. } elseif ($current_time > $end_time1 && $current_time <= $start_time3) {
  528. $data['sczl_kgsj'] = date('Y-m-d', strtotime('+1 day')) . ' 08:30:00';
  529. }elseif ($current_time > $start_time4 && $current_time <= $start_time3){
  530. $data['sczl_kgsj'] = date('Y-m-d') . ' 20:30:00';
  531. }
  532. if (!empty($params['order']) && !empty($params['yjno'])){
  533. $option['Gy0_gdbh'] = $params['order'];
  534. $option['Gy0_yjno'] = $params['yjno'];
  535. $option['Gy0_gxh'] = $data['sczl_gxh'];
  536. $data['任务ID'] = \db('工单_工艺资料')->where($option)->value('UniqId');
  537. }else{
  538. $data['任务ID'] = '';
  539. }
  540. $sql = \db('设备_糊盒报工采集')->fetchSql(true)->insert($data);
  541. $res = Db::query($sql);
  542. if ($res === false) {
  543. $this->error('设置失败');
  544. } else {
  545. $this->success('设置成功');
  546. }
  547. }
  548. /**
  549. * 糊盒设备运行跟踪数据页面显示
  550. * @return void
  551. * @throws \think\db\exception\DataNotFoundException
  552. * @throws \think\db\exception\ModelNotFoundException
  553. * @throws \think\exception\DbException
  554. */
  555. public function getGluingReportDataList()
  556. {
  557. if ($this->request->isGet() === false){
  558. $this->error('请求错误');
  559. }
  560. $params = $this->request->param();
  561. if (empty($params['machine']) || empty($params['day'])){
  562. $this->error('参数错误');
  563. }
  564. $where = [
  565. 'a.sczl_jtbh' => $params['machine'],
  566. 'a.sczl_rq' => ['like',$params['day'].'%']
  567. ];
  568. $field = ['a.sczl_gdbh as 工单编号','a.sczl_gxmc as 工序名称','a.来料数量','a.sczl_cl as 产量',
  569. 'a.sczl_zcfp as 制程废品','a.startTime as 开始时间','a.endTime as 结束时间','a.sczl_ls as 联数','a.sczl_rq as 日期',
  570. 'a.sczl_dedh as 定额代号','a.工价系数','a.保养工时','a.装版工时','a.异常工时','a.异常类型','a.设备运行工时','a.role',
  571. 'a.sys_id as 创建人员','a.sys_rq as 上报时间','a.mod_rq as 修改时间','a.Uid','a.sczl_jtbh as 机台编号','a.price',
  572. 'b.Gd_cpdh as 产品代号','b.Gd_cpmc as 产品名称'];
  573. $list = \db('设备_糊盒报工资料')
  574. ->alias('a')
  575. ->join('工单_基本资料 b','a.sczl_gdbh = b.Gd_gdbh','left')
  576. ->field($field)
  577. ->where($where)
  578. ->group('a.Uid')
  579. ->select();
  580. if (empty($list)){
  581. $this->error('未找到报工数据');
  582. }
  583. foreach ($list as $key=>$value) {
  584. $list[$key]['class'] = [];
  585. $class = \db('糊盒报工班组')
  586. ->where('id', $value['role'])
  587. ->field("bh1,bh2,bh3,bh4,bh5,bh6,bh7,bh8,bh9,bh10,bh11,bh12,bh13,bh14,bh15,bh16,bh17,bh18,bh19,bh20,bh21,bh22,bh23,
  588. bh24,bh25,bh26,bh27,bh28,bh29,bh30,rate1,rate2,rate3,rate4,rate5,rate6,rate7,rate8,rate9,rate10,rate11,rate12,rate13,rate14,
  589. rate15,rate16,rate17,rate18,rate19,rate20,rate21,rate22,rate23,rate24,rate25,rate26,rate27,rate28,rate29,rate30")
  590. ->find();
  591. if (!empty($class)) {
  592. for ($i = 1; $i <=30; $i++) {
  593. if ($class['bh' . $i] != '' && $class['bh' . $i] != '000000') {
  594. $name = \db('人事_基本资料')->where('员工编号', $class['bh' . $i])->field('rtrim(员工姓名) as 姓名')->find();
  595. $list[$key]['class'][] = [
  596. '编号' => $class['bh' . $i],
  597. '姓名' => $name['姓名'],
  598. '比例' => $class['rate'.$i],
  599. ];
  600. }
  601. }
  602. $list[$key]['class'] = array_values($list[$key]['class']);
  603. }
  604. }
  605. $this->success('成功',$list);
  606. }
  607. /**
  608. * 报工数据详情显示
  609. * @return void
  610. * @throws \think\db\exception\DataNotFoundException
  611. * @throws \think\db\exception\ModelNotFoundException
  612. * @throws \think\exception\DbException
  613. */
  614. public function getGluingReportDataDetail()
  615. {
  616. if ($this->request->isGet() === false){
  617. $this->error('请求错误');
  618. }
  619. $params = $this->request->param();
  620. if(!isset($params['id']) || empty($params['id'])){
  621. $this->error('参数错误');
  622. }
  623. $where = ['Uid'=>$params['id']];
  624. $field = ['a.sczl_gdbh as 工单编号','a.sczl_gxmc as 工序名称','a.来料数量','a.sczl_cl as 产量',
  625. 'a.sczl_zcfp as 制程废品','a.startTime as 开始时间','a.endTime as 结束时间','a.sczl_ls as 联数','a.sczl_rq as 日期',
  626. 'a.sczl_dedh as 定额代号','a.工价系数','a.保养工时','a.装版工时','a.异常工时','a.异常类型','a.设备运行工时','a.role',
  627. 'a.sys_id as 创建人员','a.sys_rq as 上报时间','a.mod_rq as 修改时间','a.Uid','a.sczl_jtbh as 机台编号','a.price',
  628. 'b.Gd_cpdh as 产品代号','b.Gd_cpmc as 产品名称'];
  629. $list = \db('设备_糊盒报工资料')
  630. ->alias('a')
  631. ->join('工单_基本资料 b','a.sczl_gdbh = b.Gd_gdbh','left')
  632. ->where($where)
  633. ->field($field)
  634. ->find();
  635. $list['class'] = [];
  636. $class = \db('糊盒报工班组')
  637. ->where('id', $list['role'])
  638. ->field("bh1,bh2,bh3,bh4,bh5,bh6,bh7,bh8,bh9,bh10,bh11,bh12,bh13,bh14,bh15,bh16,bh17,bh18,bh19,bh20,bh21,bh22,bh23,
  639. bh24,bh25,bh26,bh27,bh28,bh29,bh30,rate1,rate2,rate3,rate4,rate5,rate6,rate7,rate8,rate9,rate10,rate11,rate12,rate13,rate14,
  640. rate15,rate16,rate17,rate18,rate19,rate20,rate21,rate22,rate23,rate24,rate25,rate26,rate27,rate28,rate29,rate30")
  641. ->find();
  642. if (!empty($class)) {
  643. for ($i = 1; $i <=30; $i++) {
  644. if ($class['bh' . $i] != '' && $class['bh' . $i] != '000000') {
  645. $name = \db('人事_基本资料')->where('员工编号', $class['bh' . $i])->field('rtrim(员工姓名) as 姓名')->find();
  646. $list['class'][] = [
  647. '编号' => $class['bh' . $i],
  648. '姓名' => $name['姓名'],
  649. '比例' => $class['rate'.$i],
  650. ];
  651. }
  652. }
  653. $list['class'] = array_values($list['class']);
  654. }
  655. $this->success('成功',$list);
  656. }
  657. /**
  658. * 修改糊盒班组报工资料
  659. * @return void
  660. * @throws \think\Exception
  661. * @throws \think\db\exception\BindParamException
  662. * @throws \think\exception\PDOException
  663. */
  664. public function getGluingReportDetailUpdate()
  665. {
  666. if ($this->request->isPost() === false){
  667. $this->error('请求错误');
  668. }
  669. $param = Request::instance()->post();
  670. if(!isset($param['id']) || empty($param['id'])){
  671. $this->error('参数错误');
  672. }
  673. $id = $param['id'];
  674. $role = $param['role'];
  675. unset($param['id']);
  676. // 核心修复:验证规则调整,明确允许#字符,兼容特殊字符
  677. $validate = new \think\Validate([
  678. 'sczl_jtbh' => 'require|regex:/^[\w#\x{4e00}-\x{9fa5}]+$/u', // 允许字母、数字、下划线、#、中文
  679. 'sczl_gdbh' => 'require',
  680. 'sczl_gxmc' => 'require',
  681. 'sczl_rq' => 'require|dateFormat:Y-m-d H:i:s',
  682. ], [
  683. 'sczl_jtbh.require' => '机组编号不能为空',
  684. 'sczl_jtbh.regex' => '机组编号仅支持字母、数字、下划线、#号和中文',
  685. 'sczl_gdbh.require' => '工单号不能为空',
  686. 'sczl_gxmc.require' => '工序名称不能为空',
  687. 'sczl_rq.require' => '报工日期不能为空',
  688. 'sczl_rq.dateFormat' => '报工日期格式错误(需为Y-m-d H:i:s)',
  689. ]);
  690. // 验证前处理:仅去除前后空格(不影响#字符)
  691. foreach (['sczl_jtbh', 'sczl_gdbh', 'sczl_gxmc', 'sczl_rq'] as $field) {
  692. if (isset($param[$field]) && is_string($param[$field])) {
  693. $param[$field] = trim($param[$field]); // 只去空格,保留#等特殊字符
  694. }
  695. }
  696. // 执行验证
  697. if (!$validate->check($param)) {
  698. $this->error($validate->getError());
  699. }
  700. // 3. 事务处理(确保数据一致性)
  701. $currentTime = date('Y-m-d H:i:s');
  702. $tableClass = '糊盒报工班组';
  703. $tableDevice = '设备_糊盒报工资料';
  704. // 4. 构建班组数据(保留#字符,无需额外处理)
  705. $classData = [
  706. 'jtbh' => $param['sczl_jtbh'], // 直接保留含#的原始值
  707. 'gdbh' => $param['sczl_gdbh'],
  708. 'gxmc' => $param['sczl_gxmc'],
  709. 'sczl_rq' => $param['sczl_rq'],
  710. ];
  711. // 处理动态字段(1-30组bh/rate)
  712. $dynamicFields = [];
  713. for ($i = 1; $i <= 30; $i++) {
  714. $dynamicFields['bh' . $i] = isset($param['bh' . $i]) ? $param['bh' . $i] : '';
  715. $dynamicFields['rate' . $i] = isset($param['rate' . $i]) ? $param['rate' . $i] : '';
  716. }
  717. $classData = array_merge($classData, $dynamicFields);
  718. // 5. 保存班组数据(框架会自动处理特殊字符转义,避免SQL注入)
  719. $classId = Db::name($tableClass)->where('id',$role)->update($classData);
  720. // 6. 构建设备数据(适配PHP7.2)
  721. $filteredParam = array_filter($param, function ($key) {
  722. return !preg_match('/^(bh|rate)\d+$/', $key);
  723. }, ARRAY_FILTER_USE_KEY);
  724. $deviceData = array_merge([
  725. 'mod_rq' => $currentTime,
  726. ], $filteredParam);
  727. // 7. 保存设备数据(含#字符的字段会被自动转义,安全插入)
  728. $sql = Db::name($tableDevice)->where('Uid',$id)->fetchSql(true)->update($deviceData);
  729. $saveResult = db()->query($sql);
  730. if ($saveResult !== false) {
  731. $this->success('修改成功');
  732. }else{
  733. $this->error('修改失败');
  734. }
  735. }
  736. /**
  737. * 修改报工数据班组
  738. * @return void
  739. * @throws \think\Exception
  740. * @throws \think\exception\PDOException
  741. */
  742. public function UpdateGluingReportClass()
  743. {
  744. if ($this->request->isPost() === false){
  745. $this->error('请求错误');
  746. }
  747. $params = Request::instance()->post();
  748. if(!isset($params['id']) || empty($params['id'])){
  749. $this->error('参数错误');
  750. }
  751. $id = $params['id'];
  752. $data['role'] = $params['role'];
  753. $res = \db('设备_糊盒报工资料')->where('Uid',$id)->update($data);
  754. if ($res === false) {
  755. $this->error('修改失败');
  756. }else{
  757. $this->success('修改成功');
  758. }
  759. }
  760. /**
  761. * 当班产量
  762. * @return void
  763. * @throws \think\db\exception\DataNotFoundException
  764. * @throws \think\db\exception\ModelNotFoundException
  765. * @throws \think\exception\DbException
  766. */
  767. public function GluingReportList()
  768. {
  769. if ($this->request->isGet() === false){
  770. $this->error('请求错误');
  771. }
  772. $params = $this->request->param();
  773. if (!isset($params['machine']) || empty($params['machine'])){
  774. $this->error('参数错误');
  775. }
  776. $machine = $params['machine'];
  777. $sczlTime = date('Y-m-d H:i:s',time());
  778. if ($sczlTime>date('Y-m-d 00:00:00') && $sczlTime<date('Y-m-d 08:30:00')){
  779. $time = date('Y-m-d 00:00:00',time()-86400);
  780. }else{
  781. $time = date('Y-m-d 00:00:00');
  782. }
  783. $where = [
  784. 'sczl_jtbh' => $machine,
  785. 'sczl_rq' => $time,
  786. ];
  787. $field = 'rtrim(sczl_gdbh) as 工单编号,rtrim(sczl_yjno) as yjno,rtrim(sczl_dedh) as dedh,rtrim(sczl_gxmc) as gxmc,
  788. rtrim(sczl_cl) as 产量,rtrim(sczl_zcfp) as 制程废品,rtrim(装版工时) as 装版工时,rtrim(保养工时) as 保养工时,
  789. rtrim(异常工时) as 异常工时,rtrim(设备运行工时) as 通电工时,startTime,endTime,Uid,rtrim(sczl_ls) as ls,来料数量';
  790. $list = \db('设备_糊盒报工资料')->where($where)->field($field)->order('Uid desc')->select();
  791. if (!empty($list)){
  792. foreach ($list as $k=>$v){
  793. $name = \db('工单_印件资料')->where('Yj_Gdbh',$v['工单编号'])->where('yj_Yjno',$v['yjno'])->field('rtrim(yj_yjmc) as cpmc')->find();
  794. if ($v['yjno']<10){
  795. $list[$k]['yjno'] = '0'.$v['yjno'];
  796. }
  797. $list[$k]['印件及工序'] = $list[$k]['yjno'].'-'.$v['gxmc'];
  798. $list[$k]['生产时间段'] = substr($v['startTime'],5,5).' '.substr($v['startTime'],11,5).'<-->'.substr($v['endTime'],5,5).' '.substr($v['endTime'],11,5);
  799. $list[$k]['产品名称'] = $name['cpmc'];
  800. }
  801. }
  802. $this->success('成功',$list);
  803. }
  804. /**
  805. * 工单查询
  806. * @return void
  807. * @throws \think\db\exception\DataNotFoundException
  808. * @throws \think\db\exception\ModelNotFoundException
  809. * @throws \think\exception\DbException
  810. */
  811. public function getWorkOrderList()
  812. {
  813. if ($this->request->isGet() === false){
  814. $this->error('请求错误');
  815. }
  816. $params = $this->request->param();
  817. if (!isset($params['search']) || empty($params['search'])){
  818. $this->error('参数错误');
  819. }
  820. $search = $params['search'];
  821. $list = \db('工单_基本资料')
  822. ->where('Gd_gdbh|Gd_cpmc','like',$search.'%')
  823. ->field('Gd_gdbh as 工单编号,Gd_cpdh as 产品代号,Gd_cpmc as 产品名称')
  824. ->order('Gd_gdbh desc')
  825. ->select();
  826. if (empty($list)){
  827. $this->error('未找到数据');
  828. }else{
  829. $this->success('成功',$list);
  830. }
  831. }
  832. /**
  833. * 查询机台编号
  834. * @return void
  835. */
  836. public function getMachineList()
  837. {
  838. if ($this->request->isGet() === false){
  839. $this->error('请求错误');
  840. }
  841. $params = $this->request->param();
  842. if (!isset($params['search']) || empty($params['search'])){
  843. $this->error('参数错误');
  844. }
  845. $search = $params['search'];
  846. $list = \db('设备_基本资料')
  847. ->where('使用部门',$search)
  848. ->column('设备名称','设备编号');
  849. if (empty($list)){
  850. $this->error('未找到数据');
  851. }else{
  852. $this->success('成功',$list);
  853. }
  854. }
  855. /**
  856. * 查询工艺名称
  857. * @return void
  858. * @throws \think\db\exception\DataNotFoundException
  859. * @throws \think\db\exception\ModelNotFoundException
  860. * @throws \think\exception\DbException
  861. */
  862. public function gitProcessList()
  863. {
  864. if ($this->request->isGet() === false){
  865. $this->error('请求错误');
  866. }
  867. $params = $this->request->param();
  868. if (!isset($params['search']) || empty($params['search'])){
  869. $this->error('参数错误');
  870. }
  871. $search = $params['search'];
  872. $list = \db('erp_常用字典')
  873. ->where('分类','糊盒工艺')
  874. ->where('名称','like','%'.$search.'%')
  875. ->field('名称')
  876. ->select();
  877. $data = [];
  878. if (empty($list)){
  879. $this->error('未找到工艺数据');
  880. }
  881. foreach ($list as $k=>$v){
  882. $name = explode('_',$v['名称']);
  883. $data[] = $name[2];
  884. }
  885. $this->success('成功',$data);
  886. }
  887. }