package com.xjrsoft.module.personnel.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.module.personnel.entity.LaborManagement;
import com.xjrsoft.module.personnel.vo.LaborManagementPageVo;
import com.xjrsoft.module.personnel.mapper.LaborManagementMapper;
import com.xjrsoft.module.personnel.dto.AddLaborManagementDto;
import com.xjrsoft.module.personnel.dto.UpdateLaborManagementDto;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.xjrsoft.module.textbook.vo.TextbookIssueRecordPageVo;
import com.xjrsoft.module.weekly.vo.WeeklyDutyScheduleListVo;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import com.xjrsoft.module.personnel.dto.LaborManagementPageDto;
import com.xjrsoft.module.personnel.service.ILaborManagementService;
import com.xjrsoft.module.personnel.vo.LaborManagementPageVo;

import com.xjrsoft.module.personnel.vo.LaborManagementVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* @title: 劳资管理信息表
* @Author brealinxx
* @Date: 2024-01-23
* @Version 1.0
*/
@RestController
@RequestMapping("/personnel" + "/laborManagement")
@Api(value = "/personnel"  + "/laborManagement",tags = "劳资管理")
@AllArgsConstructor
public class LaborManagementController {


    private final ILaborManagementService laborManagementService;

    @GetMapping(value = "/page")
    @ApiOperation(value="劳资管理列表(分页)")
    @SaCheckPermission("baseteacher:detail")
    public RT<PageOutput<LaborManagementPageVo>> page(@Valid LaborManagementPageDto dto){
        Page<LaborManagementPageVo> page = laborManagementService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<LaborManagementPageVo> pageOutput = ConventPage.getPageOutput(page, LaborManagementPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public ResponseEntity<byte[]> exportData() {
        List<LaborManagementPageVo> list = laborManagementService.listLaborManagementPageVo();

//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
//        for (LaborManagementPageVo vo : list) {
//            vo.setStartWorkTime(sdf.format(vo.getStartWorkTime()));
//        }

        // 写入Excel
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, LaborManagementPageVo.class)
                .automaticMergeHead(false)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet()
                .doWrite(list);

        // 返回文件流
        return RT.fileStream(bot.toByteArray(), "LaborManagement" + ExcelTypeEnum.XLSX.getValue());
    }
}