package com.yc.ship.module.resource.controller.admin.guidecheckin;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.resource.controller.admin.guidecheckin.vo.GuideCheckInPageReqVO;
import com.yc.ship.module.resource.controller.admin.guidecheckin.vo.GuideCheckInRespVO;
import com.yc.ship.module.resource.controller.admin.guidecheckin.vo.GuideCheckInSaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.guidecheckin.GuideCheckInDO;
import com.yc.ship.module.resource.service.guidecheckin.GuideCheckInService;
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 com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 导游出勤")
@RestController
@RequestMapping("/resource/guide-check-in")
@Validated
public class GuideCheckInController {

    @Resource
    private GuideCheckInService guideCheckInService;

    @PostMapping("/create")
    @Operation(summary = "创建导游出勤")
    @PreAuthorize("@ss.hasPermission('dispatch:guide-check-in:create')")
    public CommonResult<String> createGuideCheckIn(@Valid @RequestBody
                                                   GuideCheckInSaveReqVO createReqVO) {
        return success(guideCheckInService.createGuideCheckIn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新导游出勤")
    @PreAuthorize("@ss.hasPermission('dispatch:guide-check-in:update')")
    public CommonResult<Boolean> updateGuideCheckIn(@Valid @RequestBody GuideCheckInSaveReqVO updateReqVO) {
        guideCheckInService.updateGuideCheckIn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除导游出勤")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dispatch:guide-check-in:delete')")
    public CommonResult<Boolean> deleteGuideCheckIn(@RequestParam("id") String id) {
        guideCheckInService.deleteGuideCheckIn(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得导游出勤")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dispatch:guide-check-in:query')")
    public CommonResult<GuideCheckInRespVO> getGuideCheckIn(@RequestParam("id") String id) {
        GuideCheckInDO guideCheckIn = guideCheckInService.getGuideCheckIn(id);
        return success(BeanUtils.toBean(guideCheckIn, GuideCheckInRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得导游出勤分页")
    @PreAuthorize("@ss.hasPermission('dispatch:guide-check-in:query')")
    public CommonResult<PageResult<GuideCheckInRespVO>> getGuideCheckInPage(@Valid GuideCheckInPageReqVO pageReqVO) {
        PageResult<GuideCheckInRespVO> pageResult = guideCheckInService.getGuideCheckInPage(pageReqVO);
        return success(pageResult);
    }
    @GetMapping("/list")
    @Operation(summary = "获得导游出勤列表")
    @PreAuthorize("@ss.hasPermission('dispatch:guide-check-in:query')")
    public CommonResult<List<GuideCheckInRespVO>> getGuideCheckInList(@Valid GuideCheckInPageReqVO pageReqVO) {
        List<GuideCheckInRespVO> checkInList = guideCheckInService.getGuideCheckInList(pageReqVO);
        return success(checkInList);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出导游出勤 Excel")
    @PreAuthorize("@ss.hasPermission('dispatch:guide-check-in:export')")
    @OperateLog(type = EXPORT)
    public CommonResult<List<GuideCheckInRespVO>> exportGuideCheckInExcel(@Valid GuideCheckInPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        List<GuideCheckInRespVO> list = guideCheckInService.getGuideCheckInList(pageReqVO);
        return success(list);
        // 导出 Excel
//        ExcelUtils.write(response, excelName+"人员出勤表.xls", "数据", GuideCheckInRespVO.class,
//                        BeanUtils.toBean(list, GuideCheckInRespVO.class));
    }

    @GetMapping("/validate")
    @Operation(summary = "校验是否请假")
    @OperateLog(type = EXPORT)
    public CommonResult<Boolean> validate(@Valid GuideCheckInPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {

        return success(guideCheckInService.validateGuideOrDriverCheckIn(pageReqVO));
    }

}
