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

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.group.vo.AttendanceGroupPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.group.vo.AttendanceGroupRespVO;
import cn.iocoder.yudao.module.system.controller.admin.group.vo.AttendanceGroupSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.groupsystem.vo.AttendanceGroupSystemRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.group.AttendanceGroupDO;
import cn.iocoder.yudao.module.system.service.attendance.group.AttendanceGroupService;
import cn.iocoder.yudao.module.system.service.attendance.groupsystem.AttendanceGroupSystemService;
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.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/group")
@Validated
public class AttendanceGroupController {

    @Resource
    private AttendanceGroupService groupService;
    @Resource
    private AttendanceGroupSystemService groupSystemService;

    @PostMapping("/create")
    @Operation(summary = "创建考勤组")
    @PreAuthorize("@ss.hasPermission('attendance:group:create')")
    public CommonResult<Long> createGroup(@Valid @RequestBody AttendanceGroupSaveReqVO createReqVO) {
        return success(groupService.createGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考勤组")
    @PreAuthorize("@ss.hasPermission('attendance:group:update')")
    public CommonResult<Boolean> updateGroup(@Valid @RequestBody AttendanceGroupSaveReqVO updateReqVO) {
        groupService.updateGroup(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得考勤组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('attendance:group:query')")
    public CommonResult<AttendanceGroupRespVO> getGroup(@RequestParam("id") Long id) {
        AttendanceGroupDO group = groupService.getGroup(id);
        List<AttendanceGroupSystemRespVO> groupSystems = groupSystemService.getGroupSystemVoByGroupId(id);
        AttendanceGroupRespVO vo = BeanUtils.toBean(group, AttendanceGroupRespVO.class);
        vo.setGroupSystems(groupSystems);
        return success(vo);
    }


    @GetMapping("/page")
    @Operation(summary = "获得考勤组分页")
    @PreAuthorize("@ss.hasPermission('attendance:group:query')")
    public CommonResult<PageResult<AttendanceGroupRespVO>> getGroupPage(@Valid AttendanceGroupPageReqVO pageReqVO) {
        PageResult<AttendanceGroupDO> pageResult = groupService.getGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendanceGroupRespVO.class));
    }


    @GetMapping("/getAll")
    @Operation(summary = "获得所有考勤组")
    public CommonResult<List<AttendanceGroupRespVO>> getAll() {
        List<AttendanceGroupDO> pageResult = groupService.getAll();
        return success(BeanUtils.toBean(pageResult, AttendanceGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考勤组 Excel")
    @PreAuthorize("@ss.hasPermission('attendance:group:export')")
    @OperateLog(type = EXPORT)
    public void exportGroupExcel(@Valid AttendanceGroupPageReqVO pageReqVO,
                                 HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendanceGroupDO> list = groupService.getGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考勤组.xls", "数据", AttendanceGroupRespVO.class,
                BeanUtils.toBean(list, AttendanceGroupRespVO.class));
    }

}
