| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- namespace addons\Seed\common\models;
- use common\components\export\CsvTool;
- use common\enums\DownloadEnum;
- use common\enums\StatusEnum;
- use common\models\common\Addons;
- use common\models\common\Download;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- use Yii;
- use yii\db\Exception;
- use yii\db\Expression;
- /**
- * This is the model class for table "{{%addons_seed_user}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $user_id 用户ID
- * @property int $total_seed_num 种子金总数量
- * @property float $total_seed_cost 种子金总价值,单位:元
- * @property int $collect_seed_num 已收取种子金总数量
- * @property int $refund_seed_num 退款种子金总数量
- * @property float $total_change_money 累计转换金额,单位:元
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class SeedUser extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_seed_user}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'total_seed_num', 'collect_seed_num', 'refund_seed_num', 'status', 'created_at', 'updated_at'], 'integer'],
- [['total_seed_cost', 'total_change_money'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'user_id' => '用户ID',
- 'total_seed_num' => '种子金总数量',
- 'total_seed_cost' => '种子金总价值,单位:元',
- 'collect_seed_num' => '已收取种子金总数量',
- 'refund_seed_num' => '退款种子金总数量',
- 'total_change_money' => '累计转换金额,单位:元',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- }
- /**
- * 关联用户表
- * @Author: lun
- * @DateTime: 2022/7/28 0028 10:07
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,username,avatar_url');
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2022/7/27 0027 11:44
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $user_id
- * @param $params
- * @return array|bool
- */
- public static function setData($mall_id, $user_id, $params)
- {
- try {
- $model = self::findOne(['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $mall_id;
- $model->user_id = $user_id;
- $model->loadDefaultValues();
- }
- // 循环赋值
- foreach ($model as $key => $value) {
- if (isset($params[$key]) && !empty($params[$key])) $model->$key += $params[$key];
- }
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- return $model->toArray();
- } catch (\Exception $e) {
- self::setStaticError('用户种子金:'.$e->getMessage());
- return false;
- }
- }
- /**
- * 导出字段
- * @Author: lun
- * @DateTime: 2022/8/5 0005 11:23
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return []
- */
- public static function fieldsList()
- {
- $fields = [
- 'user_id' => '用户ID',
- 'nickname' => '用户昵称',
- 'mobile' => '用户手机',
- 'seed_num' => '现有种子金总数量',
- 'total_seed_num' => '累计种子金总数量',
- 'total_seed_cost' => '种子金总价值',
- 'collect_seed_num' => '已收取种子金',
- 'refund_seed_num' => '退款种子金数量',
- 'total_change_money' => '累计转换金额',
- 'created_at' => '创建时间',
- ];
- return $fields;
- }
- /**
- * 导出处理
- * @Author: lun
- * @DateTime: 2022/8/5 0005 11:23
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $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;
- $seel_user_model = self::find();
- $seel_user_model->where(['mall_id' => $params['mall_id']]);
- !empty($params['user_id']) && $seel_user_model->where(['user_id' => $params['user_id']]);
- if (!empty($params['mobile'])){
- $user = User::find()->where(['like','mobile',$params['mobile'].'%',false])->all();
- $where[] = ['in', 'user_id', array_column($user,'id')];
- $seel_user_model->andWhere(['in', 'user_id', array_column($user,'id')]);
- }
- $seel_user_model->andWhere(['=','status',StatusEnum::ENABLED]);
- $seel_user_model->with(['user' => function($query){
- $query->select('id,nickname,username,mobile');
- }]);
- $fields = $params['fields'];
- $fields_arr = self::fieldsList();
- $have_select_field_arr = [];
- 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;
- foreach ($seel_user_model->batch() as $seel_users) {
- $csv->data = array_map(function($seel_user)use($fields){
- $row = [];
- foreach ($fields as $field){
- switch (true){
- case ($field == 'nickname'):
- $row[] = !empty($seel_user[$field])?date('Y-m-d H:i:s',$seel_user[$field]):'';
- break;
- case ($field == 'mobile'):
- $row[] = !empty($seel_user['order'][$field])?date('Y-m-d H:i:s',$seel_user['order'][$field]):'';
- break;
- case ($field == 'seed_num'):
- $row[] = $seel_user['total_seed_num'] - $seel_user['collect_seed_num'] - $seel_user['refund_seed_num'];
- break;
- default:
- $row[] = !empty($seel_user[$field])?$seel_user[$field]:'';
- break;
- }
- }
- return $row;
- }, $seel_users);
- $csv->export();
- }
- $csv->updateDown($download,$params['mall_id']);
- return true;
- } catch (\Exception $e) {echo $e->getMessage();
- $download->status = 3;
- $download->save();
- self::setStaticError($e->getMessage());
- return $e->getMessage();
- }
- }
- }
- /**
- * 修改统计数据
- * @Author: lun
- * @DateTime: 2022/8/30 0030 10:37
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $user_id
- * @param $params
- * @throws Exception
- */
- public static function saveStatistics($user_id, $params)
- {
- $update_data['updated_at'] = time();
- foreach ($params as $key => $value) {
- $symbol = $value >= 0 ? '+' : '-';
- $update_data[$key] = new Expression($key.$symbol.abs($value));
- }
- $res = self::updateAll($update_data, ['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
- if (!$res) throw new Exception('修改统计数据失败');
- }
- public static function getAddons(array $params){
- $addons_name = $params['addons_name'];
- $is_install = MallAddons::isInstall($params['mall_id'], $addons_name);
- if($is_install){
- $detail = Addons::getOne([['name' =>$addons_name], ['is_online' => 1]], true,);
- return [
- 'addons_name'=>$addons_name,
- 'title'=>$detail['title'],
- ];
- }
- return [];
- }
- }
|