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

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.storediscount.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.storediscount.StoreDiscountDO;
import cn.iocoder.yudao.module.member.service.storediscount.StoreDiscountService;

@Tag(name = "管理后台 - 门店超市折扣")
@RestController
@RequestMapping("/member/store-discount")
@Validated
public class StoreDiscountController {

    @Resource
    private StoreDiscountService storeDiscountService;

    @PostMapping("/create")
    @Operation(summary = "创建门店超市折扣")
    @PreAuthorize("@ss.hasPermission('member:store-discount:create')")
    public CommonResult<Long> createStoreDiscount(@Valid @RequestBody StoreDiscountSaveReqVO createReqVO) {
        return success(storeDiscountService.createStoreDiscount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新门店超市折扣")
    @PreAuthorize("@ss.hasPermission('member:store-discount:update')")
    public CommonResult<Boolean> updateStoreDiscount(@Valid @RequestBody StoreDiscountSaveReqVO updateReqVO) {
        storeDiscountService.updateStoreDiscount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除门店超市折扣")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:store-discount:delete')")
    public CommonResult<Boolean> deleteStoreDiscount(@RequestParam("id") Long id) {
        storeDiscountService.deleteStoreDiscount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得门店超市折扣")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:store-discount:query')")
    public CommonResult<StoreDiscountRespVO> getStoreDiscount(@RequestParam("id") Long id) {
        StoreDiscountDO storeDiscount = storeDiscountService.getStoreDiscount(id);
        return success(BeanUtils.toBean(storeDiscount, StoreDiscountRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得门店超市折扣分页")
    @PreAuthorize("@ss.hasPermission('member:store-discount:query')")
    public CommonResult<PageResult<StoreDiscountRespVO>> getStoreDiscountPage(@Valid StoreDiscountPageReqVO pageReqVO) {
        PageResult<StoreDiscountDO> pageResult = storeDiscountService.getStoreDiscountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreDiscountRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出门店超市折扣 Excel")
    @PreAuthorize("@ss.hasPermission('member:store-discount:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreDiscountExcel(@Valid StoreDiscountPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreDiscountDO> list = storeDiscountService.getStoreDiscountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "门店超市折扣.xls", "数据", StoreDiscountRespVO.class,
                        BeanUtils.toBean(list, StoreDiscountRespVO.class));
    }

}