| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <?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\goods\GoodsCateRelate;
- 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}}".
- *
- * @property int $id
- * @property int $mall_id
- * @property string $title
- * @property int $start_at 开始时间,单位:秒
- * @property bigInteger $end_at 结束时间,单位:秒
- * @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_info 活动商品详情
- * @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 参与类型
- * @property int $is_grant 参与类型
- * @property int $is_reward_after_n 是否开启第N人后开始才可以获得奖励 0否 1是
- * @property int $reward_threshold 具体配置的第N人之后
- * @property int $is_freeze_first_reward 是否冻结第一项奖励
- * @property int $item_type 活动商品类型 0商品ID 1商品分类ID
- * @property int $freeze_first_reward_type 冻结第一项奖励比例 0全部冻结 1按设置百分比冻结
- * @property float $freeze_first_reward_percentage 冻结第一项奖励百分比
- */
- class Rebate extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_rebate}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'title'], 'required'],
- [['mall_id', 'start_at', 'end_at', 'initiate_type', 'consume_type', 'item_id', 'reward_type', 'is_repeat', 'status', 'created_at', 'updated_at','join_type','repeat_setting','agent_limit_num','is_grant', 'is_reward_after_n', 'reward_threshold', 'is_freeze_first_reward', 'item_type', 'freeze_first_reward_type'], 'integer'],
- [['freeze_first_reward_percentage'], 'number'],
- [['end_at'], 'safe'],
- [['title', 'initiate_level','consume_level','item_info','reward_info'], 'string'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'title' => '活动名称',
- 'start_at' => '开始时间,单位:秒',
- 'end_at' => '结束时间,单位:秒',
- '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_info' => '活动商品详情',
- 'reward_type' => '奖励类型[1:奖励条件;2:阶梯奖励]',
- 'reward_info' => '奖励具体信息',
- 'is_repeat' => '阶梯奖励是否重复奖励[0=否,1=是]',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- 'join_type' => '参与类型',
- 'is_grant' => '优惠/抵扣不发奖开关:1开启 0关闭',
- 'is_reward_after_n' => '是否开启第N人后开始才可以获得奖励 0否 1是',
- 'reward_threshold' => '具体配置的第N人之后',
- 'is_freeze_first_reward' => '冻结第一项奖励,',
- 'freeze_first_reward_type' => '冻结第一项奖励比例 0全部冻结 1按设置百分比冻结,',
- 'freeze_first_reward_percentage' => '冻结第一项奖励百分比,',
- ];
- }
- /**
- * 关联统计表
- * @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($mall_id, array $params){
- try{
- if(!empty($params['id'])){
- $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $mall_id],['<>','status', -1]]);
- if(empty($model)) throw new \Exception('活动不存在或已删除');
- }else{
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $mall_id;
- }
- if (!empty($params['is_reward_after_n'])) {
- if (empty($params['reward_threshold']) || !is_numeric($params['reward_threshold'])) {
- throw new \RuntimeException('请设置第N人后开始才可以获得奖励');
- }
- if (!in_array($params['reward_threshold'], array_column($params['reward_info'], 'num'))) {
- throw new \RuntimeException('第N人后开始才可以获得奖励设置错误');
- }
- } else {
- $params['is_reward_after_n'] = 0;
- $params['reward_threshold'] = 0;
- }
- // 将内容转为json字符串
- !empty($params['initiate_level']) && $params['initiate_level'] = Json::encode($params['initiate_level']);
- !empty($params['consume_level']) && $params['consume_level'] = Json::encode($params['consume_level']);
- !empty($params['item_info']) && $params['item_info'] = Json::encode($params['item_info']);
- !empty($params['reward_info']) && $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,title,initiate_type,initiate_level,start_at,end_at,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;
- // 判断活动是否过期
- $time = time();
- if($data['start_at'] > $time || $time > $data['end_at']) 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,title,item_id,initiate_type,initiate_level,start_at,end_at,consume_type,consume_level,reward_type," .
- "reward_info,is_repeat,join_type,is_grant,repeat_setting,agent_limit_num,is_reward_after_n,reward_threshold," .
- "is_freeze_first_reward,item_type, freeze_first_reward_type, freeze_first_reward_percentage";
- $data = self::find()->select($select)
- ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])
- ->andWhere(["<", "start_at", $time])
- ->andWhere([">", "end_at", $time])
- ->asArray()->all();
- foreach($data as &$item) {
- if($item['item_type'] == 1) {
- // 分类ID查询对应商品
- $item['goods'] = GoodsCateRelate::find()->where(['cate_id' => $item['item_id']])
- ->select("id,cate_id,goods_id")
- ->with(['goods' => function($query){
- $query->select("id,goods_name,price,cover_pic");
- }])
- ->asArray()
- ->all();
- }
- }
- // 初始化结果数组
- $result = [];
- // 遍历原始数据数组
- foreach ($data as $originalItem) {
- if ($originalItem['item_type'] == 1) {
- //商品分类的重新组装数据
- foreach ($originalItem['goods'] as $good) {
- if (!empty($good['goods'])) {
- $newItem = array_diff_key($originalItem, array_flip(['goods']));
- $newItem['item_id'] = $good['goods_id'];
- $result[] = $newItem;
- }
- }
- } else {
- //商品ID的直接返回
- $result[] = $originalItem;
- }
- }
- return $result;
- }
- /**
- * 判断该商品是否已参与其他活动
- * @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;
- }
- }
|