package com.ym.mec.teacher.controller;

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.ActivityQueryDto;
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.ActivityStudentQueryInfo;
import com.ym.mec.biz.dal.page.VipGroupActivityQueryInfo;
import com.ym.mec.biz.service.SysConfigService;
import com.ym.mec.biz.service.SysUserService;
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 SysUserService sysUserService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private StudentDao studentDao;

    @ApiOperation(value = "获取老师待排课的活动方案列表")
    @RequestMapping("/queryWaitCourseActivity")
    public Object queryWaitCourseActivity(){
        return succeed(vipGroupActivityService.queryWaitCourseActivity(sysUserService.getUserId(),null));
    }

    @ApiOperation(value = "获取活动待排课学员数量")
    @RequestMapping("/getActivityWaitCourseStudentNum")
    public Object getActivityWaitCourseStudentNum(Integer activityId){
        return succeed(vipGroupActivityService.getActivityWaitCourseStudentNum(sysUserService.getUserId(),activityId));
    }

    @ApiOperation(value = "获取活动学员课排课次数")
    @RequestMapping("/getActivityStudentCanCourseNum")
    public Object getActivityStudentCanCourseNum(ActivityStudentQueryInfo queryInfo){
        queryInfo.setUserId(sysUserService.getUserId());
        return succeed(vipGroupActivityService.getActivityStudentCanCourseNum(queryInfo));
    }

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

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

    @ApiOperation(value = "修改活动方案")
    @PostMapping("/update")
    public Object update(@RequestBody VipGroupActivityAddDto vipGroupActivityAddDto){
        vipGroupActivityService.updateVipGroupActivity(vipGroupActivityAddDto, sysUserService.getUserId());
        return succeed();
    }

    @ApiOperation(value = "根据课程类型获取对应课程活动方案")
    @GetMapping("/findByVipGroupCategory")
    public Object findByVipGroupCategory(ActivityQueryDto activityQueryDto){
        activityQueryDto.setTeacherId(sysUserService.getUserId());
        String studentIds = activityQueryDto.getStudentIds();
        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 newStudentNumCount = students.stream().filter(s -> s.getIsNewUser()).count();
        long memberCount = students.stream().filter(e -> e.getMemberRankSettingId() != null).count();

        if(newStudentNumCount == 0){
            activityQueryDto.setNewStudentNum(1);
        }else if(newStudentNumCount == userIds.size()){
            activityQueryDto.setNewStudentNum(0);
        }
        if(memberCount == 0){
            activityQueryDto.setMemberNum(1);
        }else if(memberCount == userIds.size()){
            activityQueryDto.setMemberNum(0);
        }

        SysUser student = teacherDao.getUser(userIds.get(0));
        if(Objects.isNull(student)){
            return failed("学员信息不存在");
        }
        activityQueryDto.setOrganId(student.getOrganId().toString());
        activityQueryDto.setIncludeClosed(activityQueryDto.isOnlySpecialActivity()?true:null);
        activityQueryDto.setEnable(true);
        List<VipGroupActivity> vipGroupActivities = vipGroupActivityService.findByVipGroupCategory(activityQueryDto);
        SysConfig sysConfig = sysConfigService.findByParamName(SysConfigService.VIP_SPECIAL_ACTIVITY_IDS);
        if(activityQueryDto.isOnlySpecialActivity()){
            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);
        }
    }

    @ApiOperation(value = "获取六一活动方案")
    @GetMapping("/getChildrenDayActivitys")
    public Object findByVipGroupCategory1(ActivityQueryDto activityQueryDto){
        activityQueryDto.setTeacherId(sysUserService.getUserId());
        String studentIds = activityQueryDto.getStudentIds();
        if(StringUtils.isBlank(studentIds)){
            return failed("请选择学员");
        }

        SysConfig activityIdConfig = sysConfigService.findByParamName(SysConfigService.CHILDREN_DAY_VIP_ACTIVITY_IDS);
        if(Objects.isNull(activityIdConfig)||StringUtils.isBlank(activityIdConfig.getParanValue())){
            return succeed();
        }

        Set<Integer> activityIds = Arrays.stream(activityIdConfig.getParanValue().split(",")).map(id->Integer.valueOf(id)).collect(Collectors.toSet());

        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 newStudentNumCount = students.stream().filter(s -> s.getIsNewUser()).count();
        long memberCount = students.stream().filter(e -> e.getMemberRankSettingId() != null).count();

        if(newStudentNumCount == 0){
            activityQueryDto.setNewStudentNum(1);
        }else if(newStudentNumCount == userIds.size()){
            activityQueryDto.setNewStudentNum(0);
        }
        if(memberCount == 0){
            activityQueryDto.setMemberNum(1);
        }else if(memberCount == userIds.size()){
            activityQueryDto.setMemberNum(0);
        }

        SysUser student = teacherDao.getUser(userIds.get(0));
        if(Objects.isNull(student)){
            return failed("学员信息不存在");
        }
        activityQueryDto.setOrganId(student.getOrganId().toString());
        activityQueryDto.setEnable(true);
        List<VipGroupActivity> vipGroupActivities = vipGroupActivityService.findByVipGroupCategory(activityQueryDto);
        Iterator<VipGroupActivity> iterator = vipGroupActivities.iterator();
        while (iterator.hasNext()){
            VipGroupActivity vipGroupActivity = iterator.next();
            if(!activityIds.contains(vipGroupActivity.getId())){
                iterator.remove();
            }
        }
        return succeed(vipGroupActivities);
    }

}
