CloudStockImportLevelFailLog.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace addons\CloudStock\common\models;
  3. use common\components\CsvExport;
  4. use common\enums\StatusEnum;
  5. use common\models\base\BaseActiveRecord;
  6. use PHPUnit\Util\Exception;
  7. use Yii;
  8. /**
  9. * This is the model class for table "qimall_user_import_fail_log".
  10. *
  11. *
  12. * @property int $id
  13. * @property int $mall_id 商城ID
  14. * @property int $log_id 导入批次id
  15. * @property int $user_id 用户id
  16. * @property int $level 等级id
  17. * @property string $reason 失败原因
  18. * @property string|null $extra 导入的内容
  19. * @property int $updated_at
  20. * @property int $created_at 添加时间
  21. */
  22. class CloudStockImportLevelFailLog extends BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_cloud_stock_import_level_fail_log}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'log_id', 'user_id','level','updated_at', 'created_at'], 'integer'],
  38. [['extra'], 'string'],
  39. [['reason'], 'string', 'max' => 255],
  40. ];
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function attributeLabels()
  46. {
  47. return [
  48. 'id' => 'ID',
  49. 'mall_id' => '商城ID',
  50. 'log_id' => '导入批次id',
  51. 'user_id' => '用户id',
  52. 'level' => '用户等级id',
  53. 'reason' => '失败原因',
  54. 'extra' => '导入的内容',
  55. 'updated_at' => 'Updated At',
  56. 'created_at' => '添加时间',
  57. ];
  58. }
  59. /**
  60. * @param array $where
  61. * @param string $select
  62. * @param array $with
  63. * @param int $is_array
  64. * @return array
  65. */
  66. public static function getList(array $where, $select = '*', array $with = [], int $is_array = 0)
  67. {
  68. $query = self::find();
  69. self::paramPack(['where' => $where, 'with' => $with], $query);
  70. return $query->select($select)->asArray($is_array)->all();
  71. }
  72. public static function packFailArr($param, $fail_arr, $fail_counts)
  73. {
  74. $fail_counts++;
  75. $param['extra'] = json_encode($param);
  76. $fail_arr[] = $param;
  77. return [$fail_arr, $fail_counts];
  78. }
  79. /**
  80. * 保存数据
  81. * @param array $params
  82. * @param $log_id
  83. * @return bool
  84. */
  85. public static function setData(array $params, $log_id)
  86. {
  87. try {
  88. foreach ($params as $param) {
  89. $model = new self();
  90. $model->loadDefaultValues();
  91. $param['log_id'] = $log_id;
  92. if (!$model->load($param, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  93. }
  94. return true;
  95. } catch (Exception $e) {
  96. self::$static_error = $e->getMessage();
  97. return false;
  98. }
  99. }
  100. public static function exportFailLog($params){
  101. $csv = new CsvExport();
  102. $list = self::getList([['log_id' => $params['log_id']]]);
  103. $file_name = date('YmdHis', time());
  104. $head_list = ["序号", "用户id", "等级权重Id", '失败原因'];
  105. $arr = [];
  106. foreach ($list as $k => $val) {
  107. $row = [];
  108. $extra = json_decode($val['extra'], true);
  109. $row[] = $k + 1;
  110. $row[] = $extra['user_id'];
  111. $row[] = $extra['level'];
  112. $row[] = $val['reason'];
  113. $arr[] = $row;
  114. }
  115. return $csv->export($arr, $head_list, $file_name);
  116. }
  117. }