package com.ym.mec.web.controller.education;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.EmployeeDao;
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.Employee;
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 com.ym.mec.common.exception.BizException;
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.security.access.prepost.PreAuthorize;
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("eduVipGroupActivity")
@RestController
public class EduVipGroupActivityController extends BaseController {

    @Autowired
    private VipGroupActivityService vipGroupActivityService;
	@Autowired
    private TeacherDao teacherDao;
	@Autowired
    private StudentDao studentDao;
	@Autowired
    private SysConfigService sysConfigService;

    @ApiOperation(value = "根据课程类型获取对应课程活动方案")
    @GetMapping("/findByVipGroupCategory")
    @PreAuthorize("@pcs.hasPermissions('vipGroupActivity/findByVipGroupCategory')")
    public Object findByVipGroupCategory(Long categoryId, String studentIds, Integer teacherId){
        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 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, teacherId, applyToStudentType, null);
        Iterator<VipGroupActivity> iterator = vipGroupActivities.iterator();
        while (iterator.hasNext()){
            VipGroupActivity vipGroupActivity = iterator.next();
            if(!activityIds.contains(vipGroupActivity.getId())){
                iterator.remove();
            }
        }
        return succeed(vipGroupActivities);
    }

}
