package com.trade.admin.tradingplatform.controller;

import com.trade.admin.system.controller.BaseController;
import com.trade.common.core.domain.ResponseResult;
import com.trade.common.core.page.GridPage;
import com.trade.common.enums.BusinessType;
import com.trade.admin.annotation.Log;
import com.trade.service.tradingplatform.domain.TradingPlatform;
import com.trade.service.tradingplatform.dto.TradingPlatformBaseInfo;
import com.trade.service.tradingplatform.dto.TradingPlatformDto;
import com.trade.service.tradingplatform.dto.TradingPlatformSearchDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.trade.admin.tradingplatform.service.TradingPlatformService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 贸易平台接口
 *
 * @author hlb
 * @since 2024-06-05 17:00:43
 */
@RestController
@RequestMapping("/tradingPlatform")
public class TradingPlatformController extends BaseController {

    /**
     * 服务对象
     */
    @Resource
    private TradingPlatformService tradingPlatformService;

    /**
     * 贸易平台基础信息列表查询
     *
     * @param search 查询实体
     * @return 所有数据
     */
    @GetMapping("/pages")
    @PreAuthorize("@ss.hasPermi('tradingPlatform:list')")
    public ResponseResult<GridPage<TradingPlatformBaseInfo>> getPages(TradingPlatformSearchDto search) {
        GridPage<TradingPlatformBaseInfo> pages = tradingPlatformService.getPages(search);
        return success(pages);
    }

    /**
     * 获取所有平台列表(页面查询条件)
     * 
     * @return
     */
    @GetMapping("/list")
    public ResponseResult<List<TradingPlatform>> getList() {
        List<TradingPlatform> pages = tradingPlatformService.getList();
        return success(pages);
    }

    /**
     * 根据id查询贸易平台详细信息
     *
     * @param id 主键
     * @return 贸易平台
     */
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermi('tradingPlatform:detil')")
    public ResponseResult<TradingPlatformDto> queryById(@PathVariable Long id) {
        TradingPlatformDto tradingPlatformDto = tradingPlatformService.queryById(id);
        return success(tradingPlatformDto);
    }

    /**
     * 新增平台
     *
     * @param confirmed 是否是确认true or false），
     * @param tradingPlatformDto 实体对象
     * @return 贸易平台id
     */
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('tradingPlatform:add')")
    @Log(title = "贸易平台", businessType = BusinessType.INSERT)
    public ResponseResult<Long> saveTradingPlatform(
            @RequestParam(value = "confirmed", required = false, defaultValue = "false") Boolean confirmed,
            @RequestBody TradingPlatformDto tradingPlatformDto) {
        Long id = tradingPlatformService.saveTradingPlatform(confirmed, tradingPlatformDto);
        return success(id);
    }

    /**
     * 编辑贸易平台
     * 
     * @param confirmed 是否是确认（true or false），
     * @param tradingPlatformDto 实体对象
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('tradingPlatform:update')")
    @Log(title = "贸易平台", businessType = BusinessType.INSERT)
    public ResponseResult<Long> updateTradingPlatform(
            @RequestParam(value = "confirmed", required = false, defaultValue = "false") Boolean confirmed,
            @RequestBody TradingPlatformDto tradingPlatformDto) {
        Long id = tradingPlatformService.saveTradingPlatform(confirmed, tradingPlatformDto);
        return success(id);
    }

    /**
     * 获取拥有权限的贸易平台（仅针对贸易平台的出口退税办理人）
     */
    @GetMapping("/getPermissionTradePlatforms")
    @ResponseBody
    @PreAuthorize("@ss.hasRole('EXPORT_TAX_REFUNDER')")
    public ResponseResult<List<TradingPlatform>> getPermissionTradePlatforms() {
        return success(tradingPlatformService.getPermissionTradePlatforms());
    }
}
