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.TbCurrency;
import com.ruoyi.project.business.domain.vo.currency.CurrencyReq;
import com.ruoyi.project.business.domain.vo.currency.CurrencySaveReq;
import com.ruoyi.project.business.service.ITbCurrencyService;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

    private final ITbCurrencyService tbCurrencyService;


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

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

    /**
     * 导出币种汇率列表
     */
    @Log(title = "币种汇率", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CurrencyReq currencyReq) {
        List<TbCurrency> list = tbCurrencyService.selectTbCurrencyList(currencyReq);
        ExcelUtil<TbCurrency> util = new ExcelUtil<>(TbCurrency.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 CurrencySaveReq tbCurrency) {
        return toAjax(tbCurrencyService.add(tbCurrency));
    }

    /**
     * 修改币种汇率
     */
    @Log(title = "币种汇率", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CurrencySaveReq 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());
    }

}
