OrderLogisticsForm.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. return false;
  36. }
  37. return true;
  38. }
  39. /**
  40. * 获取订单.
  41. */
  42. public function taskAutoAssembleOrder()
  43. {
  44. // 查询符合条件的订单
  45. $orderListData = Order::find()
  46. ->select('SN')
  47. ->where('STATUS = :STATUS AND CREATED_AT > :CREATED_AT', [':STATUS' => \Yii::$app->params['orderStatus']['paid']['value'], ':CREATED_AT' => strtotime('-100 days')])
  48. ->asArray()
  49. ->limit(500)
  50. ->all();
  51. // 生成Excel
  52. if ($orderListData) {
  53. $path = __DS__ . $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  54. $this->mkdir($path);
  55. $realFile = $path . __DS__ . $this->getFileName();
  56. $_fp = fopen($realFile, 'w');
  57. foreach ($orderListData as $orderData) {
  58. fputcsv($_fp, $orderData);
  59. }
  60. fclose($_fp);
  61. return $realFile;
  62. }
  63. return false;
  64. }
  65. /**
  66. * 生成路径
  67. * @param mixed ...$params
  68. * @return string
  69. */
  70. public function pathCreator(...$params): string
  71. {
  72. $hash = md5(implode('_', $params));
  73. $first = substr($hash, 0, 3);
  74. $second = substr($hash, 3, 2);
  75. return $first . __DS__ . $second . __DS__;
  76. }
  77. /**
  78. * 生成文件名
  79. */
  80. public function getFileName($ext = '.csv'): string
  81. {
  82. return date('YmdHis', Date::nowTime()) . random_int(1000, 9999) . $ext;
  83. }
  84. /**
  85. * 获取保存的路径
  86. */
  87. public function getSavePath(): string
  88. {
  89. $savePath = $this->pathCreator(date('Ymd', Date::nowTime()));
  90. return trim($savePath, __DS__);
  91. }
  92. /**
  93. * 获取文件存储位置
  94. */
  95. public function getSaveBasePath(): string
  96. {
  97. $saveBasePath = \Yii::getAlias('@common/runtime') . __DS__ . 'excelExport' . __DS__ . \Yii::$app->params['excelLocalDir'];
  98. return trim($saveBasePath, __DS__);
  99. }
  100. /**
  101. * 创建目录(递归创建)
  102. *
  103. * @param $dir
  104. * @return false
  105. */
  106. public function mkdir($dir): bool
  107. {
  108. if (!is_dir($dir)) {
  109. $this->mkdir(dirname($dir));
  110. if (!mkdir($dir)) {
  111. return false;
  112. }
  113. @exec('chown -R centos:centos /' . $dir);
  114. @exec('chmod -R 777 /' . $dir);
  115. }
  116. return $dir;
  117. }
  118. }