CloudStockImportStockFailLog.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "addons_cloud_stock_import_stock_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 $goods_id 商品id
  17. * @property int $num 数量
  18. * @property string $reason 失败原因
  19. * @property string|null $extra 导入的内容
  20. * @property int $updated_at
  21. * @property int $created_at 添加时间
  22. */
  23. class CloudStockImportStockFailLog extends BaseActiveRecord
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function tableName()
  29. {
  30. return '{{%addons_cloud_stock_import_stock_fail_log}}';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['mall_id', 'log_id', 'user_id','goods_id','num','updated_at', 'created_at'], 'integer'],
  39. [['extra'], 'string'],
  40. [['reason'], 'string', 'max' => 255],
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => 'ID',
  50. 'mall_id' => '商城ID',
  51. 'log_id' => '导入批次id',
  52. 'user_id' => '用户id',
  53. 'goods_id' => '商品id',
  54. 'num' => '数量',
  55. 'reason' => '失败原因',
  56. 'extra' => '导入的内容',
  57. 'updated_at' => 'Updated At',
  58. 'created_at' => '添加时间',
  59. ];
  60. }
  61. /**
  62. * @param array $where
  63. * @param string $select
  64. * @param array $with
  65. * @param int $is_array
  66. * @return array
  67. */
  68. public static function getList(array $where, $select = '*', array $with = [], int $is_array = 0)
  69. {
  70. $query = self::find();
  71. self::paramPack(['where' => $where, 'with' => $with], $query);
  72. return $query->select($select)->asArray($is_array)->all();
  73. }
  74. public static function packFailArr($param, $fail_arr, $fail_counts)
  75. {
  76. $fail_counts++;
  77. $param['extra'] = json_encode($param);
  78. $fail_arr[] = $param;
  79. return [$fail_arr, $fail_counts];
  80. }
  81. /**
  82. * 保存数据
  83. * @param array $params
  84. * @param $log_id
  85. * @return bool
  86. */
  87. public static function setData(array $params, $log_id)
  88. {
  89. try {
  90. foreach ($params as $param) {
  91. $model = new self();
  92. $model->loadDefaultValues();
  93. $param['log_id'] = $log_id;
  94. if (!$model->load($param, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  95. }
  96. return true;
  97. } catch (Exception $e) {
  98. self::$static_error = $e->getMessage();
  99. return false;
  100. }
  101. }
  102. public static function exportFailLog($params){
  103. $csv = new CsvExport();
  104. $list = self::getList([['log_id' => $params['log_id']]]);
  105. $file_name = date('YmdHis', time());
  106. $head_list = ["序号", "用户id", "商品id","数量",'失败原因'];
  107. $arr = [];
  108. foreach ($list as $k => $val) {
  109. $row = [];
  110. $extra = json_decode($val['extra'], true);
  111. $row[] = $k + 1;
  112. $row[] = $extra['user_id'];
  113. $row[] = $extra['goods_id'];
  114. $row[] = $extra['num'];
  115. $row[] = $val['reason'];
  116. $arr[] = $row;
  117. }
  118. return $csv->export($arr, $head_list, $file_name);
  119. }
  120. }