package com.xjrsoft.module.room.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.AddWfRoomStayOvernightDto;
import com.xjrsoft.module.room.dto.UpdateWfRoomStayOvernightDto;
import com.xjrsoft.module.room.dto.WfRoomStayOvernightPageDto;
import com.xjrsoft.module.room.entity.WfRoomStayOvernight;
import com.xjrsoft.module.room.service.IWfRoomStayOvernightService;
import com.xjrsoft.module.room.vo.WfRoomStayOvernightPageVo;
import com.xjrsoft.module.room.vo.WfRoomStayOvernightVo;
import com.xjrsoft.module.teacher.entity.XjrUser;
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.text.SimpleDateFormat;
import java.util.List;

/**
* @title: 留校住宿申请
* @Author szs
* @Date: 2024-01-02
* @Version 1.0
*/
@RestController
@RequestMapping("/room" + "/wfRoomStayOvernight")
@Api(value = "/room"  + "/wfRoomStayOvernight",tags = "留校住宿申请代码")
@AllArgsConstructor
public class WfRoomStayOvernightController {


    private final IWfRoomStayOvernightService wfRoomStayOvernightService;

    @GetMapping(value = "/page")
    @ApiOperation(value="留校住宿申请列表(分页)")
    @SaCheckPermission("wfroomstayovernight:detail")
    public RT<PageOutput<WfRoomStayOvernightPageVo>> page(@Valid WfRoomStayOvernightPageDto dto){
        MPJLambdaWrapper<WfRoomStayOvernight> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .disableSubLogicDel()
                .like(ObjectUtil.isNotNull(dto.getUserName()),"t1.name",dto.getUserName())
                .like(ObjectUtil.isNotNull(dto.getClassName()),WfRoomStayOvernight::getClassName,dto.getClassName())
                .like(ObjectUtil.isNotNull(dto.getGenderName()),WfRoomStayOvernight::getGenderName,dto.getGenderName())
                .like(ObjectUtil.isNotNull(dto.getStudentId()),WfRoomStayOvernight::getStudentId,dto.getStudentId())
                .ge(ObjectUtil.isNotNull(dto.getStartTime()),WfRoomStayOvernight::getStartTime,dto.getStartTime())
                .le(ObjectUtil.isNotNull(dto.getEndTime()),WfRoomStayOvernight::getEndTime,dto.getEndTime())
                .leftJoin(XjrUser.class,XjrUser::getId,WfRoomStayOvernight::getApplicantUserId,ext1->ext1
                        .selectAs(XjrUser::getName,WfRoomStayOvernightPageVo::getApplicantUserIdCN))
                .leftJoin(XjrUser.class,XjrUser::getId,WfRoomStayOvernight::getHeadTeacherId,ext2->ext2
                        .selectAs(XjrUser::getName,WfRoomStayOvernightPageVo::getHeadTeacherIdCN))
                .orderByDesc(WfRoomStayOvernight::getId)
                .select(WfRoomStayOvernight::getId)
                .select(WfRoomStayOvernight.class,x -> VoToColumnUtil.fieldsToColumns(WfRoomStayOvernightPageVo.class).contains(x.getProperty()));
        IPage<WfRoomStayOvernightPageVo> page = wfRoomStayOvernightService.selectJoinListPage(ConventPage.getPage(dto), WfRoomStayOvernightPageVo.class, queryWrapper);
        PageOutput<WfRoomStayOvernightPageVo> pageOutput = ConventPage.getPageOutput(page, WfRoomStayOvernightPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询留校住宿申请信息")
    @SaCheckPermission("wfroomstayovernight:detail")
    public RT<WfRoomStayOvernightVo> info(@RequestParam Long id){
        WfRoomStayOvernight wfRoomStayOvernight = wfRoomStayOvernightService.getById(id);
        if (wfRoomStayOvernight == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(wfRoomStayOvernight, WfRoomStayOvernightVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增留校住宿申请")
    @SaCheckPermission("wfroomstayovernight:add")
    public RT<Boolean> add(@Valid @RequestBody AddWfRoomStayOvernightDto dto){
        WfRoomStayOvernight wfRoomStayOvernight = BeanUtil.toBean(dto, WfRoomStayOvernight.class);
        boolean isSuccess = wfRoomStayOvernightService.save(wfRoomStayOvernight);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改留校住宿申请")
    @SaCheckPermission("wfroomstayovernight:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateWfRoomStayOvernightDto dto){

        WfRoomStayOvernight wfRoomStayOvernight = BeanUtil.toBean(dto, WfRoomStayOvernight.class);
        return RT.ok(wfRoomStayOvernightService.updateById(wfRoomStayOvernight));

    }

    @DeleteMapping
    @ApiOperation(value = "删除留校住宿申请")
    @SaCheckPermission("wfroomstayovernight:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(wfRoomStayOvernightService.removeBatchByIds(ids));

    }

    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public ResponseEntity<byte[]> exportData(@Valid WfRoomStayOvernightPageDto dto, @RequestParam(defaultValue = "false") Boolean isTemplate) {
        MPJLambdaWrapper<WfRoomStayOvernight> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .disableSubLogicDel()
                .leftJoin(XjrUser.class,XjrUser::getId,WfRoomStayOvernight::getApplicantUserId,ext1->ext1
                        .selectAs(XjrUser::getName,WfRoomStayOvernightVo::getApplicantUserIdCN))
                .leftJoin(XjrUser.class,XjrUser::getId,WfRoomStayOvernight::getHeadTeacherId,ext2->ext2
                        .selectAs(XjrUser::getName,WfRoomStayOvernightVo::getHeadTeacherIdCN))
                .orderByDesc(WfRoomStayOvernight::getId)
                .select(WfRoomStayOvernight::getId)
                .select(WfRoomStayOvernight.class,x -> VoToColumnUtil.fieldsToColumns(WfRoomStayOvernightVo.class).contains(x.getProperty()));
        List<WfRoomStayOvernightVo> wfRoomStayOvernightVoList = wfRoomStayOvernightService.selectJoinList(WfRoomStayOvernightVo.class, queryWrapper);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        for (WfRoomStayOvernightVo wfRoomStayOvernightVo : wfRoomStayOvernightVoList) {
            wfRoomStayOvernightVo.setStartTimeStr(sdf.format(wfRoomStayOvernightVo.getStartTime()));
            wfRoomStayOvernightVo.setEndTimeStr(sdf.format(wfRoomStayOvernightVo.getEndTime()));
        }

        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, WfRoomStayOvernightVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet("WfRoomStayOvernight").doWrite(wfRoomStayOvernightVoList);

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