123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- <?php
- namespace catchAdmin\hydraulic\controller;
- error_reporting(1);
- use catchAdmin\api\model\PublishAck;
- use catchAdmin\equipment\model\EquipmentType;
- use catcher\base\CatchRequest as Request;
- use catcher\CatchResponse;
- use catcher\base\CatchController;
- use catchAdmin\hydraulic\model\HydEquipment as hydEquipmentModel;
- use catchAdmin\hydraulic\model\Hydraulic;
- use catchAdmin\hydraulic\model\Wrench;
- use catchAdmin\system\model\SysDictData;
- use catcher\base\CatchRequest;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use PhpParser\Node\Stmt\Catch_;
- use think\facade\Db;
- class HydEquipment extends CatchController
- {
- protected $hydEquipmentModel;
- public function __construct(HydEquipmentModel $hydEquipmentModel)
- {
- $this->hydEquipmentModel = $hydEquipmentModel;
- }
- /**
- * 列表
- * @time 2022年06月21日 11:02
- * @param Request $request
- */
- public function index(Request $request): \think\Response
- {
- return CatchResponse::paginate($this->hydEquipmentModel->getList());
- }
- /**
- * 保存信息
- * @time 2022年06月21日 11:02
- * @param Request $request
- */
- public function save(Request $request): \think\Response
- {
- $data = $request->post();
- $hydraulic = null;
- $wrench = null;
- $hydequ = [
- // 物料号
- 'material_number' => $data['material_number'],
- // 设备类别
- 'equipment_type' => $data['equipment_type'],
- // 工具名称
- 'name' => $data['name'],
- // 设备型号
- 'equipment_model' => $data['equipment_model'],
- // 发放单位
- 'issue_unit' => $data['issue_unit'],
- // 固定资产编号
- 'fixed_asset_number' => $data['fixed_asset_number'],
- // 类固定资产编号
- 'fixed_asset_number2' => $data['fixed_asset_number2'],
- // 序列号
- 'serial_number' => $data['serial_number'],
- // 出厂编号
- 'factory_number' => $data['factory_number'],
- // 上次校验时间
- 'check_last_time' => $data['check_last_time'],
- // 下次校验时间
- 'check_next_time' => $data['check_next_time'],
- // 校验状态
- 'check_status' => $data['check_status'],
- // 状态
- 'status' => $data['status'],
- 'address' => $data['address'],
- 'longitude' => $data['longitude'],
- 'latitude' => $data['latitude']
- ];
- Db::startTrans();
- try {
- $id = $this->hydEquipmentModel->storeBy($hydequ);
- if ($data['equipment_type'] == 2) {
- $hydraulic = [
- // 编号
- 'number' => $data['number'],
- // 所属部门
- 'department_id' => $data['department_id'],
- 'remark' => $data['remark'] ? $data['remark'] : '',
- 'max_pressure' => $data['max_pressure'],
- 'min_pressure' => $data['min_pressure'],
- 'imei' => $data['imei'],
- 'effective_period' => $data['effective_period'],
- //设备的id
- 'eq_id' => $id
- ];
- $hyd_exist = Hydraulic::where('imei', $data['imei'])->find();
- if ($hyd_exist) {
- Db::rollback();
- return CatchResponse::fail('imei号已存在');
- }
- Hydraulic::create($hydraulic);
- }
- //保存扳手
- if ($data['equipment_type'] == 3 || $data['equipment_type'] == 4) {
- $wrench = [
- // 编号
- 'number' => $data['number'],
- //设备id
- 'eq_id' => $id,
- // 所属部门
- 'department_id' => $data['department_id'],
- 'remark' => $data['remark'] ? $data['remark'] : '',
- 'min_pressure' => $data['min_pressure'],
- 'max_pressure' => $data['max_pressure'],
- 'min_torque' => $data['min_torque'],
- 'max_torque' => $data['max_torque'],
- 'angle_sensor' => $data['angle_sensor'],
- 'angular_resolution' => $data['angular_resolution']
- ];
- $wrench_exist = Wrench::where('number', $data['number'])->find();
- if ($wrench_exist) {
- Db::rollback();
- return CatchResponse::fail('扳手编号已存在');
- }
- Wrench::create($wrench);
- }
- } catch (\Exception $e) {
- Db::rollback();
- return CatchResponse::fail($e->getMessage());
- }
- Db::commit();
- return CatchResponse::success();
- }
- /**
- * 读取
- * @time 2022年06月21日 11:02
- * @param $id
- */
- public function read($id): \think\Response
- {
- return CatchResponse::success($this->hydEquipmentModel->findBy($id));
- }
- /**
- * 更新
- * @time 2022年06月21日 11:02
- * @param Request $request
- * @param $id
- */
- public function update(Request $request, $id): \think\Response
- {
- $data = $request->post();
- $hydraulic = null;
- $wrench = null;
- $hydequ = [
- // 物料号
- 'material_number' => $data['material_number'],
- // 设备类别
- 'equipment_type' => $data['equipment_type'],
- // 工具名称
- 'name' => $data['name'],
- // 设备型号
- 'equipment_model' => $data['equipment_model'],
- // 发放单位
- 'issue_unit' => $data['issue_unit'],
- // 固定资产编号
- 'fixed_asset_number' => $data['fixed_asset_number'],
- // 类固定资产编号
- 'fixed_asset_number2' => $data['fixed_asset_number2'],
- // 序列号
- 'serial_number' => $data['serial_number'],
- // 出厂编号
- 'factory_number' => $data['factory_number'],
- // 上次校验时间
- 'check_last_time' => $data['check_last_time'] ? strstr($data['check_last_time'], '-') ? strtotime($data['check_last_time']) : $data['check_last_time'] : '',
- // 下次校验时间
- 'check_next_time' => $data['check_next_time'] ? strstr($data['check_next_time'], '-') ? strtotime($data['check_next_time']) : $data['check_next_time'] : '',
- // 校验状态
- 'check_status' => $data['check_status'],
- // 状态
- 'status' => $data['status'],
- 'address' => $data['address'],
- 'longitude' => $data['longitude'],
- 'latitude' => $data['latitude']
- ];
- if ($data['equipment_type'] == 2) {
- $hydraulic = [
- // 编号
- 'number' => $data['number'],
- // 所属部门
- 'department_id' => $data['department_id'],
- //备注
- 'remark' => $data['remark'],
- 'max_pressure' => $data['max_pressure'],
- 'min_pressure' => $data['min_pressure'],
- 'imei' => $data['imei'],
- 'effective_period' => $data['effective_period'],
- //设备的id
- 'eq_id' => $id
- ];
- $hyd_exist = Hydraulic::where('imei', $data['imei'])->where('eq_id', '<>', $id)->find();
- if ($hyd_exist) {
- Db::rollback();
- return CatchResponse::fail('imei号已存在');
- }
- $bool = Hydraulic::where('eq_id', $id)->find();
- if ($bool) {
- Hydraulic::where('eq_id', $id)->update($hydraulic);
- } else {
- Hydraulic::create($hydraulic);
- }
- }
- //保存扳手
- if ($data['equipment_type'] == 3 || $data['equipment_type'] == 4) {
- $wrench = [
- // 编号
- 'number' => $data['number'],
- //设备id
- 'eq_id' => $id,
- // 所属部门
- 'department_id' => $data['department_id'],
- 'remark' => $data['remark'],
- 'min_pressure' => $data['min_pressure'],
- 'max_pressure' => $data['max_pressure'],
- 'min_torque' => $data['min_torque'],
- 'max_torque' => $data['max_torque'],
- 'angular_resolution' => $data['angular_resolution'],
- 'angle_sensor' => $data['angle_sensor'],
- // 扭矩
- ];
- $wrench_exist = Wrench::where('number', $data['number'])->where('id', '<>', $data['list']['id'])->find();
- if ($wrench_exist) {
- Db::rollback();
- return CatchResponse::fail('扳手编号已存在');
- }
- $bool = Wrench::where('eq_id', $id)->find();
- if ($bool) {
- Wrench::update($wrench, ['eq_id' => $id]);
- } else {
- Wrench::create($wrench);
- }
- }
- return CatchResponse::success($this->hydEquipmentModel->updateBy($id, $hydequ));
- }
- /**
- * 删除
- * @time 2022年06月21日 11:02
- * @param $id
- */
- public function delete($id): \think\Response
- {
- $data = array();
- $where = [];
- if (is_array($id)) {
- $where[] = ['id', 'in', $id];
- $data = $this->hydEquipmentModel->where($where)->select()->toArray();
- } else {
- $list = $this->hydEquipmentModel->where('id', $id)->find();
- $data[] = $list;
- }
- Db::startTrans();
- try {
- foreach ($data as $item) {
- if ($item['equipment_type'] == 2) {
- Hydraulic::where('eq_id', '=', $item['id'])->delete();
- }
- if ($item['equipment_type'] == 3 || $item['equipment_type'] == 4) {
- $wrench = Wrench::where('eq_id', '=', $item['id'])->find();
- $wrench->delete();
- }
- }
- $this->hydEquipmentModel->deleteBy($id, true);
- } catch (\Exception $e) {
- Db::rollback();
- return CatchResponse::fail($e->getMessage());
- }
- Db::commit();
- return CatchResponse::success();
- }
- /**
- * @Descripttion: 获取设备状态和校验状态
- * @name: likang
- * @return {*}
- */
- public function getHydEquipmentType(Request $request)
- {
- $data = $request->get();
- $code = $data['code'];
- $sysDict = new SysDictData();
- $list = $sysDict->getTypesByCodeWithRemark($code, '');
- return CatchResponse::success($list);
- }
- /**
- * 根据设备类别统计每个设备的总数
- */
- public function getTotalByEquipmentType($id)
- {
- $list = EquipmentType::where('pid', $id)->order('order desc')->field('id,name')->limit(4)->select();
- foreach ($list as $key => $value) {
- $list[$key]['total'] = $this->hydEquipmentModel->where('equipment_type', $value['id'])->count();
- }
- return CatchResponse::success($list);
- }
- /**
- * @Descripttion: 获取不同类型的设备
- * @name: likang
- * @return {*}
- */
- public function getAllHydEqu(Request $request)
- {
- $data = $request->get();
- $type = $data['type'];
- $list = $this->hydEquipmentModel->where('equipment_type', $type)->field('id as value,name as text')->select();
- return CatchResponse::success($list);
- }
- public function importExcel(Request $request)
- {
- $map = [
- ['key' => 'A', 'value' => 'material_number'],
- ['key' => 'C', 'value' => 'equipment_type'],
- ['key' => 'D', 'value' => 'name'],
- ['key' => 'E', 'value' => 'equipment_model'],
- ['key' => 'F', 'value' => 'issue_unit'],
- ['key' => 'G', 'value' => 'fixed_asset_number'],
- ['key' => 'H', 'value' => 'fixed_asset_number2'],
- ['key' => 'I', 'value' => 'serial_number'],
- ['key' => 'J', 'value' => 'factory_number'],
- ['key' => 'K', 'value' => 'check_last_time'],
- ['key' => 'L', 'value' => 'check_next_time'],
- ['key' => 'M', 'value' => 'check_status'],
- ['key' => 'N', 'value' => 'status'],
- ];
- //过滤
- $func = function ($data) {
- $data['equipment_type'] = EquipmentType::where('pid', '<>', 0)->where('name', $data['equipment_type'])->value('id');
- if ($data['check_status'] == '已校验') {
- $data['check_status'] = 2;
- } else if ($data['check_status'] == '待校验') {
- $data['check_status'] = 1;
- } else if ($data['check_status'] == '已报废') {
- $data['check_status'] = -1;
- } else {
- $data['check_status'] = 1;
- }
- if ($data['status'] = '使用中') {
- $data['status'] = 4;
- } else if ($data['status'] == '待维修') {
- $data['status'] = -1;
- } else if ($data['status'] == '在途') {
- $data['status'] = 1;
- } else if ($data['status'] == '待校验') {
- $data['status'] = 2;
- } else if ($data['status'] == '备用') {
- $data['status'] = 3;
- }
- return $data;
- };
- return importExcel($request, $map, $this->hydEquipmentModel, $func);
- }
- /**
- * @Descripttion: 查看上传的日志
- * @name: likang
- * @return {*}
- */
- public function SelectLogByImei(Request $request)
- {
- //获取设备的imei号
- $imei = $request->get('imei');
- $pub_ack = new PublishAck();
- $list = Db::name('publish_ack')->where('Imei', $imei)->field('id,PublishVersion,AddTime,Succ,AckTime,Imei')->order('PublishVersion', 'desc')->select()->toArray();
- foreach ($list as $key => $item) {
- //时间
- $list[$key]['AddTime'] = date('Y-m-d H:i:s', substr($item['AddTime'], 0, 10));
- $list[$key]['AckTime'] = date('Y-m-d H:i:s', substr($item['AckTime'], 0, 10));
- }
- return CatchResponse::success($list);
- }
- /**
- * @Descripttion: 获取日志详情
- * @name: likang
- * @param {Request} $request
- * @return {*}
- */
- public function getLogDetail(Request $request)
- {
- $type = $request->get('type');
- $id = $request->get('id');
- $data = null;
- if ($type == 1) {
- $data = Db::name('publish_ack')->where('id', $id)->value('PublishContent');
- $data = optimize_json($data);
- $data = json_decode($data, true);
- if (json_last_error() != 0) {
- return CatchResponse::fail(json_last_error_msg());
- }
- } else {
- $data = Db::name('publish_ack')->where('id', $id)->value('Extra');
- $data = optimize_json($data);
- }
- return CatchResponse::success($data);
- }
- }
|