| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- namespace app\common\model;
- use alipay\request\AlipayUserInfoAuthRequest;
- use app\api\services\CouponServices;
- use app\common\model\TimeModel;
- use jobs\CouponPlanJob;
- use think\db\builder\Oracle;
- use think\Exception;
- use think\facade\Db;
- use think\Model;
- use utils\Queue;
- class CouponPlanModel extends Model
- {
- protected $name = "coupon_plan";
- public function area ()
- {
- return $this->belongsTo('app\common\model\CouponAreaModel', 'area_id', 'id');
- }
- // 添加计划 uid 用户uid fail_id 记录id get_time 请求进来时间cuo
- public function addPlan($uid, $money, $plan_id, $is_coin = false, $fail_id, $get_time)
- {
- // sr_testDb($uid, 99);
- if (!$money || !$plan_id)
- throw new Exception('购买失败,参数错误');
- if ($money % 10 > 0)
- {
- throw new Exception('购买失败,金额必须是10的整数倍');
- }
- $plan = self::where('id', $plan_id)->find();
- // 判断用户积分
- $m_uer = new UserModel();
- $userinfo = $m_uer->where('id', $uid)->field('score, is_auth,id,pid,level,path,medal,score_away,income_type')->find();
- // 老马的下级不能转赠
- if (strpos($userinfo['path'], '7493430') !== false){
- sr_throw('失败,请联系上面团队长');
- }
- if ($uid == 7493430){
- sr_throw('失败,请联系上面团队长');
- }
- $is_fl = false;
- if (in_array($plan['area_id'], [17,18])){
- // 判断锁定积分
- $is_fl = true;
- if ($userinfo['income_type'] != 3){
- sr_throw('暂无权限购买');
- }
- }
- if ($is_fl && $is_coin){
- sr_throw('福利区不能使用优先勋章');
- }
- if ($userinfo['is_auth'] != 1){
- throw new Exception('还未实名,参与失败');
- }
- if ($is_fl){
- if ($userinfo['score_away'] < $money){
- throw new Exception('购买失败,用户锁定积分不足');
- }
- }else{
- if ($userinfo['score'] < $money){
- throw new Exception('购买失败,用户积分不足');
- }
- }
- if (!$plan){
- throw new Exception('购买失败,计划不存在');
- }
- if ($plan && $plan['state'] != 0){
- throw new Exception('已被抢完啦~~~');
- }
- // 判断计划是否满足增长时间
- if ($get_time - strtotime($plan['create_time']) < env('COMMON.PLAN_EMIT_TIME')){
- // throw new Exception('购买失败,请在'.sr_getcurtime(strtotime($plan['create_time'])+env('COMMON.PLAN_EMIT_TIME')).'在购买');
- throw new Exception('暂未开放');
- }
- if ($money<$plan['buy_least']){
- throw new Exception('购买失败,购买数量小于最小购买数');
- }
- if (!in_array($uid, [7493267,7493266])){
- if ($money>$plan['buy_most']){
- throw new Exception('购买失败,购买数量大于最大购买数');
- }
- }
- $modelarea = new CouponAreaModel();
- $area = $modelarea->where('id', $plan['area_id'])->find();
- if (empty($area['buy_time'])){
- throw new Exception('购买失败,购买时间暂未开放');
- }
- if ($area['can_buy'] != 1){
- throw new Exception('购买失败,大区暂未开放,请联系客服');
- }
- $hasintime = false;
- $curdata = json_decode($area['buy_time']);
- $day = sr_getcurtime($curdata->day, 'Y-m-d');
- $arr_time = $curdata->time;
- $curtime = $get_time;
- for ($i = 0; $i < count($arr_time); $i++){
- $val1 = $curdata->time[$i];
- $start_time = strtotime($day.' '.sr_getcurtime($val1->start_time, 'H:i:s'));
- $end_time = strtotime($day.' '.sr_getcurtime($val1->end_time, 'H:i:s'));
- if ($curtime > $start_time && $curtime < $end_time){
- $hasintime = true;
- break;
- }
- }
- if (!$hasintime){
- if ($is_coin == true){
- // if ($userinfo['medal'] < 1){
- // throw new Exception('优先勋章不足,购买失败');
- // }
- $count = Db::name('medal_log')->whereDay('create_at', 'today')->where('uid', $uid)->where('type', 2)->count();
- if ($count >4){
- if (!in_array($uid, [7493267,7493266])){
- throw new Exception('每天使用优先勋章 最多五次');
- }
- }
- }else{
- if (in_array($plan['area_id'], [17,18])){
- sr_throw('还未到时间,暂未开放');
- }else{
- throw new Exception('购买失败,购买时间暂未开放', 478);
- }
- }
- }
- // if (!$is_coin){
- // if (Db::name('coupon_plan_log')->where('plan_id', $plan_id)->where('coin_type', 2)->where('uid', $uid)->count('id') > 4){
- // throw new Exception('每人限购五次');
- // }
- // }
- if ($plan['cur_num'] + $money > $plan['target_num']){
- $maxBuy = $plan['target_num']-$plan['cur_num'];
- if ($maxBuy <= 0){
- throw new Exception('购买失败,请稍后再试', 33333);
- }else{
- throw new Exception('购买失败,'.'最多可购买数量'. strval($maxBuy));
- }
- }
- if ($plan['less_num'] <=0 && $plan['target_num'] > $plan['cur_num']){
- Db::name('coupon_plan')->where('id', $plan_id)->inc('less_num', $plan['target_num'] - $plan['cur_num'])->update();
- }
-
- $userinfo = $m_uer->where('id', $uid)->field('score, is_auth,id,pid,level,path,medal,score_away,income_type')->find();
- if ($is_fl){
- if ($userinfo['score_away'] < $money){
- throw new Exception('购买失败,用户锁定积分不足');
- }
- }else{
- if ($userinfo['score'] < $money){
- throw new Exception('购买失败,用户积分不足');
- }
- }
- if ($is_coin){
- // 判断是否是最大额度的一半
- // if ($money > ($plan['buy_most']/2)){
- // sr_throw('优先勋章最多只能购买'.$plan['buy_most']/2);
- // }
- $total_cur_sum = $plan['target_num']-$plan['less_num'];
- $ed_yiban = $plan['target_num']/2;
- if (!in_array($uid, [7493267,7493266])){
- if ($total_cur_sum >= $ed_yiban){
- sr_throw('优先勋章额度已抢满');
- }
- if ($total_cur_sum + $money > $ed_yiban){
- sr_throw('优先勋章最多只能抢'.($ed_yiban-$total_cur_sum));
- }
- }
- //
- // 消耗勋章 并购买
- if ($userinfo['medal'] < 1){
- throw new Exception('优先勋章不足,购买失败');
- }
- edit_user_medal(2, $uid, 1);
- }
- if ($is_fl){
- edit_user_scoreaway(2, $uid, $money, $plan_id);
- }else{
- edit_user_score(1, $uid, $money, $plan_id);
- }
- // 赠送抢购值
- edit_user_couponnum(1, $uid, intval($money/10));
- self::where('id', $plan_id)->inc('cur_num', intval($money))->update();
- self::where('id', $plan_id)->dec('less_num', intval($money))->update();
- $modelplanlog = new CouponPlanLogModel();
- $modelplanlog->insert([
- 'uid'=>$uid,
- 'plan_id'=>$plan_id,
- 'buy_num'=>$money,
- 'area_id'=>$plan['area_id'],
- 'create_time'=>sr_getcurtime($get_time),
- 'coin_type'=>$is_coin?1:2
- ]);
- // 给用户添加业绩
- incUserPerformance($uid, $money);
- if ($plan['cur_num'] + $money >= $plan['target_num']){
- self::where('id', $plan_id)->save(['state' => 1]);
- Queue::instance()->log('执行成功新增计划')->do("couponAddNewPlan")->job(CouponPlanJob::class)->push([$plan['area_id'], $plan['id'], 1]);
- }
- }
- public function baocang($id){
- $row = self::find($id);
- if (!$row){throw new Exception('数据错误');}
- if ($row['state'] != 0){
- throw new Exception('状态错误');
- }
- $row->save(['state'=>3]);
- Queue::instance()->log('执行成功baocang')->do("couponAddNewPlan")->job(CouponPlanJob::class)->push([$row['area_id'], $id, 2]);
- }
- public function fafang($id){
- $row = self::find($id);
- if (!$row){throw new Exception('数据错误');}
- if ($row['state'] != 1){
- throw new Exception('状态错误');
- }
- $maxPlan = self::where('area_id', $row['area_id'])->where('lun_num', $row['lun_num'])->order('qi_num desc')->select()->toArray();
- if (count($maxPlan) > 0){
- if ($maxPlan[0]['qi_num'] - $row['qi_num'] < 3){
- throw new Exception('发放失败,认购中才第'.$maxPlan[0]['qi_num'].'期');
- }
- }
- // 改变计划状态
- // $row->save(['state'=>2]);
- Queue::instance()->log('执行成功fafang')->do("couponPlanFafang")->job(CouponPlanJob::class)->push([$id]);
- // $modellog = new CouponPlanLogModel();
- // $list = $modellog->where('plan_id', $id)->where('state', 1)->select()->toArray();
- // $scale = env('COUPON.FAFANG_SCALE', '0');
- // foreach ($list as $key=>$val){
- //// edit_user_score(, number_format());
- // $user_de = number_format($val['buy_num']*(1+$scale/100), 0, '.', '');
- // edit_user_score(2, $val['uid'], $user_de);
- // // 修改日志状态
- // $modellog->where('id', $val['id'])->save(['state'=>1]);
- // }
- }
- }
|