package com.xhpc.evcs.api;

import com.xhpc.evcs.domain.XhpcInternetUser;
import com.xhpc.evcs.domain.XhpcStationInternetBlacklist;
import com.xhpc.evcs.dto.CommonRequest;
import com.xhpc.evcs.dto.CommonResponse;
import com.xhpc.evcs.dto.EquipAuthRequest;
import com.xhpc.evcs.dto.EquipAuthResponse;
import com.xhpc.evcs.jpa.XhpcInternetUserRepository;
import com.xhpc.evcs.jpa.XhpcStationInternetBlacklistRepository;
import com.xhpc.evcs.utils.JSONUtil;
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.time.Instant;
import java.util.List;
import java.util.Map;

import static cn.hutool.core.util.NumberUtil.isInteger;
import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;

/**
 * program: ruoyi
 * User: HongYun
 * Date:2021-10-18 15
 */

@RestController
public class QueryEquipAuthController {

    @Autowired
    private XhpcStationInternetBlacklistRepository xhpcStationInternetBlacklistRepo;
    @Autowired
    private XhpcInternetUserRepository xhpcInternetUserRepository;

    @PostMapping(value = "/v1/query_equip_auth")
    public CommonResponse queryEquipAuth(@RequestBody CommonRequest<EquipAuthRequest> commonRequest) throws Exception {

        CommonResponse resp = new CommonResponse();
        EquipAuthResponse equipAuthResponse = new EquipAuthResponse();
        EquipAuthRequest equipAuthRequest = JSONUtil.readParams(commonRequest.getData(), EquipAuthRequest.class);
        String equipAuthSeq = equipAuthRequest.getEquipAuthSeq();
        String connectorId = equipAuthRequest.getConnectorId();
        equipAuthResponse.setEquipAuthSeq(equipAuthSeq);
        equipAuthResponse.setConnectorId(connectorId);
        String pileId = connectorId.substring(0, connectorId.length() - 2);
        Map<String, Object> pileDataModel = REDIS.getCacheMap("pile:" + pileId);
        Long stationId = Long.parseLong(pileDataModel.get("stationId").toString());
        List<XhpcInternetUser> xhpcInternetUserList =
                xhpcInternetUserRepository.findByCooperationStartTimeBeforeAndCooperationEndTimeAfterAndOperatorIdEvcsLike(Instant.now(),
                        Instant.now(), commonRequest.getOperatorId());
        if (xhpcInternetUserList.isEmpty()) {
            equipAuthResponse.setSuccStat(1);
            equipAuthResponse.setFailReason(2);
            resp.setRet("500");
            resp.setMsg("auth denied");
        } else {
            Long internetUserId = xhpcInternetUserList.get(0).getId();
            XhpcStationInternetBlacklist xhpcStationInternetBlacklist =
                    xhpcStationInternetBlacklistRepo.findByChargingStationIdAndInternetUserId(stationId, internetUserId).orElse(null);
            if (null != xhpcStationInternetBlacklist) {
                equipAuthResponse.setSuccStat(1);
                equipAuthResponse.setFailReason(2);
                resp.setRet("500");
                resp.setMsg("auth denied");
            } else {
                Map<String, Object> cacheGun = REDIS.getCacheMap("gun:".concat(connectorId));
                String terminalStatus = (String) cacheGun.get("vehicleGunStatus");
                String status = (String) cacheGun.get("status");
                if (!"空闲".equals(status)) {
                    resp.setMsg("终端不在空闲状态:[".concat(status == null ? "未注册" : isInteger(status) ? "充电中" : status).concat("]"));
                    resp.setRet("500");
                    equipAuthResponse.setSuccStat(1);
                    equipAuthResponse.setFailReason(1);
                } else if ("否".equals(terminalStatus)) {
                    resp.setMsg("未插枪");
                    resp.setRet("500");
                    equipAuthResponse.setSuccStat(1);
                    equipAuthResponse.setFailReason(1);
                } else {
                    resp.setRet("0");
                    resp.setMsg("success");
                    equipAuthResponse.setSuccStat(0);
                    equipAuthResponse.setFailReason(0);
                }

            }
        }
        resp.setData(JSONUtil.toJSONString(equipAuthResponse));
        return resp;
    }
}
