package com.bizzan.bitrade.controller.system;

import com.bizzan.bitrade.controller.common.BaseAdminController;
import com.bizzan.bitrade.entity.CurrencyCoin;
import com.bizzan.bitrade.service.CurrencyCoinService;
import com.bizzan.bitrade.util.MessageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 法币管理
 */
@RestController
@RequestMapping("/system/currency-coin")
@Slf4j
public class CurrencyCoinController extends BaseAdminController {

    @Autowired
    private CurrencyCoinService currencyCoinService;

    /**
     * 分页查询
     */
    @PostMapping("page-query")
    public MessageResult pageQuery(
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "15") int pageSize,
            @RequestParam(required = false) String currency
    ) {
        Pageable pageable = new PageRequest(pageNo - 1, pageSize, new Sort(Sort.Direction.ASC, "currency"));
        Page<CurrencyCoin> page = currencyCoinService.findByCondition(currency, pageable);
        return success(page);
    }

    /**
     * 全量列表（供下拉选择用）
     */
    @GetMapping("list")
    public MessageResult list() {
        List<CurrencyCoin> list = currencyCoinService.findAll();
        return success(list);
    }

    /**
     * 详情
     */
    @GetMapping("detail")
    public MessageResult detail(@RequestParam Long id) {
        CurrencyCoin coin = currencyCoinService.findOne(id);
        Assert.notNull(coin, "记录不存在");
        return success(coin);
    }

    /**
     * 新增
     */
    @PostMapping("create")
    public MessageResult create(
            @RequestParam String currency,
            @RequestParam String symbol,
            @RequestParam BigDecimal rate,
            @RequestParam Integer coinPrecision
    ) {
        if (currencyCoinService.findByCurrency(currency) != null) {
            return error("法币名称已存在: " + currency);
        }
        CurrencyCoin coin = new CurrencyCoin();
        coin.setCurrency(currency.toUpperCase().trim());
        coin.setSymbol(symbol.trim());
        coin.setRate(rate);
        coin.setCoinPrecision(coinPrecision);
        return success(currencyCoinService.create(coin));
    }

    /**
     * 修改
     */
    @PostMapping("update")
    public MessageResult update(
            @RequestParam Long id,
            @RequestParam String currency,
            @RequestParam String symbol,
            @RequestParam BigDecimal rate,
            @RequestParam Integer coinPrecision
    ) {
        CurrencyCoin coin = currencyCoinService.findOne(id);
        Assert.notNull(coin, "记录不存在");
        // 检查 currency 是否与其他记录重复
        CurrencyCoin existing = currencyCoinService.findByCurrency(currency.toUpperCase().trim());
        if (existing != null && !existing.getId().equals(id)) {
            return error("法币名称已存在: " + currency);
        }
        coin.setCurrency(currency.toUpperCase().trim());
        coin.setSymbol(symbol.trim());
        coin.setRate(rate);
        coin.setCoinPrecision(coinPrecision);
        return success(currencyCoinService.update(coin));
    }

    /**
     * 批量删除
     */
    @PostMapping("deletes")
    public MessageResult deletes(@RequestParam String ids) {
        String[] idArr = ids.split(",");
        Long[] longIds = new Long[idArr.length];
        for (int i = 0; i < idArr.length; i++) {
            longIds[i] = Long.parseLong(idArr[i].trim());
        }
        currencyCoinService.deleteBatch(longIds);
        return success();
    }
}
