package cn.qg.qaplatform.zdata.service.xyqb.impl;

import cn.qg.qaplatform.zdata.enums.LoanStatus;
import cn.qg.qaplatform.enums.Namespace;
import cn.qg.qaplatform.zdata.mapper.XyqbLoanDataMapper;
import cn.qg.qaplatform.zdata.entity.XjfqGenProcessVo;
import cn.qg.qaplatform.zdata.entity.XjfqUserEntity;
import cn.qg.qaplatform.config.SwitchDataSource;
import cn.qg.qaplatform.zdata.service.xyqb.XyqbBasicQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class XyqbBasicQueryServiceImpl implements XyqbBasicQueryService {

    @Autowired
    XyqbLoanDataMapper xyqbLoanDataMapper;

    /**
     * 获取所有用户状态
     * @return 用户状态列表
     */
    @Override
    public List<Map<String, Object>> getAllUserLoanStatus() {
        List<Map<String, Object>> allUserLoanStatusList = new ArrayList<>();
        for (LoanStatus loanStatus:LoanStatus.values()) {
            Map<String, Object> tempMap = new HashMap<>();
            tempMap.put("value", loanStatus.getStatus());
            tempMap.put("label", loanStatus.getDesc());
            allUserLoanStatusList.add(tempMap);
        }
        return allUserLoanStatusList;
    }

    /**
     * 获取所有环境信息
     * @return 环境信息列表
     */
    @Override
    public List<Map<String, Object>> getAllEnvInfo() {
        List<Map<String, Object>> allEnvList = new ArrayList<>();
        for (Namespace namespace: Namespace.values()) {
            Map<String, Object> tempMap = new HashMap<>();
            tempMap.put("value", namespace.getKey());
            tempMap.put("label", namespace.getDesc());
            allEnvList.add(tempMap);
        }
        return allEnvList;
    }

    /**
     * 根据uuidList获取用户信息
     * @param uuidList
     * @return
     */
    @Override
    public List<XjfqUserEntity> getUserDataListByUuidList(String namespace, List<String> uuidList) {
        SwitchDataSource.dataSourceSwitch(namespace, "xyqb_user");
        return xyqbLoanDataMapper.getUserDataListByUuidList(uuidList);
    }

    /**
     * 获取未申请用户uuidList
     * @return
     */
    @Override
    public List<String> getUnApplyUserUuidList(String namespace, Integer channel) {
        SwitchDataSource.dataSourceSwitch(namespace, "business_flow");
        return xyqbLoanDataMapper.getUnApplyLoanUserUuidList(channel);
    }

    /**
     * 查看手机号是否在库里
     * @param phone
     * @return
     */
    @Override
    public boolean queryIfPhoneExist(String namespace, String phone) {
        SwitchDataSource.dataSourceSwitch(namespace, "xyqb_user");
        if (xyqbLoanDataMapper.queryIfPhoneExist(phone) != null) {
            return true;
        }
        return false;
    }

    /**
     * 根据审核状态获取用户Id列表
     * @param namespace
     * @param status 0-授信中，1-审核拒绝，2-审核成功
     * @return
     */
    @Override
    public List<String> getAuditStatusUserIdList(String namespace, Integer status, Integer channel) {
        SwitchDataSource.dataSourceSwitch(namespace, "xyqb");
        if (status == 0) {
            return xyqbLoanDataMapper.getAuditStatusUserIdList(0, channel);
        }
        if (status == 1) {
            return xyqbLoanDataMapper.getAuditStatusUserIdList(1, channel);
        }
        if (status == 2) {
            return xyqbLoanDataMapper.getAuditStatusUserIdList(2, channel);
        }
        return null;
    }

    /**
     * 审核通过未提现
     * @param namespace 环境
     * @param channel 渠道id
     * @param fundId 资方id
     * @return 用户id列表
     */
    @Override
    public List<String> queryAuthAndUnWithdraw(String namespace, Integer channel, Integer fundId) {
        SwitchDataSource.dataSourceSwitch(namespace, "xyqb");
        List<String> userIdList = xyqbLoanDataMapper.queryAuthAndUnWithdraw(channel, fundId);
        return userIdList;
    }

    /**
     * 根据userIdList获取用户信息列表
     *
     * @param userIdList
     * @return
     */
    @Override
    public List<XjfqUserEntity> getUserDataListByUserIdList(String namespace, List<String> userIdList) {
        SwitchDataSource.dataSourceSwitch(namespace, "xyqb_user");
        List<XjfqUserEntity> result = xyqbLoanDataMapper.getUserDataListByUserIdList(userIdList);
        return result;
    }

    /**
     * 获取不同放款状态的用户
     * @param namespace
     * @param status 0-放款失败，1-放款成功
     * @return
     */
    @Override
    public List<String> getLoanStatusUserIdList(String namespace, Integer status, Integer channel, Integer fundId) {
        SwitchDataSource.dataSourceSwitch(namespace, "xyqb");
        if (status == 0) {
            return xyqbLoanDataMapper.getLoanStatusUserIdList(23, channel, fundId);
        }
        if (status == 1) {
            return xyqbLoanDataMapper.getLoanStatusUserIdList(15, channel, fundId);
        }
        return null;
    }

    /**
     * 获取多订单用户列表
     * @param namespace
     * @return
     */
    @Override
    public List<String> getMultiOrderUserDataList(String namespace) {
        SwitchDataSource.dataSourceSwitch("fe", "workflow");
        return xyqbLoanDataMapper.getMultiOrderuuidList();
    }

    @Override
    public Integer getAssignFundIdByUserId(String namespace, String userId) {
        SwitchDataSource.dataSourceSwitch(namespace, "xyqb");
        return xyqbLoanDataMapper.getAssignFundIdByUserId(userId);
    }

    @Override
    public XjfqGenProcessVo getUserInfoByPhoneNo(String namespace, String phoneNo) {
        SwitchDataSource.dataSourceSwitch(namespace, "xyqb_user");
        return xyqbLoanDataMapper.getUserInfoByPhoneNo(phoneNo);
    }

    @Override
    public Integer getLoanProgressByUserId(String namespace, String userId) {
        SwitchDataSource.dataSourceSwitch(namespace, "xyqb");
        return xyqbLoanDataMapper.getLoanProgressByUserId(userId);
    }
}
