package com.xjrsoft.module.attendance.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.activity.dto.ChangeStatusDto;
import com.xjrsoft.module.attendance.dto.AddAttendanceStatisticsDto;
import com.xjrsoft.module.attendance.dto.AttendanceStatisticsRecordDto;
import com.xjrsoft.module.attendance.dto.AttendanceRuleCategoryListDto;
import com.xjrsoft.module.attendance.dto.AttendanceStatisticsPageDto;
import com.xjrsoft.module.attendance.dto.UpdateAttendanceStatisticsDto;
import com.xjrsoft.module.attendance.entity.AttendanceRuleCategory;
import com.xjrsoft.module.attendance.entity.AttendanceStatistics;
import com.xjrsoft.module.attendance.service.IAttendanceRuleCategoryService;
import com.xjrsoft.module.attendance.service.IAttendanceStatisticsService;
import com.xjrsoft.module.attendance.vo.AttendanceRuleCategoryListVo;
import com.xjrsoft.module.attendance.vo.AttendanceStatisticsPageVo;
import com.xjrsoft.module.attendance.vo.AttendanceStatisticsRecordVo;
import com.xjrsoft.module.attendance.vo.AttendanceStatisticsVo;
import com.xjrsoft.module.classtime.dto.RefreshStatisticsDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
* @title: 考勤统计
* @Author dzx
* @Date: 2024-10-19
* @Version 1.0
*/
@RestController
@RequestMapping("/attendance" + "/statistics")
@Api(value = "/attendance"  + "/statistics",tags = "考勤统计代码")
@AllArgsConstructor
public class AttendanceStatisticsController {

    private final IAttendanceRuleCategoryService ruleCategoryService;
    private final IAttendanceStatisticsService attendanceStatisticsService;

    @GetMapping(value = "/page")
    @ApiOperation(value="考勤统计列表(分页)")
    @SaCheckPermission("attendancestatistics:detail")
    public RT<PageOutput<AttendanceStatisticsPageVo>> page(@Valid AttendanceStatisticsPageDto dto){
        IPage<AttendanceStatisticsPageVo> page = attendanceStatisticsService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<AttendanceStatisticsPageVo> pageOutput = ConventPage.getPageOutput(page, AttendanceStatisticsPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/rule-list")
    @ApiOperation(value="考勤规则列表")
    @SaCheckPermission("attendancestatistics:detail")
    public RT<List<AttendanceRuleCategoryListVo>> list(@Valid AttendanceRuleCategoryListDto dto){
        List<AttendanceRuleCategory> list = ruleCategoryService.list(
                new QueryWrapper<AttendanceRuleCategory>().lambda()
                        .eq(dto.getRoleId() != null, AttendanceRuleCategory::getRoleId, dto.getRoleId())
        );
        return RT.ok(BeanUtil.copyToList(list, AttendanceRuleCategoryListVo.class));
    }


    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询考勤统计信息")
    @SaCheckPermission("attendancestatistics:detail")
    public RT<AttendanceStatisticsVo> info(@RequestParam Long id){
        AttendanceStatistics attendanceStatistics = attendanceStatisticsService.getById(id);
        if (attendanceStatistics == null) {
           return RT.error("找不到此数据！");
        }
        AttendanceRuleCategory ruleCategory = ruleCategoryService.getById(attendanceStatistics.getAttendanceRuleCategoryId());
        AttendanceStatisticsVo statisticsVo = BeanUtil.toBean(attendanceStatistics, AttendanceStatisticsVo.class);
        if (ruleCategory != null) {
            statisticsVo.setRuleName(ruleCategory.getName());
        }
        return RT.ok(statisticsVo);
    }

    @PostMapping(value = "/change-status")
    @ApiOperation(value="修改状态")
    @SaCheckPermission("attendancestatistics:detail")
    public RT<Boolean> changeStatus(@Valid @RequestBody ChangeStatusDto dto) throws Exception {
        AttendanceStatistics statistics = attendanceStatisticsService.getById(dto.getId());
        if(statistics == null){
            throw new MyException("未能查到数据");
        }
        statistics.setStatus(dto.getStatus());
        statistics.setModifyDate(new Date());
        attendanceStatisticsService.updateById(statistics);
        return RT.ok(true);
    }


    @PostMapping
    @ApiOperation(value = "新增考勤统计")
    @SaCheckPermission("attendancestatistics:add")
    public RT<Boolean> add(@Valid @RequestBody AddAttendanceStatisticsDto dto){
        boolean isSuccess = attendanceStatisticsService.add(dto);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改考勤统计")
    @SaCheckPermission("attendancestatistics:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateAttendanceStatisticsDto dto){
        AttendanceStatistics attendanceStatistics = BeanUtil.toBean(dto, AttendanceStatistics.class);
        return RT.ok(attendanceStatisticsService.update(attendanceStatistics));
    }

    @DeleteMapping
    @ApiOperation(value = "删除考勤统计")
    @SaCheckPermission("attendancestatistics:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(attendanceStatisticsService.delete(ids));
    }

    @GetMapping(value = "/record-list")
    @ApiOperation(value="考情统计详情列表")
    @SaCheckPermission("attendancestatistics:detail")
    public RT<List<AttendanceStatisticsRecordVo>> recordPage(@Valid AttendanceStatisticsRecordDto dto){
        List<AttendanceStatisticsRecordVo> recordList = attendanceStatisticsService.getRecordList(dto);
        return RT.ok(recordList);
    }

    @PostMapping("/refresh-statistics")
    @ApiOperation(value = "刷新课时统计")
    @SaCheckPermission("attendancestatistics:detail")
    public RT<Boolean> refreshStatistics(@Valid @RequestBody RefreshStatisticsDto dto){
        Boolean aBoolean = attendanceStatisticsService.refreshRecord(dto.getId());

        return RT.ok(aBoolean);
    }

    @PostMapping(value = "/record-export-query")
    @ApiOperation(value="考勤统计详情导出")
    @SaCheckPermission("attendancestatistics:detail")
    public ResponseEntity<byte[]> recordExport(@Valid @RequestBody  RefreshStatisticsDto dto) throws IOException {
        byte[] bytes = attendanceStatisticsService.recordExport(dto.getId());
        String fileName = "recordExport" + ExcelTypeEnum.XLSX.getValue();
        return RT.fileStream(bytes, fileName);
    }

}