package com.xhpc.evcs.api;

import com.xhpc.common.data.redis.CacheRateModel;
import com.xhpc.common.data.redis.CacheRealtimeData;
import com.xhpc.evcs.domain.EtOrderMapping;
import com.xhpc.evcs.dto.ChargeInfoRequest;
import com.xhpc.evcs.dto.CommonRequest;
import com.xhpc.evcs.dto.CommonResponse;
import com.xhpc.evcs.dto.EquipChargeStatus;
import com.xhpc.evcs.encryption.EvcsConst;
import com.xhpc.evcs.http.ServerInternalException;
import com.xhpc.evcs.jpa.OrderMappingRepository;
import com.xhpc.evcs.jpa.XhpcHistoryOrderRepository;
import com.xhpc.evcs.utils.JSONUtil;
import com.xhpc.pp.utils.HexUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
import static com.xhpc.evcs.notification.NotificationEquipChargeStatusTask.calculateEm;

@RestController()
public class QueryEquipChargeStatusController {

    @Autowired
    XhpcHistoryOrderRepository xhpcHistoryOrderRepository;
    @Autowired
    OrderMappingRepository orderMappingRepository;

    @PostMapping("/v1/query_equip_charge_status")
    public CommonResponse queryEquipChargeStatus(@RequestBody CommonRequest<ChargeInfoRequest> commonRequest) throws IOException {
        //创建数据实体类
        EquipChargeStatus equipChargeStatus = new EquipChargeStatus();
        //充电订单号 三方传进来都是27位
        String data = commonRequest.getData();
        ChargeInfoRequest chargeInfoRequest = JSONUtil.readParams(data, ChargeInfoRequest.class);
        String startChargeSeq = chargeInfoRequest.getStartChargeSeq();
        equipChargeStatus.setStartChargeSeq(startChargeSeq);
        //充电订单状态
        EtOrderMapping etOrderMapping = orderMappingRepository.findByEvcsOrderNo(startChargeSeq).orElse(null);
        if (etOrderMapping == null) {
            throw new ServerInternalException("未查询到该订单编号数据");
        }
        String internalOrderNum = etOrderMapping.getXhOrderNo();
        Map<String, Object> order = REDIS.getCacheMap("order:" + internalOrderNum);
        String orderStatus = (String) order.get("status");
        int startChargeSeqStat = 0;
        switch (orderStatus) {
            case "启动中":
                startChargeSeqStat = 1;
                break;
            case "充电中":
                startChargeSeqStat = 2;
                break;
            case "停止中":
                startChargeSeqStat = 3;
                break;
            case "已结束":
                startChargeSeqStat = 4;
                break;
            default:
                startChargeSeqStat = 5;
                break;
        }
        equipChargeStatus.setStartChargeSeqStat(startChargeSeqStat);
        //充电设备接口编码
        String connectorId = internalOrderNum.substring(0, 16);
        equipChargeStatus.setConnectorID(connectorId);
        //充电设备接口状态
        Map<String, Object> cacheGunData = REDIS.getCacheMap("gun:" + connectorId);
        String gunStatus = (String) cacheGunData.get("status");
        int connectorStatus = 0;
        switch (gunStatus) {
            case "空闲":
                connectorStatus = 1;
                break;
            case "离线":
                connectorStatus = 2;
                break;
            case "故障":
                connectorStatus = 255;
                break;
            default:
                connectorStatus = 3;
                break;
        }
        equipChargeStatus.setConnectorStatus(connectorStatus);
        //A相电流
        String current = (String) cacheGunData.get("current");
        Double currentA = HexUtils.reverseHexInt(current) / 10.0;
        equipChargeStatus.setCurrentA(currentA);
        //A相电压
        String voltage = (String) cacheGunData.get("voltage");
        Double voltageA = HexUtils.reverseHexInt(voltage) / 10.0;
        equipChargeStatus.setVoltageA(voltageA);
        //电池剩余电量
        Integer endSoc = (Integer) order.get("endSoc");
        Double soc = Double.valueOf(endSoc == null ? 0 : endSoc);
        equipChargeStatus.setSoc(soc);
        //开始充电时间
        String startTime = (String) order.get("startTime");
        equipChargeStatus.setStartTime(startTime);
        //本次采样时间 直接new一个当前时间的Date就可以了
        CacheRealtimeData lord = REDIS.getCacheObject("order:" + internalOrderNum + ".lord");
        equipChargeStatus.setEndTime(lord.getCreateTime());
        Integer chargeModel = (Integer) order.get("chargeModel");
        equipChargeStatus.setChargeModel(chargeModel == null ? 0 : chargeModel);
        //累计充电量
        Double chargingDegree = Double.valueOf(lord.getChargingDegree() == null ? 0 : lord.getChargingDegree());
        equipChargeStatus.setTotalPower(chargingDegree / 10000);
        //总金额
        equipChargeStatus.setTotalMoney(lord.getAmountCharged() == null ? 0 : lord.getAmountCharged() / 10000.0);
        final Long rateModelId = REDIS.getCacheMapValue("pile:".concat(connectorId.substring(0, 14)), "rateModelId");
        final CacheRateModel cacheRateModel = REDIS.getCacheObject("rateModel:" + rateModelId);
        calculateEm(equipChargeStatus, cacheRateModel);
        CommonResponse response = new CommonResponse();
        response.setRet(EvcsConst.RET_SUCC);
        response.setData(JSONUtil.toJSONString(equipChargeStatus));
        response.setMsg("success");
        return response;
    }
}
