EquipmentComponents.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Request;
  5. /**
  6. * 设备部件寿命跟踪
  7. */
  8. class EquipmentComponents extends Api
  9. {
  10. const EXCLUDED_DEPARTMENT = '研发中心';
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 获取车间机台
  15. * @return void
  16. * @throws \think\db\exception\DataNotFoundException
  17. * @throws \think\db\exception\ModelNotFoundException
  18. * @throws \think\exception\DbException
  19. */
  20. public function getMachineList()
  21. {
  22. // 1. 使用更清晰的请求方法判断
  23. if (!$this->request->isGet()) {
  24. $this->error('请求错误');
  25. }
  26. // 3. 统一查询条件,避免重复代码
  27. $baseQuery = function() {
  28. return \db('设备_基本资料')
  29. ->where('设备编组', '<>', '')
  30. ->where('sys_sbID', '<>', '');
  31. };
  32. $data = [];
  33. // 4. 获取部门列表(添加性能优化)
  34. $department = $baseQuery()
  35. ->distinct(true)
  36. ->where('使用部门', '<>', self::EXCLUDED_DEPARTMENT)
  37. ->order('设备编组')
  38. ->column('使用部门');
  39. if (empty($department)) {
  40. $this->success('未获取到机台数据', []);
  41. }
  42. // 5. 批量查询优化,减少数据库查询次数
  43. foreach ($department as $dept) {
  44. $trimmedDept = rtrim($dept);
  45. // 获取该部门下的所有设备编组
  46. $benchClasses = $baseQuery()
  47. ->where('使用部门', $dept)
  48. ->distinct(true)
  49. ->order('设备编组')
  50. ->column('设备编组');
  51. foreach ($benchClasses as $benchClass) {
  52. $trimmedClass = rtrim($benchClass);
  53. if ($trimmedClass === '') {
  54. continue; // 跳过空编组
  55. }
  56. // 获取该编组下的所有设备
  57. $machines = $baseQuery()
  58. ->where('使用部门', $dept)
  59. ->where('设备编组', $benchClass)
  60. ->field([
  61. 'rtrim(设备编号) as 设备编号',
  62. 'rtrim(设备名称) as 设备名称',
  63. '设备编号 as original_id', // 保留原始ID便于调试
  64. '设备名称 as original_name'
  65. ])
  66. ->order('设备编号')
  67. ->select();
  68. // 6. 使用数组映射简化数据处理
  69. $formattedMachines = array_map(function($machine) {
  70. return $machine['设备编号'] . '-->' . $machine['设备名称'];
  71. }, $machines);
  72. if (!empty($formattedMachines)) {
  73. $data[$trimmedDept][$trimmedClass] = $formattedMachines;
  74. }
  75. }
  76. }
  77. $this->success('成功', $data);
  78. }
  79. /**
  80. * 获取组件清单
  81. * @return void
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. * @throws \think\exception\DbException
  85. */
  86. public function getEquipmentList()
  87. {
  88. if (!$this->request->isGet()) {
  89. $this->error('请求错误');
  90. }
  91. $equipmentList = db('设备_组件清单')
  92. ->field('序号,部件编号,部件名称,Uniqid')
  93. ->select();
  94. if (empty($equipmentList)) {
  95. $this->success('未找到组件数据');
  96. }
  97. foreach ($equipmentList as $key => $equipment) {
  98. $equipmentList[$key]['名称'] = $equipment['部件编号'] . '-->' . $equipment['部件名称'];
  99. }
  100. $this->success('成功', $equipmentList);
  101. }
  102. /**
  103. * 创建机台组件配置
  104. * @return void
  105. * @throws \think\Exception
  106. * @throws \think\db\exception\BindParamException
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. * @throws \think\exception\DbException
  110. * @throws \think\exception\PDOException
  111. */
  112. public function CreateEquipmentInfo()
  113. {
  114. if (!$this->request->isPost()) {
  115. $this->error('请求错误');
  116. }
  117. $data = Request::instance()->post();
  118. if (empty($data['ids']) || empty($data['jtbh'])) {
  119. $this->error('参数错误');
  120. }
  121. $ids = explode(',',$data['ids']);
  122. $equipmentList = db('设备_组件清单')
  123. ->field('序号,部件编号,部件名称')
  124. ->whereIn('Uniqid',$ids)
  125. ->select();
  126. db('设备_组件配置清单')->where('设备编号',$data['jtbh'])->delete();
  127. $machineEquipmentList = [];
  128. foreach ($equipmentList as $equipment) {
  129. $machineEquipmentList[] = [
  130. '设备编号' => $data['jtbh'],
  131. '部件编号' => $equipment['部件编号'],
  132. '部件名称' => $equipment['部件名称'],
  133. 'Sys_id' => $data['sys_id'],
  134. 'Sys_rq' => date('Y-m-d H:i:s', time()),
  135. ];
  136. }
  137. $sql = db('设备_组件配置清单')->fetchSql(true)->insertAll($machineEquipmentList);
  138. $res = \db()->query($sql);
  139. if ($res === false){
  140. $this->error('创建失败');
  141. }else{
  142. $this->success('创建成功');
  143. }
  144. }
  145. /**
  146. * 设备部件寿命跟踪上方列表
  147. * @return void
  148. * @throws \think\db\exception\DataNotFoundException
  149. * @throws \think\db\exception\ModelNotFoundException
  150. * @throws \think\exception\DbException
  151. */
  152. public function getMachineEquipmentList()
  153. {
  154. if (!$this->request->isGet()) {
  155. $this->error('请求错误');
  156. }
  157. $params = $this->request->param();
  158. if (empty($params['machine'])) {
  159. $this->error('参数错误');
  160. }
  161. // 优化1:使用链式查询,添加设备编号过滤条件
  162. $list = db('设备_组件配置清单')
  163. ->alias('a')
  164. ->join('设备_组件清单 b', 'a.部件编号 = b.部件编号')
  165. ->where('a.设备编号', $params['machine']) // 添加设备编号过滤
  166. ->field('a.设备编号,a.部件编号,a.部件名称,a.组件编号,a.小时寿命,a.阈值,a.更换日期,a.Uniqid,b.序号')
  167. ->order('b.序号')
  168. ->select();
  169. if (empty($list)) {
  170. $this->error('未找到机台部件配置,请先配置部件...');
  171. }
  172. // 优化2:提取有更换日期的记录进行批量查询
  173. $hasReplacement = [];
  174. foreach ($list as $key => $equipment) {
  175. $list[$key]['运行工时'] = 0;
  176. // 收集需要查询运行工时的设备信息
  177. if (!empty($equipment['更换日期'])) {
  178. $hasReplacement[$key] = [
  179. 'device' => $equipment['设备编号'],
  180. 'date' => $equipment['更换日期']
  181. ];
  182. }
  183. }
  184. // 优化3:批量查询运行工时数据
  185. if (!empty($hasReplacement)) {
  186. // 按设备编号分组,避免重复查询
  187. $deviceRunTimes = [];
  188. foreach ($hasReplacement as $item) {
  189. $device = $item['device'];
  190. $date = $item['date'];
  191. // 如果该设备还未查询过,则进行查询
  192. if (!isset($deviceRunTimes[$device])) {
  193. $deviceRunTimes[$device] = db('设备_产量计酬')
  194. ->where('sczl_jtbh', $device)
  195. ->where('sczl_rq', '>=', $date)
  196. ->field('SUM(sczl_设备运行工时) as total_hours')
  197. ->find();
  198. }
  199. // 为每个记录设置运行工时
  200. $key = array_search($item, $hasReplacement);
  201. $list[$key]['运行工时'] = $deviceRunTimes[$device]['total_hours'];
  202. }
  203. }
  204. $this->success('成功', $list);
  205. }
  206. /**
  207. * 设备部件寿命下方列表
  208. * @return void
  209. * @throws \think\db\exception\DataNotFoundException
  210. * @throws \think\db\exception\ModelNotFoundException
  211. * @throws \think\exception\DbException
  212. */
  213. public function getEquipmentDetailList()
  214. {
  215. if (!$this->request->isGet()) {
  216. $this->error('请求错误');
  217. }
  218. $params = $this->request->param();
  219. if (empty($params['machine']) || empty($params['date'])) {
  220. $this->error('参数错误');
  221. }
  222. $list = db('设备_产量计酬')
  223. ->alias('a')
  224. ->join('工单_印件资料 b', 'a.sczl_gdbh = b.Yj_Gdbh and a.sczl_yjno = b.yj_Yjno')
  225. ->field('a.sczl_gdbh as 工单编号,a.sczl_yjno as 印件号,b.yj_yjmc as 印件名称,a.sczl_rq as 日期,SUM(a.sczl_设备运行工时) as 工时,a.sczl_jtbh as 机台编号')
  226. ->where([
  227. 'a.sczl_jtbh' => $params['machine'],
  228. 'a.sczl_rq' => ['>=', $params['date']],
  229. ])
  230. ->group('a.sczl_gdbh, a.sczl_yjno')
  231. ->select();
  232. if (empty($list)) {
  233. $this->error('未找到详情数据');
  234. }else{
  235. $this->success('成功', $list);
  236. }
  237. }
  238. /**
  239. * 部件数据修改
  240. * @return void
  241. * @throws \think\Exception
  242. * @throws \think\db\exception\BindParamException
  243. * @throws \think\exception\PDOException
  244. */
  245. public function getEquipmentUpdate()
  246. {
  247. if (!$this->request->isPost()) {
  248. $this->error('请求错误');
  249. }
  250. $params = Request::instance()->post();
  251. if(empty($params['id'])) {
  252. $this->error('参数错误');
  253. }
  254. $data = [
  255. '组件编号' => $params['code'],
  256. '小时寿命' => $params['hour'],
  257. '更换日期' => $params['date'],
  258. '阈值' => $params['number']
  259. ];
  260. $rql = db('设备_组件配置清单')
  261. ->where('Uniqid', $params['id'])
  262. ->fetchSql(true)
  263. ->update($data);
  264. $res = \db()->query($rql);
  265. if ($res === false){
  266. $this->error('修改失败');
  267. }else{
  268. $this->success('修改成功');
  269. }
  270. }
  271. }