package com.yc.ship.module.ota.controller.admin.distributorrecharge;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.ota.controller.admin.distributorrecharge.vo.DistributorRechargeAuditReqVO;
import com.yc.ship.module.ota.controller.admin.distributorrecharge.vo.DistributorRechargePageReqVO;
import com.yc.ship.module.ota.controller.admin.distributorrecharge.vo.DistributorRechargeRespVO;
import com.yc.ship.module.ota.controller.admin.distributorrecharge.vo.DistributorRechargeSaveReqVO;
import com.yc.ship.module.ota.service.distributorrecharge.DistributorRechargeService;
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.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 分销商充值")
@RestController
@RequestMapping("/distributor-recharge")
@Validated
public class DistributorRechargeController {

    @Resource
    private DistributorRechargeService distributorRechargeService;

    @PostMapping("/create")
    @Operation(summary = "创建分销商充值")
    @PreAuthorize("@ss.hasPermission('ota:distributor-recharge:create')")
    public CommonResult<Long> createDistributorRecharge(@Valid @RequestBody DistributorRechargeSaveReqVO createReqVO) {
        return success(distributorRechargeService.createDistributorRecharge(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分销商充值")
    @PreAuthorize("@ss.hasPermission('ota:distributor-recharge:update')")
    public CommonResult<Boolean> updateDistributorRecharge(@Valid @RequestBody DistributorRechargeSaveReqVO updateReqVO) {
        distributorRechargeService.updateDistributorRecharge(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分销商充值")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ota:distributor-recharge:delete')")
    public CommonResult<Boolean> deleteDistributorRecharge(@RequestParam("id") Long id) {
        distributorRechargeService.deleteDistributorRecharge(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分销商充值")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('ota:distributor-recharge:query')")
    public CommonResult<DistributorRechargeRespVO> getDistributorRecharge(@RequestParam("id") Long id) {
        DistributorRechargeRespVO respVO = distributorRechargeService.get(id);
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得分销商充值分页")
//    @PreAuthorize("@ss.hasPermission('ota:distributor-recharge:query')")
    public CommonResult<PageResult<DistributorRechargeRespVO>> getDistributorRechargePage(@Valid DistributorRechargePageReqVO pageReqVO) {
        PageResult<DistributorRechargeRespVO> pageResult = distributorRechargeService.getDistributorRechargePage(pageReqVO);
        return success(pageResult);
    }

    @PutMapping("/audit")
    @Operation(summary = "审核分销商充值")
    @PreAuthorize("@ss.hasPermission('ota:distributor-recharge:audit')")
    public CommonResult<Boolean> auditDistributorRecharge(@Valid @RequestBody DistributorRechargeAuditReqVO auditReqVO) {
        distributorRechargeService.auditDistributorRecharge(auditReqVO);
        return success(true);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分销商充值 Excel")
    @PreAuthorize("@ss.hasPermission('ota:distributor-recharge:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportDistributorRechargeExcel(@Valid DistributorRechargePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DistributorRechargeRespVO> list = distributorRechargeService.getDistributorRechargePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分销商充值.xls", "数据", DistributorRechargeRespVO.class, list);
    }

}