123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace catchAdmin\yunying\controller;
- use catcher\base\CatchRequest as Request;
- use catcher\CatchResponse;
- use catcher\base\CatchController;
- use catchAdmin\yunying\model\KeyPersonnel as keyPerSonnelModel;
- use catcher\Utils;
- use catcher\library\excel\Excel;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use think\facade\Env;
- use think\facade\Db;
- class KeyPersonnel extends CatchController
- {
- protected $keyPerSonnelModel;
-
- public function __construct(KeyPerSonnelModel $keyPerSonnelModel)
- {
- $this->keyPerSonnelModel = $keyPerSonnelModel;
- }
-
-
- public function index(Request $request) : \think\Response
- {
- return CatchResponse::paginate($this->keyPerSonnelModel->getList());
- }
-
-
- public function save(Request $request) : \think\Response
- {
-
- return CatchResponse::success($this->keyPerSonnelModel->storeBy($request->post()));
- }
-
-
- public function read($id) : \think\Response
- {
- return CatchResponse::success($this->keyPerSonnelModel->findBy($id));
- }
-
-
- public function update(Request $request, $id) : \think\Response
- {
- return CatchResponse::success($this->keyPerSonnelModel->updateBy($id, $request->post()));
- }
-
-
- public function delete($id) : \think\Response
- {
- return CatchResponse::success($this->keyPerSonnelModel->deleteBy($id));
- }
-
- public function importExcel(Request $request)
- {
- $url = $request->post('url');
- if (!$url) {
- return CatchResponse::fail('请上传文件');
- }
- $creator_id = $request->post('creator_id');
-
- $parse_url = parse_url($url)['path'];
-
- $objPHPExcel = IOFactory::load(public_path() . $parse_url);
-
-
-
- $sheetCount = $objPHPExcel->getSheetCount();
- $fail = 0;
- $success = 0;
- $total = 0;
- $data = [];
-
- for ($index = 0; $index < $sheetCount; $index++) {
-
- $sheet = $objPHPExcel->getSheet($index);
-
- $highestRow = $sheet->getHighestRow();
-
- if ($highestRow <= 1) {
- continue;
- }
- $card_type_id = Db::table("sys_dict_type")->where('code', 'CARD_TYPE_OPTION')->value('id');
- $total += $highestRow - 1;
- for ($j = 2; $j <= $highestRow; $j++) {
- $arr = array();
- $arr['name'] = strtoupper(trim($sheet->getCell("A" . $j)->getFormattedValue()));
- $arr['gender'] = strtoupper(trim($sheet->getCell("B" . $j)->getFormattedValue()));
- $card_type_text = strtoupper(trim($sheet->getCell("C" . $j)->getFormattedValue()));
- $arr['card_type'] = Db::table("sys_dict_data")->where('type_id', $card_type_id)->whereLike('value','%'.$card_type_text.'%')->cache(60)->value('code');
- $arr['card_number'] = strtoupper(trim($sheet->getCell("D" . $j)->getFormattedValue()));
- $arr['rfid_sn'] = strtoupper(trim($sheet->getCell("E" . $j)->getFormattedValue()));
- $arr['address'] = strtoupper(trim($sheet->getCell("F" . $j)->getFormattedValue()));
- $arr['guardians_name'] = strtoupper(trim($sheet->getCell("G" . $j)->getFormattedValue()));
- $arr['guardians_gender'] = strtoupper(trim($sheet->getCell("H" . $j)->getFormattedValue()));
- $arr['guardians_phone'] = strtoupper(trim($sheet->getCell("I" . $j)->getFormattedValue()));
- $arr['guardians_address'] = strtoupper(trim($sheet->getCell("J" . $j)->getFormattedValue()));
-
- array_push($data,$arr);
- }
- }
- array_unique($data, SORT_REGULAR);
-
- $success = $this->keyPerSonnelModel->limit(100)->insertAll($data);
- if ($success) {
- return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . ($total-$success) . '条');
- }
- return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
- }
- }
|