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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.addressgroup.vo.AttendanceAddressGroupItemRespVO;
import cn.iocoder.yudao.module.system.controller.admin.addressgroup.vo.AttendanceAddressGroupPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.addressgroup.vo.AttendanceAddressGroupRespVO;
import cn.iocoder.yudao.module.system.controller.admin.addressgroup.vo.AttendanceAddressGroupSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.addressgroup.AttendanceAddressGroupDO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.addressgroup.AttendanceAddressGroupItemDO;
import cn.iocoder.yudao.module.system.service.attendance.addressgroup.AttendanceAddressGroupItemService;
import cn.iocoder.yudao.module.system.service.attendance.addressgroup.AttendanceAddressGroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

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

    @Resource
    private AttendanceAddressGroupService attendanceAddressGroupService;
    @Resource
    private AttendanceAddressGroupItemService attendanceAddressGroupItemService;

    @PostMapping("/createOrUpdate")
    @Operation(summary = "创建更新考勤地址组")
    @PreAuthorize("@ss.hasPermission('system:attendance-address-group:create')")
    public CommonResult<Long> createAttendanceAddressGroup(@Valid @RequestBody AttendanceAddressGroupSaveReqVO createReqVO) {
        return success(attendanceAddressGroupService.createAttendanceAddressGroup(createReqVO));
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得考勤地址组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:attendance-address-group:query')")
    public CommonResult<AttendanceAddressGroupRespVO> getAttendanceAddressGroup(@RequestParam("id") Long id) {
        AttendanceAddressGroupDO attendanceAddressGroup = attendanceAddressGroupService.getAttendanceAddressGroup(id);
        AttendanceAddressGroupRespVO vo = BeanUtils.toBean(attendanceAddressGroup, AttendanceAddressGroupRespVO.class);
        List<AttendanceAddressGroupItemDO> listByAddressGroupIds = attendanceAddressGroupItemService.getListByAddressGroupIds(Collections.singletonList(vo.getId()));
        List<AttendanceAddressGroupItemRespVO> items = BeanUtils.toBean(listByAddressGroupIds, AttendanceAddressGroupItemRespVO.class);
        vo.setItems(items);
        return success(vo);
    }

    @GetMapping("/page")
    @Operation(summary = "获得考勤地址组分页")
    @PreAuthorize("@ss.hasPermission('system:attendance-address-group:query')")
    public CommonResult<PageResult<AttendanceAddressGroupRespVO>> getAttendanceAddressGroupPage(@Valid AttendanceAddressGroupPageReqVO pageReqVO) {
        PageResult<AttendanceAddressGroupDO> pageResult = attendanceAddressGroupService.getAttendanceAddressGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendanceAddressGroupRespVO.class));
    }

    @GetMapping("/allList")
    @Operation(summary = "获得考勤地址组")
    @PreAuthorize("@ss.hasPermission('system:attendance-address-group:query')")
    public CommonResult<List<AttendanceAddressGroupRespVO>> getAllList() {
        List<AttendanceAddressGroupDO> list = attendanceAddressGroupService.getAllList();
        return success(BeanUtils.toBean(list, AttendanceAddressGroupRespVO.class));
    }

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

}
