BossImportLevelFailLog.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace addons\Boss\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 "addons_boss_import_level_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 BossImportLevelFailLog extends BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_boss_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. // 因为日志后台传递的是user_id 所以这里要删除mobile字段。并给user_id字段默认0
  77. unset($param['mobile']);
  78. $param['user_id'] = 0;
  79. $fail_arr[] = $param;
  80. return [$fail_arr, $fail_counts];
  81. }
  82. /**
  83. * 保存数据
  84. * @param array $params
  85. * @param $log_id
  86. * @return bool
  87. */
  88. public static function setData(array $params, $log_id)
  89. {
  90. try {
  91. foreach ($params as $param) {
  92. $model = new self();
  93. $model->loadDefaultValues();
  94. $param['log_id'] = $log_id;
  95. if (!$model->load($param, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  96. }
  97. return true;
  98. } catch (Exception $e) {
  99. self::$static_error = $e->getMessage();
  100. return false;
  101. }
  102. }
  103. public static function exportFailLog($params){
  104. $csv = new CsvExport();
  105. $list = self::getList([['log_id' => $params['log_id']]]);
  106. $file_name = date('YmdHis', time());
  107. $head_list = ["序号", "用户手机号", "等级权重Id", '失败原因'];
  108. $arr = [];
  109. foreach ($list as $k => $val) {
  110. $row = [];
  111. $extra = json_decode($val['extra'], true);
  112. $row[] = $k + 1;
  113. $row[] = $extra['mobile'];
  114. $row[] = $extra['level'];
  115. $row[] = $val['reason'];
  116. $arr[] = $row;
  117. }
  118. return $csv->export($arr, $head_list, $file_name);
  119. }
  120. }