AddonsHandBounsLog.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace addons\HandBouns\common\models;
  3. use common\components\export\CsvTool;
  4. use common\enums\DownloadEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\common\Download;
  7. use Yii;
  8. use yii\db\Exception;
  9. /**
  10. * This is the model class for table "qimall_addons_hand_bouns_log".
  11. *
  12. * @property int $id
  13. * @property int $mall_id 商城ID
  14. * @property int $hand_bouns_id 导入ID,qimall_addons_hand_bouns.id
  15. * @property string $mobile 手机号码
  16. * @property float $balance 余额
  17. * @property int $score 积分
  18. * @property string $commission 佣金
  19. * @property int $status 0是禁用,1是导入成功,2是导入失败
  20. * @property int $created_at 创建时间
  21. * @property int $updated_at 修改时间
  22. * @property int $export_status 导出状态
  23. * @property string $reason 失败原因
  24. */
  25. class AddonsHandBounsLog extends \common\models\base\BaseActiveRecord
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function tableName()
  31. {
  32. return '{{%addons_hand_bouns_log}}';
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['mall_id', 'hand_bouns_id', 'score','export_status', 'status','reason', 'created_at', 'updated_at'], 'integer'],
  41. [['balance'], 'number'],
  42. [['mobile'], 'string', 'max' => 11],
  43. [['commission'], 'string', 'max' => 64],
  44. ];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'id' => 'ID',
  53. 'mall_id' => '商城ID',
  54. 'hand_bouns_id' => '导入ID,qimall_addons_hand_bouns.id',
  55. 'mobile' => '手机号码',
  56. 'balance' => '余额',
  57. 'score' => '积分',
  58. 'commission' => '佣金',
  59. 'status' => '0是禁用,1是导入成功,2是导入失败',
  60. 'export_status' => '导入状态,0是正在处理中,1是导入成功,2是导入失败',
  61. 'created_at' => '创建时间',
  62. 'updated_at' => '修改时间',
  63. 'reason' => '失败的原因',
  64. ];
  65. }
  66. /**
  67. * @Author: ltm
  68. * @DateTime: 2022/3/7 0007 11:31
  69. * @Copyright: copyright (c) 2021 广东七件事集团
  70. * @param $data
  71. * @return string
  72. */
  73. public static function setData($mall_id, $params = null)
  74. {
  75. try {
  76. if (empty($params)) {
  77. $model = new self();
  78. $model->loadDefaultValues();
  79. $model->mall_id = $mall_id;
  80. $model->period_num = self::getPrevPeriodNum($mall_id);
  81. } else {
  82. $model = self::findOne(['id' => $params['id']]);
  83. }
  84. if ($params && !$model->load($params, '')) throw new Exception($model->getErrorMessage());
  85. if (!$model->save()) throw new Exception($model->getErrorMessage());
  86. return $model->id;
  87. } catch (\Exception $e) {
  88. return false;
  89. }
  90. }
  91. /**
  92. * 失败分红导出
  93. * @Author: ltm
  94. * @DateTime: 2022/3/7 0007 14:58
  95. * @Copyright: copyright (c) 2021 广东七件事集团
  96. * @param $data
  97. * @return string
  98. */
  99. public static function exportHandle(array $data) {
  100. $download_id = $data['download_id'] ?? 0;
  101. $download = Download::findOne(['id' => $download_id]);
  102. if ($download) {
  103. try {
  104. $params = json_decode($download->params,true);
  105. $filename = $download->name;
  106. $type = $download->type;
  107. $list = self::find()->select("mobile,balance,score,commission,reason")->where(['mall_id' => $params['mall_id'], 'hand_bouns_id' => $params['hand_bouns_id'], 'export_status' => $params['style']])->asArray()->all();
  108. if($params['style'] == 1){ // 成功
  109. $have_select_field_arr = ['手机号码', '余额','佣金','积分'];
  110. }else{ // 失败
  111. $have_select_field_arr = ['手机号码', '余额','佣金','积分','失败原因'];
  112. }
  113. $csv = new CsvTool();
  114. $csv->filename = $filename;
  115. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  116. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  117. $csv->header = $have_select_field_arr;
  118. foreach ($list as $item) {
  119. is_numeric($item['id']) && $item['id'] = $item['id'] . "\t";
  120. $rows[] = $item;
  121. }
  122. $csv->data = $rows;
  123. $csv->export();
  124. $csv->updateDown($download,$params['mall_id']);
  125. return true;
  126. } catch (\Exception $e) {
  127. $download->status = 3;
  128. $res = $download->save();
  129. self::setStaticError($e->getMessage());
  130. return $e->getMessage();
  131. }
  132. }
  133. }
  134. }