package com.xjrsoft.module.teacher.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.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.teacher.dto.AddAttendancePersonnelDto;
import com.xjrsoft.module.teacher.dto.AttendancePersonnelPageDto;
import com.xjrsoft.module.teacher.dto.SelectPersonPageDto;
import com.xjrsoft.module.teacher.dto.UpdateAttendancePersonnelDto;
import com.xjrsoft.module.teacher.entity.AttendancePersonnel;
import com.xjrsoft.module.teacher.service.IAttendancePersonnelService;
import com.xjrsoft.module.teacher.vo.AttendancePersonnelPageVo;
import com.xjrsoft.module.teacher.vo.AttendancePersonnelVo;
import com.xjrsoft.module.teacher.vo.SelectPersonPageVo;
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.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
* @title: 考勤-人员
* @Author szs
* @Date: 2023-12-18
* @Version 1.0
*/
@RestController
@RequestMapping("/teacher" + "/attendancePersonnel")
@Api(value = "/teacher"  + "/attendancePersonnel",tags = "考勤-人员代码")
@AllArgsConstructor
public class AttendancePersonnelController {


    private final IAttendancePersonnelService attendancePersonnelService;

    @GetMapping(value = "/page")
    @ApiOperation(value="考勤-人员列表(分页)")
    @SaCheckPermission("attendancepersonnel:detail")
    public RT<PageOutput<AttendancePersonnelPageVo>> page(@Valid AttendancePersonnelPageDto dto){
        IPage<AttendancePersonnelPageVo> page = attendancePersonnelService.getListPage(dto);
        PageOutput<AttendancePersonnelPageVo> pageOutput = ConventPage.getPageOutput(page, AttendancePersonnelPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/selectPersonPage")
    @ApiOperation(value="考勤-选人人员列表(分页)")
    @SaCheckPermission("attendancepersonnel:detail")
    public RT<PageOutput<SelectPersonPageVo>> selectPersonPage(@Valid SelectPersonPageDto dto){
        IPage<SelectPersonPageVo> page = attendancePersonnelService.selectPersonPage(dto);
        PageOutput<SelectPersonPageVo> pageOutput = ConventPage.getPageOutput(page, SelectPersonPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询考勤-人员信息")
    @SaCheckPermission("attendancepersonnel:detail")
    public RT<AttendancePersonnelVo> info(@RequestParam Long id){
        AttendancePersonnel attendancePersonnel = attendancePersonnelService.getById(id);
        if (attendancePersonnel == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(attendancePersonnel, AttendancePersonnelVo.class));
    }

    @PostMapping
    @ApiOperation(value = "新增考勤-人员")
    @SaCheckPermission("attendancepersonnel:add")
    public RT<Boolean> add(@Valid @RequestBody AddAttendancePersonnelDto dto){
        AttendancePersonnel attendancePersonnel = BeanUtil.toBean(dto, AttendancePersonnel.class);
        boolean isSuccess = attendancePersonnelService.save(attendancePersonnel);
    return RT.ok(isSuccess);
    }

    @PostMapping("/addBatch")
    @ApiOperation(value = "批量添加考勤-人员")
    @SaCheckPermission("attendancepersonnel:add")
    public RT<Boolean> addBatch(@Valid @RequestBody List<Long> userIds){
        boolean isSuccess = attendancePersonnelService.addBatch(userIds);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改考勤-人员")
    @SaCheckPermission("attendancepersonnel:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateAttendancePersonnelDto dto){

        AttendancePersonnel attendancePersonnel = BeanUtil.toBean(dto, AttendancePersonnel.class);
        return RT.ok(attendancePersonnelService.updateById(attendancePersonnel));

    }

    @DeleteMapping
    @ApiOperation(value = "删除考勤-人员")
    @SaCheckPermission("attendancepersonnel:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(attendancePersonnelService.removeBatchByIds(ids));

    }

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

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