package com.ruoyi.web.controller.system;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ss.channel.domain.Channel;
import com.ruoyi.ss.channel.domain.ChannelQuery;
import com.ruoyi.ss.channel.domain.ChannelVO;
import com.ruoyi.ss.channel.service.IChannelService;
import com.ruoyi.ss.userWithdraw.domain.UserWithdraw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static com.ruoyi.framework.datasource.DynamicDataSourceContextHolder.log;

/**
 * 充值渠道Controller
 *
 * @author ruoyi
 * @date 2024-04-15
 */
@RestController
@RequestMapping("/system/channel")
public class ChannelController extends BaseController
{
    @Autowired
    private IChannelService channelService;

    /**
     * 查询充值渠道列表
     */
    @PreAuthorize("@ss.hasPermi('system:channel:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChannelQuery smChannel)
    {
        startPage();
        List<ChannelVO> list = channelService.selectSmChannelList(smChannel);
        return getDataTable(list);
    }

    /**
     * 查询充值渠道列表
     */
    @PreAuthorize("@ss.hasPermi('system:channel:listByIds')")
    @PostMapping("/listByIds")
    public AjaxResult listByIds(@RequestBody List<Long> ids)
    {
        List<ChannelVO> list = channelService.selectByIds(ids);
        return success(list);
    }

    /**
     * 导出充值渠道列表
     */
    @PreAuthorize("@ss.hasPermi('system:channel:export')")
    @Log(title = "充值渠道", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChannelQuery smChannel)
    {
        List<ChannelVO> list = channelService.selectSmChannelList(smChannel);
        ExcelUtil<ChannelVO> util = new ExcelUtil<ChannelVO>(ChannelVO.class);
        util.exportExcel(response, list, "充值渠道数据");
    }

    /**
     * 获取充值渠道详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:channel:query')")
    @GetMapping(value = "/{channelId}")
    public AjaxResult getInfo(@PathVariable("channelId") Long channelId)
    {
        return success(channelService.selectChannelByChannelId(channelId));
    }


    /**
     * 添加渠道
     */
    @PreAuthorize("@ss.hasPermi('system:channel:add')")
    @Log(title = "添加渠道", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Channel channel)
    {
        log.info("【添加渠道】：【{}】", JSON.toJSONString(channel));
        return toAjax(channelService.insertSmChannel(channel));
    }

    /**
     * 修改充值渠道
     */
    @PreAuthorize("@ss.hasPermi('system:channel:edit')")
    @Log(title = "充值渠道", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) Channel form)
    {
        Channel channel = new Channel();
        channel.setChannelId(form.getChannelId());
        channel.setEnabled(form.getEnabled());
        channel.setCostRate(form.getCostRate());
        channel.setPicture(form.getPicture());
        channel.setMerchantId(form.getMerchantId());
        channel.setApiV3Key(form.getApiV3Key());
        channel.setNotifyUrl(form.getNotifyUrl());
        channel.setPrivateKeyPath(form.getPrivateKeyPath());
        channel.setMerchantSerialNumber(form.getMerchantSerialNumber());
        channel.setRefundNotifyUrl(form.getRefundNotifyUrl());
        return toAjax(channelService.updateSmChannel(channel));
    }

}
