package com.xhpc.order.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.web.service.BaseService;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.order.domain.XhpcChargingStationPower;
import com.xhpc.order.mapper.XhpcChargingStationPowerMapper;
import com.xhpc.order.service.IXhpcChargingStationPowerService;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author yuyang
 * @date 2023-06-28 14:19
 */
@Service
public class XhpcChargingStationPowerServiceImpl extends BaseService implements IXhpcChargingStationPowerService {

    @Resource
    TokenService tokenService;
    @Resource
    private XhpcChargingStationPowerMapper xhpcChargingStationPowerMapper;

    private final ExecutorService executorService = Executors.newFixedThreadPool(20);

    private final String [] enumeration ={"zero","one","two","three","four","five", "six","seven","eight","nine","ten","eleven",
            "twelve","thirteen","fourteen","fifteen","sixteen","seventeen", "eighteen","nineteen","twenty","twenty-one","twenty-two","twenty-three",
            "twenty-four","twenty-five","twenty-six","twenty-seven", "twenty-eight","twenty-nine","thirty","thirty-one","thirty-two","thirty-three","thirty-four","thirty-five",
            "thirty-six","thirty-seven","thirty-eight","thirty-nine","forty","forty-one","forty-two","forty-three","forty-four","forty-five","forty-six","forty-seven",
            "forty-eight","forty-nine","fifty","fifty-one","fifty-two","fifty-three","fifty-four","fifty-five","fifty-six","fifty-seven","fifty-eight","fifty-nine",
            "sixty","sixty-one","sixty-two","sixty-three","sixty-four","sixty-five","sixty-six","sixty-seven","sixty-eight","sixty-nine","seventy","seventy-one",
            "seventy-two","seventy-three","seventy-four","seventy-five","seventy-six","seventy-seven","seventy-eight","seventy-nine","eighty","eighty-one","eighty-two","eighty-three",
            "eighty-four","eighty-five","eighty-six","eighty-seven","eighty-eight","eighty-nine","ninety","ninety-one","ninety-two","ninety-three","ninety-four","ninety-five"};

    @Override
    public void addChargingStationPower() {

        //获取所有场站
        List<Map<String, Object>> chargingStationS = xhpcChargingStationPowerMapper.getChargingStationId();

        if(chargingStationS !=null && chargingStationS.size()>0){
            //获取时间
            String now = DateUtil.now();
            int hh = Integer.parseInt(now.substring(11,13));
            int mm = Integer.parseInt(now.substring(14,16));
            int dd = Integer.parseInt(now.substring(17,19));
            int numbrt =0;
            String sterTime = "";
            String endTime = "";
            String substr = now.substring(0,13);
            if(mm<15){
                numbrt = hh*4;
                sterTime = substr+":00:00";
                endTime = substr+":14:59";
            }else if(mm<30){
                numbrt = hh*4+1;
                sterTime = substr+":15:00";
                endTime = substr+":29:59";
            }else if(mm<45){
                numbrt = hh*4+2;
                sterTime = substr+":30:00";
                endTime = substr+":44:59";
            }else{
                numbrt = hh*4+3;
                sterTime = substr+":45:00";
                endTime = substr+":59:59";
            }
            for (int i = 0; i <chargingStationS.size() ; i++) {
                Map<String, Object> objectMap = chargingStationS.get(i);

                Long chargingStationId =Long.valueOf(objectMap.get("chargingStationId").toString());
                String tenantId = objectMap.get("tenantId").toString();
                //查询是否已统计（订单编号、时间段）
                String subTime = now.substring(0,10);
                String formatYu = cn.hutool.core.date.DateUtil.format(DateUtil.offsetDay(new Date(),-10), "yyyy-MM-dd HH:mm:ss");
                List<Map<String, Object>> xhpcTerminals = xhpcChargingStationPowerMapper.getXhpcTerminals(chargingStationId);

                for (int j = 0; j < xhpcTerminals.size(); j++) {

                    Map<String, Object> terminal = xhpcTerminals.get(j);
                    Long terminalId = Long.valueOf(terminal.get("terminalId").toString());
                    int determine = xhpcChargingStationPowerMapper.duplicateStatistics(chargingStationId, terminalId,numbrt, subTime,formatYu);
                    if(determine==0){
                        List<Map<String, Object>> xhpcRealTimeOrderList = xhpcChargingStationPowerMapper.getXhpcRealTimeOrderList(chargingStationId,terminalId, sterTime, endTime, numbrt,subTime);
                        if(xhpcRealTimeOrderList !=null && xhpcRealTimeOrderList.size()>0){
                            int finalNumbrt = numbrt;
                            executorService.execute(new Runnable() {
                                @Override
                                public void run() {
                                    for (int j = 0; j < xhpcRealTimeOrderList.size(); j++) {
                                        Map<String, Object> map = xhpcRealTimeOrderList.get(j);
                                        map.put("number", finalNumbrt);
                                        addXhpcChargingStationPower(map);
                                    }
                                }
                            });
                        }
                    }
                }
            }
        }
    }

    @Override
    public List<Map<String, Object>> getListPage(Map<String, Object> params) {

        //桩的统计、该时段金额
        List<Map<String, Object>> list = new ArrayList<>();
        //!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())
        if(false){
            Long logUserId = SecurityUtils.getUserId();
            LoginUser loginUser = tokenService.getLoginUser();
            SysUser sysUser = loginUser.getSysUser();
            startPage();
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                params.put("number",1);
                params.put("user",logOperatorId);
                //运营商看自己的场站
                list = xhpcChargingStationPowerMapper.getListPage(params);
            }else{
                params.put("number",2);
                params.put("user",logUserId);
                //查询赋值的场站
                list = xhpcChargingStationPowerMapper.getListPage(params);
            }
        }else{
            startPage();
            params.put("number",0);
            params.put("user",1);
            list =xhpcChargingStationPowerMapper.getListPage(params);
        }
        if(list !=null && list.size()>0){
            for (int i = 0; i <list.size() ; i++) {
                String[] shuzu ={"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47",
                        "48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95"};
                Map<String, Object> map = list.get(i);
                String status = map.get("status").toString();
                String[] split = status.split(",");
                String power = map.get("power").toString();
                String[] powerSplit = power.split(",");
                for (int j = 0; j <split.length ; j++) {
                    int time = Integer.parseInt(split[j]);
                    if(map.get(enumeration[time]) !=null){
                        Double aDouble = Double.valueOf(map.get(enumeration[time]).toString());
                        Double aDouble1 = Double.valueOf(powerSplit[j].toString());
                        double v = aDouble + aDouble1;
                        map.put(enumeration[time],new Formatter().format("%.2f", v).toString());
                    }else{
                        map.put(enumeration[time],new Formatter().format("%.2f", Double.valueOf(powerSplit[j].toString())).toString());
                    }
                    shuzu[time] ="0.00";
                }
                for (int j = 0; j < shuzu.length; j++) {
                    if(!shuzu[j].equals("0.00")){
                        map.put(enumeration[j],"0.00");
                    }
                }
            }
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> getChargingStationList() {
        return xhpcChargingStationPowerMapper.getChargingStationId();
    }

    @Override
    public List<Map<String, Object>> getMaxPowerList(Map<String, Object> params) {
        Long logUserId = SecurityUtils.getUserId();
        LoginUser loginUser = tokenService.getLoginUser();
        //桩的统计、该时段金额
        List<Map<String, Object>> list = new ArrayList<>();
        if (loginUser.getUserid() != UserTypeUtil.USER_ID) {
            Map<String, Object> landUser = xhpcChargingStationPowerMapper.getLandUser(logUserId);
            if (landUser != null) {
                if (landUser.get("userType") != null) {
                    startPage();
                    if (UserTypeUtil.SYS_USER_TYPE_ONE.equals(landUser.get("userType").toString())) {
                        Long logOperatorId = Long.valueOf(landUser.get("operatorId").toString());

                        params.put("status",1);
                        params.put("userId",logOperatorId);
                        startPage();
                        //运营商看自己的场站
                        list = xhpcChargingStationPowerMapper.getMaxPowerList(params);
                    } else {
                        params.put("status",2);
                        params.put("userId",logUserId);
                        startPage();
                        //查询赋值的场站
                        list = xhpcChargingStationPowerMapper.getMaxPowerList(params);
                    }
                }
            }
        } else {
            params.put("status",0);
            startPage();
            list = xhpcChargingStationPowerMapper.getMaxPowerList(params);
        }
        return list;
    }

    private void addXhpcChargingStationPower(Map<String, Object> map){
        XhpcChargingStationPower xhpcChargingStationPower =new XhpcChargingStationPower();
        if(map.get("chargeOrderId")!=null){
            xhpcChargingStationPower.setChargeOrderId(Long.valueOf(map.get("chargeOrderId").toString()));
        }
        if(map.get("number")!=null){
            xhpcChargingStationPower.setStatus(Integer.valueOf(map.get("number").toString()));
        }
        if(map.get("voltage")!=null){
            xhpcChargingStationPower.setVoltage(Double.valueOf(map.get("voltage").toString()));
        }
        if(map.get("electriccurrent")!=null){
            xhpcChargingStationPower.setCurrent(Double.valueOf(map.get("electriccurrent").toString()));
        }
        if(map.get("chargingStationId")!=null){
            xhpcChargingStationPower.setChargingStationId(Long.valueOf(map.get("chargingStationId").toString()));
        }
        if(map.get("chargingPileId")!=null){
            xhpcChargingStationPower.setChargingPileId(Long.valueOf(map.get("chargingPileId").toString()));
        }
        if(map.get("terminalId")!=null){
            xhpcChargingStationPower.setTerminalId(Long.valueOf(map.get("terminalId").toString()));
        }
        if(map.get("tenantId")!=null){
            xhpcChargingStationPower.setTenantId(map.get("tenantId").toString());
        }
        if(map.get("installedTotalPower")!=null){
            xhpcChargingStationPower.setInstalledTotalPower(Double.valueOf(map.get("installedTotalPower").toString()));
        }
        xhpcChargingStationPower.setCreateTime(new Date());
        xhpcChargingStationPowerMapper.addXhpcChargingStationPower(xhpcChargingStationPower);
    }


    private Map<String, Object> getStatistics(String time){
        Map<String, Object> map =new HashMap<>();
        map.put("chargingDegree","0.00");
        map.put("chargingTime","0.00");
        map.put("chargingNumber","0.00");
        map.put("powerPrice","0.00");
        map.put("servicePrice","0.00");
        map.put("totalPrice","0.00");
        map.put("activityPowerPriceTotal","0.00");
        map.put("activityServicePriceTotal","0.00");
        map.put("activityTotalPrice","0.00");
        map.put("promotionDiscount","0.00");
        map.put("actPrice","0.00");
        map.put("actPowerPrice","0.00");
        map.put("actServicePrice","0.00");
        map.put("internetCommission","0.00");
        map.put("internetSvcCommission","0.00");
        map.put("internetDegreeCommission","0.00");
        map.put("platformCommission","0.00");
        map.put("platformSvcCommisssion","0.00");
        map.put("operationCommission","0.00");
        map.put("operationSvcCommission","0.00");
        map.put("time",time);
        return map;
    }


    public static void main(String[] args) {
        String time ="2023-10-10 19:00:07";
        System.out.println("===="+time.substring(0,10));
    }

}
