2048], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => '商城id', 'user_id' => '用户id', 'remarks' => '备注', 'total_childs' => '所有下级数量', 'total_order' => '订单数量', 'upgrade_at' => '等级升级时间', 'total_price' => '累计收益', 'price' => '已结算收益', 'frozen_price' => '未结算收益', 'upgrade_status' => '升级类型;1条件升级;2升级礼包;3平台添加;4上级添加', 'level' => '代理商等级权重', 'before_level' => '升级前等级;null为无', 'status' => '状态[-1:删除;0:禁用;1启用]', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'fill_num' => '补货数量', ]; } public function getUser() { return $this->hasOne(User::class, ['id' => 'user_id'])->select(['id', 'nickname', 'avatar_url', 'mobile', 'parent_id']); } public function getStockLevel() { return $this->hasOne(CloudStockTwoLevel::class, ['level' => 'level', 'mall_id' => 'mall_id'])->where(['status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]); } public static function setData(array $params) { try { 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('服务不存在或已删除'); $params['before_level'] = $model->level; if(isset($params['level'])){ if ($params['before_level'] < $params['level']) $params['upgrade_at'] = time(); } } else { $model = new self(); $model->loadDefaultValues(); } if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage()); if (!$model->save()) throw new \Exception($model->getErrorMessage()); if(isset($params['level'])&&$params['level']>$model['before_level']){ $level = CloudStockTwoLevel::findOne(['mall_id' => $params['mall_id'], 'level' => $params['level'],'status'=>[StatusEnum::ENABLED]]); if (!empty($level)) { $give_quota = json_decode($level['give_quota'], true); if (!empty($give_quota)) { foreach($give_quota as $item){ if(!empty($item['is_use'])&&!empty($item['num'])){ $param = []; $param['mall_id'] = $model['mall_id']; $param['user_id'] = $model['user_id']; $param['num'] = $item['num']; $param['level'] = $item['level']; $res = CloudStockTwoAgentQuota::setData($param); if ($res==false) throw new Exception(CloudStockTwoAgentQuota::getStaticError()); } } } } } if(isset($params['status'])&&$params['status']==StatusEnum::DELETE){ CloudStockTwoAgentQuota::updateAll(['status'=>StatusEnum::DELETE],['user_id'=>$model['user_id']]); } return $model; } catch (\Exception $e) { self::$static_error = $e->getMessage(); return false; } } /** * @name: * @msg: 获取供应商 * @param {*} $agent_user_id 当前代理商 user_id * @param {*} $level 当前代理商等级 * @return object */ public static function getUserParentAgent($agent_user_id, $level) { $parent_id_arr = UserPartitionRelationship::getParentIdArr($agent_user_id); $parent_id_arr = array_reverse($parent_id_arr); //补货只能找等级比自己大的代理补货 $parent_agent_list = CloudStockTwoAgent::lists(['where' => [['user_id' => $parent_id_arr], ['>', 'level', $level],['status'=>[StatusEnum::DISABLED, StatusEnum::ENABLED]]], 'index' => 'user_id'], false); $parent_agent = null; foreach ($parent_id_arr as $user_id) { if (isset($parent_agent_list[$user_id])) { $parent_agent = $parent_agent_list[$user_id]; break; } } return $parent_agent; } public static function getUserParentAgentList($agent_user_id,$is_array = false) { $parent_id_arr = UserPartitionRelationship::getParentIdArr($agent_user_id); $parent_id_arr = array_reverse($parent_id_arr); $parent_agent_list = CloudStockTwoAgent::lists(['where' => [['user_id' => $parent_id_arr]], 'index' => 'user_id'], $is_array); $list = []; foreach ($parent_id_arr as $user_id) { if (isset($parent_agent_list[$user_id])) { $list[] = $parent_agent_list[$user_id]; } } return $list; } //投递到资产队列 public static function sendUserWalletQueue($insert_wallet) { try { if ($insert_wallet) { $res = UserWalletService::batchHandleByQueue($insert_wallet); if ($res == false) throw new \Exception(UserWalletService::getStaticError()); $user_ids = array_column($insert_wallet,'user_id'); $user_arr = User::lists(['where'=>[['id'=>$user_ids]],'select'=>'id,mobile','index'=>'id']); foreach ($insert_wallet as $k => $user) { $agent = CloudStockTwoAgent::findOne(['user_id' => $user['user_id'],'status'=>StatusEnum::ENABLED]); if (!empty($user['is_frozen'])) { $agent->frozen_price += $user['money']; } else { $agent->price += $user['money']; } $agent->total_price += $user['money']; $res = $agent->save(); if (empty($res)) throw new \Exception($agent->getErrorMessage()); //奖励短信 $mobile = $user_arr[$user['user_id']]['mobile']??0; if(!empty($mobile)&&!$user['is_frozen']){ if (in_array($user['capital_type'], [UserWalletEnum::FILL, UserWalletEnum::OVER, UserWalletEnum::EQUAL,UserWalletEnum::TEAM])) { $obj = new CloudStockTwoAgent(); $obj->sendMessage($user['mall_id'],$mobile,$user['money']); } //货款短信 if (in_array($user['capital_type'], [UserWalletEnum::PAYMENT_FOR_GOODS_FILL, UserWalletEnum::PAYMENT_FOR_GOODS_BUY])) { $obj = new CloudStockTwoAgent(); $obj->sendMessage($user['mall_id'],$mobile,$user['money']); } } } } } catch (\Exception $e) { // echo 'sendUserWalletQueue:' . $e->getMessage(); \Yii::error('sendUserWalletQueue:' . $e->getMessage()); // return false; } } /** * @name: * @msg: 设置下级代理 * @param {int} $mall_id * @param {array} $data user_id:当前代理会员主表 id;agent_id:当前代理 id;child_id:下级会员 id;set_level:给下级设定的等级 * @return bool */ public static function setWholesaler(int $mall_id, array $data): bool { $transaction = \Yii::$app->db->beginTransaction(); $params_agent = [ 'mall_id' => $mall_id, 'user_id' => $data['child_id'], 'level' => $data['set_level'], 'upgrade_status' => $data['upgrade_status'], ]; unset($data['upgrade_status']); $res_agent = self::setData($params_agent); if (false === $res_agent) { $transaction->rollBack(); return false; } $data['mall_id'] = $mall_id; $data['child_agent_id'] = $res_agent->id; $res_log = CloudStockTwoSetAgentLog::setData($mall_id, $data); if (false === $res_log) { $transaction->rollBack(); self::$static_error = CloudStockTwoSetAgentLog::getStaticError(); return false; } $params = []; $params['mall_id'] = $mall_id; $params['user_id'] = $data['user_id']; $params['num'] = -1; $params['level'] =$data['set_level']; CloudStockTwoAgentQuota::setData($params); $transaction->commit(); return true; } //发送短信 public function sendMessage($mall_id,$mobile,$money){ try{ if(!empty($mobile)){ $SmsConfig = new Sms($mall_id); $easy_sms = $SmsConfig->easySms; $message = new RewardMessage($mall_id,bcmul($money,1,2)); $easy_sms->send($mobile, $message); } }catch (NoGatewayAvailableException $e){ $res = $e->getExceptions(); if(!empty($res)){ foreach ($res as $exception){ if(is_string($exception->getMessage())){ \Yii::error('云库存收益通知短信发送失败:' .$exception->getMessage()); } } } } } //货款短信 public function sendGoodsMoneyMessage($user_arr,$item){ $mobile = $user_arr[$item['user_id']]['mobile']??0; if(!empty($mobile)){ $this->sendMessage($item['mall_id'],$mobile, $item['change_num']); } } 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 []; } }