| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- namespace addons\CloudStockTwo\common\models;
- use addons\CloudStockTwo\common\sms\RewardMessage;
- use addons\CloudStockTwo\common\sms\SmsConfig;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\helpers\sms\Sms;
- use common\models\base\BaseActiveRecord;
- use common\models\common\Addons;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- use common\models\user\UserPartitionRelationship;
- use Overtrue\EasySms\EasySms;
- use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
- use services\common\UserWalletService;
- use yii\db\Exception;
- /**
- * This is the model class for table "qimall_addons_cloud_stock_agent".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $user_id 用户id
- * @property string $remarks 备注
- * @property int $total_childs 所有下级数量
- * @property int $total_order 订单数量
- * @property int $upgrade_at 等级升级时间
- * @property float $total_price 累计收益
- * @property float $price 已结算收益
- * @property float $frozen_price 未结算收益
- * @property int $upgrade_status 升级类型;1条件升级;2升级礼包;3平台添加;4上级添加
- * @property int $level 代理商等级权重
- * @property int|null $before_level 升级前等级;null为无
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- * @property int $fill_num 补货数量
- */
- class CloudStockTwoAgent extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_cloud_stock_two_agent}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'total_childs', 'total_order', 'upgrade_status', 'upgrade_at', 'level', 'before_level', 'status', 'created_at', 'updated_at','fill_num'], 'integer'],
- [['total_price', 'price', 'frozen_price'], 'number'],
- [['remarks'], 'string', 'max' => 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 [];
- }
- }
|