package cn.newfeifan.mall.module.distri.controller.app.usercollectbefore;

import cn.newfeifan.mall.framework.security.core.annotations.PreAuthenticated;
import cn.newfeifan.mall.module.distri.controller.app.usercollectbefore.vo.UserCollectBeforePageReqVO;
import cn.newfeifan.mall.module.distri.controller.app.usercollectbefore.vo.UserCollectBeforeRespVO;
import cn.newfeifan.mall.module.distri.controller.app.usercollectbefore.vo.UserCollectBeforeSaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
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.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;

import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;

import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.newfeifan.mall.module.distri.dal.dataobject.usercollectbefore.UserCollectBeforeDO;
import cn.newfeifan.mall.module.distri.service.usercollectbefore.UserCollectBeforeService;

@Tag(name = "用户APP - 用户曾经收藏过什么商品的")
@RestController
@RequestMapping("/distri/user-collect-before")
@Validated
public class UserCollectBeforeController {

    @Resource
    private UserCollectBeforeService userCollectBeforeService;

    @PostMapping("/create")
    @Operation(summary = "创建用户曾经收藏过什么商品的")
    @PreAuthenticated
    public CommonResult<Long> createUserCollectBefore(@Valid @RequestBody UserCollectBeforeSaveReqVO createReqVO) {
        return success(userCollectBeforeService.createUserCollectBefore(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户曾经收藏过什么商品的")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserCollectBefore(@Valid @RequestBody UserCollectBeforeSaveReqVO updateReqVO) {
        userCollectBeforeService.updateUserCollectBefore(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户曾经收藏过什么商品的")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthenticated
    public CommonResult<Boolean> deleteUserCollectBefore(@RequestParam("id") Long id) {
        userCollectBeforeService.deleteUserCollectBefore(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户曾经收藏过什么商品的")
    @PreAuthenticated
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<UserCollectBeforeRespVO> getUserCollectBefore(@RequestParam("id") Long id) {
        UserCollectBeforeDO userCollectBefore = userCollectBeforeService.getUserCollectBefore(id);
        return success(BeanUtils.toBean(userCollectBefore, UserCollectBeforeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户曾经收藏过什么商品的分页")
    @PreAuthenticated
    public CommonResult<PageResult<UserCollectBeforeRespVO>> getUserCollectBeforePage(@Valid UserCollectBeforePageReqVO pageReqVO) {
        PageResult<UserCollectBeforeDO> pageResult = userCollectBeforeService.getUserCollectBeforePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserCollectBeforeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户曾经收藏过什么商品的 Excel")
    @OperateLog(type = EXPORT)
    @PreAuthenticated
    public void exportUserCollectBeforeExcel(@Valid UserCollectBeforePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserCollectBeforeDO> list = userCollectBeforeService.getUserCollectBeforePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户曾经收藏过什么商品的.xls", "数据", UserCollectBeforeRespVO.class,
                        BeanUtils.toBean(list, UserCollectBeforeRespVO.class));
    }

}