package com.zt.plat.module.contractorder.controller.admin.amountdismantle;

import com.zt.plat.module.contractorder.api.vo.contract.AmountDismantlePageReqVO;
import com.zt.plat.module.contractorder.api.vo.contract.AmountDismantleRespVO;
import com.zt.plat.module.contractorder.api.vo.contract.AmountDismantleSaveReqVO;
import com.zt.plat.module.contractorder.dal.dataobject.amountdismantle.AmountDismantleDO;
import com.zt.plat.module.contractorder.service.amountdismantle.AmountDismantleService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.framework.excel.core.util.ExcelUtils;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 结算金额拆分")
@RestController
@RequestMapping("/bse/amount-dismantle")
@Validated
public class AmountDismantleController implements BusinessControllerMarker {


    @Resource
    private AmountDismantleService amountDismantleService;

    @PostMapping("/create")
    @Operation(summary = "创建结算金额拆分")
    @PreAuthorize("@ss.hasPermission('bse:amount-dismantle:create')")
    public CommonResult<AmountDismantleRespVO> createAmountDismantle(@Valid @RequestBody AmountDismantleSaveReqVO createReqVO) {
        return success(amountDismantleService.createAmountDismantle(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新结算金额拆分")
    @PreAuthorize("@ss.hasPermission('bse:amount-dismantle:update')")
    public CommonResult<Boolean> updateAmountDismantle(@Valid @RequestBody AmountDismantleSaveReqVO updateReqVO) {
        amountDismantleService.updateAmountDismantle(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除结算金额拆分")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bse:amount-dismantle:delete')")
    public CommonResult<Boolean> deleteAmountDismantle(@RequestParam("id") Long id) {
        amountDismantleService.deleteAmountDismantle(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除结算金额拆分")
                @PreAuthorize("@ss.hasPermission('bse:amount-dismantle:delete')")
    public CommonResult<Boolean> deleteAmountDismantleList(@RequestBody BatchDeleteReqVO req) {
        amountDismantleService.deleteAmountDismantleListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得结算金额拆分")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bse:amount-dismantle:query')")
    public CommonResult<AmountDismantleRespVO> getAmountDismantle(@RequestParam("id") Long id) {
        AmountDismantleDO amountDismantle = amountDismantleService.getAmountDismantle(id);
        return success(BeanUtils.toBean(amountDismantle, AmountDismantleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得结算金额拆分分页")
    @PreAuthorize("@ss.hasPermission('bse:amount-dismantle:query')")
    public CommonResult<PageResult<AmountDismantleRespVO>> getAmountDismantlePage(@Valid AmountDismantlePageReqVO pageReqVO) {
        PageResult<AmountDismantleDO> pageResult = amountDismantleService.getAmountDismantlePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AmountDismantleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出结算金额拆分 Excel")
    @PreAuthorize("@ss.hasPermission('bse:amount-dismantle:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAmountDismantleExcel(@Valid AmountDismantlePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AmountDismantleDO> list = amountDismantleService.getAmountDismantlePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "结算金额拆分.xls", "数据", AmountDismantleRespVO.class,
                        BeanUtils.toBean(list, AmountDismantleRespVO.class));
    }

}