This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,206 @@
<?php
namespace Stemcell\Datacollection\Cron;
use Alekseon\CustomFormsBuilder\Model\ResourceModel\Form\Collection as FormCollection;
use Alekseon\CustomFormsBuilder\Model\ResourceModel\FormRecord\Attribute as FormRecordAttributeResourceModel;
use Alekseon\CustomFormsBuilder\Model\ResourceModel\FormRecord\Attribute\CollectionFactory as FormRecordAttributeCollectionFactory;
use Alekseon\CustomFormsBuilder\Model\ResourceModel\FormRecord\CollectionFactory as FormRecordCollectionFactory;
use Alekseon\CustomFormsEmailNotification\Model\EmailNotification;
use Alekseon\CustomFormsEmailNotification\Model\EmailNotificationFactory as EmailNotificationFactory;
use Alekseon\CustomFormsEmailNotification\Model\ResourceModel\EmailNotification as EmailNotificationResourceModel;
use Alekseon\CustomFormsEmailNotification\Model\ResourceModel\EmailNotification\CollectionFactory as EmailNotificationCollectionFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\State;
use Magento\Framework\Module\Dir;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Psr\Log\LoggerInterface;
use Stemcell\Datacollection\Helper\Email;
use Symfony\Component\Console\Command\Command;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
/**
* 通过邮件将所有询价表单数据用excel附件的形式发到指定邮箱
*/
class FormRecordsNotification
{
private array $formAttributesOptions = [];
private string $excelType = 'xlsx';
public function __construct(
private FormRecordCollectionFactory $formRecordCollectionFactory,
private FormCollection $formCollection,
private FormRecordAttributeCollectionFactory $formRecordAttributeCollectionFactory,
private FormRecordAttributeResourceModel $formRecordAttributeResourceModel,
private State $state,
private Email $email,
protected ScopeConfigInterface $scopeConfig,
private Dir $dir,
private EmailNotificationCollectionFactory $emailNotificationCollectionFactory,
private EmailNotificationResourceModel $emailNotificationResourceModel,
private EmailNotificationFactory $emailNotificationFactory,
private LoggerInterface $logger,
private TimezoneInterface $timezone
)
{
}
/**
* @param int $formId
* @return array
*/
protected function getSentFromRecordsId(int $formId)
{
$collection = $this->emailNotificationCollectionFactory->create()
->addFieldToSelect('record_id')
->addFieldToFilter('sent_status', EmailNotification::STATUS_SENT)
->addFieldToFilter('form_id', $formId);
return $collection->getColumnValues('record_id');
}
/**
* @param array $records
* @param array $headers
* @param string $title
* @return string
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
protected function generateExcelContent(array $records, array $headers, string $title)
{
$excelSaveDir = $this->dir->getDir('Stemcell_Datacollection') . '/data';
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$column = 'A';
foreach ($headers as $header) {
$sheet->setCellValue($column . '1', $header);
$column++;
}
$sheet->getStyle('A1:' . $column . '1')->getFont()->setBold(true);
$rowIndex = 2;
foreach ($records as $record) {
$column = 'A';
foreach ($headers as $header) {
$sheet->setCellValue($column . $rowIndex, $record[$header] ?? '');
$column++;
}
$rowIndex++;
}
$writer = new Xlsx($spreadsheet);
// $writer->save($excelSaveDir.DIRECTORY_SEPARATOR.$title);
ob_start();
$writer->save('php://output');
return ob_get_clean();
}
public function execute()
{
// $this->state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);
$receivers = (string)$this->scopeConfig->getValue('alekseon_custom_forms/new_entity_notification_email/to');
$receivers = array_filter(explode(',', $receivers), function (string $email) {
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
});
if (empty($receivers)) {
$this->logger->critical('邮件发送询价表单数据。如果要使用此项功能,请在管理后台配置Alekseon Extensions->Custom Forms->New Entity Notification Email的Send Email To邮件接收人');
return;
}
$forms = $this->formCollection->addAttributeToSelect('title');
$excels = [];
$sentFormRecords = [];
foreach ($forms as $form) {
$formRecordAttributes = $this->formRecordAttributeCollectionFactory->create()
->addFieldToSelect(['attribute_code', 'frontend_input', 'frontend_label'])
->addFieldToFilter('form_id', $form->getId())
->setOrder('sort_order', 'ASC');
$attributes = [];
$headers = [];
foreach ($formRecordAttributes as $recordAttribute) {
$attributes[$recordAttribute->getAttributeCode()] = $recordAttribute;
$headers[] = $recordAttribute->getFrontendLabel();
}
$headers[] = '创建时间';
$formRecords = $this->formRecordCollectionFactory->create()->addAttributeToSelect(array_keys($attributes))
->addAttributeToFilter('form_id', $form->getId());
$sentRecords = $this->getSentFromRecordsId($form->getId());
if ($sentRecords) {
$formRecords->addAttributeToFilter('entity_id', ['nin' => $sentRecords]);
}
$records = [];
foreach ($formRecords as $formRecord) {
$record = [];
foreach ($formRecord->getData() as $attribute => $value) {
if (isset($attributes[$attribute])) {
if (in_array($attributes[$attribute]->getFrontendInput(), ['multiselect', 'select'])) {
if (!isset($this->formAttributesOptions[$attribute])) {
foreach ($this->formRecordAttributeResourceModel->getAttributeOptionValues($attributes[$attribute]) as $key => $dataObject) {
/* @var $dataObject \Magento\Framework\DataObject */
$this->formAttributesOptions[$attribute][$key] = $dataObject->getLabel();
}
}
$optionsId = explode(',', $value);
$optionsLabel = [];
foreach ($optionsId as $optionId) {
if ($optionId) {
$optionsLabel[] = $this->formAttributesOptions[$attribute][$optionId];
}
}
$formRecord->setData($attributes[$attribute]->getFrontendLabel(), implode(',', $optionsLabel));
$record[$attributes[$attribute]->getFrontendLabel()] = implode(',', $optionsLabel);
} else {
$record[$attributes[$attribute]->getFrontendLabel()] = $value;
}
}
if ($attribute == 'created_at') {
$convertedDate = $this->timezone->date(new \DateTime($value));
$record['创建时间'] = $convertedDate->format('Y-m-d H:i:s');
}
}
$records[] = $record;
$sentFormRecords[] = [
'form_id' => $form->getId(),
'record_id' => $formRecord->getId(),
'notification_type' => 'email',
'sent_status' => EmailNotification::STATUS_SENT,
'sent_at' => date('Y-m-d H:i:s'),
];
}
$noData = empty($records) ? '无数据-' : '';
$title = $noData.$form->getTitle(). '-' . date('Ymd') . '.' . $this->excelType;
$excels[] = [
'title' => $title,
'content' => $this->generateExcelContent($records, $headers, $title)
];
}
$this->email->sendEmailWithAttachment(
$receivers,
'小程序询价表单-' . date('Ymd'),
$excels
);
if (!empty($sentFormRecords)) {
foreach ($sentFormRecords as $sentFormRecord) {
$this->emailNotificationResourceModel->save(
$this->emailNotificationFactory->create()->setData($sentFormRecord)
);
}
}
}
}