package cn.newfeifan.mall.module.distri.controller.admin.usersigninlog;

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.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;

import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;

import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.newfeifan.mall.module.distri.controller.admin.usersigninlog.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.usersigninlog.UserSignInLogDO;
import cn.newfeifan.mall.module.distri.service.usersigninlog.UserSignInLogService;

@Tag(name = "管理后台 - 用户签到日志")
@RestController
@RequestMapping("/distri/user-sign-in-log")
@Validated
public class UserSignInLogController {

    @Resource
    private UserSignInLogService userSignInLogService;

    @PostMapping("/create")
    @Operation(summary = "创建用户签到日志")
    @PreAuthorize("@ss.hasPermission('distri:user-sign-in-log:create')")
    public CommonResult<Long> createUserSignInLog(@Valid @RequestBody UserSignInLogSaveReqVO createReqVO) {
        return success(userSignInLogService.createUserSignInLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户签到日志")
    @PreAuthorize("@ss.hasPermission('distri:user-sign-in-log:update')")
    public CommonResult<Boolean> updateUserSignInLog(@Valid @RequestBody UserSignInLogSaveReqVO updateReqVO) {
        userSignInLogService.updateUserSignInLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户签到日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:user-sign-in-log:delete')")
    public CommonResult<Boolean> deleteUserSignInLog(@RequestParam("id") Long id) {
        userSignInLogService.deleteUserSignInLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户签到日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:user-sign-in-log:query')")
    public CommonResult<UserSignInLogRespVO> getUserSignInLog(@RequestParam("id") Long id) {
        UserSignInLogDO userSignInLog = userSignInLogService.getUserSignInLog(id);
        return success(BeanUtils.toBean(userSignInLog, UserSignInLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户签到日志分页")
    @PreAuthorize("@ss.hasPermission('distri:user-sign-in-log:query')")
    public CommonResult<PageResult<UserSignInLogRespVO>> getUserSignInLogPage(@Valid UserSignInLogPageReqVO pageReqVO) {
        PageResult<UserSignInLogDO> pageResult = userSignInLogService.getUserSignInLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserSignInLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户签到日志 Excel")
    @PreAuthorize("@ss.hasPermission('distri:user-sign-in-log:export')")
    @OperateLog(type = EXPORT)
    public void exportUserSignInLogExcel(@Valid UserSignInLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserSignInLogDO> list = userSignInLogService.getUserSignInLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户签到日志.xls", "数据", UserSignInLogRespVO.class,
                        BeanUtils.toBean(list, UserSignInLogRespVO.class));
    }

}