MachineList.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. namespace app\api\controller;
  3. use think\Request;
  4. use app\common\controller\Api;
  5. use think\response\Json;
  6. /**
  7. * 设备维修保养记录
  8. */
  9. class MachineList extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. //设备维修保养记录左侧菜单
  14. public function getTab()
  15. {
  16. if ($this->request->isGet() === false){
  17. $this->error('请求错误');
  18. }
  19. $year = date('Y',time());
  20. $lastYear = date('Y', strtotime('-1 year'));
  21. //年度维修统计
  22. $yearResult1 = $this->getYearSite($year);
  23. $yearResult2 = $this->getYearSite($lastYear);
  24. $data['1、年度维修统计'] = [
  25. $year => $yearResult1,
  26. $lastYear => $yearResult2
  27. ];
  28. //月度维修记录
  29. $lastMouth = date('Y-m', strtotime('-1 year'));
  30. $mouthReault = $this->getMouthSite($lastMouth);
  31. $data['2、月度维修记录'] = $mouthReault;
  32. //年度保养记录
  33. $YearMaintenanceSist1 = $this->getYearMaintenanceSist($year);
  34. $YearMaintenanceSist2 = $this->getYearMaintenanceSist($lastYear);
  35. if (!empty($YearMaintenanceSist1)){
  36. $data['3、年度保养统计'][$year] = $YearMaintenanceSist1;
  37. }
  38. if (!empty($YearMaintenanceSist2)){
  39. $data['3、年度保养统计'][$lastYear] = $YearMaintenanceSist2;
  40. }
  41. //月度保养记录
  42. $mouthMaintenanceSist = $this->getMouthMaintenanceSist($lastMouth);
  43. $data['4、月度保养记录'] = $mouthMaintenanceSist;
  44. $this->success('成功',$data);
  45. }
  46. private function getYearSite($year)
  47. {
  48. $sist = db('设备_维修记录')
  49. ->where('报障时间','like',$year.'%')
  50. ->group('使用部门')
  51. ->column('rtrim(使用部门)');
  52. $data = [];
  53. if (!empty($sist)){
  54. foreach ($sist as $item){
  55. $count = db('设备_维修记录')
  56. ->where('使用部门',$item)
  57. ->where('报障时间','like',$year.'%')
  58. ->count();
  59. if ($count !== 0){
  60. $data[] = $item.'('.$count.'台次)';
  61. }
  62. }
  63. }
  64. return $data;
  65. }
  66. private function getMouthSite($mouth)
  67. {
  68. $mouth = db('设备_维修记录')
  69. ->where('报障时间','>',$mouth.'-01 00:00:00')
  70. ->where('报障时间','<',date('Y-m-d H:i:s',time()))
  71. ->group('date')
  72. ->order('date desc')
  73. ->column('DATE_FORMAT(报障时间, "%Y-%m") AS date');
  74. $data = [];
  75. foreach ($mouth as $item){
  76. $sist = db('设备_维修记录')
  77. ->where('报障时间','like',$item.'%')
  78. ->group('使用部门')
  79. ->column('rtrim(使用部门)');
  80. foreach ($sist as $value){
  81. $count = db('设备_维修记录')
  82. ->where('使用部门',$value)
  83. ->where('报障时间','like',$item.'%')
  84. ->count();
  85. $data[$item][] = $value.'('.$count.'台次)';
  86. }
  87. }
  88. return $data;
  89. }
  90. private function getYearMaintenanceSist($year)
  91. {
  92. $sist = db('设备_保养记录')
  93. ->where('保养开始时间','like',$year.'%')
  94. ->group('使用部门')
  95. ->column('rtrim(使用部门)');
  96. $data = [];
  97. if (!empty($sist)){
  98. foreach ($sist as $item){
  99. $count = db('设备_保养记录')
  100. ->where('使用部门',$item)
  101. ->where('保养开始时间','like',$year.'%')
  102. ->count();
  103. if ($count !== 0){
  104. $data[] = $item.'('.$count.'台次)';
  105. }
  106. }
  107. }
  108. return $data;
  109. }
  110. private function getMouthMaintenanceSist($mouth)
  111. {
  112. $mouth = db('设备_保养记录')
  113. ->where('保养开始时间','>',$mouth.'-01 00:00:00')
  114. ->where('保养开始时间','<',date('Y-m-d H:i:s',time()))
  115. ->group('date')
  116. ->order('date desc')
  117. ->column('DATE_FORMAT(保养开始时间, "%Y-%m") AS date');
  118. $data = [];
  119. foreach ($mouth as $item){
  120. $sist = db('设备_保养记录')
  121. ->where('保养开始时间','like',$item.'%')
  122. ->group('使用部门')
  123. ->column('rtrim(使用部门)');
  124. foreach ($sist as $value){
  125. $count = db('设备_保养记录')
  126. ->where('使用部门',$value)
  127. ->where('保养开始时间','like',$item.'%')
  128. ->count();
  129. $data[$item][] = $value.'('.$count.'台次)';
  130. }
  131. }
  132. return $data;
  133. }
  134. /**
  135. * 年度维修记录上方列表
  136. * @return Json|void
  137. */
  138. public function YearMaintenanceList()
  139. {
  140. // 验证请求方法
  141. if (!$this->request->isGet()) {
  142. $this->error('请求错误');
  143. }
  144. // 获取并验证参数
  145. $param = $this->request->param();
  146. if (empty($param) || !isset($param['sist']) || !isset($param['year'])) {
  147. $this->error('参数错误');
  148. }
  149. $department = $param['sist'];
  150. $year = $param['year'];
  151. try {
  152. // 获取该部门该年份的所有维修设备编号
  153. $machineQuery = db('设备_维修记录')
  154. ->where('使用部门', $department)
  155. ->where('报障时间', 'like', $year . '%')
  156. ->order('设备编号');
  157. $machines = $machineQuery->column('distinct(设备编号) as 设备编号');
  158. if (empty($machines)) {
  159. $this->success('成功', []);
  160. }
  161. // 预加载设备名称
  162. $machineNames = db('设备_基本资料')
  163. ->whereIn('设备编号', $machines)
  164. ->column('rtrim(设备名称) as 设备名称', '设备编号');
  165. $result = [];
  166. // 一次性获取所有维修记录
  167. $allRecords = db('设备_维修记录')
  168. ->where('使用部门', $department)
  169. ->where('报障时间', 'like', $year . '%')
  170. ->field([
  171. '设备编号',
  172. 'DATE_FORMAT(报障时间, "%m") AS month', // 直接提取月份数字
  173. 'COUNT(*) AS count'
  174. ])
  175. ->group('设备编号, DATE_FORMAT(报障时间, "%m")')
  176. ->select();
  177. // 按设备编号组织数据
  178. $recordsByMachine = [];
  179. foreach ($allRecords as $record) {
  180. $monthKey = $record['month']; // 直接使用月份数字作为键
  181. $recordsByMachine[$record['设备编号']][$monthKey] = $record['count'];
  182. }
  183. // 构建结果数组
  184. foreach ($machines as $machineId) {
  185. $monthData = [];
  186. $total = 0;
  187. if (isset($recordsByMachine[$machineId])) {
  188. foreach ($recordsByMachine[$machineId] as $month => $count) {
  189. $monthData[$month] = $count; // 保持月份键为字符串
  190. $total += $count;
  191. }
  192. }
  193. // 确保月份键是两位数格式
  194. $formattedMonthData = [];
  195. foreach ($monthData as $month => $count) {
  196. $formattedMonth = str_pad($month, 2, '0', STR_PAD_LEFT);
  197. $formattedMonthData[$formattedMonth] = $count;
  198. }
  199. $result[] = [
  200. '使用部门' => $department,
  201. '设备编号' => $machineId,
  202. '设备名称' => $machineNames[$machineId] ?? '未知设备',
  203. 'total' => $total
  204. ] + $formattedMonthData;
  205. }
  206. return json([
  207. 'code' => 1,
  208. 'msg' => '成功',
  209. 'time' =>time(),
  210. 'data' => $result
  211. ]);
  212. } catch (\Exception $e) {
  213. $this->error('查询失败: ' . $e->getMessage());
  214. }
  215. }
  216. /**
  217. * 设备维修记录配件详情
  218. * @return void
  219. * @throws \think\db\exception\DataNotFoundException
  220. * @throws \think\db\exception\ModelNotFoundException
  221. * @throws \think\exception\DbException
  222. */
  223. public function YearAccessoriesList()
  224. {
  225. if ($this->request->isGet() === false) {
  226. $this->error('请求错误');
  227. }
  228. $param = $this->request->param();
  229. if (empty($param) || !isset($param['machine']) || !isset($param['year']) || !isset($param['sist'])) {
  230. $this->error('参数错误');
  231. }
  232. $list = db('物料_收发记录')
  233. ->alias('a')
  234. ->join('物料_仓库信息 b', 'a.仓库编号 = b.编号','left')
  235. ->join('设备_基本资料 c', 'a.st_jtbh = c.设备编号','left')
  236. ->join('物料_存货编码 d', 'a.st_wlbh = d.物料代码')
  237. ->where('a.st_jtbh',$param['machine'])
  238. ->where('a.st_rq','like',$param['year'].'%')
  239. ->where(function ($query) {
  240. $query->where('a.仓库编号', '107')
  241. ->whereOr('a.仓库编号', 'Y107');
  242. })
  243. ->where('st_jylb','like','修理领用%')
  244. ->field(['DATE_FORMAT(a.st_rq, "%y-%m-%d") AS 日期','rtrim(a.st_jylb) as 交易类别','a.st_jtbh AS 机台编号','rtrim(a.仓库编号) as 仓库编号',
  245. 'rtrim(b.名称) as 仓库名称','rtrim(c.使用部门) as 领用部门','a.st_wlbh as 物料编号','rtrim(d.物料名称) as 物料名称','a.st_sl as 领用数量',
  246. 'rtrim(a.st_dw) as 领用单位','a.领用单价','a.st_sl*a.领用单价 as 金额','a.st_desc as 备注'])
  247. ->order('日期 DESC')
  248. ->select();
  249. if (empty($list)) {
  250. $this->error('未找到数据');
  251. }else{
  252. $this->success('成功', $list);
  253. }
  254. }
  255. /**
  256. * 年度保养统计上方几台列表
  257. * @return Json|void
  258. */
  259. public function YearMaintainList()
  260. {
  261. // 验证请求方法
  262. if (!$this->request->isGet()) {
  263. $this->error('请求错误');
  264. }
  265. // 获取并验证参数
  266. $param = $this->request->param();
  267. if (empty($param) || !isset($param['sist']) || !isset($param['year'])) {
  268. $this->error('参数错误');
  269. }
  270. $department = $param['sist'];
  271. $year = $param['year'];
  272. try {
  273. // 获取该部门该年份的所有维修设备编号
  274. $machineQuery = db('设备_保养记录')
  275. ->where('使用部门', $department)
  276. ->where('保养开始时间', 'like', $year . '%')
  277. ->order('设备编号');
  278. $machines = $machineQuery->column('distinct(设备编号) as 设备编号');
  279. if (empty($machines)) {
  280. $this->success('成功', []);
  281. }
  282. // 预加载设备名称
  283. $machineNames = db('设备_基本资料')
  284. ->whereIn('设备编号', $machines)
  285. ->column('rtrim(设备名称) as 设备名称', '设备编号');
  286. $result = [];
  287. // 一次性获取所有维修记录
  288. $allRecords = db('设备_保养记录')
  289. ->where('使用部门', $department)
  290. ->where('保养开始时间', 'like', $year . '%')
  291. ->field([
  292. '设备编号',
  293. 'DATE_FORMAT(保养开始时间, "%m") AS month', // 直接提取月份数字
  294. 'COUNT(*) AS count'
  295. ])
  296. ->group('设备编号, DATE_FORMAT(保养开始时间, "%m")')
  297. ->select();
  298. // 按设备编号组织数据
  299. $recordsByMachine = [];
  300. foreach ($allRecords as $record) {
  301. $monthKey = $record['month']; // 直接使用月份数字作为键
  302. $recordsByMachine[$record['设备编号']][$monthKey] = $record['count'];
  303. }
  304. // 构建结果数组
  305. foreach ($machines as $machineId) {
  306. $monthData = [];
  307. $total = 0;
  308. if (isset($recordsByMachine[$machineId])) {
  309. foreach ($recordsByMachine[$machineId] as $month => $count) {
  310. $monthData[$month] = $count; // 保持月份键为字符串
  311. $total += $count;
  312. }
  313. }
  314. // 确保月份键是两位数格式
  315. $formattedMonthData = [];
  316. foreach ($monthData as $month => $count) {
  317. $formattedMonth = str_pad($month, 2, '0', STR_PAD_LEFT);
  318. $formattedMonthData[$formattedMonth] = $count;
  319. }
  320. $result[] = [
  321. '使用部门' => $department,
  322. '设备编号' => $machineId,
  323. '设备名称' => $machineNames[$machineId] ?? '未知设备',
  324. 'total' => $total
  325. ] + $formattedMonthData;
  326. }
  327. return json([
  328. 'code' => 1,
  329. 'msg' => '成功',
  330. 'time' =>time(),
  331. 'data' => $result
  332. ]);
  333. } catch (\Exception $e) {
  334. $this->error('查询失败: ' . $e->getMessage());
  335. }
  336. }
  337. //月度设备维修记录上方机台列表
  338. // public function MonthAccessoriesList()
  339. // {
  340. // if ($this->request->isGet() === false) {
  341. // $this->error('请求错误');
  342. // }
  343. // $param = $this->request->param();
  344. // if (empty($param) || !isset($param['sist']) || !isset($param['mouth'])) {
  345. // $this->error('参数错误');
  346. // }
  347. // $list = db('设备_维修记录')
  348. // ->alias('a')
  349. // ->join('<UNK>_<UNK> b', 'a.<UNK> = b.<UNK>','left')
  350. // ->where('使用部门', $param['sist'])
  351. // ->where('保障时间', 'like', $param['mouth'] . '%')
  352. // ->field('设备编号,设备名称,报障时间,维修受理时间,修复时间,实际维修工时 as 故障维修工时,验收情况,');
  353. // }
  354. }