package com.xjrsoft.module.textbook.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.textbook.dto.AddTextbookCoreAttributeDto;
import com.xjrsoft.module.textbook.dto.UpdateTextbookCoreAttributeDto;
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.textbook.dto.TextbookCoreAttributePageDto;
import com.xjrsoft.module.textbook.entity.TextbookCoreAttribute;
import com.xjrsoft.module.textbook.service.ITextbookCoreAttributeService;
import com.xjrsoft.module.textbook.vo.TextbookCoreAttributePageVo;

import com.xjrsoft.module.textbook.vo.TextbookCoreAttributeVo;
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-08
* @Version 1.0
*/
@RestController
@RequestMapping("/textbook" + "/textbookCoreAttribute")
@Api(value = "/textbook"  + "/textbookCoreAttribute",tags = "教材核心信息管理代码")
@AllArgsConstructor
public class TextbookCoreAttributeController {


    private final ITextbookCoreAttributeService textbookCoreAttributeService;

    @GetMapping(value = "/page")
    @ApiOperation(value="教材核心信息管理列表(分页)")
    @SaCheckPermission("textbookcoreattribute:detail")
    public RT<PageOutput<TextbookCoreAttributePageVo>> page(@Valid TextbookCoreAttributePageDto dto){

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

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询教材核心信息管理信息")
    @SaCheckPermission("textbookcoreattribute:detail")
    public RT<TextbookCoreAttributeVo> info(@RequestParam Long id){
        TextbookCoreAttribute textbookCoreAttribute = textbookCoreAttributeService.getById(id);
        if (textbookCoreAttribute == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(textbookCoreAttribute, TextbookCoreAttributeVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增教材核心信息管理")
    @SaCheckPermission("textbookcoreattribute:add")
    public RT<Boolean> add(@Valid @RequestBody AddTextbookCoreAttributeDto dto){
        TextbookCoreAttribute textbookCoreAttribute = BeanUtil.toBean(dto, TextbookCoreAttribute.class);
        boolean isSuccess = textbookCoreAttributeService.save(textbookCoreAttribute);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改教材核心信息管理")
    @SaCheckPermission("textbookcoreattribute:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateTextbookCoreAttributeDto dto){

        TextbookCoreAttribute textbookCoreAttribute = BeanUtil.toBean(dto, TextbookCoreAttribute.class);
        return RT.ok(textbookCoreAttributeService.updateById(textbookCoreAttribute));

    }

    @DeleteMapping
    @ApiOperation(value = "删除教材核心信息管理")
    @SaCheckPermission("textbookcoreattribute:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(textbookCoreAttributeService.removeBatchByIds(ids));

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

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

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