| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php
- namespace addons\BusinessBonus\common\models;
- use addons\BusinessBonus\common\enums\BusinessBonusEnums;
- use addons\Mch\common\models\Mch;
- use addons\Store\common\models\Store;
- use common\components\export\CsvTool;
- use common\enums\DownloadEnum;
- use common\enums\StatusEnum;
- use common\models\common\Download;
- use common\models\user\User;
- use Yii;
- use Exception;
- /**
- * This is the model class for table "{{%addons_business_bonus_store}}".
- *
- * @property int $id
- * @property int|null $mall_id 商城ID
- * @property int|null $store_id 门店、多商户的ID
- * @property int|null $store_type 类型:1门店,2多商户
- * @property int|null $store_user_id 店主、商户user_id
- * @property int|null $business_user_id 招商员user_id
- * @property float|null $history_income 累计收入
- * @property float|null $history_earn 累计利润
- * @property int|null $status 状态:-1删除,0禁用,1启用
- * @property int|null $created_at 创建时间
- * @property int|null $updated_at 更新时间
- */
- class BusinessBonusStore extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_business_bonus_store}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'store_id', 'store_type', 'store_user_id', 'business_user_id', 'status', 'store_created_at', 'created_at', 'updated_at'], 'integer'],
- [['history_income', 'history_earn'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'store_id' => '门店、多商户的ID',
- 'store_type' => '类型:1门店,2多商户',
- 'store_user_id' => '店主、商户user_id',
- 'business_user_id' => '招商员user_id',
- 'history_income' => '累计收入',
- 'history_earn' => '累计利润',
- 'status' => '状态:-1删除,0禁用,1启用',
- 'store_created_at' => '门店创建时间',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- /**
- * 数据设置
- */
- public static function setData(array $params)
- {
- try{
- if (!isset($params['mall_id']) || empty($params['mall_id'])) throw new Exception('缺少商城ID');
- if (isset($params['store_id']) && !empty($params['store_id'])) {
- $model = self::findOne(['mall_id' => $params['mall_id'], 'store_id' => $params['store_id'], 'store_type' => $params['store_type'], 'status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]);
- if ($model) {
- if (isset($params['history_income'])) {
- $model->history_income += $params['history_income'];
- unset($params['history_income']);
- }
- if (isset($params['history_earn'])) {
- $model->history_earn += $params['history_earn'];
- unset($params['history_earn']);
- }
- }
- }
- if (!isset($model) || empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $params['mall_id'];
- $model->status = StatusEnum::ENABLED;
- }
- if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
- return $model;
- }catch(Exception $e){
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 门店店主
- * @return \yii\db\ActiveQuery
- */
- public function getStoreUser()
- {
- return $this->hasOne(User::class, ['id' => 'store_user_id'])->select('id,mobile,nickname,avatar_url,parent_id,username');
- }
- /**
- * 招商员
- * @return \yii\db\ActiveQuery
- */
- public function getBusinessUser()
- {
- return $this->hasOne(User::class, ['id' => 'business_user_id'])->select('id,mobile,nickname,avatar_url,parent_id,username');
- }
- /**
- * 处理门店列表
- */
- public static function dealStoreList(array $list, $mall_id)
- {
- $store_ids = $mch_ids = [];
- foreach ($list as $item) {
- if ($item['store_type'] == BusinessBonusEnums::STORE_TYPE_STORE) {
- $store_ids[] = $item['store_id'];
- } else {
- $mch_ids[] = $item['store_id'];
- }
- }
- $store_array = $mch_array = [];
- if ($store_ids) {
- $store_list = Store::find()
- ->select('id,user_id,store_name,expire_time,user_nums,created_at,province_id,city_id,district_id,address')
- ->andWhere(['>=', 'status', StatusEnum::DISABLED])
- ->andWhere(['in', 'id', $store_ids])
- ->andWhere(['=', 'mall_id', $mall_id])
- ->asArray()
- ->all();
- $store_array = array_column($store_list, null, 'id');
- }
- if ($mch_ids) {
- $mch_list = Mch::find()
- ->select('id,user_id,store_name,expire_time,user_nums,created_at,province_id,city_id,district_id,address')
- ->andWhere(['>=', 'status', StatusEnum::DISABLED])
- ->andWhere(['in', 'id', $mch_ids])
- ->andWhere(['=', 'mall_id', $mall_id])
- ->asArray()
- ->all();
- $mch_array = array_column($mch_list, null, 'id');
- }
- foreach ($list as $key => $val) {
- if ($val['store_type'] == BusinessBonusEnums::STORE_TYPE_STORE) {
- $val['store_info'] = $store_array[$val['store_id']] ?? [];
- } else if ($val['store_type'] == BusinessBonusEnums::STORE_TYPE_MCH) {
- $val['store_info'] = $mch_array[$val['store_id']] ?? [];
- }
- $list[$key] = $val;
- }
- return $list;
- }
- /**
- * 可导出的字段
- * @param null $field
- * @return array
- */
- public static function fieldsList($field = null, $store_type)
- {
- if ($store_type == BusinessBonusEnums::FROM_TYPE_STORE) {
- $store_name = '门店';
- $store_user_nums = '门店会员总数';
- } else {
- $store_name = '商户';
- $store_user_nums = '商户会员总数';
- }
- $fields = [
- 'id' => '序号',
- 'store_name' => $store_name,
- 'store_user_id' => '店长ID',
- 'store_user_nickname' => '店长昵称',
- 'store_user_mobile' => '店长手机号',
- 'business_user_id' => '招商员ID',
- 'business_user_nickname' => '招商员昵称',
- 'business_user_mobile' => '招商员手机号',
- 'history_income' => '累计收入',
- 'history_earn' => '累计利润',
- 'store_user_nums' => $store_user_nums,
- 'expire_time' => '有效期',
- 'store_created_at' => '创建时间',
- ];
- if ($field === null) return $fields;
- $temp = [];
- foreach ($field as $val) {
- $temp[$val] = $fields[$val] ?? $val;
- }
- return $temp;
- }
- /**
- * 导出处理手柄
- */
- 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);
- $condition = $params['condition'];
- $list = self::lists($condition, true);
- $list = self::dealStoreList($list, $params['mall_id']);
- $filename = $download->name;
- $type = $download->type;
- $fields = $params['fields'];
- $fields_arr = self::fieldsList(null, $params['store_type']);
- $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;
- $csv_data = [];
- foreach ($list as $data) {
- $row = [];
- foreach ($fields as $field) {
- switch ($field) {
- case 'store_name':
- $row[] = $data['store_info']['store_name'] ?? '';
- break;
- case 'store_user_mobile':
- $row[] = $data['storeUser']['mobile'] ?? '';
- break;
- case 'store_user_nickname':
- $row[] = $data['storeUser']['nickname'] ?? '';
- break;
- case 'business_user_nickname':
- $row[] = $data['businessUser']['nickname'] ?? '';
- break;
- case 'business_user_mobile':
- $row[] = $data['businessUser']['mobile'] ?? '';
- break;
- case 'store_user_nums':
- $row[] = $data['store_info']['user_nums'] ?? '';
- break;
- case 'expire_time':
- $time = $data['store_info']['expire_time'] ?? '';
- $row[] = $time ? date('Y-m-d H:i:s', $time) : '';
- break;
- case 'store_created_at':
- $time = $data['store_created_at'] ?? '';
- $row[] = $time ? date('Y-m-d H:i:s', $time) : '';
- break;
- default:
- $row[] = $data[$field] ?? '';
- break;
- }
- }
- $csv_data[] = $row;
- }
- $csv->data = $csv_data;
- $csv->export();
- $csv->updateDown($download, $params['mall_id']);
- return true;
- } catch (Exception $e) {
- $download->status = 3;
- $download->save();
- self::setStaticError($e->getMessage());
- return $e->getMessage();
- }
- }
- }
- }
|