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

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.groupshift.vo.AttendanceGroupShiftPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.groupshift.vo.AttendanceGroupShiftRespVO;
import cn.iocoder.yudao.module.system.controller.admin.groupshift.vo.AttendanceGroupShiftSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.groupshift.vo.AttendanceGroupShiftVO;
import cn.iocoder.yudao.module.system.controller.admin.groupshiftitem.vo.AttendanceGroupShiftItemSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.groupshift.AttendanceGroupShiftDO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.groupshiftitem.AttendanceGroupShiftItemDO;
import cn.iocoder.yudao.module.system.service.attendance.group.AttendanceGroupService;
import cn.iocoder.yudao.module.system.service.attendance.groupshift.AttendanceGroupShiftService;
import cn.iocoder.yudao.module.system.service.attendance.groupshiftitem.AttendanceGroupShiftItemService;
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-shift")
@Validated
public class AttendanceGroupShiftController {

    @Resource
    private AttendanceGroupShiftService groupShiftService;
    @Resource
    private AttendanceGroupShiftItemService groupShiftItemService;

    @PostMapping("/create")
    @Operation(summary = "创建考勤组班次")
    @PreAuthorize("@ss.hasPermission('attendance:group-shift:create')")
    public CommonResult<Long> createGroupShift(@Valid @RequestBody AttendanceGroupShiftSaveReqVO createReqVO) {
        return success(groupShiftService.createGroupShift(createReqVO));
    }

    // 如果返回的有值 (判断不是空数组) - 那么前端 跳出提示框 (该班次已有考勤组在使用请选择更新方式)  下面两个按钮   立即生效 和 (推荐)次日生效
    @PutMapping("/update")
    @Operation(summary = "更新考勤组班次")
    @PreAuthorize("@ss.hasPermission('attendance:group-shift:update')")
    public CommonResult<List<Long>> updateGroupShift(@Valid @RequestBody AttendanceGroupShiftSaveReqVO updateReqVO) {
        List<Long> groupIds = groupShiftService.updateGroupShift(updateReqVO);
        return success(groupIds);
    }

    @PostMapping("/effectiveImmediately")
    @Operation(summary = "更换考勤班次立即生效")
    public CommonResult<Boolean> effectiveImmediately(@RequestBody List<Long> groupIds) {
        groupShiftService.effectiveImmediately(groupIds);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得考勤组班次")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('attendance:group-shift:query')")
    public CommonResult<AttendanceGroupShiftRespVO> getGroupShift(@RequestParam("id") Long id) {
        AttendanceGroupShiftDO groupShift = groupShiftService.getGroupShift(id);
        AttendanceGroupShiftRespVO vo = BeanUtils.toBean(groupShift, AttendanceGroupShiftRespVO.class);
        List<AttendanceGroupShiftItemDO> items = groupShiftItemService.getGroupShiftItemListByShiftId(groupShift.getId());
        vo.setItems(BeanUtils.toBean(items, AttendanceGroupShiftItemSaveReqVO.class));
        return success(vo);
    }

    @GetMapping("/page")
    @Operation(summary = "获得考勤组班次分页")
    @PreAuthorize("@ss.hasPermission('attendance:group-shift:query')")
    public CommonResult<PageResult<AttendanceGroupShiftRespVO>> getGroupShiftPage(@Valid AttendanceGroupShiftPageReqVO pageReqVO) {
        PageResult<AttendanceGroupShiftDO> pageResult = groupShiftService.getGroupShiftPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendanceGroupShiftRespVO.class));
    }

    @GetMapping("/allList")
    @Operation(summary = "获取所有列表")
    public CommonResult<List<AttendanceGroupShiftVO>> allList() {
        List<AttendanceGroupShiftVO> dos = groupShiftService.allList();
        return success(dos);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考勤组班次 Excel")
    @PreAuthorize("@ss.hasPermission('attendance:group-shift:export')")
    @OperateLog(type = EXPORT)
    public void exportGroupShiftExcel(@Valid AttendanceGroupShiftPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendanceGroupShiftDO> list = groupShiftService.getGroupShiftPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考勤组班次.xls", "数据", AttendanceGroupShiftRespVO.class,
                BeanUtils.toBean(list, AttendanceGroupShiftRespVO.class));
    }

}