package com.xjrsoft.module.xycxedu.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.base.entity.BaseSemester;
import com.xjrsoft.module.xycxedu.dto.AddExamPlanDto;
import com.xjrsoft.module.xycxedu.dto.ExamPlanPageDto;
import com.xjrsoft.module.xycxedu.dto.ExamStatisticsPageDto;
import com.xjrsoft.module.xycxedu.dto.UpdateExamPlanDto;
import com.xjrsoft.module.xycxedu.entity.ExamPlan;
import com.xjrsoft.module.xycxedu.entity.XycxeduExamList;
import com.xjrsoft.module.xycxedu.service.IExamPlanService;
import com.xjrsoft.module.xycxedu.service.IExamSubjectScoreService;
import com.xjrsoft.module.xycxedu.service.IXycxeduExamListService;
import com.xjrsoft.module.xycxedu.vo.ExamPlanListVo;
import com.xjrsoft.module.xycxedu.vo.ExamPlanPageVo;
import com.xjrsoft.module.xycxedu.vo.ExamPlanVo;
import com.xjrsoft.module.xycxedu.vo.ExamStatisticsPageVo;
import com.xjrsoft.module.xycxedu.vo.XycxeduExamListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
* @title: 考试计划维护
* @Author dzx
* @Date: 2024-07-18
* @Version 1.0
*/
@RestController
@RequestMapping("/xycxedu" + "/examPlan")
@Api(value = "/xycxedu"  + "/examPlan",tags = "考试计划维护代码")
@AllArgsConstructor
public class ExamPlanController {


    private final IExamPlanService examPlanService;
    private final IXycxeduExamListService examListService;
    private final IExamSubjectScoreService scoreService;
    @GetMapping(value = "/page")
    @ApiOperation(value="考试计划维护列表(分页)")
    @SaCheckPermission("examplan:detail")
    public RT<PageOutput<ExamPlanPageVo>> page(@Valid ExamPlanPageDto dto){

        MPJLambdaWrapper<ExamPlan> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.orderByDesc(ExamPlan::getId)
                .eq(dto.getSemesterId() != null, ExamPlan::getSemesterId, dto.getSemesterId())
                .like(StrUtil.isNotEmpty(dto.getName()), ExamPlan::getName, dto.getName())
                .leftJoin(BaseSemester.class, BaseSemester::getId, ExamPlan::getSemesterId)
                .select(ExamPlan::getId)
                .selectAs(BaseSemester::getName, ExamPlanPageVo::getSemesterName)
                .select(ExamPlan.class,x -> VoToColumnUtil.fieldsToColumns(ExamPlanPageVo.class).contains(x.getProperty()));
        IPage<ExamPlanPageVo> page = examPlanService.selectJoinListPage(ConventPage.getPage(dto), ExamPlanPageVo.class, queryWrapper);
        PageOutput<ExamPlanPageVo> pageOutput = ConventPage.getPageOutput(page, ExamPlanPageVo.class);
        for (ExamPlanPageVo record : pageOutput.getList()) {
            String[] split = record.getMilexamids().split(",");
            List<Long> milexamids = Arrays.asList(split).stream()
                    .map(Long::parseLong)
                    .collect(Collectors.toList());
            List<XycxeduExamList> list = examListService.list(new QueryWrapper<XycxeduExamList>().lambda().in(XycxeduExamList::getMilexamid, milexamids));
            Set<String> collect = list.stream().map(XycxeduExamList::getMilexamname).collect(Collectors.toSet());
            record.setMilexamnames(collect.toString().replace("[", "").replace("]", ""));
        }
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询考试计划维护信息")
    @SaCheckPermission("examplan:detail")
    public RT<ExamPlanVo> info(@RequestParam Long id){
        ExamPlan examPlan = examPlanService.getById(id);
        if (examPlan == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(examPlan, ExamPlanVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增考试计划维护")
    @SaCheckPermission("examplan:add")
    public RT<Boolean> add(@Valid @RequestBody AddExamPlanDto dto){
        ExamPlan examPlan = BeanUtil.toBean(dto, ExamPlan.class);
        boolean isSuccess = examPlanService.save(examPlan);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改考试计划维护")
    @SaCheckPermission("examplan:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateExamPlanDto dto){

        ExamPlan examPlan = BeanUtil.toBean(dto, ExamPlan.class);
        return RT.ok(examPlanService.updateById(examPlan));

    }

    @DeleteMapping
    @ApiOperation(value = "删除考试计划维护")
    @SaCheckPermission("examplan:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(examPlanService.removeBatchByIds(ids));
    }

    @GetMapping(value = "/exam-list")
    @ApiOperation(value="查询所有考试记录")
    @SaCheckPermission("examplan:detail")
    public RT<List<XycxeduExamListVo>> allExam(){
        List<XycxeduExamList> list = examListService.list();
        List<XycxeduExamListVo> listVos = BeanUtil.copyToList(list, XycxeduExamListVo.class);
        return RT.ok(listVos);
    }

    @GetMapping(value = "/plan-list")
    @ApiOperation(value="查询所有考试计划")
    @SaCheckPermission("examplan:detail")
    public RT<List<ExamPlanListVo>> allPlan(){
        List<ExamPlan> list = examPlanService.list();
        List<ExamPlanListVo> listVos = BeanUtil.copyToList(list, ExamPlanListVo.class);
        return RT.ok(listVos);
    }

    @GetMapping(value = "/exam-statistics")
    @ApiOperation(value="考试记录(分页)")
    @SaCheckPermission("examplan:detail")
    public RT<PageOutput<ExamStatisticsPageVo>> page(@Valid ExamStatisticsPageDto dto){
        Page<ExamStatisticsPageVo> page = scoreService.getStatisticsPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<ExamStatisticsPageVo> pageOutput = ConventPage.getPageOutput(page, ExamStatisticsPageVo.class);
        return RT.ok(pageOutput);
    }
}