| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace common\models\forms;
- use common\components\Model;
- use common\helpers\Date;
- use common\helpers\LoggerTool;
- use common\models\Order;
- /**
- * 订单物流表单
- */
- class OrderLogisticsForm extends Model
- {
- /**
- * 自动发送订单到仓储服务.
- * @return bool
- */
- public function taskAutoSendEmail(): bool
- {
- try {
- // 公司邮箱
- $adminEmail = \Yii::$app->params['adminEmail'];
- // 物流仓储邮件
- $logisticsEmail = \Yii::$app->params['logisticsEmail'];
- // 生成附件
- $attachFile = $this->taskAutoAssembleOrder();
- // 发送邮件
- $result = \Yii::$app->mailer->compose()
- ->setFrom([$adminEmail => 'elken'])
- ->setTo($logisticsEmail)
- ->setSubject('Order List')
- ->setTextBody('Plain text content')
- ->attach($attachFile)
- ->send();
- } catch(\Exception $e) {
- LoggerTool::info('taskAutoSendEmail Exception!' . $e->getMessage());
- return false;
- }
- return true;
- }
- /**
- * 获取订单.
- */
- public function taskAutoAssembleOrder()
- {
- // 查询符合条件的订单
- $orderListData = Order::find()
- ->select('SN')
- ->where('STATUS = :STATUS AND CREATED_AT > :CREATED_AT', [':STATUS' => \Yii::$app->params['orderStatus']['paid']['value'], ':CREATED_AT' => strtotime('-100 days')])
- ->asArray()
- ->limit(500)
- ->all();
- // 生成Excel
- if ($orderListData) {
- $path = __DS__ . $this->getSaveBasePath() . __DS__ . $this->getSavePath();
- $this->mkdir($path);
- $realFile = $path . __DS__ . $this->getFileName();
- $_fp = fopen($realFile, 'w');
- foreach ($orderListData as $orderData) {
- fputcsv($_fp, $orderData);
- }
- fclose($_fp);
- return $realFile;
- }
- return false;
- }
- /**
- * 生成路径
- * @param mixed ...$params
- * @return string
- */
- public function pathCreator(...$params): string
- {
- $hash = md5(implode('_', $params));
- $first = substr($hash, 0, 3);
- $second = substr($hash, 3, 2);
- return $first . __DS__ . $second . __DS__;
- }
- /**
- * 生成文件名
- */
- public function getFileName($ext = '.csv'): string
- {
- return date('YmdHis', Date::nowTime()) . random_int(1000, 9999) . $ext;
- }
- /**
- * 获取保存的路径
- */
- public function getSavePath(): string
- {
- $savePath = $this->pathCreator(date('Ymd', Date::nowTime()));
- return trim($savePath, __DS__);
- }
- /**
- * 获取文件存储位置
- */
- public function getSaveBasePath(): string
- {
- $saveBasePath = \Yii::getAlias('@common/runtime') . __DS__ . 'excelExport' . __DS__ . \Yii::$app->params['excelLocalDir'];
- return trim($saveBasePath, __DS__);
- }
- /**
- * 创建目录(递归创建)
- *
- * @param $dir
- * @return false
- */
- public function mkdir($dir): bool
- {
- if (!is_dir($dir)) {
- $this->mkdir(dirname($dir));
- if (!mkdir($dir)) {
- return false;
- }
- @exec('chown -R centos:centos /' . $dir);
- @exec('chmod -R 777 /' . $dir);
- }
- return $dir;
- }
- }
|