123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- use think\facade\Db;
- use think\migration\Seeder;
- class ImportFakerComment extends Seeder
- {
- /**
- * Run Method.
- *
- * Write your database seeder using this method.
- *
- * More information on writing seeders is available here:
- * http://docs.phinx.org/en/latest/seeding.html
- */
- public function run()
- {
- // 清空假评论
- // \app\common\model\Comment::where('create_time', 0)->delete();
- $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
- $excel = $reader->load(public_path().'public/uploads/import_comment.xlsx');
- // $excel = $reader->load(public_path().'public/uploads/import_comment.xlsx');
- // $excel = PHPExcel_IOFactory::load(public_path().'/uploads/import_comment.xlsx');
- $nameArr = $excel->getSheetNames();
- $sheetCount = $excel->getSheetCount();
-
- $goodsIds = [
- // 线上环境
- 10036, // 2.5kg
- 10035, // 5kg
- 10037, // 礼盒装
- 10041, // 2.5kg 10袋
- 10042, // 5kg 5袋
- 10040, // 5kg礼盒 4盒
- // 预生产测试
- // 10044,
- // 10043,
- // 10042,
- // 10038,
- // 10039,
- // 10040,
- ];
- for ($i = 0; $i < $sheetCount; $i++) {
- $sheet = $excel->getSheet($i);
- $totalRows = $sheet->getHighestRow();
- $totalColumn = $sheet->getHighestColumn();
- \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($totalColumn);
- for ($j = 2; $j <= $totalRows; $j++) {
- $nickName = trim($this->removeEmojiChar($sheet->getCell('B' . $j)->getValue()));
- $content = trim($this->removeEmojiChar($sheet->getCell('C' . $j)->getValue()));
- if (!empty($nickName) && !empty($content)) {
- // 写入评价
- \app\common\model\Comment::create([
- 'score' => 5,
- 'content' => $content,
- 'user_id' => 0,
- 'nick_name' => $nickName,
- 'goods_id' => $goodsIds[$i],
- 'store_id' => 10001,
- 'is_public' => 1,
- 'status' => 1,
- 'create_time' => 0,
- 'update_time' => 0,
- ]);
- }
- }
- }
- }
- public function removeEmojiChar($str)
- {
- $mbLen = mb_strlen($str);
- $strArr = [];
- for ($i = 0; $i < $mbLen; $i++) {
- $mbSubstr = mb_substr($str, $i, 1, 'utf-8');
- if (strlen($mbSubstr) >= 4) {
- continue;
- }
- $strArr[] = $mbSubstr;
- }
- return implode('', $strArr);
- }
- }
|