|
@@ -0,0 +1,82 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Imports;
|
|
|
+
|
|
|
+use App\Http\Requests\API\CustomField\CreateOrUpdateRequest;
|
|
|
+use App\Models\CustomField;
|
|
|
+use App\Models\CustomOptions;
|
|
|
+use App\Models\NamingRule;
|
|
|
+use Illuminate\Support\Collection;
|
|
|
+use Illuminate\Support\Facades\Auth;
|
|
|
+use Maatwebsite\Excel\Concerns\ToCollection;
|
|
|
+use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
|
|
+
|
|
|
+class NameRuleImport implements ToCollection, WithHeadingRow
|
|
|
+{
|
|
|
+ use ImportValidatorHelper;
|
|
|
+ public function collection(Collection $collection)
|
|
|
+ {
|
|
|
+ $group=request()->group;
|
|
|
+ $namingRule = NamingRule::query()->where("company_id", Auth::user()->company_id)->where("id", $group)->first();
|
|
|
+ if (! $namingRule) {
|
|
|
+ return $this->forbidden('Naming rule does not exist');
|
|
|
+ }
|
|
|
+ $requestRule = new CreateOrUpdateRequest();
|
|
|
+
|
|
|
+ $this->validatorByCollection($collection,$requestRule->importRules());
|
|
|
+
|
|
|
+ $options=[];
|
|
|
+ $inputs=[];
|
|
|
+ $keys=[];
|
|
|
+
|
|
|
+ //拿到每个key和对应的type去重key
|
|
|
+ foreach ($collection as $item){
|
|
|
+ $key = $item->get('key');
|
|
|
+ $type = $item->get('type');
|
|
|
+
|
|
|
+ if (!isset($seenKeys[$key])) {
|
|
|
+ $keys[$key] = $type;
|
|
|
+ $seenKeys[$key] = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($keys as $key=>$value){
|
|
|
+ //封装下拉列表或者文本
|
|
|
+ foreach ($collection as $item){
|
|
|
+ if($key == $item->get('key')){
|
|
|
+ $type=$item->get('type');
|
|
|
+ $langData=[
|
|
|
+ 'en'=>$item->get('custom_key'),
|
|
|
+ 'zh'=>null,
|
|
|
+ ];
|
|
|
+ $valueData = $item->get('custom_value');
|
|
|
+ $remarkData = $item->get('remark');
|
|
|
+
|
|
|
+ if($type==1){
|
|
|
+ $inputs[] =new CustomOptions($langData,$valueData,$remarkData);
|
|
|
+ }else{
|
|
|
+ $options[] =new CustomOptions($langData,$valueData,$remarkData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $formData=[
|
|
|
+ 'global'=>0,
|
|
|
+ 'group'=>$group,
|
|
|
+ 'key'=>$key,
|
|
|
+ 'type'=>$value,
|
|
|
+ 'options'=>empty($options)?null:$options,
|
|
|
+ 'required'=>0,
|
|
|
+ 'inputs'=>empty($inputs)?null:$inputs,
|
|
|
+ ];
|
|
|
+ CustomField::query()->updateOrCreate([
|
|
|
+ 'group' => $group,
|
|
|
+ 'key' => $key,
|
|
|
+ ], $formData);
|
|
|
+ $options=[];
|
|
|
+ $inputs=[];
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|