123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace app\api\controller;
- use app\common\constant\ApplyConstant;
- use app\common\constant\CommonConstant;
- use app\common\constant\ContractConstant;
- use app\common\constant\EvectionConstant;
- use app\common\constant\LeaveConstant;
- use app\common\constant\MaintainConstant;
- use app\common\constant\OfferConstant;
- use hg\apidoc\annotation as Apidoc;
- use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
- use PhpOffice\PhpSpreadsheet\Reader\Csv;
- use PhpOffice\PhpSpreadsheet\Reader\Xls;
- use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
- use think\db\exception\BindParamException;
- use think\Exception;
- use think\exception\PDOException;
- /**
- * @Apidoc\Title("公用")
- * @Apidoc\Group("api")
- * @Apidoc\Sort("4")
- */
- class Common extends Base
- {
- public function initialize()
- {
- parent::initialize();
- }
- /**
- * 类型列表
- *
- * @Apidoc\Method("POST")
- * @Apidoc\Returned("module_list", type="array", desc="模块列表")
- * @Apidoc\Returned("degree_list", type="array", desc="缓急程度列表")
- * @Apidoc\Returned("pay_type_list", type="array", desc="采购支付方式列表")
- * @Apidoc\Returned("time_list", type="array", desc="请假周期列表")
- * @Apidoc\Returned("data1", type="array", desc="采购类型列表")
- * @Apidoc\Returned("data2", type="array", desc="呈批类型列表")
- * @Apidoc\Returned("data5", type="array", desc="出差类型列表")
- * @Apidoc\Returned("data6", type="array", desc="请假类型列表")
- * @Apidoc\Returned("data8", type="array", desc="维修类型列表")
- * @Apidoc\Returned("data9", type="array", desc="合同类型列表")
- */
- public function get_type_list()
- {
- $module_list = get_one_two_array(CommonConstant::get_module_list(), 'id', 'name');
- $degree_list = get_one_two_array(OfferConstant::get_degree_list(), 'id', 'name');
- $pay_type_list = get_one_two_array(ApplyConstant::get_pay_type_list(), 'id', 'name');
- $time_list = get_one_two_array(LeaveConstant::get_time_list(), 'id', 'name');
- $data1 = get_one_two_array(ApplyConstant::get_type_list(), 'id', 'name');
- $data2 = get_one_two_array(OfferConstant::get_type_list(), 'id', 'name');
- $data5 = get_one_two_array(EvectionConstant::get_type_list(), 'id', 'name');
- $data6 = get_one_two_array(LeaveConstant::get_type_list(), 'id', 'name');
- $data8 = get_one_two_array(MaintainConstant::get_type_list(), 'id', 'name');
- $data9 = get_one_two_array(ContractConstant::get_type_list(), 'id', 'name');
- $data = compact("module_list", "degree_list", "pay_type_list", "time_list", "data1", "data2", "data5", "data6", "data8", "data9");
- $this->success('类型列表', $data);
- }
- /**
- * 导入
- *
- * @Apidoc\Method("POST")
- * @Apidoc\Param("file", type="string", require=true, desc="文件地址")
- * @return void
- * @throws PDOException
- * @throws BindParamException
- */
- public function import()
- {
- set_time_limit(0);
- $file = $this->request->post('file');
- if (!$file) {
- $this->error(__('Parameter %s can not be empty', 'file'));
- }
- $filePath = ROOT_PATH . DS . 'public' . DS . $file;
- if (!is_file($filePath)) {
- $this->error(__('No results were found'));
- }
- //实例化reader
- $ext = pathinfo($filePath, PATHINFO_EXTENSION);
- if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
- $this->error(__('Unknown data format'));
- }
- if ($ext === 'csv') {
- $file = fopen($filePath, 'r');
- $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
- $fp = fopen($filePath, 'w');
- $n = 0;
- while ($line = fgets($file)) {
- $line = rtrim($line, "\n\r\0");
- $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
- if ($encoding !== 'utf-8') {
- $line = mb_convert_encoding($line, 'utf-8', $encoding);
- }
- if ($n == 0 || preg_match('/^".*"$/', $line)) {
- fwrite($fp, $line . "\n");
- } else {
- fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
- }
- $n++;
- }
- fclose($file) || fclose($fp);
- $reader = new Csv();
- } elseif ($ext === 'xls') {
- $reader = new Xls();
- } else {
- $reader = new Xlsx();
- }
- //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
- $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';
- $table = $this->model->getQuery()->getTable();
- $database = \think\Config::get('database.database');
- $fieldArr = [];
- $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
- foreach ($list as $k => $v) {
- if ($importHeadType == 'comment') {
- $v['COLUMN_COMMENT'] = explode(':', $v['COLUMN_COMMENT'])[0]; //字段备注有:时截取
- $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
- } else {
- $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
- }
- }
- //加载文件
- $insert = [];
- try {
- if (!$PHPExcel = $reader->load($filePath)) {
- $this->error(__('Unknown data format'));
- }
- $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
- $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
- $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
- $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
- $fields = [];
- for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
- for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
- $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
- $fields[] = $val;
- }
- }
- $product = Product::column('id,name'); // 产品列表
- $status_list = config('bill_status_array');
- $status_list_flip = array_flip($status_list); // 交换数组中的键和值
- $status_value = array_values($status_list);
- for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
- $values = [];
- for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
- $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
- $values[] = is_null($val) ? '' : $val;
- }
- $row = [];
- $temp = array_combine($fields, $values);
- foreach ($temp as $k => $v) {
- if (isset($fieldArr[$k]) && $k !== '') {
- $row[$fieldArr[$k]] = $v;
- }
- }
- if ($row) {
- if(isset($row['date']) && !empty($row['date'])){
- // 状态 转为状态码
- if(in_array($row['status'],$status_value)){
- $row['status'] = $status_list_flip[$row['status']];
- }
- // 产品名称 转为产品ID
- if(in_array($row['product_id'],$product)){
- foreach ($product as $kk=>$vv){
- if($row['product_id'] == $vv){
- $row['product_id'] = $kk;
- }
- }
- } else{
- $row['product_id'] = 0;
- }
- $insert[] = $row;
- }
- }
- }
- } catch (Exception $exception) {
- $this->error($exception->getMessage());
- }
- if (!$insert) {
- $this->error(__('No rows were updated'));
- }
- try {
- $this->model->saveAll($insert);
- $msg = '导入成功';
- } catch (PDOException $exception) {
- $msg = $exception->getMessage();
- $this->error($msg);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success($msg);
- }
- }
|