HydEquipment.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?php
  2. namespace catchAdmin\hydraulic\controller;
  3. error_reporting(1);
  4. use catchAdmin\api\model\PublishAck;
  5. use catchAdmin\equipment\model\EquipmentType;
  6. use catcher\base\CatchRequest as Request;
  7. use catcher\CatchResponse;
  8. use catcher\base\CatchController;
  9. use catchAdmin\hydraulic\model\HydEquipment as hydEquipmentModel;
  10. use catchAdmin\hydraulic\model\Hydraulic;
  11. use catchAdmin\hydraulic\model\Wrench;
  12. use catchAdmin\system\model\SysDictData;
  13. use catcher\base\CatchRequest;
  14. use PhpOffice\PhpSpreadsheet\IOFactory;
  15. use PhpParser\Node\Stmt\Catch_;
  16. use think\facade\Db;
  17. class HydEquipment extends CatchController
  18. {
  19. protected $hydEquipmentModel;
  20. public function __construct(HydEquipmentModel $hydEquipmentModel)
  21. {
  22. $this->hydEquipmentModel = $hydEquipmentModel;
  23. }
  24. /**
  25. * 列表
  26. * @time 2022年06月21日 11:02
  27. * @param Request $request
  28. */
  29. public function index(Request $request): \think\Response
  30. {
  31. return CatchResponse::paginate($this->hydEquipmentModel->getList());
  32. }
  33. /**
  34. * 保存信息
  35. * @time 2022年06月21日 11:02
  36. * @param Request $request
  37. */
  38. public function save(Request $request): \think\Response
  39. {
  40. $data = $request->post();
  41. $hydraulic = null;
  42. $wrench = null;
  43. $hydequ = [
  44. // 物料号
  45. 'material_number' => $data['material_number'],
  46. // 设备类别
  47. 'equipment_type' => $data['equipment_type'],
  48. // 工具名称
  49. 'name' => $data['name'],
  50. // 设备型号
  51. 'equipment_model' => $data['equipment_model'],
  52. // 发放单位
  53. 'issue_unit' => $data['issue_unit'],
  54. // 固定资产编号
  55. 'fixed_asset_number' => $data['fixed_asset_number'],
  56. // 类固定资产编号
  57. 'fixed_asset_number2' => $data['fixed_asset_number2'],
  58. // 序列号
  59. 'serial_number' => $data['serial_number'],
  60. // 出厂编号
  61. 'factory_number' => $data['factory_number'],
  62. // 上次校验时间
  63. 'check_last_time' => $data['check_last_time'],
  64. // 下次校验时间
  65. 'check_next_time' => $data['check_next_time'],
  66. // 校验状态
  67. 'check_status' => $data['check_status'],
  68. // 状态
  69. 'status' => $data['status'],
  70. 'address' => $data['address'],
  71. 'longitude' => $data['longitude'],
  72. 'latitude' => $data['latitude']
  73. ];
  74. Db::startTrans();
  75. try {
  76. $id = $this->hydEquipmentModel->storeBy($hydequ);
  77. if ($data['equipment_type'] == 2) {
  78. $hydraulic = [
  79. // 编号
  80. 'number' => $data['number'],
  81. // 所属部门
  82. 'department_id' => $data['department_id'],
  83. 'remark' => $data['remark'] ? $data['remark'] : '',
  84. 'max_pressure' => $data['max_pressure'],
  85. 'min_pressure' => $data['min_pressure'],
  86. 'imei' => $data['imei'],
  87. 'effective_period' => $data['effective_period'],
  88. //设备的id
  89. 'eq_id' => $id
  90. ];
  91. $hyd_exist = Hydraulic::where('imei', $data['imei'])->find();
  92. if ($hyd_exist) {
  93. Db::rollback();
  94. return CatchResponse::fail('imei号已存在');
  95. }
  96. Hydraulic::create($hydraulic);
  97. }
  98. //保存扳手
  99. if ($data['equipment_type'] == 3 || $data['equipment_type'] == 4) {
  100. $wrench = [
  101. // 编号
  102. 'number' => $data['number'],
  103. //设备id
  104. 'eq_id' => $id,
  105. // 所属部门
  106. 'department_id' => $data['department_id'],
  107. 'remark' => $data['remark'] ? $data['remark'] : '',
  108. 'min_pressure' => $data['min_pressure'],
  109. 'max_pressure' => $data['max_pressure'],
  110. 'min_torque' => $data['min_torque'],
  111. 'max_torque' => $data['max_torque'],
  112. 'angle_sensor' => $data['angle_sensor'],
  113. 'angular_resolution' => $data['angular_resolution']
  114. ];
  115. $wrench_exist = Wrench::where('number', $data['number'])->find();
  116. if ($wrench_exist) {
  117. Db::rollback();
  118. return CatchResponse::fail('扳手编号已存在');
  119. }
  120. Wrench::create($wrench);
  121. }
  122. } catch (\Exception $e) {
  123. Db::rollback();
  124. return CatchResponse::fail($e->getMessage());
  125. }
  126. Db::commit();
  127. return CatchResponse::success();
  128. }
  129. /**
  130. * 读取
  131. * @time 2022年06月21日 11:02
  132. * @param $id
  133. */
  134. public function read($id): \think\Response
  135. {
  136. return CatchResponse::success($this->hydEquipmentModel->findBy($id));
  137. }
  138. /**
  139. * 更新
  140. * @time 2022年06月21日 11:02
  141. * @param Request $request
  142. * @param $id
  143. */
  144. public function update(Request $request, $id): \think\Response
  145. {
  146. $data = $request->post();
  147. $hydraulic = null;
  148. $wrench = null;
  149. $hydequ = [
  150. // 物料号
  151. 'material_number' => $data['material_number'],
  152. // 设备类别
  153. 'equipment_type' => $data['equipment_type'],
  154. // 工具名称
  155. 'name' => $data['name'],
  156. // 设备型号
  157. 'equipment_model' => $data['equipment_model'],
  158. // 发放单位
  159. 'issue_unit' => $data['issue_unit'],
  160. // 固定资产编号
  161. 'fixed_asset_number' => $data['fixed_asset_number'],
  162. // 类固定资产编号
  163. 'fixed_asset_number2' => $data['fixed_asset_number2'],
  164. // 序列号
  165. 'serial_number' => $data['serial_number'],
  166. // 出厂编号
  167. 'factory_number' => $data['factory_number'],
  168. // 上次校验时间
  169. 'check_last_time' => $data['check_last_time'] ? strstr($data['check_last_time'], '-') ? strtotime($data['check_last_time']) : $data['check_last_time'] : '',
  170. // 下次校验时间
  171. 'check_next_time' => $data['check_next_time'] ? strstr($data['check_next_time'], '-') ? strtotime($data['check_next_time']) : $data['check_next_time'] : '',
  172. // 校验状态
  173. 'check_status' => $data['check_status'],
  174. // 状态
  175. 'status' => $data['status'],
  176. 'address' => $data['address'],
  177. 'longitude' => $data['longitude'],
  178. 'latitude' => $data['latitude']
  179. ];
  180. if ($data['equipment_type'] == 2) {
  181. $hydraulic = [
  182. // 编号
  183. 'number' => $data['number'],
  184. // 所属部门
  185. 'department_id' => $data['department_id'],
  186. //备注
  187. 'remark' => $data['remark'],
  188. 'max_pressure' => $data['max_pressure'],
  189. 'min_pressure' => $data['min_pressure'],
  190. 'imei' => $data['imei'],
  191. 'effective_period' => $data['effective_period'],
  192. //设备的id
  193. 'eq_id' => $id
  194. ];
  195. $hyd_exist = Hydraulic::where('imei', $data['imei'])->where('eq_id', '<>', $id)->find();
  196. if ($hyd_exist) {
  197. Db::rollback();
  198. return CatchResponse::fail('imei号已存在');
  199. }
  200. $bool = Hydraulic::where('eq_id', $id)->find();
  201. if ($bool) {
  202. Hydraulic::where('eq_id', $id)->update($hydraulic);
  203. } else {
  204. Hydraulic::create($hydraulic);
  205. }
  206. }
  207. //保存扳手
  208. if ($data['equipment_type'] == 3 || $data['equipment_type'] == 4) {
  209. $wrench = [
  210. // 编号
  211. 'number' => $data['number'],
  212. //设备id
  213. 'eq_id' => $id,
  214. // 所属部门
  215. 'department_id' => $data['department_id'],
  216. 'remark' => $data['remark'],
  217. 'min_pressure' => $data['min_pressure'],
  218. 'max_pressure' => $data['max_pressure'],
  219. 'min_torque' => $data['min_torque'],
  220. 'max_torque' => $data['max_torque'],
  221. 'angular_resolution' => $data['angular_resolution'],
  222. 'angle_sensor' => $data['angle_sensor'],
  223. // 扭矩
  224. ];
  225. $wrench_exist = Wrench::where('number', $data['number'])->where('id', '<>', $data['list']['id'])->find();
  226. if ($wrench_exist) {
  227. Db::rollback();
  228. return CatchResponse::fail('扳手编号已存在');
  229. }
  230. $bool = Wrench::where('eq_id', $id)->find();
  231. if ($bool) {
  232. Wrench::update($wrench, ['eq_id' => $id]);
  233. } else {
  234. Wrench::create($wrench);
  235. }
  236. }
  237. return CatchResponse::success($this->hydEquipmentModel->updateBy($id, $hydequ));
  238. }
  239. /**
  240. * 删除
  241. * @time 2022年06月21日 11:02
  242. * @param $id
  243. */
  244. public function delete($id): \think\Response
  245. {
  246. $data = array();
  247. $where = [];
  248. if (is_array($id)) {
  249. $where[] = ['id', 'in', $id];
  250. $data = $this->hydEquipmentModel->where($where)->select()->toArray();
  251. } else {
  252. $list = $this->hydEquipmentModel->where('id', $id)->find();
  253. $data[] = $list;
  254. }
  255. Db::startTrans();
  256. try {
  257. foreach ($data as $item) {
  258. if ($item['equipment_type'] == 2) {
  259. Hydraulic::where('eq_id', '=', $item['id'])->delete();
  260. }
  261. if ($item['equipment_type'] == 3 || $item['equipment_type'] == 4) {
  262. $wrench = Wrench::where('eq_id', '=', $item['id'])->find();
  263. $wrench->delete();
  264. }
  265. }
  266. $this->hydEquipmentModel->deleteBy($id, true);
  267. } catch (\Exception $e) {
  268. Db::rollback();
  269. return CatchResponse::fail($e->getMessage());
  270. }
  271. Db::commit();
  272. return CatchResponse::success();
  273. }
  274. /**
  275. * @Descripttion: 获取设备状态和校验状态
  276. * @name: likang
  277. * @return {*}
  278. */
  279. public function getHydEquipmentType(Request $request)
  280. {
  281. $data = $request->get();
  282. $code = $data['code'];
  283. $sysDict = new SysDictData();
  284. $list = $sysDict->getTypesByCodeWithRemark($code, '');
  285. return CatchResponse::success($list);
  286. }
  287. /**
  288. * 根据设备类别统计每个设备的总数
  289. */
  290. public function getTotalByEquipmentType($id)
  291. {
  292. $list = EquipmentType::where('pid', $id)->order('order desc')->field('id,name')->limit(4)->select();
  293. foreach ($list as $key => $value) {
  294. $list[$key]['total'] = $this->hydEquipmentModel->where('equipment_type', $value['id'])->count();
  295. }
  296. return CatchResponse::success($list);
  297. }
  298. /**
  299. * @Descripttion: 获取不同类型的设备
  300. * @name: likang
  301. * @return {*}
  302. */
  303. public function getAllHydEqu(Request $request)
  304. {
  305. $data = $request->get();
  306. $type = $data['type'];
  307. $list = $this->hydEquipmentModel->where('equipment_type', $type)->field('id as value,name as text')->select();
  308. return CatchResponse::success($list);
  309. }
  310. public function importExcel(Request $request)
  311. {
  312. $map = [
  313. ['key' => 'A', 'value' => 'material_number'],
  314. ['key' => 'C', 'value' => 'equipment_type'],
  315. ['key' => 'D', 'value' => 'name'],
  316. ['key' => 'E', 'value' => 'equipment_model'],
  317. ['key' => 'F', 'value' => 'issue_unit'],
  318. ['key' => 'G', 'value' => 'fixed_asset_number'],
  319. ['key' => 'H', 'value' => 'fixed_asset_number2'],
  320. ['key' => 'I', 'value' => 'serial_number'],
  321. ['key' => 'J', 'value' => 'factory_number'],
  322. ['key' => 'K', 'value' => 'check_last_time'],
  323. ['key' => 'L', 'value' => 'check_next_time'],
  324. ['key' => 'M', 'value' => 'check_status'],
  325. ['key' => 'N', 'value' => 'status'],
  326. ];
  327. //过滤
  328. $func = function ($data) {
  329. $data['equipment_type'] = EquipmentType::where('pid', '<>', 0)->where('name', $data['equipment_type'])->value('id');
  330. if ($data['check_status'] == '已校验') {
  331. $data['check_status'] = 2;
  332. } else if ($data['check_status'] == '待校验') {
  333. $data['check_status'] = 1;
  334. } else if ($data['check_status'] == '已报废') {
  335. $data['check_status'] = -1;
  336. } else {
  337. $data['check_status'] = 1;
  338. }
  339. if ($data['status'] = '使用中') {
  340. $data['status'] = 4;
  341. } else if ($data['status'] == '待维修') {
  342. $data['status'] = -1;
  343. } else if ($data['status'] == '在途') {
  344. $data['status'] = 1;
  345. } else if ($data['status'] == '待校验') {
  346. $data['status'] = 2;
  347. } else if ($data['status'] == '备用') {
  348. $data['status'] = 3;
  349. }
  350. return $data;
  351. };
  352. return importExcel($request, $map, $this->hydEquipmentModel, $func);
  353. }
  354. /**
  355. * @Descripttion: 查看上传的日志
  356. * @name: likang
  357. * @return {*}
  358. */
  359. public function SelectLogByImei(Request $request)
  360. {
  361. //获取设备的imei号
  362. $imei = $request->get('imei');
  363. $pub_ack = new PublishAck();
  364. $list = Db::name('publish_ack')->where('Imei', $imei)->field('id,PublishVersion,AddTime,Succ,AckTime,Imei')->order('PublishVersion', 'desc')->select()->toArray();
  365. foreach ($list as $key => $item) {
  366. //时间
  367. $list[$key]['AddTime'] = date('Y-m-d H:i:s', substr($item['AddTime'], 0, 10));
  368. $list[$key]['AckTime'] = date('Y-m-d H:i:s', substr($item['AckTime'], 0, 10));
  369. }
  370. return CatchResponse::success($list);
  371. }
  372. /**
  373. * @Descripttion: 获取日志详情
  374. * @name: likang
  375. * @param {Request} $request
  376. * @return {*}
  377. */
  378. public function getLogDetail(Request $request)
  379. {
  380. $type = $request->get('type');
  381. $id = $request->get('id');
  382. $data = null;
  383. if ($type == 1) {
  384. $data = Db::name('publish_ack')->where('id', $id)->value('PublishContent');
  385. $data = optimize_json($data);
  386. $data = json_decode($data, true);
  387. if (json_last_error() != 0) {
  388. return CatchResponse::fail(json_last_error_msg());
  389. }
  390. } else {
  391. $data = Db::name('publish_ack')->where('id', $id)->value('Extra');
  392. $data = optimize_json($data);
  393. }
  394. return CatchResponse::success($data);
  395. }
  396. }