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

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.storebusiness.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.storebusiness.StoreBusinessDO;
import cn.iocoder.yudao.module.member.service.storebusiness.StoreBusinessService;

@Tag(name = "管理后台 - 超市营业统计")
@RestController
@RequestMapping("/member/store-business")
@Validated
public class StoreBusinessController {

    @Resource
    private StoreBusinessService storeBusinessService;

    @PostMapping("/create")
    @Operation(summary = "创建超市营业统计")
    @PreAuthorize("@ss.hasPermission('member:store-business:create')")
    public CommonResult<Long> createStoreBusiness(@Valid @RequestBody StoreBusinessSaveReqVO createReqVO) {
        return success(storeBusinessService.createStoreBusiness(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新超市营业统计")
    @PreAuthorize("@ss.hasPermission('member:store-business:update')")
    public CommonResult<Boolean> updateStoreBusiness(@Valid @RequestBody StoreBusinessSaveReqVO updateReqVO) {
        storeBusinessService.updateStoreBusiness(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除超市营业统计")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:store-business:delete')")
    public CommonResult<Boolean> deleteStoreBusiness(@RequestParam("id") Long id) {
        storeBusinessService.deleteStoreBusiness(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得超市营业统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:store-business:query')")
    public CommonResult<StoreBusinessRespVO> getStoreBusiness(@RequestParam("id") Long id) {
        StoreBusinessDO storeBusiness = storeBusinessService.getStoreBusiness(id);
        return success(BeanUtils.toBean(storeBusiness, StoreBusinessRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得超市营业统计分页")
    @PreAuthorize("@ss.hasPermission('member:store-business:query')")
    public CommonResult<PageResult<StoreBusinessRespVO>> getStoreBusinessPage(@Valid StoreBusinessPageReqVO pageReqVO) {
        PageResult<StoreBusinessDO> pageResult = storeBusinessService.getStoreBusinessPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreBusinessRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出超市营业统计 Excel")
    @PreAuthorize("@ss.hasPermission('member:store-business:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreBusinessExcel(@Valid StoreBusinessPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreBusinessDO> list = storeBusinessService.getStoreBusinessPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "超市营业统计.xls", "数据", StoreBusinessRespVO.class,
                        BeanUtils.toBean(list, StoreBusinessRespVO.class));
    }

}