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

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.userlevelgrowthvalueconfig.vo.*;
import co.yixiang.yshop.module.member.dal.dataobject.userlevelgrowthvalueconfig.UserLevelGrowthValueConfigDO;
import co.yixiang.yshop.module.member.convert.userlevelgrowthvalueconfig.UserLevelGrowthValueConfigConvert;
import co.yixiang.yshop.module.member.service.userlevelgrowthvalueconfig.UserLevelGrowthValueConfigService;

@Tag(name = "管理后台 - 会员成长任务配置")
@RestController
@RequestMapping("/member/user-level-growth-value-config")
@Validated
public class UserLevelGrowthValueConfigController {

    @Resource
    private UserLevelGrowthValueConfigService userLevelGrowthValueConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建会员成长任务配置")
    @PreAuthorize("@ss.hasPermission('member:user-level-growth-value-config:create')")
    public CommonResult<Integer> createUserLevelGrowthValueConfig(@Valid @RequestBody UserLevelGrowthValueConfigCreateReqVO createReqVO) {
        return success(userLevelGrowthValueConfigService.createUserLevelGrowthValueConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员成长任务配置")
    @PreAuthorize("@ss.hasPermission('member:user-level-growth-value-config:update')")
    public CommonResult<Boolean> updateUserLevelGrowthValueConfig(@Valid @RequestBody UserLevelGrowthValueConfigUpdateReqVO updateReqVO) {
        userLevelGrowthValueConfigService.updateUserLevelGrowthValueConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员成长任务配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-level-growth-value-config:delete')")
    public CommonResult<Boolean> deleteUserLevelGrowthValueConfig(@RequestParam("id") Integer id) {
        userLevelGrowthValueConfigService.deleteUserLevelGrowthValueConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员成长任务配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-level-growth-value-config:query')")
    public CommonResult<UserLevelGrowthValueConfigRespVO> getUserLevelGrowthValueConfig(@RequestParam("id") Integer id) {
        UserLevelGrowthValueConfigDO userLevelGrowthValueConfig = userLevelGrowthValueConfigService.getUserLevelGrowthValueConfig(id);
        return success(UserLevelGrowthValueConfigConvert.INSTANCE.convert(userLevelGrowthValueConfig));
    }

    @GetMapping("/list")
    @Operation(summary = "获得会员成长任务配置列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('member:user-level-growth-value-config:query')")
    public CommonResult<List<UserLevelGrowthValueConfigRespVO>> getUserLevelGrowthValueConfigList(@RequestParam("ids") Collection<Integer> ids) {
        List<UserLevelGrowthValueConfigDO> list = userLevelGrowthValueConfigService.getUserLevelGrowthValueConfigList(ids);
        return success(UserLevelGrowthValueConfigConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员成长任务配置分页")
    @PreAuthorize("@ss.hasPermission('member:user-level-growth-value-config:query')")
    public CommonResult<PageResult<UserLevelGrowthValueConfigRespVO>> getUserLevelGrowthValueConfigPage(@Valid UserLevelGrowthValueConfigPageReqVO pageVO) {
        PageResult<UserLevelGrowthValueConfigDO> pageResult = userLevelGrowthValueConfigService.getUserLevelGrowthValueConfigPage(pageVO);
        return success(UserLevelGrowthValueConfigConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员成长任务配置 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-level-growth-value-config:export')")
    @OperateLog(type = EXPORT)
    public void exportUserLevelGrowthValueConfigExcel(@Valid UserLevelGrowthValueConfigExportReqVO exportReqVO,
                                                      HttpServletResponse response) throws IOException {
        List<UserLevelGrowthValueConfigDO> list = userLevelGrowthValueConfigService.getUserLevelGrowthValueConfigList(exportReqVO);
        // 导出 Excel
        List<UserLevelGrowthValueConfigExcelVO> datas = UserLevelGrowthValueConfigConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "会员成长任务配置.xls", "数据", UserLevelGrowthValueConfigExcelVO.class, datas);
    }

}
