| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- namespace addons\ScoreBonus\common\models;
- use Yii;
- use common\models\base\BaseActiveRecord;
- use yii\behaviors\TimestampBehavior;
- use yii\db\Exception;
- use addons\ScoreBonus\common\enums\ScoreBonusEnums;
- use common\enums\StatusEnum;
- use common\models\common\Download;
- use common\components\export\CsvTool;
- use common\enums\DownloadEnum;
- /**
- * This is the model class for table "{{%addon_default}}".
- */
- class ScoreBonusModel extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_score_bonus}}';
- }
-
- public function rules()
- {
- return [
- [['mall_id','bonus_circle', 'status', 'created_at', 'num', 'updated_at', 'date_time', 'bonus_num', 'settle_method', 'total_weight'], 'integer'],
- [['bonus_price', 'settle_price', 'total_score'], 'number'],
- [['num_text'], 'string'],
- ];
- }
- public static function fieldsList($field = null)
- {
- $fields = [
- 'num' => '期数',
- 'bonus_num' => '分红用户数',
- 'settle_price' => '结算金额',
- 'bonus_price' => '周期分红池总额',
- 'total_score' => '周期总积分',
- 'status' => '结算状态',
- 'bonus_circle' => '结算周期',
- 'created_at' => '结算时间',
- ];
- if ($field === null) return $fields;
- $temp = [];
- foreach ($field as $val) {
- if (isset($fields[$val])) $temp[$val] = $fields[$val];
- }
- return $temp;
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2023/6/2 0002 15:53
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $params
- * @return bool
- */
- public static function setData(array $params)
- {
- try {
- $model = new self();
- $model->loadDefaultValues();
- if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- return $model;
- } catch (Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function getDataOne($params = [], $orderBy = 'id desc')
- {
- return self::find()->where($params)->orderBy($orderBy)->asArray()->one();
- }
- public static function exportHandle(array $data)
- {
- date_default_timezone_set('PRC');
- $download_id = $data['download_id'] ?? 0;
- $download = Download::findOne(['id' => $download_id]);
- if($download){
- try{
- $params = json_decode($download->params,true);
- // print_r($params);exit;
- $filename = $download->name;
- $type = $download->type;
- $mall_id = $params['mall_id'];
- $fields = $params['fields'];
- $fields_arr = self::fieldsList();
- $have_select_field_arr = [];
- // print_r($params);exit;
- foreach ($fields as $value){
- $have_select_field_arr[]=$fields_arr[$value];
- }
- $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;
- $backend_contents_model = self::find();
- $backend_contents_model->where(['mall_id' => $params['mall_id']]);
- if (isset($params['status']) && is_numeric($params['status'])) $backend_contents_model->andWhere(['status' => $params['status']]);
- if (!empty($params['start'])) $backend_contents_model->andWhere(['>=', 'created_at', strtotime($params['start'])]);
- if (!empty($params['end'])) $backend_contents_model->andWhere(['<=', 'created_at', strtotime($params['end'])]);
- if (!empty($params['num_text'])) $backend_contents_model->andWhere(['like', 'num_text', $params['num_text']]);
-
- if (!empty($params['id_str'])) {
- $id_arr = explode(',', $params['id_str']);
- $backend_contents_model->andWhere(['in', 'id', $id_arr]);
- }
- //设置排序
- $order = 'id desc';
- if (!empty($params['orderBy'])) $order = $params['orderBy'];
- $backend_contents_model->orderBy($order);
- $bonus_circle = ScoreBonusEnums::getSettleCircleMap();
- $data_list = [];
- foreach($backend_contents_model->batch() as $contents){
- foreach ($contents as $key => $value) {
- $row = [];
- foreach ($fields as $field){
- switch ($field) {
- case 'num':
- if ($value['num'] < 10) {
- $value['num'] = '0' . $value['num'];
- }
- $row[] = $value['date_time'].$value['num'].'期'??'';
- break;
- case 'bonus_num':
- $row[] = $value['bonus_num']??'0';
- break;
- case 'settle_price':
- $row[] = $value['settle_price']??'0.00';
- break;
- case 'bonus_price':
- $row[] = $value['bonus_price']??'0.00';
- break;
- case 'total_score':
- $row[] = $value['total_score']??'0.00';
- break;
- case 'status':
- if ($value['status'] == 1) {
- $row[] = '成功';
- } else {
- $row[] = '失败';
- }
- break;
- case 'bonus_circle':
- $row[] = $bonus_circle[$value['bonus_circle']];
- break;
- case 'created_at':
- $row[] = date('Y-m-d H:i:s',$value['created_at'])??'';
- break;
- }
- }
- $data_list[]=$row;
- }
- }
- $csv->data = $data_list;
- $csv->export();
- $url = $csv->upload($params['mall_id'],$csv->relative_filename);
- $download->status = 2;
- $download->url = $url;
- $download->num = $csv->line_num;
- $download->size = $csv->file_size;
- $res = $download->save();
- return true;
- }catch(Exception $e){
- $download->status = 3;
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
- }
|