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.dao.StudentDao;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.dto.VipGroupActivityAddDto;
import com.ym.mec.biz.dal.entity.Student;
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.util.CollectionUtils;
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;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private StudentDao studentDao;

    @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, String studentIds, boolean onlySpecialActivity){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("用户信息获取失败");
        }

        if(StringUtils.isBlank(studentIds)){
            return failed("请选择学员");
        }
        List<Integer> userIds = Arrays.stream(studentIds.split(",")).map(id -> Integer.valueOf(id)).collect(Collectors.toList());
        List<Student> students = studentDao.findByStudentIds(userIds);
        if(CollectionUtils.isEmpty(students)||students.size()!=userIds.size()){
            return failed("学员信息不存在");
        }
        long newStudentNum = students.stream().filter(s -> s.getIsNewUser()).count();

        Integer applyToStudentType = -1;
        if(newStudentNum==0){
            applyToStudentType = 0;
        }else if(newStudentNum==userIds.size()){
            applyToStudentType = 1;
        }

        SysUser student = teacherDao.getUser(userIds.get(0));
        if(Objects.isNull(student)){
            return failed("学员信息不存在");
        }
        String organIds = student.getOrganId().toString();

        List<VipGroupActivity> vipGroupActivities = vipGroupActivityService.findByVipGroupCategory(categoryId, organIds, sysUser.getId(),applyToStudentType, onlySpecialActivity?true:null);
        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.nonNull(sysConfig)&&StringUtils.isNotBlank(sysConfig.getParanValue())){
                Set<Integer> activityIds = Arrays.stream(sysConfig.getParanValue().split(",")).map(id->Integer.valueOf(id)).collect(Collectors.toSet());
                Iterator<VipGroupActivity> iterator = vipGroupActivities.iterator();
                while(iterator.hasNext()){
                    VipGroupActivity vipGroupActivity = iterator.next();
                    if(activityIds.contains(vipGroupActivity.getId())){
                        iterator.remove();
                    }
                }
            }
            return succeed(vipGroupActivities);
        }
    }

}
