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

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.entity.Student;
import com.ym.mec.biz.dal.entity.SysConfig;
import com.ym.mec.biz.dal.entity.VipGroupActivity;
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.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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(ActivityQueryDto activityQueryDto){
        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);
    }

}
