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

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.common.util.object.BeanUtils;
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.distributorselfrecharge.vo.DistributorSelfRechargePageReqVO;
import com.yc.ship.module.ota.controller.admin.distributorselfrecharge.vo.DistributorSelfRechargeRespVO;
import com.yc.ship.module.ota.controller.admin.distributorselfrecharge.vo.DistributorSelfRechargeSaveReqVO;
import com.yc.ship.module.ota.controller.admin.distributorselfrecharge.vo.DistributorSelfRechargeSimpleRespVO;
import com.yc.ship.module.ota.dal.dataobject.distributorselfrecharge.DistributorSelfRechargeDO;
import com.yc.ship.module.ota.service.distributorselfrecharge.DistributorSelfRechargeService;
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-self-recharge")
@Validated
public class DistributorSelfRechargeController {

    @Resource
    private DistributorSelfRechargeService distributorSelfRechargeService;

    @PostMapping("/create")
    @Operation(summary = "创建自助充值明细")
    @PreAuthorize("@ss.hasPermission('ota:distributor-self-recharge:create')")
    public CommonResult<DistributorSelfRechargeSimpleRespVO> createDistributorSelfRecharge(@Valid @RequestBody DistributorSelfRechargeSaveReqVO createReqVO) {
        return success(distributorSelfRechargeService.createDistributorSelfRecharge(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新自助充值明细")
    @PreAuthorize("@ss.hasPermission('ota:distributor-self-recharge:update')")
    public CommonResult<Boolean> updateDistributorSelfRecharge(@Valid @RequestBody DistributorSelfRechargeSaveReqVO updateReqVO) {
        distributorSelfRechargeService.updateDistributorSelfRecharge(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除自助充值明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ota:distributor-self-recharge:delete')")
    public CommonResult<Boolean> deleteDistributorSelfRecharge(@RequestParam("id") Long id) {
        distributorSelfRechargeService.deleteDistributorSelfRecharge(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得自助充值明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ota:distributor-self-recharge:query')")
    public CommonResult<DistributorSelfRechargeRespVO> getDistributorSelfRecharge(@RequestParam("id") Long id) {
        DistributorSelfRechargeDO distributorSelfRecharge = distributorSelfRechargeService.getDistributorSelfRecharge(id);
        return success(BeanUtils.toBean(distributorSelfRecharge, DistributorSelfRechargeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得自助充值明细分页")
    @PreAuthorize("@ss.hasPermission('ota:distributor-self-recharge:query')")
    public CommonResult<PageResult<DistributorSelfRechargeRespVO>> getDistributorSelfRechargePage(@Valid DistributorSelfRechargePageReqVO pageReqVO) {
        PageResult<DistributorSelfRechargeDO> pageResult = distributorSelfRechargeService.getDistributorSelfRechargePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DistributorSelfRechargeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出自助充值明细 Excel")
    @PreAuthorize("@ss.hasPermission('ota:distributor-self-recharge:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportDistributorSelfRechargeExcel(@Valid DistributorSelfRechargePageReqVO pageReqVO,
                                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DistributorSelfRechargeDO> list = distributorSelfRechargeService.getDistributorSelfRechargePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "自助充值明细.xls", "数据", DistributorSelfRechargeRespVO.class,
                BeanUtils.toBean(list, DistributorSelfRechargeRespVO.class));
    }

}
