package com.xhpc.evcs.cdjgpc.api;

import com.xhpc.evcs.dto.CommonRequest;
import com.xhpc.evcs.dto.CommonResponse;
import com.xhpc.evcs.dto.NotificationStartStopChargeResultResponse;
import com.xhpc.evcs.dto.NotificationStopChargeResultRequestData;
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;

/**
 * @author yuyang
 * @date 2023-09-24 19:13
 */
@Slf4j
@RestController()
public class CDNotificationStopChargeResultController {
    @Autowired
    private XhpcHistoryOrderRepository xhpcHistoryOrderRepository;

    /**
     * 推送停止充电结果
     * @param commonRequest
     * @return
     * @throws IOException
     */
    @PostMapping("/v10/notification_stop_charge_result")
    public CommonResponse notifyStartChargeResult(@RequestBody CommonRequest<NotificationStopChargeResultRequestData> commonRequest) throws IOException {

        String data = commonRequest.getData();
        NotificationStopChargeResultRequestData stopChargeResultRequestData = JSONUtil.readParams(data,
                NotificationStopChargeResultRequestData.class);
        String startChargeSeq = stopChargeResultRequestData.getStartChargeSeq();
//        ChargeOrderInfo chargeOrderInfo = chargeOrderInfoRepository.findById(startChargeSeq).orElse(new ChargeOrderInfo()
//        .setStartChargeSeqAndReturn(startChargeSeq));
//        chargeOrderInfo.setStartChargeSeqStat(stopChargeResultRequestData.getStartChargeSeqStat());
//        chargeOrderInfo.setFailReason(stopChargeResultRequestData.getFailReason());
//        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(stopChargeResultRequestData.getStartChargeSeq());
        response.setData(JSONUtil.toJSONString(resp));
        return response;
    }
}
