OrderBatchDeliveryError.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace common\models\order;
  3. use common\components\export\CsvTool;
  4. use common\enums\DownloadEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\common\Download;
  7. use Yii;
  8. /**
  9. * This is the model class for table "{{%order_batch_delivery_error}}".
  10. *
  11. *
  12. * @property int $id ID
  13. * @property int $mall_id 商城ID
  14. * @property int $batch_delivery_id qimall_order_batch_delivery.id
  15. * @property string $order_no 订单号
  16. * @property string $express_no 物流单号
  17. * @property string $content 错误原因
  18. * @property int $status 状态【1启用 0禁用 -1删除】
  19. * @property int $created_at 创建时间
  20. * @property int $updated_at 更新时间
  21. */
  22. class OrderBatchDeliveryError extends \common\models\base\BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%order_batch_delivery_error}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'batch_delivery_id', 'status', 'created_at', 'updated_at'], 'integer'],
  38. [['content'], 'required'],
  39. [['content'], 'string'],
  40. [['order_no', 'express_no'], 'string', 'max' => 50],
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => 'ID',
  50. 'mall_id' => '商城ID',
  51. 'batch_delivery_id' => 'qimall_order_batch_delivery.id',
  52. 'order_no' => '订单号',
  53. 'express_no' => '物流单号',
  54. 'content' => '错误原因',
  55. 'status' => '状态【1启用 0禁用 -1删除】',
  56. 'created_at' => '创建时间',
  57. 'updated_at' => '更新时间',
  58. ];
  59. }
  60. /**
  61. * 保存数据
  62. * @Author: lun
  63. * @DateTime: 2021/12/3 0003 19:17
  64. * @Copyright: copyright (c) 2021 广东七件事集团
  65. * @param $params
  66. * @return bool|OrderBatchDeliveryError
  67. */
  68. public static function setData($params)
  69. {
  70. try {
  71. $mobel = new self();
  72. if (!$mobel->load($params, '') || !$mobel->save()) throw new \Exception($mobel->getErrorMessage());
  73. return $mobel;
  74. } catch (\Exception $e) {
  75. self::setStaticError($e->getMessage());
  76. return false;
  77. }
  78. }
  79. /**
  80. * 失败订单导出
  81. * @Author: lun
  82. * @DateTime: 2021/12/3 0003 19:15
  83. * @Copyright: copyright (c) 2021 广东七件事集团
  84. * @param array $data
  85. * @return bool|string
  86. */
  87. public static function exportHandle(array $data) {
  88. $download_id = $data['download_id'] ?? 0;
  89. $download = Download::findOne(['id' => $download_id]);
  90. if ($download) {
  91. try {
  92. $params = json_decode($download->params,true);
  93. $filename = $download->name;
  94. $type = $download->type;
  95. $list = self::find()->select("order_no,express_no,content")->where(['mall_id' => $params['mall_id'], 'batch_delivery_id' => $params['batch_delivery_id'], 'status' => StatusEnum::ENABLED])->asArray()->all();
  96. $have_select_field_arr = ['订单号','物流单号','失败原因'];
  97. $csv = new CsvTool();
  98. $csv->filename = $filename;
  99. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  100. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  101. $csv->header = $have_select_field_arr;
  102. foreach ($list as $item) {
  103. is_numeric($item['express_no']) && $item['express_no'] = $item['express_no'] . "\t";
  104. $rows[] = $item;
  105. }
  106. $csv->data = $rows;
  107. $csv->export();
  108. $csv->updateDown($download,$params['mall_id']);
  109. return true;
  110. } catch (\Exception $e) {
  111. $download->status = 3;
  112. $res = $download->save();
  113. self::setStaticError($e->getMessage());
  114. return $e->getMessage();
  115. }
  116. }
  117. }
  118. }