MachineList.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. <?php
  2. namespace app\api\controller;
  3. use think\Request;
  4. use app\common\controller\Api;
  5. use think\response\Json;
  6. use function fast\e;
  7. /**
  8. * 设备维修保养记录
  9. */
  10. class MachineList extends Api
  11. {
  12. protected $noNeedLogin = ['*'];
  13. protected $noNeedRight = ['*'];
  14. //设备维修保养记录左侧菜单
  15. public function getTab()
  16. {
  17. if ($this->request->isGet() === false){
  18. $this->error('请求错误');
  19. }
  20. $year = date('Y',time());
  21. $lastYear = date('Y', strtotime('-1 year'));
  22. //年度维修统计
  23. $yearResult1 = $this->getYearSite($year);
  24. $yearResult2 = $this->getYearSite($lastYear);
  25. $data['1、年度维修统计'] = [
  26. $year => $yearResult1,
  27. $lastYear => $yearResult2
  28. ];
  29. //月度维修记录
  30. $lastMouth = date('Y-m', strtotime('-1 year'));
  31. $mouthReault = $this->getMouthSite($lastMouth);
  32. $data['2、月度维修记录'] = $mouthReault;
  33. //年度保养记录
  34. $YearMaintenanceSist1 = $this->getYearMaintenanceSist($year);
  35. $YearMaintenanceSist2 = $this->getYearMaintenanceSist($lastYear);
  36. if (!empty($YearMaintenanceSist1)){
  37. $data['3、年度保养统计'][$year] = $YearMaintenanceSist1;
  38. }
  39. if (!empty($YearMaintenanceSist2)){
  40. $data['3、年度保养统计'][$lastYear] = $YearMaintenanceSist2;
  41. }
  42. //月度保养记录
  43. $mouthMaintenanceSist = $this->getMouthMaintenanceSist($lastMouth);
  44. $data['4、月度保养记录'] = $mouthMaintenanceSist;
  45. $this->success('成功',$data);
  46. }
  47. private function getYearSite($year)
  48. {
  49. $sist = db('设备_维修记录')
  50. ->where('报障时间','like',$year.'%')
  51. ->group('使用部门')
  52. ->column('rtrim(使用部门)');
  53. $data = [];
  54. if (!empty($sist)){
  55. foreach ($sist as $item){
  56. $count = db('设备_维修记录')
  57. ->where('使用部门',$item)
  58. ->where('报障时间','like',$year.'%')
  59. ->count();
  60. if ($count !== 0){
  61. $data[] = $item.'('.$count.'台次)';
  62. }
  63. }
  64. }
  65. return $data;
  66. }
  67. private function getMouthSite($mouth)
  68. {
  69. $mouth = db('设备_维修记录')
  70. ->where('报障时间','>',$mouth.'-01 00:00:00')
  71. ->where('报障时间','<',date('Y-m-d H:i:s',time()))
  72. ->group('date')
  73. ->order('date desc')
  74. ->column('DATE_FORMAT(报障时间, "%Y-%m") AS date');
  75. $data = [];
  76. foreach ($mouth as $item){
  77. $sist = db('设备_维修记录')
  78. ->where('报障时间','like',$item.'%')
  79. ->group('使用部门')
  80. ->column('rtrim(使用部门)');
  81. foreach ($sist as $value){
  82. $count = db('设备_维修记录')
  83. ->where('使用部门',$value)
  84. ->where('报障时间','like',$item.'%')
  85. ->count();
  86. $data[$item][] = $value.'('.$count.'台次)';
  87. }
  88. }
  89. return $data;
  90. }
  91. private function getYearMaintenanceSist($year)
  92. {
  93. $sist = db('设备_保养记录')
  94. ->where('保养开始时间','like',$year.'%')
  95. ->group('使用部门')
  96. ->column('rtrim(使用部门)');
  97. $data = [];
  98. if (!empty($sist)){
  99. foreach ($sist as $item){
  100. $count = db('设备_保养记录')
  101. ->where('使用部门',$item)
  102. ->where('保养开始时间','like',$year.'%')
  103. ->count();
  104. if ($count !== 0){
  105. $data[] = $item.'('.$count.'台次)';
  106. }
  107. }
  108. }
  109. return $data;
  110. }
  111. private function getMouthMaintenanceSist($mouth)
  112. {
  113. $mouth = db('设备_保养记录')
  114. ->where('保养开始时间','>',$mouth.'-01 00:00:00')
  115. ->where('保养开始时间','<',date('Y-m-d H:i:s',time()))
  116. ->group('date')
  117. ->order('date desc')
  118. ->column('DATE_FORMAT(保养开始时间, "%Y-%m") AS date');
  119. $data = [];
  120. foreach ($mouth as $item){
  121. $sist = db('设备_保养记录')
  122. ->where('保养开始时间','like',$item.'%')
  123. ->group('使用部门')
  124. ->column('rtrim(使用部门)');
  125. foreach ($sist as $value){
  126. $count = db('设备_保养记录')
  127. ->where('使用部门',$value)
  128. ->where('保养开始时间','like',$item.'%')
  129. ->count();
  130. $data[$item][] = $value.'('.$count.'台次)';
  131. }
  132. }
  133. return $data;
  134. }
  135. /**
  136. * 年度维修记录上方列表
  137. * @return Json|void
  138. */
  139. public function YearMaintenanceList()
  140. {
  141. // 验证请求方法
  142. if (!$this->request->isGet()) {
  143. $this->error('请求错误');
  144. }
  145. // 获取并验证参数
  146. $param = $this->request->param();
  147. if (empty($param) || !isset($param['sist']) || !isset($param['year'])) {
  148. $this->error('参数错误');
  149. }
  150. $department = $param['sist'];
  151. $year = $param['year'];
  152. try {
  153. // 获取该部门该年份的所有维修设备编号
  154. $machineQuery = db('设备_维修记录')
  155. ->where('使用部门', $department)
  156. ->where('报障时间', 'like', $year . '%')
  157. ->order('设备编号');
  158. $machines = $machineQuery->column('distinct(设备编号) as 设备编号');
  159. if (empty($machines)) {
  160. $this->success('成功', []);
  161. }
  162. // 预加载设备名称
  163. $machineNames = db('设备_基本资料')
  164. ->whereIn('设备编号', $machines)
  165. ->column('rtrim(设备名称) as 设备名称', '设备编号');
  166. $result = [];
  167. // 一次性获取所有维修记录
  168. $allRecords = db('设备_维修记录')
  169. ->where('使用部门', $department)
  170. ->where('报障时间', 'like', $year . '%')
  171. ->field([
  172. '设备编号',
  173. 'DATE_FORMAT(报障时间, "%m") AS month', // 直接提取月份数字
  174. 'COUNT(*) AS count'
  175. ])
  176. ->group('设备编号, DATE_FORMAT(报障时间, "%m")')
  177. ->select();
  178. // 按设备编号组织数据
  179. $recordsByMachine = [];
  180. foreach ($allRecords as $record) {
  181. $monthKey = $record['month']; // 直接使用月份数字作为键
  182. $recordsByMachine[$record['设备编号']][$monthKey] = $record['count'];
  183. }
  184. // 构建结果数组
  185. foreach ($machines as $machineId) {
  186. $monthData = [];
  187. $total = 0;
  188. if (isset($recordsByMachine[$machineId])) {
  189. foreach ($recordsByMachine[$machineId] as $month => $count) {
  190. $monthData[$month] = $count; // 保持月份键为字符串
  191. $total += $count;
  192. }
  193. }
  194. // 确保月份键是两位数格式
  195. $formattedMonthData = [];
  196. foreach ($monthData as $month => $count) {
  197. $formattedMonth = str_pad($month, 2, '0', STR_PAD_LEFT);
  198. $formattedMonthData[$formattedMonth] = $count;
  199. }
  200. $result[] = [
  201. '使用部门' => $department,
  202. '设备编号' => $machineId,
  203. '设备名称' => $machineNames[$machineId] ?? '未知设备',
  204. 'total' => $total
  205. ] + $formattedMonthData;
  206. }
  207. return json([
  208. 'code' => 0,
  209. 'msg' => '成功',
  210. 'time' =>time(),
  211. 'data' => $result
  212. ]);
  213. } catch (\Exception $e) {
  214. $this->error('查询失败: ' . $e->getMessage());
  215. }
  216. }
  217. /**
  218. * 设备维修记录配件详情
  219. * @return void
  220. * @throws \think\db\exception\DataNotFoundException
  221. * @throws \think\db\exception\ModelNotFoundException
  222. * @throws \think\exception\DbException
  223. */
  224. public function YearAccessoriesList()
  225. {
  226. if ($this->request->isGet() === false) {
  227. $this->error('请求错误');
  228. }
  229. $param = $this->request->param();
  230. if (empty($param) || !isset($param['machine']) || !isset($param['year']) || !isset($param['sist'])) {
  231. $this->error('参数错误');
  232. }
  233. $where = [];
  234. if (isset($param['type']) && $param['type'] == '修理') {
  235. $where['st_jylb'] =['like','修理领用%'];
  236. }
  237. $list = db('物料_收发记录')
  238. ->alias('a')
  239. ->join('物料_仓库信息 b', 'a.仓库编号 = b.编号','left')
  240. ->join('设备_基本资料 c', 'a.st_jtbh = c.设备编号','left')
  241. ->join('物料_存货编码 d', 'a.st_wlbh = d.物料代码')
  242. ->where('a.st_jtbh',$param['machine'])
  243. ->where('a.st_rq','like',$param['year'].'%')
  244. ->where(function ($query) {
  245. $query->where('a.仓库编号', '107')
  246. ->whereOr('a.仓库编号', 'Y107');
  247. })
  248. ->where($where)
  249. ->field(['DATE_FORMAT(a.st_rq, "%y-%m-%d") AS 日期','rtrim(a.st_jylb) as 交易类别','a.st_jtbh AS 机台编号','rtrim(a.仓库编号) as 仓库编号',
  250. 'rtrim(b.名称) as 仓库名称','rtrim(c.使用部门) as 领用部门','a.st_wlbh as 物料编号','rtrim(d.物料名称) as 物料名称','a.st_sl as 领用数量',
  251. 'rtrim(a.st_dw) as 领用单位','a.领用单价','a.st_sl*a.领用单价 as 金额','a.st_desc as 备注'])
  252. ->order('日期 DESC')
  253. ->select();
  254. if (empty($list)) {
  255. $this->error('未找到数据');
  256. }else{
  257. $this->success('成功', $list);
  258. }
  259. }
  260. /**
  261. * 年度保养统计上方几台列表
  262. * @return Json|void
  263. */
  264. public function YearMaintainList()
  265. {
  266. // 验证请求方法
  267. if (!$this->request->isGet()) {
  268. $this->error('请求错误');
  269. }
  270. // 获取并验证参数
  271. $param = $this->request->param();
  272. if (empty($param) || !isset($param['sist']) || !isset($param['year'])) {
  273. $this->error('参数错误');
  274. }
  275. $department = $param['sist'];
  276. $year = $param['year'];
  277. try {
  278. // 获取该部门该年份的所有维修设备编号
  279. $machineQuery = db('设备_保养记录')
  280. ->where('使用部门', $department)
  281. ->where('保养开始时间', 'like', $year . '%')
  282. ->order('设备编号');
  283. $machines = $machineQuery->column('distinct(设备编号) as 设备编号');
  284. if (empty($machines)) {
  285. $this->success('成功', []);
  286. }
  287. // 预加载设备名称
  288. $machineNames = db('设备_基本资料')
  289. ->whereIn('设备编号', $machines)
  290. ->column('rtrim(设备名称) as 设备名称', '设备编号');
  291. $result = [];
  292. // 一次性获取所有维修记录
  293. $allRecords = db('设备_保养记录')
  294. ->where('使用部门', $department)
  295. ->where('保养开始时间', 'like', $year . '%')
  296. ->field([
  297. '设备编号',
  298. 'DATE_FORMAT(保养开始时间, "%m") AS month', // 直接提取月份数字
  299. 'COUNT(*) AS count'
  300. ])
  301. ->group('设备编号, DATE_FORMAT(保养开始时间, "%m")')
  302. ->select();
  303. // 按设备编号组织数据
  304. $recordsByMachine = [];
  305. foreach ($allRecords as $record) {
  306. $monthKey = $record['month']; // 直接使用月份数字作为键
  307. $recordsByMachine[$record['设备编号']][$monthKey] = $record['count'];
  308. }
  309. // 构建结果数组
  310. foreach ($machines as $machineId) {
  311. $monthData = [];
  312. $total = 0;
  313. if (isset($recordsByMachine[$machineId])) {
  314. foreach ($recordsByMachine[$machineId] as $month => $count) {
  315. $monthData[$month] = $count; // 保持月份键为字符串
  316. $total += $count;
  317. }
  318. }
  319. // 确保月份键是两位数格式
  320. $formattedMonthData = [];
  321. foreach ($monthData as $month => $count) {
  322. $formattedMonth = str_pad($month, 2, '0', STR_PAD_LEFT);
  323. $formattedMonthData[$formattedMonth] = $count;
  324. }
  325. $result[] = [
  326. '使用部门' => $department,
  327. '设备编号' => $machineId,
  328. '设备名称' => $machineNames[$machineId] ?? '未知设备',
  329. 'total' => $total
  330. ] + $formattedMonthData;
  331. }
  332. return json([
  333. 'code' => 0,
  334. 'msg' => '成功',
  335. 'time' =>time(),
  336. 'data' => $result
  337. ]);
  338. } catch (\Exception $e) {
  339. $this->error('查询失败: ' . $e->getMessage());
  340. }
  341. }
  342. /**
  343. * 月度设备维修记录上方机台列表
  344. * @return void
  345. * @throws \think\db\exception\DataNotFoundException
  346. * @throws \think\db\exception\ModelNotFoundException
  347. * @throws \think\exception\DbException
  348. */
  349. public function MonthAccessoriesList()
  350. {
  351. if ($this->request->isGet() === false) {
  352. $this->error('请求错误');
  353. }
  354. $param = $this->request->param();
  355. if (empty($param)) {
  356. $this->error('参数错误');
  357. }
  358. $where = [];
  359. if (isset($param['mouth'])) {
  360. $where['a.报障时间'] = ['like', $param['mouth'] . '%'];
  361. }
  362. if (isset($param['sist'])) {
  363. $where['a.使用部门'] = $param['sist'];
  364. }
  365. if (isset($param['search'])) {
  366. $where['a.故障现象'] = ['like','%'.$param['search'].'%'];
  367. }
  368. $query = db('设备_维修记录')
  369. ->alias('a')
  370. ->join('人事_基本资料 b','a.报障人 = b.员工编号','left')
  371. ->field('rtrim(a.设备编号) as 设备编号,rtrim(a.设备名称) as 设备名称,a.报障时间,a.维修受理时间,a.修复时间,a.实际维修工时 as 故障维修工时,rtrim(a.验收情况) as 验收情况,
  372. rtrim(b.员工姓名) as 机长,rtrim(c1.员工姓名) as 维修姓名1,rtrim(c2.员工姓名) as 维修姓名2,rtrim(c3.员工姓名) as 维修姓名3,rtrim(c4.员工姓名) as 维修姓名4,
  373. a.维修人员1,a.维修人员2,a.维修人员3,a.维修人员4,
  374. rtrim(a.故障现象) as 故障现象,rtrim(a.处理方法) as 处理方法,rtrim(a.使用部门) as 使用部门,a.sys_id as 创建用户,a.sys_rq as 创建时间,a.mod_rq as 修改时间,a.UniqId');
  375. for($i=1;$i<=4;$i++){
  376. $tableNumber = 'c'.$i;
  377. $field = 'a.维修人员'.$i;
  378. $query->join("人事_基本资料 $tableNumber", "$field = {$tableNumber}.员工编号 AND {$field} IS NOT NULL", 'LEFT');
  379. }
  380. $list = $query->where($where)->order('设备编号')->select();
  381. if (empty($list)) {
  382. $this->error('未找到数据');
  383. }else{
  384. $this->success('成功', $list);
  385. }
  386. }
  387. /**
  388. * 月度设备保养记录上方机台列表
  389. * @return void
  390. * @throws \think\db\exception\DataNotFoundException
  391. * @throws \think\db\exception\ModelNotFoundException
  392. * @throws \think\exception\DbException
  393. */
  394. public function MouthMaintainList()
  395. {
  396. if ($this->request->isGet() === false) {
  397. $this->error('请求错误');
  398. }
  399. $param = $this->request->param();
  400. if (empty($param) || !isset($param['sist']) || !isset($param['mouth'])) {
  401. $this->error('参数错误');
  402. }
  403. $fields = 'rtrim(a.设备编号) as 设备编号,rtrim(a.设备名称) as 设备名称,rtrim(a.使用部门) as 使用部门,rtrim(a.保养类型) as 保养类型,a.保养开始时间,a.保养结束时间,
  404. a.保养工时,rtrim(b.员工姓名) as 执行机修,rtrim(c.员工姓名) as 执行机长,rtrim(a.保养备注) as 保养备注,d.员工姓名 as 验收人员,rtrim(a.验收情况) as 验收情况,
  405. rtrim(a.验收备注) as 验收备注,rtrim(a.sys_id) as 创建用户,a.sys_rq as 创建时间,a.UniqId';
  406. $query = db('设备_保养记录')
  407. ->alias('a')
  408. ->join('人事_基本资料 b','a.执行机修 = b.员工编号','left')
  409. ->join('人事_基本资料 c','a.执行机长 = c.员工编号','left')
  410. ->join('人事_基本资料 d','a.验收人员 = d.员工编号','left')
  411. ->field($fields)
  412. ->where('a.使用部门', $param['sist'])
  413. ->where('a.保养开始时间', 'like', $param['mouth'] . '%')
  414. ->order('a.设备编号');
  415. $list = $query->select();
  416. if (empty($list)) {
  417. $this->error('未找到数据');
  418. }else{
  419. $this->success('成功', $list);
  420. }
  421. }
  422. /**
  423. * 设备维修记录修改-》维修信息获取
  424. * @return void
  425. * @throws \think\db\exception\DataNotFoundException
  426. * @throws \think\db\exception\ModelNotFoundException
  427. * @throws \think\exception\DbException
  428. */
  429. public function MachineMaintenanceDetail()
  430. {
  431. if ($this->request->isGet() === false) {
  432. $this->error('请求错误');
  433. }
  434. $param = $this->request->param();
  435. if (empty($param) || !isset($param['UniqId'])) {
  436. $this->error('参数错误');
  437. }
  438. $field = 'rtrim(a.设备编号) as 设备编号,rtrim(a.设备名称) as 设备名称,rtrim(a.使用部门) as 使用部门,a.报障时间,a.等配件时间,a.修复时间,rtrim(a.故障现象) as 故障现象,
  439. rtrim(a.报障人) as 机长,b.员工姓名 as 机长姓名,rtrim(a.维修性质) as 维修性质,a.维修受理时间,a.修复时间,a.等配件时间,
  440. a.修复时间,a.维修人员1,a.维修人员2,a.维修人员3,a.维修人员4,c1.员工姓名 as 维修人员姓名1,c2.员工姓名 as 维修人员姓名2,c3.员工姓名 as 维修人员姓名3,c4.员工姓名 as 维修人员姓名4,
  441. rtrim(a.处理方法) as 原因分析及措施,a.实际维修工时,a.故障工时,rtrim(a.验收人) as 验收人,rtrim(a.验收情况) as 验收情况,a.UniqId';
  442. $query = db('设备_维修记录')
  443. ->alias('a')
  444. ->join('人事_基本资料 b','a.报障人 = b.员工编号','left')
  445. ->field($field)
  446. ->where('a.UniqId', $param['UniqId']);
  447. for($i=1;$i<=4;$i++){
  448. $tableNumber = 'c'.$i;
  449. $field = 'a.维修人员'.$i;
  450. $query->join("人事_基本资料 $tableNumber", "$field = {$tableNumber}.员工编号 AND {$field} IS NOT NULL", 'LEFT');
  451. }
  452. $list = $query->find();
  453. if (empty($list)) {
  454. $this->error('未找到数据');
  455. }else{
  456. $this->success('成功', $list);
  457. }
  458. }
  459. /**
  460. * 设备维修修改
  461. * @return void
  462. * @throws \think\Exception
  463. * @throws \think\db\exception\BindParamException
  464. * @throws \think\exception\PDOException
  465. */
  466. public function MachineMaintenanceUpdate()
  467. {
  468. if ($this->request->isPost() === false) {
  469. $this->error('请求错误');
  470. }
  471. $param = Request::instance()->post();
  472. if (empty($param) || !isset($param['UniqId'])) {
  473. $this->error('参数错误');
  474. }
  475. $data = $param;
  476. unset($data['UniqId']);
  477. $data['mod_rq'] = date('Y-m-d H:i:s');
  478. $sql = db('设备_维修记录')
  479. ->where('UniqId', $param['UniqId'])
  480. ->fetchSql(true)
  481. ->update($data);
  482. $res = db()->query($sql);
  483. if ($res !== false) {
  484. $this->success('修改成功');
  485. }else{
  486. $this->error('修改失败');
  487. }
  488. }
  489. /**
  490. * 设备维修记录添加
  491. * @return void
  492. * @throws \think\db\exception\BindParamException
  493. * @throws \think\exception\PDOException
  494. */
  495. public function MachineMaintenanceAdd()
  496. {
  497. if ($this->request->isPost() === false) {
  498. $this->error('请求错误');
  499. }
  500. $param = Request::instance()->post();
  501. if (empty($param)) {
  502. $this->error('参数错误');
  503. }
  504. $param['sys_rq'] = date('Y-m-d H:i:s');
  505. $param['mod_rq'] = '1900-01-01 00:00:00';
  506. $sql = db('设备_维修记录')
  507. ->fetchSql(true)
  508. ->insert($param);
  509. $res = db()->query($sql);
  510. if ($res !== false) {
  511. $this->success('添加成功');
  512. }else{
  513. $this->error('添加失败');
  514. }
  515. }
  516. /**
  517. * 设备保养记录修改信息获取
  518. * @return void
  519. * @throws \think\db\exception\DataNotFoundException
  520. * @throws \think\db\exception\ModelNotFoundException
  521. * @throws \think\exception\DbException
  522. */
  523. public function MachineMaintainDetail()
  524. {
  525. if ($this->request->isGet() === false) {
  526. $this->error('请求错误');
  527. }
  528. $param = $this->request->param();
  529. if (empty($param) || !isset($param['UniqId'])) {
  530. $this->error('参数错误');
  531. }
  532. $field = 'rtrim(a.设备编号) as 设备编号,rtrim(a.设备名称) as 设备名称,rtrim(a.使用部门) as 使用部门,rtrim(a.保养类型) as 保养类型,a.保养开始时间,
  533. a.保养结束时间,a.保养工时,rtrim(a.执行机修) as 执行机修,b.员工姓名 as 执行机修姓名,a.执行机长,c.员工姓名 as 执行机长姓名,rtrim(a.保养备注) as 保养备注,
  534. rtrim(a.验收人员) as 验收人员,d.员工姓名 as 验收人员姓名,rtrim(a.验收备注) as 验收备注,rtrim(a.验收情况) as 验收情况,a.UniqId';
  535. $query = db('设备_保养记录')
  536. ->alias('a')
  537. ->join('人事_基本资料 b','a.执行机修 = b.员工编号','left')
  538. ->join('人事_基本资料 c','a.执行机长 = c.员工编号','left')
  539. ->join('人事_基本资料 d','a.验收人员 = d.员工编号','left')
  540. ->field($field)
  541. ->where('a.UniqId', $param['UniqId']);
  542. $list = $query->find();
  543. if (empty($list)) {
  544. $this->error('未找到数据');
  545. }else{
  546. $this->success('成功', $list);
  547. }
  548. }
  549. /**
  550. * 设备保养记录修改
  551. * @return void
  552. * @throws \think\Exception
  553. * @throws \think\db\exception\BindParamException
  554. * @throws \think\exception\PDOException
  555. */
  556. public function MachineMaintainUpdate()
  557. {
  558. if ($this->request->isPost() === false) {
  559. $this->error('请求错误');
  560. }
  561. $param = Request::instance()->post();
  562. if (empty($param) || !isset($param['UniqId'])) {
  563. $this->error('参数错误');
  564. }
  565. $data = $param;
  566. unset($data['UniqId']);
  567. $data['mod_rq'] = date('Y-m-d H:i:s');
  568. $sql = db('设备_保养记录')
  569. ->where('UniqId', $param['UniqId'])
  570. ->fetchSql(true)
  571. ->update($data);
  572. $res = db()->query($sql);
  573. if ($res !== false) {
  574. $this->success('修改成功');
  575. }else{
  576. $this->error('修改失败');
  577. }
  578. }
  579. /**
  580. * 设备保养记录新增
  581. * @return void
  582. * @throws \think\db\exception\BindParamException
  583. * @throws \think\exception\PDOException
  584. */
  585. public function MachineMaintainAdd()
  586. {
  587. if ($this->request->isPost() === false) {
  588. $this->error('请求错误');
  589. }
  590. $param = Request::instance()->post();
  591. if (empty($param)) {
  592. $this->error('参数错误');
  593. }
  594. $param['sys_rq'] = date('Y-m-d H:i:s');
  595. $param['mod_rq'] = '1900-01-01 00:00:00';
  596. $sql = db('设备_保养记录')
  597. ->fetchSql(true)
  598. ->insert($param);
  599. $res = db()->query($sql);
  600. if ($res !== false) {
  601. $this->success('添加成功');
  602. }else{
  603. $this->error('添加失败');
  604. }
  605. }
  606. /**
  607. * 设备保养维修记录删除
  608. * @return void
  609. * @throws \think\Exception
  610. * @throws \think\exception\PDOException
  611. */
  612. public function MachineMaintainDelete()
  613. {
  614. if ($this->request->isGet() === false) {
  615. $this->error('请求错误');
  616. }
  617. $param = $this->request->param();
  618. if (empty($param) || !isset($param['UniqId']) || !isset($param['type'])) {
  619. $this->error('参数错误');
  620. }
  621. if ($param['type'] == 0) {
  622. $table = '设备_维修记录';
  623. }else{
  624. $table = '设备_保养记录';
  625. }
  626. $res = db($table)->where('UniqId', $param['UniqId'])->delete();
  627. if ($res !== false) {
  628. $this->success('删除成功');
  629. }else{
  630. $this->error('删除失败');
  631. }
  632. }
  633. /**
  634. * 维修工时统计
  635. * @return void
  636. * @throws \think\db\exception\DataNotFoundException
  637. * @throws \think\db\exception\ModelNotFoundException
  638. * @throws \think\exception\DbException
  639. */
  640. public function MaintenanceHours()
  641. {
  642. if ($this->request->isGet() === false) {
  643. $this->error('请求错误');
  644. }
  645. $param = $this->request->param();
  646. if (empty($param) || !isset($param['month'])) {
  647. $this->error('参数错误');
  648. }
  649. $where['a.报障时间'] = ['like',$param['month'].'%'];
  650. $field = 'a.维修人员1,a.维修人员2,a.维修人员3,a.维修人员4,c1.员工姓名 as 维修人员姓名1,c2.员工姓名 as 维修人员姓名2,c3.员工姓名 as 维修人员姓名3,c4.员工姓名 as 维修人员姓名4,
  651. a.实际维修工时';
  652. $query = db('设备_维修记录')
  653. ->alias('a')
  654. ->where($where)
  655. ->field($field);
  656. for($i=1;$i<=4;$i++){
  657. $tableNumber = 'c'.$i;
  658. $field = 'a.维修人员'.$i;
  659. $query->join("人事_基本资料 $tableNumber", "$field = {$tableNumber}.员工编号 AND {$field} IS NOT NULL", 'LEFT');
  660. }
  661. $list = $query->select();
  662. $data = array();
  663. foreach ($list as $k=>$v){
  664. for ($i=1;$i<=4;$i++){
  665. $code = $v['维修人员'.$i];
  666. $name = $v['维修人员姓名'.$i];
  667. if ($code !== '') {
  668. $data[] = [
  669. 'code' => $code,
  670. 'name' => $name,
  671. 'hour' => $v['实际维修工时'],
  672. ];
  673. }
  674. }
  675. }
  676. $result = [];
  677. foreach ($data as $item) {
  678. $code = $item['code'];
  679. $hour = (float)$item['hour'];
  680. if (isset($result[$code])) {
  681. $result[$code]['hour'] += $hour;
  682. $result[$code]['Frequency'] += 1;
  683. } else {
  684. $result[$code] = [
  685. 'code' => $code,
  686. 'name' => '',
  687. 'hour' => $hour,
  688. 'Frequency' => 1
  689. ];
  690. }
  691. }
  692. $result = array_values($result);
  693. $this->success('成功', $result);
  694. }
  695. }