You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Exports;
use App\Models\CustomForm;
use App\Models\CustomFormField;
use App\Models\ParameterDetail;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
class BaseExport implements FromCollection
{
public $fields;
public $data;
public $tableName;
public function __construct($exportFields, $data, $tableName)
{
$this->fields = $exportFields;
$this->data = $data;
$this->tableName = $tableName;
}
//数组转集合
public function collection()
{
return new Collection($this->createData());
}
//业务代码
public function createData()
{
// 获取表字段备注
$rowTableFieldByComment = (new CustomFormField)->getRowTableFieldsByComment($this->tableName);
// 筛选需要导出的字段设置
$exportFieldsByComment = [];
foreach ($rowTableFieldByComment as $key => $value) {
if (in_array($key, $this->fields)) {
$exportFieldsByComment[$key] = $value;
}
}
// 表头
$header = array_values($exportFieldsByComment);
$flip = array_flip($this->fields);
$newList = [];
foreach ($this->data as $info) {
// 取原始查询数据中指定的几个字段数据
$temp = array_intersect_key($info, $flip);
$newList[] = $this->intToSting($temp);
}
array_unshift($newList, $header); //插入表头
return $newList;
}
/**
* 枚举型数据转换
*/
public function intToSting($fields)
{
$model = CustomForm::tableToModel($this->tableName);
// 模型中定义$intToString静态方法实现枚举型的int转字符串
$array = $model::$intToString;
foreach ($fields as $key => &$item) {
if (isset($array[$key])) {
if (is_array($array[$key])) {
// 枚举
$item = $array[$key][$item];
}
if (is_int($array[$key])) {
// 数据字典
$item = ParameterDetail::where('id', $item)->value('value');
}
}
}
return $fields;
}
}