package com.xhpc.card.service.impl;

import com.xhpc.card.domain.*;
import com.xhpc.common.constant.ConstantClass;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.common.util.MyDateUtil;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.card.mapper.XhpcAppUserMapper;
import com.xhpc.card.mapper.XhpcChargingStationMapper;
import com.xhpc.card.mapper.XhpcHistoryOrderMapper;
import com.xhpc.card.mapper.XhpcRateTimeMapper;
import com.xhpc.card.service.XhpcDataBigScreenService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author WH
 * @date 2022/2/28 17:14
 * @since version-1.0
 */
@Service
public class XhpcDataBigScreenServiceImpl implements XhpcDataBigScreenService {

    @Resource
    XhpcChargingStationMapper xhpcChargingStationMapper;
    @Resource
    XhpcAppUserMapper xhpcAppUserMapper;
    @Resource
    XhpcHistoryOrderMapper xhpcHistoryOrderMapper;
    @Resource
    XhpcRateTimeMapper xhpcRateTimeMapper;
    @Resource
    private RedisService redisService;

    @Override
    public R<List<Map<String, Object>>> returnCoreLocation(CoreParam coreParam) {

        String tenantIdsStr = null;
        List<Map<String, Object>> xhpcChargingStationList = null;

        if (coreParam.getParamType() == null) {
            return R.fail("ParamType不能为null");
        }
        //judge care param function
        switch (coreParam.getParamType()) {
            case 0:
                tenantIdsStr = coreParam.getTenantIds();
                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndChargingStationId(tenantIdsStr, null);
                    return R.ok(xhpcChargingStationList);
                } else {
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndChargingStationId(null, null);
                    return R.ok(xhpcChargingStationList);
                }
            case 1:
                //query charging station infos of whole area
                Integer areaCode = coreParam.getAreaCode();
                tenantIdsStr = coreParam.getTenantIds();
                int number =0;
                if(areaCode !=null){
                    number = xhpcChargingStationMapper.getXhpcAreaLevel(areaCode);
                }
                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
                    return R.ok(xhpcChargingStationList);
                } else {
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(null, areaCode,number);
                    return R.ok(xhpcChargingStationList);
                }
            case 2:
                //query location info of special charging station of special tenant
                tenantIdsStr = coreParam.getTenantIds();
                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
                    String chargingStationIdStr = coreParam.getChargingStationIds();
                    long chargingStationId = Long.parseLong(chargingStationIdStr);
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndChargingStationId(tenantIdsStr, chargingStationId);
                    return R.ok(xhpcChargingStationList);
                } else {
                    return R.fail("传入的参数有误");
                }
            default:
                return R.fail("传入参数有误！");
        }
    }

    @Override
    public R returnCoreChargingStation(CoreParam coreParam) {
        String tenantIdsStr = coreParam.getTenantIds();
        if (coreParam.getParamType() == null) {
            return R.fail("ParamType不能为null");
        }
        Integer areaCode = coreParam.getAreaCode();
        int number =0;
        if(areaCode !=null){
            number = xhpcChargingStationMapper.getXhpcAreaLevel(areaCode);
        }
        String chargingStationIdStr = coreParam.getChargingStationIds();
        if(chargingStationIdStr==null || "".equals(chargingStationIdStr)){
            List<Map<String, Object>> list = xhpcChargingStationMapper.returnCoreChargingStation(tenantIdsStr, null, areaCode, number+1);
            Object[] objects = list.toArray();
            return R.ok(objects);
        }
        long chargingStationId = Long.parseLong(chargingStationIdStr);
        //judge care param function
        List<Map<String, Object>> list = xhpcChargingStationMapper.returnCoreChargingStation(tenantIdsStr, chargingStationId, areaCode, number+1);
        Object[] objects = list.toArray();
        return R.ok(objects);
    }

    @Override
    public R<Object> queryUserTotalCount(CoreParam coreParam) {

        String tenantIdsStr = null;
        List<Map<String, Object>> xhpcChargingStationList = null;

        Long appUserCount = xhpcAppUserMapper.selectTotalCountByTenantIdAndCurrentTime(tenantIdsStr, null);
        List<Long> chargingStationIdList = xhpcChargingStationMapper.selectChargingStationIds(tenantIdsStr);
        //（0 C端用户 1 流量方用户 2社区用户 3B端用户）
        Long communityUserCount = xhpcHistoryOrderMapper.xhpcCommunityPersonnelSum();
        Long customerUserCount = xhpcHistoryOrderMapper.xhpcCustomersPersonnelSum();
        return R.ok(appUserCount + communityUserCount + customerUserCount);

        //judge care param function
//        switch (coreParam.getParamType()) {
//            case 0:
//                tenantIdsStr = coreParam.getTenantIds();
//                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
//                    //get user count of c client
//                    Long appUserCount = xhpcAppUserMapper.selectTotalCountByTenantIdAndCurrentTime(tenantIdsStr, null);
//                    List<Long> chargingStationIdList = xhpcChargingStationMapper.selectChargingStationIds(tenantIdsStr);
//                    //（0 C端用户 1 流量方用户 2社区用户 3B端用户）
//                    Long communityUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.COMMUNIT_TYPE, null);
//                    Long customerUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.CUSTOMERS_TYPE, null);
//                    return R.ok(appUserCount + communityUserCount + customerUserCount);
//                } else {
//                    //get user count of c client
//                    Long appUserCount = xhpcAppUserMapper.selectTotalCountByTenantIdAndCurrentTime(tenantIdsStr, null);
//                    List<Long> chargingStationIdList = xhpcChargingStationMapper.selectChargingStationIds(tenantIdsStr);
//                    //（0 C端用户 1 流量方用户 2社区用户 3B端用户）
//                    Long communityUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.COMMUNIT_TYPE, null);
//                    Long customerUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.CUSTOMERS_TYPE, null);
//                    return R.ok(appUserCount + communityUserCount + customerUserCount);
//                }
//                //query charging station infos of whole area
//            case 1:
//                Integer areaCode = coreParam.getAreaCode();
//                tenantIdsStr = coreParam.getTenantIds();
//                int number =0;
//                if(areaCode !=null){
//                    number = xhpcChargingStationMapper.getXhpcAreaLevel(areaCode);
//                }
//                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
//                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
//                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
//                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
//                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
//                    }
//                    Long appUserCount = xhpcAppUserMapper.selectTotalCountByTenantIdAndCurrentTime(tenantIdsStr, null);
//                    Long communityUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.COMMUNIT_TYPE, null);
//                    Long customerUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.CUSTOMERS_TYPE, null);
//                    return R.ok(appUserCount + communityUserCount + customerUserCount);
//                } else {
//                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
//                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
//                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
//                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
//                    }
//                    Long appUserCount = xhpcAppUserMapper.selectTotalCountByTenantIdAndCurrentTime(tenantIdsStr, null);
//                    Long communityUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.COMMUNIT_TYPE, null);
//                    Long customerUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.CUSTOMERS_TYPE, null);
//                    return R.ok(appUserCount + communityUserCount + customerUserCount);
//                }
//            case 2:
//                //query location info of special charging station of special tenant
//                tenantIdsStr = coreParam.getTenantIds();
//                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
//                    String chargingStationIdStr = coreParam.getChargingStationIds();
//                    long chargingStationId = Long.parseLong(chargingStationIdStr);
//                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndChargingStationId(tenantIdsStr, chargingStationId);
//                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
//                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
//                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
//                    }
//                    Long appUserCount = xhpcAppUserMapper.selectTotalCountByTenantIdAndCurrentTime(tenantIdsStr, null);
//                    Long communityUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.COMMUNIT_TYPE, null);
//                    Long customerUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.CUSTOMERS_TYPE, null);
//                    return R.ok(appUserCount + communityUserCount + customerUserCount);
//                } else {
//                    return R.fail("传入的参数有误");
//                }
//            default:
//                R.fail("param type is invalid");
//                break;
//        }
//        return null;
    }

    @Override
    public R<Object> queryUserActivation(CoreParam coreParam) {

        String tenantIdsStr = null;
        String currentTime = MyDateUtil.getCurrentDateStrInYyyyMmDdHhMmSsFormat();

        //tenantIdsStr = coreParam.getTenantIds();
        Long appUserCount = xhpcAppUserMapper.selectTotalCountByTenantIdAndCurrentTime(tenantIdsStr, currentTime);
        List<Long> chargingStationIdList = new ArrayList<>();
        //（0 C端用户 1 流量方用户 2社区用户 3B端用户）
        //Long communityUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.COMMUNIT_TYPE, currentTime);
        //Long customerUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.CUSTOMERS_TYPE, currentTime);
        Long number = xhpcHistoryOrderMapper.countUserType(tenantIdsStr, chargingStationIdList, UserTypeUtil.CUSTOMERS_TYPE, currentTime);

        return R.ok(appUserCount + number);
        //judge care param function
//        switch (coreParam.getParamType()) {
//            case 0:
//                tenantIdsStr = coreParam.getTenantIds();
//                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
//                    //get user count of c client
//                    Long appUserCount = xhpcAppUserMapper.selectTotalCountByTenantIdAndCurrentTime(tenantIdsStr, currentTime);
//                    List<Long> chargingStationIdList = xhpcChargingStationMapper.selectChargingStationIds(tenantIdsStr);
//                    //（0 C端用户 1 流量方用户 2社区用户 3B端用户）
//                    Long communityUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.COMMUNIT_TYPE, currentTime);
//                    Long customerUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.CUSTOMERS_TYPE, currentTime);
//                    return R.ok(appUserCount + communityUserCount + customerUserCount);
//                } else {
//                    //get user count of c client
//                    Long appUserCount = xhpcAppUserMapper.selectTotalCountByTenantIdAndCurrentTime(tenantIdsStr, currentTime);
//                    List<Long> chargingStationIdList = xhpcChargingStationMapper.selectChargingStationIds(tenantIdsStr);
//                    //（0 C端用户 1 流量方用户 2社区用户 3B端用户）
//                    Long communityUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.COMMUNIT_TYPE, currentTime);
//                    Long customerUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.CUSTOMERS_TYPE, currentTime);
//                    return R.ok(appUserCount + communityUserCount + customerUserCount);
//                }
//                //query charging station infos of whole area
//            case 1:
//                Integer areaCode = coreParam.getAreaCode();
//                tenantIdsStr = coreParam.getTenantIds();
//                int number =0;
//                if(areaCode !=null){
//                    number = xhpcChargingStationMapper.getXhpcAreaLevel(areaCode);
//                }
//                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
//                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
//                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
//                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
//                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
//                    }
//                    Long appUserCount = xhpcAppUserMapper.selectTotalCountByTenantIdAndCurrentTime(tenantIdsStr, null);
//                    Long communityUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.COMMUNIT_TYPE, null);
//                    Long customerUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.CUSTOMERS_TYPE, null);
//                    return R.ok(appUserCount + communityUserCount + customerUserCount);
//                } else {
//                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
//                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
//                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
//                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
//                    }
//                    Long appUserCount = xhpcAppUserMapper.selectTotalCountByTenantIdAndCurrentTime(tenantIdsStr, null);
//                    Long communityUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.COMMUNIT_TYPE, null);
//                    Long customerUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.CUSTOMERS_TYPE, null);
//                    return R.ok(appUserCount + communityUserCount + customerUserCount);
//                }
//            case 2:
//                //query location info of special charging station of special tenant
//                tenantIdsStr = coreParam.getTenantIds();
//                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
//                    String chargingStationIdStr = coreParam.getChargingStationIds();
//                    long chargingStationId = Long.parseLong(chargingStationIdStr);
//                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndChargingStationId(tenantIdsStr, chargingStationId);
//                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
//                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
//                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
//                    }
//                    Long appUserCount = xhpcAppUserMapper.selectTotalCountByTenantIdAndCurrentTime(tenantIdsStr, null);
//                    Long communityUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.COMMUNIT_TYPE, null);
//                    Long customerUserCount = xhpcHistoryOrderMapper.selectUserTotalCountByTenantIdsAndChargingStationIdsAndUserTypeAndCurrentTime(tenantIdsStr, chargingStationIdList, UserTypeUtil.CUSTOMERS_TYPE, null);
//                    return R.ok(appUserCount + communityUserCount + customerUserCount);
//                } else {
//                    return R.fail("传入的参数有误");
//                }
//            default:
//                R.fail("param type is invalid");
//                break;
//        }
//        return null;
    }

    @Override
    public R<Object> queryElectricQuantity(CoreParam coreParam) {

        String tenantIdsStr = null;
        List<Map<String, Object>> xhpcChargingStationList = null;
        //judge care param function
        switch (coreParam.getParamType()) {
            case 0:
                tenantIdsStr = coreParam.getTenantIds();
                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
                    Long totalElectricQuantity = xhpcHistoryOrderMapper.selectTotalElectricQuantity(tenantIdsStr, null);
                    return R.ok(totalElectricQuantity);
                } else {
                    Long totalElectricQuantity = xhpcHistoryOrderMapper.selectTotalElectricQuantity(null, null);
                    return R.ok(totalElectricQuantity);
                }
                //query charging station infos of whole area
            case 1:
                Integer areaCode = coreParam.getAreaCode();
                tenantIdsStr = coreParam.getTenantIds();
                int number =0;
                if(areaCode !=null){
                    number = xhpcChargingStationMapper.getXhpcAreaLevel(areaCode);
                }
                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
                    }
                    Long totalElectricQuantity = xhpcHistoryOrderMapper.selectTotalElectricQuantity(tenantIdsStr, chargingStationIdList);
                    return R.ok(totalElectricQuantity);
                } else {
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
                    }
                    Long totalElectricQuantity = xhpcHistoryOrderMapper.selectTotalElectricQuantity(tenantIdsStr, chargingStationIdList);
                    return R.ok(totalElectricQuantity);
                }
            case 2:
                //query location info of special charging station of special tenant
                tenantIdsStr = coreParam.getTenantIds();
                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
                    String chargingStationIdStr = coreParam.getChargingStationIds();
                    long chargingStationId = Long.parseLong(chargingStationIdStr);
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndChargingStationId(tenantIdsStr, chargingStationId);
                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
                    }
                    Long totalElectricQuantity = xhpcHistoryOrderMapper.selectTotalElectricQuantity(tenantIdsStr, chargingStationIdList);
                    return R.ok(totalElectricQuantity);
                } else {
                    return R.fail("传入的参数有误");
                }
            default:
                R.fail("param type is invalid");
                break;
        }
        return null;
    }

    @Override
    public R<OrderRatio> queryOrderRatio(CoreParam coreParam) {

        String tenantIdsStr = null;
        List<Map<String, Object>> xhpcChargingStationList = null;
        //judge care param function
        switch (coreParam.getParamType()) {
            case 0:
                tenantIdsStr = coreParam.getTenantIds();
                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
                    OrderRatio orderRatio = getOrderRatio(tenantIdsStr, null);
                    return R.ok(orderRatio);
                } else {
                    OrderRatio orderRatio = getOrderRatio(null, null);
                    return R.ok(orderRatio);
                }
                //query charging station infos of whole area
            case 1:
                Integer areaCode = coreParam.getAreaCode();
                tenantIdsStr = coreParam.getTenantIds();
                int number =0;
                if(areaCode !=null){
                    number = xhpcChargingStationMapper.getXhpcAreaLevel(areaCode);
                }
                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
                    }
                    OrderRatio orderRatio = getOrderRatio(tenantIdsStr, chargingStationIdList);
                    return R.ok(orderRatio);
                } else {
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
                    }
                    OrderRatio orderRatio = getOrderRatio(tenantIdsStr, chargingStationIdList);
                    return R.ok(orderRatio);
                }
            case 2:
                //query location info of special charging station of special tenant
                tenantIdsStr = coreParam.getTenantIds();
                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
                    String chargingStationIdStr = coreParam.getChargingStationIds();
                    long chargingStationId = Long.parseLong(chargingStationIdStr);
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndChargingStationId(tenantIdsStr, chargingStationId);
                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
                    }
                    OrderRatio orderRatio = getOrderRatio(tenantIdsStr, chargingStationIdList);
                    return R.ok(orderRatio);
                } else {
                    return R.fail("传入的参数有误");
                }
            default:
                R.fail("param type is invalid");
                break;
        }
        return null;
    }

    /**
     * 枪使用率
     *
     * @param coreParam
     * @return
     */
    @Override
    public R queryTerminalGun(CoreParam coreParam) {
        String tenantIdsStr = null;
        List<Map<String, Object>> xhpcChargingStationList = null;
        Map<String, Object> map =new HashMap<>();
        ArrayList<Long> chargingStationIdList = new ArrayList<>();
        //judge care param function
        switch (coreParam.getParamType()) {
            case 0://全国
                tenantIdsStr = coreParam.getTenantIds();
                return getMapR(chargingStationIdList,tenantIdsStr, map);
            //query charging station infos of whole area
            case 1://省市区
                Integer areaCode = coreParam.getAreaCode();
                tenantIdsStr = coreParam.getTenantIds();
                int number =0;
                if(areaCode !=null){
                    number = xhpcChargingStationMapper.getXhpcAreaLevel(areaCode);
                }
                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
                    }
                } else {
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
                    }
                }
                return getMapR(chargingStationIdList,tenantIdsStr, map);
            case 2://场站
                //query location info of special charging station of special tenant
                tenantIdsStr = coreParam.getTenantIds();
                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
                    String chargingStationIdStr = coreParam.getChargingStationIds();
                    long chargingStationId = Long.parseLong(chargingStationIdStr);
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndChargingStationId(tenantIdsStr, chargingStationId);
                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
                    }
                    return getMapR(chargingStationIdList,tenantIdsStr, map);
                } else {
                    return R.fail("传入的参数有误");
                }
            default:
                R.fail("param type is invalid");
                break;
        }
        return R.ok();
    }

    private R<Map<String, Object>> getMapR(List<Long> chargingStationIdList,String tenantIdsStr, Map<String, Object> map) {
        List<String> terminal = xhpcChargingStationMapper.getTerminal(chargingStationIdList, tenantIdsStr);
        int total =terminal.size();//离线
        int offLine =0;//离线
        int fault =0;//故障
        int freeTime =0;//空闲
        int insertGun =0;//插枪
        int charge =0;//充电
        int unknown =0;//未知
        if(terminal !=null && terminal.size()>0){
            for (String st:terminal) {
                Map<String, Object> cacheMap = redisService.getCacheMap("gun:" + st);
                if(!cacheMap.isEmpty()){
                    if (cacheMap.containsKey("status") &&
                            "离线".equals(cacheMap.get("status").toString())){
                        offLine++;
                    }else if(cacheMap.containsKey("status") &&
                            "故障".equals(cacheMap.get("status").toString())){
                        fault++;
                    }else if("空闲".equals(cacheMap.get("status").toString()) || "插枪".equals(cacheMap.get("status").toString())){
                        if("是".equals(cacheMap.get("vehicleGunStatus").toString())){
                            insertGun++;
                        }else{
                            freeTime++;
                        }
                    }else{
                        charge++;
                    }
                }else{
                    unknown++;
                }
            }
        }
        map.put("offLine",offLine);
        map.put("fault",fault);
        map.put("freeTime",freeTime);
        map.put("charge",charge);
        map.put("unknown",unknown);
        map.put("total",total);
        map.put("insertGun",insertGun);
        return R.ok(map);
    }

    @Override
    public R<RealtimeOrders> queryRealtimeOrders(CoreParam coreParam) {

        String tenantIdsStr = null;
        List<Map<String, Object>> xhpcChargingStationList = null;
        List<RealtimeOrder> realtimeOrderList =new ArrayList<>();
        RealtimeOrders realtimeOrders = new RealtimeOrders();
        //judge care param function
        switch (coreParam.getParamType()) {
            case 0:
                tenantIdsStr = coreParam.getTenantIds();
                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
                    realtimeOrderList = xhpcRateTimeMapper.selectByTenantIdsAndCharingStationIds(tenantIdsStr, null,null);
                } else {
                    realtimeOrderList = xhpcRateTimeMapper.selectByTenantIdsAndCharingStationIds(tenantIdsStr, null,null);
                }
                realtimeOrders.setTotalItems(0L);
                if (realtimeOrderList.size()==0) {
                    realtimeOrders.setData(new ArrayList<>());
                    return R.ok(realtimeOrders);
                }
                realtimeOrders.setData(realtimeOrderList);
                realtimeOrders.setTotalItems((long) realtimeOrderList.size());
                return R.ok(realtimeOrders);
            case 1:
                Integer areaCode = coreParam.getAreaCode();
                tenantIdsStr = coreParam.getTenantIds();
                int number =0;
                if(areaCode !=null){
                    number = xhpcChargingStationMapper.getXhpcAreaLevel(areaCode);
                }
                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
                    }
                    realtimeOrderList = xhpcRateTimeMapper.selectByTenantIdsAndCharingStationIds(tenantIdsStr, chargingStationIdList,null);
                } else {
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
                    }
                   realtimeOrderList = xhpcRateTimeMapper.selectByTenantIdsAndCharingStationIds(tenantIdsStr, chargingStationIdList,null);
                }
                realtimeOrders.setTotalItems(0L);
                if (realtimeOrderList.size()==0) {
                    realtimeOrders.setData(new ArrayList<>());
                    return R.ok(realtimeOrders);
                }
                realtimeOrders.setData(realtimeOrderList);
                realtimeOrders.setTotalItems((long) realtimeOrderList.size());
                return R.ok(realtimeOrders);
            case 2:
                //query location info of special charging station of special tenant
                tenantIdsStr = coreParam.getTenantIds();

                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
                    long chargingStationId = Long.parseLong(coreParam.getChargingStationIds());
                    realtimeOrderList = xhpcRateTimeMapper.selectByTenantIdsAndCharingStationIds(tenantIdsStr,null,chargingStationId);
                    realtimeOrders.setTotalItems(0L);
                    if (realtimeOrderList.size()==0) {
                        realtimeOrders.setData(new ArrayList<>());
                        return R.ok(realtimeOrders);
                    }
                    realtimeOrders.setData(realtimeOrderList);
                    realtimeOrders.setTotalItems((long) realtimeOrderList.size());
                    return R.ok(realtimeOrders);
                } else {
                    return R.fail("传入的参数有误");
                }
            default:
                return R.fail("param type is invalid");
        }
    }

    @Override
    public R<OrderTrend> queryOrderTrend(CoreParam coreParam) {

        String tenantIdsStr = null;
        List<Map<String, Object>> xhpcChargingStationList = null;
        //judge care param function
        switch (coreParam.getParamType()) {
            case 0:
                tenantIdsStr = coreParam.getTenantIds();
                return getOrderTrend(tenantIdsStr, null);
            case 1:
                Integer areaCode = coreParam.getAreaCode();
                tenantIdsStr = coreParam.getTenantIds();
                int number =0;
                if(areaCode !=null){
                    number = xhpcChargingStationMapper.getXhpcAreaLevel(areaCode);
                }
                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
                    }
                    return getOrderTrend(tenantIdsStr, chargingStationIdList);
                } else {
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
                    }
                    return getOrderTrend(tenantIdsStr, chargingStationIdList);
                }
            case 2:
                //query location info of special charging station of special tenant
                tenantIdsStr = coreParam.getTenantIds();
                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
                    String chargingStationIdStr = coreParam.getChargingStationIds();
                    long chargingStationId = Long.parseLong(chargingStationIdStr);
                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndChargingStationId(tenantIdsStr, chargingStationId);
                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
                    }
                    return getOrderTrend(tenantIdsStr, chargingStationIdList);
                } else {
                    return R.fail("传入的参数有误");
                }
            default:
                return R.fail("param type is invalid");
        }
    }

    @Override
    public R queryWorkOrder(CoreParam coreParam) {
        String tenantIdsStr = null;
        List<Map<String, Object>> xhpcChargingStationList = null;
        return R.ok(xhpcChargingStationMapper.getWorkOrderTerminal(null,null));
        //judge care param function
//        switch (coreParam.getParamType()) {
//            case 0:
//                tenantIdsStr = coreParam.getTenantIds();
//                return R.ok(xhpcChargingStationMapper.getWorkOrderTerminal(null,tenantIdsStr));
//                //query charging station infos of whole area
//            case 1:
//                Integer areaCode = coreParam.getAreaCode();
//                tenantIdsStr = coreParam.getTenantIds();
//                int number =0;
//                if(areaCode !=null){
//                    number = xhpcChargingStationMapper.getXhpcAreaLevel(areaCode);
//                }
//                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
//                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
//                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
//                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
//                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
//                    }
//                    return R.ok(xhpcChargingStationMapper.getWorkOrderTerminal(chargingStationIdList,tenantIdsStr));
//                } else {
//                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndAreaCode(tenantIdsStr, areaCode,number);
//                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
//                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
//                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
//                    }
//                    return R.ok(xhpcChargingStationMapper.getWorkOrderTerminal(chargingStationIdList,tenantIdsStr));
//                }
//            case 2:
//                //query location info of special charging station of special tenant
//                tenantIdsStr = coreParam.getTenantIds();
//                if (!"".equals(tenantIdsStr) && tenantIdsStr != null) {
//                    String chargingStationIdStr = coreParam.getChargingStationIds();
//                    long chargingStationId = Long.parseLong(chargingStationIdStr);
//                    xhpcChargingStationList = xhpcChargingStationMapper.selectByTenantIdAndChargingStationId(tenantIdsStr, chargingStationId);
//                    ArrayList<Long> chargingStationIdList = new ArrayList<>();
//                    for (Map<String, Object> chargingStation : xhpcChargingStationList) {
//                        chargingStationIdList.add((Long) chargingStation.get(ConstantClass.CHARGING_STATION_ID));
//                    }
//                    return R.ok(xhpcChargingStationMapper.getWorkOrderTerminal(chargingStationIdList,tenantIdsStr));
//                } else {
//                    return R.fail("传入的参数有误");
//                }
//            default:
//                return R.fail("param type is invalid");
//        }
    }

    private R<OrderTrend> getOrderTrend(String tenantIdsStr, ArrayList<Long> chargingStationIdList) {

        List<Map<String, Object>> orderTrendList = xhpcHistoryOrderMapper.selectOrderTrendByTenantIdsAndChargingStationIds(tenantIdsStr, chargingStationIdList);
        OrderTrend orderTrend = new OrderTrend();
        orderTrend.setData(new ArrayList<>());
        orderTrend.setTotalItems(0L);
        if (orderTrendList.isEmpty()) {
            return R.ok(orderTrend);
        }
        for (Map<String, Object> orderTrendMap : orderTrendList) {
            OrderTrend.DataDTO dataDTO = new OrderTrend.DataDTO();
            dataDTO.setTime((String) orderTrendMap.get("current_time"));
            dataDTO.setOrderNumber((Long) orderTrendMap.get("number"));
            dataDTO.setActPrice(new BigDecimal(orderTrendMap.get("actPrice").toString()));
            orderTrend.getData().add(dataDTO);
        }
        orderTrend.setTotalItems((long) orderTrendList.size());
        return R.ok(orderTrend);
    }

    /**
     * 获取实时订单数据
     *
     * @author WH
     * @date 2022/3/17 15:46
     * @since version-1.0
     */
    private R<RealtimeOrders> getRealtimeOrders(List<Map<String, Object>> realtimeOrderList) {
        //init wrapper class object,set default value
        RealtimeOrders realtimeOrders = new RealtimeOrders();
//        ArrayList<RealtimeOrders.RealtimeOrder> realtimeOrderWrapperList = new ArrayList<>();
//        realtimeOrders.setData(realtimeOrderWrapperList);
//        realtimeOrders.setTotalItems(0L);
//        if (realtimeOrderList.isEmpty()) {
//            return R.ok(realtimeOrders);
//        }
//        realtimeOrders.setTotalItems((long) realtimeOrderList.size());
//        for (Map<String, Object> realtimeOrderMap : realtimeOrderList) {
//            RealtimeOrders.RealtimeOrder realtimeOrder = new RealtimeOrders.RealtimeOrder();
//            realtimeOrder.setUserType((String) realtimeOrderMap.get("chargingMode"));
//            realtimeOrder.setStartChargingTime((String) realtimeOrderMap.get("startTime"));
//            realtimeOrder.setChargingTime((String) realtimeOrderMap.get("chargingTime"));
//            realtimeOrder.setChargedElectric((Double) realtimeOrderMap.get("chargingDegree"));
//            realtimeOrder.setChargingSum((Double) realtimeOrderMap.get("amountCharged"));
//            realtimeOrder.setElectricCurrent((Double) realtimeOrderMap.get("electricCurrent"));
//            realtimeOrder.setVoltage((Double) realtimeOrderMap.get("voltage"));
//            realtimeOrder.setSoc((String) realtimeOrderMap.get("soc"));
//            realtimeOrderWrapperList.add(realtimeOrder);
//        }
        return R.ok(realtimeOrders);
    }

    /**
     * 获取订单来源比例方法
     *
     * @param chargingStationIdList 电站id集合
     * @author WH
     * @date 2022/3/15 17:08
     * @since version-1.0
     */
    private OrderRatio getOrderRatio(String tenantIdsStr, List<Long> chargingStationIdList) {

        Long totalCount = xhpcHistoryOrderMapper.selectCountByTenantIdsAndChargingStationIds(tenantIdsStr, chargingStationIdList);
        OrderRatio orderRatio = new OrderRatio();
        orderRatio.setTotalCount(totalCount);
        Long stClientNumber = xhpcHistoryOrderMapper.selectOrderCountBy(tenantIdsStr, chargingStationIdList, UserTypeUtil.COMMUNIT_TYPE, null);
        double stClientNumberDouble = stClientNumber.doubleValue();
        long stClientRatio = (long) (stClientNumberDouble / totalCount * 100);
        orderRatio.setStClientRatio(stClientRatio);
        Long bClientNumber = xhpcHistoryOrderMapper.selectOrderCountBy(tenantIdsStr, chargingStationIdList, UserTypeUtil.CUSTOMERS_TYPE, null);
        double bClientNumberDouble = bClientNumber.doubleValue();
        long bClientRatio = (long) (bClientNumberDouble / totalCount * 100);
        orderRatio.setBClientRatio(bClientRatio);
        Long kuaiDianNumber = xhpcHistoryOrderMapper.selectOrderCountBy(tenantIdsStr, chargingStationIdList, UserTypeUtil.INTERNET_TYPE, ConstantClass.KUAI_DIAN_EVCS);
        double kuaiDianNumberDouble = kuaiDianNumber.doubleValue();
        long kuaiDianRatio = (long) (kuaiDianNumberDouble / totalCount * 100);
        orderRatio.setKuaiDianRatio(kuaiDianRatio);
        Long hengDaNumber = xhpcHistoryOrderMapper.selectOrderCountBy(tenantIdsStr, chargingStationIdList, UserTypeUtil.INTERNET_TYPE, ConstantClass.HENG_DA_EVCS);
        double hengDaNumberDouble = hengDaNumber.doubleValue();
        long hengDaRatio = (long) (hengDaNumberDouble / totalCount * 100);
        orderRatio.setHengDaRatio(hengDaRatio);
        Long xinDianTuNumber = xhpcHistoryOrderMapper.selectOrderCountBy(tenantIdsStr, chargingStationIdList, UserTypeUtil.INTERNET_TYPE, ConstantClass.XIN_DIAN_TU_EVCS);
        double xinDianTuNumberDouble = xinDianTuNumber.doubleValue();
        long xinDianTuRatio = (long) (xinDianTuNumberDouble / totalCount * 100);
        orderRatio.setXinDianTuRatio(xinDianTuRatio);
        Long xiaoJuNumber = xhpcHistoryOrderMapper.selectOrderCountBy(tenantIdsStr, chargingStationIdList, UserTypeUtil.INTERNET_TYPE, ConstantClass.XIAO_JU_EVCS);
        double xiaoJuNumberDouble = xiaoJuNumber.doubleValue();
        long xiaoJuRatio = (long) (xiaoJuNumberDouble / totalCount * 100);
        orderRatio.setXiaoJuRatio(xiaoJuRatio);
        //为了防止转换成百分比导致的进度丢失问题，故C端用户直接算出来，而不是查出来
        long cClientRatio = 100 - xiaoJuRatio - kuaiDianRatio - xinDianTuRatio - hengDaRatio - stClientRatio - bClientRatio;
        orderRatio.setCClientRatio(cClientRatio);
        return orderRatio;
    }

}
