package cn.quant.baa.pay.rest;

import cn.quant.baa.pay.model.web.MerchantAcquirerProperties;
import cn.quant.baa.pay.model.web.MerchantChannelResponseData;
import cn.quant.baa.pay.model.web.MerchantChannelRequestData;
import cn.quant.baa.pay.annotation.BusinessMapping;
import cn.quant.baa.pay.context.TransactionSession;
import cn.quant.baa.pay.service.ProfileStorageService;
import cn.quant.baa.pay.util.AssertUtils;
import cn.quant.baa.pay.model.BusinessRequest;
import cn.quant.baa.pay.service.MerchantStorageService;
import cn.quant.spring.http.HttpResponseData;
import cn.quant.spring.rest.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static cn.quant.baa.pay.dict.MessageEnum.*;

/**
 * Created by Administrator on 2021/8/23 0023.
 */
@RestController
@RequestMapping("/merchant/channel")
public class MerchantController extends AbstractController {

    @Autowired
    private MerchantStorageService merchantStorageService;

    @Autowired
    private CacheManager cacheManager;

    @Autowired
    private ProfileStorageService profileStorageService;

    @ResponseBody
    @BusinessMapping(session = 1)
    @PostMapping("/list")
    public ResponseEntity list(@RequestBody BusinessRequest<MerchantChannelRequestData> request) throws Exception {
        TransactionSession session = TransactionSession.session();
        String requestId = session.getRequestId();

        MerchantChannelRequestData data = request.getData();
        if (data == null) {
            AssertUtils.throwMessage(ILLEGAL_REQ_DATA);
        }

        Map<String, MerchantChannelResponseData> channels = merchantStorageService.getChannels(data);
        ResponseEntity<HttpResponseData> entity = succeed(requestId, channels);
        return entity;
    }

    @ResponseBody
    @BusinessMapping(session = 1)
    @PostMapping("/one")
    public ResponseEntity one(@RequestBody BusinessRequest<MerchantChannelRequestData> request) throws Exception {

        TransactionSession session = TransactionSession.session();
        String requestId = session.getRequestId();

        MerchantChannelRequestData data = request.getData();
        if (data == null) {
            AssertUtils.throwMessage(ILLEGAL_REQ_DATA);
        }

        MerchantAcquirerProperties channel = merchantStorageService.getChannel(Long.parseLong(data.getChanId()));
        ResponseEntity<HttpResponseData> entity = succeed(requestId, channel);
        return entity;
    }
}
