123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?php
- /**
- * Created by: PhpStorm
- * User: lytian
- * Date: 2019/9/26
- * Time: 15:05
- */
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\common\library\Redis;
- use app\main\constants\PayConstants;
- use app\main\helper\StringHelper;
- use think\Config;
- class vipactivity extends Backend
- {
- /**
- * @var \app\common\model\Activity
- */
- protected $model = null;
- protected $activity_type = 0;
- protected $business_line = 0;
- //平台活动
- private const ACTIVITY_TYPE_PLATFORM = 0;
- //渠道自定义活动
- private const ACTIVIYT_TYPE_CHANNEL = 1;
- //赠币活动
- private const ACTIVIYT_TYPE_GIVE = 3;
- //APP活动
- private const ACTIVITY_TYPE_APP = 2;
- /**
- * 是否是关联查询
- */
- protected $relationSearch = true;
- protected $noNeedRight = ['index'];
- public function _initialize()
- {
- parent::_initialize();
- $this->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();
- }
- }
|