| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- namespace addons\HandBouns\common\models;
- use common\components\export\CsvTool;
- use common\enums\DownloadEnum;
- use common\enums\StatusEnum;
- use common\models\common\Download;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "qimall_addons_hand_bouns_log".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $hand_bouns_id 导入ID,qimall_addons_hand_bouns.id
- * @property string $mobile 手机号码
- * @property float $balance 余额
- * @property int $score 积分
- * @property string $commission 佣金
- * @property int $status 0是禁用,1是导入成功,2是导入失败
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property int $export_status 导出状态
- * @property string $reason 失败原因
- */
- class AddonsHandBounsLog extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_hand_bouns_log}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'hand_bouns_id', 'score','export_status', 'status','reason', 'created_at', 'updated_at'], 'integer'],
- [['balance'], 'number'],
- [['mobile'], 'string', 'max' => 11],
- [['commission'], 'string', 'max' => 64],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'hand_bouns_id' => '导入ID,qimall_addons_hand_bouns.id',
- 'mobile' => '手机号码',
- 'balance' => '余额',
- 'score' => '积分',
- 'commission' => '佣金',
- 'status' => '0是禁用,1是导入成功,2是导入失败',
- 'export_status' => '导入状态,0是正在处理中,1是导入成功,2是导入失败',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- 'reason' => '失败的原因',
- ];
- }
- /**
- * @Author: ltm
- * @DateTime: 2022/3/7 0007 11:31
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return string
- */
- public static function setData($mall_id, $params = null)
- {
- try {
- if (empty($params)) {
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $mall_id;
- $model->period_num = self::getPrevPeriodNum($mall_id);
- } else {
- $model = self::findOne(['id' => $params['id']]);
- }
- if ($params && !$model->load($params, '')) throw new Exception($model->getErrorMessage());
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- return $model->id;
- } catch (\Exception $e) {
- return false;
- }
- }
- /**
- * 失败分红导出
- * @Author: ltm
- * @DateTime: 2022/3/7 0007 14:58
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return string
- */
- public static function exportHandle(array $data) {
- $download_id = $data['download_id'] ?? 0;
- $download = Download::findOne(['id' => $download_id]);
- if ($download) {
- try {
- $params = json_decode($download->params,true);
- $filename = $download->name;
- $type = $download->type;
- $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();
- if($params['style'] == 1){ // 成功
- $have_select_field_arr = ['手机号码', '余额','佣金','积分'];
- }else{ // 失败
- $have_select_field_arr = ['手机号码', '余额','佣金','积分','失败原因'];
- }
- $csv = new CsvTool();
- $csv->filename = $filename;
- $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
- $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
- $csv->header = $have_select_field_arr;
- foreach ($list as $item) {
- is_numeric($item['id']) && $item['id'] = $item['id'] . "\t";
- $rows[] = $item;
- }
- $csv->data = $rows;
- $csv->export();
- $csv->updateDown($download,$params['mall_id']);
- return true;
- } catch (\Exception $e) {
- $download->status = 3;
- $res = $download->save();
- self::setStaticError($e->getMessage());
- return $e->getMessage();
- }
- }
- }
- }
|