package com.xjrsoft.module.room.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.room.dto.AddWfRoomApplicantDto;
import com.xjrsoft.module.room.dto.UpdateWfRoomApplicantDto;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.springframework.http.ResponseEntity;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

import com.xjrsoft.module.room.dto.WfRoomApplicantPageDto;
import com.xjrsoft.module.room.entity.WfRoomApplicant;
import com.xjrsoft.module.room.service.IWfRoomApplicantService;
import com.xjrsoft.module.room.vo.WfRoomApplicantPageVo;

import com.xjrsoft.module.room.vo.WfRoomApplicantVo;
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 dzx
* @Date: 2024-01-02
* @Version 1.0
*/
@RestController
@RequestMapping("/room" + "/wfRoomApplicant")
@Api(value = "/room"  + "/wfRoomApplicant",tags = "寝室申请代码")
@AllArgsConstructor
public class WfRoomApplicantController {


    private final IWfRoomApplicantService wfRoomApplicantService;

    @GetMapping(value = "/page")
    @ApiOperation(value="寝室申请列表(分页)")
    @SaCheckPermission("wfroomapplicant:detail")
    public RT<PageOutput<WfRoomApplicantPageVo>> page(@Valid WfRoomApplicantPageDto dto){

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

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询寝室申请信息")
    @SaCheckPermission("wfroomapplicant:detail")
    public RT<WfRoomApplicantVo> info(@RequestParam Long id){
        WfRoomApplicant wfRoomApplicant = wfRoomApplicantService.getById(id);
        if (wfRoomApplicant == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(wfRoomApplicant, WfRoomApplicantVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增寝室申请")
    @SaCheckPermission("wfroomapplicant:add")
    public RT<Boolean> add(@Valid @RequestBody AddWfRoomApplicantDto dto){
        WfRoomApplicant wfRoomApplicant = BeanUtil.toBean(dto, WfRoomApplicant.class);
        boolean isSuccess = wfRoomApplicantService.save(wfRoomApplicant);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改寝室申请")
    @SaCheckPermission("wfroomapplicant:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateWfRoomApplicantDto dto){

        WfRoomApplicant wfRoomApplicant = BeanUtil.toBean(dto, WfRoomApplicant.class);
        return RT.ok(wfRoomApplicantService.updateById(wfRoomApplicant));

    }

    @DeleteMapping
    @ApiOperation(value = "删除寝室申请")
    @SaCheckPermission("wfroomapplicant:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(wfRoomApplicantService.removeBatchByIds(ids));

    }

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

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