package com.ruoyi.web.controller.app;

import com.fasterxml.jackson.annotation.JsonView;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.JsonViewProfile;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.channel.domain.ChannelQuery;
import com.ruoyi.ss.channel.domain.ChannelVO;
import com.ruoyi.ss.channel.domain.enums.ChannelType;
import com.ruoyi.ss.channel.service.ChannelService;
import com.ruoyi.ss.user.domain.SmUserVO;
import com.ruoyi.ss.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wjh
 * 2024/4/15
 */
@RestController
@RequestMapping("/app/channel/recharge")
public class AppChannelController extends BaseController {

    @Autowired
    private ChannelService channelService;

    @Autowired
    private UserService userService;

    @ApiOperation("获取充值渠道列表")
    @JsonView(JsonViewProfile.App.class)
    @GetMapping("/enabledList")
    public AjaxResult getRechargeEnabledList(@RequestParam(required = false) Long mchId) {

        List<ChannelVO> channelList = new ArrayList<>();

        // 用户所属的应用
        Long appId = getLoginUser().getAppId();

        // 优先查询商户专属渠道
        SmUserVO mch = userService.selectSmUserByUserId(mchId);
        if (mch != null && CollectionUtils.isNotEmptyElement(mch.getChannelIds())) {
            ChannelQuery mchQuery = new ChannelQuery();
            mchQuery.setChannelIds(mch.getChannelIds());
            mchQuery.setAppId(appId);
            List<ChannelVO> mchChannelList = channelService.selectEnabledRechargeList(mchQuery);
            channelList.addAll(mchChannelList);
        }

        // 查询平台收款的渠道
        ChannelQuery query = new ChannelQuery();
        query.setType(ChannelType.PLATFORM.getType());
        query.setAppId(appId);
        List<ChannelVO> platformChannelList = channelService.selectEnabledRechargeList(query);
        channelList.addAll(platformChannelList);

        return success(channelList);
    }

}
