Vipactivity.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * Created by: PhpStorm
  4. * User: lytian
  5. * Date: 2019/9/26
  6. * Time: 15:05
  7. */
  8. namespace app\admin\controller;
  9. use app\common\controller\Backend;
  10. use app\common\library\Redis;
  11. use app\main\constants\PayConstants;
  12. use app\main\helper\StringHelper;
  13. use think\Config;
  14. class vipactivity extends Backend
  15. {
  16. /**
  17. * @var \app\common\model\Activity
  18. */
  19. protected $model = null;
  20. protected $activity_type = 0;
  21. protected $business_line = 0;
  22. //平台活动
  23. private const ACTIVITY_TYPE_PLATFORM = 0;
  24. //渠道自定义活动
  25. private const ACTIVIYT_TYPE_CHANNEL = 1;
  26. //赠币活动
  27. private const ACTIVIYT_TYPE_GIVE = 3;
  28. //APP活动
  29. private const ACTIVITY_TYPE_APP = 2;
  30. /**
  31. * 是否是关联查询
  32. */
  33. protected $relationSearch = true;
  34. protected $noNeedRight = ['index'];
  35. public function _initialize()
  36. {
  37. parent::_initialize();
  38. $this->model = model('Activity');
  39. $this->view->assign("statusList", $this->model->getStatusList());
  40. $this->activity_type = self::ACTIVIYT_TYPE_CHANNEL;
  41. $this->assign('activity_type',$this->activity_type);
  42. $this->assignconfig('activity_type',$this->activity_type);
  43. $this->business_line = PayConstants::BUSINESS_WECHAT;
  44. if (in_array($this->activity_type, [2])) {
  45. $this->business_line = PayConstants::BUSINESS_APP;
  46. }
  47. $this->assignconfig('business_line', $this->business_line);
  48. }
  49. /**
  50. * 查看
  51. * @return string|\think\response\Json
  52. * @throws \think\Exception
  53. */
  54. public function index()
  55. {
  56. //设置过滤方法
  57. $this->request->filter(['strip_tags']);
  58. $this->assignconfig('time',$this->time);
  59. $this->assign('time',$this->time);
  60. $this->assign('group',$this->group);
  61. $today = date('Ymd',$this->time);
  62. if ($this->request->isAjax()) {
  63. //如果发送的来源是Selectpage,则转发到Selectpage
  64. if ($this->request->request('pkey_name')) {
  65. return $this->selectpage();
  66. }
  67. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  68. $maps = [
  69. 'admin_id' => $this->auth->id,
  70. //'status' => '1'
  71. ];
  72. $total = $this->model
  73. ->where($maps)
  74. ->where($where)
  75. ->where('type',$this->activity_type)
  76. ->count();
  77. $list = $this->model
  78. ->where($maps)
  79. ->where($where)
  80. ->where('type',$this->activity_type)
  81. ->order($sort, $order)
  82. ->limit($offset, $limit)
  83. ->select();
  84. if ($total) {
  85. foreach ($list as &$row) {
  86. $money = 0;
  87. $allmoney = 0;
  88. //拉取下面所有的活动链接
  89. $slaveRows = model("VipActivityRelation")
  90. ->join('activity', 'activity.id = vip_activity_relation.slave_activity_id')
  91. ->join('activity_collect c','activity.id=c.aid and c.createdate='.$today.' and c.admin_id=0','left')
  92. ->join('activity_collect_all a','activity.id=a.aid and a.admin_id=0','left')
  93. ->field('activity.*,c.money,a.money as allmoney')
  94. ->where($where)
  95. ->where('master_activity_id', 'eq', $row['id'])
  96. ->select();
  97. $channel_ids = '';
  98. if ($slaveRows) {
  99. foreach ($slaveRows as $slaveRow) {
  100. $money = $money + $slaveRow['money'];
  101. $allmoney = $allmoney + $slaveRow['allmoney'];
  102. $channel_ids = $channel_ids . ','. $slaveRow['admin_id'];
  103. }
  104. }
  105. $row['money'] = StringHelper::moneyFormat($money);
  106. $row['allmoney'] = StringHelper::moneyFormat($allmoney);
  107. $row['channel_ids'] = trim($channel_ids, ',');
  108. }
  109. }
  110. $result = array("total" => $total, "rows" => $list);
  111. return json($result);
  112. }
  113. return $this->view->fetch();
  114. }
  115. /**
  116. * 选择
  117. */
  118. public function doChoose($ids = "", $qdsIds = "", $groupIds="")
  119. {
  120. if (empty($ids)) {
  121. Log::info('缺少参数');
  122. return json_encode(['code' => 200]);
  123. }
  124. if ($qdsIds != false){
  125. $qdsIds = trim($qdsIds, ',');
  126. $qdsIds = explode(',', $qdsIds);
  127. }
  128. if ($groupIds != false){
  129. $groupIds = explode(',',trim($groupIds,','));
  130. //获取改组下面的所有渠道ID
  131. $channelArr = model('VipGroupInfo')->field('channel_id')->where('group_id','in',$groupIds)->select();
  132. if ($channelArr != false){
  133. $channelArr = array_column($channelArr,'channel_id');
  134. $qdsIds = $channelArr;
  135. }
  136. }
  137. //过滤一下不是该VIP下账号的
  138. $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();
  139. $success = $fail = [];
  140. if (!empty($rows)) {
  141. $activityConfig = $this->model->where('id', 'eq', $ids)->find();
  142. if ($activityConfig) {
  143. if ($activityConfig['status'] != '1') {
  144. $this->error('VIP活动状态隐藏');
  145. }
  146. } else {
  147. $this->error('VIP活动不存在');
  148. }
  149. $activity_config = [];
  150. if ($activityConfig['type'] == self::ACTIVIYT_TYPE_CHANNEL) {
  151. $redisKeyPrefix = 'AT-M:';
  152. $activity_config = Config::get('site.activity_config');
  153. } else if ($activityConfig['type'] == self::ACTIVIYT_TYPE_GIVE) {
  154. $redisKeyPrefix = 'AT-M-G:';
  155. $activity_config = Config::get('site.activity_give_config');
  156. }
  157. //循环生成
  158. $redis = Redis::instance();
  159. foreach ($rows as $channel) {
  160. $channel_id = $channel['admin_id_slave'];
  161. //是否已创建过
  162. $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();
  163. if ($has) {
  164. continue;
  165. }
  166. $count = intval($redis->hget($redisKeyPrefix.date('Ym'), $channel_id));
  167. $limited_number = intval($activity_config['limited_number'] ?? 0);
  168. if($limited_number <= $count){
  169. $fail[$channel_id] = "渠道{$channel_id}当月只能创建 {$limited_number} 条活动";
  170. continue;
  171. }
  172. $params = $activityConfig->getData();
  173. $params['admin_id'] = $channel_id;
  174. $params['createtime'] = time();
  175. $params['updatetime'] = time();
  176. unset($params['id']);
  177. $activityId = $this->model->allowField(true)->insertGetId($params);
  178. if ($activityId !== false)
  179. {
  180. //保存关系
  181. model("VipActivityRelation")->allowField(true)->insert(['master_admin_id' => $this->auth->id, 'slave_admin_id' => $channel_id, 'master_activity_id' => $ids, 'slave_activity_id' =>$activityId]);
  182. $redis->hIncrBy($redisKeyPrefix.date('Ym'), $channel_id,1);
  183. $redis->expire($redisKeyPrefix.date('Ym'),31*86400);
  184. $success[$channel_id] = '成功啦';
  185. $popKey = "S-P-T-C:N:{$channel_id}";
  186. Redis::instance()->del($popKey);
  187. }
  188. else
  189. {
  190. $fail[$channel_id] = $this->model->getError();
  191. }
  192. }
  193. }
  194. return json_encode(['code' => 200, 'success' => $success, 'fail' => implode(',', $fail)]);
  195. }
  196. /**
  197. * 查看分流链接
  198. * @param string $ids
  199. */
  200. public function viewdata($ids ='')
  201. {
  202. if (empty($ids)) {
  203. $this->error('缺少参数');
  204. }
  205. //设置过滤方法
  206. $this->request->filter(['strip_tags']);
  207. $this->assignconfig('time',$this->time);
  208. $this->assign('time',$this->time);
  209. $this->assign('group',$this->group);
  210. $today = date('Ymd',$this->time);
  211. if ($this->request->isAjax()) {
  212. //如果发送的来源是Selectpage,则转发到Selectpage
  213. if ($this->request->request('pkey_name')) {
  214. return $this->selectpage();
  215. }
  216. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  217. $total = model("VipActivityRelation")
  218. ->join('activity', 'activity.id = vip_activity_relation.slave_activity_id')
  219. ->where($where)
  220. ->where('master_activity_id', 'eq', $ids)
  221. ->count();
  222. $list =model("VipActivityRelation")
  223. ->join('activity', 'activity.id = vip_activity_relation.slave_activity_id')
  224. ->join('activity_collect c','activity.id=c.aid and c.createdate='.$today.' and c.admin_id=0','left')
  225. ->join('activity_collect_all a','activity.id=a.aid and a.admin_id=0','left')
  226. ->field('activity.*,c.money,a.money as allmoney')
  227. ->where($where)
  228. ->where('master_activity_id', 'eq', $ids)
  229. ->field('activity.*')
  230. ->order($sort, $order)
  231. ->limit($offset, $limit)
  232. ->select();
  233. $result = array("total" => $total, "rows" => $list);
  234. return json($result);
  235. }
  236. $this->assignconfig('id', $ids);
  237. return $this->view->fetch();
  238. }
  239. }