package com.xjrsoft.module.room.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.room.dto.AddWfRoomReportDto;
import com.xjrsoft.module.room.dto.UpdateWfRoomReportDto;
import com.xjrsoft.module.room.dto.WfRoomReportPageDto;
import com.xjrsoft.module.room.entity.WfRoomReport;
import com.xjrsoft.module.room.service.IWfRoomReportService;
import com.xjrsoft.module.room.vo.WfRoomReportPageVo;
import com.xjrsoft.module.room.vo.WfRoomReportVo;
import com.xjrsoft.module.system.entity.File;
import com.xjrsoft.module.system.service.IFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
* @title: 寝室情况汇报
* @Author szs
* @Date: 2024-01-03
* @Version 1.0
*/
@RestController
@RequestMapping("/room" + "/wfRoomReport")
@Api(value = "/room"  + "/wfRoomReport",tags = "寝室情况汇报代码")
@AllArgsConstructor
public class WfRoomReportController {


    private final IWfRoomReportService wfRoomReportService;

    private final IFileService fileService;

    @GetMapping(value = "/page")
    @ApiOperation(value="寝室情况汇报列表(分页)")
    @SaCheckPermission("wfroomreport:detail")
    public RT<PageOutput<WfRoomReportPageVo>> page(@Valid WfRoomReportPageDto dto){

        MPJLambdaWrapper<WfRoomReport> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .select(WfRoomReport::getId)
                .select("t1.`name` as applicantUserIdCN")
                .select("t2.`name` as headTeacherIdCN")
                .leftJoin("xjr_user t1 on t1.id = t.applicant_user_id")
                .leftJoin("xjr_user t2 on t2.id = t.head_teacher_id")
                .select(WfRoomReport.class,x -> VoToColumnUtil.fieldsToColumns(WfRoomReportPageVo.class).contains(x.getProperty()))
                .disableSubLogicDel()
                .orderByDesc(WfRoomReport::getId);
        IPage<WfRoomReportPageVo> page = wfRoomReportService.selectJoinListPage(ConventPage.getPage(dto), WfRoomReportPageVo.class, queryWrapper);
        PageOutput<WfRoomReportPageVo> pageOutput = ConventPage.getPageOutput(page, WfRoomReportPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询寝室情况汇报信息")
    @SaCheckPermission("wfroomreport:detail")
    public RT<WfRoomReportVo> info(@RequestParam Long id){
        MPJLambdaWrapper<WfRoomReport> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .select(WfRoomReport::getId)
                .select("t1.`name` as applicantUserIdCN")
                .select("t2.`name` as headTeacherIdCN")
                .leftJoin("xjr_user t1 on t1.id = t.applicant_user_id")
                .leftJoin("xjr_user t2 on t2.id = t.head_teacher_id")
                .select(WfRoomReport.class,x -> VoToColumnUtil.fieldsToColumns(WfRoomReportVo.class).contains(x.getProperty()))
                .eq(WfRoomReport::getId, id)
                .disableSubLogicDel()
                .orderByDesc(WfRoomReport::getId);
        WfRoomReportVo wfRoomReportVo = wfRoomReportService.selectJoinOne(WfRoomReportVo.class, queryWrapper);

        if (wfRoomReportVo == null) {
           return RT.error("找不到此数据！");
        }

        wfRoomReportVo.setFileList(fileService.list(Wrappers.<File>query().lambda().eq(File::getFolderId, wfRoomReportVo.getFileId())));

        return RT.ok(wfRoomReportVo);
    }


    @PostMapping
    @ApiOperation(value = "新增寝室情况汇报")
    @SaCheckPermission("wfroomreport:add")
    public RT<Boolean> add(@Valid @RequestBody AddWfRoomReportDto dto){
        WfRoomReport wfRoomReport = BeanUtil.toBean(dto, WfRoomReport.class);
        boolean isSuccess = wfRoomReportService.save(wfRoomReport);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改寝室情况汇报")
    @SaCheckPermission("wfroomreport:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateWfRoomReportDto dto){

        WfRoomReport wfRoomReport = BeanUtil.toBean(dto, WfRoomReport.class);
        return RT.ok(wfRoomReportService.updateById(wfRoomReport));

    }

    @DeleteMapping
    @ApiOperation(value = "删除寝室情况汇报")
    @SaCheckPermission("wfroomreport:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(wfRoomReportService.removeBatchByIds(ids));

    }

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

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