UserImportFailLog.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace common\models\user;
  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 $user_import_log_id 导入批次id
  15. * @property string $mobile 手机号码
  16. * @property string $reason 失败原因
  17. * @property string|null $extra 导入的内容
  18. * @property int $updated_at
  19. * @property int $created_at 添加时间
  20. */
  21. class UserImportFailLog extends BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%user_import_fail_log}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'user_import_log_id', 'updated_at', 'created_at'], 'integer'],
  37. [['extra'], 'string'],
  38. [['mobile'], 'string', 'max' => 11],
  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. 'user_import_log_id' => '导入批次id',
  51. 'mobile' => '手机号码',
  52. 'reason' => '失败原因',
  53. 'extra' => '导入的内容',
  54. 'updated_at' => 'Updated At',
  55. 'created_at' => '添加时间',
  56. ];
  57. }
  58. /**
  59. * @param array $where
  60. * @param string $select
  61. * @param array $with
  62. * @param int $is_array
  63. * @return array|User[]|\yii\db\ActiveRecord[]
  64. */
  65. public static function getList(array $where, $select = '*', array $with = [], int $is_array = 0)
  66. {
  67. $query = self::find();
  68. self::paramPack(['where' => $where, 'with' => $with], $query);
  69. return $query->select($select)->asArray($is_array)->all();
  70. }
  71. public static function packFailArr($param, $fail_arr, $fail_counts)
  72. {
  73. $fail_counts++;
  74. $param['extra'] = json_encode($param);
  75. $fail_arr[] = $param;
  76. return [$fail_arr, $fail_counts];
  77. }
  78. /**
  79. * 保存数据
  80. * @param array $params
  81. * @param $user_import_log_id
  82. * @return bool|UserImportFailLog
  83. */
  84. public static function setData(array $params, $user_import_log_id)
  85. {
  86. try {
  87. foreach ($params as $param) {
  88. $model = new self();
  89. $model->loadDefaultValues();
  90. $param['user_import_log_id'] = $user_import_log_id;
  91. if (!$model->load($param, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  92. }
  93. return true;
  94. } catch (Exception $e) {
  95. self::$static_error = $e->getMessage();
  96. return false;
  97. }
  98. }
  99. public static function exportFailLog($params){
  100. $csv = new CsvExport();
  101. $list = self::getList([['user_import_log_id' => $params['user_import_log_id']]]);
  102. $file_name = date('YmdHis', time());
  103. $head_list = ["序号", "手机号(必填)", "昵称(最多10个字必填", '推荐人手机号(匹配不到则导入失败)', '性别(男/女)', '生日(1989/08/09或1989-08-09)', '积分(限整数)', '余额(限整数)', '备注', '失败原因'];
  104. $arr = [];
  105. foreach ($list as $k => $val) {
  106. $row = [];
  107. $extra = json_decode($val['extra'], true);
  108. $row[] = $k + 1;
  109. $row[] = $extra['mobile'];
  110. $row[] = $extra['nickname'];
  111. $row[] = $extra['parent_mobile'];
  112. $row[] = $extra['sex'];
  113. $row[] = $extra['birthday'];
  114. $row[] = $extra['source'];
  115. $row[] = $extra['balance'];
  116. $row[] = $extra['remark'];
  117. $row[] = $val['reason'];
  118. $arr[] = $row;
  119. }
  120. return $csv->export($arr, $head_list, $file_name);
  121. }
  122. }