| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579 |
- <?php
- namespace addons\Area\common\models;
- use addons\Area\common\enums\AreaEnum;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\helpers\FormatHelper;
- use common\models\base\BaseActiveRecord;
- use common\models\common\Addons;
- use common\models\common\Region;
- use common\models\mall\MallAddons;
- use common\models\order\OrderDetail;
- use common\models\user\Town;
- use common\models\user\User;
- use Exception;
- use services\common\UserWalletService;
- use Yii;
- use yii\db\Expression;
- /**
- * This is the model class for table "qimall_addons_area_agent".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $user_id 会员id
- * @property string $realname 代理真实姓名
- * @property string $mobile 代理联系方式
- * @property float $total_commission 累计佣金
- * @property int $level 0普通用户 4、镇代 3、区代 2、市代 1、省代
- * @property int $regional_id 大区id
- * @property int $province_id 省id
- * @property int $city_id 市id
- * @property int $district_id 区id
- * @property int $town_id 镇id
- * @property int $community_id 小区id
- * @property string $address 省市区镇地址名称
- * @property string|null $remarks 备注
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property int $is_enable_dividend 修改时间
- * @property float $total_digital 累计数字币
- */
- class AreaAgent extends BaseActiveRecord
- {
- const LEVEL_FIELD_MAP = [
- AreaEnum::PROVINCE => 'province_id',
- AreaEnum::CITY => 'city_id',
- AreaEnum::DISTRICT => 'district_id',
- AreaEnum::TOWN => 'town_id',
- AreaEnum::COMMUNITY => 'community_id',
- AreaEnum::REGIONAL => 'regional_id',
- ];
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_area_agent}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'level', 'regional_id', 'province_id', 'city_id', 'district_id', 'town_id',
- 'status', 'created_at', 'updated_at', 'community_id', 'is_enable_dividend'], 'integer'],
- [['total_commission'], 'number'],
- [['remarks'], 'string'],
- [['address', 'realname', 'mobile'], 'string', 'max' => 255],
- [['total_digital'],'number']
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'user_id' => '会员id',
- 'realname' => '代理真实姓名',
- 'mobile' => '代理联系方式',
- 'total_commission' => '累计佣金',
- 'level' => '0普通用户 4、镇代 3、区代 2、市代 1、省代',
- 'regional_id' => '大区id',
- 'province_id' => '省id',
- 'city_id' => '市id',
- 'district_id' => '区id',
- 'town_id' => '镇id',
- 'address' => '省市区镇地址名称',
- 'remarks' => '备注',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- }
- /**
- * @desc:关联会员信息
- * @Author: hua
- * @Date: 2022/1/18
- * @Time: 17:25
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getUser(): \yii\db\ActiveQuery
- {
- return $this->hasOne(User::class, ['id' => 'user_id']);
- }
- public function getGoods()
- {
- return $this->hasMany(AreaGoods::class, ['agent_id' => 'id'])
- ->where(['status' => StatusEnum::ENABLED]);
- }
- public static function getAreaAgentByLevel($level, $address_id, $mall_id): array
- {
- $query = self::find()->innerJoinWith(['user'], false)->where(['qimall_addons_area_agent.mall_id' => $mall_id, 'qimall_addons_area_agent.level' => $level, 'qimall_addons_area_agent.status' => StatusEnum::ENABLED]);
- switch ($level) {
- case AreaEnum::COMMUNITY:
- $query->andWhere(['community_id' => $address_id]);
- break;
- case AreaEnum::TOWN:
- $query->andWhere(['town_id' => $address_id]);
- break;
- case AreaEnum::DISTRICT:
- $query->andWhere(['district_id' => $address_id]);
- break;
- case AreaEnum::CITY:
- $query->andWhere(['city_id' => $address_id]);
- break;
- case AreaEnum::PROVINCE:
- $query->andWhere(['province_id' => $address_id]);
- break;
- case AreaEnum::REGIONAL:
- $query->andWhere(['regional_id' => $address_id]);
- break;
- }
- return $query->all();
- }
- public static function getAreaAgentOneByLevel($user_id,$level, $address_id, $mall_id)
- {
- $query = self::find()->where(['mall_id' => $mall_id, 'user_id'=>$user_id,'level' => $level, 'status' => StatusEnum::ENABLED]);
- switch ($level) {
- case AreaEnum::COMMUNITY:
- $query->andWhere(['community_id' => $address_id]);
- break;
- case AreaEnum::TOWN:
- $query->andWhere(['town_id' => $address_id]);
- break;
- case AreaEnum::DISTRICT:
- $query->andWhere(['district_id' => $address_id]);
- break;
- case AreaEnum::CITY:
- $query->andWhere(['city_id' => $address_id]);
- break;
- case AreaEnum::PROVINCE:
- $query->andWhere(['province_id' => $address_id]);
- break;
- case AreaEnum::REGIONAL:
- $query->andWhere(['regional_id' => $address_id]);
- break;
- }
- return $query->one();
- }
- public static function setData(array $params)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $is_edit = false;
- if (!empty($params['id'])) {
- $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) throw new Exception('服务不存在或已删除');
- $is_edit = true;
- } else {
- $model = new self();
- $model->loadDefaultValues();
- $params['is_enable_dividend'] = StatusEnum::DISABLED;
- }
-
- switch ($params['level']) {
- case AreaEnum::PROVINCE:
- $field = 'province_id';
- $value = $params['province_id'];
- break;
- case AreaEnum::CITY:
- $field = 'city_id';
- $value = $params['city_id'];
- break;
- case AreaEnum::DISTRICT:
- $field = 'district_id';
- $value = $params['district_id'];
- break;
- case AreaEnum::TOWN:
- $field = 'town_id';
- $value = $params['town_id'];
- break;
- case AreaEnum::COMMUNITY:
- $field = 'community_id';
- $value = $params['community_id'];
- break;
- case AreaEnum::REGIONAL:
- $field = 'regional_id';
- $value = $params['regional_id'];
- break;
- default:
- throw new Exception('请选择区域');
- }
- if (!$is_edit) {
- $area_agent = AreaAgent::findOne([
- 'user_id' => $params['user_id'],
- 'mall_id' => $params['mall_id'],
- $field => $value,
- 'level' => $params['level'],
- 'status' => StatusEnum::ENABLED,
- ]);
- if (!empty($area_agent)) throw new Exception('该会员已经是该地区区域代理');
- }
- $config = \Yii::$app->services->setting->addons->get($params['mall_id'], AreaEnum::ADDONS_NAME, 'basic');
- if(empty($params['id'])){
- $counts = AreaAgent::find()->where(['user_id' => $params['user_id'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED])->count();
- if (!empty($config['can_apply_area_num']) && $config['can_apply_area_num'] != -1 && $counts >= $config['can_apply_area_num']) throw new Exception('超出可申请最大数量');
- }
- $region_id_arr = [];
- if (!empty($params['province_id'])) $region_id_arr[] = $params['province_id'];
- if (!empty($params['city_id'])) $region_id_arr[] = $params['city_id'];
- if (!empty($params['district_id'])) $region_id_arr[] = $params['district_id'];
- $address = '';
- if ($params['level'] == AreaEnum::REGIONAL && !empty($params['regional_id'])) {
- $regional_name = AreaRegional::find()->where(['id' => $params['regional_id']])->select('regional_name')->scalar();
- !empty($regional_name) && $address .= $regional_name;
- }
- if (!empty($region_id_arr)) {
- $region_list = Region::lists(['where' => [['id' => $region_id_arr]], 'select' => 'id,name', 'order' => 'id asc']);
- foreach ($region_list as $item) {
- $address .= $item['name'];
- }
- if (!empty($params['town_id'])) {
- $town = Town::getOne([['id' => $params['town_id']]], true, [], false, 'id,name');
- if (!empty($town['name'])) $address .= $town['name'];
- }
- if (!empty($params['community_id'])) {
- $community = AreaCommunity::find()->where(['id' => $params['community_id']])->select('community_name')->scalar();
- if ($community) $address .= $community;
- }
- }
- if (!empty($address)) $params['address'] = $address;
- //对身份为区代的用户修改身份时,查询区代身份是否为购买,是则需要退回金额
- if (!empty($params['id']) && $model['level'] == 3) {
- $order_data = AreaOrder::getOne([
- ['mall_id' => $model['mall_id']],
- ['user_id' => $model['user_id']],
- ['area_id' => $model['district_id']],
- ['status' => StatusEnum::ENABLED],
- ['is_pay' => StatusEnum::ENABLED],
- ]);
- if (!empty($order_data)) {
- $insert_wallet[] = [
- 'mall_id' => $order_data->mall_id,
- 'user_id' => $order_data->user_id,
- 'is_frozen' => false,
- 'wallet_type' => 'balance',
- 'money' => $order_data->price, //转入金额
- 'desc' => '代理身份被修改,退回金额',
- 'source_table' => 'addons_area_order',
- 'source_table_id' => $order_data->id,//结算周期id
- 'from_type' => UserWalletEnum::REFUND,
- 'capital_type' => UserWalletEnum::AREA_BONUS_REFUND,
- // 'not_add_total' => true,
- ];
- $order_data->status = StatusEnum::DELETE;
- }
- }
- if (!$model->load($params, '')) throw new Exception($model->getErrorMessage());
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- //维护订单数据,和审核记录
- if (!empty($order_data)) {
- $order_data->save();
- AreaApply::updateAll(['is_refund' => 1, 'refund_time' => time(), 'refund_remark' => '代理身份被删除'], ['mall_id' => $order_data['mall_id'], 'order_id' => $order_data['id']]);
- if (!empty($insert_wallet)) {
- UserWalletService::batchHandleByQueue($insert_wallet);
- }
- }
- if (!empty($params['goods'])) { // 提交了商品
- // 删除之前旧的商品
- AreaGoods::batchSave($params['mall_id'], $params['user_id'], $model->id, $params['goods']);
- }
- $t->commit();
- return $model;
- } catch (Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function getRegionList($parent_id, $mall_id, $level_id_arr)
- {
- $list = Region::lists(['where' => [['parent_id' => $parent_id]], 'select' => 'id,level,name,parent_id']);
- $id_arr = [];
- $level = '';
- if (!empty($list)) {
- foreach ($list as $val) {
- $id_arr[] = $val['id'];
- $level = $val['level'];
- }
- $index = $level_id_arr[$level];
- $area_agent_list = AreaAgent::lists([
- 'where' => [
- [$index => $id_arr],
- ['level' => $level],
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED]
- ],
- 'select' => 'id,user_id,' . $index,
- 'with' => ['user' => function ($query) {
- $query->select('id,nickname,avatar_url');
- }],
- 'index' => $index
- ]);
- foreach ($list as &$item) {
- $item['agent_list'] = [];
- if (isset($area_agent_list[$item['id']])) $item['agent_list'][] = $area_agent_list[$item['id']];
- }
- }
- return $list;
- }
- public static function getDistrictList($params)
- {
- $level_arr = [AreaEnum::PROVINCE];
- if ($params['level'] == AreaEnum::CITY) $level_arr[] = AreaEnum::CITY;
- if (in_array($params['level'], [AreaEnum::DISTRICT, AreaEnum::TOWN])) {
- $level_arr[] = AreaEnum::CITY;
- $level_arr[] = AreaEnum::DISTRICT;
- }
- $where[] = ['level' => $level_arr];
- $list = Region::getRegionList($where, 'id,name,parent_id,level');
- if (in_array($params['level'], [AreaEnum::CITY, AreaEnum::DISTRICT, AreaEnum::TOWN])) {
- $tree = [];
- $level_arr = [];
- foreach ($list as $k => $item) {
- if ($item['level'] == AreaEnum::PROVINCE) $tree[] = $item;
- if (in_array($item['level'], [AreaEnum::CITY, AreaEnum::DISTRICT])) $level_arr[$item['parent_id']][] = $item;
- }
- foreach ($tree as &$province) {
- if (isset($level_arr[$province['id']])) {
- foreach ($level_arr[$province['id']] as &$city) {
- if (isset($level_arr[$city['id']])) $city['list'] = $level_arr[$city['id']];
- }
- $province['list'] = $level_arr[$province['id']];
- }
- }
- return $tree;
- } else {
- return $list;
- }
- }
- /**
- * @desc:推广中心-调用
- * @Author: hua
- * @Date: 2021/11/12
- * @Time: 9:30
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params ['user_id' => 1, 'mall_id' => 5, 'addons_name' => 'Agent']
- * @return array
- */
- public static function getIncomeInfo(array $params)
- {
- $is_install = MallAddons::isInstall($params['mall_id'], $params['addons_name']);
- $data['total_commission'] = 0;
- $data['is_install'] = 0;
- $basic = \Yii::$app->services->setting->addons->get($params['mall_id'], $params['addons_name'], 'basic');
- $data['is_show_at_center'] = 1;
- if(isset($basic['is_show_at_center'])) $data['is_show_at_center'] = $basic['is_show_at_center'];
- if ($is_install) {
- $total_commission = self::find()->where(['user_id' => $params['user_id'],'status'=>StatusEnum::ENABLED])->sum('total_commission');
- $data['total_commission'] = $total_commission ?? 0;
- $data['is_install'] = $is_install;
- }
- return $data;
- }
- public static function getLevel($params){
- $config = \Yii::$app->services->setting->addons->get($params['mall_id'], AreaEnum::ADDONS_NAME, 'basic');
- return [
- ['level'=>6,'name'=> $config["level_6"] ?? '大区代'],
- ['level'=>1,'name'=> $config["level_1"] ?? '省代'],
- ['level'=>2,'name'=> $config["level_2"] ?? '市代'],
- ['level'=>3,'name'=> $config["level_3"] ?? '区代'],
- ['level'=>4,'name'=> $config["level_4"] ?? '镇代'],
- ['level'=>5,'name'=> $config["level_5"] ?? '小区代'],
- ];
- }
- public static function getAddons(array $params){
- $is_install = MallAddons::isInstall($params['mall_id'], AreaEnum::ADDONS_NAME);
- if($is_install){
- $detail = Addons::getOne([['name' => AreaEnum::ADDONS_NAME], ['is_online' => 1]], true);
- return [
- 'addons_name'=>AreaEnum::ADDONS_NAME,
- 'title'=>$detail['title'],
- ];
- }
- return [];
- }
- /**
- * 订单退款维护数据表统计字段(外部开事务)
- *
- * @param $wallet_type
- * @param $order_detail_id
- *
- * @return bool
- */
- public static function orderRefundUpdateTotalFields($wallet_type, $order_detail_id): bool
- {
- if (empty($order_detail_id) || empty($wallet_type)) {
- return false;
- }
- $rewardLogs = AreaCumulativeRewardLog::lists([
- 'where' => [
- [
- 'is_return' => StatusEnum::DISABLED,
- 'source_table' => 'order_detail',
- 'source_table_id' => $order_detail_id,
- 'status' => StatusEnum::ENABLED,
- 'wallet_type' => $wallet_type
- ]
- ],
- 'select' => 'agent_id, change_num, id'
- ]);
-
- if (empty($rewardLogs)) {
- return false;
- }
- $now = time();
- foreach ($rewardLogs as $rewardLog) {
- $field = 'total_' . $wallet_type;
- self::updateAll(
- [
- $field => new Expression("`{$field}` - {$rewardLog['change_num']}"),
- 'updated_at' => $now,
- ],
- [
- 'id' => $rewardLog['agent_id']
- ]
- );
- }
- AreaCumulativeRewardLog::updateAll(
- [
- 'is_return' => StatusEnum::ENABLED,
- 'updated_at' => $now
- ],
- [
- 'id' => array_column($rewardLogs, 'id')
- ]
- );
- return true;
- }
- /**
- * 订单退款维护数据表统计字段(旧方法,为了公用,迁移到这里)
- *
- * @return bool
- */
- public static function orderRefundUpdateTotalFieldsOld($commission_list,$wallet_type,$order_detail_id, $order = null)
- {
- if (empty($order)) {
- $orderDetail = OrderDetail::getOne([['id'=>$order_detail_id]],true,['order']);
- if(empty($orderDetail['order'])) return false;
- $order = $orderDetail['order'];
- }
- $province_id = $order['province'];
- $city_id = $order['city'];
- $area_id = $order['area'];
- $town_id = $order['town'];
- $community_id = $order['community'];
- $regional_id = 0; // 大区ID
- if (!empty($province_id)) {
- $like_conditions = ['%,'. $province_id, '%,'. $province_id. ',%', $province_id. ',%'];
- $regional = AreaRegional::getOne([['or like', 'province_ids', $like_conditions, false], ['status' => StatusEnum::ENABLED], ['mall_id' => $order['mall_id']]]);
- !empty($regional) && $regional_id = $regional['id'];
- }
- $user_ids = array_column($commission_list,'user_id');
- $area_agent_list = AreaAgent::lists([
- 'where'=>[
- ['user_id'=>$user_ids],
- ['status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]
- ]],false);
- $area_agents = [];
- foreach ($area_agent_list as $val){
- $area_agents[$val['user_id']][]=$val;
- }
- foreach ($commission_list as $item) {
- if(!isset($area_agents[$item['user_id']])) continue;
- $area_agent_arr = $area_agents[$item['user_id']];
- foreach ($area_agent_arr as $area_agent){
- switch ($area_agent['level']){
- case 1:
- if($province_id==$area_agent['province_id']){
- $field = 'total_' . $wallet_type;
- $area_agent->$field += -$item['change_num'];
- if($area_agent->$field<0) $area_agent->$field = 0;
- $res = $area_agent->save();
- }
- break;
- case 2:
- if($city_id==$area_agent['city_id']){
- $field = 'total_' . $wallet_type;
- $area_agent->$field += -$item['change_num'];
- if($area_agent->$field<0) $area_agent->$field = 0;
- $res = $area_agent->save();
- }
- break;
- case 3:
- if($area_id==$area_agent['district_id']){
- $field = 'total_' . $wallet_type;
- $area_agent->$field += -$item['change_num'];
- if($area_agent->$field<0) $area_agent->$field = 0;
- $res = $area_agent->save();
- }
- break;
- case 4:
- if($town_id==$area_agent['town_id']){
- $field = 'total_' . $wallet_type;
- $area_agent->$field += -$item['change_num'];
- if($area_agent->$field<0) $area_agent->$field = 0;
- $res = $area_agent->save();
- }
- break;
- case 5:
- if($community_id==$area_agent['community_id']){
- $field = 'total_' . $wallet_type;
- $area_agent->$field += -$item['change_num'];
- if($area_agent->$field<0) $area_agent->$field = 0;
- $res = $area_agent->save();
- }
- break;
- case 6:
- if($regional_id == $area_agent['regional_id']){
- $field = 'total_' . $wallet_type;
- $area_agent->$field += -$item['change_num'];
- if($area_agent->$field<0) $area_agent->$field = 0;
- $res = $area_agent->save();
- }
- break;
- }
- }
- }
- return true;
- }
- }
|