123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- <?php
- namespace catchAdmin\hydraulic\controller;
- error_reporting(1);
- 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号已存在');
- }
- Hydraulic::where('eq_id', $id)->update($hydraulic);
- }
- //保存扳手
- if ($data['equipment_type'] == 3 || $data['equipment_type'] == 4) {
- $wrench = [
- 'id' => $data['list']['id'],
- // 编号
- '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('扳手编号已存在');
- }
- Wrench::update($wrench, ['id' => $data['list']['id']]);
- }
- 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 importStores(Request $request)
- {
- $url = $request->post('url');
- if (!$url) {
- return CatchResponse::fail('请上传文件');
- }
- $brand_id = $request->post('brand_id');
- if (!$brand_id) {
- return CatchResponse::fail('请选择品牌');
- }
- $creator_id = $request->post('creator_id');
- //解析地址
- $parse_url = parse_url($url)['path'];
- //载入excel表格
- $objPHPExcel = IOFactory::load(public_path() . $parse_url);
- // var_dump($objPHPExcel);
- //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3')
- // $nameArr = $objPHPExcel->getSheetNames();
- // var_dump($nameArr);
- //获取表的数量
- $sheetCount = $objPHPExcel->getSheetCount();
- $fail = 0; //失败条数
- $success = 0; //成功条数
- $total = 0; //总导入数
- $excel_data = [];
- //循环读取每一张表
- for ($index = 0; $index < $sheetCount; $index++) {
- //设置当前要读取的表
- $sheet = $objPHPExcel->getSheet($index); //excel中的第一张sheet
- // var_dump($sheet);exit;
- $highestRow = $sheet->getHighestRow(); // 取得总行数
- // var_dump($highestRow);
- if ($highestRow <= 2) {
- continue;
- }
- $total += $highestRow - 3;
- for ($j = 4; $j <= $highestRow - 1; $j++) {
- $data = array(); //每条门店信息
- $name = trim($sheet->getCell("B" . $j)->getFormattedValue()); //门店名称
- $city_name = trim($sheet->getCell("C" . $j)->getFormattedValue()); //市区
- $area_name = trim($sheet->getCell("D" . $j)->getFormattedValue()); //县区
- $street = trim($sheet->getCell("E" . $j)->getFormattedValue()); //街道办
- $address = trim($sheet->getCell("F" . $j)->getFormattedValue()); //详细地址
- $longitude = trim($sheet->getCell("G" . $j)->getFormattedValue()); //经度
- $latitude = trim($sheet->getCell("H" . $j)->getFormattedValue()); //维度
- $category = trim($sheet->getCell("I" . $j)->getFormattedValue()); //门店类别
- if ($category == '直营店') {
- $mendian = '1';
- }
- if ($category == '经销商') {
- $mendian = '2';
- }
- if ($category == '加盟店') {
- $mendian = '3';
- }
- $principal = trim($sheet->getCell("J" . $j)->getFormattedValue()); //店长
- $mobile = trim($sheet->getCell("K" . $j)->getFormattedValue()); //电话
- if (!$name) {
- $fail++;
- $msg = '导入第' . $j . '行门店失败:门店名称不存在';
- $this->importFailLog($msg);
- continue;
- }
- $data = array(
- 'department_name' => $name,
- 'brand_id' => $brand_id,
- 'area_id' => Db::table('area')->whereLike('area_name', $area_name)->value('id'),
- 'street' => $street,
- 'address' => $address,
- 'status' => 1, //正常
- 'longitude' => $longitude,
- 'latitude' => $latitude,
- 'category' => $mendian,
- 'principal' => $principal,
- 'mobile' => $mobile,
- 'reserve_num' => 999, //预约剩余数
- 'creator_id' => $creator_id,
- 'created_at' => time(),
- 'updated_at' => time()
- );
- array_push($excel_data, $data);
- }
- }
- // var_dump($excel_data);
- // return CatchResponse::success();
- //防止Excel有重复,去重
- array_unique($excel_data, SORT_REGULAR);
- if ($count = $this->department->limit(100)->insertAll($excel_data)) {
- return CatchResponse::success('共' . $total . '条数据,成功' . $count . '条,失败' . $fail . '条');
- }
- return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
- // return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . $fail . '条');
- }
- }
|