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.student.dto.AddEnrollmentPlanGraduateSchoolDto;
import com.xjrsoft.module.student.dto.EnrollmentPlanGraduateSchoolPageDto;
import com.xjrsoft.module.student.dto.UpdateEnrollmentPlanGraduateSchoolDto;
import com.xjrsoft.module.student.entity.EnrollmentPlanGraduateSchool;
import com.xjrsoft.module.student.service.IEnrollmentPlanGraduateSchoolService;
import com.xjrsoft.module.student.vo.EnrollmentPlanGraduateSchoolPageVo;
import com.xjrsoft.module.student.vo.EnrollmentPlanGraduateSchoolVo;
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" + "/enrollmentPlanGraduateSchool")
@Api(value = "/student"  + "/enrollmentPlanGraduateSchool",tags = "毕业学校招生计划详情代码")
@AllArgsConstructor
public class EnrollmentPlanGraduateSchoolController {


    private final IEnrollmentPlanGraduateSchoolService enrollmentPlanGraduateSchoolService;


    @GetMapping(value = "/page")
    @ApiOperation(value="毕业学校招生计划详情列表(分页)")
    @SaCheckPermission("enrollmentplangraduateschool:detail")
    public RT<PageOutput<EnrollmentPlanGraduateSchoolPageVo>> page(@Valid EnrollmentPlanGraduateSchoolPageDto dto){

        MPJLambdaWrapper<EnrollmentPlanGraduateSchool> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .select(EnrollmentPlanGraduateSchool::getId)
                .select("t1.name as baseGraduateSchoolIdCn")
                .select(EnrollmentPlanGraduateSchool.class,x -> VoToColumnUtil.fieldsToColumns(EnrollmentPlanGraduateSchoolPageVo.class).contains(x.getProperty()))
                .leftJoin("base_graduate_school t1 on t1.id = t.base_graduate_school_id")
                .orderByDesc(EnrollmentPlanGraduateSchool::getCreateDate);
        IPage<EnrollmentPlanGraduateSchoolPageVo> page = enrollmentPlanGraduateSchoolService.selectJoinListPage(ConventPage.getPage(dto), EnrollmentPlanGraduateSchoolPageVo.class, queryWrapper);
        PageOutput<EnrollmentPlanGraduateSchoolPageVo> pageOutput = ConventPage.getPageOutput(page, EnrollmentPlanGraduateSchoolPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询毕业学校招生计划详情信息")
    @SaCheckPermission("enrollmentplangraduateschool:detail")
    public RT<EnrollmentPlanGraduateSchoolVo> info(@RequestParam Long id){
        EnrollmentPlanGraduateSchool enrollmentPlanGraduateSchool = enrollmentPlanGraduateSchoolService.getById(id);
        if (enrollmentPlanGraduateSchool == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(enrollmentPlanGraduateSchool, EnrollmentPlanGraduateSchoolVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增毕业学校招生计划详情")
    @SaCheckPermission("enrollmentplangraduateschool:add")
    public RT<Boolean> add(@Valid @RequestBody AddEnrollmentPlanGraduateSchoolDto dto){
        EnrollmentPlanGraduateSchool enrollmentPlanGraduateSchool = BeanUtil.toBean(dto, EnrollmentPlanGraduateSchool.class);
        boolean isSuccess = enrollmentPlanGraduateSchoolService.save(enrollmentPlanGraduateSchool);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改毕业学校招生计划详情")
    @SaCheckPermission("enrollmentplangraduateschool:edit")
    public RT<Boolean> update(@Valid @RequestBody List<UpdateEnrollmentPlanGraduateSchoolDto> dtos){

        List<EnrollmentPlanGraduateSchool> enrollmentPlanGraduateSchools = new ArrayList<>();
        for(UpdateEnrollmentPlanGraduateSchoolDto dto : dtos){
            EnrollmentPlanGraduateSchool enrollmentPlanGraduateSchool = BeanUtil.toBean(dto, EnrollmentPlanGraduateSchool.class);
            enrollmentPlanGraduateSchools.add(enrollmentPlanGraduateSchool);
        }

        return RT.ok(enrollmentPlanGraduateSchoolService.updateBatchById(enrollmentPlanGraduateSchools));

    }

    @DeleteMapping
    @ApiOperation(value = "删除毕业学校招生计划详情")
    @SaCheckPermission("enrollmentplangraduateschool:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(enrollmentPlanGraduateSchoolService.removeBatchByIds(ids));

    }

}