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

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.dividecompany.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.dividecompany.DivideCompanyDO;
import cn.iocoder.yudao.module.pay.service.dividecompany.DivideCompanyService;

@Tag(name = "管理后台 - 分账公司")
@RestController
@RequestMapping("/pay/divide-company")
@Validated
public class DivideCompanyController {

    @Resource
    private DivideCompanyService divideCompanyService;

    @PostMapping("/create")
    @Operation(summary = "创建分账公司")
    @PreAuthorize("@ss.hasPermission('pay:divide-company:create')")
    public CommonResult<Long> createDivideCompany(@Valid @RequestBody DivideCompanySaveReqVO createReqVO) {
        return success(divideCompanyService.createDivideCompany(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分账公司")
    @PreAuthorize("@ss.hasPermission('pay:divide-company:update')")
    public CommonResult<Boolean> updateDivideCompany(@Valid @RequestBody DivideCompanySaveReqVO updateReqVO) {
        divideCompanyService.updateDivideCompany(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得分账公司")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:divide-company:query')")
    public CommonResult<DivideCompanyRespVO> getDivideCompany(@RequestParam("id") Long id) {
        DivideCompanyDO divideCompany = divideCompanyService.getDivideCompany(id);
        return success(BeanUtils.toBean(divideCompany, DivideCompanyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分账公司分页")
    @PreAuthorize("@ss.hasPermission('pay:divide-company:query')")
    public CommonResult<PageResult<DivideCompanyRespVO>> getDivideCompanyPage(@Valid DivideCompanyPageReqVO pageReqVO) {
        PageResult<DivideCompanyDO> pageResult = divideCompanyService.getDivideCompanyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DivideCompanyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分账公司 Excel")
    @PreAuthorize("@ss.hasPermission('pay:divide-company:export')")
    @OperateLog(type = EXPORT)
    public void exportDivideCompanyExcel(@Valid DivideCompanyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DivideCompanyDO> list = divideCompanyService.getDivideCompanyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分账公司.xls", "数据", DivideCompanyRespVO.class,
                        BeanUtils.toBean(list, DivideCompanyRespVO.class));
    }

}