package com.yc.ship.module.otc.controller.admin.storeplatcreditlog;

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.otc.controller.admin.storeplatcreditlog.vo.StorePlatCreditLogPageReqVO;
import com.yc.ship.module.otc.controller.admin.storeplatcreditlog.vo.StorePlatCreditLogRespVO;
import com.yc.ship.module.otc.controller.admin.storeplatcreditlog.vo.StorePlatCreditLogSaveReqVO;
import com.yc.ship.module.otc.dal.dataobject.storecreditlog.StorePlatCreditLogDO;
import com.yc.ship.module.otc.service.storecreditlog.StorePlatCreditLogService;
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.*;


@Tag(name = "管理后台 - 旅行社门店平台授信额度变更")
@RestController
@RequestMapping("/store-plat-credit-log")
@Validated
public class StorePlatCreditLogController {

    @Resource
    private StorePlatCreditLogService storePlatCreditLogService;

    @PostMapping("/create")
    @Operation(summary = "创建旅行社门店平台授信额度变更")
    @PreAuthorize("@ss.hasPermission('otc:store-plat-credit-log:create')")
    public CommonResult<Long> createStorePlatCreditLog(@Valid @RequestBody StorePlatCreditLogSaveReqVO createReqVO) {
        return success(storePlatCreditLogService.createStorePlatCreditLog(createReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得旅行社门店平台授信额度变更")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('otc:store-plat-credit-log:query')")
    public CommonResult<StorePlatCreditLogRespVO> getStorePlatCreditLog(@RequestParam("id") Long id) {
        StorePlatCreditLogDO storePlatCreditLog = storePlatCreditLogService.getStorePlatCreditLog(id);
        return success(BeanUtils.toBean(storePlatCreditLog, StorePlatCreditLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得旅行社门店平台授信额度变更分页")
    @PreAuthorize("@ss.hasPermission('otc:store-plat-credit-log:query')")
    public CommonResult<PageResult<StorePlatCreditLogRespVO>> getStorePlatCreditLogPage(@Valid StorePlatCreditLogPageReqVO pageReqVO) {
        PageResult<StorePlatCreditLogDO> pageResult = storePlatCreditLogService.getStorePlatCreditLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StorePlatCreditLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出旅行社门店平台授信额度变更 Excel")
    @PreAuthorize("@ss.hasPermission('otc:store-plat-credit-log:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportStorePlatCreditLogExcel(@Valid StorePlatCreditLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StorePlatCreditLogDO> list = storePlatCreditLogService.getStorePlatCreditLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "旅行社门店平台授信额度变更.xls", "数据", StorePlatCreditLogRespVO.class,
                        BeanUtils.toBean(list, StorePlatCreditLogRespVO.class));
    }

}