package com.ruoyi.project.business.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.domain.TbContractCurrency;
import com.ruoyi.project.business.domain.vo.currency.ContractCurrencyReq;
import com.ruoyi.project.business.domain.vo.currency.ContractCurrencySaveReq;
import com.ruoyi.project.business.service.ITbContractCurrencyService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 币种汇率Controller
 *
 * @author ruoyi
 * @date 2024-05-14
 */
@RestController
@RequestMapping("/user/contract/currency")
@RequiredArgsConstructor
public class TbContractCurrencyController extends BaseController {

    private final ITbContractCurrencyService tbCurrencyService;


    /**
     * 查询币种汇率列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ContractCurrencyReq currencyReq) {
        startPage();
        List<TbContractCurrency> list = tbCurrencyService.selectTbContractCurrencyList(currencyReq);
        return getDataTable(list);
    }

    /**
     * 查询币种汇率列表
     */
    @GetMapping("/listAll")
    public TableDataInfo listAll() {
        List<TbContractCurrency> list = tbCurrencyService.listAll();
        return getDataTable(list);
    }

    /**
     * 导出币种汇率列表
     */
    @Log(title = "币种汇率", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ContractCurrencyReq currencyReq) {
        List<TbContractCurrency> list = tbCurrencyService.selectTbContractCurrencyList(currencyReq);
        ExcelUtil<TbContractCurrency> util = new ExcelUtil<>(TbContractCurrency.class);
        util.exportExcel(response, list, "币种汇率数据");
    }

    /**
     * 获取币种汇率详细信息
     */
    @GetMapping(value = "/{currencyId}")
    public AjaxResult getInfo(@PathVariable("currencyId") Long currencyId) {
        return success(tbCurrencyService.getInfo(currencyId));
    }

    /**
     * 新增币种汇率
     */
    @Log(title = "币种汇率", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ContractCurrencySaveReq tbCurrency) {
        return toAjax(tbCurrencyService.add(tbCurrency));
    }

    /**
     * 修改币种汇率
     */
    @Log(title = "币种汇率", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ContractCurrencySaveReq tbCurrency) {
        return toAjax(tbCurrencyService.edit(tbCurrency));
    }

    /**
     * 删除币种汇率
     */
    @Log(title = "币种汇率", businessType = BusinessType.DELETE)
    @DeleteMapping("/{currencyIds}")
    public AjaxResult remove(@PathVariable Long[] currencyIds) {
        // return toAjax(tbCurrencyService.removeByIds(Arrays.asList(currencyIds)));
        return toAjax(tbCurrencyService.remove(Arrays.asList(currencyIds)));
    }

    /**
     * 根据币种查询可用的汇率
     */
    @GetMapping("/usable")
    public TableDataInfo getUsableCurrencyRate() {
        return getDataTable(tbCurrencyService.getUsableCurrencyRate());
    }

}
