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.AddBaseMajorCategorDto;
import com.xjrsoft.module.student.dto.UpdateBaseMajorCategorDto;
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.BaseMajorCategorPageDto;
import com.xjrsoft.module.student.entity.BaseMajorCategor;
import com.xjrsoft.module.student.service.IBaseMajorCategorService;
import com.xjrsoft.module.student.vo.BaseMajorCategorPageVo;

import com.xjrsoft.module.student.vo.BaseMajorCategorVo;
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" + "/baseMajorCategor")
@Api(value = "/student"  + "/baseMajorCategor",tags = "专业分类表代码")
@AllArgsConstructor
public class BaseMajorCategorController {


    private final IBaseMajorCategorService baseMajorCategorService;

    @GetMapping(value = "/page")
    @ApiOperation(value="专业分类表列表(分页)")
    @SaCheckPermission("basemajorcategor:detail")
    public RT<PageOutput<BaseMajorCategorPageVo>> page(@Valid BaseMajorCategorPageDto dto){

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

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询专业分类表信息")
    @SaCheckPermission("basemajorcategor:detail")
    public RT<BaseMajorCategorVo> info(@RequestParam Long id){
        BaseMajorCategor baseMajorCategor = baseMajorCategorService.getById(id);
        if (baseMajorCategor == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(baseMajorCategor, BaseMajorCategorVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增专业分类表")
    @SaCheckPermission("basemajorcategor:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseMajorCategorDto dto){
        BaseMajorCategor baseMajorCategor = BeanUtil.toBean(dto, BaseMajorCategor.class);
        boolean isSuccess = baseMajorCategorService.save(baseMajorCategor);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改专业分类表")
    @SaCheckPermission("basemajorcategor:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBaseMajorCategorDto dto){

        BaseMajorCategor baseMajorCategor = BeanUtil.toBean(dto, BaseMajorCategor.class);
        return RT.ok(baseMajorCategorService.updateById(baseMajorCategor));

    }

    @DeleteMapping
    @ApiOperation(value = "删除专业分类表")
    @SaCheckPermission("basemajorcategor:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(baseMajorCategorService.removeBatchByIds(ids));

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

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

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