package com.xjrsoft.module.student.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.student.dto.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.xjrsoft.module.student.vo.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.springframework.http.ResponseEntity;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

import com.xjrsoft.module.student.entity.PbCseFeeobjupdate;
import com.xjrsoft.module.student.service.IPbCseFeeobjupdateService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* @title: 
* @Author szs
* @Date: 2024-03-29
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/pbCseFeeobjupdate")
@Api(value = "/student"  + "/pbCseFeeobjupdate",tags = "攀宝学生信息")
@AllArgsConstructor
public class PbCseFeeobjupdateController {


    private final IPbCseFeeobjupdateService pbCseFeeobjupdateService;

    @GetMapping(value = "/enrollment-statistics")
    @ApiOperation(value="招生统计")
    @SaCheckPermission("pbcsefeeobjupdate:detail")
    public RT<EnrollmentStatisticsInfoVo> getEnrollmentStatisticsInfo(@Valid EnrollmentStatisticsInfoDto dto){
        EnrollmentStatisticsInfoVo enrollmentStatisticsInfoVo = pbCseFeeobjupdateService.getEnrollmentStatisticsInfo(dto);
        return RT.ok(enrollmentStatisticsInfoVo);
    }

    @GetMapping(value = "/enrollment-statistics-calendar")
    @ApiOperation(value="招生统计每日动态数据")
    @SaCheckPermission("pbcsefeeobjupdate:detail")
    public RT<EnrollmentStatisticsCalendarInfoVo> getEnrollmentStatisticsCalendarInfo(@Valid EnrollmentStatisticsCalendarInfoDto dto){
        EnrollmentStatisticsCalendarInfoVo enrollmentStatisticsCalendarInfoVo = pbCseFeeobjupdateService.getEnrollmentStatisticsCalendarInfo(dto);
        return RT.ok(enrollmentStatisticsCalendarInfoVo);
    }

    @GetMapping(value = "/enrollment-statistics-graduation")
    @ApiOperation(value="招生统计毕业学校数据")
    @SaCheckPermission("pbcsefeeobjupdate:detail")
    public RT<EnrollmentStatisticsGraduationInfoVo> getEnrollmentStatisticsGraduationInfo(@Valid EnrollmentStatisticsGraduationInfoDto dto){
        EnrollmentStatisticsGraduationInfoVo enrollmentStatisticsGraduationInfoVo = pbCseFeeobjupdateService.getEnrollmentStatisticsGraduationInfo(dto);
        return RT.ok(enrollmentStatisticsGraduationInfoVo);
    }

    @GetMapping(value = "/page")
    @ApiOperation(value="列表(分页)")
    @SaCheckPermission("pbcsefeeobjupdate:detail")
    public RT<PageOutput<PbCseFeeobjupdatePageVo>> page(@Valid PbCseFeeobjupdatePageDto dto){

        LambdaQueryWrapper<PbCseFeeobjupdate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                    //.orderByDesc(PbCseFeeobjupdate::getId)
                .select(PbCseFeeobjupdate.class,x -> VoToColumnUtil.fieldsToColumns(PbCseFeeobjupdatePageVo.class).contains(x.getProperty()));
        IPage<PbCseFeeobjupdate> page = pbCseFeeobjupdateService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<PbCseFeeobjupdatePageVo> pageOutput = ConventPage.getPageOutput(page, PbCseFeeobjupdatePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询信息")
    @SaCheckPermission("pbcsefeeobjupdate:detail")
    public RT<PbCseFeeobjupdateVo> info(@RequestParam Long id){
        PbCseFeeobjupdate pbCseFeeobjupdate = pbCseFeeobjupdateService.getById(id);
        if (pbCseFeeobjupdate == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(pbCseFeeobjupdate, PbCseFeeobjupdateVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增")
    @SaCheckPermission("pbcsefeeobjupdate:add")
    public RT<Boolean> add(@Valid @RequestBody AddPbCseFeeobjupdateDto dto){
        PbCseFeeobjupdate pbCseFeeobjupdate = BeanUtil.toBean(dto, PbCseFeeobjupdate.class);
        boolean isSuccess = pbCseFeeobjupdateService.save(pbCseFeeobjupdate);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    @SaCheckPermission("pbcsefeeobjupdate:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdatePbCseFeeobjupdateDto dto){

        PbCseFeeobjupdate pbCseFeeobjupdate = BeanUtil.toBean(dto, PbCseFeeobjupdate.class);
        return RT.ok(pbCseFeeobjupdateService.updateById(pbCseFeeobjupdate));

    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @SaCheckPermission("pbcsefeeobjupdate:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(pbCseFeeobjupdateService.removeBatchByIds(ids));

    }
    @PostMapping("/import")
    @ApiOperation(value = "导入")
    public RT<Boolean> importData(@RequestParam MultipartFile file) throws IOException {
        List<PbCseFeeobjupdatePageVo> savedDataList = EasyExcel.read(file.getInputStream()).head(PbCseFeeobjupdatePageVo.class).sheet().doReadSync();
        Boolean result = pbCseFeeobjupdateService.saveBatch(BeanUtil.copyToList(savedDataList, PbCseFeeobjupdate.class));
        return RT.ok(result);
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public ResponseEntity<byte[]> exportData(@Valid PbCseFeeobjupdatePageDto dto, @RequestParam(defaultValue = "false") Boolean isTemplate) {
        List<PbCseFeeobjupdatePageVo> customerList = isTemplate != null && isTemplate ? new ArrayList<>() : ((PageOutput<PbCseFeeobjupdatePageVo>) page(dto).getData()).getList();
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, PbCseFeeobjupdatePageVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(customerList);

        return RT.fileStream(bot.toByteArray(), "PbCseFeeobjupdate" + ExcelTypeEnum.XLSX.getValue());
    }

}