package com.ym.mec.teacher.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dto.VipGroupActivityAddDto;
import com.ym.mec.biz.dal.entity.SysConfig;
import com.ym.mec.biz.dal.entity.VipGroupActivity;
import com.ym.mec.biz.dal.page.VipGroupActivityQueryInfo;
import com.ym.mec.biz.service.SysConfigService;
import com.ym.mec.biz.service.VipGroupActivityService;
import com.ym.mec.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author Joburgess
 * @Date 2019/10/1
 */

@Api(tags = "vip课活动")
@RequestMapping("vipGroupActivity")
@RestController
public class VipGroupActivityController extends BaseController {

    @Autowired
    private VipGroupActivityService vipGroupActivityService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private SysConfigService sysConfigService;

    @ApiOperation(value = "新增vip课活动方案")
    @PostMapping("/addVipGroupActivity")
    public Object addVipGroupActivity(@RequestBody VipGroupActivityAddDto vipGroupActivityAddDto){
        vipGroupActivityService.addVipGroupActivity(vipGroupActivityAddDto);
        return succeed();
    }

    @ApiOperation(value = "分页查询活动方案")
    @GetMapping("/queryPage")
    public Object queryPage(VipGroupActivityQueryInfo queryInfo){
        return succeed(vipGroupActivityService.queryPage(queryInfo));
    }

    @ApiOperation(value = "修改活动方案")
    @PostMapping("/update")
    public Object update(@RequestBody VipGroupActivityAddDto vipGroupActivityAddDto){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("用户信息获取失败");
        }
        vipGroupActivityService.updateVipGroupActivity(vipGroupActivityAddDto, sysUser.getId());
        return succeed();
    }

    @ApiOperation(value = "根据课程类型获取对应课程活动方案")
    @GetMapping("/findByVipGroupCategory")
    public Object findByVipGroupCategory(Long categoryId, boolean onlySpecialActivity){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("用户信息获取失败");
        }
        List<VipGroupActivity> vipGroupActivities = vipGroupActivityService.findByVipGroupCategory(categoryId, sysUser.getId());
        SysConfig sysConfig = sysConfigService.findByParamName(SysConfigService.VIP_SPECIAL_ACTIVITY_IDS);
        if(onlySpecialActivity){
            if(Objects.isNull(sysConfig)|| StringUtils.isBlank(sysConfig.getParanValue())){
                return succeed(Collections.EMPTY_LIST);
            }
            Set<Integer> specialActivityIds = Arrays.stream(sysConfig.getParanValue().split(",")).map(Integer::valueOf).collect(Collectors.toSet());
            List<VipGroupActivity> results = vipGroupActivities.stream().filter(e -> specialActivityIds.contains(e.getId())).collect(Collectors.toList());
            return succeed(results);
        }else{
            if(Objects.isNull(sysConfig)|| StringUtils.isBlank(sysConfig.getParanValue())){
                return succeed(vipGroupActivities);
            }
            Set<Integer> specialActivityIds = Arrays.stream(sysConfig.getParanValue().split(",")).map(Integer::valueOf).collect(Collectors.toSet());
            List<VipGroupActivity> results = vipGroupActivities.stream().filter(e -> !specialActivityIds.contains(e.getId())).collect(Collectors.toList());
            return succeed(results);
        }
    }

}
