package com.xjrsoft.module.banding.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.module.banding.dto.BandingTaskClassPageDto;
import com.xjrsoft.module.banding.dto.BandingTaskMajorConditionListDto;
import com.xjrsoft.module.banding.dto.SaveMajorConditionDto;
import com.xjrsoft.module.banding.entity.BandingTask;
import com.xjrsoft.module.banding.service.IBandingTaskMajorConditionService;
import com.xjrsoft.module.banding.service.IBandingTaskService;
import com.xjrsoft.module.banding.vo.BandingTaskMajorConditionListVo;
import com.xjrsoft.module.student.entity.BaseNewStudent;
import com.xjrsoft.module.student.service.IBaseNewStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @title: 新生分班任务
* @Author dzx
* @Date: 2024-07-01
* @Version 1.0
*/
@RestController
@RequestMapping("/banding" + "/bandingTaskMajorCondition")
@Api(value = "/banding"  + "/bandingTaskMajorCondition",tags = "新生分班任务专业限制条件代码")
@AllArgsConstructor
public class BandingTaskMajorConditionController {


    private final IBandingTaskMajorConditionService conditionService;
    private final IBandingTaskService taskService;
    private final IBaseNewStudentService newStudentService;
    @GetMapping(value = "/list")
    @ApiOperation(value="专业限制条件列表")
    @SaCheckPermission("bandingtaskmajorcondition:detail")
    public RT<List<BandingTaskMajorConditionListVo>> page(@Valid BandingTaskMajorConditionListDto dto){
        BandingTask bandingTask = taskService.getById(dto.getBandingTaskId());

        BandingTaskClassPageDto dto2 = new BandingTaskClassPageDto(){{
            setBandingTaskId(bandingTask.getId());
            setEnrollType(bandingTask.getEnrollType());
            setGradeId(bandingTask.getGradeId());
        }};
        List<BaseNewStudent> studentMajorId = conditionService.getStudentMajorId(dto2);
        List<Long> majorIds = new ArrayList<>();
        Set<Long> collect = studentMajorId.stream().filter(x -> x.getFirstAmbition() != null).map(BaseNewStudent::getFirstAmbition).collect(Collectors.toSet());
        majorIds.addAll(collect.stream().collect(Collectors.toList()));

        Set<Long> collect2 = studentMajorId.stream().filter(x -> x.getSecondAmbition() != null).map(BaseNewStudent::getSecondAmbition).collect(Collectors.toSet());
        majorIds.addAll(collect2.stream().collect(Collectors.toList()));
        dto.setMajorIds(majorIds);
        List<BandingTaskMajorConditionListVo> page = conditionService.getList(dto);
        return RT.ok(page);
    }


    @PostMapping("save")
    @ApiOperation(value = "保存")
    @SaCheckPermission("bandingtaskmajorcondition:save")
    public RT<Boolean> add(@Valid @RequestBody SaveMajorConditionDto dto){
        boolean isSuccess = conditionService.save(dto);
        return RT.ok(isSuccess);
    }
}