OrderLogisticsForm.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Date;
  5. use common\helpers\LoggerTool;
  6. use common\models\Order;
  7. /**
  8. * 订单物流表单
  9. */
  10. class OrderLogisticsForm extends Model
  11. {
  12. /**
  13. * 自动发送订单到仓储服务.
  14. * @return bool
  15. */
  16. public function taskAutoSendEmail(): bool
  17. {
  18. try {
  19. // 公司邮箱
  20. $adminEmail = \Yii::$app->params['adminEmail'];
  21. // 物流仓储邮件
  22. $logisticsEmail = \Yii::$app->params['logisticsEmail'];
  23. // 生成附件
  24. $attachFile = $this->taskAutoAssembleOrder();
  25. // 发送邮件
  26. $result = \Yii::$app->mailer->compose()
  27. ->setFrom([$adminEmail => 'elken'])
  28. ->setTo($logisticsEmail)
  29. ->setSubject('Order List')
  30. ->setTextBody('Plain text content')
  31. ->attach($attachFile)
  32. ->send();
  33. } catch(\Exception $e) {
  34. LoggerTool::info('taskAutoSendEmail Exception!' . $e->getMessage());
  35. }
  36. return true;
  37. }
  38. /**
  39. * 获取订单.
  40. */
  41. public function taskAutoAssembleOrder()
  42. {
  43. // 查询符合条件的订单
  44. $orderListData = Order::find()
  45. ->select('SN')
  46. ->where(
  47. 'STATUS = :STATUS AND CREATED_AT > :CREATED_AT',
  48. [':STATUS' => \Yii::$app->params['orderStatus']['paid']['value'], ':CREATED_AT' => strtotime('-100 days')]
  49. )
  50. ->asArray()
  51. ->limit(10)
  52. ->all();
  53. // 生成Excel
  54. if ($orderListData) {
  55. $path = __DS__ . $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  56. $this->mkdir($path);
  57. $realFile = $path . __DS__ . $this->getFileName();
  58. $_fp = fopen($realFile, 'w');
  59. foreach ($orderListData as $orderData) {
  60. fputcsv($_fp, $orderData);
  61. }
  62. fclose($_fp);
  63. return $realFile;
  64. }
  65. return false;
  66. }
  67. /**
  68. * 生成复杂的路径
  69. * @param mixed ...$params
  70. * @return string
  71. */
  72. public function pathCreator(...$params): string
  73. {
  74. $hash = md5(implode('_', $params));
  75. $first = substr($hash, 0, 3);
  76. $second = substr($hash, 3, 2);
  77. return $first . __DS__ . $second . __DS__;
  78. }
  79. /**
  80. * 获取文件名
  81. */
  82. public function getFileName($ext = '.csv'): string
  83. {
  84. return date('YmdHis', Date::nowTime()) . random_int(1000, 9999) . $ext;
  85. }
  86. /**
  87. * 获取保存的路径
  88. */
  89. public function getSavePath(): string
  90. {
  91. $savePath = $this->pathCreator(date('Ymd', Date::nowTime()));
  92. return trim($savePath, __DS__);
  93. }
  94. /**
  95. * 获取文件存储位置
  96. */
  97. public function getSaveBasePath(): string
  98. {
  99. $saveBasePath = \Yii::getAlias('@common/runtime') . __DS__ . 'excelExport' . __DS__ . \Yii::$app->params['excelLocalDir'];
  100. return trim($saveBasePath, __DS__);
  101. }
  102. /**
  103. * 创建目录(递归创建)
  104. *
  105. * @param $dir
  106. * @return false
  107. */
  108. public function mkdir($dir): bool
  109. {
  110. if (!is_dir($dir)) {
  111. $this->mkdir(dirname($dir));
  112. if (!mkdir($dir)) {
  113. return false;
  114. }
  115. @exec('chown -R centos:centos /' . $dir);
  116. @exec('chmod -R 777 /' . $dir);
  117. }
  118. return $dir;
  119. }
  120. }