package com.xjrsoft.module.weekly.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.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.weekly.dto.WeeklyDutySchedulePageDto;
import com.xjrsoft.module.weekly.entity.WeeklyDutySchedule;
import com.xjrsoft.module.weekly.service.IWeeklyDutyScheduleService;
import com.xjrsoft.module.weekly.vo.WeeklyDutyScheduleListVo;
import com.xjrsoft.module.weekly.vo.WeeklyDutySchedulePageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @title: 值周排班
* @Author dzx
* @Date: 2023-12-30
* @Version 1.0
*/
@RestController
@RequestMapping("/weekly" + "/weeklyDutySchedule")
@Api(value = "/weekly"  + "/weeklyDutySchedule",tags = "值周排班代码")
@AllArgsConstructor
public class WeeklyDutyScheduleController {


    private final IWeeklyDutyScheduleService weeklyDutyScheduleService;

    @GetMapping(value = "/page")
    @ApiOperation(value="值周排班列表(分页)")
    @SaCheckPermission("weeklydutyschedule:detail")
    public RT<PageOutput<WeeklyDutySchedulePageVo>> page(@Valid WeeklyDutySchedulePageDto dto){

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

    @PostMapping("/import")
    @ApiOperation(value = "导入")
    public RT<Boolean> importData(@RequestParam("file") MultipartFile file) throws IOException {
        //先清空
        weeklyDutyScheduleService.deleteAll();
        //在导入
        List<Map<Integer, Object>> excelDataList = EasyExcel.read(file.getInputStream()).sheet().headRowNumber(3).doReadSync();
//        List<WeeklyDutySchedulePageVo> savedDataList = EasyExcel.read(file.getInputStream()).head(WeeklyDutySchedulePageVo.class).sheet().doReadSync();
        Boolean result = weeklyDutyScheduleService.insertMany(excelDataList);
        return RT.ok(result);
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出")

    public ResponseEntity<byte[]> exportData(@Valid WeeklyDutySchedulePageDto dto, @RequestParam(defaultValue = "false") Boolean isTemplate) {
        List<WeeklyDutySchedule> list = weeklyDutyScheduleService.list();
        List<WeeklyDutyScheduleListVo> customerList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        for (WeeklyDutySchedule vo : list) {
            WeeklyDutyScheduleListVo listVo = BeanUtil.toBean(vo, WeeklyDutyScheduleListVo.class);
            listVo.setDateBirth(sdf.format(vo.getDateBirth()));

            customerList.add(listVo);
        }
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, WeeklyDutyScheduleListVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(customerList);

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