package cn.iocoder.yudao.module.system.controller.admin.addressgroup;

import cn.iocoder.yudao.module.system.controller.admin.addressgroup.vo.AttendanceAddressGroupItemPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.addressgroup.vo.AttendanceAddressGroupItemRespVO;
import cn.iocoder.yudao.module.system.controller.admin.addressgroup.vo.AttendanceAddressGroupItemSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.addressgroup.AttendanceAddressGroupItemDO;

import cn.iocoder.yudao.module.system.service.attendance.addressgroup.AttendanceAddressGroupItemService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 考勤地址组子表")
@RestController
@RequestMapping("/system/attendance-address-group-item")
@Validated
public class AttendanceAddressGroupItemController {

    @Resource
    private AttendanceAddressGroupItemService attendanceAddressGroupItemService;

    @PostMapping("/create")
    @Operation(summary = "创建考勤地址组子表")
    @PreAuthorize("@ss.hasPermission('system:attendance-address-group-item:create')")
    public CommonResult<Long> createAttendanceAddressGroupItem(@Valid @RequestBody AttendanceAddressGroupItemSaveReqVO createReqVO) {
        return success(attendanceAddressGroupItemService.createAttendanceAddressGroupItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考勤地址组子表")
    @PreAuthorize("@ss.hasPermission('system:attendance-address-group-item:update')")
    public CommonResult<Boolean> updateAttendanceAddressGroupItem(@Valid @RequestBody AttendanceAddressGroupItemSaveReqVO updateReqVO) {
        attendanceAddressGroupItemService.updateAttendanceAddressGroupItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除考勤地址组子表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:attendance-address-group-item:delete')")
    public CommonResult<Boolean> deleteAttendanceAddressGroupItem(@RequestParam("id") Long id) {
        attendanceAddressGroupItemService.deleteAttendanceAddressGroupItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得考勤地址组子表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:attendance-address-group-item:query')")
    public CommonResult<AttendanceAddressGroupItemRespVO> getAttendanceAddressGroupItem(@RequestParam("id") Long id) {
        AttendanceAddressGroupItemDO attendanceAddressGroupItem = attendanceAddressGroupItemService.getAttendanceAddressGroupItem(id);
        return success(BeanUtils.toBean(attendanceAddressGroupItem, AttendanceAddressGroupItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考勤地址组子表分页")
    @PreAuthorize("@ss.hasPermission('system:attendance-address-group-item:query')")
    public CommonResult<PageResult<AttendanceAddressGroupItemRespVO>> getAttendanceAddressGroupItemPage(@Valid AttendanceAddressGroupItemPageReqVO pageReqVO) {
        PageResult<AttendanceAddressGroupItemDO> pageResult = attendanceAddressGroupItemService.getAttendanceAddressGroupItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendanceAddressGroupItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考勤地址组子表 Excel")
    @PreAuthorize("@ss.hasPermission('system:attendance-address-group-item:export')")
    @OperateLog(type = EXPORT)
    public void exportAttendanceAddressGroupItemExcel(@Valid AttendanceAddressGroupItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendanceAddressGroupItemDO> list = attendanceAddressGroupItemService.getAttendanceAddressGroupItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考勤地址组子表.xls", "数据", AttendanceAddressGroupItemRespVO.class,
                        BeanUtils.toBean(list, AttendanceAddressGroupItemRespVO.class));
    }

}
