model = model('Activity'); $this->view->assign("statusList", $this->model->getStatusList()); $this->activity_type = self::ACTIVIYT_TYPE_CHANNEL; $this->assign('activity_type',$this->activity_type); $this->assignconfig('activity_type',$this->activity_type); $this->business_line = PayConstants::BUSINESS_WECHAT; if (in_array($this->activity_type, [2])) { $this->business_line = PayConstants::BUSINESS_APP; } $this->assignconfig('business_line', $this->business_line); } /** * 查看 * @return string|\think\response\Json * @throws \think\Exception */ public function index() { //设置过滤方法 $this->request->filter(['strip_tags']); $this->assignconfig('time',$this->time); $this->assign('time',$this->time); $this->assign('group',$this->group); $today = date('Ymd',$this->time); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $maps = [ 'admin_id' => $this->auth->id, //'status' => '1' ]; $total = $this->model ->where($maps) ->where($where) ->where('type',$this->activity_type) ->count(); $list = $this->model ->where($maps) ->where($where) ->where('type',$this->activity_type) ->order($sort, $order) ->limit($offset, $limit) ->select(); if ($total) { foreach ($list as &$row) { $money = 0; $allmoney = 0; //拉取下面所有的活动链接 $slaveRows = model("VipActivityRelation") ->join('activity', 'activity.id = vip_activity_relation.slave_activity_id') ->join('activity_collect c','activity.id=c.aid and c.createdate='.$today.' and c.admin_id=0','left') ->join('activity_collect_all a','activity.id=a.aid and a.admin_id=0','left') ->field('activity.*,c.money,a.money as allmoney') ->where($where) ->where('master_activity_id', 'eq', $row['id']) ->select(); $channel_ids = ''; if ($slaveRows) { foreach ($slaveRows as $slaveRow) { $money = $money + $slaveRow['money']; $allmoney = $allmoney + $slaveRow['allmoney']; $channel_ids = $channel_ids . ','. $slaveRow['admin_id']; } } $row['money'] = StringHelper::moneyFormat($money); $row['allmoney'] = StringHelper::moneyFormat($allmoney); $row['channel_ids'] = trim($channel_ids, ','); } } $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } /** * 选择 */ public function doChoose($ids = "", $qdsIds = "", $groupIds="") { if (empty($ids)) { Log::info('缺少参数'); return json_encode(['code' => 200]); } if ($qdsIds != false){ $qdsIds = trim($qdsIds, ','); $qdsIds = explode(',', $qdsIds); } if ($groupIds != false){ $groupIds = explode(',',trim($groupIds,',')); //获取改组下面的所有渠道ID $channelArr = model('VipGroupInfo')->field('channel_id')->where('group_id','in',$groupIds)->select(); if ($channelArr != false){ $channelArr = array_column($channelArr,'channel_id'); $qdsIds = $channelArr; } } //过滤一下不是该VIP下账号的 $rows = model('VipAdminBind')->field("admin_id_slave")->where('admin_id_master', 'eq', $this->auth->id)->where('admin_id_slave', 'in', $qdsIds)->where('flag', 'eq', 1)->select(); $success = $fail = []; if (!empty($rows)) { $activityConfig = $this->model->where('id', 'eq', $ids)->find(); if ($activityConfig) { if ($activityConfig['status'] != '1') { $this->error('VIP活动状态隐藏'); } } else { $this->error('VIP活动不存在'); } $activity_config = []; if ($activityConfig['type'] == self::ACTIVIYT_TYPE_CHANNEL) { $redisKeyPrefix = 'AT-M:'; $activity_config = Config::get('site.activity_config'); } else if ($activityConfig['type'] == self::ACTIVIYT_TYPE_GIVE) { $redisKeyPrefix = 'AT-M-G:'; $activity_config = Config::get('site.activity_give_config'); } //循环生成 $redis = Redis::instance(); foreach ($rows as $channel) { $channel_id = $channel['admin_id_slave']; //是否已创建过 $has = model("VipActivityRelation")->field('id')->where('master_admin_id', 'eq', $this->auth->id)->where('slave_admin_id', 'eq', $channel_id)->where('master_activity_id', 'eq', $ids)->find(); if ($has) { continue; } $count = intval($redis->hget($redisKeyPrefix.date('Ym'), $channel_id)); $limited_number = intval($activity_config['limited_number'] ?? 0); if($limited_number <= $count){ $fail[$channel_id] = "渠道{$channel_id}当月只能创建 {$limited_number} 条活动"; continue; } $params = $activityConfig->getData(); $params['admin_id'] = $channel_id; $params['createtime'] = time(); $params['updatetime'] = time(); unset($params['id']); $activityId = $this->model->allowField(true)->insertGetId($params); if ($activityId !== false) { //保存关系 model("VipActivityRelation")->allowField(true)->insert(['master_admin_id' => $this->auth->id, 'slave_admin_id' => $channel_id, 'master_activity_id' => $ids, 'slave_activity_id' =>$activityId]); $redis->hIncrBy($redisKeyPrefix.date('Ym'), $channel_id,1); $redis->expire($redisKeyPrefix.date('Ym'),31*86400); $success[$channel_id] = '成功啦'; $popKey = "S-P-T-C:N:{$channel_id}"; Redis::instance()->del($popKey); } else { $fail[$channel_id] = $this->model->getError(); } } } return json_encode(['code' => 200, 'success' => $success, 'fail' => implode(',', $fail)]); } /** * 查看分流链接 * @param string $ids */ public function viewdata($ids ='') { if (empty($ids)) { $this->error('缺少参数'); } //设置过滤方法 $this->request->filter(['strip_tags']); $this->assignconfig('time',$this->time); $this->assign('time',$this->time); $this->assign('group',$this->group); $today = date('Ymd',$this->time); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $total = model("VipActivityRelation") ->join('activity', 'activity.id = vip_activity_relation.slave_activity_id') ->where($where) ->where('master_activity_id', 'eq', $ids) ->count(); $list =model("VipActivityRelation") ->join('activity', 'activity.id = vip_activity_relation.slave_activity_id') ->join('activity_collect c','activity.id=c.aid and c.createdate='.$today.' and c.admin_id=0','left') ->join('activity_collect_all a','activity.id=a.aid and a.admin_id=0','left') ->field('activity.*,c.money,a.money as allmoney') ->where($where) ->where('master_activity_id', 'eq', $ids) ->field('activity.*') ->order($sort, $order) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list); return json($result); } $this->assignconfig('id', $ids); return $this->view->fetch(); } }