package com.xhpc.evcs.api;

import com.xhpc.evcs.dto.CommonRequest;
import com.xhpc.evcs.dto.CommonResponse;
import com.xhpc.evcs.dto.NotificationStartChargeResultRequestData;
import com.xhpc.evcs.dto.NotificationStartStopChargeResultResponse;
import com.xhpc.evcs.encryption.EvcsConst;
import com.xhpc.evcs.jpa.XhpcHistoryOrderRepository;
import com.xhpc.evcs.utils.JSONUtil;
import lombok.extern.slf4j.Slf4j;
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;

@Slf4j
@RestController()
public class NotificationStartChargeResultController {

    @Autowired
    private XhpcHistoryOrderRepository xhpcHistoryOrderRepository;

    /**
     * 推送启动充电结果
     * @param commonRequest
     * @return
     * @throws IOException
     */
    @PostMapping("/v1/notification_start_charge_result")
    public CommonResponse notifyStartChargeResult(@RequestBody CommonRequest<NotificationStartChargeResultRequestData> commonRequest) throws IOException {

        String data = commonRequest.getData();
        NotificationStartChargeResultRequestData startChargeResultRequest = JSONUtil.readParams(data,
                NotificationStartChargeResultRequestData.class); // todo check NPE
        String startChargeSeq = startChargeResultRequest.getStartChargeSeq();
//		ChargeOrderInfo chargeOrderInfo = chargeOrderInfoRepository.findById(startChargeSeq).orElse(new ChargeOrderInfo()
//		.setStartChargeSeqAndReturn(startChargeSeq));
//		chargeOrderInfo.setStartChargeSeqStat(startChargeResultRequest.getStartChargeSeqStat());
//		chargeOrderInfo.setIdentCode(startChargeResultRequest.getIdentCode());
//		chargeOrderInfoRepository.save(chargeOrderInfo); todo
        CommonResponse response = new CommonResponse();
        response.setRet(EvcsConst.RET_SUCC);
        NotificationStartStopChargeResultResponse resp = new NotificationStartStopChargeResultResponse();
        resp.setSuccStat(0);
        resp.setFailReason(0);
        resp.setStartChargeSeq(startChargeResultRequest.getStartChargeSeq());
        response.setData(JSONUtil.toJSONString(resp));
        return response;
    }

}
