package cn.newfeifan.mall.module.distri.controller.admin.dailyawardrecords;

import org.springframework.context.annotation.Lazy;
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.*;
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.controller.admin.dailyawardrecords.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.dailyawardrecords.DailyAwardRecordsDO;
import cn.newfeifan.mall.module.distri.service.dailyawardrecords.DailyAwardRecordsService;

@Tag(name = "管理后台 - 每日用户获得合赢奖记录")
@RestController
@RequestMapping("/distri/daily-award-records")
@Validated
public class DailyAwardRecordsController {

    @Resource
    @Lazy
    private DailyAwardRecordsService dailyAwardRecordsService;

    @PostMapping("/create")
    @Operation(summary = "创建每日用户获得合赢奖记录")
    @PreAuthorize("@ss.hasPermission('distri:daily-award-records:create')")
    public CommonResult<Long> createDailyAwardRecords(@Valid @RequestBody DailyAwardRecordsSaveReqVO createReqVO) {
        return success(dailyAwardRecordsService.createDailyAwardRecords(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新每日用户获得合赢奖记录")
    @PreAuthorize("@ss.hasPermission('distri:daily-award-records:update')")
    public CommonResult<Boolean> updateDailyAwardRecords(@Valid @RequestBody DailyAwardRecordsSaveReqVO updateReqVO) {
        dailyAwardRecordsService.updateDailyAwardRecords(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除每日用户获得合赢奖记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:daily-award-records:delete')")
    public CommonResult<Boolean> deleteDailyAwardRecords(@RequestParam("id") Long id) {
        dailyAwardRecordsService.deleteDailyAwardRecords(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得每日用户获得合赢奖记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:daily-award-records:query')")
    public CommonResult<DailyAwardRecordsRespVO> getDailyAwardRecords(@RequestParam("id") Long id) {
        DailyAwardRecordsDO dailyAwardRecords = dailyAwardRecordsService.getDailyAwardRecords(id);
        return success(BeanUtils.toBean(dailyAwardRecords, DailyAwardRecordsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得每日用户获得合赢奖记录分页")
    @PreAuthorize("@ss.hasPermission('distri:daily-award-records:query')")
    public CommonResult<PageResult<DailyAwardRecordsRespVO>> getDailyAwardRecordsPage(@Valid DailyAwardRecordsPageReqVO pageReqVO) {
        PageResult<DailyAwardRecordsDO> pageResult = dailyAwardRecordsService.getDailyAwardRecordsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DailyAwardRecordsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出每日用户获得合赢奖记录 Excel")
    @PreAuthorize("@ss.hasPermission('distri:daily-award-records:export')")
    @OperateLog(type = EXPORT)
    public void exportDailyAwardRecordsExcel(@Valid DailyAwardRecordsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DailyAwardRecordsDO> list = dailyAwardRecordsService.getDailyAwardRecordsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "每日用户获得合赢奖记录.xls", "数据", DailyAwardRecordsRespVO.class,
                        BeanUtils.toBean(list, DailyAwardRecordsRespVO.class));
    }

}