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

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.userlevelequity.vo.*;
import co.yixiang.yshop.module.member.dal.dataobject.userlevelequity.UserLevelEquityDO;
import co.yixiang.yshop.module.member.convert.userlevelequity.UserLevelEquityConvert;
import co.yixiang.yshop.module.member.service.userlevelequity.UserLevelEquityService;

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

    @Resource
    private UserLevelEquityService userLevelEquityService;

    @PostMapping("/create")
    @Operation(summary = "创建会员等级权益配置")
    @PreAuthorize("@ss.hasPermission('member:user-level-equity:create')")
    public CommonResult<Long> createUserLevelEquity(@Valid @RequestBody UserLevelEquityCreateReqVO createReqVO) {
        return success(userLevelEquityService.createUserLevelEquity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员等级权益配置")
    @PreAuthorize("@ss.hasPermission('member:user-level-equity:update')")
    public CommonResult<Boolean> updateUserLevelEquity(@Valid @RequestBody UserLevelEquityUpdateReqVO updateReqVO) {
        userLevelEquityService.updateUserLevelEquity(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得会员等级权益配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-level-equity:query')")
    public CommonResult<UserLevelEquityRespVO> getUserLevelEquity(@RequestParam("id") Long id) {
        UserLevelEquityDO userLevelEquity = userLevelEquityService.getUserLevelEquity(id);
        return success(UserLevelEquityConvert.INSTANCE.convert(userLevelEquity));
    }

    @GetMapping("/list")
    @Operation(summary = "获得会员等级权益配置列表")
    @PreAuthorize("@ss.hasPermission('member:user-level-equity:query')")
    public CommonResult<List<UserLevelEquityRespVO>> getUserLevelEquityList() {
        List<UserLevelEquityDO> list = userLevelEquityService.getUserLevelEquityList();
        return success(UserLevelEquityConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员等级权益配置分页")
    @PreAuthorize("@ss.hasPermission('member:user-level-equity:query')")
    public CommonResult<PageResult<UserLevelEquityRespVO>> getUserLevelEquityPage(@Valid UserLevelEquityPageReqVO pageVO) {
        PageResult<UserLevelEquityDO> pageResult = userLevelEquityService.getUserLevelEquityPage(pageVO);
        return success(UserLevelEquityConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员等级权益配置 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-level-equity:export')")
    @OperateLog(type = EXPORT)
    public void exportUserLevelEquityExcel(@Valid UserLevelEquityExportReqVO exportReqVO,
                                           HttpServletResponse response) throws IOException {
        List<UserLevelEquityDO> list = userLevelEquityService.getUserLevelEquityList(exportReqVO);
        // 导出 Excel
        List<UserLevelEquityExcelVO> datas = UserLevelEquityConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "会员等级权益配置.xls", "数据", UserLevelEquityExcelVO.class, datas);
    }

}
