GluingReport.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. ->order('设备编号')
  851. ->column('设备名称','设备编号');
  852. if (empty($list)){
  853. $this->error('未找到数据');
  854. }else{
  855. $this->success('成功',$list);
  856. }
  857. }
  858. /**
  859. * 查询工艺名称
  860. * @return void
  861. * @throws \think\db\exception\DataNotFoundException
  862. * @throws \think\db\exception\ModelNotFoundException
  863. * @throws \think\exception\DbException
  864. */
  865. public function gitProcessList()
  866. {
  867. if ($this->request->isGet() === false){
  868. $this->error('请求错误');
  869. }
  870. $params = $this->request->param();
  871. if (!isset($params['search']) || empty($params['search'])){
  872. $this->error('参数错误');
  873. }
  874. $search = $params['search'];
  875. $list = \db('erp_常用字典')
  876. ->where('分类','糊盒工艺')
  877. ->where('名称','like','%'.$search.'%')
  878. ->field('名称')
  879. ->select();
  880. $data = [];
  881. if (empty($list)){
  882. $this->error('未找到工艺数据');
  883. }
  884. foreach ($list as $k=>$v){
  885. $name = explode('_',$v['名称']);
  886. $data[] = $name[2];
  887. }
  888. $this->success('成功',$data);
  889. }
  890. //最新糊盒班组查询
  891. public function classifyList()
  892. {
  893. // 1. 请求方法验证
  894. if (!$this->request->isGet()) {
  895. $this->error('请使用GET请求');
  896. }
  897. // 2. 参数验证
  898. $params = $this->request->param();
  899. // 验证必需参数
  900. $requiredParams = ['sczl_jtbh', 'sczl_bzdh'];
  901. foreach ($requiredParams as $param) {
  902. if (empty($params[$param] ?? null)) {
  903. $this->error("缺少必要参数: {$param}");
  904. }
  905. }
  906. $jtbh = trim($params['sczl_jtbh']);
  907. $bzdh = trim($params['sczl_bzdh']);
  908. // 3. 查询糊盒报工资料
  909. $role = \db('设备_糊盒报工资料')
  910. ->where('sczl_jtbh', $jtbh)
  911. ->where('sczl_bzdh', $bzdh)
  912. ->order('Uid', 'desc')
  913. ->value('role');
  914. if (empty($role)) {
  915. $this->error('未找到对应的报工班组信息');
  916. }
  917. // 4. 动态生成字段列表
  918. $fields = [];
  919. for ($i = 1; $i <= 30; $i++) {
  920. $fields[] = "bh{$i}";
  921. $fields[] = "rate{$i}";
  922. }
  923. // 5. 查询班组信息
  924. $class = \db('糊盒报工班组')
  925. ->where('id', $role)
  926. ->field($fields)
  927. ->find();
  928. if (empty($class)) {
  929. $this->error('班组信息不存在');
  930. }
  931. // 6. 处理班组成员信息
  932. $members = [];
  933. $employeeIds = [];
  934. // 收集所有有效的员工编号
  935. for ($i = 1; $i <= 30; $i++) {
  936. $bh = $class["bh{$i}"] ?? '';
  937. $rate = $class["rate{$i}"] ?? 0;
  938. // 验证员工编号是否有效
  939. $bh = trim($bh);
  940. if (!empty($bh) && $bh !== '000000') {
  941. $employeeIds[$bh] = [
  942. 'index' => $i,
  943. 'rate' => $rate
  944. ];
  945. }
  946. }
  947. // 如果有有效的员工编号,批量查询员工信息
  948. if (!empty($employeeIds)) {
  949. $employeeNames = [];
  950. $ids = array_keys($employeeIds);
  951. // 批量查询员工信息(减少数据库查询次数)
  952. $result = \db('人事_基本资料')
  953. ->whereIn('员工编号', $ids)
  954. ->field('员工编号, rtrim(员工姓名) as 姓名')
  955. ->select();
  956. foreach ($result as $item) {
  957. $employeeNames[$item['员工编号']] = $item['姓名'];
  958. }
  959. // 组装返回数据
  960. foreach ($employeeIds as $employeeId => $info) {
  961. $members[] = [
  962. '编号' => $employeeId,
  963. '姓名' => $employeeNames[$employeeId] ?? '未知',
  964. '比例' => $info['rate']
  965. ];
  966. }
  967. }
  968. $this->success('获取成功', array_values($members));
  969. }
  970. }