1 个管道
的构建
通过
耗费
9 秒
导入功能添加创建订单逻辑
正在显示
1 个修改的文件
包含
204 行增加
和
1 行删除
@@ -2,7 +2,15 @@ | @@ -2,7 +2,15 @@ | ||
2 | 2 | ||
3 | namespace app\admin\controller\user; | 3 | namespace app\admin\controller\user; |
4 | 4 | ||
5 | +use app\admin\library\Auth; | ||
5 | use app\common\controller\Backend; | 6 | use app\common\controller\Backend; |
7 | +use Exception; | ||
8 | +use fast\Random; | ||
9 | +use PhpOffice\PhpSpreadsheet\Cell\Coordinate; | ||
10 | +use PhpOffice\PhpSpreadsheet\Reader\Csv; | ||
11 | +use PhpOffice\PhpSpreadsheet\Reader\Xls; | ||
12 | +use PhpOffice\PhpSpreadsheet\Reader\Xlsx; | ||
13 | +use think\exception\PDOException; | ||
6 | 14 | ||
7 | /** | 15 | /** |
8 | * 会员管理 | 16 | * 会员管理 |
@@ -75,8 +83,203 @@ class User extends Backend | @@ -75,8 +83,203 @@ class User extends Backend | ||
75 | return parent::edit($ids); | 83 | return parent::edit($ids); |
76 | } | 84 | } |
77 | 85 | ||
86 | + | ||
78 | public function import(){ | 87 | public function import(){ |
79 | - parent::import(); | 88 | + |
89 | + $file = $this->request->request('file'); | ||
90 | + if (!$file) { | ||
91 | + $this->error(__('Parameter %s can not be empty', 'file')); | ||
92 | + } | ||
93 | + $filePath = ROOT_PATH . DS . 'public' . DS . $file; | ||
94 | + if (!is_file($filePath)) { | ||
95 | + $this->error(__('No results were found')); | ||
96 | + } | ||
97 | + //实例化reader | ||
98 | + $ext = pathinfo($filePath, PATHINFO_EXTENSION); | ||
99 | + if (!in_array($ext, ['csv', 'xls', 'xlsx'])) { | ||
100 | + $this->error(__('Unknown data format')); | ||
101 | + } | ||
102 | + if ($ext === 'csv') { | ||
103 | + $file = fopen($filePath, 'r'); | ||
104 | + $filePath = tempnam(sys_get_temp_dir(), 'import_csv'); | ||
105 | + $fp = fopen($filePath, "w"); | ||
106 | + $n = 0; | ||
107 | + while ($line = fgets($file)) { | ||
108 | + $line = rtrim($line, "\n\r\0"); | ||
109 | + $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']); | ||
110 | + if ($encoding != 'utf-8') { | ||
111 | + $line = mb_convert_encoding($line, 'utf-8', $encoding); | ||
112 | + } | ||
113 | + if ($n == 0 || preg_match('/^".*"$/', $line)) { | ||
114 | + fwrite($fp, $line . "\n"); | ||
115 | + } else { | ||
116 | + fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n"); | ||
117 | + } | ||
118 | + $n++; | ||
119 | + } | ||
120 | + fclose($file) || fclose($fp); | ||
121 | + | ||
122 | + $reader = new Csv(); | ||
123 | + } elseif ($ext === 'xls') { | ||
124 | + $reader = new Xls(); | ||
125 | + } else { | ||
126 | + $reader = new Xlsx(); | ||
127 | + } | ||
128 | + | ||
129 | + //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name | ||
130 | + $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment'; | ||
131 | + | ||
132 | + $table = $this->model->getQuery()->getTable(); | ||
133 | + $database = \think\Config::get('database.database'); | ||
134 | + $fieldArr = []; | ||
135 | + $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]); | ||
136 | + foreach ($list as $k => $v) { | ||
137 | + if ($importHeadType == 'comment') { | ||
138 | + $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME']; | ||
139 | + } else { | ||
140 | + $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME']; | ||
141 | + } | ||
142 | + } | ||
143 | + | ||
144 | + //加载文件 | ||
145 | + $insert = []; | ||
146 | + $product = []; | ||
147 | + try { | ||
148 | + if (!$PHPExcel = $reader->load($filePath)) { | ||
149 | + $this->error(__('Unknown data format')); | ||
150 | + } | ||
151 | + $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表 | ||
152 | + $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号 | ||
153 | + $allRow = $currentSheet->getHighestRow(); //取得一共有多少行 | ||
154 | + $maxColumnNumber = Coordinate::columnIndexFromString($allColumn); | ||
155 | + $fields = []; | ||
156 | + $col = ''; | ||
157 | + for ($currentRow = 1; $currentRow <= 1; $currentRow++) { | ||
158 | + for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) { | ||
159 | + $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue(); | ||
160 | + $fields[] = $val; | ||
161 | + if($val == '产品id'){ | ||
162 | + $col = $currentColumn; | ||
163 | + } | ||
164 | + } | ||
165 | + } | ||
166 | + | ||
167 | + for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) { | ||
168 | + $values = []; | ||
169 | + for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) { | ||
170 | + $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue(); | ||
171 | + $values[] = is_null($val) ? '' : $val; | ||
172 | + } | ||
173 | + $row = []; | ||
174 | + $temp = array_combine($fields, $values); | ||
175 | + foreach ($temp as $k => $v) { | ||
176 | + if (isset($fieldArr[$k]) && $k !== '') { | ||
177 | + $row[$fieldArr[$k]] = $v; | ||
178 | + } | ||
179 | + } | ||
180 | + | ||
181 | + if ($row) { | ||
182 | + $insert[] = $row; | ||
183 | + | ||
184 | + if(!empty($col)){ | ||
185 | + //处理产品id | ||
186 | + $productId = $currentSheet->getCellByColumnAndRow($col, $currentRow)->getValue(); | ||
187 | + $product[] = ['product_id'=>$productId]; | ||
188 | + } | ||
189 | + } | ||
190 | + } | ||
191 | + } catch (Exception $exception) { | ||
192 | + $this->error($exception->getMessage()); | ||
193 | + } | ||
194 | + if (!$insert) { | ||
195 | + $this->error(__('No rows were updated')); | ||
196 | + } | ||
197 | + | ||
198 | + try { | ||
199 | + //是否包含admin_id字段 | ||
200 | + $has_admin_id = false; | ||
201 | + foreach ($fieldArr as $name => $key) { | ||
202 | + if ($key == 'admin_id') { | ||
203 | + $has_admin_id = true; | ||
204 | + break; | ||
205 | + } | ||
206 | + } | ||
207 | + if ($has_admin_id) { | ||
208 | + $auth = Auth::instance(); | ||
209 | + foreach ($insert as &$val) { | ||
210 | + if (!isset($val['admin_id']) || empty($val['admin_id'])) { | ||
211 | + $val['admin_id'] = $auth->isLogin() ? $auth->id : 0; | ||
212 | + } | ||
213 | + } | ||
214 | + } | ||
215 | + | ||
216 | + foreach ($insert as $k => $v){ | ||
217 | + $id = $this->model->insertGetId($v); | ||
218 | + if(!empty($col)){ | ||
219 | + $product[$k]['user_id'] = $id; | ||
220 | + } | ||
221 | + } | ||
222 | + } | ||
223 | + catch (PDOException $exception) { | ||
224 | + $msg = $exception->getMessage(); | ||
225 | + if (preg_match("/.+Integrity constraint violation: 1062 Duplicate entry '(.+)' for key '(.+)'/is", $msg, $matches)) { | ||
226 | + $msg = "导入失败,包含【{$matches[1]}】的记录已存在"; | ||
227 | + }; | ||
228 | + $this->error($msg); | ||
229 | + } catch (Exception $e) { | ||
230 | + $this->error($e->getMessage()); | ||
231 | + } | ||
232 | + | ||
233 | + | ||
234 | + if(!empty($product)){ | ||
235 | + foreach ($product as $k=>$v){ | ||
236 | + $product_id = $v['product_id']; | ||
237 | + $user_id = $v['user_id']; | ||
238 | + | ||
239 | + $product = new \app\admin\model\Product(); | ||
240 | + $product = $product->where(['id'=>$product_id])->find(); | ||
241 | + if(empty($product)){ | ||
242 | + continue; | ||
243 | + } | ||
244 | + $product = $product->toArray(); | ||
245 | + | ||
246 | + $productCourse = new \app\admin\model\ProductCoures(); | ||
247 | + $productCourse = $productCourse->where(['product_id'=>$product_id])->select(); | ||
248 | + $product['product_course'] = $productCourse; | ||
249 | + | ||
250 | + $user = new \app\admin\model\User(); | ||
251 | + $user = $user->where(['id'=>$user_id])->find(); | ||
252 | + if(empty($user)){ | ||
253 | + continue; | ||
254 | + } | ||
255 | + | ||
256 | + $order_id = Random::uuid(); | ||
257 | + $order_id = str_replace("-","",$order_id); | ||
258 | + | ||
259 | + $now = date('Y-m-d H:i:s'); | ||
260 | + $data['product_id'] = $product_id; | ||
261 | + $data['user_id'] = $user_id; | ||
262 | + $data['order_id'] = $order_id; | ||
263 | + $data['status'] = 'success'; | ||
264 | + $data['product_doc'] = json_encode($product); | ||
265 | + $data['total'] = $product['price']; | ||
266 | + $data['create_time'] = $now; | ||
267 | + $data['third_order_id'] = ''; | ||
268 | + $data['count'] = $product['count']; | ||
269 | + $data['remain'] = ($product['count'] == -1)?999999:$product['count']; | ||
270 | + $data['start'] = $now; | ||
271 | + $data['end'] = date('Y-m-d H:i:s',strtotime('+'.$product['dateCount'].' day',strtotime($now))); | ||
272 | + $data['success_time'] = $now; | ||
273 | + $data['notify'] = false; | ||
274 | + | ||
275 | + $order = new \app\admin\model\Order(); | ||
276 | + $order->insertGetId($data); | ||
277 | + } | ||
278 | + } | ||
279 | + | ||
280 | + $this->success(); | ||
281 | + | ||
282 | + | ||
80 | } | 283 | } |
81 | 284 | ||
82 | /** | 285 | /** |
-
请 注册 或 登录 后发表评论