package cn.qg.qaplatform.controller.xyqb;

import cn.qg.qaplatform.utils.JsonResult;
import cn.qg.qaplatform.enums.ChannelEnum;
import cn.qg.qaplatform.enums.FundEnum;
import cn.qg.qaplatform.entity.vo.XyqbApplyDataVo;
import cn.qg.qaplatform.entity.vo.XjfqGenProcessVo;
import cn.qg.qaplatform.service.xyqb.LexinDataService;
import cn.qg.qaplatform.service.xyqb.XyqbService;
import cn.qg.qaplatform.utils.EnumUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/xyqb/gen")
@Api(tags = "造数据相关接口")
public class XyqbGenController {

    @Autowired
    XyqbService xyqbService;

    @Autowired
    LexinDataService lexinDataService;

    @ApiOperation(value = "造数据接口")
    @PostMapping("/data")
    public JsonResult genLoanUserData(@RequestBody XyqbApplyDataVo xyqbApplyDataVo) throws Exception {
        boolean result = false;
        if (xyqbApplyDataVo.getChannel().equals(ChannelEnum.XYQB.getChannelId())) {
            result = xyqbService.genLoanUser(xyqbApplyDataVo);
        }
//        if (xyqbApplyDataVo.getChannel().equals(ChannelEnum.LEXIN.getChannelId())) {
//            result = lexinDataService.genLexinData(xyqbApplyDataVo);
//        }
        return JsonResult.success(result);
    }

    @ApiOperation(value = "获取所有支持的资方和渠道")
    @GetMapping("/supportChannelAndFund")
    public JsonResult getSupportedLoanUserData() {
        Map<String, List> map = new HashMap();
        List supportChannel = EnumUtils.enumToListMap(ChannelEnum.class);
        List supportFund = EnumUtils.enumToListMap(FundEnum.class);
        map.put("channelList", supportChannel);
        map.put("fundList", supportFund);
        return JsonResult.success(map);
    }

    @ApiOperation(value = "查询当前用户所处状态")
    @GetMapping("/userStatus")
    public JsonResult queryUserStatus(@RequestParam String namespace,
                                      @RequestParam String phoneNo) {
        XjfqGenProcessVo xjfqGenProcessVo = xyqbService.queryUserStatus(namespace, phoneNo);
        return JsonResult.success(xjfqGenProcessVo);
    }

    @GetMapping("/channelMappedFund")
    public JsonResult mapFund(@RequestParam Integer channel) {
        List<Map<String, Object>> supportFund = EnumUtils.enumToListMap(FundEnum.class);
        List<Map> resultFundList = new LinkedList<>();
        if (channel == 1) {
            for (Map<String, Object> temp: supportFund) {
                if (temp.get("fundId").equals(480) || temp.get("fundId").equals(1030) || temp.get("fundId").equals(1040)) {
                    resultFundList.add(temp);
                }
            }
        }
        if (channel == 159886) {
            for (Map<String, Object> temp: supportFund) {
                if (temp.get("fundId").equals(1010)) {
                    resultFundList.add(temp);
                }
            }
        }
        return JsonResult.success(resultFundList);
    }

    /**
     * 资方渠道映射
     * @param fundId 资方id
     * @return
     */
    @GetMapping("/fundMappedChannel")
    public JsonResult mapChannel(@RequestParam Integer fundId) {
        List<Map<String, Object>> supportChannel = EnumUtils.enumToListMap(ChannelEnum.class);
        List<Map> resultChannelList = new LinkedList<>();
        if (fundId == 480 || fundId == 1030  || fundId == 1040) {
            for (Map<String, Object> temp: supportChannel) {
                if (temp.get("channelId").equals(1)) {
                    resultChannelList.add(temp);
                }
            }
        }

        if (fundId == 1010) {
            for (Map<String, Object> temp: supportChannel) {
                if (temp.get("channelId").equals(159886)) {
                    resultChannelList.add(temp);
                }
            }
        }
        return JsonResult.success(resultChannelList);
    }

    /**
     * @param namespace 环境
     * @param fundCorpId 资方id
     * @return
     * @throws Exception
     */
    @GetMapping("/assign/fund")
    public JsonResult assignFund(String namespace, Integer fundCorpId) throws Exception {
        return JsonResult.success(xyqbService.assignFund(namespace, fundCorpId));
    }

}
