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

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.divideinfo.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.divideinfo.DivideInfoDO;
import cn.iocoder.yudao.module.pay.service.divideinfo.DivideInfoService;

@Tag(name = "管理后台 - 分账明细")
@RestController
@RequestMapping("/pay/divide-info")
@Validated
public class DivideInfoController {

    @Resource
    private DivideInfoService divideInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建分账明细")
    @PreAuthorize("@ss.hasPermission('pay:divide-info:create')")
    public CommonResult<Long> createDivideInfo(@Valid @RequestBody DivideInfoSaveReqVO createReqVO) {
        return success(divideInfoService.createDivideInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分账明细")
    @PreAuthorize("@ss.hasPermission('pay:divide-info:update')")
    public CommonResult<Boolean> updateDivideInfo(@Valid @RequestBody DivideInfoSaveReqVO updateReqVO) {
        divideInfoService.updateDivideInfo(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得分账明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:divide-info:query')")
    public CommonResult<DivideInfoRespVO> getDivideInfo(@RequestParam("id") Long id) {
        DivideInfoDO divideInfo = divideInfoService.getDivideInfo(id);
        return success(BeanUtils.toBean(divideInfo, DivideInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分账明细分页")
    @PreAuthorize("@ss.hasPermission('pay:divide-info:query')")
    public CommonResult<PageResult<DivideInfoRespVO>> getDivideInfoPage(@Valid DivideInfoPageReqVO pageReqVO) {
        PageResult<DivideInfoDO> pageResult = divideInfoService.getDivideInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DivideInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分账明细 Excel")
    @PreAuthorize("@ss.hasPermission('pay:divide-info:export')")
    @OperateLog(type = EXPORT)
    public void exportDivideInfoExcel(@Valid DivideInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DivideInfoDO> list = divideInfoService.getDivideInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分账明细.xls", "数据", DivideInfoRespVO.class,
                        BeanUtils.toBean(list, DivideInfoRespVO.class));
    }

}