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

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.userlevelequityref.vo.*;
import co.yixiang.yshop.module.member.dal.dataobject.userlevelequityref.UserLevelEquityRefDO;
import co.yixiang.yshop.module.member.convert.userlevelequityref.UserLevelEquityRefConvert;
import co.yixiang.yshop.module.member.service.userlevelequityref.UserLevelEquityRefService;

@Tag(name = "管理后台 - 会员等级权益中间")
@RestController
@RequestMapping("/member/user-level-equity-ref")
@Validated
public class UserLevelEquityRefController {

    @Resource
    private UserLevelEquityRefService userLevelEquityRefService;

    @PostMapping("/create")
    @Operation(summary = "创建会员等级权益中间")
    @PreAuthorize("@ss.hasPermission('member:user-level-equity-ref:create')")
    public CommonResult<Long> createUserLevelEquityRef(@Valid @RequestBody UserLevelEquityRefCreateReqVO createReqVO) {
        return success(userLevelEquityRefService.createUserLevelEquityRef(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员等级权益中间")
    @PreAuthorize("@ss.hasPermission('member:user-level-equity-ref:update')")
    public CommonResult<Boolean> updateUserLevelEquityRef(@Valid @RequestBody UserLevelEquityRefUpdateReqVO updateReqVO) {
        userLevelEquityRefService.updateUserLevelEquityRef(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员等级权益中间")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-level-equity-ref:delete')")
    public CommonResult<Boolean> deleteUserLevelEquityRef(@RequestParam("id") Long id) {
        userLevelEquityRefService.deleteUserLevelEquityRef(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员等级权益中间")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-level-equity-ref:query')")
    public CommonResult<UserLevelEquityRefRespVO> getUserLevelEquityRef(@RequestParam("id") Long id) {
        UserLevelEquityRefDO userLevelEquityRef = userLevelEquityRefService.getUserLevelEquityRef(id);
        return success(UserLevelEquityRefConvert.INSTANCE.convert(userLevelEquityRef));
    }

    @GetMapping("/list")
    @Operation(summary = "获得会员等级权益中间列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('member:user-level-equity-ref:query')")
    public CommonResult<List<UserLevelEquityRefRespVO>> getUserLevelEquityRefList(@RequestParam("ids") Collection<Long> ids) {
        List<UserLevelEquityRefDO> list = userLevelEquityRefService.getUserLevelEquityRefList(ids);
        return success(UserLevelEquityRefConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员等级权益中间分页")
    @PreAuthorize("@ss.hasPermission('member:user-level-equity-ref:query')")
    public CommonResult<PageResult<UserLevelEquityRefRespVO>> getUserLevelEquityRefPage(@Valid UserLevelEquityRefPageReqVO pageVO) {
        PageResult<UserLevelEquityRefDO> pageResult = userLevelEquityRefService.getUserLevelEquityRefPage(pageVO);
        return success(UserLevelEquityRefConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员等级权益中间 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-level-equity-ref:export')")
    @OperateLog(type = EXPORT)
    public void exportUserLevelEquityRefExcel(@Valid UserLevelEquityRefExportReqVO exportReqVO,
                                              HttpServletResponse response) throws IOException {
        List<UserLevelEquityRefDO> list = userLevelEquityRefService.getUserLevelEquityRefList(exportReqVO);
        // 导出 Excel
        List<UserLevelEquityRefExcelVO> datas = UserLevelEquityRefConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "会员等级权益中间.xls", "数据", UserLevelEquityRefExcelVO.class, datas);
    }

}
