package co.yixiang.yshop.module.member.controller.admin.userinvitelog;

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

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;

import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;

import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.*;

import co.yixiang.yshop.module.member.controller.admin.userinvitelog.vo.*;
import co.yixiang.yshop.module.member.dal.dataobject.userinvitelog.UserInviteLogDO;
import co.yixiang.yshop.module.member.convert.userinvitelog.UserInviteLogConvert;
import co.yixiang.yshop.module.member.service.userinvitelog.UserInviteLogService;

@Tag(name = "管理后台 - 邀请日志")
@RestController
@RequestMapping("/member/user-invite-log")
@Validated
public class UserInviteLogController {

    @Resource
    private UserInviteLogService userInviteLogService;

    @PostMapping("/create")
    @Operation(summary = "创建邀请日志")
    @PreAuthorize("@ss.hasPermission('member:user-invite-log:create')")
    public CommonResult<Long> createUserInviteLog(@Valid @RequestBody UserInviteLogCreateReqVO createReqVO) {
        return success(userInviteLogService.createUserInviteLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新邀请日志")
    @PreAuthorize("@ss.hasPermission('member:user-invite-log:update')")
    public CommonResult<Boolean> updateUserInviteLog(@Valid @RequestBody UserInviteLogUpdateReqVO updateReqVO) {
        userInviteLogService.updateUserInviteLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除邀请日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-invite-log:delete')")
    public CommonResult<Boolean> deleteUserInviteLog(@RequestParam("id") Long id) {
        userInviteLogService.deleteUserInviteLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得邀请日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-invite-log:query')")
    public CommonResult<UserInviteLogRespVO> getUserInviteLog(@RequestParam("id") Long id) {
        UserInviteLogDO userInviteLog = userInviteLogService.getUserInviteLog(id);
        return success(UserInviteLogConvert.INSTANCE.convert(userInviteLog));
    }

    @GetMapping("/list")
    @Operation(summary = "获得邀请日志列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('member:user-invite-log:query')")
    public CommonResult<List<UserInviteLogRespVO>> getUserInviteLogList(@RequestParam("ids") Collection<Long> ids) {
        List<UserInviteLogDO> list = userInviteLogService.getUserInviteLogList(ids);
        return success(UserInviteLogConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得邀请日志分页")
    @PreAuthorize("@ss.hasPermission('member:user-invite-log:query')")
    public CommonResult<PageResult<UserInviteLogRespVO>> getUserInviteLogPage(@Valid UserInviteLogPageReqVO pageVO) {
        PageResult<UserInviteLogDO> pageResult = userInviteLogService.getUserInviteLogPage(pageVO);
        return success(UserInviteLogConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出邀请日志 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-invite-log:export')")
    @OperateLog(type = EXPORT)
    public void exportUserInviteLogExcel(@Valid UserInviteLogExportReqVO exportReqVO,
                                         HttpServletResponse response) throws IOException {
        List<UserInviteLogDO> list = userInviteLogService.getUserInviteLogList(exportReqVO);
        // 导出 Excel
        List<UserInviteLogExcelVO> datas = UserInviteLogConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "邀请日志.xls", "数据", UserInviteLogExcelVO.class, datas);
    }

}
