package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsMerPlatformNo;
import com.hfkj.entity.BsMerPlatformTerm;
import com.hfkj.model.ResponseData;
import com.hfkj.service.BsMerPlatformNoService;
import com.hfkj.service.BsMerPlatformTermService;
import com.hfkj.sysenum.PlatformTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import netscape.javascript.JSObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: BsMerPlatformNo
 * @author: HuRui
 * @date: 2023/5/23
 **/
@Controller
@Api(value = "商户渠道管理")
@RequestMapping(value = "/merPlatform")
public class BsMerPlatformNoController {

    private static Logger log = LoggerFactory.getLogger(BsMerLedgerController.class);

    @Resource
    private BsMerPlatformNoService merPlatformNoService;

    @RequestMapping(value="/getMerPlatformList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商户渠道平台列表")
    public ResponseData getMerPlatformList(@RequestParam(name = "merId", required = true) Long merId,
                                           @RequestParam(name = "platformType", required = false) Integer platformType) {
        try {

            Map<String, Object> param = new HashMap<>();
            param.put("merId", merId);
            param.put("platformType", platformType);
            return ResponseMsgUtil.success(merPlatformNoService.getMerPlatformNoList(param));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/switchPlatform",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "切换渠道")
    public ResponseData switchPlatform(@RequestBody JSONObject body) {
        try {

            if (body == null || body.getLong("merId") == null || body.getInteger("platformType") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.OPEN_API_REQ_PARAMS_ERR, "");
            }
            merPlatformNoService.switchPlatform(body.getLong("merId"),PlatformTypeEnum.getDataByNumber(body.getInteger("platformType")));
            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
