| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- <?php
- /*
- * @Descripttion:
- * @copyright: Copyright (c) 2021 广东七件事集团
- * @Author: lun
- * @Date: 2021-12-28 21:23:40
- */
- namespace addons\Rebate\common\models;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use common\models\order\Order;
- use common\models\statistics\UserStatisticsTotal;
- use common\models\user\User;
- use phpseclib3\Math\BigInteger;
- use Yii;
- use yii\db\Exception;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%addons_rebate_goods}}".
- *
- * @property int $id
- * @property int $mall_idis_enable
- * @property int $is_enable
- * @property int $initiate_type 发起人权限类型[0:全部;1:新会员;2:等级会员]
- * @property string $initiate_level 等级会员json串[initiate_type=2时使用]
- * @property int $consume_type 参与人权限类型[0:全部;1:新会员;2:等级会员]
- * @property string $consume_level 等级会员json串[consume_type=2时使用]
- * @property int $item_id 活动商品id
- * @property string $item_type
- * @property int $reward_type 奖励类型[1:奖励条件;2:阶梯奖励]
- * @property string $reward_info 奖励详情
- * @property int $is_repeat 阶梯奖励是否重复奖励[0=否,1=是]
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- * @property int $join_type 参与类型
- */
- class RebateGoods extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_rebate_goods}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id'], 'required'],
- [['mall_id','is_enable', 'initiate_type', 'consume_type', 'item_id', 'reward_type', 'is_repeat', 'status', 'created_at', 'updated_at','join_type','repeat_setting','agent_limit_num'], 'integer'],
- [['initiate_level','item_type','consume_level','reward_info'], 'string'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'is_enable' => '是否开启[0=否,1=是]',
- 'initiate_type' => '发起人权限类型[0:全部;1:新会员;2:等级会员]',
- 'initiate_level' => '等级会员json串[initiate_type=2时使用]',
- 'consume_type' => '参与人权限类型[0:全部;1:新会员;2:等级会员]',
- 'consume_level' => '等级会员json串[consume_type=2时使用]',
- 'item_id' => '活动商品id',
- 'item_type'=>'项目类型:商品goods',
- 'reward_type' => '奖励类型[1:奖励条件;2:阶梯奖励]',
- 'reward_info' => '奖励具体信息',
- 'is_repeat' => '阶梯奖励是否重复奖励[0=否,1=是]',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- 'join_type' => '参与类型',
- ];
- }
- /**
- * 关联统计表
- * @Author: lun
- * @DateTime: 2022/1/18 0018 15:17
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getStatistics()
- {
- return $this->hasOne(RebateStatistics::class, ['r_id' => 'id'])->where(['status' => 1]);
- }
- /**
- * 关联商品信息
- * @Author: lun
- * @DateTime: 2022/1/18 0018 16:56
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'item_id']);
- }
- /**
- * goods表字段过滤
- * @param $item
- * @return array
- */
- public static function filterGoods($goods_id)
- {
- if (!$goods_id) {
- return [];
- }
- $goods = Goods::findOne(['id' =>$goods_id]);
- if (!$goods) {
- return [];
- }
- return [
- 'id' => $goods->id,
- 'name' => $goods->name,
- 'cover_pic' => $goods->coverPic,
- 'price' => $goods->price,
- ];
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2022/1/15 0015 9:26
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param array $params
- * @return Rebate|bool
- */
- public static function setData($mallArr, array $params){
- try {
- $params = $params['commission'];
- $model = self::findOne(['item_id' => $mallArr['item_id'], 'mall_id' => $mallArr['mall_id'], 'status' => [StatusEnum::ENABLED]]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $mallArr['mall_id'];
- $model->item_id = $mallArr['item_id'];
- $model->loadDefaultValues();
- }
- // 将内容转为json字符串
- $params['initiate_level'] = !empty($params['initiate_level']) ? Json::encode($params['initiate_level']) : '';
- $params['consume_level'] = !empty($params['consume_level']) ? Json::encode($params['consume_level']) : '';
- $params['reward_info'] = !empty($params['reward_info']) ? Json::encode($params['reward_info']) : '';
- if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 根据商品id获取活动内容
- * @Author: lun
- * @DateTime: 2022/1/15 0015 9:26
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $goods_id
- * @return array|bool|\yii\db\ActiveRecord
- */
- public static function getRebateByGoodsId($mall_id, $goods_id)
- {
- $select = "id,initiate_type,initiate_level,consume_type,consume_level,reward_type,reward_info,is_repeat";
- $data = self::find()->select($select)->where(['mall_id' => $mall_id, 'item_id' => $goods_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
- if(empty($data)) return false;
- return $data;
- }
- /**
- * 根据商品id获取活动内容
- * @Author: lun
- * @DateTime: 2022/1/15 0015 9:26
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $goods_id
- * @return array|bool|\yii\db\ActiveRecord
- */
- public static function getRebateAllGoodsId($mall_id)
- {
- $time = time();
- $select = "id,item_id,initiate_type,initiate_level,consume_type,consume_level,reward_type,reward_info,is_repeat,join_type,repeat_setting,agent_limit_num";
- $data = self::find()->select($select)
- ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])
- ->asArray()->all();
- return $data;
- }
- /**
- * 判断该商品是否已参与其他活动
- * @Author: lun
- * @DateTime: 2022/1/15 0015 9:24
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $goods_id
- * @param int $r_id
- * @return bool
- */
- public static function checkGoods($mall_id, $goods_id, $r_id = 0)
- {
- $checkGoods = self::find()->select("id")->where(['mall_id' => $mall_id, 'item_id' => $goods_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
- if ($checkGoods) {
- if(empty($r_id) || (!empty($r_id) && $checkGoods['id'] != $r_id)) {
- return false;
- }
- }
- return true;
- }
- /**
- * 判断用户是否支付过
- * @Author: lun
- * @DateTime: 2022/1/15 0015 9:24
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $user_id
- * @return mixed
- */
- public static function checkUserHasOrder($mall_id, $user_id)
- {
- // $num = UserStatisticsTotal::find()->select("pay_num")->where(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->scalar();
- $num = Order::find()->select("id")->where(['mall_id' => $mall_id, 'user_id' => $user_id, 'pay_status' => StatusEnum::ENABLED,'status' => StatusEnum::ENABLED])->asArray()->count();
- if (empty($num)) return false;
- return $num;
- }
- /**
- * 判断用户等级是否符合条件
- * @Author: lun
- * @DateTime: 2022/1/6 0006 17:54
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $user_id
- * @param $level_arr
- * @return bool
- */
- public static function checkUserLevel($user_id, $level_arr)
- {
- $level = User::find()->select("level")->where(['id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->scalar();
- if(in_array($level, Json::decode($level_arr, true))) return true;
- return false;
- }
- public static function checkNewRegisterUser($mall_id, $user_id)
- {
- $res = User::find()->where([
- 'mall_id'=>$mall_id,
- 'id'=>$user_id,
- 'status'=>StatusEnum::ENABLED
- ])->andWhere(['>=','created_at',time()-7*24*3600])->one();
- if (empty($res)) return false;
- return true;
- }
- public static function checkRebateUserLevel($user_id, $level_arr)
- {
- $level = RebateUser::find()->select("level")->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->scalar();
- if(in_array($level, Json::decode($level_arr, true))) return true;
- return false;
- }
- public static function listsInit($params)
- {
- $list= self::lists($params);
- if(!empty($list))
- foreach ($list as $key => &$item) {
- $item['consume_level'] = empty($item['consume_level']) ? [] : Json::decode($item['consume_level'], true);
- $item['initiate_level'] = empty($item['initiate_level']) ? [] : Json::decode($item['initiate_level'], true);
- $item['reward_info'] = Json::decode($item['reward_info'], true);
- }
- return $list;
- }
- }
|