package cn.iocoder.yudao.module.member.controller.admin.userpreference;

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 cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.userpreference.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userpreference.UserPreferenceDO;
import cn.iocoder.yudao.module.member.service.userpreference.UserPreferenceService;

@Tag(name = "管理后台 - 用户偏好")
@RestController
@RequestMapping("/member/user-preference")
@Validated
public class UserPreferenceController {

    @Resource
    private UserPreferenceService userPreferenceService;

    @PostMapping("/create")
    @Operation(summary = "创建用户偏好")
    @PreAuthorize("@ss.hasPermission('member:user-preference:create')")
    public CommonResult<Long> createUserPreference(@Valid @RequestBody UserPreferenceSaveReqVO createReqVO) {
        return success(userPreferenceService.createUserPreference(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户偏好")
    @PreAuthorize("@ss.hasPermission('member:user-preference:update')")
    public CommonResult<Boolean> updateUserPreference(@Valid @RequestBody UserPreferenceSaveReqVO updateReqVO) {
        userPreferenceService.updateUserPreference(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户偏好")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-preference:delete')")
    public CommonResult<Boolean> deleteUserPreference(@RequestParam("id") Long id) {
        userPreferenceService.deleteUserPreference(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户偏好")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-preference:query')")
    public CommonResult<UserPreferenceRespVO> getUserPreference(@RequestParam("id") Long id) {
        UserPreferenceDO userPreference = userPreferenceService.getUserPreference(id);
        return success(BeanUtils.toBean(userPreference, UserPreferenceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户偏好分页")
    @PreAuthorize("@ss.hasPermission('member:user-preference:query')")
    public CommonResult<PageResult<UserPreferenceRespVO>> getUserPreferencePage(@Valid UserPreferencePageReqVO pageReqVO) {
        PageResult<UserPreferenceDO> pageResult = userPreferenceService.getUserPreferencePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserPreferenceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户偏好 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-preference:export')")
    @OperateLog(type = EXPORT)
    public void exportUserPreferenceExcel(@Valid UserPreferencePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserPreferenceDO> list = userPreferenceService.getUserPreferencePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户偏好.xls", "数据", UserPreferenceRespVO.class,
                        BeanUtils.toBean(list, UserPreferenceRespVO.class));
    }

}