package com.xjrsoft.module.student.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
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.AddPbCseFeeitem2Dto;
import com.xjrsoft.module.student.dto.UpdatePbCseFeeitem2Dto;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
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.dto.PbCseFeeitem2PageDto;
import com.xjrsoft.module.student.entity.PbCseFeeitem2;
import com.xjrsoft.module.student.service.IPbCseFeeitem2Service;
import com.xjrsoft.module.student.vo.PbCseFeeitem2PageVo;

import com.xjrsoft.module.student.vo.PbCseFeeitem2Vo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

/**
* @title: 
* @Author szs
* @Date: 2024-04-01
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/pbCseFeeitem2")
@Api(value = "/student"  + "/pbCseFeeitem2",tags = "代码")
@AllArgsConstructor
public class PbCseFeeitem2Controller {


    private final IPbCseFeeitem2Service pbCseFeeitem2Service;

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

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

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


    @PostMapping
    @ApiOperation(value = "新增")
    @SaCheckPermission("pbcsefeeitem2:add")
    public RT<Boolean> add(@Valid @RequestBody AddPbCseFeeitem2Dto dto){
        PbCseFeeitem2 pbCseFeeitem2 = BeanUtil.toBean(dto, PbCseFeeitem2.class);
        boolean isSuccess = pbCseFeeitem2Service.save(pbCseFeeitem2);
    return RT.ok(isSuccess);
    }

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

        PbCseFeeitem2 pbCseFeeitem2 = BeanUtil.toBean(dto, PbCseFeeitem2.class);
        return RT.ok(pbCseFeeitem2Service.updateById(pbCseFeeitem2));

    }

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

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

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

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