CloudStockOrderBatchDeliveryError.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace addons\CloudStock\common\models;
  3. use common\components\export\CsvTool;
  4. use common\enums\DownloadEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\base\BaseActiveRecord;
  7. use common\models\common\Download;
  8. use Yii;
  9. /**
  10. * This is the model class for table "qimall_addons_cloud_stock_order_batch_delivery_error".
  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 CloudStockOrderBatchDeliveryError extends BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return 'qimall_addons_cloud_stock_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' => 'Mall ID',
  51. 'batch_delivery_id' => 'Batch Delivery ID',
  52. 'order_no' => 'Order No',
  53. 'express_no' => 'Express No',
  54. 'content' => 'Content',
  55. 'status' => 'Status',
  56. 'created_at' => 'Created At',
  57. 'updated_at' => 'Updated At',
  58. ];
  59. }
  60. public static function setData($params)
  61. {
  62. try {
  63. $mobel = new self();
  64. if (!$mobel->load($params, '') || !$mobel->save()) throw new \Exception($mobel->getErrorMessage());
  65. return $mobel;
  66. } catch (\Exception $e) {
  67. self::setStaticError($e->getMessage());
  68. return false;
  69. }
  70. }
  71. public static function exportHandle(array $data) {
  72. $download_id = $data['download_id'] ?? 0;
  73. $download = Download::findOne(['id' => $download_id]);
  74. if ($download) {
  75. try {
  76. $params = json_decode($download->params,true);
  77. $filename = $download->name;
  78. $type = $download->type;
  79. $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();
  80. $have_select_field_arr = ['订单号','物流单号','失败原因'];
  81. $csv = new CsvTool();
  82. $csv->filename = $filename;
  83. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  84. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  85. $csv->header = $have_select_field_arr;
  86. foreach ($list as $item) {
  87. is_numeric($item['express_no']) && $item['express_no'] = $item['express_no'] . "\t";
  88. $rows[] = $item;
  89. }
  90. $csv->data = $rows;
  91. $csv->export();
  92. $csv->updateDown($download,$params['mall_id']);
  93. return true;
  94. } catch (\Exception $e) {
  95. $download->status = 3;
  96. $res = $download->save();
  97. self::setStaticError($e->getMessage());
  98. return $e->getMessage();
  99. }
  100. }
  101. }
  102. }