package com.jsowell.pile.service.impl;

import com.jsowell.common.constant.Constants;
import com.jsowell.common.core.domain.ykc.TransactionRecordsData;
import com.jsowell.common.util.DateUtils;
import com.jsowell.pile.domain.OrderBasicInfo;
import com.jsowell.pile.domain.PileBasicInfo;
import com.jsowell.pile.service.PileBasicInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import com.jsowell.pile.mapper.PersonalChargingRecordMapper;
import com.jsowell.pile.domain.PersonalChargingRecord;
import com.jsowell.pile.service.PersonalChargingRecordService;
@Service
public class PersonalChargingRecordServiceImpl implements PersonalChargingRecordService{

    @Resource
    private PersonalChargingRecordMapper personalChargingRecordMapper;

    @Autowired
    private PileBasicInfoService pileBasicInfoService;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return personalChargingRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(PersonalChargingRecord record) {
        return personalChargingRecordMapper.insert(record);
    }

    @Override
    public int insertOrUpdate(PersonalChargingRecord record) {
        return personalChargingRecordMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(PersonalChargingRecord record) {
        return personalChargingRecordMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int insertSelective(PersonalChargingRecord record) {
        return personalChargingRecordMapper.insertSelective(record);
    }

    @Override
    public PersonalChargingRecord selectByPrimaryKey(Integer id) {
        return personalChargingRecordMapper.selectByPrimaryKey(id);
    }

    public PersonalChargingRecord selectByTransactionCode(String transactionCode) {
        return personalChargingRecordMapper.selectByTransactionCode(transactionCode);
    }

    @Override
    public int updateByPrimaryKeySelective(PersonalChargingRecord record) {
        return personalChargingRecordMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(PersonalChargingRecord record) {
        return personalChargingRecordMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateBatch(List<PersonalChargingRecord> list) {
        return personalChargingRecordMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<PersonalChargingRecord> list) {
        return personalChargingRecordMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<PersonalChargingRecord> list) {
        return personalChargingRecordMapper.batchInsert(list);
    }

    /**
     * 收到交易记录时, 处理个人桩充电记录
     * @param data
     */
    @Override
    public void processPersonalChargingRecord(TransactionRecordsData data) {
        // 根据交易流水号查询，如果数据库没有就入库
        String transactionCode = data.getTransactionCode();
        PersonalChargingRecord chargingRecord = this.selectByTransactionCode(transactionCode);
        if (chargingRecord == null) {
            // 为空，数据保存到数据库
            chargingRecord = new PersonalChargingRecord();
        }

        PileBasicInfo pileBasicInfo = pileBasicInfoService.selectPileBasicInfoBySN(data.getPileSn());

        // 处理数据
        chargingRecord.setTransactionCode(transactionCode);
        chargingRecord.setStatus("6");
        chargingRecord.setMemberId("");
        chargingRecord.setStationId(pileBasicInfo.getStationId() + "");
        chargingRecord.setMerchantId(pileBasicInfo.getMerchantId() + "");
        chargingRecord.setPileSn(data.getPileSn());
        chargingRecord.setConnectorCode(data.getConnectorCode());
        chargingRecord.setPileConnectorCode(chargingRecord.getPileSn() + chargingRecord.getConnectorCode());
        chargingRecord.setLogicCard(data.getLogicCard());
        chargingRecord.setVinCode(data.getVinCode());
        chargingRecord.setStartMode("");
        chargingRecord.setPlateNumber("");
        chargingRecord.setChargeStartTime(DateUtils.parseDate(data.getStartTime()));
        chargingRecord.setChargeEndTime(DateUtils.parseDate(data.getEndTime()));
        chargingRecord.setStartType(null);
        chargingRecord.setReservationStartTime(null);
        chargingRecord.setReservationEndTime(null);
        chargingRecord.setStartSoc(null);
        chargingRecord.setEndSoc(null);
        BigDecimal sharpUsedElectricity = new BigDecimal(data.getSharpUsedElectricity());
        chargingRecord.setSharpUsedElectricity(sharpUsedElectricity);
        BigDecimal peakUsedElectricity = new BigDecimal(data.getPeakUsedElectricity());
        chargingRecord.setPeakUsedElectricity(peakUsedElectricity);
        BigDecimal flatUsedElectricity = new BigDecimal(data.getFlatUsedElectricity());
        chargingRecord.setFlatUsedElectricity(flatUsedElectricity);
        BigDecimal valleyUsedElectricity = new BigDecimal(data.getValleyUsedElectricity());
        chargingRecord.setValleyUsedElectricity(valleyUsedElectricity);
        BigDecimal totalUsedElectricity = sharpUsedElectricity.add(peakUsedElectricity).add(flatUsedElectricity).add(valleyUsedElectricity);
        chargingRecord.setTotalUsedElectricity(totalUsedElectricity);
        chargingRecord.setReason(data.getStopReasonMsg());
        // 创建或更新
        personalChargingRecordMapper.insertOrUpdateSelective(chargingRecord);
    }

    /**
     * 一键启动时, 处理个人桩充电记录
     * @param orderBasicInfo
     */
    @Override
    public void processPersonalChargingRecord(OrderBasicInfo orderBasicInfo) {
        // 根据交易流水号查询，如果数据库没有就入库
        PersonalChargingRecord chargingRecord = new PersonalChargingRecord();

        // 处理数据
        chargingRecord.setTransactionCode(orderBasicInfo.getTransactionCode());
        chargingRecord.setStatus(Constants.ONE);
        chargingRecord.setMemberId(orderBasicInfo.getMemberId());
        chargingRecord.setStationId(orderBasicInfo.getStationId());
        chargingRecord.setMerchantId(orderBasicInfo.getMerchantId());
        chargingRecord.setPileSn(orderBasicInfo.getPileSn());
        chargingRecord.setConnectorCode(orderBasicInfo.getConnectorCode());
        chargingRecord.setPileConnectorCode(orderBasicInfo.getPileConnectorCode());
        chargingRecord.setLogicCard(orderBasicInfo.getLogicCard());
        chargingRecord.setVinCode(orderBasicInfo.getVinCode());
        chargingRecord.setStartMode(orderBasicInfo.getStartMode());
        chargingRecord.setPlateNumber(orderBasicInfo.getPlateNumber());
        chargingRecord.setChargeStartTime(orderBasicInfo.getChargeStartTime());
        chargingRecord.setChargeEndTime(orderBasicInfo.getChargeEndTime());
        chargingRecord.setStartType(orderBasicInfo.getStartType());
        chargingRecord.setReservationStartTime(orderBasicInfo.getReservedStartTime());
        chargingRecord.setReservationEndTime(orderBasicInfo.getReservedEndTime());
        chargingRecord.setStartSoc(orderBasicInfo.getStartSoc());
        chargingRecord.setEndSoc(null);
        // BigDecimal sharpUsedElectricity = new BigDecimal(data.getSharpUsedElectricity());
        // chargingRecord.setSharpUsedElectricity(sharpUsedElectricity);
        // BigDecimal peakUsedElectricity = new BigDecimal(data.getPeakUsedElectricity());
        // chargingRecord.setPeakUsedElectricity(peakUsedElectricity);
        // BigDecimal flatUsedElectricity = new BigDecimal(data.getFlatUsedElectricity());
        // chargingRecord.setFlatUsedElectricity(flatUsedElectricity);
        // BigDecimal valleyUsedElectricity = new BigDecimal(data.getValleyUsedElectricity());
        // chargingRecord.setValleyUsedElectricity(valleyUsedElectricity);
        // BigDecimal totalUsedElectricity = sharpUsedElectricity.add(peakUsedElectricity).add(flatUsedElectricity).add(valleyUsedElectricity);
        // chargingRecord.setTotalUsedElectricity(totalUsedElectricity);
        // chargingRecord.setReason(data.getStopReasonMsg());
        // 创建或更新
        personalChargingRecordMapper.insertOrUpdateSelective(chargingRecord);
    }

}
