package com.xjrsoft.module.dev.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.R;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.dev.dto.AddTestfrom3Dto;
import com.xjrsoft.module.dev.dto.UpdateTestfrom3Dto;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.xjrsoft.common.utils.ExcelUtil;
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.dev.dto.Testfrom3PageDto;
import com.xjrsoft.module.dev.entity.Testfrom3;
import com.xjrsoft.module.dev.service.ITestfrom3Service;
import com.xjrsoft.module.dev.vo.Testfrom3PageVo;

import com.xjrsoft.module.datalog.service.DatalogService;
import com.xjrsoft.module.dev.vo.Testfrom3Vo;
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: 测试3
* @Author 管理员
* @Date: 2025-09-25
* @Version 1.0
*/
@RestController
@RequestMapping("/dev" + "/testfrom3")
@Api(value = "/dev"  + "/testfrom3",tags = "测试3代码")
@AllArgsConstructor
public class Testfrom3Controller {


    private final ITestfrom3Service testfrom3Service;
	private final DatalogService dataService;

    @GetMapping(value = "/page")
    @ApiOperation(value="Testfrom3列表(分页)")
    @SaCheckPermission("testfrom3:list")
    public R page(@Valid Testfrom3PageDto dto){

        LambdaQueryWrapper<Testfrom3> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(StrUtil.isNotBlank(dto.getDanXingWenBen8163()),Testfrom3::getDanXingWenBen8163,dto.getDanXingWenBen8163())
                .like(StrUtil.isNotBlank(dto.getDanXingWenBen3634()),Testfrom3::getDanXingWenBen3634,dto.getDanXingWenBen3634())
                .like(StrUtil.isNotBlank(dto.getDanXingWenBen5097()),Testfrom3::getDanXingWenBen5097,dto.getDanXingWenBen5097())
                    .orderByDesc(Testfrom3::getId)
                .select(Testfrom3.class,x -> VoToColumnUtil.fieldsToColumns(Testfrom3PageVo.class).contains(x.getProperty()));
        IPage<Testfrom3> page = testfrom3Service.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<Testfrom3PageVo> pageOutput = ConventPage.getPageOutput(page, Testfrom3PageVo.class);
        return R.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询Testfrom3信息")
    @SaCheckPermission("testfrom3:detail")
    public R info(@RequestParam Long id){
        Testfrom3 testfrom3 = testfrom3Service.getById(id);
        if (testfrom3 == null) {
           return R.error("找不到此数据！");
        }
        return R.ok(BeanUtil.toBean(testfrom3, Testfrom3Vo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增Testfrom3")
    @SaCheckPermission("testfrom3:add")
    public R add(@Valid @RequestBody UpdateTestfrom3Dto dto){
        UpdateTestfrom3Dto res = dataService.insert(dto);
        return R.ok(res.getId());
    }

    @PutMapping
    @ApiOperation(value = "修改Testfrom3")
    @SaCheckPermission("testfrom3:edit")
    public R update(@Valid @RequestBody UpdateTestfrom3Dto dto){
	    return R.ok(dataService.updateById(dto));
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @SaCheckPermission("testfrom3:delete")
    public R delete(@Valid @RequestBody List<Long> ids){
    return R.ok(dataService.deleteByIds(UpdateTestfrom3Dto.class, ids));

    }
    @PostMapping("/import")
    @ApiOperation(value = "导入")
    @SaCheckPermission("testfrom3:import")
    public R importData(@RequestParam MultipartFile file) throws IOException {
        List<Testfrom3PageVo> savedDataList = EasyExcel.read(file.getInputStream()).head(Testfrom3PageVo.class).sheet().doReadSync();
        ExcelUtil.transExcelData(savedDataList, true);
        dataService.insertBatch(BeanUtil.copyToList(savedDataList,UpdateTestfrom3Dto.class));
        return R.ok();
    }

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

        return R.fileStream(resultBot.toByteArray(), "Testfrom3" + ExcelTypeEnum.XLSX.getValue());
    }
}