package cn.iocoder.yudao.module.pay.controller.admin.divide;

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.pay.controller.admin.divide.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.divide.DivideDO;
import cn.iocoder.yudao.module.pay.service.divide.DivideService;

@Tag(name = "管理后台 - 分账单")
@RestController
@RequestMapping("/pay/divide")
@Validated
public class DivideController {

    @Resource
    private DivideService divideService;

    @PostMapping("/create")
    @Operation(summary = "创建分账单")
    @PreAuthorize("@ss.hasPermission('pay:divide:create')")
    public CommonResult<Long> createDivide(@Valid @RequestBody DivideSaveReqVO createReqVO) {
        return success(divideService.createDivide(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分账单")
    @PreAuthorize("@ss.hasPermission('pay:divide:update')")
    public CommonResult<Boolean> updateDivide(@Valid @RequestBody DivideSaveReqVO updateReqVO) {
        divideService.updateDivide(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分账单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pay:divide:delete')")
    public CommonResult<Boolean> deleteDivide(@RequestParam("id") Long id) {
        divideService.deleteDivide(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分账单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:divide:query')")
    public CommonResult<DivideRespVO> getDivide(@RequestParam("id") Long id) {
        DivideDO divide = divideService.getDivide(id);
        return success(BeanUtils.toBean(divide, DivideRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分账单分页")
    @PreAuthorize("@ss.hasPermission('pay:divide:query')")
    public CommonResult<PageResult<DivideRespVO>> getDividePage(@Valid DividePageReqVO pageReqVO) {
        PageResult<DivideDO> pageResult = divideService.getDividePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DivideRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分账单 Excel")
    @PreAuthorize("@ss.hasPermission('pay:divide:export')")
    @OperateLog(type = EXPORT)
    public void exportDivideExcel(@Valid DividePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DivideDO> list = divideService.getDividePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分账单.xls", "数据", DivideRespVO.class,
                        BeanUtils.toBean(list, DivideRespVO.class));
    }

}