package com.xjrsoft.module.student.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.BaseMajorSet;
import com.xjrsoft.module.organization.entity.Department;
import com.xjrsoft.module.student.dto.AddEnrollmentPlanMajorSetDto;
import com.xjrsoft.module.student.dto.BaseGraduateSchoolOfEnrollmentPlanListDto;
import com.xjrsoft.module.student.dto.BaseMajorSetOfEnrollmentPlanPageDto;
import com.xjrsoft.module.student.dto.EnrollmentPlanMajorSetPageDto;
import com.xjrsoft.module.student.dto.UpdateEnrollmentPlanMajorSetDto;
import com.xjrsoft.module.student.entity.EnrollmentPlanMajorSet;
import com.xjrsoft.module.student.service.IEnrollmentPlanMajorSetService;
import com.xjrsoft.module.student.vo.BaseGraduateSchoolOfEnrollmentPlanListVo;
import com.xjrsoft.module.student.vo.BaseMajorSetOfEnrollmentPlanPageVo;
import com.xjrsoft.module.student.vo.EnrollmentPlanMajorSetPageVo;
import com.xjrsoft.module.student.vo.EnrollmentPlanMajorSetVo;
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.ArrayList;
import java.util.List;

/**
* @title: 专业方向招生计划详情
* @Author phoenix
* @Date: 2024-06-13
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/enrollmentPlanMajorSet")
@Api(value = "/student"  + "/enrollmentPlanMajorSet",tags = "专业方向招生计划详情代码")
@AllArgsConstructor
public class EnrollmentPlanMajorSetController {


    private final IEnrollmentPlanMajorSetService enrollmentPlanMajorSetService;

    @GetMapping(value = "/generated-relation-page")
    @ApiOperation(value="已生成招生专业方向+毕业中学关系列表(分页)")
    @SaCheckPermission("enrollmentplandetail:detail")
    public RT<PageOutput<EnrollmentPlanMajorSetPageVo>> generatedRelationPage(@Valid EnrollmentPlanMajorSetPageDto dto){

        MPJLambdaWrapper<EnrollmentPlanMajorSet> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .select(EnrollmentPlanMajorSet::getId)
                .selectAs(BaseMajorSet::getStudyYear, EnrollmentPlanMajorSetPageVo::getStudyYear)
                .selectAs(BaseMajorSet::getName, EnrollmentPlanMajorSetPageVo::getBaseMajorSetIdCn)
                .select("t3.name as baseMajorGradationIdCn")
                .selectAs(Department::getName, EnrollmentPlanMajorSetPageVo::getDepartmentIdCn)
                .select("(select count(*) from enrollment_plan_graduate_school a where a.delete_mark = 0 and a.enrollment_plan_major_set_id = t.id) as graduateSchoolSum")
                .select(EnrollmentPlanMajorSet.class,x -> VoToColumnUtil.fieldsToColumns(EnrollmentPlanMajorSetPageVo.class).contains(x.getProperty()))
                .leftJoin(BaseMajorSet.class, BaseMajorSet::getId, EnrollmentPlanMajorSet::getBaseMajorSetId)
                .leftJoin(Department.class, Department::getId, BaseMajorSet::getDepartmentId)
                .leftJoin("base_major_gradation t3 on t3.id = t1.major_gradation_id")
                .eq(dto.getEnrollmentPlanId() != null && dto.getEnrollmentPlanId() > 0, EnrollmentPlanMajorSet::getEnrollmentPlanId, dto.getEnrollmentPlanId())
                .eq(dto.getDepartmentId() != null && dto.getDepartmentId() > 0, BaseMajorSet::getDepartmentId, dto.getDepartmentId())
                .like(dto.getBaseMajorSetIdCn() != null && !dto.getBaseMajorSetIdCn().isEmpty(), BaseMajorSet::getName, dto.getBaseMajorSetIdCn())
                .orderByDesc(EnrollmentPlanMajorSet::getCreateDate);
        IPage<EnrollmentPlanMajorSetPageVo> page = enrollmentPlanMajorSetService.selectJoinListPage(ConventPage.getPage(dto), EnrollmentPlanMajorSetPageVo.class, queryWrapper);
        PageOutput<EnrollmentPlanMajorSetPageVo> pageOutput = ConventPage.getPageOutput(page, EnrollmentPlanMajorSetPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/baseMajorSet-page")
    @ApiOperation(value="专业分类(分页)")
    @SaCheckPermission("enrollmentplandetail:detail")
    public RT<PageOutput<BaseMajorSetOfEnrollmentPlanPageVo>> baseMajorSetOfEnrollmentPlan(@Valid BaseMajorSetOfEnrollmentPlanPageDto dto){
        IPage<BaseMajorSetOfEnrollmentPlanPageVo> page = enrollmentPlanMajorSetService.baseMajorSetOfEnrollmentPlan(dto);
        PageOutput<BaseMajorSetOfEnrollmentPlanPageVo> pageOutput = ConventPage.getPageOutput(page, BaseMajorSetOfEnrollmentPlanPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/baseGraduateSchool-list")
    @ApiOperation(value="毕业学校")
    @SaCheckPermission("enrollmentplandetail:detail")
    public RT<List<BaseGraduateSchoolOfEnrollmentPlanListVo>> baseGraduateSchoolOfEnrollmentPlan(@Valid BaseGraduateSchoolOfEnrollmentPlanListDto dto){
        List<BaseGraduateSchoolOfEnrollmentPlanListVo> result = enrollmentPlanMajorSetService.baseGraduateSchoolOfEnrollmentPlan(dto);
        return RT.ok(result);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询专业方向招生计划详情信息")
    @SaCheckPermission("enrollmentplanmajorset:detail")
    public RT<EnrollmentPlanMajorSetVo> info(@RequestParam Long id){
        EnrollmentPlanMajorSet enrollmentPlanMajorSet = enrollmentPlanMajorSetService.getById(id);
        if (enrollmentPlanMajorSet == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(enrollmentPlanMajorSet, EnrollmentPlanMajorSetVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增专业方向招生计划详情")
    @SaCheckPermission("enrollmentplanmajorset:add")
    public RT<Boolean> add(@Valid @RequestBody AddEnrollmentPlanMajorSetDto dto){
        boolean isSuccess = enrollmentPlanMajorSetService.add(dto);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改专业方向招生计划详情")
    @SaCheckPermission("enrollmentplanmajorset:edit")
    public RT<Boolean> update(@Valid @RequestBody List<UpdateEnrollmentPlanMajorSetDto> dtos){

        List<EnrollmentPlanMajorSet> enrollmentPlanMajorSets = new ArrayList<>();
        for (UpdateEnrollmentPlanMajorSetDto dto : dtos){
            EnrollmentPlanMajorSet enrollmentPlanMajorSet = BeanUtil.toBean(dto, EnrollmentPlanMajorSet.class);
            enrollmentPlanMajorSets.add(enrollmentPlanMajorSet);
        }

        return RT.ok(enrollmentPlanMajorSetService.updateBatchById(enrollmentPlanMajorSets));
    }

    @DeleteMapping
    @ApiOperation(value = "删除专业方向招生计划详情")
    @SaCheckPermission("enrollmentplanmajorset:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(enrollmentPlanMajorSetService.removeBatch(ids));
    }

}