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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.business.vo.BusinessPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.business.vo.BusinessRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.business.BusinessDO;
import cn.iocoder.yudao.module.member.service.business.BusinessService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 营业统计")
@RestController
@RequestMapping("/t/business")
@Validated
public class BusinessController {

    @Resource
    private BusinessService businessService;

//    @PostMapping("/create")
//    @Operation(summary = "创建营业统计")
//    @PreAuthorize("@ss.hasPermission('t:business:create')")
//    public CommonResult<Long> createBusiness(@Valid @RequestBody BusinessSaveReqVO createReqVO) {
//        return success(businessService.createBusiness(createReqVO));
//    }

//    @PutMapping("/update")
//    @Operation(summary = "更新营业统计")
//    @PreAuthorize("@ss.hasPermission('t:business:update')")
//    public CommonResult<Boolean> updateBusiness(@Valid @RequestBody BusinessSaveReqVO updateReqVO) {
//        businessService.updateBusiness(updateReqVO);
//        return success(true);
//    }

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

    @GetMapping("/get")
    @Operation(summary = "获得营业统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('t:business:query')")
    public CommonResult<BusinessRespVO> getBusiness(@RequestParam("id") Long id) {
        BusinessDO business = businessService.getBusiness(id);
        return success(BeanUtils.toBean(business, BusinessRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得营业统计分页")
    //@PreAuthorize("@ss.hasPermission('t:business:query')")
    public CommonResult<PageResult<BusinessRespVO>> getBusinessPage(@Valid BusinessPageReqVO pageReqVO) {
        PageResult<BusinessDO> pageResult = businessService.getBusinessPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BusinessRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出营业统计 Excel")
    //@PreAuthorize("@ss.hasPermission('t:business:export')")
    @OperateLog(type = EXPORT)
    public void exportBusinessExcel(@Valid BusinessPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessDO> list = businessService.getBusinessPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "营业统计.xls", "数据", BusinessRespVO.class,
                        BeanUtils.toBean(list, BusinessRespVO.class));

    }
}