zhaojs
2023-08-01 cc6247528b559cf6a9468591bb889bb58dc14199
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
namespace app\api\logic;
 
use app\common\logic\Basic as BasicLogic;
use think\console\output\descriptor\Console;
use think\Db;
use app\common\model\User as ModelUser;
use app\api\logic\SettlementRecord as LogicSettlementRecord;
use app\api\logic\UserBrowse as LogicUserBrowse;
 
class ActivityInfo extends BasicLogic
{
 
 
    /**
     * 获取活动信息
     */
    public function getActInfo($actid,$userid)
    {
        $res=Db::query('SELECT * FROM t_activity_manage WHERE id=?',[$actid]);
        $response=[
                'actstatus'=>1,
                'joinstatus'=>0
            ];
        if(empty($res)){
            $response['actstatus']=0;//不存在
            return $response;
        }
        if($res[0]['act_status']==0)
        {
            $response['actstatus']=-1;//已结束
            return $response;
        }
        if(time()<strtotime($res[0]['start_time']))
        {
            $response['actstatus']= -2;//未开始
            return $response;
        }
        if(time()>strtotime($res[0]['end_time']))
        {
            $response['actstatus']= -3;//已结束
            return $response;
        }
        //是否已参加活动
        $recruitid=$actid.'u'.$userid;
        $canjia=Db::query('select id from t_recruit_act where id=?',[$recruitid]);
        if(empty($canjia))
        {
            $response['joinstatus']=0;//未参加
        }
        else{
            $response['joinstatus']=1;//已参加
        }
        return $response;
    }
 
    /**
     * 参加活动
     * 
     */
    public function attendAct($actid,$userid)
    {
        //查询活动是否存在
        $res=Db::query('SELECT * FROM t_activity_manage WHERE id=?',[$actid]);
        if(empty($res)){
           return false;
        }
        if($res[0]['act_status']==0||time()>strtotime($res[0]['end_time'])||time()<strtotime($res[0]['start_time']))
        {
            return false;
        }
        //插入活动表
        $recruitid=$actid.'u'.$userid;
        $inser=Db::execute('insert ignore into t_recruit_act (id,user_id,act_id,create_time) values(?,?,?,?)',[$recruitid,$userid,$actid,time()]);
        return true;
    }
 
   
 
    /**
     * 更新获取已邀请人数
     */
    public function updateRecruitCount($userid)
    {
        try{
             //查询进行中的招募活动
            $nowTime= date("Y-m-d H:i:s",  time());
            $actList=Db::query('select id,end_time from t_activity_manage where act_status=1 and start_time<=? and end_time>=? and act_type=0 limit 1',[$nowTime,$nowTime]);
            if(empty($actList))
            {//没有进行中的活动,不用处理
                return true;
            }
           
            //查询是否参加了招募活动
            $recruitid=$actList[0]['id'].'u'.$userid;
            $actReinfo=Db::query('select id,create_time from t_recruit_act where id=? and user_t=0 and settlement_status=0',[$recruitid]);
            if(empty($actReinfo))
            {//没有参加活动,不用处理
                return true;
            }
            //获取已邀请人数
            $actEndTime=strtotime($actList[0]['end_time']);//活动结束时间
            $yqCount=Db::query('select count(DISTINCT(login_deviceid)) as count from t_user where invitation_id=? and status=? and invite_bind_time>=? and invite_bind_time<=?',[$userid,'normal',$actReinfo[0]['create_time'],$actEndTime]);
           $count=0;
           if(!empty($yqCount))
           {
            $count=$yqCount[0]['count'];
           }
            //增加count
            $countres=Db::execute('update t_recruit_act set recruit_count=? where id=?',[$count,$recruitid]);
            return $countres==1;
        }
        catch(\Exception $e)
        {
 
        }
    }
 
    /**
     * 获取已邀请人数
     */
    public function getActRecruitCount($actid,$userid)
    {
        $recruitid=$actid.'u'.$userid;
        $count=Db::query('select recruit_count from t_recruit_act where id=?',[$recruitid]);
        if(empty($count))
        {
            return [
                'membercount'=>0,
                'award'=>0
            ];
        }
        //获取金额信息
        $actList=Db::query('select * from t_activity_manage where id=? and count_status=0 and act_type=0 and act_status=1',[$actid]);
        $rewardRule=json_decode($actList[0]['act_json']);
        $award=$this->stepAllRule($rewardRule->rule,$count[0]['recruit_count']);
        return [
            'membercount'=>$count[0]['recruit_count'],
            'award'=>$award
        ];
    }
 
    /**
     * 获取排名
     */
    public function getRankingList($actid,$userid)
    {
        $rList=Db::query('SELECT a.id,a.nickname,a.avatar,b.recruit_count from t_recruit_act b left join t_user a on a.id =b.user_id where b.act_id=? and b.recruit_count>0 order by b.recruit_count desc limit 7',[$actid]);
        if(empty($rList))
        {
            return [];
        }
        return $rList;
    }
 
 
    /**
     * 计算招募活动金额
     * 
     */
    public function run_countRecruitAct()
    {
        //先查询招募活动
        $actList=Db::query('select * from t_activity_manage where count_status=0 and act_type=0 and act_status=1');
        if(empty($actList))
        {
            return '没有符合的活动';
        }
       
        foreach($actList as $act)
        {//循环活动,计算金额
            //修改活动计算状态
           $this->run_UpdateActCount($act['id'],1);
            $actEndTime=strtotime($act['end_time']);//活动结束时间
            //查询所有参与活动的会员
            $cusList=Db::query('select * from t_recruit_act where act_id=? and settlement_status=0 and user_t=0',[$act['id']]);
            if(empty($cusList))
            {
                $this->run_UpdateActCount($act['id'],2);
                continue;
            }
            //活动规则
            $rewardRule=json_decode($act['act_json']);
            foreach($cusList as $cus)
            {//循环参与活动的会员
                $tradeId=$act['id'].'u'.$cus['user_id'];
                $yqCount=Db::query('select count(DISTINCT(login_deviceid)) as count from t_user where invitation_id=? and status=? and invite_bind_time>=? and invite_bind_time<=?',[$cus['user_id'],'normal',$cus['create_time'],$actEndTime]);
                if(empty($yqCount))
                {
                    Db::execute('update t_recruit_act set settlement_status=?,settlement_time=?,act_count=0,act_award=0 where id=?',[1, date("Y-m-d H:i:s",  time()),$tradeId]);
                    continue;
                }
                //计算应得金额
                $price=0;
                switch($rewardRule->act_type)
                {
                    case 'step_all'://阶梯完成后赠送
                        $price=$this->stepAllRule($rewardRule->rule,$yqCount[0]['count']);
                        break;
                    default:
                        break;
                }
                if($price<=0)
                {
                    Db::execute('update t_recruit_act set settlement_status=?,settlement_time=?,act_count=?,act_award=0 where id=?',[1, date("Y-m-d H:i:s",  time()),$yqCount[0]['count'],$tradeId]);
                    continue;
                }
                //插入聚推客的订单
                $tradeHas=Db::query('select id from t_jtk_union_order_commission where trade_parent_id=?',[$tradeId]);
                if(!empty($tradeHas))
                {//已经有订单
                    continue;
                }
                //计算订单收益
                $title = $act['act_name'].'-活动奖励结算';
                ModelUser::money('inc',$price,$cus['user_id'],$title);
                LogicSettlementRecord::writeLog('jtk',$price,$cus['user_id'],$title);
                $tradeRes=Db::execute('insert into t_jtk_union_order_commission (trade_parent_id,trade_id,member_id,buy_member_id,is_own,alipay_total_price,pub_share_pre_fee,item_title,item_id,is_settle,tb_paid_time,create_time,update_time,tk_status,tk_earning_time,pub_share_fee)values(?,?,?,?,1,?,?,?,?,?,?,?,?,?,?,?)',[$tradeId,$tradeId,$cus['user_id'],$cus['user_id'],$price,$price,$act['act_name'].'-活动奖励',$act['id'],1,date("Y-m-d H:i:s",  time()),time(),time(),3,time(),$price]);
                if($tradeRes==1)
                {//修改计算状态
                    Db::execute('update t_recruit_act set settlement_status=?,settlement_time=?,act_count=?,act_award=? where id=?',[1, date("Y-m-d H:i:s",  time()),$yqCount[0]['count'],$price,$tradeId]);
                }
            }
            $this->run_UpdateActCount($act['id'],2);
        }
        return '执行-success';
    }
 
    public function run_UpdateActCount($actid,$countStatus)
    {
        if($countStatus==1)
        {
            Db::execute('update t_activity_manage set count_status=? where id=?',[$countStatus,$actid]);
        }
        else{
            Db::execute('update t_activity_manage set count_status=?,count_time=? where id=?',[$countStatus,time(),$actid]);
        }
    }
 
    /**
     * step_all规则
     * 
     */
    public function stepAllRule($rule,$yqCount)
    {
        $maxReward=0;
        foreach($rule as $r_item)
        {
            if($r_item->c_count>$yqCount)
            {
                continue;
            }
            if($maxReward<$r_item->reward)
            {
                $maxReward=$r_item->reward;
            }
        }
        return $maxReward;
    }
 
    /**
     * 自定义弹窗-0元购
     */
    public function popupcus_freebuy($userInfo)
    {
        $LogicUserBrowse = new LogicUserBrowse();
        $res = $LogicUserBrowse->checkIsFirstFree($userInfo->id);
        if($res === false)
        {
            return false;
        }
        if(empty($res))
        {
            return true;
        }
        return false;
    }
 
    /***
     * 记录商品点击
     */
    public function recordItemClick($goodsid,$plat,$title,$act_price)
    {
        $inser=Db::execute('insert into t_wxitems (id,create_time,goodsid,plat,title,act_price) values(?,?,?,?,?,?)',[guid(),date("Y-m-d H:i:s",  time()),$goodsid,$plat,$title,$act_price]);
        return true;
    }
}